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')
>>> min = c.GetMinimum('Muon_Mother')
>>> max = c.GetMaximum('Muon_Mother')
>>> print min, max
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')
>>> 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
<TCanvas::MakeDefCanvas>: created default TCanvas with name c1
And again, the Draw command gives a plot with nonzero entries.
the code doesn’t call GetMaximum() on the leaf, just that it will always return 0 (w/o error message …) if the leaf can not be located.
Any chance of getting access to one of the root files so that I could try it out, rather than guessing semi-blindly? Thanks.
You can find one of the root files here
The muons are very sparse in these files, because it is a dijet sample, but you should find 9 muons in this file when you draw with the cut Muon_N>0.
Thanks for you help.
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 )
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?
TTree::GetMinimum and TTree::GetMaximum currently only support branch created with the leaflist branch creation method.
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.