【问题标题】:Reading a TTree in root using PyRoot使用 PyRoot 在根目录中读取 TTree
【发布时间】:2016-12-13 15:10:08
【问题描述】:

我刚开始使用 pyroot 读取根文件,但无法使用 jupyter notebook 从文件中读取数据。以下是 TBrowser 的外观:

我是这样开始的:

import ROOT as root
import numpy as np

f = root.TFile("RealData.root")
myTree = f.Get("tree")

entries = myTree.GetEntriesFast()

到目前为止,它正在工作,如果我打印条目,我会得到文件中正确数量的整数。但我不知道如何从树中读取实际数据(event_number、n_channels 等)。如果我尝试类似 myTree.eventsmyTree.event_number 的操作,内核将停止工作。我应该怎么做才能从树中读取数据?

【问题讨论】:

    标签: python root-framework pyroot


    【解决方案1】:

    通常使用 pyROOT,您可以执行以下操作:

    import ROOT as root
    import numpy as np
    
    f = root.TFile("RealData.root")
    myTree = f.Get("tree")
    for entry in myTree:         
         # Now you have acess to the leaves/branches of each entry in the tree, e.g.
         events = entry.events
    

    我对 jupyter 的工作原理知之甚少,无法知道这是否会导致任何特定问题。您是否尝试过仅使用常规 python 解释器运行相同的脚本?

    【讨论】:

    • 如果我这样做,我会收到此错误:TypeError: requested class 'ROOT::Event' does not exist
    • 听起来您缺少 Event 类的 ROOT 字典(我假设这是您使用的自定义类而不是核心根目录?)。您应该能够添加如下行:root.gInterpreter.GenerateDictionary("\path\to\Event.h") 以获取 ROOT 以即时生成字典。或者看wlav.web.cern.ch/wlav/pyroot/tpymyclass.html
    猜你喜欢
    • 1970-01-01
    • 2017-12-15
    • 2020-06-08
    • 2012-08-13
    • 2014-06-28
    • 2011-10-24
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    相关资源
    最近更新 更多