python - 将 C++ 转换为 pyroot : How to get a file object from a file in pyroot?

标签 python c++ root-framework pyroot

  1. 我正在进行中微子研究,这需要我通过叠加直方图进行数据分析。我们正在使用 ROOT。我目前正在尝试将以下代码从 C++ 转换为 pyroot:

    #include "TFile.h"
    #include "TH1F.h"
    #include "TCanvas.h"
    #include "TString.h"
    void myscript()
    {
      //get a histogram named vtx_0 from the file 5A_data
      TFile* file = TFile::Open("5A_data");
      TH1F* hist = file->Get("vtx_0");
      TCanvas* canvas = new TCanvas("c1", "Dynamic Filling Example", 200, 10, 700,500);
      hist->Draw();
    }
    
  2. 这是我目前的代码,用 python 重写:

    from ROOT import TFile, TH1F, TCanvas, TString
    def myscript():
      #get vtx_0 from 5A_data
      TFile file1 = open("5A_data")
      TH1F hist = 
    
  3. 我对 Python 的接触有限。上面的 Python 代码主要是通过查看各种在线示例创建的,所以我什至不确定到目前为止我写的是否正确。

  4. 我最需要的,也是我一直无法在网上找到的,是如何将 C++ 中的以下行转换为 Python 中的等价行。

    TH1F* hist = file->Get("vtx_0");
    

如何做到这一点?

  1. 此外,如果您发现我目前编写的 Python 代码有任何问题,请告诉我哪里做错了以及我该如何解决。谢谢。

最佳答案

变量声明不需要类型,像这样的事情可能会让你开始:

from ROOT import TFile, TH1F, TCanvas, TString
def myscript():
    tf = TFile("5A_data")
    print(dir(tf))
    #tree = tf.Get("vtx_0")
    fo = tf.GetObject("vtx_0")
    print(dir(fo))
    for x in fo:
        print(x)
    import pdb;pdb.set_trace()

引用:

关于python - 将 C++ 转换为 pyroot : How to get a file object from a file in pyroot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51448012/

相关文章:

python - 添加到 numpy 字符串数组

c++ - 将字符串变量从 bash 脚本传递到根宏

c++ - 如何防止将 TTree 写入 TFile

c++ - 在直方图中仅绘制特定的 bin

python - GTK3 Python 更改单个按钮颜色

找不到索引时,Python list.index 抛出异常

c++ - 默认构造函数调用

c++ - 枚举与数组

c++ - 如何在另一种语言的随机函数中复制 C++ rand?

python - 在 Python 中高效地删除列表中的列表