To put two histgrams on an canvas with overlap


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


the code is as below:
oid tau_vis_m()
{
auto C = new TCanvas(“C”,“distribution of _vis_m”,600,500);

TFile* f1 = new TFile("…/output_sig1_20180916_04-33-25/hist-sig1.root");
TTree* t1 = (TTree*) f1->Get(“Nominal/HadHad_hadhad_FINAL”);

TFile* f2 = new TFile("…/output_sig1_20180921_09-03-53/hist-sig1.root");
TTree* t2 = (TTree*) f2->Get(“Nominal/HadHad_hadhad_FINAL”);

TH1F* h1 = new TH1F(“vis_m”,“vis_m”,100,0,1000);
TH1F* h2 = new TH1F(“vis_m”,“vis_m”,100,0,1000);

int n1 = t1-> GetEntries();
int n2 = t2-> GetEntries();

std::cout << n1 <<" "<< n2 << std::endl;
t1-> Draw(“tau_vis_m >> h1”); //1
t2-> Draw(“tau_vis_m >> h2”); //2

h1-> Draw(); //3
h2-> Draw(“same”); //4
// to store this histgram
TFile f(“f1.root”,“RECREATE”);
h1->Write();
}

//1 and //2 can work,but it seems that h1’s memory released subsequently.
So ,//3 and //4 do not work,drawing nothing.

I have no idea what mistakes i made .
Could you help me?

Dear 123,

Are //3 and //4 drawing really nothing or is the drawing going away when exiting the macro? Is the h1 saved correctly in the file ‘f1.root’ (what do you get when you re-open the file and draw the histogram)?
Does it change anything if you use DrawClone instead ? e.g.

h1->DrawClone(); //3
h2->DrawClone(“same”); //4

G Ganis

Dear Ganis,
Thank you very much for your information!

When the code end,there will be a coordinate system,with Entries = 0 ,mean=0,RMS=0.
And f1.root contains the canvas above.
Nothing is changing with “DrawClone”.

Thanks for the trouble!

Xin(123)

I suggest you try two more ways which should both give you the expected behavior:

  1. Replace the Draw method of TTree by the Project method:
t1 -> Project("h1", “tau_vis_m”); //1
t2 -> Project("h2", “tau_vis_m”); //2

h1-> Draw(); //3
h2-> Draw(“same”); //4
  1. Add Draw options to the TTree Draw method:
t1-> Draw(“tau_vis_m >> h1”); //1
t2-> Draw(“tau_vis_m >> h2”, "1", "same"); //2

Dear Triple_S,
Thanks a lot for your information.It does work.

xin

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.