Accessing stats-box from fit: Illegal pointer error


ROOT Version: 5.34/30
Platform: linuxx8664gcc
Compiler: unknown


Hello Rooters,

I am writing a macro in which I create a canvas, add a TMultiGraph to it, and import two TGraphErrors from two different files, to display both on the TMultiGraph. I then fit each Graph and want to display both fit-curves on the TMultiGraph, as long as both stats-boxes for the fits next to each other.

What doesn’t work is accessing the instances of the stats-boxes in order to place them on the canvas or to modify their properties (like the text color).

The pseudo-code for a single plot would look like this:
[[

TF1* FivBendingFunc = new TF1("FivBendingFunc","[0]+[1]*x^4", 1., 0.);
TCanvas* canvas= new TCanvas( "my fancy canvas", "the fancy canvas" );
TMultiGraph* graph= new TMultiGraph();

TFile* filefiv=new TFile( "/path/", "READ" );

TGraphErrors* fivgraph=(TGraphErrors*)filefiv->Get("ImportedGraphName");
filefiv->Close();
fivgraph->SetLineColor(kGreen);

fivgraph->Fit("FivBendingFunc");
FivBendingFunc->SetLineColor(kGreen+2);
graph->Add(fivgraph);

graph->Draw("AP");    

canvas->Update();
TPaveText* p5 = (TPaveText*)fivgraph->FindObject("stats");
// Also tried:
// TPaveStats *p5 = (TPaveStats*)fivgraph->GetListOfFunctions()->FindObject("stats");
// TPaveStats *p5 = (TPaveStats*)(fivgraph->FindObject("stats"));
p5->SetTextColor(kGreen+2); // THIS IS LINE #42 !

]]

Which results in the error message right after the console-output of the fit:
Error: illegal pointer to class object p5 0x0 833 MacroName.C:42:
Or in a segmentation fault without the error, depending on the chosen possibility and pointer combination (. or ->).

The code for the second plot is analogous and was omitted here, as it behaves identically to the above lines (it consists in replacing “fiv” by “six”).

I was not able to spot relevant differences to the examples I could find during my research for this problem, like in the links below: (note: I tried various possibilities and only left a few in the pseudo-code)

Hence, I am not sure if I am messing up basic object-declaration and pointer-handling, what I may have missed, or if this could simply be caused by version/library problems.

Adding the line if( p5 == NULL ){ cout << "is null" << endl; } before line 42 returned me is null in the console. May it be that I am not even creating the stats-object like I believe I do?

Thank you for your help.
Note: I am a student and beginner at root, and primarily learned how to write simple plot-macros required for the work I am doing here, so I may be overlooking basic rules, though I hope not.

The problem is if you fit a TGraph with default settings no stats box is created and therefore it cannot be accessed. After you call gStyle->SetOptFit(x); with a non-zero value for x, the stats box is created and can be accessed with (TPaveText*)fivgraph->FindObject("stats")
Further information on the SetOptFit function can be found here: ROOT: TStyle Class Reference

Maybe this topic is also helpful it is a somehow related topic:

Hello Triple_S!

Thank you very very much for your hint and your time, I indeed did not set the fit-options. With the corrected lines, the pseudo-code now looks like this:

gStyle->SetOptFit(111); // ! Line Modified!

TF1* FivBendingFunc = new TF1("FivBendingFunc","[0]+[1]*x^4", 1., 0.);
TCanvas* canvas= new TCanvas( "my fancy canvas", "the fancy canvas" );
TMultiGraph* graph= new TMultiGraph();

TFile* filefiv=new TFile( "/path/", "READ" );

TGraphErrors* fivgraph=(TGraphErrors*)filefiv->Get("ImportedGraphName");
filefiv->Close();
fivgraph->SetLineColor(kGreen);

fivgraph->Fit("FivBendingFunc");
FivBendingFunc->SetLineColor(kGreen+2);
graph->Add(fivgraph);

graph->Draw("AP");    

canvas->Update();
TPaveText *p5 = (TPaveText*)fivgraph->FindObject("stats"); // ! Line Modified!
p5->SetTextColor(kGreen+2);

And it works! My error feels kind of obvious, and I was searching way off the mark. But at least we gathered a nice collection of links in this thread.

I will mark the thread as closed tomorrow, thank you again!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.