Plot two root file in one frame

I have two root file (cT1.root and cT2.root) and I want to plot both in one frame.
So I have made this script to plot the two .root file together:

void MultiplotInOneFrame()
{

TFile* fr1 = TFile::Open("cT1.root");
TCanvas* h1;
	fr1->GetObject("cT1;1", h1);
	std::cout<<"Check cT1"<<endl;



TFile* fr2 = TFile::Open("cT2.root");
TCanvas* h2;
	fr2->GetObject("cT2;1", h2);
	std::cout<<"Check cT2"<<endl;
h1->Draw();	
h2->Draw("same");
}

but the output is separate plot.
can any one help me?

You should open only one canvas:

void MultiplotInOneFrame()
{

   auto C = new TCanvas();

   TFile* fr1 = TFile::Open("cT1.root");
   fr1->GetObject("cT1;1", h1);
   std::cout<<"Check cT1"<<endl;

   TFile* fr2 = TFile::Open("cT2.root");
   fr2->GetObject("cT2;1", h2);
   std::cout<<"Check cT2"<<endl;

   h1->Draw();	
   h2->Draw("same");
}

Hi, thanks for replying my post.
I run your script and give this output:

root [0]
Processing MergePlot.C…
In file included from input_line_8:1:
/home/msb/garfield/Examples/GenerateGasTable_Ar90_ch4_10/build/MergePlot.C:29:28: error: use of undeclared identifier ‘h1’
fr1->GetObject(“cT1;1”, h1);
^
/home/msb/garfield/Examples/GenerateGasTable_Ar90_ch4_10/build/MergePlot.C:33:28: error: use of undeclared identifier ‘h2’
fr2->GetObject(“cT2;1”, h2);
^
/home/msb/garfield/Examples/GenerateGasTable_Ar90_ch4_10/build/MergePlot.C:36:4: error: use of undeclared identifier ‘h1’
h1->Draw();
^
/home/msb/garfield/Examples/GenerateGasTable_Ar90_ch4_10/build/MergePlot.C:37:4: error: use of undeclared identifier ‘h2’
h2->Draw(“same”);

I changed the code to this
void MergePlot()
{

auto C = new TCanvas();

TFile* fr1 = TFile::Open(“cT1.root”);
TCanvas* h1;
fr1->GetObject(“cT1;1”, h1);
std::cout<<“Check cT1”<<endl;

TFile* fr2 = TFile::Open(“cT2.root”);
TCanvas* h2;
fr2->GetObject(“cT2;1”, h2);
std::cout<<“Check cT2”<<endl;

h1->Draw();
h2->Draw(“same”);
}

then run again:
output is tow separate plot and a empty canvas.

Ah, I see … cT1 et cT2 are canvases ? … right? You cannot superimpose canvases that way option SAME is for histograms (see help)

How can I do it?

That’s not trivial because canvases are complete pictures. If you want to superimpose the histograms which are inside the canvases you can get the histograms from the canvases and superimpose them using SAME. You better store histograms and note canvases.

These are the output of garfield++ example and I don’t know how to change it.
can you help me to do it?

Can you post a ROOT file containing the 2 canvases?

ok.
cT1.root (18.6 KB)
cT2.root (19.4 KB)

void MergePlot() {
   TFile* fr2 = TFile::Open("cT2.root");
   TCanvas* c2;
   fr2->GetObject("cT2;1", c2);
   TGraph *g1 = (TGraph*)c2->GetListOfPrimitives()->FindObject("Graph");

   TFile* fr1 = TFile::Open("cT1.root");
   TCanvas* c1;
   fr1->GetObject("cT1;1", c1);
   TGraph *g2 = (TGraph*)c1->GetListOfPrimitives()->FindObject("Graph0");

   auto C = new TCanvas();
   C->SetLogx();

   auto m = new TMultiGraph();
   m->Add(g1);
   m->Add(g2);
   m->Draw("AL");
}

thank you.

May be this version is a bit better (color changed, axis redrawing, log scale along Y)

void MergePlot() {
   TFile* fr2 = TFile::Open("cT2.root");
   TCanvas* c2;
   fr2->GetObject("cT2;1", c2);
   TGraph *g1 = (TGraph*)c2->GetListOfPrimitives()->FindObject("Graph");
   g1->SetLineColor(kGreen);

   TFile* fr1 = TFile::Open("cT1.root");
   TCanvas* c1;
   fr1->GetObject("cT1;1", c1);
   TGraph *g2 = (TGraph*)c1->GetListOfPrimitives()->FindObject("Graph0");

   auto C = new TCanvas();
   C->SetLogx();
   C->SetLogy();

   auto m = new TMultiGraph();
   m->Add(g1);
   m->Add(g2);
   m->Draw("AL");

   C->RedrawAxis();
}

actually I need it.

thank you.

1 Like

Hi Olivier
Thanks for answered my question.
I ran your script and gave a output. But when I run it today, give an error.
I send the error as a txt file.
If it possible help me to solve it.
error.txt (2.4 KB)

That’s weird. Have you changed the macro ? if yes can you send me the new version ?

No, I didn’t change it.
But, After I sent the question to you, I ran the code again and got the answer.
‌Very strange!!!

I have another question.
Suppose I have a root file.
How can I find out what information this file contains? For example, if it is a histogram, what is its x and y axis?

You can you the command line tool:

rootls -l yourfile.root

thank you.