RDataFrame Filter for arrays

Dear all,
I would like to count the number of times I have a specific particle track (e.g. a muon).
In a tree I have the following branches/variables:

   static constexpr Int_t kMaxMCTracks  = 1000;
   Double_t        fVox; ///< Vertex X
   Int_t           MCTracks_;
   Int_t           MCTracks_fPdgCode[kMaxMCTracks];    //[MCTracks_]
   Int_t           MCTracks_fMother[kMaxMCTracks][2];  //[MCTracks_]
   Int_t           MCTracks_fDaughter[kMaxMCTracks][2];//[MCTracks_]


       fChain->SetBranchAddress("fVox", &fVox, &b_Primary_fVox);
       fChain->SetBranchAddress("MCTracks", &MCTracks_, &b_MCTracks_);
       fChain->SetBranchAddress("MCTracks.fPdgCode", MCTracks_fPdgCode, &b_MCTracks_fPdgCode);
       fChain->SetBranchAddress("MCTracks.fMother[2]", MCTracks_fMother, &b_MCTracks_fMother);
       fChain->SetBranchAddress("MCTracks.fDaughter[2]", MCTracks_fDaughter, &b_MCTracks_fDaughter);

Is there a way, through RDataFrame to count how many tracks are there with a given fPdgCode?

I tried something like that:

    auto nmuons = d->Filter("MCTracks.fPdgCode==13||MCTracks.fPdgCode==-13").Count();
    cout << *nmuons << endl;

but it doesn’t work.
Thank you for your help!
Best, Germano

Please read tips for efficient and successful posting and posting code

_ROOT Version: 6.22/02
_Platform: MacOSX
Compiler: Not Provided

Filter works at the event level – it discards complete events.
If I understand correctly, what you need instead is, per event, to select just some array elements, and you want to know how many array elements satisfy the condition across all entries.
RDataFrame reads collection branches as RVecs (see the section “Reading and manipulating collections” in the RDF docs).

You can count the number of elements of fPdgCode that are equal to 13 or -13 across the whole dataset with:

df.Define("n_good", "Sum(MCTracks.fPdgCode==13 || MCTracks.fPdgCode==-1)").Sum("n_good");


Dear Enrico (@eguiraud),
it works just fine. Thank you very much. Now I understand the logic behind it.


1 Like

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