# Convert canvas into histogram in root

**URL:** <https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322>\
**Category:** Roofit and RooStats\
**Created:** [July 3, 2013, 9:27am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322 "2013-07-03T09:27:42Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![kanishka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/3e96dc/32.png) [@kanishka](https://root-forum.cern.ch/u/kanishka)\
**Post date:** [July 3, 2013, 9:27am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/1 "2013-07-03T09:27:42Z")

</div>

Hello all,

I have a root file which has canvas saved inside it. I want to convert canvas into histograms. I have attached my root file with this.

My root code is:

{  
TFile file(“jetht\_mad.root”,“read”);  
TH1F \*h;  
TCanvas _c1 = (TCanvas_)file.Get(“Canvas\_1”);  
TGraphAsymmErrors _ae = (TGraphAsymmErrors_)c1-\>GetPrimitive(“JetsHT\_mu\_inc1jet”);

TH1F _h = (TH1F_)ae-\>GetHistogram();

// h1 = (TGraphAsymmErrors\*)c1-\>GetPrimitive(“JetsHT\_mu\_inc1jet”);  
c1-\>Clear();  
c1-\>Draw();  
//TCanvas c2;  
h-\>Draw();  
}  
[jetht\_mad.root](https://root-forum.cern.ch/uploads/default/original/2X/7/7e14c52cf70ac69b86535bb312add356a23f7795.root) (13.7 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 3, 2013, 10:12am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/2 "2013-07-03T10:12:23Z")

</div>

when you do:

```auto
{ 
	TFile file("jetht_mad.root","read");
	TCanvas *c1 = (TCanvas*)file.Get("Canvas_1");
	TGraphAsymmErrors *ae = (TGraphAsymmErrors*)c1->GetPrimitive("JetsHT_mu_inc1jet");
	TH1F *h = (TH1F*)ae->GetHistogram();
	TCanvas c2;
   h->Draw();
}
```

you get a canvas with an empty plot. That’s normal because the histogram inside the Graph is just an empty frame used to draw the axis. The data are hold by the graph.

---

<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:** [July 3, 2013, 10:17am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/3 "2013-07-03T10:17:11Z")

</div>

The [TGraph::GetHistogram](http://root.cern.ch/root/html/TGraph.html#TGraph:GetHistogram) method returns a pointer to the histogram used to draw the axis. This histogram is “empty” (i.e. the contents of the graph is not used to fill the histogram). If this is what you want, try:

```auto
TH1F *h = (TH1F*)(ae->GetHistogram()->Clone("h")); h->SetDirectory(gROOT);

```

---

<div class="post-metadata">

**Author:** ![kanishka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/3e96dc/32.png) [@kanishka](https://root-forum.cern.ch/u/kanishka)\
**Post date:** [July 3, 2013, 11:54am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/4 "2013-07-03T11:54:24Z")

</div>

I have used a discussion to get the plot but again I am getting the Canvas. [root.cern.ch/root/roottalk/roottalk04/2633.html](http://root.cern.ch/root/roottalk/roottalk04/2633.html)  
Please let me know if I am going wrong somewhere. I did now:

{  
TFile file(“jetht\_mad.root”,“read”);  
TH1F _h;  
TCanvas c1 = (TCanvas)file.Get(“Canvas\_1”);  
h = (TH1F_)c1-\>GetPrimitive(“JetsHT\_mu\_inc1jet”);  
c1-\>Clear();  
c1-\>Draw();  
h-\>Draw();  
}

---

<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:** [July 3, 2013, 12:02pm UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/5 "2013-07-03T12:02:19Z")

</div>

Try:

```auto
TFile file("jetht_mad.root","read");
file.ls();
Canvas_1->ls();

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 3, 2013, 1:50pm UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/6 "2013-07-03T13:50:29Z")

</div>

Your canvas contains a TGraph not a TH1 I would suggest you use the TGraph as follow:

```auto
{ 
   TFile file("jetht_mad.root","read");
   TCanvas *c1 = (TCanvas*)file.Get("Canvas_1");
   TGraphAsymmErrors *ae = (TGraphAsymmErrors*)c1->GetPrimitive("JetsHT_mu_inc1jet");
   TCanvas c2;
   ae->Draw();
}
```

Then you can eventually get the points from the graph and fill an histogram with them but I think you better use directly the graph …

---

<div class="post-metadata">

**Author:** ![kanishka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/3e96dc/32.png) [@kanishka](https://root-forum.cern.ch/u/kanishka)\
**Post date:** [July 3, 2013, 4:37pm UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/7 "2013-07-03T16:37:06Z")

</div>

This is the output after trying this:

TFile\*\* jetht\_mad.root  
TFile\* jetht\_mad.root  
KEY: TCanvas Canvas\_1;1 Canvas\_1  
Canvas Name=Canvas\_1 Title=Canvas\_1 Option=  
TCanvas fXlowNDC=0 fYlowNDC=0 fWNDC=1 fHNDC=1 Name= Canvas\_1 Title= Canvas\_1 Option=  
OBJ: TList TList Doubly linked list : 0  
TFrame X1= 0.000000 Y1=-3.795196 X2=1022.200000 Y2=0.379502  
OBJ: TGraphAsymmErrors JetsHT\_mu\_inc1jet : 1 at: 0x8c8cd98

---

<div class="post-metadata">

**Author:** ![kanishka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/3e96dc/32.png) [@kanishka](https://root-forum.cern.ch/u/kanishka)\
**Post date:** [July 3, 2013, 4:39pm UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/8 "2013-07-03T16:39:56Z")

</div>

I have tried this :  
{  
TFile file(“jetht\_mad.root”,“read”);  
TCanvas _c1 = (TCanvas_)file.Get(“Canvas\_1”);  
TGraphAsymmErrors _ae = (TGraphAsymmErrors_)c1-\>GetPrimitive(“JetsHT\_mu\_inc1jet”);  
TCanvas c2;  
ae-\>Draw();  
}

But i m getting an empty canvas.

---

<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:** [July 3, 2013, 5:12pm UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/9 "2013-07-03T17:12:10Z")

</div>

```auto
{
  TFile file("jetht_mad.root","read");
  TCanvas *c1 = (TCanvas*)file.Get("Canvas_1");
  TGraphAsymmErrors *ae = (TGraphAsymmErrors*)(c1->GetPrimitive("JetsHT_mu_inc1jet")->Clone("ae"));
  delete c1; // no longer needed
  TCanvas *c2 = new TCanvas("c2", "c2");
  ae->Draw("AL");
}

```

---

<div class="post-metadata">

**Author:** ![kanishka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/3e96dc/32.png) [@kanishka](https://root-forum.cern.ch/u/kanishka)\
**Post date:** [July 3, 2013, 5:41pm UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/10 "2013-07-03T17:41:45Z")

</div>

Thanks. Now I’m getting the plot but its again on new canvas. I want to get the plot in histogram form not in the canvas form. can you please help?

---

<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:** [July 3, 2013, 5:44pm UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/11 "2013-07-03T17:44:39Z")

</div>

There is no method which transforms a “TGraphAsymmErrors” into a “TH1”.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 4, 2013, 7:45am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/12 "2013-07-04T07:45:04Z")

</div>

> [@](#):
>
> I want to get the plot in histogram form not in the canvas form. can you please help?

As I said earlier you can get the TGraphAsymmErrors content and fill a histogram with it but seems to me you better stay with the TGraphAsymmErrors. Your data set consist of discrete points with asymmetric errors bars. TGraphAsymmErrors is the best object to hold such data.

---

<div class="post-metadata">

**Author:** ![kanishka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/3e96dc/32.png) [@kanishka](https://root-forum.cern.ch/u/kanishka)\
**Post date:** [July 4, 2013, 8:24am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/13 "2013-07-04T08:24:51Z")

</div>

Actually I have two root files. One is attached above. Other one is attached with this message. I want to superimpose two histograms from these files. Both root files have canvas saved inside it. But I am n’t able to superimpose them. That’s why I want to convert them to histograms. I followed this:

{  
TFile f1(“jetht\_mad.root”);  
h1-\>Draw();  
h1-\>SetLineColor(kRed);  
TFile f2(“jet\_HT\_data.root”);  
h2-\>Draw(“same”);  
h2-\>SetLineColor(kBlue);  
c1-\>Update();

}

There are canvas inside these root files. So do nt know how to deal with them.

regards,  
kanishka  
[jet\_HT\_data.root](https://root-forum.cern.ch/uploads/default/original/2X/4/4657c5fce6e7a966082632690395769edb807d7f.root) (14.8 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 4, 2013, 9:12am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/14 "2013-07-04T09:12:18Z")

</div>

```auto
{ 
   TFile f1("jetht_mad.root","read");
   TCanvas *c1 = (TCanvas*)f1.Get("Canvas_1");
   TGraphAsymmErrors *ae = (TGraphAsymmErrors*)c1->GetPrimitive("JetsHT_mu_inc1jet");
   
   TFile f2("jet_HT_data.root","read");
   TCanvas *c2 = (TCanvas*)f2.Get("c1");
   TH1D *h1;
   
   TList* l = c2-> GetListOfPrimitives();
   TIter next(l);
   TObject *found, *obj;
   while ((obj=next())) {
      if (obj->InheritsFrom(TH1D::Class())) {
         h1 = (TH1D*)obj;
      }
   }
   

   TCanvas c3;
   h1->Draw();
   ae->Draw("L");
}
```

---

<div class="post-metadata">

**Author:** ![kanishka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/3e96dc/32.png) [@kanishka](https://root-forum.cern.ch/u/kanishka)\
**Post date:** [July 4, 2013, 9:25am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/15 "2013-07-04T09:25:12Z")

</div>

Thanks it works…

regards,  
Kanishka

---

<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:** [July 4, 2013, 9:42am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/16 "2013-07-04T09:42:18Z")

</div>

```auto
#include "TFile.h"
#include "TCanvas.h"
#include "TPad.h"
#include "TGraphAsymmErrors.h"
#include "TH1.h"
#include "THStack.h"
#include "TROOT.h"
#include "TObject.h"

void trial(void)
{
  TFile *f;
  TCanvas *c;
  TGraphAsymmErrors *g = 0;
  TH1D *h = 0;
  
  f = TFile::Open("jetht_mad.root");
  f->GetObject("Canvas_1", c);
  if (c) {
    g = (TGraphAsymmErrors*)(c->GetPrimitive("JetsHT_mu_inc1jet")->Clone("g"));
    if (g) {
      g->SetMarkerColor(kRed);
      g->SetLineColor(kRed);
    }
  }
  delete c; // no longer needed
  delete f; // no longer needed
  
  f = TFile::Open("jet_HT_data.root");
  f->GetObject("c1", c);
  if (c) {
    h = (TH1D*)(c->GetPrimitive("Central")->Clone("h"));
    if (h) {
      h->SetDirectory(gROOT);
      h->SetMarkerColor(kBlue);
      h->SetLineColor(kBlue);
      h->SetStats(0); // delete the statistics box for this histogram
    }
  }
  delete c; // no longer needed
  delete f; // no longer needed
  
  c = new TCanvas("c", "c");
  gPad->SetLogy(kTRUE);
  
  // create and draw a "common" axes frame
  THStack *s = new THStack("s", "s");
  if (g) s->Add(g->GetHistogram());
  if (h) s->Add(h);
  s->Draw("nostack axis");
  
  if (g) {
    g->Draw("same");
    if (h) h->Draw("same");
  } else if (h) h->Draw("same");
  
  c->Modified(); c->Update();
}

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 4, 2013, 10:11am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/17 "2013-07-04T10:11:26Z")

</div>

Note that in your second canvas the TH1D has a totally unusable name, that’s why I was obliged to use this ugly code to retrieve it. In the future try to have short names with no special characters. Such long string should be reserved for the title.

---

<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:** [July 4, 2013, 10:18am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/18 "2013-07-04T10:18:12Z")

</div>

Fortunately, ROOT recognized that TH1D as “Central”. Let’s call it a “feature”, not a “bug”. ![:mrgreen:](https://root-forum.cern.ch/uploads/default/original/2X/2/2799b3ea86c419a02327c767288415b2a17e6ee1.gif "Mr. Green")

---

<div class="post-metadata">

**Author:** ![mendoza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mendoza/32/26128_2.png) [@mendoza](https://root-forum.cern.ch/u/mendoza)\
**Post date:** [November 25, 2020, 10:00am UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-root/16322/19 "2020-11-25T10:00:08Z")

</div>

Dear experts,

I am sorry to bother you. I am trying to implement your code in my program, but using pyroot. I can make it work and I don’t know what more to do.

Thanks for your time and I apologize again.

Diego

> [@Convert canvas into histogram in pyroot](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-pyroot/42425):
>
> Dear Users, I use PYROOT and I have a TFile with a canvas inside. I need to convert it into an histogram because I will work with it after. I have used some of the topics very related to that but it does not work, maybe because I am using PYROOT.
