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)
- Options->Show Fit Parameters for multiple fits
- https://iktp.tu-dresden.de/~nbarros/doc/root/TMultiGraph.html
- https://root.cern.ch/root/roottalk/roottalk02/2843.html\
- https://root.cern.ch/root/roottalk/roottalk04/3011.html
- https://root.cern.ch/doc/master/classTGraph.html#aa978c8ee0162e661eae795f6f3a35589
- One TGraph with two fits and two stat boxes
- Two plots on same canvas - cannot move stats box
- How to move statistics box when using Draw("sames"
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.