One TGraph with two fits and two stat boxes

Hi there,

I have a single TGraphErrors and want to fit two separate functions, each with its own Stat Box. All the tutorials I found used TMultiGraph, but I’d rather use a single graph object as it’s one set of data I’m using and some points are shared by both fits. Right now I iterate over my TF1 fit functions and change position and color of the stat box each time.

The peculiar result is that the stat box shown at the end is the one for the first fit, but with style and position of the one for the second fit.

A small example:

using namespace std;
vector<TF1*> v; // Vector for the fits
TF1 *fit1 = new TF1(“fit1”, “pol1”, 0, 20); v.push_back(fit1);
TF1 *fit2 = new TF1(“fit2”, “pol1”, 15, 30); v.push_back(fit2);

TCanvas mf;
TGraphErrors *g = new TGraphErrors(); // fill with some linear data
for(i=0; i<v.size(); i++) {
g->Fit(v[i], “R+”);
if (i==0) { g->Draw(“AP”); }
else { g->Draw(“sames”); }
mf.Modified(); mf.Update();
TPaveStats ps = (TPaveStats) g->GetListOfFunctions()->FindObject(“stats”);
if (i==0) {
ps->SetTextColor(kRed); // or change position
} else if (i==1) {
ps->SetTextColor(kBlue); // or change position
} else {
ps->SetTextColor(kGreen); // or change position
// Open ROOT file and save g
TFile f(“example.root”, “RECREATE”);

What I see in the root file on my end is the graph with both fits applied, a blue stat box but the data of the first fit. If I move the box around with my cursor, a second one does not appear underneath.

Am I doing something wrong or do I have to use a TMultiGraph?

Edit: Using ROOT 5.34/20

Try (see also [url= old thread[/url]): // ... if (i==0) { ps->SetName("stats_kRed"); ps->SetTextColor(kRed); // or change position } else if (i==1) { ps->SetName("stats_kBlue"); ps->SetTextColor(kBlue); // or change position } else { ps->SetName("stats_kGreen"); ps->SetTextColor(kGreen); // or change position } // ...