Can't access the "status" box of TGraph

Hi,

It’s weird that I can’t access the “status” box of a TGraph.
Though an almost same line is shown in this link : root.cern.ch/root/roottalk/roottalk02/2843.html.

My code is like this :

TCanvas, c1, definition;
TGraph, gr, definition ; 
A linear fit to the data of TGraph;
gPad->Modified();
gPad->Updated();
TPaveStats *Lccd_st = (TPaveStats*)gr->GetListOfFunctions()->FindObject("stats");

The error message is
" Error: illegal pointer to class object Lccd_st 0x0 3161 "

Could any people have a comment please ?

Thanks !

Best,
Junhui

I’m afraid we need a simple macro which reproduces your problem (the “error message” that you show does not come from the “code” that you show).
BTW. I assume that after you execute “gPad->Update();”, you do see the “statistics box” displayed. If not, make sure that you execute “gStyle->SetOptFit(1);” before you try to fit your graph.

Hi, Wile,

You got it !
After being added the line of “gStyle->SetOptFit(1)”, it worked well.

However, another problem arises, :frowning: .
I’d like to add some fit results to the statistical box.
The problem is, I can’t put it into the box although I can print the results on my screen.
At the same time, there is no any error displayed either when running it.
It just can’t display the stuffs.

The code as following is coped from a TH1 version which works perfectly.
The only line I’ve commented is " ///gr->GetListOfFunctions()->SetStats(0);"
I commented because if I keep it, my script can’t pass compiling.

Do you have any comments ?
Thanks !

Best,
Junhui

              gStyle->SetOptFit(1);
              TF1 *pol1_LCCD = new TF1("pol1","pol1",0,maxColumn);
              gr->Fit(pol1_LCCD,"R");
              gPad->Modified();
              gPad->Update(); // make sure it's (re)drawn

              TPaveStats *Lccd_st = (TPaveStats*)(gr->GetListOfFunctions()->FindObject("stats"));
              Lccd_st->SetOptFit(1);
              gPad->Modified();
              gPad->Update(); // make sure it's (re)drawn
              Lccd_st->SetX1NDC(0.7);
              Lccd_st->SetX2NDC(0.99);
              Lccd_st->SetY1NDC(0.2);
              Lccd_st->SetY2NDC(0.5);

              Lccd_st->SetName("mystatus");
              ///gr->GetListOfFunctions()->SetStats(0);

              TList *list = Lccd_st->GetListOfLines();
              TLatex *myt = new TLatex(0,0,TString::Format("CTI = %g", pol1_LCCD->GetParameter("p1") / pol1_LCCD->GetParameter("p0")));
              cout << endl << "CTI =" << pol1_LCCD->GetParameter("p1") / pol1_LCCD->GetParameter("p0") << endl;
              myt->SetTextFont(42);
              myt->SetTextSize(0.04);
              myt->SetTextColor(kRed);
              list->Add(myt);

              gPad->Modified();
              gPad->Update(); // make sure it's (re)drawn

Hi, Wile,

Also, I realized actually in TGraph, one can’t displaying similarly as in a histogram by calling “gStyle->SetOptStat(000001111);”(In my script, this line is right after the line of “gStyle->SetOptFit(1);” ) .
There is no error too.

TGraph itself has no classes of “SetOptStat()”, root.cern.ch/root/html/TGraph.html .
But gStyle has, root.cern.ch/root/html/TStyle.html, Why can’t work neither ?

Thanks !

Best,
Junhui

Try:

// ...
Lccd_st->SetName("mystatus"); // actually, you do not need it
gr->GetListOfFunctions()->Remove(Lccd_st); Lccd_st->Draw();
//...

P.S. Note that the TStyle::SetOptStat method is relevant for histograms only (see also the TH1::SetStats method).


Hi, Wile,

Thanks, you got it again !

However, there is another small problem : two statistical boxes appeared.
As the attached plot.

I figured out a “dirty” way to get rid of the upper one which is commenting the four lines of Lccd_st->SetNDC to overlap two boxes.
Do you have an alternative way to kill the upper statistical box ?

Thanks !

Best,
Junhui

Try (I’m not sure if this is the right way, though):

gr->GetListOfFunctions()->Remove(Lccd_st); gStyle->SetOptFit(0); Lccd_st->Draw();
1 Like

Hi, Wile,

You made it !
Thanks !

Best,
Junhui