From $ROOTSYS/tutorials/hist/statsEditing.C

TCanvas *statsEditing() {
// This example shows:
//    - how to remove a stat element from the stat box
//    - how to add a new one
//
//  Author: Olivier Couet

   // Create and plot a test histogram with stats
   TCanvas *se = new TCanvas;
   TH1F *h = new TH1F("h","test",100,-3,3);
   h->FillRandom("gaus",3000);
   gStyle->SetOptStat();
   h->Draw();
   se->Update();

   // Retrieve the stat box
   TPaveStats *ps = (TPaveStats*)se->GetPrimitive("stats");
   ps->SetName("mystats");
   TList *list = ps->GetListOfLines();

   // Remove the RMS line
   TText *tconst = ps->GetLineWith("RMS");
   list->Remove(tconst);

   // 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);
   list->Add(myt);

   // the following line is needed to avoid that the automatic redrawing of stats
   h->SetStats(0);

   se->Modified();
   return se;
}
 statsEditing.C:1
 statsEditing.C:2
 statsEditing.C:3
 statsEditing.C:4
 statsEditing.C:5
 statsEditing.C:6
 statsEditing.C:7
 statsEditing.C:8
 statsEditing.C:9
 statsEditing.C:10
 statsEditing.C:11
 statsEditing.C:12
 statsEditing.C:13
 statsEditing.C:14
 statsEditing.C:15
 statsEditing.C:16
 statsEditing.C:17
 statsEditing.C:18
 statsEditing.C:19
 statsEditing.C:20
 statsEditing.C:21
 statsEditing.C:22
 statsEditing.C:23
 statsEditing.C:24
 statsEditing.C:25
 statsEditing.C:26
 statsEditing.C:27
 statsEditing.C:28
 statsEditing.C:29
 statsEditing.C:30
 statsEditing.C:31
 statsEditing.C:32
 statsEditing.C:33
 statsEditing.C:34
 statsEditing.C:35
 statsEditing.C:36
 statsEditing.C:37
 statsEditing.C:38
 statsEditing.C:39