I am trying to fill a TH1F histogram with a lead within a tree
from ROOT import *
#-------------------------------------------------------------------------------
rootfile = 'AOD_1.root'
def main():
# read all entries and fill the histograms
c1 = TCanvas('c1', 'Histogram', 200, 10, 700, 900)
hpx = TH1F("hpx","DY MC Ntuple AOD_ele pt hist", 0, 100, 4200)
myfile = TFile.Open(rootfile)
#lldjNtuple.cd()
#treeshow = EventTree.Print()
mytree = myfile.Get("lldjNtuple/EventTree")
entries = mytree.GetEntries()
mytree.Print()
print(entries)
for i in range(entries):
#pt = mytree.
mytree.GetEntry(i)
#mytree.GetEntry(i)
#ept = getattr(mytree, 'AOD_elePt')
# print(mytree.AOD_elePt)
#mytree.GetLeaf("AOD_elePt")
#pt = event.AOD_elePt
hpx.Fill(AOD_elePt,1)
hpx.Fill(AOD_eleEta,1)
# hpx.Fill(ePt,1)
myfile.Close()
#hfile.Close()
Root file’s structure is
root->lldjNtuple directory - >EventTree (tree) -> Leaf of AOD_elePt and AOD_eleEta
I want to plot AOD_elePt in histogram, which should be simple, but all documents and postings are for C++.
I have to go through each entry since I have more codings to add-on, but at this moment, I wanted to confirm how I would fill in TH1F histogram using GetEntry method for my root file
How can I fill TH1F histogram with AOD_elePt for my root file’s structure?
Traceback (most recent call last):
File "2tree.py", line 36, in <module>
main()
File "2tree.py", line 27, in main
hpx.Fill(pt,1)
TypeError: none of the 3 overloaded methods succeeded. Full details:
int TH1::Fill(double x) =>
takes at most 1 arguments (2 given)
int TH1::Fill(const char* name, double w) =>
could not convert argument 1 (expected string or Unicode object, vector<float> found)
int TH1::Fill(double x, double w) =>
could not convert argument 1 (a float is required)
I see, it seems that AOD_elePt and AOD_eleEta are not floats, but vectors of floats. And you can’t fill the histogram with a vector.
Would this be closer to what you want?
for entry in mytree:
vec_pts = entry.AOD_elePt # vec_pts is a vector of floats
vec_etas = entry.AOD_eleEta # vec_etas is a vector of floats
for pt in vec_pts:
hpx.Fill(pt,1)
for eta in vec_etas:
hpx.Fill(eta,1)