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:

tree->Draw(“myarray:Iteration$>>h(512,0,512)”)

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++)
{
  myarray[i]=i;
}

// create tree
TTree *tree=new TTree("tree","test");
tree->Branch("myarray",myarray,"myarray[512]/F");
tree->Fill();

// create histogram from TTree draw method
tree->Draw("myarray:Iteration$>>abc(512,0,512)","Entry$==0");
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++)
{
  xyz->Fill(i,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();
def->Add(abc);

// 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:
tree->Draw(“myarray:Iteration$>>h(512,0,512)”)

generates 2 2-D plot (myarray vs Iteration$) that you try to inject
in a 1-d histogram h.
You should do instead
tree->Draw(“myarray>>h(512,0,512)”,“Iteration$”)

Rene

[quote=“brun”]
You should do instead
tree->Draw(“myarray>>h(512,0,512)”,“Iteration$”)
Rene[/quote]

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.

tree->Draw(“myarray>>h(512,0,512)”,“Iteration$”&&Entry$==1)

whereas

tree->Draw(“myarray:Iteration$>>h(512,0,512)”,“Entry$==1”)

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…

Thanks,
tom

What you observe is correct.
Note the difference between

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

tree->Draw("myarray>>abc(512,0,512)","Iteration$*(Entry$==1)");
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

Rene