Adding histograms stored as TCanvas

Hello,

I’m new to ROOT. I have been reading all the posts about adding histograms but I still don’t manage to do it. I try to add two histograms stored in two different .root files as TCanvas. I paste below my code and some other information that might be helpful. Any help will be very much appreciated!

root [1] TFile b1("histoEnergy_1.5keV_tripleMaxFit_NoMagnet_PreliminaryCuts_Bckg_InCalRange.root");

root [2] .ls
TFile**         histoEnergy_1.5keV_tripleMaxFit_NoMagnet_PreliminaryCuts_Bckg_InCalRange.root
 TFile*         histoEnergy_1.5keV_tripleMaxFit_NoMagnet_PreliminaryCuts_Bckg_InCalRange.root
  KEY: TCanvas  combined;1      combined

root [3] TH1D * h1=(TH1D*)b1->Get("combined");

root [5] h1
(class TH1D*)0x27022f0

root [6] h1->SetName("histo1");

root [7] TFile b2("histoEnergy_2.1keV_tripleMaxFit_NoMagnet_PreliminaryCuts_Bckg_InCalRange.root");

root [8] .ls
TFile**         histoEnergy_2.1keV_tripleMaxFit_NoMagnet_PreliminaryCuts_Bckg_InCalRange.root
 TFile*         histoEnergy_2.1keV_tripleMaxFit_NoMagnet_PreliminaryCuts_Bckg_InCalRange.root
  KEY: TCanvas  combined;1      combined

root [9] TH1D * h2=(TH1D*)b2->Get("combined");

root [10] h2
(class TH1D*)0x28ddf30

root [11] h2->SetName("histo2");

h1->Add(h2,1);`´

What am I doing wrong?

Thank you very much in advance.
C.

_ROOT Version: root_v5.34.32


combined in not a TH1D it is a TCanvas. So you should first get it as a TCanvas. Once done you can do combined->ls() . This will give you the list of objects stored in the canvas’ display. One of them should be the histogram you are looking for. You should then do:

 TH1D *h = (TH1D*)combined->GetListOfPrimitives()->FindObject("histogram_name")

Of course you can deduce from this exercise that storing histogram “as TCanvas” is not a good idea…

Hello, I have been reading and trying based on your answer but I still don’t manage to do it… At the end I get

> Error: illegal pointer to class object h1 0x0 353  (tmpfile):1:

The code is:

root [1] TFile b1("histoEnergy_1.5keV_tripleMaxFit_NoMagnet_PreliminaryCuts_Bckg_InCalRange.root");
root [2] TCanvas * c1 = (TCanvas*)b1.Get("combined");
root [3] combined->ls()

Canvas Name=combined Title=combined Option=
 TCanvas fXlowNDC=0 fYlowNDC=0 fWNDC=1 fHNDC=1 Name= combined Title= combined Option=
  OBJ: TList    TList   Doubly linked list : 0
   TPad fXlowNDC=0.01 fYlowNDC=0.01 fWNDC=0.98 fHNDC=0.98 Name= combined_1 Title= combined_1 Option=
    OBJ: TList  TList   Doubly linked list : 0
     TFrame  X1= 0.000000 Y1=0.000000 X2=43122.000000 Y2=2.100000
     OBJ: TH1F  Spectrum        Hits analysis energy spectrum : 1 at: 0x29ae450
     OBJ: TPaveText     title   X1= 2695.124719 Y1=2.194271 X2=40426.875281 Y2=2.349375

root [4] TH1F *h1 = (TH1F*)combined->GetListOfPrimitives()->FindObject("Spectrum");
root [5] h1
(class TH1F*)0x0
root [6] TFile b2("histoEnergy_2.1keV_tripleMaxFit_NoMagnet_PreliminaryCuts_Bckg_InCalRange.root");
root [7] TCanvas * c2 = (TCanvas*)b2.Get("combined");
root [8] c2->GetListOfPrimitives()->ls();

OBJ: TList      TList   Doubly linked list : 0
 TPad fXlowNDC=0.01 fYlowNDC=0.01 fWNDC=0.98 fHNDC=0.98 Name= combined_1 Title= combined_1 Option=
  OBJ: TList    TList   Doubly linked list : 0
   TFrame  X1= 0.000000 Y1=0.000000 X2=43122.000000 Y2=1.050000
   OBJ: TH1F    Spectrum        Hits analysis energy spectrum : 1 at: 0x29ad8e0
   OBJ: TPaveText       title   X1= 2695.124719 Y1=1.097135 X2=40426.875281 Y2=1.174688

root [9] TH1F *h2 = (TH1F*)combined->GetListOfPrimitives()->FindObject("Spectrum");
root [10] h1->Add(h2,1);
Error: illegal pointer to class object h1 0x0 353  (tmpfile):1:
*** Interpreter error recovered ***´

Do you see what I’m doing wrong?

Thanks.

Try:

TH1F *h1 = (TH1F*)c1->FindObject("Spectrum");
// ...
TH1F *h2 = (TH1F*)c2->FindObject("Spectrum");

Thanks. I’ve tried and this way I don’t get any error but when I type h1->Draw(), it does nothing. I will try to solve it myself and if I have problems I’ll come back to you in a few days.

C.

Try:

new TCanvas();
h1->Draw();

Great! It works! Thank you very much :grinning:
I paste the final code here in case it is useful for someone in the future.

TFile b1("histoEnergy_1.5keV_tripleMaxFit_NoMagnet_PreliminaryCuts_Bckg_InCalRange.root")
TCanvas * c1 = (TCanvas*)b1->Get("combined")
TH1F *h1 = (TH1F*)c1->FindObject("Spectrum")
TFile b2("histoEnergy_2.1keV_tripleMaxFit_NoMagnet_PreliminaryCuts_Bckg_InCalRange.root")
TCanvas * c2 = (TCanvas*)b2->Get("combined")
TH1F *h2 = (TH1F*)c2->FindObject("Spectrum")
h1->Add(h2,1)
new TCanvas()
h1->Draw()

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.