How do you superimpose graphs from different root files in C++/root

  • Context: C/C++ 
  • Thread starter Thread starter lavster
  • Start date Start date
  • Tags Tags
    files Graphs Root
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 11K views
lavster
Messages
213
Reaction score
0
Hi,

ive created several graphs in different emacs files. I now want to superimpose them using code in a different emacs file. This is what i have done:

Code:
  //get the input files

  TFile * inputfile = new TFile("p60.root","READ");
  TFile * inputfile2 = new TFile("p20.root","READ");
  TFile * inputfile3 = new TFile("p80.root","READ");
  TFile * inputfile4 = new TFile("p40.root","READ");
  
  //define canvas and plotting like before
  
  /////////////////////////////////////////////////////////////////
  //superposition of different energies of proton beam (one proton)
  /////////////////////////////////////////////////////////////////
  
   gStyle->SetPalette(1);
  
  TCanvas* comp = new TCanvas("comp","",1100,1100);
  comp->Clear();
  comp->Divide(1,1);
  //TGraph* g40 = (TGraph*) inputfile4 -> Get("g4");//bit in the bracket is the name of the histogram
   TGraph* g20 = (TGraph*) inputfile2 -> Get("g4");
  comp->cd(1);
  g20->SetLineColor(2);//2=red
  g20->GetXaxis()->SetTitle("x (mm)");//title
  g20->GetYaxis()->SetTitle("energy deposited (Mev)");
  g20->Draw();
  g20->GetXaxis()->SetTitleOffset(0.9);//changes vertical postion of the title
  g20->GetXaxis()->SetTitleSize(0.045);//changes size of xaxis title (default)
  g20->GetXaxis()->SetLabelSize(0.04); //changes the size of numbers on xaxis
  g40->Draw("sames");// the s at end means that can have multiple stats box
  g60->Draw("sames");
  g20->SetLineColor(3);//2=red
  g80->Draw("sames");
  g20->SetLineColor(4);//2=red
  //add legends
  
   comp->Modified();
   comp->Update();
   TLegend* legends = new TLegend(0.130,0.828,0.436,0.980,"","brNDC"); // the numbers determine the position of the box
   legends->SetFillColor(0);
   legends->SetHeader("proton dose distributions");
   legends->AddEntry(g20, "20MeV proton beam","l");//(name of hist,what you want it called in legend, l=line, p=polymarker, f=boxy thing )
   legends->AddEntry(g40, "40MeV proton beam","l");
   //legends->AddEntry(g60, "60MeV proton beam","l");
   // legends->AddEntry(g80, "80MeV proton beam","l");
  // legends->Draw();
  
  comp->cd();

however, this doesn't seem to work. can anyone tell me where i have gone wrong? thanks
 
Physics news on Phys.org
I know it is an old post, but I'll answer it to avoid an unanswered post.

The problem is in the options to draw the graphs. The "same" or "sames" option works only for histograms.
For graphs, the first should be, at least, with the "A" option to include the axis. An extra option should appear for the marks or lines. Now, the next graphs should be drawn WITHOUT the A option, but with the mark option.

I think it should work