Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:18:48

0001 TTree *CopyTreeMulti(const char *selection, TTree *input, Long64_t ncandscut, Long64_t nentries = 1000000, Long64_t firstentry = 0)
0002 {
0003   // based on TTree::CopyTree
0004   //
0005   // copy a Tree with selection
0006   // make a clone of this Tree header.
0007   // then copy the selected entries
0008   // multiple candidate cuts possible
0009   //
0010   // selection is a standard selection expression (see TTreePlayer::Draw)
0011   // option is reserved for possible future use
0012   // nentries is the number of entries to process (default is all)
0013   // first is the first entry to process (default is 0)
0014   //
0015   // IMPORTANT: The copied tree stays connected with this tree until this tree
0016   //            is deleted.  In particular, any changes in branch addresses
0017   //            in this tree are forwarded to the clone trees.  Any changes
0018   //            made to the branch addresses of the copied trees are over-ridden
0019   //            anytime this tree changes its branch addresses.
0020   //            Once this tree is deleted, all the addresses of the copied tree
0021   //            are reset to their default values.
0022   //
0023   // The following example illustrates how to copy some events from the Tree
0024   // generated in $ROOTSYS/test/Event
0025   //
0026   //   gSystem->Load("libEvent");
0027   //   TFile f("Event.root");
0028   //   TTree *T = (TTree*)f.Get("T");
0029   //   Event *event = new Event();
0030   //   T->SetBranchAddress("event",&event);
0031   //   TFile f2("Event2.root","recreate");
0032   //   TTree *T2 = T->CopyTree("fNtrack<595");
0033   //   T2->Write();
0034 
0035 
0036   // we make a copy of the tree header
0037   TTree *tree = input->CloneTree(0);
0038   if (tree == 0) return 0;
0039 
0040   // The clone should not delete any shared i/o buffers.
0041   TObjArray* branches = tree->GetListOfBranches();
0042   Int_t nb = branches->GetEntriesFast();
0043   for (Int_t i = 0; i < nb; ++i) {
0044     TBranch* br = (TBranch*) branches->UncheckedAt(i);
0045     if (br->InheritsFrom("TBranchElement")) {
0046       ((TBranchElement*) br)->ResetDeleteObject();
0047     }
0048   }
0049 
0050   Long64_t entry,entryNumber;
0051   nentries = input->GetEntries();
0052 
0053   // Compile selection expression if there is one
0054   TTreeFormula *select = 0; // no need to interfer with fSelect since we
0055   // handle the loop explicitly below and can call
0056   // UpdateFormulaLeaves ourselves.
0057   if (strlen(selection)) {
0058     select = new TTreeFormula("Selection",selection,input);
0059     if (!select || !select->GetNdim()) { delete select; }
0060   }
0061 
0062   //loop on the specified entries
0063   Int_t tnumber = -1;
0064   for (entry=firstentry;entry<firstentry+nentries;entry++) {
0065     Long64_t ncand = 0;
0066     entryNumber = input->GetEntryNumber(entry);
0067     if (entryNumber < 0) break;
0068     Long64_t localEntry = input->LoadTree(entryNumber);
0069     if (localEntry < 0) break;
0070     if (tnumber != input->GetTreeNumber()) {
0071       tnumber = input->GetTreeNumber();
0072       if (select) select->UpdateFormulaLeaves();
0073     }
0074     if (select) {
0075       Int_t ndata = select->GetNdata();
0076       for(Int_t current = 0; current<ndata; current++) {
0077     if (select->EvalInstance(current) != 0) ncand++;
0078       }
0079       if (ncand < ncandscut) continue;
0080     }
0081     input->GetEntry(entryNumber);
0082     tree->Fill();
0083   }
0084   return tree;
0085 }