//----------------------------------------------------------------------------- // Dec 15 1999 P.Murat: // -------------------- // an example showing how to copy a subset of events from the input ntuple // (UC STNTUPLE 5.x converted to ROOT) to output TStntuple (C++ equivalent of // US STNTUPLE) // // the user is supposed to supply a function event_is_ok(THBookStnEvent*) // returning 1 if the input event has to be copied into output ntuple and // 0 otherwise //----------------------------------------------------------------------------- #ifndef __CINT__ #include "TROOT.h" #include "TFile.h" #include "TTree.h" #include "TInterpreter.h" #include #include #include #include #endif THBookStnEvent* input_event; TStntupleEvent* output_event; //----------------------------------------------------------------------------- // return 1 if there is at least 1 loose tau candidate //----------------------------------------------------------------------------- int event_is_ok (THBookStnEvent* ev) { int rc = 0; StnTausBlock_t* blk = ev->fTausBlock; for (int i=0; iNtau; i++) { if (blk->Tstat[i] > 2000.) { rc = 1; break; } } // printf(" event/run = %6d/%6d, ntau,rc = %3d , %3d \n", // ev->fGeneralBlock->Event,ev->fGeneralBlock->Run, // ev->fTausBlock->Ntau, rc); return rc; } //----------------------------------------------------------------------------- // `dir' - name of the directory with root files, I'm assuming that all the // *.root files in `dir' produce one chain //----------------------------------------------------------------------------- int copy_tree(const char* dir_name, int first_event=0, int last_event=-1) { // define input chain input_event = new THBookStnEvent(); output_event = new TStntupleEvent(); THBookStntuple* input = new THBookStntuple("/STNTUPLE/h1",input_event); void* dir; char filename[200]; Ssiz_t len; if (dir = gSystem->OpenDirectory(dir_name)) { char* fname; while (fname = gSystem->GetDirEntry(dir)) { sprintf(filename,"%s/%s",dir_name,fname); TRegexp regexp("\.root"); if (regexp.Index(filename,&len,0) > 0) { input->Add(filename); } } } else { // this could be a single file TFile f(dir_name); if (f.IsOpen()) { input->Add(dir_name); } else { printf(" ------------ cant open input file \n"); return -2; } } input->Print(); input->LoadTree(0); // define output tree TFile* output = new TFile("test_stn.root","RECREATE"); TTree* output_tree = new TTree("TStntuple","TStntuple"); TBranch* branch = output_tree->Branch("Event","TStntupleEvent", &output_event,64000,1); branch->SetCompressionLevel(1); input->SetBranches(); int ngood = 0; int nbytes; double nentries = input->GetEntries(); printf("nentries = %5d\n",nentries); int ifirst = 0; if (first_event > 0) ifirst = first_event; int ilast = nentries; if (last_event >= 0) ilast = last_event; for (Int_t ievent=ifirst; ieventLoadTree(ievent) < 0) break; // read the event in nbytes = input->GetEntry(ievent); // check if it is what we need // printf(" ievent %6d %6d passed OK\n",ievent, // input_event->fGeneralBlock->Event); if (event_is_ok(input_event)) { ngood++; output_event->InitFromHBook(input_event); output_tree->Fill(); // printf(" ievent %6d passed OK, ngood = \n",ievent,ngood); } } output->Write(); printf(" -- N(good events): %5d\n",ngood); delete input; delete output; delete input_event; delete output_event; }