///////////////////////////////////////////////////// // // To run Macro type in terminal: root stack_hist.C // //////////////////////////////////////////////////// #include #include #include #include #include #include #include #include #include #include using namespace std; #include void stack_hist(){ // Load Samples files cout<<"Loading Samples Files....."< (file1->Get("gaussian")); TH1* h2 = dynamic_cast(file2->Get("gaussian")); TH1* h3 = dynamic_cast(file3->Get("gaussian")); TH1* h4 = dynamic_cast(file4->Get("gaussian")); // Adjust Appearance of Histos h1->SetFillColor(kRed); h1->SetLineColor(kBlack); h1->SetLineWidth(2); h1->Draw(); h2->SetFillColor(kBlue); h2->SetLineColor(kBlack); h2->SetLineWidth(2); h2->Draw(); h3->SetLineColor(kOrange ); h3->SetLineColor(kBlack); h3->SetLineWidth(4); h3->Draw(); h4->SetFillColor(kViolet+3); h4->SetLineColor(kBlack); h4->SetLineWidth(2); h4->Draw(); // Define output file for saving stacked histos TFile *out_stack = new TFile("output_stack.root","RECREATE"); // Build Canvas TCanvas *cnv = new TCanvas("c1","stacked histo",600,600); // Define Stacked Histo cout<<"Building Stacked Histo....."<Add(h2,"HIST SAME"); stk->Add(h3,"HIST SAME"); stk->Add(h4,"HIST SAME"); cnv->cd(); gPad->SetLogy(1); stk->Draw(); h1->Draw("HIST SAME") ; stk->GetXaxis()->SetTitle("x label"); stk->GetYaxis()->SetTitle("Events"); stk->GetXaxis()->SetLimits(-10,10); gPad->BuildLegend(0.75,0.75,0.95,0.95,""); // automatic build legend from objects in current pad-> ok if histos have different titles // Build a Legend TLegend *lg = new TLegend(0.7,0.7,0.9,0.9); lg->SetHeader("analysis","C"); lg->AddEntry(h2,"bkg 1","f"); lg->AddEntry(h3,"bkg 2","f"); lg->AddEntry(h4,"bkg 3","f"); lg->AddEntry(h1,"signal","l"); lg->Draw(); cnv->Write(); out_stack->Close(); return; }