Putting a stat box on a histogram read from a root file

Hi rooters,

I have some problems trying to put a stat box on an histogram read out from a root file. Here is what I do:

gStyle->SetOptStat(1111) ; TFile *f = new TFile("TemperatureB3.root"); DeltaT->Draw(); TPaveStats *stat = (TPaveStats*)DeltaT->GetListOfFunctions()->FindObject("stats"); stat->SetX1NDC(0.9); stat->SetY1NDC(0.9); stat->Draw("same");

This (I expected…) would retrieve the histogram and put back the stat box which was on it when it was originally created. Instead, I get:

Error: illegal pointer to class object stat 0x0 366

In the macro where the plot was first created, there was a stat box on it (actually a fit parameter box). Why can’t it find it back?



I tried to reproduce your problem with the following macro:

   gStyle->SetOptStat(111111) ;
   TFile f("hsimple.root");
   TCanvas *c1 = new TCanvas("c1","performance",10,10,800,600) ;
   hpx->Draw() ;
   TPaveStats *st = (TPaveStats*)hpx->GetListOfFunctions()->FindObject("stats") ;
   st->SetY1NDC(0.85) ;

For me it works , “st” is defined. hsimple.root is the file generated by $ROOTSYS/tutorials/hsimple.C .
What is your histogram DeltaT ?
has it something special ?

The “stats” box is only generated when the canvas is effectively paint.
In your script, you should add the following line after your hpx->Draw call.


oops … yes of course… I did try in interactive mode… outside a script (I made the script afterwards)

I tried adding the c1->Update(); to my macro but I still get the same result.

The plot DeltaT is filled and then drawn using the following code:

TH1F *B3DeltaTCorr = new TH1F("DeltaT","Temperature variation distribution",100,-5,5);

The … just means that there is other code lines creating different plots in between. Is there anything wrong with this in the first place?


I notice that in your hsimple.C file, you are using hfile->Write(); instead of hpx->Write(); . What is the difference? In my macro, I am opening the root file, then writing all histos using histoname->Write(); then I close the file using f->Close(); . Appart from this, I don’t see any differences between your macro and mine that could generate the problem. Am I missing something here?

Thanks for your help,