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
Click For Summary
SUMMARY

The discussion focuses on superimposing graphs from different ROOT files in C++ using the ROOT framework. The user attempts to plot multiple graphs from files named "p20.root", "p40.root", "p60.root", and "p80.root" on a single canvas. The main issue identified is the incorrect usage of the "sames" option for drawing TGraphs, which should instead utilize the "A" option for the first graph and omit it for subsequent graphs. This adjustment is crucial for successful graph superimposition.

PREREQUISITES
  • Familiarity with C++ programming language
  • Understanding of ROOT framework for data analysis
  • Knowledge of TGraph and TCanvas classes in ROOT
  • Basic experience with Emacs for coding
NEXT STEPS
  • Review ROOT documentation on TGraph and TCanvas usage
  • Learn about drawing options in ROOT, specifically "A" and "sames"
  • Explore examples of superimposing graphs in ROOT
  • Investigate error handling in ROOT for debugging graph plotting
USEFUL FOR

Researchers, physicists, and developers working with data visualization in ROOT, particularly those needing to superimpose multiple graphs for comparative analysis.

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
 
Technology 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