Finding the max/min in a tree

Hello,

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.

Ryan,

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?

Cheers,
Wim

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.

Ryan,

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.

Cheers,
Wim

You can find one of the root files here

/afs/cern.ch/user/r/reece/public/user08.RyanDReece.105011.J2.AAN.28.06.AANT0._00011.root

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.

Ryan,

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?

HTH,
Wim

Hi,

TTree::GetMinimum and TTree::GetMaximum currently only support branch created with the leaflist branch creation method.

Cheers,
Philippe.

Wim,

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

Ryan,

‘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.

Cheers,
Wim