How to put two or more histograms in one canvas?

Hello everyone i’m a total newbie to ROOT and i’m trying to learn it all by myself, and i have faced a problem of overplaying 3 histograms. I’ve seen many examples and gotten macros, but compiling macros show no result with no error message. I even downloaded other people’s root files and run them for seeing my error, but those successful macros with their root files don’t show any result. I have no clue what to do.
Here is my macro.
test.cpp (535 Bytes)
ROOT Version: root_v5.34.36
Platform: Not Provided
Compiler: Emacs

To overlay, you need to pass the draw option SAME. See e.g. here:

Yep, i’ve written “same”, but no result.
Here is the root files. Can anyone run this macro with these files for testing whether it’s working or not?
acceleratedregion.root (15.5 KB)
fragmentation.root (15.2 KB)
general.root (15.2 KB)

test.cpp (531 Bytes)
But in this macro i used “stack”.

   h1->SetDirectory(gROOT); // gROOT ... or ... 0
   h2->SetDirectory(gROOT); // gROOT ... or ... 0
   h3->SetDirectory(gROOT); // gROOT ... or ... 0

I also tried that too in my other macro, but no success. I see no error, just nothing appears.
carbonallregions.cpp (655 Bytes)

{
  TFile *f; TCanvas *c;
  
  f = TFile::Open("general.root");
  // f->ls();
  f->GetObject("C0000", c);
  // c->ls();
  TH1F *h1 = (TH1F*)(c->GetPrimitive(" proton ")->Clone("general"));
  h1->SetDirectory(gROOT);
  delete c; delete f; // no longer needed
  
  f = TFile::Open("fragmentation.root");
  f->GetObject("C0000", c);
  TH1F *h2 = (TH1F*)(c->GetPrimitive(" proton ")->Clone("fragmentation"));
  h2->SetDirectory(gROOT);
  delete c; delete f; // no longer needed
  
  f = TFile::Open("acceleratedregion.root");
  f->GetObject("C0000", c);
  TH1F *h3 = (TH1F*)(c->GetPrimitive(" proton ")->Clone("acceleratedregion"));
  h3->SetDirectory(gROOT);
  delete c; delete f; // no longer needed
  
  h1->SetStats(kFALSE);
  h2->SetStats(kFALSE);
  h3->SetStats(kFALSE);
  
  THStack *st = new THStack();
  st->Add(h1);
  st->Add(h2);
  st->Add(h3);
  
  st->Draw("NOSTACK");
  
  gPad->BuildLegend();
}
2 Likes

I’ve no word to tell, thank you very much.

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