Specifying the x-axis when creating a TH1 from a text file

Hi Root Experts,

I am trying to create a TH1 from a text file. I have found that I can do this by saving the data table as an ntuple, drawing the ntuple, saving this plot as a TH2, and then converting this TH2 to a TProfile and finally to a TH1 with TH2::ProfileX::ProjectionX.

The problem I am running into is that I can’t figure out how force the x-axis binning that I would like. When I create my TH2 while plotting the ntuple with something like:
I can’t force htemp to use the binning I am specifying. If I specify a range bigger or smaller than the ntuple range, htemp has the exact range of the ntuple.

For example, if I have a x-range of 1.5-1.9, I can’t figure out how to make my histogram have a range of 1.6-1.8 or 1.0-2.0. My TH2 will just have an x-range of 1.5-1.9. This is inconvenient since I can’t make a TH1 with the binning I would like after TH2::ProfileX::ProjectionX.

Below I wrote a simple test macro to demonstrate my issue. Perhaps there are better ways to get from a text file to a histogram? I hope I have explained my issue at least somewhat coherently. Thanks is advance!!!



TNtuple *ntuple = new TNtuple(“ntuple”,“NTUPLE”,“a:b”); //Define ntuple
ntuple->ReadFile(“data.txt”); //Fill it with data
ntuple->Draw(“b:a>>htemp(3,1.1,1.8)”,"",""); //Draw data saving htemp

htemp->ProfileX()->Draw(); //Draw the TProfile. Why is the range 1.5-1.9 and not 1.1-1.8 as specified above???
htemp->ProfileX()->ProjectionX()->Print(“all”); //Show the TH1D from the TProfile



1.5 30
1.6 145
1.7 600
1.75 700
1.8 93
1.9 45

Well, “b:a>>htemp” means that the “htemp” will be a TH2F, so try to set BOTH, the “x-” and the “y-” direction binnings:
ntuple->Draw(“b:a>>htemp(10, 1, 2, 10, 0, 1000)”, “”, “”)
See “Saving the result of Draw to an histogram” in the TTree::Draw method description.

Thanks for the reply!

Indeed that does fix things. It looks like there can be a tough balance between defining enough y bins so as to not loose accuracy and yet not define so many y bins as to make the macro quite slow. I wish there were more direct ways to get from a text file to a TH1. Something like a TH1::ReadFile would be extremely useful.

Anyway, many thanks for the help!