_ROOT Version:6.08
Platform:Ubuntu 16.04
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?
Thanks
@etejedor perhaps you can help here please?
Hi @trenta_coollime
Can you try this?
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)
mytree = myfile.Get("lldjNtuple/EventTree")
mytree.Print()
for entry in mytree:
pt = entry.AOD_elePt
eta = entry.AOD_eleEta
hpx.Fill(pt,1)
hpx.Fill(eta,1)
myfile.Close()
To. etejedor
- I get an error message
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)
which is what I had been getting before.
- I did EventTree.Print() for just more information
******************************************************************************
*Tree :EventTree : Event data *
*Entries : 15153 : Total = 46235915 bytes File Size = 14941157 *
* : : Tree compression factor = 3.09 *
******************************************************************************
*Br 0 :run : run/I *
*Entries : 15153 : Total Size= 61228 bytes File Size = 516 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 117.76 *
*............................................................................*
*Br 1 :event : event/L *
*Entries : 15153 : Total Size= 122014 bytes File Size = 32996 *
*Baskets : 4 : Basket Size= 32000 bytes Compression= 3.68 *
*............................................................................*
*Br 2 :lumis : lumis/I *
*Entries : 15153 : Total Size= 61240 bytes File Size = 1011 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 60.10 *
.
.
.
*............................................................................*
*Br 190 :nAODEle : nAODEle/I *
*Entries : 15153 : Total Size= 61252 bytes File Size = 8291 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 7.33 *
*............................................................................*
*Br 191 :AOD_elePt : vector<float> *
*Entries : 15153 : Total Size= 251386 bytes File Size = 91160 *
*Baskets : 10 : Basket Size= 32000 bytes Compression= 2.75 *
*............................................................................*
*Br 192 :AOD_eleEn : vector<float> *
*Entries : 15153 : Total Size= 251386 bytes File Size = 91490 *
*Baskets : 10 : Basket Size= 32000 bytes Compression= 2.74 *
*............................................................................*
*Br 193 :AOD_eleEta : vector<float> *
*Entries : 15153 : Total Size= 251400 bytes File Size = 92955 *
*Baskets : 10 : Basket Size= 32000 bytes Compression= 2.70 *
*............................................................................*
.
.
.
Thank you.
Hi @trenta_coollime
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)