Looping over a subset of entries in a TTree

Hello again,

I’m trying to sum the contents of a TBranch taken from a subset of a TTree, and I’m doing so as follows:



 TStopwatch timer1;
 outFile->cd();
 timer1.Start();
 TTree *scuttree = sintuplecut[i]->CopyTree(cutstring+otherCuts); //copy subset of original TTree to new TTree
 timer1.Stop();
 cout << "copying tree took: " << timer1.RealTime() << " seconds" << endl;
 s[cut][i][k] = scuttree->GetEntries(); //Number of entries copied
 ep[cut][i][k] = 0.0;
 Float_t val = 0.0;
 scuttree->SetBranchAddress(weightname,&val);
 for(UInt_t l=0; l<s[cut][i][k]; l++){ //loop over copied entries to sum val
         scuttree->GetEntry(l); 
         ep[cut][i][k] += (Double_t)val;
         }
scuttree->Delete();

This works, but because it is copying a (large) portion of a (large) TTree it takes a very long time. I intend to do this very many times altering the selection string (which is why there are 3D vectors :frowning: ), so I am looking for something faster.

Is there a way to select a subset of entries from a TTree using a selection string that I can then loop/iterate over without having to create a new TTree? I have been looking at TSelector and TTreePlayer but I can’t tell if these are the right methods to use, or even how to use them.

Thanks,

Conor

Use a TEntryList. See doc of this class and Users Guide for TTree::Draw

Rene

Thanks again for the quick response, Rene!

For those interested, here is how I’ve changed the code:

                                        TStopwatch timer1;
                                        outFile->cd();
                                        timer1.Start();
                                        sintuplecut[i]->Draw(">>elist",cutstring+otherCuts,"entrylist");
                                        TEntryList *elist = (TEntryList*)gDirectory->Get("elist");
                                        timer1.Stop();
                                        cout << "copying tree took: " << timer1.RealTime() << " seconds" << endl;
                                        s[cut][i][k] = elist->GetN();
                                        cout << "Number of entries: " <<  s[cut][i][k] << endl;
                                        ep[cut][i][k] = 0.0;
                                        Float_t val = 0.0;
                                        sintuplecut[i]->SetBranchAddress(weightname,&val);
                                        for(UInt_t l=0; l<s[cut][i][k]; l++){
                                                sintuplecut[i]->GetEntry(elist->Next());
                                                ep[cut][i][k] += (Double_t)val;
                                        }
                                        elist->Reset();