I’m trying to get the min/max of a variable in a tree (chain) such that I know what range to manually define the binning of my histogram before I call Draw. I though I could do the following:
>>> c = TChain('Truth0')
>>> c.Add('*.root*')
>>> min = c.GetMinimum('Muon_Mother')
>>> max = c.GetMaximum('Muon_Mother')
>>> print min, max
0.0 0.0
Yet, when I draw that variable, it obviously is filled with some values:
>>> c.Draw('Muon_Mother', 'Muon_N>0')
How should I do this? Please note that I want to know the min/max bin-range, not the height of the bin. Thanks very much.
if I read the code of GetMaximum and GetMinimum, then one way of getting 0.0 consistently, is if GetLeaf fails. What does ‘c.GetLeaf( “Muon_Mother” )’ yield?
Thanks for following up Wim. I still get zero max when I get the leaf:
>>> c = TChain('Truth0')
>>> c.Add('*.root*')
147
>>> L = c.GetLeaf('Muon_Mother')
TClass::TClass:0: RuntimeWarning: no dictionary for class AttributeListLayout is available
TClass::TClass:0: RuntimeWarning: no dictionary for class pair<string,string> is available
>>> L.GetMaximum()
0
>>> c.Draw('Muon_Mother','Muon_N>0')
<TCanvas::MakeDefCanvas>: created default TCanvas with name c1
1935L
And again, the Draw command gives a plot with nonzero entries.
thanks for the file. Looking at the code in TTree::GetMaximum() again and now knowing that Muon_Mother is a vector of ints, I don’t see how that code could possibly give the desired effect (it won’t loop over the vector to actually look at the values it carries, as it would for e.g. a builtin array or builtin type).
The following is not efficient, but does do the trick:a = 0
for cc in c:
a = max( max( c.Muon_Mother ), a )
print a
Not sure if there is anything more convenient/smarter … Since the same problem shows up in a normal root.exe session as well, maybe you can try in the general forum?
Thanks very much for your fix. Is there a way I can get the value of Muon_Mother in the for loop without doing cc.Muon_Mother? What if I want to generalize it to any variable stored in a string and I don’t want to use an eval? Is there something like cc.Get(‘Muon_Mother’)? I’m a bit confused about what cc is? What ROOT class do you get when you iterate over a tree (or chain) like in for cc in c? Thanks
‘c’ is the chain object, and so is ‘cc’, but each ‘cc’ is the next iteration TTree-wise (i.e. c.GetEntry(index+1) has been called). Alternatively, you could do the normal SetBranchAddress() etc. and call GetEntry() yourself.