# Problem with accessing histogram from canvas

**URL:** <https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307>\
**Category:** ROOT\
**Created:** [November 7, 2010, 3:22pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307 "2010-11-07T15:22:53Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shilpi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/958977/32.png) [@Shilpi](https://root-forum.cern.ch/u/Shilpi)\
**Post date:** [November 7, 2010, 3:22pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/1 "2010-11-07T15:22:53Z")

</div>

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-\>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);

h-\>SetFillColor(4);  
h-\>SetFillStyle(0);  
h-\>SetLineColor(4);  
h-\>SetLineWidth(2);  
canvas-\>Clear();  
canvas-\>Draw();  
h-\>Draw(“e”);[/code]

But I am getting segmentation violation error.

Could somebody please point the error here?

Thanks & Regards  
Shilpi

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [November 8, 2010, 9:05pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/2 "2010-11-08T21:05:38Z")

</div>

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

Rene

---

<div class="post-metadata">

**Author:** ![Shilpi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/958977/32.png) [@Shilpi](https://root-forum.cern.ch/u/Shilpi)\
**Post date:** [November 8, 2010, 9:53pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/3 "2010-11-08T21:53:46Z")

</div>

Hi Rene,

I was following a previous post on the same topic. the link is here :  
[http://root.cern.ch/root/roottalk/roottalk04/2633.html](http://root.cern.ch/root/roottalk/roottalk04/2633.html)

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,  
Shilpi

---

<div class="post-metadata">

**Author:** ![Shilpi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/958977/32.png) [@Shilpi](https://root-forum.cern.ch/u/Shilpi)\
**Post date:** [November 8, 2010, 10:15pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/4 "2010-11-08T22:15:55Z")

</div>

Hi Rene,

I checked. Its still giving segmentation violation.

Thanks,  
Shilpi

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [November 9, 2010, 7:59am UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/5 "2010-11-09T07:59:49Z")

</div>

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

Rene

---

<div class="post-metadata">

**Author:** ![Shilpi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/958977/32.png) [@Shilpi](https://root-forum.cern.ch/u/Shilpi)\
**Post date:** [November 9, 2010, 5:46pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/6 "2010-11-09T17:46:31Z")

</div>

Hello Rene,

Please find the attached script.  
The root file is ~9MB and not getting attached here.  
I am putting it in here :  
/afs/cern.ch/user/s/shilpi/public/root\_error

Please let me know if you could access it.

Thanks & Regards,  
Shilpi  
[plot\_Single.C](https://root-forum.cern.ch/uploads/default/original/2X/3/3685f79a3d31426dadb3064529632ee06bfdbb8a.C) (1.11 KB)

---

<div class="post-metadata">

**Author:** ![Shilpi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/958977/32.png) [@Shilpi](https://root-forum.cern.ch/u/Shilpi)\
**Post date:** [November 9, 2010, 8:54pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/7 "2010-11-09T20:54:58Z")

</div>

Hello Rene,

You can find the root file here as well :

[docs.google.com/leaf?id=0B-Fi4D … 45YK&pli=1](https://docs.google.com/leaf?id=0B-Fi4DWNYDa7ZDNhZmM5NWYtOGEwNS00ZTNlLWJkZjEtNjliMTkxYmNmYTlh&hl=en&authkey=CLiD45YK&pli=1)

Thanks,  
Shilpi

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [November 9, 2010, 10:04pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/8 "2010-11-09T22:04:16Z")

</div>

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

Rene  
[plot\_Single.C](https://root-forum.cern.ch/uploads/default/original/2X/6/62ec53e53c3303b29dabc6c391203beae6e8aacc.C) (1.16 KB)

---

<div class="post-metadata">

**Author:** ![Shilpi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/958977/32.png) [@Shilpi](https://root-forum.cern.ch/u/Shilpi)\
**Post date:** [November 9, 2010, 10:12pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/9 "2010-11-09T22:12:23Z")

</div>

Hello Rene,

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

Thanks & Regards,  
Shilpi

---

<div class="post-metadata">

**Author:** ![yoshi80](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/e19b73/32.png) [@yoshi80](https://root-forum.cern.ch/u/yoshi80)\
**Post date:** [May 19, 2011, 3:23pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/10 "2011-05-19T15:23:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [May 22, 2011, 2:00pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/11 "2011-05-22T14:00:01Z")

</div>

Hi,

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.

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![Viesturs](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/7cd45c/32.png) [@Viesturs](https://root-forum.cern.ch/u/Viesturs)\
**Post date:** [October 18, 2012, 2:22pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/12 "2012-10-18T14:22:56Z")

</div>

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: [root.cern.ch/root/roottalk/roottalk04/2632.html](http://root.cern.ch/root/roottalk/roottalk04/2632.html)

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_)c1-\>GetPrimitive(“Energy\_spectrum\_top\_A”);  
6: TCanvas c2;  
7: hFra40-\>Draw();  
8:}

where  
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,  
Viesturs

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 18, 2012, 2:31pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/13 "2012-10-18T14:31:26Z")

</div>

hi,

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?

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 18, 2012, 4:41pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/14 "2012-10-18T16:41:05Z")

</div>

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](https://root-forum.cern.ch/t/error-fitting-histo-from-a-canvas/15097/4)

---

<div class="post-metadata">

**Author:** ![Viesturs](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/7cd45c/32.png) [@Viesturs](https://root-forum.cern.ch/u/Viesturs)\
**Post date:** [November 27, 2012, 4:38pm UTC](https://root-forum.cern.ch/t/problem-with-accessing-histogram-from-canvas/11307/15 "2012-11-27T16:38:08Z")

</div>

Hello,  
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:

```auto
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.

```auto
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")));
    hFra40->Draw();
    if (!hFra40) {
        fprintf(stdout,"Energy_spectrum_top_A not found\n");
        c1->GetListOfPrimitives()->ls();
    } else {
        
	    cout<<"histogram found"<<endl;
	    TCanvas *c2=new TCanvas;
            hFra40->Draw();
    }
}
```
