void microNtuple_addition(TString inputRootFile, TString compareTo, TString addFrom, TString baseFilename) { cout << "Opening files ... "; //TFile* ifile = TFile::Open(inputRootFile); //TFile* addFromFile = TFile::Open(addFrom); //TFile* compareToFile = TFile::Open(compareTo); cout << "DONE" << endl; cout << "Making all of the ntuples ... "; EventNtuple *ntuple = new EventNtuple(); EventNtuple *ntupleFinal = new EventNtuple(); METree *metree = new METree(); MicroNtuple *mnt = new MicroNtuple(); cout << "DONE" << endl; //Set up the EventNtuple cout << "Setting up EventNtuple ... " << flush; //compareToFile->cd("PS"); TChain *t1 = new TChain("jets2p","Output tree for matrix element");//(TChain*)gDirectory->Get("jets2p"); t1->Add(compareTo+"/PS/jets2p"); t1->LoadTree(0); TTree* t1t = t1->GetTree(); t1t->SetCacheSize(10000000); t1t->AddBranchToCache("*"); t1->SetBranchAddress("EvtNtuple", &ntuple); t1->SetBranchStatus("*",0); t1->SetBranchStatus("event",1); t1->GetTree()->BuildIndex("event"); TTreeIndex* eventNtupleIndex = new TTreeIndex(); eventNtupleIndex->Append((TTreeIndex*)t1->GetTree()->GetTreeIndex()); int nEventNtuple = static_cast<int>(t1->GetEntries()); t1->SetBranchStatus("*",1); cout << "DONE" << endl; //Set up the final MicroNtuple cout << "Setting up original/final MicroNtuple ... "; //ifile->cd(); TChain *t2 = new TChain("METree");//(TChain*)gDirectory->Get("METree"); t2->Add(inputRootFile); t2->SetBranchAddress("EvtTree",&ntupleFinal); t2->SetBranchAddress("METree",&metree); t2->SetBranchAddress("mnt", &mnt); cout << "DONE" << endl; //Set up the MicroNtuple you will be adding events from cout << "Setting up MicroNtuple that has the events to be added ... "; //addFromFile->cd(); TChain* t3 = new TChain("METree");//(TChain*)gDirectory->Get("METree"); t3->Add(addFrom); t3->SetBranchAddress("EvtTree",&ntupleFinal); t3->SetBranchAddress("METree",&metree); t3->SetBranchAddress("mnt", &mnt); //t3->GetTree()->BuildIndex("m_event"); //TTreeIndex* microNtupleIndex = new TTreeIndex(); //microNtupleIndex->Append((TTreeIndex*)t3->GetTree()->GetTreeIndex()); int nMicroNtuple = static_cast<int>(t3->GetEntries()); cout << "DONE" << endl; cout << "Building the eventIndex ... " << endl; Long64_t local = -1; t3->SetBranchStatus("*",0); t3->SetBranchStatus("event*",1); map<int,int> eventIndex; int nIndex = eventNtupleIndex->GetN(); for( int i = 0; i < nIndex ; ++i ) { loadbar(i+1,nIndex); local = eventNtupleIndex->GetIndex()[i]; eventIndex[eventNtupleIndex->GetIndexValues()[i]>>31] = local; } t3->SetBranchStatus("*",1); //counters int eventsSkipped = 0; int eventsAdded = 0; cout << endl << "Looping through the additional MicroNtuple and checking the EventNtuple ... " << endl; //ifile->cd(); for(int ientry=0; ientry<nMicroNtuple; ientry++) { //cout << "sfsg1.1" << endl; t3->GetEntry(ientry); //cout << "sfsg1.2" << endl; loadbar(ientry+1,nMicroNtuple); if(eventIndex.find(mnt->event)!=eventIndex.end()) { //cout << "sfsg1.3" << endl; //t3->GetEntry(eventIndex[ntuple->event]); //cout << "sfsg1.4" << endl; //t2->Fill(); //cout << "sfsg1.5" << endl; eventsAdded++; } else eventsSkipped++; } //Opens the .ROOT file and set it so that new graph will be added, but nothing will be deleted //cout << endl << "Setting up the output file ... "; //TFile myfile(TString(baseFilename+".root"), "RECREATE"); //cout << "DONE" << endl; if(eventsSkipped == 0 && eventsAdded == t3->GetEntries()) { cout << endl << "Merging the original/final MicroNtuple with the additional MicroNtuple ... " << flush; t2->Add(addFrom); t2->Merge(TString(baseFilename+".root")); cout << "DONE" << endl; } else { cout << endl << "WARNING::No merge because either eventsSkipped [" << eventsSkipped << "] is greater than 0 or eventsAdded(" << eventsAdded << ") does not equal t3->GetEntries() [" << t3->GetEntries() << "]" << endl; } cout << endl << "Events Skipped: " << eventsSkipped << endl; cout << "Events Added: " << eventsAdded << endl; cout << "Events Total: " << t2->GetEntries() << endl; cout << "Difference from EventNtuple: " << t1->GetEntries() - t2->GetEntries() << endl; //t2->AutoSave(); //t2->Write(); //myfile.Close(); }
void microNtuple_subtraction(TString inputRootFile, TString compareTo, TString baseFilename) { cout << "Opening files ... "; TFile* ifile = TFile::Open(inputRootFile); TFile* compareToFile = TFile::Open(compareTo); cout << "DONE" << endl; //Set up the EventNtuple cout << "Setting up EventNtuple ... " << flush; compareToFile->cd("PS"); TChain *t1 = (TChain*)gDirectory->Get("jets2p"); EventNtuple *ntuple = new EventNtuple(); t1->SetBranchAddress("EvtNtuple", &ntuple); t1->SetBranchStatus("*",0); t1->SetBranchStatus("event",1); int nEventNtuple = static_cast<int>(t1->GetEntries()); cout << "DONE" << endl; //Set up the MicroNtuple cout << "Setting up original MicroNtuple ... " << flush; ifile->cd(); TChain *t2 = (TChain*)gDirectory->Get("METree"); MicroNtuple *mnt = new MicroNtuple(); t2->SetBranchAddress("mnt", &mnt); t2->GetTree()->BuildIndex("m_event"); TTreeIndex* microNtupleIndex = new TTreeIndex(); microNtupleIndex->Append((TTreeIndex*)t2->GetTree()->GetTreeIndex()); //int nMicroNtuple = static_cast<int>(t2->GetEntries()); cout << "DONE" << endl; cout << "Building the eventIndex ... " << endl; Long64_t local = -1; t2->SetBranchStatus("*",0); t2->SetBranchStatus("event*",1); map<int,int> eventIndex; map<int,int> duplicateIndex; int nIndex = microNtupleIndex->GetN(); for( int i = 0; i < nIndex ; ++i ) { loadbar(i+1,nIndex); local = microNtupleIndex->GetIndex()[i]; if(eventIndex.find(microNtupleIndex->GetIndexValues()[i]>>31)!=eventIndex.end()) { duplicateIndex[local] = microNtupleIndex->GetIndexValues()[i]>>31; cout << "WARNING::makeMicroNtuple::There could be duplicate events in the chain" << endl; } if(local>t2->GetEntries()) { cout << "WARNING::makeMicroNtuple::The local index is greater than the number of entries in the chain" << endl; } eventIndex[microNtupleIndex->GetIndexValues()[i]>>31] = local; } t2->SetBranchStatus("*",1); //Opens the .ROOT file and set it so that new graph will be added, but nothing will be deleted cout << endl << "Setting up the output file and chain ... "; TFile myfile(TString(baseFilename+".root"), "RECREATE"); TChain *newntuple = (TChain*)t2->CloneTree(0); cout << "DONE" << endl; //counters int eventsRemoved = 0; int duplicates = 0; // Holds the list of events to avoid duplication RunEventSet runEventSet; cout << "Looping through the EventNtuple and filling subtracted MicroNtuple ... " << endl; for(int ientry=0; ientry<nEventNtuple; ientry++) { t1->GetEntry(ientry); loadbar(ientry+1,nEventNtuple); if (runEventSet.alreadySeen(ntuple->run, ntuple->event)) { cout << "WARNING, event Run = " << ntuple->run << ", Event = " << ntuple->event <<" is duplicated" << endl << "We will skip this event." << endl; duplicates++; eventsRemoved++; } else if(eventIndex.find(ntuple->event)!=eventIndex.end()) { t2->GetEntry(eventIndex[ntuple->event]); newntuple->Fill(); } else eventsRemoved++; } cout << endl << "Events Removed: " << eventsRemoved << endl; cout << "Events Remaining: " << newntuple->GetEntries() << endl; cout << "Duplicate Events: " << duplicateIndex.size() << " (" << duplicates << ")" << endl; newntuple->AutoSave(); newntuple->Write(); myfile.Close(); }