Problem with accessing histogram from canvas

Hello all,

I have a root file which has canvas saved inside it.
I want to :

  • access histograms from those canvas.
  • set their X axis and Y axis title.

What I am doing to achieve this is :

[code]TFile* _fileD = TFile::Open(“tpeff_muId_analyzer_full.root”);
TFile* _fileMC = TFile::Open(“dm_id_mc.root”);

TDirectory* dir = _fileD->GetDirectory(“muonEffs/goodMu_pt/fit_eff_plots/”);
TString fitname = “pt_PLOT”;

TCanvas canvas = (TCanvas)dir->Get(“pt_PLOT”);
TH1F h = (TH1F)canvas->GetPrimitive(“hxy_fit_eff”);

h->GetYaxis()->SetTitle(“Muon ID efficiency”);


But I am getting segmentation violation error.

Could somebody please point the error here?

Thanks & Regards

Why do you call “canvas->Clear()” ?


Hi Rene,

I was following a previous post on the same topic. the link is here :

I also tried commenting lines :

h->GetXaxis()->SetTitle("pt_muon"); h->GetXaxis()->SetTitleSize(0.06); h->GetXaxis()->SetTitleOffset(0.56); h->GetYaxis()->SetTitle("Muon ID efficiency"); h->GetYaxis()->SetTitleSize(0.05); h->SetMaximum(1.05); h->SetMinimum(0.6);

with everything else being same.
I saw that the error was gone. But I need to call SetTitle() etc. for my plot.

I can then try with not calling canvas->clear() and see if it helps (with the above line uncommented)

Thanks & Regards,

Hi Rene,

I checked. Its still giving segmentation violation.


Our usual request;
post a short script and data file reproducing your problem


Hello Rene,

Please find the attached script.
The root file is ~9MB and not getting attached here.
I am putting it in here :

Please let me know if you could access it.

Thanks & Regards,
plot_Single.C (1.11 KB)

Hello Rene,

You can find the root file here as well : … 45YK&pli=1


Your object “hxy_fit_eff” is not a TH1F but a RooHist !!
I have modified your script such that it works. see attachment

plot_Single.C (1.16 KB)

Hello Rene,

yes, this is a RooHist. Thanks.
will try now and let you know.

Thanks & Regards,

Hi, I am also interested in extracting a TH1 histogram from a canvas and also in my case the primitive is a RooHist. I have considered the corrected macro you posted here some months ago, but at the end I need to draw the TH1 histogram not the canvas. If I put h->Draw(); instead of canvas->Draw(); , I get only the x/y axes drawn but not the points. is there a way to plot h? Thanks a lot


You probably need a way to access the RooHist rather than the underlying histogram. Since this is actually a different question, I will have better luck getting the answer by reposting your question in the Math and Stats forum.


It appears that the important and simple task of accessing histograms from canvas causes trouble from time to time. Some easy to follow and fail-safe rules are needed, because otherwise it becomes difficult to work with the data that are on a canvas.

In my case I followed the advice in this discussion:

and developed my code which is a copy of the one given in the said discussion:

1: void program(){
2: TFile miofile(“histogram22Na.root”,“read”);
3: TH1F hFra40;
4: TCanvas c1 = (TCanvas)miofile.Get(“A”);
5: hFra40 = (TH1F
6: TCanvas c2;
7: hFra40->Draw();

histogram22Na.root is a file with several Canvases and histograms;
A - a Canvas with several histograms
Energy_spectrum_top_A - title of a histogram

Everything seems perfect, but the error I persistently get is the following:

Error: illegal pointer to class object hFra40 0x0 403 program.c:7:
*** Interpreter error recovered ***

I cannot understand why and where is the problem.

Best regards,


What does:void program(){ TFile miofile("histogram22Na.root","read"); TH1F *hFra40; TCanvas *c1 = (TCanvas*)miofile.Get("A"); hFra40 = (TH1F*)c1->GetPrimitive("Energy_spectrum_top_A"); if (!hFra40) { fprintf(stdout,"Energy_spectrum_top_A not found\n"); c1->GetListOfPrimitives()->ls(); } else { TCanvas c2; hFra40->Draw(); } }prints?


If your histogram resides in any sub-pad, it will NOT be found by “canvas->GetPrimitive(…)”.
For some solutions see here (the “MCA” resides in the “pad1”): [url]Error fitting Histo from a Canvas

Sorry for taking a very long time to respond. I had been busy working on other projects.
The message that I got was the following:

Energy_spectrum_top_A not found
TPad fXlowNDC=0.01 fYlowNDC=0.51 fWNDC=0.48 fHNDC=0.48 Name= A_1 Title= A_1 Option=
 OBJ: TH1F      Energy_spectrum_top_A   Energy spectrum top A : 0 at: 0xc53cf00
TPad fXlowNDC=0.51 fYlowNDC=0.51 fWNDC=0.48 fHNDC=0.48 Name= A_2 Title= A_2 Option=
 OBJ: TH1F      No_events_per_channel_top_A     No events top A : 0 at: 0xc5ba800
TPad fXlowNDC=0.01 fYlowNDC=0.01 fWNDC=0.48 fHNDC=0.48 Name= A_3 Title= A_3 Option=
 OBJ: TH1F      Energy_spectrum_bottom_A        Energy spectrum bottom A : 0 at: 0xc5bb410
TPad fXlowNDC=0.51 fYlowNDC=0.01 fWNDC=0.48 fHNDC=0.48 Name= A_4 Title= A_4 Option=
 OBJ: TH1F      No_events_per_channel_bottom_A  No events bottom A : 0 at: 0xc5bc270

Hence, I realised that the histogram was residing on a TPad, and did a modification as suggest by Mr. Coyote.
The code below appears to get the job done. Thanks.

void canvas_test(){
	TFile miofile("/home/viesturs/Programmas/histogram22Na.root","read");
    //TH1F *hFra40;
    TCanvas *c1 = (TCanvas*)miofile.Get("A");
   // hFra40 = (TH1F*)c1->GetPrimitive("/home/viesturs/Programmas/Energy_spectrum_top_A");
   // ((TPad *)c1->FindObject("A_1"))->GetListOfPrimitives()->Print();
    TH1D *hFra40 = ((TH1D*)(((TPad *)c1->FindObject("A_1"))->GetPrimitive("Energy_spectrum_top_A")));
    if (!hFra40) {
        fprintf(stdout,"Energy_spectrum_top_A not found\n");
    } else {
	    cout<<"histogram found"<<endl;
	    TCanvas *c2=new TCanvas;
1 Like