Saving of histo in root tree

Hi,

I am running a macro which save several histogram in a root file. But I am not able to save one particular histogram in the root file. I define the histo as:

TH1F *hbfinal_pt1_[BkgType]; //array of bkg histogram pointers with all pt-bins

for(Int_t ibkg=0;ibkg < BkgType;ibkg++){

   char Bfinal_pt1[30];
   sprintf(Bfinal_pt1,"%s%d%s","hbfinal_pt1_",ibkg,"\0");
   TString Bfinal_Pt1(Bfinal_pt1);

hbfinal_pt1_[ibkg]=(TH1F*)new TH1F(Bfinal_Pt1,Bfinal_Pt1,nbin,xlo,xhi);

and then I fill these histos with some value(by adding other histos to it with same bins) and then do:

for(Int_t i=0;i<4; i++){
hbfinal_pt1_[i]->Draw();
hbfinal_pt1_[i]->Write();
}


 I can see that the histogram has been plotted correctly but i do not get it inside the root file. However i can save the same type of other histos! Is something wrong with the above piece of code?

While running the macro i only get “potenetial memory leak” warnings.

Could you send the shortest running script reproducing your problem?

Rene

Hi,
I just solve the problem. I made a short script similar and tried to reproduced the problem . But this time it works.

In the original script I noticed if i define the mentioned piece of code other than the filling/adding part before the following lines,


   TFile *fs[maxNoSignalFile];

     for(Int_t is=0;is<NoSignalFile;is++)   //Run over the different pt-bins files of the signal
      {
          fs[is] = (TFile*)gROOT->GetListOfFiles()->FindObject(sigFileName[is]);
          if (!fs[is])
            {
             fs[is] = new TFile(sigFileName[is]);
            }


      TTree *tree = (TTree*)fs[is]->Get(inputTreeName);
      :
      :
      :

where I am reading a tree from a root files, it works(I do no know the reason whay??)
But if i put it after ther reading of tree then it does not write the histo to output root file but fills and draw them correctly.

with best,
sushil

[quote] But if i put it after ther reading of tree then it does not write the histo to output root file but fills and draw them correctly. [/quote]This is the expected behavior. By default the histogram attach themselves to the current ROOT directory (and file). Opening a ROOT file makes it the current directory. To solve the problem do either:outputFile->cd(); myhisto = new TH1F(....);ormyhisto = new TH1F(....); myhisto->SetDirectory(outputFile);

Cheers,
Philippe.

PS. For more detail see the User’s Guide.