Drawing two histograms after normalising from different rootfiles

Hi,
I have written the following macro to plot two histograms (from two different root files) on the same plot after normalizing them :

void Exp_Theo_Mass_Events()
{
Double_t s1, s2, s3;

TCanvas *c1 = new TCanvas("c1","Comparison of Experimental and Theoritical TKE Distribution",800,600);
gStyle->SetOptStat(0);

TFile *file3=TFile::Open("RAW173SM_treeFF01.root");
TTree *tree3=(TTree*)file3->Get("Analysis_Fis");

c1->cd();
   TH1D*h1 = new TH1D("h1","Normalised to the total number of events",100.,40.,140.);
   h1->SetLineColor(8);
   h1->SetLineStyle(1);
   h1->SetLineWidth(3);
   tree3->Draw("M_Tot>>h1");
   s1 = 200./10478.;   // Normalised to total number of events
   h1->Scale(s1);
   h1->Draw("");

TFile *file=TFile::Open("QnmFF_ROOT.root");
TTree *tree=(TTree*)file->Get("DITY");


   TH1D*h3 = new TH1D("h3","Au_158.62_MeV,40.7_MeV",100.,40.,140.);
   h3->SetLineColor(2);
   h3->SetLineStyle(1);
   tree->Draw("M_Tot_Res_amu>>h3");
   s3 = 200./1236.;
   h3->Scale(s3);
   h3->Draw("SAME");


   auto legend = new TLegend(0.1,0.7,0.48,0.9);
   legend->SetHeader("","C"); 
   legend->AddEntry(h1,"Experimental data","l");
   legend->AddEntry(h3,"Theoritical data","l");
   legend->SetBorderSize(0);
   legend->Draw();
 

}

Unfortunately I am not able to plot two histograms together.


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


This is the output I get when I run my macro:

Regarding drawing you do:

   tree3->Draw("M_Tot>>h1"); // draw h1
   h1->Draw(""); // redraw h1 again
   tree->Draw("M_Tot_Res_amu>>h3"); // overwrite the previous plot with h3
   h3->Draw("SAME"); /// redraw h3 on h3 ....
   legend->Draw(); // draw the legend
}

so you see h and the legend…

You should do:

   tree3->Draw("M_Tot>>h1", "", "goff"); // do not draw h1
   h1->Draw(""); // draw h1 
   tree->Draw("M_Tot_Res_amu>>h3", "", "goff"); // do not draw h3
   h3->Draw("SAME"); /// draw h3 on the same plot as h1
   legend->Draw(); // draw the legend

Another option, which works with or without goff, is with a THStack

THStack *ts = new THStack();
ts->Add(h1);
ts->Add(h3);
ts->Draw("nostack");

(remove h1->Draw, h3->Draw in this case).

True, but it is better to put goff anyway because h1 and h3 will be drawn for nothing.

1 Like

Thank you it works…