Problems with draw "same" option (canvas stays empty)

Hey guys,

I’m a beginner in root and therefore I have some problems with it.

My goal is it to draw two histogram (from different root files) in one canvas.

My Problem is that the code below does not draw anything into the canvas (the created pdf is empty…).
If I execute the different steps individually, I recognized that a Canvas will be created (as wanted) but if I then try to draw something, it will be drawn, but not into the created canvas (a additional window pops up with the histogram in it).

The most confusing thing is, that I tried it with a another root file and it worked perfectly.
Could be there a difference? The one with which the code worked was really a histogram. And the problematic one is a graphic were I have several simulated points.

I have no idea where the difference is… Except that the names of the two histograms are the same (both myCanvas).

Thanks in advance for your help!!


//#include “TApplication.h”
#include “TCanvas.h”
#include “TFile.h”
#include “TH1F.h”

void macro(){

TFile* f1 = new TFile(“E_vs_vDrift_Ar_100.root”);
TFile* f2 = new TFile(“E_vs_vDrift_CH4_100.root”);
TH1F *h1 = f1->Get(“myCanvas”);
TH1F *h2 = f2->Get(“myCanvas”);

TCanvas* c1 = new TCanvas(“c1”, “Test”, 800, 600);

c1->Print(“CombinedTest.pdf”, “pdf”);



Attach both your ROOT files for inspection here.

Here are my two root files for inspection.
E_vs_vDrift_Ar_100.root (24.1 KB)
E_vs_vDrift_CH4_100.root (24.1 KB)

[code]#include “TFile.h”
#include “TCanvas.h”
#include “TGraph.h”

void macro() {
TFile *f1 = new TFile(“E_vs_vDrift_Ar_100.root”);
TCanvas *c1; f1->GetObject(“myCanvas”, c1);
TGraph *g1 = ((TGraph *)(c1->FindObject(“Graph”)));
g1->SetMarkerSize(0.5); g1->SetMarkerColor(kGreen);

TFile *f2 = new TFile(“E_vs_vDrift_CH4_100.root”);
TCanvas *c2; f2->GetObject(“myCanvas”, c2);
TGraph *g2 = ((TGraph *)(c2->FindObject(“Graph”)));
g2->SetMarkerSize(0.5); g2->SetMarkerColor(kRed);

TCanvas *c = new TCanvas(“c”, “Test”, 800, 600);
c->DrawFrame(0.99 * g1->GetX()[0],
0.99 * g1->GetY()[0],
1.01 * g1->GetX()[(g1->GetN() - 1)],
1.01 * g1->GetY()[(g1->GetN() - 1)],
“Combined Test;E / V / cm;v_{Drift}^{} / m / #mus”);
g1->Draw(“P SAME”);
g2->Draw(“P SAME”);
c->Print(“CombinedTest.pdf”, “pdf”);

delete f1;
delete f2;

Than you very much for your help!!!
It works now! :slight_smile:
Do you have any idea how to set xtitle and ytitle?

Sorry, I forgot to add the code where the xtitles are created…

Thank you again!

Try the modified macro from my previous post above.