Reading Tree data from different events in the same loop (coincidences)

Hello all,

I have a Tree that looks like:

I now how to extract, for each event, all the data with a for loop:

E = np.array( [] )                    #store the Energy

for event in tree:  #for each event, store the desired values
   E = np.append(E, tree.Energy) 

I want to make coincidence plots (if the time difference between event X and event X+1, store its energies values). I could do this in the fashion showed above, first stored all the values, then compared them, but it is very time consumig, so I wonder if I could do it more neatly.

With C++ you could do it using Tree->GetEntry(n); in a loop throught all the events, I can get the energy of the event n and n+1 (the following) and then compare it, and if they are in coincidence store the energy (plot it), but I do not know how I could do this with pyROOT.

Thank you so much in advance,

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.24/00
Python version: 3.8.5

Hi @Daniel_Lopez,

The GetEntry() function should also be callable from Python (via PyROOT). On a more Pythonic way, you can take a look to this tutorial to get some ideas: ROOT: tutorials/pyroot/ File Reference.


Please note that TTree.AsMatrix is deprecated since 6.24, please use AsNumpy instead:

You could create an RDataFrame from your tree and store the Energy column in a numpy array with AsNumpy for further processing. You could also use RDataFrame::Take but in that case you’d get an std::vector.