void yaxis(const char* patORpfx, const char* title) { TRegexp reg(patORpfx, kFALSE); TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); TObject* obj = 0; while (obj = iter->Next()) { if (! (obj->InheritsFrom(TH1::Class()) || obj->InheritsFrom(THStack::Class()))) continue; TString name = obj->GetName(); if (TString(patORpfx).MaybeRegexp()) { if (TString(obj->GetName()).Index(reg) < 0 ) continue; } else if (! name.BeginsWith(patORpfx)) continue; if (obj->InheritsFrom(TH1::Class())) ((TH1*)obj)->GetYaxis()->SetTitle(title); if (obj->InheritsFrom(THStack::Class())) { ((THStack*)obj)->Draw(); ((THStack*)obj)->GetYaxis()->SetTitle(title); } } }
void scalebins(const char* patORpfx, Double_t scale) { TRegexp reg(patORpfx, kFALSE); TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); TObject* obj = 0; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; TString name = obj->GetName(); if (TString(patORpfx).MaybeRegexp()) { if (TString(obj->GetName()).Index(reg) < 0 ) continue; } else if (! name.BeginsWith(patORpfx)) continue; Double_t binWidth, binContent, binError, newBinContent, newBinError; for (Int_t i = 1; i <= ((TH1*)obj)->GetNbinsX(); ++i) { binWidth = ((TH1*)obj)->GetBinWidth(i); binContent = ((TH1*)obj)->GetBinContent(i); binError = ((TH1*)obj)->GetBinError(i); newBinContent = (binContent*scale)/binWidth; newBinError = (binError*scale)/binWidth; ((TH1*)obj)->SetBinContent(i, newBinContent); ((TH1*)obj)->SetBinError(i, newBinContent); // Rename y axis with scale } } }
void normalize(const char* patORpfx) { TRegexp reg(patORpfx, kFALSE); TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); TObject* obj = 0; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; TString name = obj->GetName(); if (TString(patORpfx).MaybeRegexp()) { if (TString(obj->GetName()).Index(reg) < 0 ) continue; } else if (! name.BeginsWith(patORpfx)) continue; Double_t integral = 0; if (obj->InheritsFrom(TH2::Class())) integral = ((TH2*)obj)->Integral(); else integral = ((TH1*)obj)->Integral(); if (integral) { ((TH1*)obj)->Sumw2(); ((TH1*)obj)->Scale(1./integral); } } }
TLegend* legend(THStack* stack, Option_t* option = "lp", Bool_t addColor = kFALSE, Int_t token = -1, Float_t xmin = 0.50, Float_t ymin = 0.51, Float_t xmax = 0.85, Float_t ymax = 0.92) { if(! stack) return 0; TLegend* leg = new TLegend(xmin, ymin, xmax, ymax); TList* list = stack->GetHists(); TIterator* iter = list->MakeIterator(); TObject* obj = 0; //Hist color iterator Int_t colorIt = 1; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; if (addColor) { hist::color(obj->GetName(), colorIt); ++colorIt; } if (token == -1) leg->AddEntry(obj, obj->GetTitle(), option); else { TString name(obj->GetName()); TObjArray* a = name.Tokenize("_"); if (a->GetEntries() <= token) leg->AddEntry(obj, obj->GetName(), option); else leg->AddEntry(obj, a->At(token)->GetName(), option); } } return leg; }
void colors(TCanvas* canvas, Color_t color = 1) { if(! canvas) return 0; TList* list = canvas->GetListOfPrimitives(); TIterator* iter = list->MakeIterator(); TObject* obj = 0; //Hist color iterator Int_t colorIt = color; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; //yellow if (colorIt == 5) ++colorIt; hist::color(obj->GetName(), colorIt); if (colorIt == 40) colorIt = 1; else ++colorIt; } }
// Method by name TH1F* eff(const char* name1, const char* name2, const char* name="eff"){ // Get a list of object and their iterator TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); // Loop over objects, set the pointers TObject* obj; TH1F* h1=0; TH1F* h2=0; TString str1 = Form("%s",name1); TString str2 = Form("%s",name2); while(obj=iter->Next()) { TString objName = obj->GetName(); if (objName == str1) h1 = (TH1F*) obj; if (objName == str2) h2 = (TH1F*) obj; } // quit if not found if (h1 == 0) { cout << "Histogram " << name1 << " not found" << endl; return 0; } if (h2 == 0) { cout << "Histogram " << name2 << " not found" << endl; return 0; } // Call the method by pointer TH1F* temp = eff(h1, h2, name); return temp; }
void scaleToRate(const char* collectionName, double rateFactor) { TRegexp reg(collectionName, kTRUE); // gDirectory->ls(); TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); TObject* obj = 0; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) { // cout << "bugger" << endl; continue; } TString name = obj->GetName(); cout << "Testing name: " << name << " against " << collectionName << endl; if (TString(collectionName).MaybeRegexp()) { cout << "we have a possible match" << endl; cout << "Trying to match to " << TString(obj->GetName()) << endl; if (TString(obj->GetName()).Index(reg) < 0 ) { cout << "failure here. Argument returns " << TString(obj->GetName()).Index(reg) << endl; continue; } } else if (! name.BeginsWith(collectionName)) continue; cout << "We're trying to scale" << name << endl; ((TH1*)obj)->Scale(rateFactor); } }
void drawsame(const char* canvasName, const char* patORpfx,Option_t* drawOption = "") { // cout << "testing this method" << endl; TRegexp reg(patORpfx, kFALSE); TList* list = gDirectory->GetList() ; // cout << "this bleeping directory has " << gDirectory->GetNkeys() << "things in it" << endl; TIterator* iter = list->MakeIterator(); TObject* obj = 0; TObject* canvas = 0; Bool_t makeCanvas = false; canvas = gROOT->GetListOfCanvases()->FindObject(canvasName); //If canvas does not exist, remember to create it // cout << "found our canvas" << endl; if (! canvas) makeCanvas = true; while (obj = iter->Next()) { // cout << "We have an object" << endl; if (! obj->InheritsFrom(TH1::Class())) continue; TString name = obj->GetName(); // cout << "Testing object of name " << name << endl; // if (TString(patORpfx).MaybeRegexp()) { THIS BASICALLY ADDS IT IF IT HAS A BLEEPING PULSE if (TString(name).Contains(patORpfx)) { // cout << "possible match" << endl; if (TString(obj->GetName()).Index(reg) < 0 ) { // cout << "not a match here" << endl; continue; } else if (! name.BeginsWith(patORpfx)) { // cout << "mismatched beginning" << endl; continue; } if (makeCanvas) { canvas = new TCanvas(canvasName, canvasName); makeCanvas = false; } ((TH1*)obj)->UseCurrentStyle(); ((TCanvas*)canvas)->cd(); if (!((TCanvas*)canvas)->GetListOfPrimitives()->GetEntries()) { // cout << "Drawing with non-same option" << endl; ((TH1*)obj)->Draw(Form("%s", drawOption)); } else { // cout << "Drawing with the same option" << endl; ((TH1*)obj)->Draw(Form("SAME%s", drawOption)); } } } hist::colors((TCanvas*)canvas); }
void deleteHistos() { // Delete all existing histograms in memory TObject* obj; TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); while (obj=iter->Next()) { if (obj->IsA()->InheritsFrom(TH1::Class()) || obj->IsA()->InheritsFrom(TH2::Class()) ) {delete obj;} } }
// Multiply everything by a scale factor void normalize_all_by_single_SF(double factor) { TList* list = gDirectory->GetList(); TIterator* iter = list->MakeIterator(); TObject* obj = 0; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; ((TH1F*)obj)->Scale(factor); } }
// Sumw2 all your bleep if you're a muppet (like Tom) and forgot to do it in // your analysis code void sumw2Everything() { TList* list = gDirectory->GetList(); TIterator* iter = list->MakeIterator(); TObject* obj = 0; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; ((TH1F*)obj)->Sumw2(); } }
// normalize all your bleep to 1 void normalizeAllToUnity() { TList* list = gDirectory->GetList(); TIterator* iter = list->MakeIterator(); TObject* obj = 0; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; int nBins = ((TH1F*)obj)->GetNbinsX(); Float_t integral = ((TH1F*)obj)->Integral(1, nBins+1); if (integral > 0) ((TH1F*)obj)->Scale(1/integral); } }
void styles(TCanvas* canvas, Style_t style = 20) { if(! canvas) return 0; TList* list = canvas->GetListOfPrimitives(); TIterator* iter = list->MakeIterator(); TObject* obj = 0; //Hist style iterator Int_t styleIt = style; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; ((TH1*)obj)->SetMarkerStyle(styleIt); ++styleIt; } }
void styles(THStack* stack, Style_t style = 20) { if(! stack) return 0; TList* list = stack->GetHists(); TIterator* iter = list->MakeIterator(); TObject* obj = 0; //Hist style iterator Int_t styleIt = style; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; ((TH1*)obj)->SetMarkerStyle(styleIt); ++styleIt; } }
void stack(const char* stackHistName, const char* patORpfx, Bool_t addColor = kFALSE, Option_t* drawOption = "") { // cout << "bleep1" << endl; TRegexp reg(patORpfx, kFALSE); TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); TObject* obj = 0; TObject* stack = 0; Bool_t makeStackHist = false; stack = gDirectory->Get(stackHistName); //If stack hist does not exist, remember to create it if (! stack) makeStackHist = true; //Hist color iterator Int_t colorIt = 1; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; TString name = obj->GetName(); if (TString(patORpfx).MaybeRegexp()) { if (TString(obj->GetName()).Index(reg) < 0 ) continue; } else if (! name.BeginsWith(patORpfx)) continue; if (makeStackHist) { stack = new THStack(stackHistName, stackHistName); makeStackHist = false; } if (addColor) { cout << "calling the color method" << endl; hist::color(obj->GetName(), colorIt); ++colorIt; } ((THStack*)stack)->Add((TH1*)obj, drawOption); } // Currently breaks .ls gDirectory->Append(stack); }
void setrangey(TCanvas* canvas, Double_t maxy = -999999, Double_t miny = 999999) { if(! canvas) return 0; TList* list = canvas->GetListOfPrimitives(); TIterator* iter = list->MakeIterator(); TObject* obj = 0; TObject* top = 0; while (obj = iter->Next()) { // cout << "iterating over objects, should be 2 in this case" << endl; if (! obj->InheritsFrom(TH1::Class())) { // cout << "bleep, stacks present a problem" << endl; continue; } if (! top) top = obj; // cout << "Make sure we are looking at different objects\n"; // cout << "max for this object: " << ((TH1*)obj)->GetMaximum() << endl; if (((TH1*)obj)->GetMaximum() > maxy) maxy = ((TH1*)obj)->GetMaximum(); // JAKE Set to log scale if max/min > 10 if (canvas->GetLogy()) { //protect against user supplied argument if (miny == 0) miny = 999999; for(Int_t bin = 1; bin <= ((TH1*)obj)->GetNbinsX(); ++bin) { Double_t binContent = ((TH1*)obj)->GetBinContent(bin); if (binContent != 0 && binContent < miny) miny = binContent; } } else if (((TH1*)obj)->GetMinimum() < miny) miny = ((TH1*)obj)->GetMinimum(); } cout << "miny, maxy = " << miny <<" "<<maxy << endl; ((TH1*)top)->SetMaximum(maxy*1.3); ((TH1*)top)->SetMinimum(miny*0.7); }
saveHist(const char* filename, const char* pat="*") { TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); TRegexp re(pat,kTRUE) ; TFile outf(filename,"RECREATE") ; while(obj=iter->Next()) { if (TString(obj->GetName()).Index(re)>=0) { obj->Write() ; cout << "." ; cout.flush() ; } } cout << endl ; outf.Close() ; delete iter ; }
void add(const char* outHistName, const char* patORpfx) { TRegexp reg(patORpfx, kFALSE); TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); TObject* obj = 0; TObject* hist = 0; Bool_t makeOutHist = false; hist = gDirectory->Get(outHistName); //If out hist does not exist, remember to create it if (! hist) makeOutHist = true; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; TString name = obj->GetName(); //Don't add out hist if (name == TString(outHistName)) continue; if (TString(patORpfx).MaybeRegexp()) { if (TString(obj->GetName()).Index(reg) < 0 ) continue; } else if (! name.BeginsWith(patORpfx)) continue; if (makeOutHist) { hist = obj->Clone(outHistName); if (hist->InheritsFrom(TH2::Class())) ((TH2*)hist)->Reset(); else ((TH1*)hist)->Reset(); ((TH1*)hist)->SetTitle(outHistName); ((TH1*)hist)->Sumw2(); makeOutHist = false; } ((TH1*)hist)->Add((TH1*)obj); } }
void saveHist(const char* filename, const char* pat) { TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); TRegexp re(pat,kTRUE) ; TFile outf(filename,"RECREATE") ; TObject* obj ; while((obj=iter->Next())) { if (TString(obj->GetName()).Index(re)>=0) { obj->Write() ; std::cout << "." ; // cout << setw(9) << counter++ << " : " << obj->GetName() << std::endl ; } } std::cout << std::endl ; outf.Close() ; delete iter ; }
void scale(const char* patORpfx, Double_t scale,Option_t* drawOption = "") { TRegexp reg(patORpfx, kFALSE); TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); TObject* obj = 0; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; TString name = obj->GetName(); if (TString(patORpfx).MaybeRegexp()) { if (TString(obj->GetName()).Index(reg) < 0 ) continue; } else if (! name.BeginsWith(patORpfx)) continue; ((TH1*)obj)->Sumw2(); ((TH1*)obj)->Scale(scale); } }
void hatch(const char* patORpfx, Int_t hatch) { TRegexp reg(patORpfx, kFALSE); TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); TObject* obj = 0; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; TString name = obj->GetName(); if (TString(patORpfx).MaybeRegexp()) { if (TString(obj->GetName()).Index(reg) < 0 ) continue; } else if (! name.BeginsWith(patORpfx)) continue; ((TH1*)obj)->SetFillStyle(hatch); } }
TLegend* legend(TCanvas* canvas, Option_t* option = "lpf", Bool_t addColor = kFALSE, Int_t token = -1, Float_t xmin = 0.75, Float_t ymin = 0.75, Float_t xmax = 0.99, Float_t ymax = 0.99) { if(! canvas) return 0; TLegend* leg = new TLegend(xmin, ymin, xmax, ymax); TList* list = canvas->GetListOfPrimitives(); TIterator* iter = list->MakeIterator(); TObject* obj = 0; //Hist color iterator Int_t colorIt = 1; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; if (addColor) { hist::color(obj->GetName(), colorIt); ++colorIt; } if (token == -1) { // cout << "Adding to TLegend " << obj->GetName() << endl; leg->AddEntry(obj, obj->GetName()); } else { TString name(obj->GetName()); TObjArray* a = name.Tokenize("_"); if (a->GetEntries() <= token) leg->AddEntry(obj, obj->GetName(), option); else leg->AddEntry(obj, a->At(token)->GetName(), option); } } return leg; }
void saveHist(const char* filename, const char* pat) { cout << "\n\n Saving histograms matching " << pat << " in file " << filename << "\n\n" << flush ; TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); TRegexp re(pat,kTRUE) ; TFile outf(filename,"RECREATE") ; TObject* obj ; while((obj=iter->Next())) { if (TString(obj->GetName()).Index(re)>=0) { obj->Write() ; std::cout << "." ; } } std::cout << std::endl ; outf.Close() ; delete iter ; }
void colors(THStack* stack, Color_t color = 1) { if(! stack) return 0; // cout << "Time for some bullbleep debugging." << endl; TList* list = stack->GetHists(); TIterator* iter = list->MakeIterator(); TObject* obj = 0; //Hist color iterator Int_t colorIt = color; // cout << "colorIt = " << colorIt << endl; while (obj = iter->Next()) { // cout << "in the while loop" << endl; if (! obj->InheritsFrom(TH1::Class())) continue; // cout << "colorIt = " << colorIt << endl; // hist::color(obj->GetName(), colorIt); ((TH1F*)obj)->SetFillColor(colorIt); ((TH1F*)obj)->SetMarkerColor(colorIt); int markerNumber = colorIt + 19; ((TH1F*)obj)->SetMarkerStyle(markerNumber); ((TH1F*)obj)->SetFillColor(colorIt); ((TH1F*)obj)->SetMarkerColor(colorIt); if (markerNumber >= 30) markerNumber = 1; if (colorIt == 50) colorIt = 1; else if (colorIt >= 9 && colorIt < 20) colorIt += 30; else ++colorIt; } }
void color(const char* patORpfx, Color_t color) { // cout << "color = " << color << endl; // if (color == 1) color+=20; TRegexp reg(patORpfx, kFALSE); TList* list = gDirectory->GetList() ; TIterator* iter = list->MakeIterator(); TObject* obj = 0; while (obj = iter->Next()) { if (! obj->InheritsFrom(TH1::Class())) continue; TString name = obj->GetName(); if (TString(patORpfx).MaybeRegexp()) { if (TString(obj->GetName()).Index(reg) < 0 ) continue; } else if (! name.BeginsWith(patORpfx)) continue; // ((TH1*)obj)->SetFillColor(color); ((TH1*)obj)->SetLineColor(color); ((TH1*)obj)->SetMarkerColor(color); } }
void splitws(string inFolderName, double mass, string channel) { cout << "Splitting workspace in " << channel << endl; int flatInterpCode = 4; int shapeInterpCode = 4; bool do2011 = 0; if (inFolderName.find("2011") != string::npos) do2011 = 1; bool conditionalAsimov = 0; bool doData = 1; //if (inFolderName.find("_blind_") != string::npos) { //conditionalAsimov = 0; //} //else { //conditionalAsimov = 1; //} set<string> channelNames; if (channel == "01j") { channelNames.insert("em_signalLike1_0j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike2_0j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike1_0j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike2_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_AfrecSR_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_ASR_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_AfrecSR_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_ASR_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CfrecZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CfrecZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_0j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike1_1j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike2_1j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike1_1j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike2_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_AfrecSR_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_ASR_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_AfrecSR_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_ASR_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CfrecZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CfrecZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_topbox_1j"+string(!do2011?"_2012":"")); } else if (channel == "0j") { channelNames.insert("em_signalLike1_0j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike2_0j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike1_0j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike2_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_AfrecSR_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_ASR_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_AfrecSR_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_ASR_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CfrecZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CfrecZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_0j"+string(!do2011?"_2012":"")); } else if (channel == "1j") { channelNames.insert("em_signalLike1_1j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike2_1j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike1_1j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike2_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_AfrecSR_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_ASR_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_AfrecSR_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_ASR_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CfrecZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CfrecZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_topbox_1j"+string(!do2011?"_2012":"")); } else if (channel == "OF01j") { channelNames.insert("em_signalLike1_0j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike2_0j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike1_0j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike2_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_sscr_0j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike1_1j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike2_1j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike1_1j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike2_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_topbox_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_sscr_1j"+string(!do2011?"_2012":"")); } else if (channel == "OF0j") { channelNames.insert("em_signalLike1_0j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike2_0j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike1_0j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike2_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_sscr_0j"+string(!do2011?"_2012":"")); } else if (channel == "OF1j") { channelNames.insert("em_signalLike1_1j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike2_1j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike1_1j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike2_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_topbox_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_sscr_1j"+string(!do2011?"_2012":"")); } else if (channel == "SF01j") { channelNames.insert("SF_AfrecSR_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_ASR_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_AfrecSR_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_ASR_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CfrecZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CfrecZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_AfrecSR_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_ASR_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_AfrecSR_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_ASR_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CfrecZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CfrecZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_topbox_1j"+string(!do2011?"_2012":"")); } else if (channel == "SF0j") { channelNames.insert("SF_AfrecSR_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_ASR_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_AfrecSR_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_ASR_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CfrecZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CfrecZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_0j"+string(!do2011?"_2012":"")); } else if (channel == "SF1j") { channelNames.insert("SF_AfrecSR_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_ASR_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_AfrecSR_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_ASR_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CfrecZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CfrecZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_topbox_1j"+string(!do2011?"_2012":"")); } else if (channel == "2j") { channelNames.insert("em_signalLike1_2j"+string(!do2011?"_2012":"")); channelNames.insert("ee_signalLike1_2j"+string(!do2011?"_2012":"")); channelNames.insert("SF_topbox_2j"+string(!do2011?"_2012":"")); } else if (channel == "OF2j") { channelNames.insert("em_signalLike1_2j"+string(!do2011?"_2012":"")); channelNames.insert("SF_topbox_2j"+string(!do2011?"_2012":"")); } else if (channel == "SF2j") { channelNames.insert("ee_signalLike1_2j"+string(!do2011?"_2012":"")); channelNames.insert("SF_topbox_2j"+string(!do2011?"_2012":"")); } else if (channel == "OF") { channelNames.insert("em_signalLike1_0j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike2_0j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike1_0j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike2_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_0j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike1_1j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike2_1j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike1_1j"+string(!do2011?"_2012":"")); channelNames.insert("me_signalLike2_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_topbox_1j"+string(!do2011?"_2012":"")); channelNames.insert("em_signalLike1_2j"+string(!do2011?"_2012":"")); channelNames.insert("SF_topbox_2j"+string(!do2011?"_2012":"")); } else if (channel == "SF") { channelNames.insert("SF_AfrecSR_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_ASR_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_AfrecSR_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_ASR_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CfrecZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CfrecZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CZpeak_0j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_0j"+string(!do2011?"_2012":"")); channelNames.insert("SF_AfrecSR_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_ASR_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_AfrecSR_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_ASR_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CfrecZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("SF_CZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CfrecZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_CZpeak_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_mainControl_1j"+string(!do2011?"_2012":"")); channelNames.insert("OF_topbox_1j"+string(!do2011?"_2012":"")); channelNames.insert("ee_signalLike1_2j"+string(!do2011?"_2012":"")); channelNames.insert("SF_topbox_2j"+string(!do2011?"_2012":"")); } else { cout << "Channel " << channel << " not defined. Please check!" << endl; exit(1); } // bool fix = 1; stringstream inFileName; inFileName << "workspaces/" << inFolderName << "/" << mass << ".root"; TFile f(inFileName.str().c_str()); RooWorkspace* w = (RooWorkspace*)f.Get("combWS"); if (!w) w = (RooWorkspace*)f.Get("combined"); RooDataSet* data = (RooDataSet*)w->data("combData"); if (!data) data = (RooDataSet*)w->data("obsData"); ModelConfig* mc = (ModelConfig*)w->obj("ModelConfig"); RooRealVar* weightVar = w->var("weightVar"); RooRealVar* mu = (RooRealVar*)mc->GetParametersOfInterest()->first(); if (!mu) mu = w->var("SigXsecOverSM"); const RooArgSet* mc_obs = mc->GetObservables(); const RooArgSet* mc_nuis = mc->GetNuisanceParameters(); const RooArgSet* mc_globs = mc->GetGlobalObservables(); const RooArgSet* mc_poi = mc->GetParametersOfInterest(); RooArgSet nuis = *mc_nuis; RooArgSet antiNuis = *mc_nuis; RooArgSet globs = *mc_globs; RooArgSet antiGlobs = *mc_globs; RooArgSet allParams; RooSimultaneous* simPdf = (RooSimultaneous*)mc->GetPdf(); RooCategory* cat = (RooCategory*)&simPdf->indexCat(); RooArgSet nuis_tmp = nuis; RooArgSet fullConstraints = *simPdf->getAllConstraints(*mc_obs,nuis_tmp,false); vector<string> foundChannels; vector<string> skippedChannels; cout << "Getting constraints" << endl; map<string, RooDataSet*> data_map; map<string, RooAbsPdf*> pdf_map; RooCategory* decCat = new RooCategory("dec_channel","dec_channel"); // int i = 0; TIterator* catItr = cat->typeIterator(); RooCatType* type; RooArgSet allConstraints; while ((type = (RooCatType*)catItr->Next())) { RooAbsPdf* pdf = simPdf->getPdf(type->GetName()); string typeName(type->GetName()); if (channelNames.size() && channelNames.find(typeName) == channelNames.end()) { skippedChannels.push_back(typeName); continue; } cout << "On channel " << type->GetName() << endl; foundChannels.push_back(typeName); decCat->defineType(type->GetName()); // pdf->getParameters(*data)->Print("v"); RooArgSet nuis_tmp1 = nuis; RooArgSet nuis_tmp2 = nuis; RooArgSet* constraints = pdf->getAllConstraints(*mc_obs, nuis_tmp1, true); constraints->Print(); allConstraints.add(*constraints); } catItr->Reset(); while ((type = (RooCatType*)catItr->Next())) { RooAbsPdf* pdf = simPdf->getPdf(type->GetName()); string typeName(type->GetName()); cout << "Considering type " << typeName << endl; if (channelNames.size() && channelNames.find(typeName) == channelNames.end()) continue; cout << "On channel " << type->GetName() << endl; RooArgSet nuis_tmp1 = nuis; RooArgSet nuis_tmp2 = nuis; RooArgSet* constraints = pdf->getAllConstraints(*mc_obs, nuis_tmp1, true); cout << "Adding pdf to map: " << typeName << " = " << pdf->GetName() << endl; pdf_map[typeName] = pdf; RooProdPdf prod("prod","prod",*constraints); RooArgSet* params = pdf->getParameters(*data); antiNuis.remove(*params); antiGlobs.remove(*params); allParams.add(*params); // cout << type->GetName() << endl; } // return; RooArgSet decNuis; TIterator* nuiItr = mc_nuis->createIterator(); TIterator* parItr = allParams.createIterator(); RooAbsArg* nui, *par; while ((par = (RooAbsArg*)parItr->Next())) { nuiItr->Reset(); while ((nui = (RooAbsArg*)nuiItr->Next())) { if (par == nui) decNuis.add(*nui); } } RooArgSet decGlobs; TIterator* globItr = mc_globs->createIterator(); parItr->Reset(); RooAbsArg* glob; while ((par = (RooAbsArg*)parItr->Next())) { globItr->Reset(); while ((glob = (RooAbsArg*)globItr->Next())) { if (par == glob) decGlobs.add(*glob); } } // antiNuis.Print(); // nuis.Print(); // globs.Print(); // i = 0; TList* datalist = data->split(*cat, true); TIterator* dataItr = datalist->MakeIterator(); RooAbsData* ds; while ((ds = (RooAbsData*)dataItr->Next())) { string typeName(ds->GetName()); if (channelNames.size() && channelNames.find(typeName) == channelNames.end()) continue; cout << "Adding dataset to map: " << ds->GetName() << endl; data_map[string(ds->GetName())] = (RooDataSet*)ds; cout << ds->GetName() << endl; } RooSimultaneous* decPdf = new RooSimultaneous("decPdf","decPdf",pdf_map,*decCat); RooArgSet decObs = *decPdf->getObservables(data); // decObs.add(*(RooAbsArg*)weightVar); decObs.add(*(RooAbsArg*)decCat); decObs.Print(); nuis.remove(antiNuis); globs.remove(antiGlobs); // nuis.Print("v"); RooDataSet* decData = new RooDataSet("obsData","obsData",RooArgSet(decObs,*(RooAbsArg*)weightVar),Index(*decCat),Import(data_map),WeightVar(*weightVar)); decData->Print(); RooArgSet poi(*(RooAbsArg*)mu); RooWorkspace decWS("combined"); ModelConfig decMC("ModelConfig",&decWS); decMC.SetPdf(*decPdf); decMC.SetObservables(decObs); decMC.SetNuisanceParameters(decNuis); decMC.SetGlobalObservables(decGlobs); decMC.SetParametersOfInterest(poi); decMC.Print(); decWS.import(*decPdf); decWS.import(decMC); decWS.import(*decData); // decWS.Print(); ModelConfig* mcInWs = (ModelConfig*)decWS.obj("ModelConfig"); decPdf = (RooSimultaneous*)mcInWs->GetPdf(); // setup(mcInWs); // return; mcInWs->GetNuisanceParameters()->Print("v"); mcInWs->GetGlobalObservables()->Print("v"); // decData->tree()->Scan("*"); // Make asimov data RooArgSet funcs = decWS.allFunctions(); TIterator* it = funcs.createIterator(); TObject* tempObj = 0; while((tempObj=it->Next())) { FlexibleInterpVar* flex = dynamic_cast<FlexibleInterpVar*>(tempObj); if(flex) { flex->setAllInterpCodes(flatInterpCode); } PiecewiseInterpolation* piece = dynamic_cast<PiecewiseInterpolation*>(tempObj); if(piece) { piece->setAllInterpCodes(shapeInterpCode); } } RooDataSet* dataInWs = (RooDataSet*)decWS.data("obsData"); makeAsimovData(mcInWs, conditionalAsimov && doData, &decWS, mcInWs->GetPdf(), dataInWs, 0); makeAsimovData(mcInWs, conditionalAsimov && doData, &decWS, mcInWs->GetPdf(), dataInWs, 1); makeAsimovData(mcInWs, conditionalAsimov && doData, &decWS, mcInWs->GetPdf(), dataInWs, 2); system(("mkdir -vp workspaces/"+inFolderName+"_"+channel).c_str()); stringstream outFileName; outFileName << "workspaces/" << inFolderName << "_" << channel << "/" << mass << ".root"; cout << "Exporting" << endl; decWS.writeToFile(outFileName.str().c_str()); cout << "\nIncluded the following channels: " << endl; for (int i=0;i<(int)foundChannels.size();i++) { cout << "-> " << foundChannels[i] << endl; } cout << "\nSkipping the following channels: " << endl; for (int i=0;i<(int)skippedChannels.size();i++) { cout << "-> " << skippedChannels[i] << endl; } cout << "Done" << endl; // decPdf->fitTo(*decData, Hesse(0), Minos(0), PrintLevel(0)); }
void loadHist(const char* filename, const char* pfx, const char* pat, Bool_t doAdd, Double_t scaleFactor) { cout << " Reading histograms from file: " << filename << endl << flush ; TFile inf(filename) ; //inf.ReadAll() ; TList* list = inf.GetListOfKeys() ; TIterator* iter = list->MakeIterator(); TRegexp re(pat,kTRUE) ; std::cout << "pat = " << pat << std::endl ; gDirectory->cd("Rint:") ; TObject* obj ; TKey* key ; std::cout << "doAdd = " << (doAdd?"T":"F") << std::endl ; std::cout << "loadHist: reading." ; while((key=(TKey*)iter->Next())) { Int_t ridx = TString(key->GetName()).Index(re) ; if (ridx==-1) { continue ; } obj = inf.Get(key->GetName()) ; TObject* clone ; if (pfx) { // Find existing TH1-derived objects TObject* oldObj = 0 ; if (doAdd){ oldObj = gDirectory->Get(Form("%s_%s",pfx,obj->GetName())) ; if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) { oldObj = 0 ; } } if (oldObj) { clone = oldObj ; if ( scaleFactor > 0 ) { ((TH1*)clone)->Sumw2() ; ((TH1*)clone)->Add((TH1*)obj, scaleFactor) ; } else { ((TH1*)clone)->Add((TH1*)obj) ; } } else { clone = obj->Clone(Form("%s_%s",pfx,obj->GetName())) ; } } else { // Find existing TH1-derived objects TObject* oldObj = 0 ; if (doAdd){ oldObj = gDirectory->Get(key->GetName()) ; if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) { oldObj = 0 ; } } if (oldObj) { clone = oldObj ; if ( scaleFactor > 0 ) { ((TH1*)clone)->Sumw2() ; ((TH1*)clone)->Add((TH1*)obj, scaleFactor) ; } else { ((TH1*)clone)->Add((TH1*)obj) ; } } else { clone = obj->Clone() ; } } if ( scaleFactor > 0 && !doAdd ) { ((TH1*) clone)->Sumw2() ; ((TH1*) clone)->Scale(scaleFactor) ; } if (!gDirectory->GetList()->FindObject(clone)) { gDirectory->Append(clone) ; } std::cout << "." ; std::cout.flush() ; } std::cout << std::endl; inf.Close() ; delete iter ; }
//put very small data entries in a binned dataset to avoid unphysical pdfs, specifically for H->ZZ->4l RooDataSet* makeData(RooDataSet* orig, RooSimultaneous* simPdf, const RooArgSet* observables, RooRealVar* firstPOI, double mass, double& mu_min) { double max_soverb = 0; mu_min = -10e9; map<string, RooDataSet*> data_map; firstPOI->setVal(0); RooCategory* cat = (RooCategory*)&simPdf->indexCat(); TList* datalist = orig->split(*(RooAbsCategory*)cat, true); TIterator* dataItr = datalist->MakeIterator(); RooAbsData* ds; RooRealVar* weightVar = new RooRealVar("weightVar","weightVar",1); while ((ds = (RooAbsData*)dataItr->Next())) { string typeName(ds->GetName()); cat->setLabel(typeName.c_str()); RooAbsPdf* pdf = simPdf->getPdf(typeName.c_str()); cout << "pdf: " << pdf << endl; RooArgSet* obs = pdf->getObservables(observables); cout << "obs: " << obs << endl; RooArgSet obsAndWeight(*obs, *weightVar); obsAndWeight.add(*cat); stringstream datasetName; datasetName << "newData_" << typeName; RooDataSet* thisData = new RooDataSet(datasetName.str().c_str(),datasetName.str().c_str(), obsAndWeight, WeightVar(*weightVar)); RooRealVar* firstObs = (RooRealVar*)obs->first(); //int ibin = 0; int nrEntries = ds->numEntries(); for (int ib=0;ib<nrEntries;ib++) { const RooArgSet* event = ds->get(ib); const RooRealVar* thisObs = (RooRealVar*)event->find(firstObs->GetName()); firstObs->setVal(thisObs->getVal()); firstPOI->setVal(0); double b = pdf->expectedEvents(*firstObs)*pdf->getVal(obs); firstPOI->setVal(1); double s = pdf->expectedEvents(*firstObs)*pdf->getVal(obs) - b; if (s > 0) { mu_min = max(mu_min, -b/s); double soverb = s/b; if (soverb > max_soverb) { max_soverb = soverb; cout << "Found new max s/b: " << soverb << " in pdf " << pdf->GetName() << " at m = " << thisObs->getVal() << endl; } } if (b == 0 && s != 0) { cout << "Expecting non-zero signal and zero bg at m=" << firstObs->getVal() << " in pdf " << pdf->GetName() << endl; } if (s+b <= 0) { cout << "expecting zero" << endl; continue; } double weight = ds->weight(); if ((typeName.find("ATLAS_H_4mu") != string::npos || typeName.find("ATLAS_H_4e") != string::npos || typeName.find("ATLAS_H_2mu2e") != string::npos || typeName.find("ATLAS_H_2e2mu") != string::npos) && fabs(firstObs->getVal() - mass) < 10 && weight == 0) { cout << "adding event: " << firstObs->getVal() << endl; thisData->add(*event, pow(10., -9.)); } else { //weight = max(pow(10.0, -9), weight); thisData->add(*event, weight); } } data_map[string(ds->GetName())] = (RooDataSet*)thisData; } RooDataSet* newData = new RooDataSet("newData","newData",RooArgSet(*observables, *weightVar), Index(*cat), Import(data_map), WeightVar(*weightVar)); orig->Print(); newData->Print(); //newData->tree()->Scan("*"); return newData; }
void addn2() { // open existing f1: char* fn[99]; int ni; // SR90 maps, mod D003 ni = -1; ni++; fn[ni] = "SR90-map-00a.root"; ni++; fn[ni] = "SR90-map-00b.root"; ni++; fn[ni] = "SR90-map-01a.root"; ni++; fn[ni] = "SR90-map-01b.root"; ni++; fn[ni] = "SR90-map-02.root"; ni++; fn[ni] = "SR90-map-03.root"; ni++; fn[ni] = "SR90-map-05a.root"; ni++; fn[ni] = "SR90-map-05b.root"; ni++; fn[ni] = "SR90-map-07a.root"; ni++; fn[ni] = "SR90-map-07b.root"; ni++; fn[ni] = "SR90-map-07c.root"; ni++; fn[ni] = "SR90-map-07d.root"; // direct X-rays 9.4.2014 module D0003 ni = -1; ni++; fn[ni] = "X-ray-20-modtd40000.root"; ni++; fn[ni] = "X-ray-20-vthr60-modtd1000.root"; ni++; fn[ni] = "X-ray-27-vthr60-modtd1000.root"; ni++; fn[ni] = "X-ray-35-modtd40000.root"; ni++; fn[ni] = "X-ray-35-vthr60-close-modtd65000.root"; ni++; fn[ni] = "X-ray-35-vthr60-midpos-0p1mA-modtd10000.root"; ni++; fn[ni] = "X-ray-35-vthr60-midpos-halfcur-modtd40000.root"; ni++; fn[ni] = "X-ray-35-vthr60-modtd40000.root"; int nmax = ni; TFile f1(fn[0]); if( f1.IsZombie() ) { cout << "Error opening " << fn[0] << endl; return; } cout << "opened " << fn[0] << endl; //-------------------------------------------------------------------- // create f0: TFile f0("fileA.root", "RECREATE"); cout << "created "; gDirectory->pwd(); /* TFile options: NEW or CREATE create a new file and open it for writing, if the file already exists the file is not opened. RECREATE create a new file, if the file already exists it will be overwritten. UPDATE open an existing file for writing. if no file exists, it is created. READ open an existing file for reading (default). NET used by derived remote file access classes, not a user callable option WEB used by derived remote http access class, not a user callable option "" (default), READ is assumed. */ //-------------------------------------------------------------------- // copy f1 to f0: f1.cd(); cout << "keys:\n"; f1.GetListOfKeys()->Print(); cout << "pwd: "; f1.pwd(); cout << "ls: \n"; f1.ls(); // f1 has sub-dir: cout << "First: " << f1.GetListOfKeys()->First()->GetName() << endl; cout << "First: " << f1.GetListOfKeys()->First()->ClassName() << endl; char* dir1 = f1.GetListOfKeys()->First()->GetName(); cout << "cd to " << dir1 << endl; f1.cd( dir1 ); cout << "we are in "; gDirectory->pwd(); gDirectory->ReadAll(); // load histos TList * lst = gDirectory->GetList(); cout << lst->GetName() << endl; cout << lst->GetTitle() << endl; cout << "size " << lst->GetSize() << endl; cout << "entries " << lst->GetEntries() << endl; cout << "last " << lst->LastIndex() << endl; TIterator *iter = lst->MakeIterator(); int ii = 0; TObject *obj; TH1D *h; TH1D *h0; TH2D *H; TH2D *H0; while( obj = iter->Next() ){ ii++; cout << setw(4) << ii << ": "; cout << obj->ClassName() << " "; cout << obj->InheritsFrom("TH1D") << " "; cout << obj->GetName() << " \""; cout << obj->GetTitle() << "\""; cout << endl; // if( obj->ClassName() == "TH1D" ){ if( obj->InheritsFrom("TH1D") ){ h = (TH1D*) obj; cout << " 1D"; cout << h->GetNbinsX() << " bins, "; cout << h->GetEntries() << " entries, "; cout << h->GetSumOfWeights() << " inside, "; cout << h->GetBinContent(0) << " under, "; cout << h->GetBinContent(h->GetNbinsX()+1) << " over"; cout << endl; f0.cd(); // output file // TH1D* h0 = (TH1D*) h->Clone(); h0 = h; // copy h0->Write(); // write to file f0 f1.cd(); // back to file 1 for the loop } else{ if( obj->InheritsFrom("TH2D") ){ H = (TH2D*) obj; cout << " 2D"; cout << H->GetNbinsX() << " bins, "; cout << H->GetEntries() << " entries, "; cout << H->GetSumOfWeights() << " inside, "; cout << H->GetBinContent(0) << " under, "; cout << H->GetBinContent(H->GetNbinsX()+1) << " over"; cout << endl; f0.cd(); // output file H0 = H; // copy H0->Write(); // write to file f0 f1.cd(); // back to file 1 for the loop } else cout << "other class " << obj->ClassName() << endl; } } cout << "copied " << ii << endl; cout << "f1 " << f1.GetName() << " close = " << f1.Close() << endl; f0.cd(); cout << "we are in "; gDirectory->pwd(); cout << "f0 " << f0.GetName() << " size = " << f0.GetSize() << endl; cout << "f0 " << f0.GetName() << " write = " << f0.Write() << endl; cout << "f0 " << f0.GetName() << " size = " << f0.GetSize() << endl; cout << "f0 " << f0.GetName() << " close = " << f0.Close() << endl; f0.Delete(); //-------------------------------------------------------------------- // list of files 2: bool lAB = true; for( int nn = 1; nn <= nmax; ++nn ){ cout << "\n\n"; cout << "loop " << nn << ": fn = " << fn[nn] << endl; cout << "lAB = " << lAB << endl; if( lAB ) { // A+2 -> B char* fn3 = "fileA.root"; char* fn4 = "fileB.root"; } else{ // B+2 -> A char* fn3 = "fileB.root"; char* fn4 = "fileA.root"; } // create f4: TFile f4( fn4, "recreate" ); if( f4.IsZombie() ) { cout << "Error creating f4\n"; return; } cout << "created f4 = " << f4.GetName() << endl; // re-open as f3: TFile f3( fn3 ); if( f3.IsZombie() ) { cout << "Error opening f3\n"; return; } cout << "re-opened f3 = " << f3.GetName() << endl; lAB = !lAB; cout << "lAB = " << lAB << " for next loop\n"; cout << "f4 = " << f4.GetName() << endl; cout << "f3 = " << f3.GetName() << endl; f3.cd(); gDirectory->ReadAll(); // load histos into f3 memory // cout << "f3 list size = " << gDirectory->GetList()->GetSize() << endl; cout << "f3 list size = " << gDirectory->GetList()->GetSize() << endl; TFile f2(fn[nn]); // TFile *f2 = new TFile(fn[nn]); if( f2.IsZombie() ) { // if( f2 == NULL ) { cout << "Error opening " << fn[nn] << endl; return; } cout << "opened " << fn[nn] << endl; // f2 has sub-dir: f2.cd( f2.GetListOfKeys()->First()->GetName() ); cout << "we are in "; gDirectory->pwd(); gDirectory->ReadAll(); // load histos into f2 memory // loop over f2: cout << "f2 list size = " << gDirectory->GetList()->GetSize() << endl; int jj = 0; TObject *ob2; TH1D *h2; TH1D *h3; TH2D *H2; TH2D *H3; TIterator *ite2 = gDirectory->GetList()->MakeIterator(); while( ob2 = ite2->Next() ){ jj++; // if( jj > 9 ) continue; cout << jj << ". "; cout << "ob2 is "; cout << ob2->GetName() << " "; cout << ob2->ClassName() << " "; cout << ob2->GetTitle(); cout << endl; if( ob2->InheritsFrom("TH1D") ) { h2 = (TH1D*) ob2; cout << "h2 " << h2->GetName() << " " << h2->GetNbinsX() << " bins\n"; cout << "h2 " << h2->GetName() << " " << h2->GetEntries() << " entries\n"; char* hnm2 = h2->GetName(); // search in f3: cout << "search for " << hnm2 << " in f3\n"; f3.cd(); cout << "we are in "; gDirectory->pwd(); h3 = (TH1D*) gDirectory->GetList()->FindObject(hnm2); if( h3 == NULL ) { cout << "h3 is null\n" ; continue; } cout << "found h3 = "; cout << h3->GetName() << " "; cout << h3->ClassName() << " "; cout << h3->GetTitle(); cout << endl; cout << "h3 " << h3->GetName() << " " << h3->GetNbinsX() << " bins\n"; cout << "h3 " << h3->GetName() << " " << h3->GetEntries() << " entries\n"; // add: f4.cd(); cout << "we are in "; gDirectory->pwd(); //TH1D h4 = *h3 + *h2; TH1D* h4 = (TH1D*) h3->Clone(); h4->Add(h2); cout << "h4 " << h4->GetEntries() << " entries\n"; cout << "h4 "; cout << h4->GetName() << " "; cout << h4->ClassName() << " "; cout << h4->GetTitle(); cout << endl; cout << "h4 dir " << h4->GetDirectory()->GetName() << endl; cout << "f4 size " << f4.GetSize() << endl; // back to f2 for next iter: f2.cd( f2.GetListOfKeys()->First()->GetName() ); }//1D if( ob2->InheritsFrom("TH2D") ) { H2 = (TH2D*) ob2; cout << "H2 " << H2->GetName() << " " << H2->GetNbinsX() << " bins\n"; cout << "H2 " << H2->GetName() << " " << H2->GetEntries() << " entries\n"; char* Hnm2 = H2->GetName(); // search in f3: cout << "search for " << Hnm2 << " in f3\n"; f3.cd(); cout << "we are in "; gDirectory->pwd(); H3 = (TH2D*) gDirectory->GetList()->FindObject(Hnm2); if( H3 == NULL ) { cout << "H3 is null\n" ; continue; } cout << "found H3 = "; cout << H3->GetName() << " "; cout << H3->ClassName() << " "; cout << H3->GetTitle(); cout << endl; cout << "H3 " << H3->GetName() << " " << H3->GetNbinsX() << " bins\n"; cout << "H3 " << H3->GetName() << " " << H3->GetEntries() << " entries\n"; // add: f4.cd(); cout << "we are in "; gDirectory->pwd(); TH2D* H4 = (TH2D*) H3->Clone(); H4->Add(H2); cout << "H4 " << H4->GetEntries() << " entries\n"; cout << "H4 "; cout << H4->GetName() << " "; cout << H4->ClassName() << " "; cout << H4->GetTitle(); cout << endl; cout << "H4 dir " << H4->GetDirectory()->GetName() << endl; cout << "f4 size " << f4.GetSize() << endl; // back to f2 for next iter: f2.cd( f2.GetListOfKeys()->First()->GetName() ); }//2D } //while cout << "processed " << jj << endl; cout << "f4 " << f4.GetName() << " size " << f4.GetSize() << endl; // cout << "f4 map:\n"; // f4.Map(); cout << "f4 " << f4.GetName() << " write = " << f4.Write() << endl; cout << "f4 " << f4.GetName() << " size = " << f4.GetSize() << endl; }// loop over files 2 cout << endl; cout << "combined " << nmax + 1 << " files\n"; cout << "Final file is " << f4.GetName() << endl; f2.Close(); f3.Close(); f4.Close(); }
loadHist(const char* filename, const char* pfx=0, const char* pat="*", Bool_t doAdd=kFALSE) { TFile inf(filename) ; //inf.ReadAll() ; TList* list = inf.GetListOfKeys() ; TIterator* iter = list->MakeIterator(); TRegexp re(pat,kTRUE) ; cout << "pat = " << pat << endl ; gDirectory->cd("Rint:") ; TObject* obj ; TKey* key ; cout << "doAdd = " << (doAdd?"T":"F") << endl ; cout << "loadHist: reading." ; while(key=(TKey*)iter->Next()) { Int_t ridx = TString(key->GetName()).Index(re) ; if (ridx==-1) { continue ; } obj = inf->Get(key->GetName()) ; TObject* clone ; if (pfx) { // Find existing TH1-derived objects TObject* oldObj = 0 ; if (doAdd){ oldObj = gDirectory->Get(Form("%s_%s",pfx,obj->GetName())) ; if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) { oldObj = 0 ; } } if (oldObj) { clone = oldObj ; ((TH1*)clone)->Add((TH1*)obj) ; } else { clone = obj->Clone(Form("%s_%s",pfx,obj->GetName())) ; } } else { // Find existing TH1-derived objects TObject* oldObj = 0 ; if (doAdd){ oldObj = gDirectory->Get(key->GetName()) ; if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) { oldObj = 0 ; } } if (oldObj) { clone = oldObj ; ((TH1*)clone)->Add((TH1*)obj) ; } else { clone = obj->Clone() ; } } if (!gDirectory->GetList()->FindObject(clone)) { gDirectory->Append(clone) ; } cout << "." ; cout.flush() ; } cout << endl; inf.Close() ; delete iter ; }