Looping inside file and get latest version/key with pyROOT


In my file, I have several versions of the same histogram ie like histo;1 histo;2 histo;3. How would it possible to loop and read the latest one ? I know how to do it with C++ but I was looping for a pyROOT solution.


By default when you “Get” a histogram from a ROOT file you will get the latest one.
In your case host;3
It does not depend on the fact you are using Python or C++.

So, my code is like this

 dirList2 = gDirectory.GetListOfKeys()
         for k2 in dirList2:
             h2 = k2.ReadObj()

             print " the hist is ", h2.GetName(), h2.Integral()

What I get from this, actually corresponds to the integral of the first copy (histol;1) and not of the latest one (histo;4) .So maybe I need to change something in my pyROOT code ?


Now, if I just open TBrowser I have

If you are looping of the keys, you will ‘get’/‘see’ the keys in increasing key number. So to know if you got the last cycle you need to pick at the next element and if the next element has a different name has the current element then you know the current element in the highest cycle for its name.

Right, I guess something like this

but somehow I fail to implement this in pyROOT - is there somewhere an example for what I am looking for ?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.