I am trying to select in my TTree all events featuring at least two muon tracks with net muon charge equalling zero, in ROOT.
In PyRoot the smartest thing I could come up with is the following
from ROOT import *
import numpy as np
for entry in tree:
#access the momenta and other vars
isMuon = np.array(entry.muon)
Charge = np.array(entry.charge)
if sum(isMuon)>=2 and sum(Charge[np.where(isMuon==1)])==0:
Can anyone suggest a similar solution for ROOT that does not feature an ugly for/if loop?
Assuming that by ‘similar solution in ROOT’ you mean in C++ and that you are using ROOT v6, you can get closer by using TTreeReader.
See https://root.cern.ch/doc/master/classTTreeReader.html .
You need to know the type of your ‘muon’ and ‘charge’ and the name of they branches.
In your case it would be something like:
// Here 'tree' is your TTree object
TTreeReader reader(tree);
// Define your variables
TTreeReaderValue< ...muon type ... > muon(reader, "<muon_branch_name>");
TTreeReaderValue< ...charge type ... > charge(reader, "<charge_branch_name>");
// Loop
while (reader.Next()) {
// *muon contains your 'muon', *charge contains your 'charge'
}
You should write then your small functions for sum(…) if the types do not provide them.