Delete and write text in stat box TPaveStats

Hi Rooters,

I would like to write what I want in the stat box of an histogram.
For example:

#include <stdio.h>
#include <stdlib.h>
#include
#include
#include

void main(void)
{
TH1F *h=new TH1F(“mult”,"",10,0,10);
h->Fill(5);
h->Draw();
TPaveStats s = (TPaveStats)h->GetListOfFunctions()->FindObject(“stats”);
s->DeleteText();
}

This doesn’t work.
What need I to do to delete first what is already written (Mean, RMS , Entries), and write the text I want ( For example the maximum…)?

Thank you.

example:

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();
}    

Thanks.

In fact, I try to write different things using 2 functions, but in the same TPaveStats.

For example:

void mystats() {
TCanvas *c1 = new TCanvas;
c1->Divide(1,2);
c1->cd(2);
TH1F *h = new TH1F(“h”,“test”,100,-3,3);
h->FillRandom(“gaus”,3000);
gStyle->SetOptStat();
h->Draw();
c1->Update();
TPaveStats ps = (TPaveStats)c1->cd(2)->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->cd(2)->Modified();
}

void mystats2() {
TH1F *h2 = new TH1F(“h2”,“test2”,100,-3,3);
h2->FillRandom(“gaus”,30);
h2->Draw(“same”);
c1->Update();
TPaveStats ps2 = (TPaveStats)c1->cd(2)->GetPrimitive(“stats”);
TList *list2 = ps->GetListOfLines();
TLatex *myt2 = new TLatex(0,0,“test2 = 20”);
list2->Add(myt2);
h2->SetStats(0);
c1->cd(2)->Modified();
}

This doesn’t work.
I would like “test=10” and “test2=20” on the same TPaveStats.
What can I do?

Thanks

I am not sure to understand what you mean. What do you do with these two functions ? you execute one and then the other ?

Yes that’s it. First I plot graph h and I display its stats, and then I plot another plot h2 on the same canvas part and I display its stats in the same TPaveStats. That’s what I want. But 2 functions are needed. My program is more complicated, I simplified to make easy to understand.

Thanks

void stats2() {
   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->cd(2)->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();       

   TH1F *h2 = new TH1F("h2","test2",100,-3,3);
   h2->FillRandom("gaus",30);
   h2->Draw("same");
   TLatex *myt2 = new TLatex(0,0,"test2 = 20");
   list->Add(myt2);
}

No, sorry, but I need two separated functions. It would be easy in the other case. :laughing:
Have you got an idea?

Thanks

Yes I know bur may example make it clear that the second function should add the new item in the same TList as the 1st one. In you example you create a new Tlist called Tlist2 … It is normal is does not show… The piece of code :

TPaveStats *ps = (TPaveStats*)c1->cd(2)->GetPrimitive("stats");
ps->SetName("mystats");
TList *list = ps->GetListOfLines(); 

Should be unique.

Yes , ok for it. But what must I add or delete in the second function to write new text in the TPaveStats in the canvas which already exists?

problem solved; thanks for your help