1
  1. I am doing neutrino research that requires me to do data analysis by overlaying histograms. We are using ROOT. I am currently trying to convert the following code from C++ to 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. This is the code I have so far, re-written in python:

    from ROOT import TFile, TH1F, TCanvas, TString
    def myscript():
      #get vtx_0 from 5A_data
      TFile file1 = open("5A_data")
      TH1F hist = 
    
  3. I have had limited exposure to Python. The Python code above was created mostly from looking at various online examples, and so I am not even sure if what I have written up to this point is correct.

  4. What I am most in need of, and what I have been unable to find online, is how to covert the following line in C++ to its equivalent in Python.

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

How does one do this?

  1. In addition, if you see anything amiss with the Python code I have written so far, please tell me what I did wrong and how I might fix it. Thank you.
pseyfert
  • 3,263
  • 3
  • 21
  • 47
Kasey
  • 11
  • 2

1 Answers1

1

types are not needed for variable declaration, and something like this might get you started:

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()

referenced:

jmunsch
  • 22,771
  • 11
  • 93
  • 114