Incorrect number of histogram bins using TTree Draw method

In TTree I have simple array with 512 elements. If I draw the array and pipe it into a histogram interactively using the TTree Draw method, I am getting a histogram with a different number of elememets than what I allocated. For example:


At this point it returns a value (Long64_t) 512.

but h->GetNbinsX() returns 515, not 512.

This is causing problems when I try and add “h” to other histograms that really do have 512 elements. Any ideas? Below is a simple macro that reproduces the problem.

ROOT Version 4.04/02g on Mac OSX 10.3.9

Float_t myarray[512];

for(Int_t i=0;i<512;i++)

// create tree
TTree *tree=new TTree("tree","test");

// create histogram from TTree draw method
cout << "number of x bins for histogram abc: " << abc->GetNbinsX() << endl;
// returns 515 on my machine

// create the same histogram by hand
TH1F *xyz=new TH1F("xyz","xyz",512,0,512);
for(Int_t i=0;i<512;i++)

cout << "number of x bins for histogram xyz: " << xyz->GetNbinsX() << endl;
// returns 512 on my machine

// try and add xyz and abc
TH1F *def=xyz->Clone();

// tells me histograms have different number of bins

The output during a root session is:

root [0] .x histbin.C
<TCanvas::MakeDefCanvas>: created default TCanvas with name c1
number of x bins for histogram abc: 515
number of x bins for histogram xyz: 512
Error in <TH1F::Add>: Attempt to add histograms with different number of bins

The statement:

generates 2 2-D plot (myarray vs Iteration$) that you try to inject
in a 1-d histogram h.
You should do instead


You should do instead

Thanks for the helpful tip. However, if I fill the tree in my example with several entries and try and select an array from a specifc entry in the tree, the histogram is very strange. e.g.




gives the expected visual result. I guess the lesson here is that I should just extract the array from the branch by hand and rebin it in a histogram rather than use the Draw method to do all my work…


What you observe is correct.
Note the difference between

tree->Draw("myarray>>abc(512,0,512)","Iteration$&&(Entry$==1)"); and

In the first case, the weight of each entry will be 0 or 1
In the second case the weight will be 0 or Iteration$

For more details , see the Users Guide and the documentation
of TTree::Draw