Two datasets in one canvas

Hi! I am a newbie with data analysis and ROOT in general. I am trying to plot two dataset in one canvas. The first dataset is generated from a code (I just save it as file.C and the second one is a dataset from a filet.txt)
I do not know if my logic is right and if what I am trying to do is correct, so far I wrote a code to read the file.txt in root and then add that to the file.C. So now, when I run root file.C, it shows two canvas with the datasets, but i need them both in one canvas.
Any help will be appreciated!

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

It’s better if you include your code to help people understand what you are doing.
What are you saving as “file.C”? is it the “first dataset” (that you generate from “a code”), or the code (“a code”) to generate that data? It sounds like you are saving data to a .C file(*) (file.C), but maybe this is not what you have, since later you mention that you run file.C and it creates 2 canvases… so what is file.C reading?
(*) Ultimately the file extension doesn’t necessarily matter here, but conventionally a .C file is for code, not for data (e.g. why not have 2 .txt files for your data, which then you can read with a .C file?).

@dastudillo There is no option for saving as .txt when I run it in root.

void cGSpec()
//=========Macro generated from canvas: cGSpec/cGSpec
//=========  (Tue Jul  7 13:44:33 2020) by ROOT version 6.20/04
   TCanvas *cGSpec = new TCanvas("cGSpec", "cGSpec",164,151,800,600);
   TH1D *hExI0GSpec_0 = new TH1D("hExI0GSpec_0","Gamma Spectrum: 0.0 MeV, Real0",500,0,7.8174); .... } 

That’s the beginning of the first file.
And what I am doing is adding on the bottom

TGraph *gr = new TGraph("capgam_z025_55Mn.txt", "%lg %lg, ",");
TCanvas *c1 = new TCanvas();

So it shoes two canvas with their respective information but I want to plot them in just one canvas. Is this possible?
Thanks for your help anyways

Try not creating the second canvas (c1), and drawing gr with “same”:


But if the ranges and scales of both hExI0GSpec_0 and gr are not very similar, you might not see gr and will have to force the largest ranges in the plot that accommodate both.
If inside file.C you are somehow generating the data that you put into hExI0GSpec_0, you can:

  • instead of putting it into that histogram, put it into another graph; then you can put the 2 graphs in a TMultiGraph.
  • save the data to a txt file the c++ way (google can help).