Pointer to the graph


I have two related questions:
I have plotted graphs in separate canvases and saved in root file.

  1. Now I want to open the root file and plot 4 of these graphs in one new canvas with four pads. How can I put 4 different canvases (which has graphs) into one canvas with four pads?

  2. How can I get pointer to the graph in canvas?


See: root.cern.ch/root/html/TPad.html#TPad
and: $ROOTSYS/tutorials/graphs/zones.C

Thanks couet for quick reply. Yes, for histograms it is fine. For a graph plotted on a canvas, I have tried following way (here graph1 is plotted on canvas c1 and saved in file test.root):

TFile f(“test.root”);
TCanvas c1 = (TCanvas)f.Get(“c1”);
TGraph gh = (TGraph)c1->GetListOfPrimitives()->FindObject(“graph1”);

But it does not work. Similarly, if I try to plot saved canvases on new one like:

TFile f(“test.root”);
TCanvas *cc = new TCanvas(“cc”, “cc”, 800, 650);

TCanvas c1 = (TCanvas)f.Get(“c1”);

TCanvas c2 = (TCanvas)f.Get(“c2”);

In this case, c1 and c2 are not plotted on the new canvas cc.


It works for me :

root [0] TFile f("c1.root");                                                   
root [1] f.ls()
TFile**         c1.root
 TFile*         c1.root
  KEY: TCanvas  c1;1    A Simple Graph Example
root [2] TCanvas *c1 = (TCanvas*)f.Get("c1");                                  
root [3] c1->ls();                                                             
Canvas Name=c1 Title=A Simple Graph Example Option=
 TCanvas fXlowNDC=0 fYlowNDC=0 fWNDC=1 fHNDC=1 Name= c1 Title= A Simple Graph Example Option=
  TFrame  X1= 0.000000 Y1=1.294155 X2=0.990000 Y2=9.604612
  OBJ: TGraph   Graph   a simple graph : 0 at: 0x8c5e828
  OBJ: TPaveText        title   X1= -0.111375 Y1=10.036861 X2=0.187190 Y2=10.591478
root [4] TGraph *gh = (TGraph*)c1->GetListOfPrimitives()->FindObject("Graph");
root [5] TCanvas c                                                            
root [6] gh->Draw("AL")                                                       
root [7] 
1 Like

I was using graph title as the object name.

Hi Olivier,

I just tried this simple example, and it doesn’t work for me as indicated. When I do:

root [0] TFile f("ECA_US.root"); 
root [1] f.ls();                                                                                             
TFile**         ECA_US.root
 TFile*         ECA_US.root
  KEY: TDirectoryFile   Trends;1        Trends
  KEY: TDirectoryFile   DataTTrees;1    DataTTrees
  KEY: TDirectoryFile   Metadata;1      Metadata
root [2] f.cd("Trends");         
root [3] f.ls();                 
TFile**         ECA_US.root
 TFile*         ECA_US.root
  TDirectoryFile*               Trends  Trends
   KEY: TCanvas ATLSCTUSENV_ELMB_Envr_CANbus_2_Envr_ELMB_3F_AI_ELMB_Channel_12_value;1  ATLSCTUSENV_ELMB_Envr_CANbus_2_Envr_ELMB_3F_AI_ELMB_Channel_12_value
  KEY: TDirectoryFile   Trends;1        Trends
  KEY: TDirectoryFile   DataTTrees;1    DataTTrees
  KEY: TDirectoryFile   Metadata;1      Metadata
root [4] TCanvas *c1 = (TCanvas*)f.Get("ATLSCTUSENV_ELMB_Envr_CANbus_2_Envr_ELMB_3F_AI_ELMB_Channel_12_value");
root [5] c1
(class TCanvas*)0x0

I get a null pointer to the TCanvas, as you can see above. What am I doing wrong here? I want to do pretty much the same thing as was asked by the original poster, i.e. to get the graph in these TCanvas(es) to plot them together on the same new canvas. I am using ROOT 5.28/00 to read the file (although I don’t know which root version is used to create the input file).

I attach the root file used above to this post, for testing.

Thanks in advance,

ECA_US.root (707 KB)


you can do either

f.cd("Trends"); TCanvas *c1 = (TCanvas*)gDirectory->Get("ATLSCTUSENV_ELMB_Envr_CANbus_2_Envr_ELMB_3F_AI_ELMB_Channel_12_value"); or simply

TCanvas *c1 = (TCanvas*)f.Get("Trends/ATLSCTUSENV_ELMB_Envr_CANbus_2_Envr_ELMB_3F_AI_ELMB_Channel_12_value");

Thanks René, it works!