# Histogram error

**URL:** <https://root-forum.cern.ch/t/histogram-error/40941>\
**Category:** Newbie\
**Created:** [August 11, 2020, 9:07am UTC](https://root-forum.cern.ch/t/histogram-error/40941 "2020-08-11T09:07:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [August 11, 2020, 9:07am UTC](https://root-forum.cern.ch/t/histogram-error/40941/1 "2020-08-11T09:07:00Z")

</div>

I’m trying to draw two histograms (h1) on the same canvas from two different root files(1.root and 2.root). I used two macros (1.C and 2.C) but both are resulting in error.  
I’m attaching root files.  
[1.root](https://root-forum.cern.ch/uploads/short-url/oyz46w8pcbfLhQcu5xwTH5dMVwb.root) (36.8 KB) [2.root](https://root-forum.cern.ch/uploads/short-url/2iTxHdZXBB03bj5ovXtvirShPAg.root) (37.0 KB)

Macro files used:  
1.C

```auto
{
    
    TFile *f1 = TFile::Open("1.root");
    TH1D *h1= (TH1D*)f1->Get("h1");
    h1->Draw("hist l");
    h1->SetLineWidth(3);
    h1->SetLineColor(2);
    
    TFile *f2 = TFile::Open("2.root");
    TH1D *h2 = (TH1D*)f2->Get("h1");
    h2->SetLineWidth(2);
    h2->SetLineColor(4);
    h2->Draw("hist l same");
    
}

```

2.C

```auto
{

TFile *f2 = TFile::Open("1.root");
TCanvas *c2 = (TCanvas*)f2->Get("h1");
h1->Draw("hist same");
h1->SetName("aa");
aa->SetLineStyle(7);
aa->SetMarkerStyle(23);
aa->SetLineColor(4);
aa->SetLineWidth(2);

TFile *f3 = TFile::Open("2.root");
TCanvas *c3 = (TCanvas*)f3->Get("h1");
h1->Draw("hist same");
h1->SetName("in");
in->SetLineStyle(9);
in->SetMarkerStyle(20);
in->SetLineColor(2);
in->SetLineWidth(2);

leg = new TLegend(0.51,0.69,0.9,0.9);
   leg->SetHeader("beam");
   leg->AddEntry(in,"2","l");
   leg->AddEntry(aa, "1","l");
   leg->SetFillColor(10);
   leg->Draw();

}

```

Error:

 ![error_1](https://root-forum.cern.ch/uploads/default/original/3X/d/5/d586fc2804d21517f4cf53f395879d202f84ef25.png)

I dont know what causing an error (possibly different y axis range) because these macro file works fine with other files having same parameter.

Looking forward to hearing from expert root users.

_ROOT Version:_ 6.14.06  
_Platform:_ Ubuntu/Linux  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [August 11, 2020, 9:22am UTC](https://root-forum.cern.ch/t/histogram-error/40941/2 "2020-08-11T09:22:45Z")

</div>

In your root files, the histograms are called 1, 2,…, not h1, h2,…

```auto
TH1D *h1= (TH1D*)f1->Get("1");

```

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [August 11, 2020, 9:24am UTC](https://root-forum.cern.ch/t/histogram-error/40941/3 "2020-08-11T09:24:48Z")

</div>

Hi,

You have several problems in the macros.  
Here is modified code for 1.C

```auto
{
TFile *f1 = TFile::Open("1.root");
TH1D *h1= (TH1D*)f1->Get("1");
h1->SetLineWidth(3);
h1->SetLineColor(2);
h1->Draw("hist l");

TFile *f2 = TFile::Open("2.root");
TH1D *h2 = (TH1D*)f2->Get("1");
h2->SetLineWidth(2);
h2->SetLineColor(4);
h2->Draw("hist l same");
}

```

And 2.C:

```auto
{
TFile *f2 = TFile::Open("1.root");
TH1 *h1 = (TH1*)f2->Get("1");
h1->SetName("aa");
h1->SetLineStyle(7);
h1->SetMarkerStyle(23);
h1->SetLineColor(4);
h1->SetLineWidth(2);
h1->Draw("hist");

TFile *f3 = TFile::Open("2.root");
TH1 *h2 = (TH1*)f3->Get("1");
h2->SetName("in");
h2->SetLineStyle(9);
h2->SetMarkerStyle(20);
h2->SetLineColor(2);
h2->SetLineWidth(2);
h2->Draw("hist same");

leg = new TLegend(0.51,0.69,0.9,0.9);
leg->SetHeader("beam");
leg->AddEntry(h1,"2","l");
leg->AddEntry(h2, "1","l");
leg->SetFillColor(10);
leg->Draw();
}

```

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [August 11, 2020, 9:31am UTC](https://root-forum.cern.ch/t/histogram-error/40941/4 "2020-08-11T09:31:54Z")

</div>

Thank you so much. 🙂

---

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [August 11, 2020, 9:33am UTC](https://root-forum.cern.ch/t/histogram-error/40941/5 "2020-08-11T09:33:08Z")

</div>

Thank you for pointing my silly mistake. 😀

---

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [August 11, 2020, 10:10am UTC](https://root-forum.cern.ch/t/histogram-error/40941/6 "2020-08-11T10:10:58Z")

</div>

I included 2 more root files that have a high range of Y-axis. The full histogram is not confined in canvas and the upper part of the histogram is cut. How this problem can be resolved?  
Root files (4)  
[1.root](https://root-forum.cern.ch/uploads/short-url/oyz46w8pcbfLhQcu5xwTH5dMVwb.root) (36.8 KB) [2.root](https://root-forum.cern.ch/uploads/short-url/2iTxHdZXBB03bj5ovXtvirShPAg.root) (37.0 KB) [3.root](https://root-forum.cern.ch/uploads/short-url/yWWQkJpwtnWiB5X6JsHCSQXzpM7.root) (36.0 KB) [4.root](https://root-forum.cern.ch/uploads/short-url/2iQCsDAJjTAEyGoecA9DO81CF8a.root) (35.3 KB)

Macro file used

{

TFile \*f1 = TFile::Open(“1.root”);  
TH1D _h1= (TH1D_)f1-\>Get(“1”);  
h1-\>SetLineWidth(3);  
h1-\>SetLineColor(2);  
h1-\>Draw(“hist l”);

TFile \*f6 = TFile::Open(“2.root”);  
TH1D _h6 = (TH1D_)f6-\>Get(“1”);  
h6-\>SetLineWidth(2);  
h6-\>SetLineColor(94);  
h6-\>Draw(“hist l same”);

TFile \*f4 = TFile::Open(“3.root”);  
TH1D _h4 = (TH1D_)f4-\>Get(“1”);  
h4-\>SetLineWidth(2);  
h4-\>SetLineColor(8);  
h4-\>Draw(“hist l same”);

TFile \*f5 = TFile::Open(“4.root”);  
TH1D _h5= (TH1D_)f5-\>Get(“1”);  
h5-\>SetLineWidth(3);  
h5-\>SetLineColor(9);  
h5-\>Draw(“hist l same”);

leg = new TLegend(0.51,0.69,0.9,0.9);  
leg-\>SetHeader(“Energy deposition”);  
leg-\>AddEntry(h1,“1”,“l”);  
leg-\>AddEntry(h4, “4”,“l”);  
leg-\>AddEntry(h5,“5”,“l”);  
leg-\>AddEntry(h6, “6”,“l”);  
leg-\>SetFillColor(10);  
leg-\>Draw();

}

![result_canvas](https://root-forum.cern.ch/uploads/default/original/3X/5/3/531da6ad0e1747bcb473f9f408d35d3973db3eaa.png)

Waiting for the kind reply.

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [August 11, 2020, 10:16am UTC](https://root-forum.cern.ch/t/histogram-error/40941/7 "2020-08-11T10:16:02Z")

</div>

> [@NewRootUser](#):
>
> The full histogram is not confined in canvas and the upper part of the histogram is cut. How this problem can be resolved?

You should first draw histogram with maximal amplitude.  
Or use [THStack](https://root.cern/doc/master/classTHStack.html) class which does proper alignments automatically.

---

<div class="post-metadata">

**Author:** ![NewRootUser](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/9fc348/32.png) [@NewRootUser](https://root-forum.cern.ch/u/NewRootUser)\
**Post date:** [August 11, 2020, 10:33am UTC](https://root-forum.cern.ch/t/histogram-error/40941/8 "2020-08-11T10:33:49Z")

</div>

Thanks, drawing the histo of maximum amplitude resolved the problem. 😀

---

<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:** [September 1, 2020, 8:02am UTC](https://root-forum.cern.ch/t/histogram-error/40941/9 "2020-09-01T08:02:05Z")

</div>

Not that using THStack would be simpler/safer in your case. THStack computes the full range containing all histograms. You do not need to know which histogram has the bigger amplitude
