Filling histograms with TTree::Draw in macro fails

hi all,

I have problems filling histograms with a TTree.
I define two histograms and a canvas:

TH2F histo2("X2_vs_X1","X2_vs_X1",100,0,0.41,100,0,1.1); TH2F histo1("X_Y","X_Y",100,0.5,2.3,100,-1,1); TCanvas *canvas=new TCanvas(); canvas->Divide(2,1);
then I load a named macro and execute it:

.L myMacro.C tryFile("myfile.root", &histo1, &histo2, "");

the output is:

Y1:X1>>X_Y Y2:X2>>+X_Y X2:X1>>X2_vs_X1 draw in "X_Y" Entries: 67774, 0 draw in "X2_vs_X1" Entries: 33887, 0

the file myMacro.C looks the following:

void tryFile(const char filename, TH2 myhisto1, TH2* myhisto2, TCut mycut)
{
TFile f(filename);
TTree* tree=(TTree*)f.Get(“myOwnTree”);
if(tree==NULL)return;
const char* draw1=TString(“Y1:X1>>”).Append(myhisto1->GetName()).Data();
const char* draw2=TString(“Y2:X2>>+”).Append(myhisto1->GetName()).Data();
const char* draw3=TString(“X1:X2>>”).Append(myhisto2->GetName()).Data();
float f1,f2,f3;
cout<<draw1<<endl;
cout<<draw2<<endl;
cout<<draw3<<endl;
canvas->cd(1);
cout<<“draw in “”<GetName()<<”" Entries: “;
f1=tree->Draw(draw1,mycut);
f2=tree->Draw(draw2,mycut);
cout<<f1+f2<<”, “<GetEntries()<<endl;;
myhisto1->Draw(“contz”);
canvas->cd(2);
cout<<“draw in “”<GetName()<<”” Entries: “;
f3=tree->Draw(draw3,mycut);
cout<<f3<<”, "<GetEntries()<<endl;;
myhisto2->Draw(“contz”);
f.Close();
}

why doesn’t any entry of the drawing of the TTree appear in the histogram? where is my fault?

[quote]why doesn’t any entry of the drawing of the TTree appear in the histogram? where is my fault?[/quote]When TTree::Draw looks up the histrogram by name, it looks in the current ROOT directory which in your case is the TFile.

So I think you can solve your problem by adding:myhisto1->GetDirectory()->cd();after opening the file.

Cheers,
Philippe

Thanks.