void runmergeWithDir() #endif { TChain *tph; tph = new TChain("CBNT/t3333"); tph->AddFile("file1.root"); tph->AddFile("file2.root"); tph->Merge("merged.root"); TFile *fil = new TFile("merged.root"); fil->ls(); }
void formChain300(){ cout << "Begin loading..." << endl; const TString treedirectory = "demo/T_weights"; TChain* InputChain = new TChain(treedirectory); InputChain->Add("rfio:/castor/cern.ch/user/k/kreis/CUSusy/QCD/T_weight_QCD_Pt300_*.root"); InputChain->Merge("/cu1/kreis/QCD/T_weights_QCD_Pt300.root"); cout << "Loaded " << InputChain->GetListOfFiles()->GetEntries() << " files" << endl; return; }
void macro( TString dirname = "thin" ) { //cout<<"Processing directory:"<<dirname<<endl; gROOT->Reset(); gROOT->SetStyle("Plain"); gStyle->SetFillColor(0); //white fill color gStyle->SetFrameBorderMode(0); //no frame border gStyle->SetCanvasBorderMode(0); //no canvas border gStyle->SetOptTitle(0); //no title //gStyle->SetOptStat(0); //no statistics _or_ gStyle->SetOptStat(1111111111); // -->all statistics (the more 1's y // connect file and tree //TFile *file = new TFile(dirname+"IPbinpT_0.4.root"); //TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject(dirname+"IPbinpT_0.33.root"); //if (f==0) { // std::cout << "What This file doesn't exist MAAAAN!" << std::endl; //} //TTree *tree = (TTree*)gDirectory->Get("sigma"); // Create the chain as above // TChain *chain = new TChain("sigma"); chain->Add(dirname+"/*.root"); chain->Merge(dirname+".root"); //cout<<"Merged file: "<<dirname<<".root"<<endl; //exit(); //chain->Draw("bx1:pTi"); /* for (Int_t i(0); i<9; ++i) { chain->GetEvent(i); } */ /*chain.SetBranchAddress("event", &event); // Start main loop on all events // In case you want to read only a few branches, use TChain::SetBranchStatus // to activate/deactivate a branch. Int_t nevent = chain.GetEntries(); for (Int_t i=0;i<nevent;i++) { chain.GetEvent(i); //read complete accepted event in memory hnseg->Fill(event->GetNseg()); //Fill histogram with number of segments } // Draw the histogram hnseg->Draw();*/ }
void WriteMergeObjects( TFile *target ) { cout << "Writing the merged data." << endl; TIterator *nextobj = MergeObjects.MakeIterator(); TObjString *pathname_obj; while( (pathname_obj = (TObjString *)nextobj->Next()) ) { TString path,name; SplitPathName(pathname_obj->String(),&path,&name); TObject *obj = MergeObjects.GetValue(pathname_obj); target->cd(path); obj->Write( name ); delete obj; } MergeObjects.Clear(); target->Write(); // Temporarily let multiple root files remain if > 2GB // Prevent Target_1.root Target_2.root, ... from happening. // long long max_tree_size = 200000000000LL; // 200 GB // if(TTree::GetMaxTreeSize() < max_tree_size ) { // TTree::SetMaxTreeSize(max_tree_size); // } nextobj = MergeChains.MakeIterator(); TObjString *pathname_obj; while( (pathname_obj = (TObjString *)nextobj->Next()) ) { TString path,name; SplitPathName(pathname_obj->String(),&path,&name); TChain *ch = (TChain *)MergeChains.GetValue(pathname_obj); target->cd(path); ch->Merge(target,0,"KEEP"); delete ch; // in case of multiple objects with same pathname, must remove // this one from the list so we don't get the same (deleted) // one next time we look up the same name MergeChains.Remove(pathname_obj); } MergeChains.Clear(); InitializedMergeObjects = false; }
void mergeHadoopFiles() { gSystem->Load("../libMiniFWLite.so"); TChain *chain = new TChain("t"); chain->SetMaxTreeSize(5000000000LL); //default is 100000000000LL chain->Add("unmerged/ww2l2nujets_merged_ntuple_10_smallTree.root"); chain->Add("unmerged/ww2l2nujets_merged_ntuple_11_smallTree.root"); chain->Add("unmerged/ww2l2nujets_merged_ntuple_1_smallTree.root"); chain->Add("unmerged/ww2l2nujets_merged_ntuple_2_smallTree.root"); chain->Add("unmerged/ww2l2nujets_merged_ntuple_3_smallTree.root"); chain->Add("unmerged/ww2l2nujets_merged_ntuple_4_smallTree.root"); chain->Add("unmerged/ww2l2nujets_merged_ntuple_5_smallTree.root"); chain->Add("unmerged/ww2l2nujets_merged_ntuple_6_smallTree.root"); chain->Add("unmerged/ww2l2nujets_merged_ntuple_7_smallTree.root"); chain->Add("unmerged/ww2l2nujets_merged_ntuple_8_smallTree.root"); chain->Add("unmerged/ww2l2nujets_merged_ntuple_9_smallTree.root"); chain->Add("unmerged/ww2l2nujets_merged_ntuple_smallTree.root"); chain->Merge("merged/ww2l2nujets.root", "fast"); }
void mergeHadoopFiles(const TString& indir, const TString& outpath) { // Benchmark TBenchmark *bmark = new TBenchmark(); bmark->Start("benchmark"); //gSystem->Load("../libMiniFWLite.so"); TChain *chain = new TChain("Events"); chain->SetMaxTreeSize(5000000000LL); //default is 100000000000LL std::cout << "Merging files from dir: " << indir << std::endl << "Ouputting to: " << outpath << std::endl; chain->Add(indir + "/*.root"); chain->Merge(outpath, "fast"); std::cout << "Total events merged: " << chain->GetEntries() << std::endl; bmark->Stop("benchmark"); cout << "------------------------------" << endl; cout << "CPU Time: " << Form( "%.01f s", bmark->GetCpuTime("benchmark") ) << endl; cout << "Real Time: " << Form( "%.01f s", bmark->GetRealTime("benchmark") ) << endl; cout << endl; delete bmark; }
void hmerge_( TDirectory *target, TList *sourcelist, double crossArray[] ) { //cout << "Target path: " << target->GetPath() << endl; TString rpath( (char*)strstr( target->GetPath(), ":" ) ); rpath.Remove( 0, 2 ); TFile *first_source = (TFile*)sourcelist->First(); first_source->cd( rpath ); TDirectory *current_sourcedir = gDirectory; //gain time, do not add the objects in the list in memory Bool_t status = TH1::AddDirectoryStatus(); TH1::AddDirectory(kFALSE); // loop over all keys in this directory TChain *globChain = 0; TIter nextkey( current_sourcedir->GetListOfKeys() ); TKey *key, *oldkey=0; while ( (key = (TKey*)nextkey())) { //keep only the highest cycle number for each key if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue; // read object from first source file first_source->cd( rpath ); TObject *obj = key->ReadObj(); if ( obj->IsA()->InheritsFrom( "TH1" ) ) { // descendant of TH1 -> merge it //cout << "Merging histogram " << obj->GetName() << endl; TH1 *h1 = (TH1*)obj; h1->Sumw2(); // Scale by the cross-section factor h1->Scale(crossArray[0]); // loop over all source files and add the content of the // correspondant histogram to the one pointed to by "h1" TFile *nextsource = (TFile*)sourcelist->After( first_source ); int q = 1; // This keeps track of which // cross section factor to use while ( nextsource ) { // make sure we are at the correct directory level by cd'ing to rpath nextsource->cd( rpath ); TKey *key2 = (TKey*)gDirectory->GetListOfKeys()->FindObject(h1->GetName()); if (key2) { TH1 *h2 = (TH1*)key2->ReadObj(); h2->Sumw2(); // Scale by the cross-section factor before adding. h2->Scale(crossArray[q]); h1->Add( h2 ); q++; delete h2; } nextsource = (TFile*)sourcelist->After( nextsource ); } } else if ( obj->IsA()->InheritsFrom( "TTree" ) ) { // loop over all source files create a chain of Trees "globChain" const char* obj_name= obj->GetName(); globChain = new TChain(obj_name); globChain->Add(first_source->GetName()); TFile *nextsource = (TFile*)sourcelist->After( first_source ); while ( nextsource ) { globChain->Add(nextsource->GetName()); nextsource = (TFile*)sourcelist->After( nextsource ); } } else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) { cout << "Found subdirectory " << obj->GetName() << endl; // create a new subdir of same name and title in the target file target->cd(); TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() ); // newdir is now the starting point of another round of merging // newdir still knows its depth within the target file via // GetPath(), so we can still figure out where we are in the recursion hmerge_( newdir, sourcelist,crossArray ); } else { // object is of no type that we know or can handle cout << "Unknown object type, name: " << obj->GetName() << " title: " << obj->GetTitle() << endl; } // now write the merged histogram (which is "in" obj) to the target file // note that this will just store obj in the current directory level, // which is not persistent until the complete directory itself is stored // by "target->Write()" below if ( obj ) { target->cd(); //!!if the object is a tree, it is stored in globChain... if(obj->IsA()->InheritsFrom( "TTree" )) globChain->Merge(target->GetFile(),0,"keep"); else obj->Write( key->GetName() ); } } // while ( ( TKey *key = (TKey*)nextkey() ) ) // save modifications to target file target->SaveSelf(kTRUE); TH1::AddDirectory(status); }
void MergeRootfile( TDirectory *target, TList *sourcelist) { // cout << "Target path: " << target->GetPath() << endl; TString path( (char*)strstr( target->GetPath(), ":" ) ); path.Remove( 0, 2 ); TFile *first_source = (TFile*)sourcelist->First(); first_source->cd( path ); TDirectory *current_sourcedir = gDirectory; //gain time, do not add the objects in the list in memory Bool_t status = TH1::AddDirectoryStatus(); TH1::AddDirectory(kFALSE); // loop over all keys in this directory TChain *globChain = 0; TIter nextkey( current_sourcedir->GetListOfKeys() ); TKey *key, *oldkey=0; std::vector<double>::iterator wItr; while ( (key = (TKey*)nextkey())) { wItr=WeightList->begin(); //keep only the highest cycle number for each key if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue; // read object from first source file first_source->cd( path ); TObject *obj = key->ReadObj(); if ( obj->IsA()->InheritsFrom( "TH1" ) ) { // descendant of TH1 -> merge it // cout << "Merging histogram " << obj->GetName() << endl; TH1 *h1 = (TH1*)obj; h1->Sumw2(); // weight is eff * sigma, because the histos are produced on the ~same initial // events, and eff is already included (in reconstruction), weight is just sigma h1->Scale(double(*wItr)); // loop over all source files and add the content of the // correspondant histogram to the one pointed to by "h1" TFile *nextsource = (TFile*)sourcelist->After( first_source ); wItr++; while ( nextsource ) { // make sure we are at the correct directory level by cd'ing to path nextsource->cd( path ); TKey *key2 = (TKey*)gDirectory->GetListOfKeys()->FindObject(h1->GetName()); if (key2) { TH1 *h2 = (TH1*)key2->ReadObj(); h2->Sumw2(); h2->Scale( double(*wItr) ); h1->Add( h2 ); delete h2; } nextsource = (TFile*)sourcelist->After( nextsource ); wItr++; } } else if ( obj->IsA()->InheritsFrom( "TTree" ) ) { // loop over all source files create a chain of Trees "globChain" const char* obj_name= obj->GetName(); globChain = new TChain(obj_name); globChain->Add(first_source->GetName()); TFile *nextsource = (TFile*)sourcelist->After( first_source ); // const char* file_name = nextsource->GetName(); // cout << "file name " << file_name << endl; while ( nextsource ) { globChain->Add(nextsource->GetName()); nextsource = (TFile*)sourcelist->After( nextsource ); } } else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) { // it's a subdirectory cout << "Found subdirectory " << obj->GetName() << endl; // create a new subdir of same name and title in the target file target->cd(); TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() ); // newdir is now the starting point of another round of merging // newdir still knows its depth within the target file via // GetPath(), so we can still figure out where we are in the recursion MergeRootfile( newdir, sourcelist ); } else { // object is of no type that we know or can handle cout << "Unknown object type, name: " << obj->GetName() << " title: " << obj->GetTitle() << endl; } // now write the merged histogram (which is "in" obj) to the target file // note that this will just store obj in the current directory level, // which is not persistent until the complete directory itself is stored // by "target->Write()" below if ( obj ) { target->cd(); //!!if the object is a tree, it is stored in globChain... if(obj->IsA()->InheritsFrom( "TTree" )) globChain->Merge(target->GetFile(),0,"keep"); else obj->Write( key->GetName() ); } } // while ( ( TKey *key = (TKey*)nextkey() ) ) // save modifications to target file target->SaveSelf(kTRUE); TH1::AddDirectory(status); }
void MergeRootfile( TDirectory *target, const vector<pair<TFile*, float> >& vFileList) { // cout << "Target path: " << target->GetPath() << endl; TString path( (char*)strstr( target->GetPath(), ":" ) ); path.Remove( 0, 2 ); vec_pair_it it = vFileList.begin(); TFile *first_source = (*it).first; const float first_weight = (*it).second; first_source->cd( path ); TDirectory *current_sourcedir = gDirectory; //gain time, do not add the objects in the list in memory Bool_t status = TH1::AddDirectoryStatus(); TH1::AddDirectory(kFALSE); // loop over all keys in this directory TChain *globChain = 0; TIter nextkey( current_sourcedir->GetListOfKeys() ); TKey *key, *oldkey=0; while ( (key = (TKey*)nextkey())) { //keep only the highest cycle number for each key if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue; // read object from first source file first_source->cd( path ); TObject *obj = key->ReadObj(); if ( obj->IsA()->InheritsFrom( TH1::Class() ) ) { // descendant of TH1 -> merge it // cout << "Merging histogram " << obj->GetName() << endl; TH1 *h1 = (TH1*)obj; if (first_weight>0) { h1->Scale(first_weight); } // loop over all source files and add the content of the // correspondant histogram to the one pointed to by "h1" for (vec_pair_it nextsrc = vFileList.begin()+1; nextsrc != vFileList.end(); ++nextsrc ) { // make sure we are at the correct directory level by cd'ing to path (*nextsrc).first->cd( path ); const float next_weight = (*nextsrc).second; TKey *key2 = (TKey*)gDirectory->GetListOfKeys()->FindObject(h1->GetName()); if (key2) { TH1 *h2 = (TH1*)key2->ReadObj(); if (next_weight>0) h2->Scale(next_weight); h1->Add( h2 ); delete h2; } } } else if ( obj->IsA()->InheritsFrom( TTree::Class() ) ) { // loop over all source files create a chain of Trees "globChain" const char* obj_name= obj->GetName(); globChain = new TChain(obj_name); globChain->Add(first_source->GetName()); for (vec_pair_it nextsrc = vFileList.begin()+1; nextsrc != vFileList.end(); ++nextsrc ) { globChain->Add(nextsrc->first->GetName()); } } else if ( obj->IsA()->InheritsFrom( TDirectory::Class() ) ) { // it's a subdirectory cout << "Found subdirectory " << obj->GetName() << endl; // create a new subdir of same name and title in the target file target->cd(); TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() ); // newdir is now the starting point of another round of merging // newdir still knows its depth within the target file via // GetPath(), so we can still figure out where we are in the recursion MergeRootfile( newdir, vFileList); } else { // object is of no type that we know or can handle cout << "Unknown object type, name: " << obj->GetName() << " title: " << obj->GetTitle() << endl; } // now write the merged histogram (which is "in" obj) to the target file // note that this will just store obj in the current directory level, // which is not persistent until the complete directory itself is stored // by "target->Write()" below if ( obj ) { target->cd(); //!!if the object is a tree, it is stored in globChain... if(obj->IsA()->InheritsFrom( TTree::Class() )) globChain->Merge(target->GetFile(),0,"keep"); else obj->Write( key->GetName() ); } } // while ( ( TKey *key = (TKey*)nextkey() ) ) // save modifications to target file target->SaveSelf(kTRUE); TH1::AddDirectory(status); }