TPaveStats ignores SetOptStat()

I would like to set the global OptStat code to display all statistics information, but for specific histograms only display a limited amount of stats info. I thought that if one got the pointer to the stats box and used TPaveStats::SetOptStat() to set the code for that statistics box it would be reflected on the canvas. This does not seem to work as the following example displays the same stats information for both histograms.

void Test() 
{  
   gStyle->SetOptStat(111111111); 
    
   TCanvas *c1 = new TCanvas(); 
   c1->Divide(1,2); 
   c1->cd(1); 
 
   TH1F *hist1 = new TH1F("hist1","Hist1",100,-5,5); 
   hist1->FillRandom("gaus"); 
   hist1->Draw(); 
 
   c1->Update(); 
 
   c1->cd(2); 
 
   TH1F *hist2 = new TH1F("hist2","Hist2",100,-5,5); 
   hist2->FillRandom("gaus",100); 
   hist2->SetStats(kTRUE); 
   hist2->Draw(); 
 
   c1->Update(); 
 
   TPaveStats *stats = (TPaveStats*)hist2->GetListOfFunctions()->FindObject("stats"); 
 
   stats->SetOptStat(10); 
   
c1->Update(); 
}

Resulting in the following:


Also, is there a more simple method to get the Stats pointer? This method seems kind of convoluted. Perhaps we need a method TH1::GetStatsPointer()?

to set the stats on an individual histogram use:

h1->SetStats(..);

This only makes the stats box appear or not appear. It does not change the information inside the stats box. I want different information in the different stats boxes, but I want them all to appear.

Yes … sorry… here it is:

{
   gStyle->SetOptStat(111111111);

   TCanvas *c1 = new TCanvas();
   c1->Divide(1,2);
   c1->cd(1);

   TH1F *hist1 = new TH1F("hist1","Hist1",100,-5,5);
   hist1->FillRandom("gaus");
   hist1->Draw();

   c1->Update();

   c1->cd(2);

   TH1F *hist2 = new TH1F("hist2","Hist2",100,-5,5);
   hist2->FillRandom("gaus",100);
   hist2->SetStats(kTRUE);
   hist2->Draw();

   c1->Update();

   TPaveStats *stats = (TPaveStats*)hist2->GetListOfFunctions()->FindObject("stats");

   stats->SetOptStat(10);
   gPad->Modified();
   gPad->Update();
}

You are correct I did not mark the pad as modified. Any thoughts on a method to get the pointer to the stats box instead of searching through the list of primitives?

using GetListOfFunctions is the normal way.

I ask because I have in the past used the method TH1::GetFunction(char *name) which simply finds the corresponding function from the list of functions and returns the pointer. I thought there should be something similar for the statistics box.

Nothing equivalent for stats. GetStats does something else:
root.cern.ch/root/html/TH1.html#TH1:GetStats
You can always do your own function having a TH1* as input calling internally GetListOfFunctions

I understand that, I just don’t like the inconsistent treatment. I’ll just stick with GetListOfFunctions(). Thanks

You can have anything in the list of functions. Including unknown users objects so it would be difficult to have a specific getter for each of them. For instance the “palette” is also stored in the list of functions when an histogram is plotted using COLZ option.
GetFunction is a special case because it is very frequents. Basically it a shortcut to GetListOfFunctions

Sorry to dig this up again, but I’ve run into a problem where the original solution of updating the canvas then getting the stats box will not work. I’m looking to create histograms and set the default stats options for each histogram separately without drawing any of them. The end user will then decided when and where to draw each histogram. Since the stats box is not created until the histogram is drawn I’m stuck. Any ideas around this?

Thanks.

I do not see any obvious solution to your problem … Without drawing the only action you can do via the individual histogram pointer if to set the stat on or off via SetStats

Any plans to change this functionality in the future? It has always seemed odd to me that one had to draw the histogram before being able to adjust the statistics options.

Well, that’s a purely graphics mechanism. The statistics themselves are available in the histogram data structure independently … There is no real plan to change this for the time being.

Alright. Thanks for the quick reply.