#include #include #include #include "TChain.h" #include "TH1.h" #include "TTree.h" #include "TKey.h" #include "Riostream.h" #include "TCanvas.h" #include "TFile.h" #include "TH2.h" #include "TLegend.h" #include "TROOT.h" #include "TStyle.h" #include #include #include #include "TList.h" #include #include "TObject.h" #include "TBranch.h" #include #include "TAxis.h" using namespace std; void Impose( TDirectory *ttarget, TList *ssourcelist, string &np_legend , vector titles_ ); void ModifyHist (TH1F * &h, int cl,vector title); void Overlap() { gROOT->SetStyle ("Plain"); gStyle->SetPalette (1); gStyle->SetTextFont(22) ; gStyle->SetTitleFont(22,"xyz") ; gStyle->SetLabelFont(22,"xyz") ; vector titles; TList *FileList; TFile *Target; titles.clear(); titles.push_back("Data"); titles.push_back("Rare"); titles.push_back("SingleTop"); titles.push_back("WJets"); titles.push_back("TT"); string fout = "mergedDataplots.root"; FileList = new TList (); for (int i=0; iAdd (TFile::Open (file.c_str())); } Target = TFile::Open (fout.c_str (), "RECREATE"); string np_title = titles[0]; Impose (Target, FileList, np_title,titles); delete FileList; delete Target; } void Impose (TDirectory * target, TList * sourcelist, string & np_title_, vector titles) { cout << " " << "========================================================" << endl; cout << " " << "Target path: " << target->GetPath () << endl; TString path ((char *) strstr (target->GetPath (), ":")); path.Remove (0, 2); float Lumi=19125.189; TH1F* allbkg; TFile *first_source = (TFile *) sourcelist->First (); first_source->cd (); 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; int count=0; while ((key = (TKey *) nextkey ())) { count++; if (count>1) break; //keep only the highest cycle number for each key if (oldkey && !strcmp (oldkey->GetName (), key->GetName ())) continue; // read object from first source file and create a canvas first_source->cd (path); TObject *obj = key->ReadObj (); TCanvas *c2 = new TCanvas ("c2",obj->GetName () ,0,22,1350,750); TH1F* hh[100]; TLegend *legend_c1 = new TLegend (0.45, 0.95, 0.9, 0.55); legend_c1->SetFillColor(0); legend_c1->SetFillStyle(0); if (obj->IsA ()->InheritsFrom ("TH1F") ) { // descendant of TH1F -> prepare the histograms to be superimposed // cout << "Modifying histogram " << obj->GetName() << endl; TH1F *h1 = (TH1F *) obj; h1->SetMinimum(0); ModifyHist (h1,1,Lumi,titles[0]); // loop over all source files and modify the correspondant // histogram to the one pointed to by "h1" TFile *nextsource = (TFile *) sourcelist->After (first_source); int cl; h1->SetStats(0); h1->SetLineWidth(2); cl=1; h1->SetLineStyle(1); h1->Draw("ep1"); hh[cl]=h1; THStack *hs = new THStack(h1->GetName(),h1->GetTitle()); allbkg = (TH1F*) hh[1]->Clone(); allbkg->Add(hh[1],-1); 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) { cl++; TH1F *h2; h2 = (TH1F*) key2->ReadObj (); h2->SetMinimum(0); ModifyHist (h2, cl,Lumi,titles[cl-1]); h2->SetStats(0); h2->SetLineWidth(1.5); hh[cl] = h2; hs->Add(h2); if (cl>1) allbkg->Add(h2); h1->GetXaxis()->SetLabelFont(63); //font in pixels h1->GetXaxis()->SetLabelSize(16); //in pixels h1->GetYaxis()->SetLabelFont(63); //font in pixels h1->GetYaxis()->SetLabelSize(16); //in pixels h1->DrawCopy(); h2->Draw("sames"); } nextsource = (TFile *) sourcelist->After (nextsource); } // while ( nextsource ) } // now draw and write the superimposed histograms to the target file // note that this will just store the canvas c1 in the current directory level, // which is not persistent until the complete directory itself is stored // by "target->Write()" below if (obj) { target->cd (); legend_c1->SetTextFont (60); legend_c1->SetTextSize (0.03); for (int k=1;kAddEntry(hh[k], titles[k-1].c_str(), "f"); if (Lumi>0) { char tt[100]; sprintf(tt,"%s",titles[k-1].c_str()); } }//title.size legend_c1->AddEntry(hh[1], "Data", "f"); legend_c1->AddEntry(hh[5], "TTJets", "f"); legend_c1->AddEntry(hh[4], "WJets", "f"); legend_c1->AddEntry(hh[3], "SingleTop", "f"); legend_c1->AddEntry(hh[2], "Other", "f"); TPad *pad1 = new TPad("pad1","pad1",0,0.25,1,1); //ratio pad TPad *pad2 = new TPad("pad2","pad2",0,0,1,0.24); pad1->SetBottomMargin(0.04); pad1->SetRightMargin(0.1); pad2->SetBottomMargin(0.2); //hs->Draw("nostack"); c2->cd(); c2->Clear(); pad1->Draw(); pad1->SetLogy(); pad2->SetGridy(1); pad2->Draw(); //pad2->Range(26.60326,0.3064516,653.8859,1.551613); pad2->SetLeftMargin(0.05); pad2->SetRightMargin(0.05); pad1->cd(); pad1->SetTopMargin(0); pad1->Clear(); hs->SetMaximum(1.1*hh[1]->GetMaximum()); hs->SetMinimum(1); TH1 *hsum = ((TH1 *)(hs->GetStack()->Last())); // the "SUM" Int_t firstbin = hsum->FindFirstBinAbove(0); Int_t lastbin = hsum->FindLastBinAbove(0); hsum->GetXaxis()->SetRange(hsum->FindFirstBinAbove(0),hsum->FindLastBinAbove(0)); hsum->SetFillColor(0); hsum->Draw(); hs->Draw("sames"); char nnn[100]; sprintf(nnn,"Entries/ %d GeV",hsum->GetBinWidth(1)); string name=hsum->GetName(); hs->GetXaxis()->SetTitle(""); hs->GetXaxis()->SetTitleOffset(0.5); hs->GetYaxis()->SetTitleOffset(1); hs->GetYaxis()->SetTitle(nnn); hs->GetYaxis()->SetTitle(nnn); hs->Draw("sames"); gPad->Update(); legend_c1->Draw("sames"); hh[1]->GetXaxis()->SetRange(hh[1]->FindFirstBinAbove(0),hh[1]->FindLastBinAbove(0)); hh[1]->Sumw2(); hh[1]->Draw("sames p E"); gPad->Modified(); gPad->Update(); char namee[100]; sprintf(namee,"%s",key->GetName ()); //c3->cd(); TH1F * ratio = (TH1F*) hh[1]->Clone(); //TH1F * ratio = (TH1F*) allbkg->Clone(); ratio->SetTitle(""); ratio->SetMarkerStyle(20); ratio->SetMaximum( 1.5 ); ratio->SetMinimum(0.5); ratio->GetXaxis()->SetTitle(hh[2]->GetXaxis()->GetTitle()); ratio->GetYaxis()->SetTitle(""); ratio->SetTitle("Data/MC"); ratio->SetTitleSize(1.5); ratio->SetMarkerSize(0.5); ratio->GetYaxis()->SetNdivisions(5); ratio->GetXaxis()->SetNdivisions(515); ratio->SetTitleFont(63); ratio->Sumw2(); pad2->cd(); ratio->SetStats(0); ratio->Divide(hsum); ratio->GetYaxis()->SetTitle(""); ratio->GetXaxis()->SetRange(ratio->FindFirstBinAbove(0),ratio->FindLastBinAbove(0)); ratio->GetXaxis()->SetNdivisions(510); ratio->GetYaxis()->SetTitle(); ratio->Draw("ep"); char ttt[100]; //hs->SetTitle("19/fb MET > 100 HT > 200 b_{tag} >= 1",30,0,1500); c2->cd(); TLine *l=new TLine(pad1->GetUxmin(),1.0,pad1->GetUxmax(),1.0); l->SetLineColor(kRed); pad2->cd(); pad2->BuildLegend(0.2,0.8,0.5,1); l->Draw(); pad2->Update(); c2->Update(); c2->Write (namee); char f[100]; sprintf(f,"Mu_100MET_200HT_1btagM/%s.png",namee); c2->SaveAs (f); //sprintf(namee,"%s_ratio",key->GetName ()); //c3->Write (namee); //string titlee=key->GetName (); } // delete obj; //delete c2; // delete legend_c1; } // while ( ( TKey *key = (TKey*)nextkey() ) ) //c2->Write (); //hs->Write(); //pad1->Write(); // save modifications to target file target->SaveSelf (kTRUE); TH1F::AddDirectory (status); cout << " " << "========================================================" << endl; cout<< " Ended SuperImpose of files.... " <ReadObj (); TCanvas *c1 = new TCanvas ("c1", obj->GetName (), 500, 500); TLegend *legend_c1 = new TLegend (0.65, 0.80, 0.89, 0.70); */ } void ModifyHist (TH1F * &h, int cl_ ,float & lumi,string & title_) { double temp_integral; int nbins=h->GetNbinsX(); int nn=1; h->GetXaxis()->SetNdivisions(512); string titlee=h->GetName(); int col=kOrange; string title1,title2; if(title_.find("Data") == 0) col= kBlack;//TT else if(title_.find("TT") == 0) col= kPink;//TT else if (std::string::npos != title_.find("WJets")) col= kBlue; else if (std::string::npos != title_.find("SingleTop")) col= kCyan; else if (std::string::npos != title_.find("TT")) col= kRed; else col= kOrange;//other bkg if (std::string::npos != title1.find("MET")) h->GetXaxis->SetTitle("GeV/c^{2}"); h->SetLineColor (col); h->SetFillColor (col); }