/////////////////////////////////////////////////////////////////////////////////////////////// // // // Event filter: Makes cuts on Merit or Svac ntuples, // // and writes out a file with the // // corresponding digi/mc events. // // First version: W. Focke, focke@slac.stanford.edu // // Rewritten version not using buggy CopyTree: H. Kelly, heather@slac.stanford.edu) // // A. W. Borgland, borgland@slac.stanford.edu // // // // This is free software! // // // /////////////////////////////////////////////////////////////////////////////////////////////// #ifndef __CINT__ #include #include #include #include "TChain.h" #include "TLeaf.h" #include "TObjArray.h" #include "TObjString.h" #endif // #include "/nfs/slac/g/svac/common/builds/Fred/EventFilter/headers.h" void EventFilter (char *meritName, char *digiName, TCut cut, char *outName) { // Input parameters: // meritName: Name of Merit or Svac ntuple // digiName: Name of input digi/mc/recon file to be filtered // cut: Cut to apply on Merit/Svac ntuple // outName: Output i.e. filtered digi/mc/recon file // Name of Merit/SVAC ntuple file: TFile *meritFile = new TFile(meritName); // Tree in the Merit/SVAC ntuple: TTree *meritTree; if ((TTree *) meritFile->Get("MeritTuple")) { meritTree = (TTree *) meritFile->Get("MeritTuple"); } if ((TTree *) meritFile->Get("Output")) { meritTree = (TTree *) meritFile->Get("Output"); } // Name of digi/mc root file: TFile *digiFile = new TFile(digiName); // Tree in the digi/mc file: Int_t useDigi = 0; Int_t useMc = 0; Int_t useRecon = 0; // Nbr of events written out: Int_t nbrEventsFiltered = 0; TTree *digiTree; if ((TTree *) digiFile->Get("Digi")) { digiTree = (TTree *) digiFile->Get("Digi"); useDigi = 1; } if ((TTree *) digiFile->Get("Mc")) { digiTree = (TTree *) digiFile->Get("Mc"); useMc = 1; } if ((TTree *) digiFile->Get("Recon")) { digiTree = (TTree *) digiFile->Get("Recon"); useRecon = 1; } // Make the eventlist based on the cuts: meritTree->Draw(">>eList", cut); // Get a pointer to the eventlist: TEventList *evtList = (TEventList*) gDirectory->Get("eList"); // Set digi branch address:: DigiEvent *evtDigi = 0; McEvent *evtMc = 0; ReconEvent *evtRecon = 0; if (useDigi == 1) { digiTree->SetBranchAddress("DigiEvent",&evtDigi); } if (useMc == 1) { digiTree->SetBranchAddress("McEvent",&evtMc); } if (useRecon == 1) { digiTree->SetBranchAddress("ReconEvent",&evtRecon); } // Filtered (output) digi file name: TFile *newfile = new TFile(outName,"recreate"); // Filtered tree is the same structure as original digi/mc tree: TTree* newtree = (TTree*)digiTree->CloneTree(0); // Total number of entries in the eventlist: Int_t nentries = evtList->GetN(); // Loop over all the events in the eventlist: for (Int_t jentry=0; jentryGetEntry(jentry); if (ientry < 0) break; // Get the digi/mc tree entry: digiTree->GetEvent(ientry); // Copy it over to the filtered tree: if (useDigi == 1) { if (evtDigi) { newtree->Fill(); evtDigi->Clear(); nbrEventsFiltered++; } } if (useMc == 1) { if (evtMc) { newtree->Fill(); evtMc->Clear(); nbrEventsFiltered++; } } if (useRecon == 1) { if (evtRecon) { newtree->Fill(); evtRecon->Clear(); nbrEventsFiltered++; } } } // Write out the filtered tree: newtree->Write(); newfile->Write(); newfile->Close(); // Output: std::cout << " " << std::endl; if (nbrEventsFiltered == 0) { std::cout << "EventFilter: No events were written out!" << std::endl; } if (nbrEventsFiltered == 1) { std::cout << "EventFilter: " << nbrEventsFiltered << " event was filtered and written to the output file." << std::endl; } if (nbrEventsFiltered > 1) { std::cout << "EventFilter: " << nbrEventsFiltered << " events were filtered and written to the output file." << std::endl; } std::cout << " " << std::endl; }