AddText in TPaveStats of histogram

Hello,

I couldn’t find an answer to the problem of Franca which was posted some time ago (I added the thread at the end).

Is there a way now to add some custom text lines to a TPaveStats of an histogram?

Thanks for the answer!
Cheers,
Gerolf

Re: [ROOT] problem with TPaveStats::AddText in ROOT 3.04.02
From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Jun 12 2003 - 22:09:32 MEST

* Next message: Rene Brun: "Re: [ROOT] Retrieve the formula expression from a TFormula"
* Previous message: Rene Brun: "Re: [ROOT] Embedding root"
* In reply to: Franca Cassol-Brunner: "[ROOT] problem with TPaveStats::AddText in ROOT 3.04.02"
* Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] 

Hi Franca,

What you do is in principle correct. However, the function
painting the stats box clears the stats box (including your added text)
before drawing it again with the latest histogram parameters.
I have to find a way to avoid this undesirable effect.

Rene Brun

On
Thu, 12 Jun
2003, Franca Cassol-Brunner wrote:

Hello,

I just moved to root 3.04.02 from root 3.02.07 and my
scripts don’t do anymore what they are supposed to do:

I want to add some lines to the TPaveStats of my histograms.
I read that now the stat belong to the histo and I do:


myHisto->Draw();

TPaveStats st =
(TPaveStats
)histo1->GetListOfFunctions()->FindObject(“stats”);

st->AddText(“test”);

st->Print(); // print to be sure to have added the new line

st->SetX1NDC(0.3); // move the stat to be sure to re-draw it

st->SetX2NDC(0.6);

st->Draw();


The Print() functions shows that the new lines are in the stats,
but the when the stats is drawn, the new line are not drawn…

Even if I use the same procedure used for release 3.02.07 ( re-name the
stat and use TPaveStats* st= (TPaveStats*)gPad->GetPrimitive(“stats”) )
the new lines are not more drawn… What should I add miss
with the new release?

thanks

Franca

You can do the following

TPaveStats *st = (TPaveStats*)histo1->GetListOfFunctions()->FindObject("stats"); histo1->SetBit(TH1::kNoStats); st->AddText("My new line1"); st->AddText("My new line2");
Rene

Dear Rene,

unfortunately I still have this problem,
I’m using TGraph and not TH1F, and solution you proposed does not work for me
For the timebeing I just added another TPaveText object with my additional text,
but would be nice to have it in the statistics box

Thanks a lot in advance!
Slava

Here the macro showing how to remove a line and add one in the the stat box.

void mystats() {
   TCanvas *c1 = new TCanvas;
   TH1F *h = new TH1F("h","test",100,-3,3);
   h->FillRandom("gaus",3000);
   gStyle->SetOptStat();
   h->Draw();
   c1->Update();
   TPaveStats *ps = (TPaveStats*)c1->GetPrimitive("stats");
   ps->SetName("mystats");
   TList *list = ps->GetListOfLines();
   TText *tconst = ps->GetLineWith("RMS");
   list->Remove(tconst);
   TLatex *myt = new TLatex(0,0,"test = 10");
   list->Add(myt);
   h->SetStats(0);
   c1->Modified();
}    

Hello.

In my case, I’d like to add a text line in the stats box as well, but I’m working with a THStack. I display two stats boxes as indicated here in the forth entry (I’m dealing with two histograms):

Then, I tried (h1_firCorrectionf1, h1_firCorrectionf2 are the histograms; st1, st2 are the TPaveStats intances from them):

h1_firCorrectionf1->SetBit(TH1::kNoStats);
st1->AddText("Xpeakf1   "+xf1);
st1->Draw();
gPad->Modified();

but the new line is not drawn in the canvas. It’s added as it can be seen with st1->Print();

TPaveStats  X1=1665.800065 Y1=516.185451 X2=2302.800084 Y2=638.540518 FillStyle=1001
Collection name='TList', class='TList', size=5
 Text  X=0.000000 Y=0.000000 Text=h1_firCorrection_0f1 Font=0 Size=0.036800 Color=0 Align=0
 Text  X=0.000000 Y=0.000000 Text=Entries = 2736    Font=0 Size=0.000000 Color=0 Align=0
 Text  X=0.000000 Y=0.000000 Text=Mean  =  6.713 Font=0 Size=0.000000 Color=0 Align=0
 Text  X=0.000000 Y=0.000000 Text=RMS   =  208.1 Font=0 Size=0.000000 Color=0 Align=0
 Text  X=0.000000 Y=0.000000 Text=Xpeakf1	-53 Font=0 Size=0.000000 Color=0 Align=0

I tried as well the following without success:

        TList* listf1 = st1->GetListOfLines();
        TList* listf2 = st2->GetListOfLines();
        TLatex* textf1 = new TLatex(0,0,"Xpeakf1        "+xf1);
        TLatex* textf2 = new TLatex(0,0,"Xpeakf2        "+xf2);
        listf1->Add(textf1);
        listf2->Add(textf2);
        st1->Print();   
        st2->Print(); 
        h1_firCorrectionf1->SetStats(0);
        h1_firCorrectionf2->SetStats(0);
        st1->Draw();
        st2->Draw();
        gPad->Modified();

and even though it’s not drawn, the print() shows that the line has been added as well.

Any suggestions?

Thank you!
Daniel

Can you provide a small running script reproducing the problem ?

Dear Couet.

Thanks for your interest!

Please find attached the requested working script.

Cheers&Thanks in advance!
Daniel
addTextTHStack.C (17.8 KB)

You example does not use GetPrimitive as the example I posted earlier … (scroll back)… get this post as example et rewrite your macro the same way.