Editing statsbox in TGraphErrors

Hello everyone,

So I did a linear fit using TGraphErrors, all good, just wanted to access the statsbox to add the correlation factor.

I´ve been following: Can't access the "status" box of TGraph - #5 by Wile_E_Coyote

But doesn´t want to work for me, here´s what´s in my .C:

    TPaveStats *pvs = (TPaveStats*)(graphC->GetListOfFunctions()->FindObject("stats"));
	pvs->SetOptFit(1);
	gPad->Modified();
	gPad->Update();
	pvs->Draw();
	TList *list = pvs->GetListOfLines();
	TLatex *myt = new TLatex(0,0,TString::Format("R^{2} = %g", graphC->GetCorrelationFactor()));
	list->Add(myt);
	pvs->Draw();
 
	cC->Modified();
	cC->Update();

The error it gives is the following:

warning: null passed to a callee that requires a non-null argument [-Wnonnull]
        pvs->SetOptFit(1);

Thanks in advance for helping.


ROOT Version: 6.22/08
Platform: Arch Linux x84_64
Compiler: g++

A working example is given here.

gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn
TPaveStats *pvs = (TPaveStats*)(...->FindObject("stats")); // retrieve "stats"

I did follow that example first, but apparently that doesn’t want to work for my TGraph and I couldn’t relate since the example shows a histogram

How can I do that?

Can you post the code you have reproducing your problem ?

void setStyle() { //standard function which controls graphics 
	gROOT->SetStyle("Plain");
	gStyle->SetOptStat(111110);
	gStyle->SetOptFit(111);
	//gStyle->SetPalette(57);
	gStyle->SetOptTitle(1);
}

void grafico () {
	TGraphErrors * graphC = new TGraphErrors("T^2-vs-L", "%lg%lg"); //%lg for each column in the file
	
	graphC->SetTitle("Confronto T^{2} e L; L [m]; T^{2} [s^{2}]"); //graph title
	graphC->GetXaxis()->CenterTitle();
	graphC->GetXaxis()->SetTitleOffset(1.5);
	graphC->GetYaxis()->CenterTitle();
	graphC->GetYaxis()->SetTitleOffset(1.5);
	graphC->SetMarkerStyle(kFullCircle); //graph markers
	graphC->SetMarkerColor(kBlack); //markers color
	
	TF1 *f1 = new TF1("Linear law", "[0]*x+[1]",600,600); //fitting function y=Ax+B
	f1->SetLineColor(kRed); //fitting color
	f1->SetLineStyle(2); //fitting graphics
	f1->SetParNames("A", "B"); //name of the parameters in the legenda
 
	graphC->Fit(f1); //fit the graph which contains the data
		TCanvas *cC = new TCanvas(); //object of the type Canvas on which the graph will lay
		cC-> cd(); //select the canvas
		cC->SetGrid();
		cC->SetLeftMargin(0.15);
		cC->SetRightMargin(0.15);
		cC->SetTopMargin(0.15);
		cC->SetBottomMargin(0.15);
		graphC->Draw("APE"); //draws the graph ("APE" = Axis, Points, Errors) 
	
	TLegend *legC = new TLegend(.1,.7,.3,.9,"Legenda"); //legenda
	legC->SetFillColor(0);
	graphC->SetFillColor(0);
	legC->AddEntry(graphC, "punti sperimentali");
	legC->AddEntry(f1, "Fit");
	legC->Draw("Same");

	TPaveStats *pvs = (TPaveStats*)(graphC->GetListOfFunctions()->FindObject("stats"));
	pvs->SetOptFit(1);
	gPad->Modified();
	gPad->Update();
	pvs->Draw();
	
	TList *list = pvs->GetListOfLines();
	TLatex *myt = new TLatex(0,0,TString::Format("R^{2} = %g", graphC->GetCorrelationFactor()));
	list->Add(myt);
	pvs->Draw();
 
	cC->Modified();
	cC->Update();
	//cC->Print("");
	
	cout << "x and y measurements correlation =" << graphC->GetCorrelationFactor()<<endl; //shows on the terminal the correlation factor
}

The data file is missing:

Processing grafico.C...
Error in <TGraphErrors::TGraphErrors>: Cannot open file: T^2-vs-L, TGraphErrors is Zombie

Yeah my bad. I didn´t pass over the file containing the data.

So the file name must be: T^2-vs-L
Containing:

L [m]	T^2 [s^2]
0.80	3.28
0.90	3.72
1.00	4.10
1.10	4.51
1.20	4.87

// ...
gStyle->SetOptFit(1111);
graphC->Draw("APE"); //draws the graph ("APE" = Axis, Points, Errors) 
graphC->Fit(f1); //fit the graph which contains the data
gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn
TPaveStats *pvs = (TPaveStats*)(graphC->GetListOfFunctions()->FindObject("stats"));
// ...

Thanks, I made the changes and this time everything executed with no errors. But yet no added line appears in the statsbox…

Solved! I just had to be sure to include this in my code which is a crucial step.

graphC->GetListOfFunctions()->Remove(pvs); 
pvs->Draw();

Thank everyone for helping!