void validateInput(const char* filename, int level=0) { TFile* file = new TFile(filename, "update"); TIter nextDirectory(file->GetListOfKeys()); TKey* idir; while((idir = (TKey*)nextDirectory())){ file->cd(); if( idir->IsFolder() ){ if( level>-1 ){ std::cout << "Found directory: " << idir->GetName() << std::endl; } file->cd(idir->GetName()); validateFolder(file, idir->GetName(), level); } else{ if( level> 0 ){ std::cout << "Found histogram: " << idir->GetName() << std::endl; } if( level>-1 ){ TH1F* h = (TH1F*)file->Get(idir->GetName()); if(h->Integral() == 0){ std::cout << "----- E R R O R ----- : histogram has 0 integral please fix this: --> " << idir->GetName() << std::endl; } } } } file->Close(); return; }
/** The LES may have changed. Repoint to the current directory if it still exists, Otherwise, go to the next one. */ bool BookDirIterator::resync (LedgerEntrySet& les) { if (mIndex.isZero ()) mIndex = mBase; else if (mIndex != mBase) --mIndex; return nextDirectory (les); }
bool BookDirIterator::firstDirectory (LedgerEntrySet& les) { WriteLog (lsTRACE, Ledger) << "BookDirIterator(" << mBase.GetHex() << ") firstDirectory"; /** Jump to the beginning */ mIndex = mBase; return nextDirectory (les); }
void rescaleSignal(bool armed, double scale, const char* filename, const char* pattern="", unsigned int debug=0) { std::vector<std::string> histnames; histnames.clear(); if( debug>0 ){ std::cout << "file = " << filename << std::endl; std::cout << "scale = " << scale << std::endl; std::cout << "armed = " << armed << std::endl; } TFile* file = new TFile(filename, "update"); TIter nextDirectory(file->GetListOfKeys()); std::vector<std::string> buffer; TKey* idir; while((idir = (TKey*)nextDirectory())){ buffer.clear(); if( idir->IsFolder() ){ file->cd(); // make sure to start in directory head if( debug>0 ){ std::cout << "Found directory: " << idir->GetName() << std::endl; } if( file->GetDirectory(idir->GetName()) ){ file->cd(idir->GetName()); // change to sub-directory buffer = signalList(idir->GetName(), pattern, debug); } // append to the vector of histograms to be rescaled for(std::vector<std::string>::const_iterator elem=buffer.begin(); elem!=buffer.end(); ++elem){ histnames.push_back(*elem); } if(debug>1){ std::cout << "added " << buffer.size() << " elements to histnames [" << histnames.size() << "] for directory " << idir->GetName() << std::endl; } } } // pick up files which are not kept in an extra folder file->cd(); buffer.clear(); buffer = signalList("", pattern, debug); // append to the vector of histograms to be rescaled for(std::vector<std::string>::const_iterator elem=buffer.begin(); elem!=buffer.end(); ++elem){ histnames.push_back(*elem); } if(debug>1){ std::cout << "added " << buffer.size() << " elements to histnames [" << histnames.size() << "] for file head" << std::endl; } for(std::vector<std::string>::const_iterator hist=histnames.begin(); hist!=histnames.end(); ++hist){ file->cd(); TH1F* h = (TH1F*)file->Get(hist->c_str()); std::string histName; if(hist->find("/")!=std::string::npos){ histName = hist->substr(hist->find("/")+1); } else{ histName = *hist; } TH1F* hout = (TH1F*)h->Clone(histName.c_str()); if(debug>1){ std::cout << "...folder : " << hist->substr(0, hist->find("/")).c_str() << std::endl; std::cout << "...histogram : " << hout->GetName () << " / " << hist->c_str() << std::endl; std::cout << "...old scale : " << hout->Integral() << std::endl; } hout->Scale(scale); if(match(pattern, "data")){ //make sure to have an integer integral when rescaling data yields hout->Scale(int(hout->Integral())/hout->Integral()); } if(debug>1){ std::cout << "...new scale : " << hout->Integral() << std::endl; } if(armed){ if(hist->find("/")!=std::string::npos){ file->cd(hist->substr(0, hist->find("/")).c_str()); } else{ file->cd(); } std::cout << "writing to file: " << hout->GetName() << std::endl; hout->Write(hist->substr(hist->find("/")+1).c_str(), TObject::kOverwrite); } } file->Close(); return; }
void rescale2SM4(bool armed, const char* filename, double ecms=7., double mass=-1) { unsigned int debug = 1; std::vector<std::string> histnames; histnames.clear(); if( debug>0 ){ std::cout << "file = " << filename << std::setw(10); std::cout << "mass = " << mass << std::setw(10); std::cout << "armed = " << armed << std::endl; } TFile* file = new TFile(filename, "update"); TIter nextDirectory(file->GetListOfKeys()); std::vector<std::string> buffer; TKey* idir; while((idir = (TKey*)nextDirectory())){ buffer.clear(); if( idir->IsFolder() ){ file->cd(); // make sure to start in directory head if( debug>1 ){ std::cout << "Found directory: " << idir->GetName() << std::endl; } if( file->GetDirectory(idir->GetName()) ){ file->cd(idir->GetName()); // change to sub-directory buffer = signalList(idir->GetName(), "", debug); } // append to the vector of histograms to be rescaled for(std::vector<std::string>::const_iterator elem=buffer.begin(); elem!=buffer.end(); ++elem){ histnames.push_back(*elem); } if(debug>1){ std::cout << "added " << buffer.size() << " elements to histnames [" << histnames.size() << "] for directory " << idir->GetName() << std::endl; } } } // pick up histograms which are not kept in an extra folder file->cd(); buffer.clear(); buffer = signalList("", "", debug); // append to the vector of histograms to be rescaled for(std::vector<std::string>::const_iterator elem=buffer.begin(); elem!=buffer.end(); ++elem){ histnames.push_back(*elem); } if(debug>1){ std::cout << "added " << buffer.size() << " elements to histnames [" << histnames.size() << "] for file head" << std::endl; } HiggsCSandWidth smx; HiggsCSandWidthSM4 sm4; for(std::vector<std::string>::const_iterator hist=histnames.begin(); hist!=histnames.end(); ++hist){ int type = 0; // determine mass from hostogram name std::string strippedName = (hist->find("/")!=std::string::npos ? hist->substr(hist->find("/")+1) : *hist); std::string massName; if(strippedName.find("ggH")!=std::string::npos) { massName = strippedName.substr(3, 3); type = 1; } if(strippedName.find("qqH")!=std::string::npos) { massName = strippedName.substr(3, 3); type = 2; } if(strippedName.find("VH" )!=std::string::npos) { massName = strippedName.substr(2, 3); type = 3; } if( type==0 ) { std::cout << "not supported process" << std::endl; continue; } else { file->cd(); float mdx = atof(massName.c_str()); TH1F* h = (TH1F*)file->Get(hist->c_str()); float smxXS = type==1 ? smx.HiggsCS(type, mdx, ecms, true) : 0.; float smxBR = smx.HiggsBR(2, mdx, true); float sm4XS = type==1 ? sm4.HiggsCS(type, mdx, ecms, true) : 0.; float sm4BR = sm4.HiggsBR(2, mdx, true); if( debug>1 ){ std::cout << " -- hist = " << std::setw(10) << h->GetName() << std::endl << " -- type = " << std::setw(10) << type << std::endl << " -- mass = " << std::setw(10) << mdx << std::endl << " -- SM = " << std::setw(10) << smxXS*smxBR << " (BR = " << smxBR << ")" << std::endl << " -- SM4 = " << std::setw(10) << sm4XS*sm4BR << " (BR = " << sm4BR << ")" << std::endl << " -- scale = " << std::setw(10) << (type==1 ? sm4XS*sm4BR/(smxXS*smxBR) : 0) << std::endl << std::endl; } if( type==1 ){ h->Scale(sm4XS*sm4BR/(smxXS*smxBR)); } //scaling old style //if( type==2 ){ h->Scale(sm4BR/smxBR); } //if( type==3 ){ h->Scale(sm4BR/smxBR); } // scaling new style if( type==2 ){ h->Scale(0.); } if( type==3 ){ h->Scale(0.); } if(armed){ if(hist->find("/")!=std::string::npos){ file->cd(hist->substr(0, hist->find("/")).c_str()); } else{ file->cd(); } h->Write(strippedName.c_str(), TObject::kOverwrite); } } } file->Close(); return; }
void changeAxis(bool armed, TString iExprMatch,float iMin, const char* filename, const char* pattern="", unsigned int debug=0) { std::vector<std::string> histnames; histnames.clear(); if( debug>0 ){ std::cout << "file = " << filename << std::endl; std::cout << "old = " << iExprMatch.Data() << std::endl; std::cout << "armed = " << armed << std::endl; } TFile* file = new TFile(filename, "update"); TIter nextDirectory(file->GetListOfKeys()); std::vector<std::string> buffer; TKey* idir; while((idir = (TKey*)nextDirectory())){ buffer.clear(); if( idir->IsFolder() ){ file->cd(); // make sure to start in directory head if( debug>0 ){ std::cout << "Found directory: " << idir->GetName() << std::endl; } if( file->GetDirectory(idir->GetName()) ){ file->cd(idir->GetName()); // change to sub-directory buffer = signalList(idir->GetName(), pattern,iExprMatch.Data(), debug); } // append to the vector of histograms to be rescaled for(std::vector<std::string>::const_iterator elem=buffer.begin(); elem!=buffer.end(); ++elem){ histnames.push_back(*elem); } if(debug>1){ std::cout << "added " << buffer.size() << " elements to histnames [" << histnames.size() << "] for directory " << idir->GetName() << std::endl; } } } // pick up files which are not kept in an extra folder file->cd(); buffer.clear(); buffer = signalList("", pattern,iExprMatch.Data(), debug); // append to the vector of histograms to be rescaled for(std::vector<std::string>::const_iterator elem=buffer.begin(); elem!=buffer.end(); ++elem){ histnames.push_back(*elem); } if(debug>1){ std::cout << "added " << buffer.size() << " elements to histnames [" << histnames.size() << "] for file head" << std::endl; } for(std::vector<std::string>::const_iterator hist=histnames.begin(); hist!=histnames.end(); ++hist){ file->cd(); TH1F* h = (TH1F*)file->Get(hist->c_str()); std::string histName; if(hist->find("/")!=std::string::npos){ histName = hist->substr(hist->find("/")+1); } else{ histName = *hist; } TH1F* hout = (TH1F*)h->Clone(histName.c_str()); if(debug>1){ std::cout << "...folder : " << hist->substr(0, hist->find("/")).c_str() << std::endl; std::cout << "...histogram : " << hout->GetName () << " / " << hist->c_str() << std::endl; } for(int i0 = 0; i0 < hout->GetNbinsX()+1; i0++) if(hout->GetXaxis()->GetBinCenter(i0) < iMin) hout->SetBinContent(i0,0); if(debug>1){ std::cout << "...new name : " << hout->GetName() << std::endl; std::cout << "...new title : " << hout->GetTitle() << std::endl; } if(armed){ if(hist->find("/")!=std::string::npos){ file->cd(hist->substr(0, hist->find("/")).c_str()); } else{ file->cd(); } std::cout << "writing to file: " << hout->GetName() << " -- " << hist->substr(hist->find("/")+1).c_str() << std::endl; hout->Write(hist->substr(hist->find("/")+1).c_str(), TObject::kOverwrite); } } file->Close(); return; }
void blindData(const char* filename, const char* background_patterns="Fakes, EWK, ttbar, Ztt", const char* signal_patterns="ggH125, qqH125, VH125", const char* directory_patterns="*", bool armed=false, int rnd=-1, float signal_scale=1., const char* outputLabel="", unsigned int debug=1) { /// prepare input parameters std::vector<std::string> signals; string2Vector(cleanupWhitespaces(signal_patterns), signals); std::vector<std::string> samples; string2Vector(cleanupWhitespaces(background_patterns), samples); samples.insert(samples.end(), signals.begin(), signals.end()); // check if mssm or sm input file bool sm=true; if(std::string(filename).find("mssm") != std::string::npos){ sm=false; std::cerr << "INFO : MSSM File " << std::endl; } // in case data_obs is supposed to be written to an extra output file // open the file, otherwise the data_obs in the input file will be // overwritten string used_filename=string(filename); TFile* outputFile = 0; if(!std::string(outputLabel).empty()){ std::string out = std::string(used_filename); if(std::string(outputLabel).find("MSSM") != std::string::npos){ outputFile = new TFile((out.substr(0, out.rfind("."))+".root").c_str(), "update"); } // make sure data_obs in correct file for MSSM injection is changed // this is the old file which is overwritten else{ outputFile = new TFile((out.substr(0, out.rfind("."))+"_"+outputLabel+".root").c_str(), "update"); } } TKey* idir; TH1F* buffer = 0; TH1F* blind_data_obs = 0; TFile* file = new TFile(used_filename.c_str(), "update"); TIter nextDirectory(file->GetListOfKeys()); while((idir = (TKey*)nextDirectory())){ if( idir->IsFolder() ){ file->cd(); // make sure to start in directory head if( debug>0 ){ std::cerr << "Found directory: " << idir->GetName() << std::endl; } // check if we want to muck w/ this directory. For the vhtt case, we // have different background types in the same root file, so we have // to run blindData twice. if (!inPatterns(std::string(idir->GetName()), directory_patterns)) { if( debug>0 ){ std::cerr << "WARNING: Skipping directory: " << idir->GetName() << std::endl; std::cerr << " No match found in pattern: " << directory_patterns << std::endl; } continue; } if( file->GetDirectory(idir->GetName()) ){ file->cd(idir->GetName()); // change to sub-directory buffer = (TH1F*)file->Get((std::string(idir->GetName())+"/data_obs").c_str()); if(!buffer){ std::cout << "WARNING: Did not find histogram data_obs in directory: " << idir->GetName() << std::endl; std::cout << " Will skip directory: " << std::endl; continue; } blind_data_obs = (TH1F*)buffer->Clone("data_obs"); if(!samples.empty()){ blind_data_obs->Reset(); std::cout << "INFO : Blinding datacads now." << std::endl; for(std::vector<std::string>::const_iterator sample = samples.begin(); sample!=samples.end(); ++sample){ if( debug>0 ){ std::cerr << "Looking for histogram: " << (std::string(idir->GetName())+"/"+(*sample)) << std::endl; } // add special treatment for et/mt ZLL,ZJ,ZL here. You can run the // macro with ZLL, ZL, ZJ in the background samples. Those samples, // which do not apply for one or the other event category are // skipped here. if(sm==true){ if(std::string(idir->GetName()).find("vbf") != std::string::npos && (*sample == std::string("ZL") || *sample == std::string("ZJ"))){ continue; } else if(std::string(idir->GetName()).find("vbf") == std::string::npos && *sample == std::string("ZLL")){ continue; } } else{ if(std::string(idir->GetName()).find("nobtag") == std::string::npos && (*sample == std::string("ZL") || *sample == std::string("ZJ"))){ continue; } else if(std::string(idir->GetName()).find("nobtag") != std::string::npos && *sample == std::string("ZLL")){ continue; } } buffer = (TH1F*)file->Get((std::string(idir->GetName())+"/"+(*sample)).c_str()); if (!buffer) { std::cerr << "ERROR : Could not get histogram from: " << std::string(idir->GetName())+"/"+(*sample) << std::endl; std::cerr << " Histogram will be skipped : " << std::string(idir->GetName())+"/"+(*sample) << std::endl; continue; } if(inPatterns(*sample, signal_patterns)) { if( debug>1 ){ std::cerr << "INFO : Scale signal sample " << *sample << " by scale " << signal_scale << std::endl; } buffer->Scale(signal_scale); } blind_data_obs->Add(buffer); if (debug > 1){ std::cerr << "INFO : Adding: " << buffer->GetName() << " -- " << buffer->Integral() << " --> New value: " << blind_data_obs->Integral() << std::endl; } } } else{ std::cout << "INFO : Data are not blinded." << std::endl; } if(rnd>=0){ // randomize histogram; this will automatically have integer integral std::cerr << "-- R A N D O M I Z I N G --" << std::endl; randomize(blind_data_obs, rnd, debug); } else{ // use expected mean with signal injected blind_data_obs->Scale(TMath::Nint(blind_data_obs->Integral())/blind_data_obs->Integral()); // adjust uncertaintie adjustUncerts(blind_data_obs); } std::cout << "INFO : New data_obs yield: " << idir->GetName() << " " << TMath::Nint(blind_data_obs->Integral()) << std::endl; if(armed){ if (debug > 1){ std::cerr << "INFO : Writing to file: " << blind_data_obs->GetName() << std::endl; } if(outputFile){ // write to a dedicated new file with name output in case output has been specified // make sure data_obs in correct file for MSSM injection is changed // this is the old file which is overwritten if(std::string(outputLabel).find("MSSM") != std::string::npos){ outputFile->cd(idir->GetName()); blind_data_obs->Write("data_obs", TObject::kOverwrite); } else{ outputFile->mkdir(idir->GetName()); outputFile->cd(idir->GetName()); blind_data_obs->Write("data_obs"); } } else{ // override old data_obs in the inputfile otherwise file->cd(idir->GetName()); blind_data_obs->Write("data_obs", TObject::kOverwrite); } } } } } file->Close(); if(outputFile){ outputFile->Close(); } return; }