# TH1F constructor, setting the x axis range does not work

**URL:** <https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162>\
**Category:** Newbie\
**Created:** [November 19, 2023, 3:38pm UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162 "2023-11-19T15:38:33Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafal4](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rafal4](https://root-forum.cern.ch/u/rafal4)\
**Post date:** [November 19, 2023, 3:38pm UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/1 "2023-11-19T15:38:33Z")

</div>

Hello,  
I would like to ask for a hint regarding the scope of the simple histogram.  
I need to draw a simple histogram of the variable x, only in a narrow range of x from 10 to 15. I set the range in the constructor: TH1F\* h = new TH1F(“Histogram X”, “Histogram ;X ; Events”, 5,10,15); but it does not change the range,  
Thank you and best regards

The macro code is below:

```auto
void histogram1(){
   TCanvas c1("Pion E=1GeV", "Pion E=1GeV; z; x", 1600, 1000); 
   TFile f("Pion1GeV.root");
   TTree * evt = (TTree*)f.Get("evt");
   TH1F* h = new TH1F("Histogram X", "Histogram ;X ; Events", 5,10,15);// it does not change the range,
   evt->Draw("x");
   evt->GetHistogram()->SetTitle("Pion E=1GeV");
   evt->GetHistogram()->SetXTitle("x");
   evt->GetHistogram()->SetYTitle("Events");
   evt->GetHistogram()->SetFillColor(kCyan);
   gPad->SetLogy(1);
   gPad->SetGrid();
   c1.SaveAs("Pion1GeV.png");
   f.Close();
}

```

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 19, 2023, 8:14pm UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/2 "2023-11-19T20:14:23Z")

</div>

Hi,

Thanks for posting the issue. I think that in order to help, we’d need some more info, if possible.  
I understand you are trying to plot the `emitter_pdgid` column values from the `evt` TTree in the `Pion1GeV.root` into a histogram with 5 bins, with an x range from 10 to 15, right?

Cheers,  
Danilo

---

<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:** [November 20, 2023, 5:22am UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/3 "2023-11-20T05:22:54Z")

</div>

Try

```auto
evt->Draw("emitter_pdgid >> Histogram X");`

```

---

<div class="post-metadata">

**Author:** ![rafal4](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rafal4](https://root-forum.cern.ch/u/rafal4)\
**Post date:** [November 20, 2023, 9:17am UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/4 "2023-11-20T09:17:12Z")

</div>

Hi,  
Thank you for your response. In this case I use TTree. I wrote it wrong, it should be evt-\>Draw(“x”);. By default, the histogram draws the full range of the x variable from - 11 to 15. I’m only interested in 10 to 15, only five bins. Is there any easy way to draw a histogram of x only from 10 to 15?

Cheers

---

<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:** [November 20, 2023, 12:32pm UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/5 "2023-11-20T12:32:55Z")

</div>

> [@rafal4](#):
>
> Is there any easy way to draw a histogram of x only from 10 to 15?

```auto
h->GetXaxis()->SetRangeUser(10.,15.); 

```

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 20, 2023, 1:45pm UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/6 "2023-11-20T13:45:12Z")

</div>

Hi,

Just to complement everything which was suggested (which I like a lot), you can easily perform this operation with [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) in just 3 simple lines:

```auto
auto rdf = ROOT::RDataFrame("evt", "Pion1GeV.root");
auto myHisto = rdf.Histo1D({"Histogram X", "Histogram ;X ; Events", 5,10,15}, "x");
myHisto->Draw();

```

I hope this helps!

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![rafal4](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rafal4](https://root-forum.cern.ch/u/rafal4)\
**Post date:** [November 22, 2023, 9:37am UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/7 "2023-11-22T09:37:02Z")

</div>

Hello.

Thank you Danilo. Now, I can set the range ! Your tips is very useful

Can I ask how to save myHisto-\>GetHistogram()-\>SetFillColor(kCyan) correctly?  
because I get an error  
error: no member named ‘GetHistogram’ in ‘TH1D’  
myHisto-\>GetHistogram()-\>SetFillColor(kCyan);

Cheers  
Rafal

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 22, 2023, 10:14am UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/9 "2023-11-22T10:14:56Z")

</div>

Hello Rafal,

In the example above, `myHisto` is a [RResultPtr](https://root.cern.ch/doc/master/classROOT_1_1RDF_1_1RResultPtr.html), a smart pointer to the result of the action booked on the data frame, in this case a `Histo1D`.

Very concretely, the `RResultPtr` wraps the result of the operation, which you can access with the `->` operator directly. Therefore, to change the colour of your histogram, the syntax would be

```auto
myHisto->SetFillColor(kCyan);

```

I hope this helps!

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![rafal4](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rafal4](https://root-forum.cern.ch/u/rafal4)\
**Post date:** [November 22, 2023, 11:18am UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/10 "2023-11-22T11:18:22Z")

</div>

> [@Danilo](#):
>
> `myHisto->->SetFillColor(kCyan);`

Thank you Danilo. The macro runs. The data range is correct. The values on the X and Y axes are correct. But the shape of the bar is not correct. Why is the bar now only a narrow horizontal line. I thought that after filling it with color it would be correct, but it isn’t

Cheers

 ![obraz](https://root-forum.cern.ch/uploads/default/original/3X/3/e/3e28569496ccbab2c91e10894d34b3eeb9f1bbd7.png)

---

<div class="post-metadata">

**Author:** ![rafal4](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rafal4](https://root-forum.cern.ch/u/rafal4)\
**Post date:** [November 22, 2023, 11:55am UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/11 "2023-11-22T11:55:28Z")

</div>

The code:

```auto
void histogram3(){
   TFile f("Pion1GeV.root");
	 
   TTree * evt = (TTree*)f.Get("evt");
   TH1F* h1 = new TH1F("Histogram", "Histogram title;X Axis;Y Axis", 5,10,15);
   TCanvas c1("Histogram", "pion", 1600, 1000);
	  
   auto rdf = ROOT::RDataFrame("evt", "Pion1GeV.root");
   auto myHisto = rdf.Histo1D({"Histogram X", "Histogram ;X ; Events", 5,10,15}, "x");
   gPad->SetLogy(1);
   gPad->SetGrid();

   myHisto->Draw("x"); 
   myHisto->SetFillColor(kCyan);
 
   c1.SaveAs("Pion1GeV.png");
   f.Close();
}

```

---

<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:** [November 22, 2023, 12:24pm UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/12 "2023-11-22T12:24:16Z")

</div>

Try (see doc):

```auto
myHisto->Draw("hist");

```

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 22, 2023, 12:50pm UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/13 "2023-11-22T12:50:20Z")

</div>

Hi!

Nothing to add to what olivier said. Maybe that you have an option too much for the Deaw method 😉

Cheers,

D

---

<div class="post-metadata">

**Author:** ![rafal4](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rafal4](https://root-forum.cern.ch/u/rafal4)\
**Post date:** [November 23, 2023, 9:37am UTC](https://root-forum.cern.ch/t/th1f-constructor-setting-the-x-axis-range-does-not-work/57162/14 "2023-11-23T09:37:41Z")

</div>

Hello,

Thank you very much Danillo and Oliver. Histograms are drawn correctly. Your help is very important to me.

Cheers,  
Rafal
