I am trying to figure out how to move the stat box for for a fit to a TMultiGraph plot. The following is my attempt:
gStyle->SetOptFit(1);
TMultiGraph* multiGraph5 = new TMultiGraph();
multiGraph5->SetTitle("p2 of Double Gaussian Fit for n_{sigma,TOF}(e); Avg. Eta Bin; p4 of Double Gaussian Fit");
multiGraph5->SetMinimum(0.8); // Set the minimum y axis value
multiGraph5->SetMaximum(1.); // Set the maximum y axis value
// :
// Populated a few TGraphError objects with the needed data
// :
// Add all TGraphError to multiGraph
multiGraph5->Add(p4Graph1);
multiGraph5->Add(p4Graph2);
multiGraph5->Add(p4Graph3);
multiGraph5->Add(p4Graph4);
multiGraph5->Add(p4Graph5);
multiGraph5->Add(p4Graph6);
// Do the fitting
TF1* poly0 = new TF1("poly0", "pol0", -0.88, 1.2);
multiGraph5->Fit("poly0", "R");
multiGraph5->Draw("AP");
c1->Update();
TPaveText* st = (TPaveText*)multiGraph5->GetListOfFunctions()->FindObject("stats");
st->SetX1NDC(.15);
st->SetX2NDC(.5);
c1->Modified();
But that gives me the following error:
Error: illegal pointer to class object st 0x0 1021 plot_read2EePairTree.C:1043:
*** Interpreter error recovered ***
My final goal is to be able to have two fits to the data on the same plot. But, to do that, I would need to be able to move the stat boxes because they would be overlayed on top of each other. So far, I have not been able to reposition a stat box for a single fit. Any help would be greatly appreciated!
Thank you very much for the reply @dastudillo! I tried your suggestion, and it returned “stats not found!” So, somehow, it is not finding the stat box object. Any thoughts on why that might be?
Thanks for the reply! I tried the your suggestion in the following, but it still returned a null pointer for st:
gStyle->SetOptFit(1);
TMultiGraph* multiGraph5 = new TMultiGraph();
multiGraph5->SetTitle("p2 of Double Gaussian Fit for n_{sigma,TOF}(e); Avg. Eta Bin; p4 of Double Gaussian Fit");
multiGraph5->SetMinimum(0.8); // Set the minimum y axis value
multiGraph5->SetMaximum(1.); // Set the maximum y axis value
// :
// Populated a few TGraphError objects with the needed data
// :
// Add all TGraphError to multiGraph
multiGraph5->Add(p4Graph1);
multiGraph5->Add(p4Graph2);
multiGraph5->Add(p4Graph3);
multiGraph5->Add(p4Graph4);
multiGraph5->Add(p4Graph5);
multiGraph5->Add(p4Graph6);
// Do the fitting
TF1* poly0 = new TF1("poly0", "pol0", -0.88, 1.2);
multiGraph5->Fit("poly0", "R");
multiGraph5->Draw("AP");
multiGraph5->GetYaxis()->SetTitleOffset(1.3);
multiGraph5->GetYaxis()->CenterTitle(true);
gPad->Modified();
gPad->Update();
TPaveStats *st = (TPaveStats*)gPad->GetPrimitive("stats");
if (!st) { cout << "stats not found!\n"; return; }
That’s interesting! Attached is the outcome of my code. Is there a way to access the stat box seen in the plot to change some of its settings? Let’s say, the color of the text.
// Do the fitting
TF1* poly0 = new TF1("poly0", "pol0", -0.88, 1.2);
multiGraph5->Fit("poly0", "R");
multiGraph5->Draw("AP");
multiGraph5->GetYaxis()->SetTitleOffset(1.3);
multiGraph5->GetYaxis()->CenterTitle(true);
and these
gPad->Modified();
gPad->Update();
(and gPad->ls())?
Maybe you are doing something else that you are not showing us and is affecting the outcome?
If you stop your code (add return;) right here
is the fit stats box shown in the canvas?
By the way, graph->Fit already draws the graph with the “AP” options, so you don’t need to do graph->Draw("AP") after that --unless, of course, you use other draw options.
@dastudillo Thank you very much for your reply! Yes, the portion of code that I show is only a small fraction of a much larger macro that produces 10’s of histograms. It could be very well that there are other parts of my code that have impact on what I am trying to accomplish in the portion I posted here.
I appreciate the help of everyone here! I decided to focus on other things since moving the stat box was not a top priority. Thank you very much for your help!