TTree::Draw() defaults to TH1F

Hi ROOTers,

I’m curious why the TTree::Draw() method defaults to TH1F rather than TH1D. If the weights in the TTree span more than about 5 or 6 orders of magnitude, the float precision of TH1F leads to roundoff errors, leading to inaccurate computation of bin contents. It makes sense that the user should be careful when explicitly calling a TH1* constructor to check whether float precision is sufficient or not, but when the histogram is generated rather implicitly by some other code (here TTree::Draw()), I would think the default behaviour should be to use the safer TH1D. (Section 12.20 of the User’s Guide could also be a little more explicit about the fact that a TH1F is generated by default.)

Anyway, in case anyone else has this problem in the future, one can explicitly construct a TH1D, then pass it to TTree::Draw() as follows:

TH1D *h = new TH1D("h","h",nbins,energy_min,energy_max);

ROOT 6 allows you to set the default histogram precision for TTree::Draw(). It can be “float” or “double” … search for “Hist.Precision” in the “${ROOTSYS}/etc/system.rootrc” (or “`root-config --etcdir`/system.rootrc”) file.

And more to the “why”: because it has always been that way (I agreee it is very annonying). If you change the default, you might break existing code. Have a look at ROOT’s git repo: you will find a change to TH1D and a revert :slight_smile: See git commits be3668c and d0016107.

1 Like

Yes, if you change float to double in ${ROOTSYS}/etc/system.rootrc you may get some backward incompatibilities and “break the code”. But once you are aware of this and if your working environment can cope with this change, why not ? you can do it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.