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

In summary: Draw("sames");// the s at end means that can have multiple stats boxg60->Draw("sames");g80->Draw("sames");g40->Draw("sames");// the s at end means that can have multiple stats boxg60->Draw("sames");g80->Draw("sames");g20->Draw("sames");g40->Draw("same","s","l"); //the s at end means that can have multiple stats boxg60->Draw("same","s","l");g80->Draw("same","s","l");
  • #1
lavster
217
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
  • #2
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
 

1. How do you load multiple root files in C++/root?

To load multiple root files in C++/root, you can use the TFile::Open function. This function takes in the path to the root file as a parameter and returns a TFile object. You can then use this object to access the data in the root file.

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

To superimpose graphs from different root files in C++/root, you can use the TMultiGraph class. This class allows you to add multiple graphs to a single plot. You can use the Add() function to add graphs from different root files to the TMultiGraph object, and then use the Draw() function to display the superimposed graphs.

3. Can you explain the process of superimposing graphs from different root files in more detail?

To superimpose graphs from different root files, you first need to load the root files using the TFile::Open function. Then, you can access the graphs from each root file and add them to a TMultiGraph object using the Add() function. Finally, you can use the Draw() function to display the superimposed graphs.

4. Is it possible to superimpose more than two graphs from different root files?

Yes, it is possible to superimpose more than two graphs from different root files using the TMultiGraph class. You can add as many graphs as you want to the TMultiGraph object using the Add() function, and they will all be displayed on the same plot.

5. Are there any limitations to superimposing graphs from different root files?

One limitation to keep in mind when superimposing graphs from different root files is that the graphs should have the same number of data points and the same x and y ranges. Otherwise, the superimposed graph may not accurately represent the data from the individual graphs.

Back
Top