TH1F constructor, setting the x axis range does not work

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:

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();
}

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

1 Like

Try

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

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

h->GetXaxis()->SetRangeUser(10.,15.); 
1 Like

Hi,

Just to complement everything which was suggested (which I like a lot), you can easily perform this operation with RDataFrame in just 3 simple lines:

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

1 Like

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

Hello Rafal,

In the example above, myHisto is a RResultPtr, 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

myHisto->SetFillColor(kCyan);

I hope this helps!

Cheers,
D

1 Like

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

The code:

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();
}

Try (see doc):

myHisto->Draw("hist");
1 Like

Hi!

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

Cheers,

D

1 Like

Hello,

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

Cheers,
Rafal