Histogram binning problems when using TTree::Draw

Hi,

I have been trying to make a macro which loops over all branches of two trees with identical structures, and plots the branch contents for both files on the same canvas so that the results can be compared.

Obviously, the default binning is not always the same due to outliers etc, and so I use the “>>hist” option to create a new histogram from what is drawn by TTree:Draw, get the nxbins, xmin, xmax from “hist”, and make the second histogram I want to plot using “>>hist1(nxbins,xmin,max)” so that the binning of hist & hist1 is the same, and they can be meaningfully compared.

However, once I set the binning of hist1, the binning of hist on the next branch seems to be changed. For instance, the nxbins for “hist” for the first few branches should be [3,101,4,4,11], but if I set the binning of “hist1” to be the same, the nxbins of “hist” becomes [3,4,6,6,8].

If I use a binning of “hist1>>(100,xmin,xmax)” then the I get the nxbins for “hist” to be [3,101,4,4,11] as it should be, but with “hist1>>(10,xmin,xmax)” I get [3,11,4,4,15].

I delete hist & hist1 at the end of the loop, so I think it is some environment variable being set that causes the problem. If so, is there some way I can reset it?

I have attached the macro. I am using root version 5.26.00e

Thanks,

Nick.
plotBranches.C (3.53 KB)

VIVE L’AMOUR!
first, it seems to me that you try to use a lot of histograms named “hist_$i” and “hist1_$i”, where “$i” is an integer number from 0 to " the total number of branches - 1".
Second, it seems to me that you expect all these histograms to exist prior to their usage in the “Draw” command. I don’t have your root files so I am not able to check if they really exist (if not, your macro will go berserk).
Third, instead of doing all these crazy things with “std::stringstream”, why don’t you try http://root.cern.ch/root/html/TString.html
I am stupid. No?
Pepe Le Pew.

Hi,

“hist_$i” and “hist1_$i” are created by the TTree::Draw, as in
root.cern.ch/root/html/TTree.html#TTree:Draw%2 (from “Saving the result of Draw to an histogram”)

I appended the $i to the names so that each branch really made a new histogram, to ensure that it wasn’t the previous contents/settings of the histograms that was causing the problem, although I think this should be dealt with anyway by deleting the histograms at the end of the loop.

hist_$i" and “hist1_$i” are created by the call to TTree::Draw, as in
root.cern.ch/root/html/TTree.html#TTree:Draw%2 (from “Saving the result of Draw to an histogram”)
The histograms are all found and plotted OK by the macro, so they do all exist.

You are probably right that I could do thigs more simply with TStrings, I didn’t realise that you could just get a const char* from it via TString::Data(). However, I check what is written to these stringstreams before they are passed to TTree:Draw, and the contents are what I expect, so again I don’t think this is where the problem lies.

Thanks,

Nick.

VIVE L’AMOUR!
sorry, you are right. I’ve got screwed by all these tons of “std::stringstream” “std::stringstream” >> << >> << .str() .c_std() .str() .c_std() …
I can see now that you create “>>hist_$i” using the defaults of the “Draw”, then you take the resulting NbinsX, Xmin and Xmax and you create “>>hist1_$i(NbinsX, Xmin, Xmax)” using another “Draw”.
Your problem may be related to the following note present in the “Draw” description:
"When a new binning is used the new value will become the default."
Try to replace:
ss << branchname << branchappend ;
with:
ss << branchname << branchappend << “(1000)” ; // “reset” automatic binning for "Draw"
A pitiful case, am I not?
Pepe Le Pew.

Yes, that seems to work.

Thanks very much!

Nick.