Inputting then superimposing two graphs (beginner)

The issue is now solved. Thanks!

There are two graphs saved within .root files.
I am able to upload the graphs and individually open them. However, I haven’t been able to figure out how to superimpose them. Here’s what I have so far:

TFile f(“fig/myfig_data_pt.root”);
TGraphErrors gr1 = (TGraphErrors)f.Get(“MassPlot”);
TFile f(“fig/myfig_mc_pt.root”);
TGraphErrors gr2 = (TGraphErrors)f.Get(“MassPlot”);

When I do this, all I get is the graph of gr2. Oddly enough, when I replace TGraphErrors with TGraph or even TH1D, I still get the graph. It’s also strange to me that when I try creating a canvas for the two graphs, the canvas loads separately, leaving me with an empty canvas and the gr2 graph.

Changing colors and marker style hasn’t helped either. Any suggestions?

Could you post your data file and the short script reproducing your problem?


Here’s how I wrote the canvas into a root file

  TCanvas *c2 = new TCanvas("Candidates by Pt Range","Candidates by Pt Range",10,10,700,500);

  TFile *MyFile = new TFile("fig/myfig_data_pt.root","RECREATE");
    Double_t x[5]={0.75,1.25,1.75,2.50,4.00};
    Double_t ex[5]={0,0,0,0,0};


/* Magnitude=sqrt(y[0]*y[0]+y[1]*y[1]+y[2]*y[2]+y[3]*y[3]+y[4]*y[4]);
for (n=0; n<5; n++) {
y[n] /= Magnitude; ey[n] /= Magnitude;}
gr = new TGraphErrors(5,x,y,ex,ey);
gr->GetXaxis()->SetTitle(“Mean Pt Value”);

y[5] and ey[5] were defined within the program. When I run the program, linux just responds, “Processing test.cxx” (test.cxx is the program’s name). Is this what you were asking for?

In the program save your graph with

TFile *MyFile = new TFile("fig/myfig_data_pt.root","RECREATE"); gr = new TGraphErrors(5,x,y,ex,ey); gr->GetXaxis()->SetTitle("Mean Pt Value"); gr->SetLineColor(kRed); gr->SetMarkerColor(kRed); gr->SetMarkerStyle(24); gr->Draw("AP"); c2->Update(); gr->Write("MassPlot");
In the reading program do:

TFile *f1 = TFile::Open("fig/myfig_data_pt.root"); TGraphErrors *gr1 = (TGraphErrors*)f1->Get("MassPlot"); TFile *f2 = TFile::Open("fig/myfig_mc_pt.root"); TGraphErrors *gr2 = (TGraphErrors*)f2->Get("MassPlot"); gr1->Draw("AP"); gr2->Draw("P");


Thanks! The program is working now that I saved the file as a graph rather than the canvas as you recommended.