Histogram declare position matters or namespace matters

Hi, Root Experts

I use tree->Draw(“Ne>>h”) to create a histogram. the tree was read from a file and the histogram h is declared as required.

The thing is when I declare the h after the file was opened, all is well, however, if I declare the h before the file was opened, the h created in “tree->Draw(“Ne>>h”)” code will not be the h I declared.

I was confused about that, is the declare position matters or namespace(e.g. gDirectory) matters?

Thanks

jsgou

// works well
// ********************************
TFile *tf = TFile::Open(fname);
// The predefined TH1D has to be put here
// after the file was opend
TH1D * h = new TH1D(“h”,“htemp”,bins,xmin,xmax);

TTree* nt1 = (TTree*)tf->Get(“nt1”);

nt1->Draw(“Ne>>h”);

h->DrawCopy();

tf->Close();

//***************************

// two “h” is different
// ********************************
TH1D * h = new TH1D(“h”,“htemp”,bins,xmin,xmax);
TFile *tf = TFile::Open(fname);

TTree* nt1 = (TTree*)tf->Get(“nt1”);
nt1->Draw(“Ne>>h”); // this h is not the “h” declared at the very beginning

h->DrawCopy();
tf->Close();

//***************************


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


May be @pcanal can explain.