TH2 Custom Stats?


I’m currently asked to plot a 3D, x and y are simple co-ordinates and z is intensity. I decided to use TH2D for this, and it currently plots co-ordinates, then using “COLZ” draw option, it colours the coordinates by %(value/avg_value)

What I also wish to show is the statistics of my data.
Such as, Average calculated intensity(not in percent), Max,Min and number of valid data. Some of the data get selectively discarded, so what gets plotted on the TH2D isn’t necessarily want to show.

I’m familiar with TLegends, but, is there a similar way to input a custom statistics?

Thank you.


Error: illegal pointer to class object ps 0x0 1804


seems not to work from my end.

No error for me:

$ root
  | Welcome to ROOT 6.07/07           |
  |                                   (c) 1995-2016, The ROOT Team |
  | Built for macosx64                                             |
  | From heads/master@v6-07-06-349-g4472cf0, May 18 2016, 10:22:12 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'     |

root [0] .x statsEditing.C
(TCanvas *) 0x7f829d1db110
root [1] 

Which ROOT version are you using ? on which machine ?


did you really read Oliviers example carefully,
i.e. did you Draw your histo with stats and Update the canvas?

   gStyle->SetOptStat();  //<<<<<<<<<<<<<<
   h->Draw();    //<<<<<<<<<<<<<<<<
   se->Update();  //<<<<<<<<<<<<<<<<<<
   // Retrieve the stat box
   TPaveStats *ps = (TPaveStats*)se->GetPrimitive("stats");


Hi Again,

By the way, the URL I gave you is pointing to the ROOT reference guide which is rebuild every night. To generate the pictures you see in the tutorials documentation web pages, the macros are actually run…So, if you see the picture and the code example it means the example was working in the last night rebuild. So if iy take the exact code written on that page it should run.


By the way it works also with TH2:

root [0] hpxpy->Draw("colz")
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [1] TPaveStats *ps = (TPaveStats*)c1->GetPrimitive("stats");
root [2] ps
(TPaveStats *) 0x7fa16f005900
root [3]  ps->SetName("mystats");
root [4] 

Note “h->SetStats(kFALSE); // mandatory (not sure why)”: [code]{
gStyle->SetOptStat(); // mandatory
TCanvas *c = new TCanvas(“c”, “c”, 600, 400);

TH2F h = new TH2F(“h”, "TH2F example ", 30, -4, 4, 30, -20, 20);
Float_t px, py;
for (Int_t i = 0; i < 25000; i++) {
gRandom->Rannor(px, py);
h->Fill(px-1, 5
h->Fill(2+0.5px, 2py-10., 0.1);

gPad->Modified(); gPad->Update(); // mandatory
TPaveStats ps = (TPaveStats)gPad->GetPrimitive(“stats”);
if (ps) {
ps->SetName(“mystats”); // mandatory
h->SetStats(kFALSE); // mandatory (not sure why)
// gPad->Modified(); gPad->Update();
TList *list = ps->GetListOfLines();

// Add a new line in the stat box.
// Note that "=" is a control character
TLatex *myt = new TLatex(0, 0, "Test = 10");
myt ->SetTextFont(42);
myt ->SetTextSize(0.04);
myt ->SetTextColor(kRed);

gPad->Modified(); gPad->Update(); // mandatory

} else { std::cout << “Error: ps = 0” << std::endl; }

return c;