#include #include #include "TFile.h" #include "TTree.h" #include "TBranch.h" #include "TEventList.h" void thinGrid(string strInFile, string strBranchNameTxt = "bNames.txt", int dpdType=1,string strOutFile = "_thin.root"){ std::string argStr; std::ifstream ifs(strInFile.c_str()); std::getline(ifs,argStr); // split by ',' std::vector fileList; for (size_t i=0,n; i <= argStr.length(); i=n+1) { n = argStr.find_first_of(',',i); if (n == string::npos) n = argStr.length(); string tmp = argStr.substr(i,n-i); fileList.push_back(tmp); } for (int nFile=0; nFile < fileList.size();nFile++){ TFile *oldfile = TFile::Open(fileList[nFile].c_str()); if (!oldfile || oldfile->IsZombie()) { cout << "Could not read: " << strInFile.c_str() << " skipping file..." << endl; } else{ string strThinCut = ""; if (dpdType == 1) { strThinCut = "((mu_staco_n > 0 && mu_staco_pt[0]>10000)||(mu_muid_n > 0 && mu_muid_pt[0]>10000)) && el_n > 0 && el_pt>10000"; //strThinCut = "((mu_staco_n > 0 )||(mu_muid_n > 0 )) && el_n > 0"; } else { strThinCut = "mu_n > 0 && el_n > 0"; } TTree *oldtree = (TTree*)oldfile->Get("physics"); //TTree *oldctree = (TTree*)oldfile->Get("CollectionTree"); oldtree->SetBranchStatus("*",0); cout << "Opening branch names: " << strBranchNameTxt << endl; ifstream bNames (strBranchNameTxt.c_str()); while (!bNames.eof()){ string line; getline (bNames,line); if(line!=""){ oldtree->SetBranchStatus(line.c_str(),1); } } //Create a new file + a clone of old tree in new file TFile *newfile = new TFile(strOutFile.c_str(),"UPDATE"); TTree *newtree = oldtree->CloneTree(0); //TTree *newctree = oldctree->CloneTree(); TEventList *evlist=new TEventList("evlist"); //int evPassed=oldtree->Draw(">>evlist",strThinCut.c_str()); oldtree->Draw(">>evlist",strThinCut.c_str()); int evPassed=evlist->GetN(); cout << "Number of events after thining: " << evPassed << endl; for(int i=0;iGetEntry(evlist->GetEntry(i)); newtree->Fill(); }//for i //newtree->Print(); newfile->Write(); delete oldfile; delete newfile; } } }