void MakeNiceStatBox(TH1* histo){ TObject *statObj; TPaveStats *stats; statObj = histo->GetListOfFunctions()->FindObject("stats"); stats= static_cast<TPaveStats*>(statObj); stats->SetFillColor(10); stats->SetLineWidth(1); stats->SetShadowColor(0); stats->SetTextFont(42); stats->SetTextSize(0.05); //stats->SetLineColor(LineColors); //stats->SetTextColor(LineColors); stats->SetX1NDC(0.615); stats->SetY1NDC(0.747); stats->SetX2NDC(0.979); stats->SetY2NDC(0.986); }
void DrawAllHistos(TString filename,Bool_t isMC,TString format){ // setTDRStyle("logredblue"); std::vector<TH1F*> h1vec; std::vector<TH2F*> h2vec; h1vec.clear(); h2vec.clear(); TFile *file = TFile::Open(filename); // to get the names of the conditions TObjArray *conditions_from_name = filename.Tokenize("_"); TString sa = conditions_from_name->At(1)->GetName(); TString sb = conditions_from_name->At(3)->GetName(); sb.ReplaceAll(".root",""); file->cd(); TDirectory *stardir = gDirectory; TObject *thesourcedir; TIter nextdir(stardir->GetListOfKeys()); while((thesourcedir=nextdir())){ TString dirName = thesourcedir->GetName(); stardir->cd(dirName); TDirectory *current_sourcedir = gDirectory; TH1::AddDirectory(kFALSE); std::cout << "*************************" <<std::endl; std::cout << "Reading Directory: " << dirName <<std::endl; TObject *obj; TIter next(current_sourcedir->GetListOfKeys()); while ((obj=next())) { TString objName =obj->GetName(); if(objName.Contains("pfx")) continue; if (objName.Contains("h2") && !objName.Contains("pfx")) { //std::cout << "Reading: " << obj->GetName() <<std::endl; TH2F* h2 = (TH2F*)file->Get(dirName+"/"+objName); h2->SetName(objName+dirName); h2vec.push_back(h2); TCanvas *c = new TCanvas(h2->GetName(),h2->GetName(),800,600); c->cd(); gPad->SetTopMargin(0.08); gPad->SetRightMargin(0.15); h2->SetStats(kFALSE); h2->Draw("colz"); c->Draw(); c->cd(); TProfile *hpfx_tmp = (TProfile*) h2->ProfileX("_pfx",1,-1,"o"); hpfx_tmp->SetStats(kFALSE); //hpfx_tmp->SetMarkerColor(kBlack); hpfx_tmp->SetMarkerColor(kRed); hpfx_tmp->SetMarkerSize(1.2); hpfx_tmp->SetMarkerStyle(20); hpfx_tmp->Draw("psame"); c->Draw(); cmsPrel(60.,sa,sb, isMC); TString canvName = h2->GetName(); c->cd()->SetLogz(); c->SaveAs(canvName+"."+format); } else { TH1F* h1 = (TH1F*)file->Get(dirName+"/"+objName); h1->SetName(objName+dirName); h1vec.push_back(h1); TCanvas *c = new TCanvas(h1->GetName(),h1->GetName(),600,600); c->cd()->SetLogy(); h1->SetMarkerColor(kBlack); h1->SetMarkerStyle(20); h1->SetLineWidth(1.5); h1->SetFillColor(393); //h1->SetFillStyle(3005); h1->Draw("hist"); h1->Draw("e1same"); c->Draw(); TObject *statObj; TPaveStats *stats; statObj = h1->GetListOfFunctions()->FindObject("stats"); stats= static_cast<TPaveStats*>(statObj); stats->SetFillColor(10); stats->SetLineWidth(1); stats->SetShadowColor(0); stats->SetTextFont(42); stats->SetTextSize(0.025); //stats->SetLineColor(LineColors); //stats->SetTextColor(LineColors); stats->SetX1NDC(0.75); stats->SetY1NDC(0.72); stats->SetX2NDC(0.97); stats->SetY2NDC(0.92); stats->Draw("same"); cmsPrel(60.,sa,sb,isMC); TString canvName = h1->GetName(); c->SaveAs(canvName+"."+format); } } } }
// ----------------------------------------------------------------------------- // Create plots void plot( std::string& path, std::string& type, std::string& sample, std::string& dir, std::string& histo, double lumi, bool scale ) { std::string canvas_name = histo + "_" + dir + "_" + type + "_" + sample; // Create canvas TCanvas* canvas = new TCanvas(canvas_name.c_str(),""); canvas->SetFillColor(0); canvas->SetFrameBorderMode(0); canvas->SetFrameFillColor(0); canvas->SetTopMargin(0.10); canvas->SetBottomMargin(0.12); canvas->SetLeftMargin(0.12); canvas->SetRightMargin(0.15); // Retrieve histogram std::string file_name = path + type + "_" + sample + ".root"; TFile* f = new TFile(file_name.c_str(),"READ"); TDirectory* d = (TDirectory*)f->Get(dir.c_str()); TH2D* his = (TH1*)d->Get(histo.c_str()); if ( !his ) return; //his->Rebin2D(2,2); if ( true ) { gPad->SetLogz(); } if ( scale ) his->Scale( lumi / 100. ); his->SetMaximum(1.e4); his->SetMinimum(1.e-5); //his->SetMinimum( his->GetMinimum(1.e-12) ); // his->SetMaximum( 20000. ); // his->SetMinimum( 2.e-4 ); // his->SetMaximum( 20000. ); // his->SetMinimum( 20. ); double xmin = his->GetXaxis()->GetXmin(); double xmax = his->GetXaxis()->GetXmax(); double ymin = his->GetYaxis()->GetXmin(); double ymax = his->GetYaxis()->GetXmax(); // Reset title std::string title = ";" + std::string(his->GetXaxis()->GetTitle()) + ";" + std::string(his->GetYaxis()->GetTitle()); his->SetTitle(title.c_str()); his->GetXaxis()->SetTitle("x_{2}"); his->GetXaxis()->SetTitleOffset(1.2); his->GetYaxis()->SetTitle("x_{1}"); his->GetYaxis()->SetTitleOffset(1.4); his->Draw("COLZ"); gPad->Update(); // Lumi if (1) { std::stringstream ss; ss << "#int L dt = " << lumi << " pb^{-1}"; double xpos = 0.05 * (xmax-xmin)+xmin; double ypos = 0.25 * (ymax-ymin)+ymin; TLatex* text1 = new TLatex(xpos,ypos,ss.str().c_str()); text1->SetTextAlign(12); text1->SetTextSize(0.035); text1->Draw(); } // Jet type if (1) { double xpos = 0.05 * (xmax-xmin)+xmin; double ypos = 0.15 * (ymax-ymin)+ymin; TText* text2 = new TText(xpos,ypos,type.c_str()); text2->SetTextAlign(12); text2->SetTextSize(0.035); text2->Draw(); } // Sample if (1) { double xpos = 0.05 * (xmax-xmin)+xmin; double ypos = 0.10 * (ymax-ymin)+ymin; TText* text3 = new TText(xpos,ypos,sample.c_str()); text3->SetTextAlign(12); text3->SetTextSize(0.035); text3->Draw(); } // Stats gStyle->SetOptStat("i"); his->SetStats(1); TPaveStats* stats = (TPaveStats*)his->GetListOfFunctions()->FindObject("stats"); std::string stats_pos = "br"; if ( stats ) { stats->SetFillColor(0); stats->SetLineColor(0); stats->SetShadowColor(0); if ( stats_pos == "tr" ) { stats->SetX1NDC(0.60); stats->SetY1NDC(0.68); stats->SetX2NDC(0.83); stats->SetY2NDC(0.88); } else if ( stats_pos == "br" ) { stats->SetX1NDC(0.60); stats->SetY1NDC(0.18); stats->SetX2NDC(0.83); stats->SetY2NDC(0.28); } else { stats->SetX1NDC(0.60); stats->SetY1NDC(0.68); stats->SetX2NDC(0.83); stats->SetY2NDC(0.88); } } // Scale gStyle->SetPalette(1); TPaletteAxis* palette = (TPaletteAxis*)his->GetListOfFunctions()->FindObject("palette"); if ( palette ) { palette->SetY1NDC(0.2); palette->SetY2NDC(0.8); } canvas->Modified(); canvas->cd(); canvas->SetSelected(canvas); canvas->SaveAs(std::string(canvas_name+".png").c_str()); //canvas->Write(); }