Cannot print Stats correctly

I am having a multigraph, in which I am plotting 3 curves.
I want each plot to have an expo fit and each stat box to be printed in different color. The code I am using is

[code]# include “TCanvas.h”

include “TROOT.h”

include “TGraphErrors.h”

include “TStyle.h”

include “TMultiGraph.h”

include “TF1.h”

include “TLegend.h”

include “TPaveStats.h”

include “TArrow.h”

include “TLatex.h”

include “TPaveText.h”

include “TText.h”

include “TPavesText.h”

void gainCurves(){

gROOT->Reset();

TCanvas *mycanvas = new TCanvas("c","c",600, 400);
mycanvas->SetFillColor(kSpring-8);
mycanvas->SetFrameFillColor(10);
TMultiGraph * mg = new TMultiGraph("gain curves","GAIN CURVES");

// The values on the X,Y axes and error on Y axis

//Vdrift=700V
const int n_700=15;
double x_700[n_700]={500,505,510,515,520,525,530,535,540,545,550,555,560,565,570};
double y_700[n_700]={1409,1648.5,1918.5,2188,2578,2937.5,3417,3837,4796,5695.5,6924,8183,9712,11660.5,13728.5};
double errorY_700[n_700];
for (int i=0; i<n_700; i++){
	errorY_700[i]=300;
}

//Vdrift=750V

const int n_750=15;
double x_750[n_750]={500,505,510,515,520,525,530,535,540,545,550,555,560,565,570};
double y_750[n_750]={1349,1648.5,1918.5,2128,2518,3027.5,3567,4196.5,4916,5605.5,6864.5,8872.5,9622,11420.5,13608.5};
double errorY_750[n_750];
for (int i=0; i<n_750; i++){
	errorY_750[i]=300;
}

//Vdrift=850V

const int n_850=11;
double x_850[n_850]={500,510,520,530,540,550,555,560,565,570,575};
double y_850[n_850]={1259,1768.5,2398,3327,4616,6564.5,7733.5,9322,11061,13489,16366.5};
double errorY_850[n_850];
for (int i=0; i<n_850; i++){
	errorY_850[i]=300;
}

gROOT->SetStyle("Plain");
// See: http://root.cern.ch/root/html/TStyle.html#TStyle:SetOptFit
gStyle->SetOptFit(1111);

TGraphErrors * gr1 = new TGraphErrors( n_700, x_700, y_700, NULL, errorY_700);
gr1->SetName(“gr1”);
gr1->SetTitle(“V_{drift}=700VV”);
gr1->SetMarkerStyle(20);
gr1->SetMarkerColor(4);
gr1->SetDrawOption(“AP”);
gr1->SetLineColor(4);
gr1->SetLineWidth(1);
gr1->SetFillStyle(0);

TGraphErrors * gr2 = new TGraphErrors( n_750, x_750, y_750, NULL, errorY_750);
gr2->SetName(“gr2”);
gr2->SetTitle(“V_{drift}=750V”);
gr2->SetMarkerStyle(20);
gr2->SetMarkerColor(2);
gr2->SetDrawOption(“AP”);
gr2->SetLineColor(2);
gr2->SetLineWidth(1);
gr2->SetFillStyle(0);

TGraphErrors * gr3 = new TGraphErrors( n_850, x_850, y_850, NULL, errorY_850);
gr3->SetName(“gr3”);
gr3->SetTitle(“V_{drift}=850V”);
gr3->SetMarkerStyle(20);
gr3->SetMarkerColor(3);
gr3->SetDrawOption(“AP”);
gr3->SetLineColor(3);
gr3->SetLineWidth(1);
gr3->SetFillStyle(0);

gr1->Fit(“expo”);
gr1->GetFunction(“expo”)->SetLineColor(gr1->GetLineColor());
gr1->GetFunction(“expo”)->SetLineWidth(2);
//gr1->Print(“all”);
gr2->Fit(“expo”);
gr2->GetFunction(“expo”)->SetLineColor(gr2->GetLineColor());
gr2->GetFunction(“expo”)->SetLineWidth(2);

gr3->Fit(“expo”);
gr3->GetFunction(“expo”)->SetLineColor(gr3->GetLineColor());
gr3->GetFunction(“expo”)->SetLineWidth(2);

TPaveStats st = ((TPaveStats)(gr1->GetListOfFunctions()->FindObject(“stats”)));
if (st) {
st->SetTextColor(gr1->GetLineColor());
st->SetX1NDC(0.3); st->SetX2NDC(0.7);
st->SetY1NDC(0.3); st->SetY2NDC(0.7);
}
st = ((TPaveStats*)(gr2->GetListOfFunctions()->FindObject(“stats”)));
if (st) {
st->SetTextColor(gr2->GetLineColor());
st->SetX1NDC(0.64); st->SetX2NDC(0.99);
st->SetY1NDC(0.15); st->SetY2NDC(0.35);
}
st = ((TPaveStats*)(gr3->GetListOfFunctions()->FindObject(“stats”)));
if (st) {
st->SetTextColor(gr3->GetLineColor());
st->SetX1NDC(0.64); st->SetX2NDC(0.99);
st->SetY1NDC(0.45); st->SetY2NDC(0.55);
}

mg->Add(gr2);
mg->Add(gr1);
mg->Add(gr3);
mg->Draw(“AP”);
mg->SetTitle(“Gain Curves for NTUASmm1;V_{mesh}[V];Gain”);
mycanvas->BuildLegend(0.85, 0.8, 0.99, 0.99,“Ar-CO2–>93%-7%”);
mycanvas->SetLogy(1);

mycanvas->Modified();
mycanvas->Update(); // make sure it’s really (re)drawn

mycanvas->Print(“gainCurves.pdf”);

}
[/code]

This code was working with 2 curves. Do you think that it cannot work with 3(I don’t really think so…)?
How can this be fixed?

Thank you in advance! 8)

Carefully compare your “old” working case “with 2 curves” with your “new” non-working case “with 3”.
Pay SPECIAL attention to WHEN the (multi-)graph is physically drawn with respect to WHEN one tries to “find” and modify its statistics box.
See also: [url]How to move statistics box when using Draw(“sames”

Thank you for your answer!
You are right, I forgot to update my canvas…
But even if I do the issue remains.
I also tried to name stats box under a different name for each of them, but again this doesn’t do the job…
I compared with my previous code, but I cannot find any difference…
My “old” and “working” is:

[code]# include “TCanvas.h”

include “TROOT.h”

include “TGraphErrors.h”

include “TStyle.h”

include “TMultiGraph.h”

include “TF1.h”

include “TLegend.h”

include “TPaveStats.h”

include “TArrow.h”

include “TLatex.h”

include “TPaveText.h”

include “TText.h”

include “TPavesText.h”

void gain(){

gROOT->Reset();

TCanvas *mycanvas = new TCanvas("c","c",600, 400);
mycanvas->SetFillColor(5);
mycanvas->SetFrameFillColor(10);
TMultiGraph * mg = new TMultiGraph("gain curve","GAIN CURVE");

// The values on the X,Y axes and error on Y axis
const int n_Fe=20;
double x_Fe[n_Fe]={560,565,570,575,580,585,590,595,600,605,610,615,620,625,630,635,640,645,650,655};
double y_Fe[n_Fe]={1056,1200,1392,1584,1824,2064,2304,2688,3024,3456,3984,4512,5088,5856,6624,7488,8640,9792,11280,12624};
double errorY_Fe[n_Fe];
for (int i=0; i<n_Fe; i++){
	errorY_Fe[i]=192;
}


const int n_alpha=9;
double x_alpha[n_alpha]={500,535,540,545,550,555,560,565,570};
double y_alpha[n_alpha]={220,490,556,622,710,754,849,959,1098};
double errorY_alpha[n_alpha];
for (int i=0; i<n_alpha; i++){
	errorY_alpha[i]=30;
}

gROOT->SetStyle("Plain");
// See: http://root.cern.ch/root/html/TStyle.html#TStyle:SetOptFit
gStyle->SetOptFit(1111);

TGraphErrors * gr1 = new TGraphErrors( n_Fe, x_Fe, y_Fe, NULL, errorY_Fe );
gr1->SetName(“gr1”);
gr1->SetTitle(“Fe”);
gr1->SetMarkerStyle(20);
gr1->SetMarkerColor(4);
gr1->SetDrawOption(“AP”);
gr1->SetLineColor(4);
gr1->SetLineWidth(1);
gr1->SetFillStyle(0);

TGraphErrors * gr2 = new TGraphErrors( n_alpha, x_alpha, y_alpha, NULL, errorY_alpha);
gr2->SetName(“gr2”);
gr2->SetTitle(“alpha”);
gr2->SetMarkerStyle(20);
gr2->SetMarkerColor(2);
gr2->SetDrawOption(“P”);
gr2->SetLineColor(2);
gr2->SetLineWidth(1);
gr2->SetFillStyle(0);

gr1->Fit(“expo”);
gr1->GetFunction(“expo”)->SetLineColor(gr1->GetLineColor());
gr1->GetFunction(“expo”)->SetLineWidth(2);
//gr1->Print(“all”);
gr2->Fit(“expo”);
gr2->GetFunction(“expo”)->SetLineColor(gr2->GetLineColor());
gr2->GetFunction(“expo”)->SetLineWidth(2);

mg->Add(gr1);
mg->Add(gr2);
mg->Draw(“AP”);
mg->SetTitle(“Gain Curves for Fe-55 X-rays and Po-210 alpha particles;Vmesh[V];Gain”);
mycanvas->BuildLegend(0.15, 0.7, 0.4, 0.9,“Ar-CO2–>70%-30%”);
mycanvas->SetLogy(1);

mycanvas->Modified(); mycanvas->Update(); // make sure it’s really (re)drawn
TPaveStats st = ((TPaveStats)(gr1->GetListOfFunctions()->FindObject(“stats”)));
if (st) {
st->SetTextColor(gr1->GetLineColor());
st->SetX1NDC(0.64); st->SetX2NDC(0.99);
st->SetY1NDC(0.4); st->SetY2NDC(0.6);
}
st = ((TPaveStats*)(gr2->GetListOfFunctions()->FindObject(“stats”)));
if (st) {
st->SetTextColor(gr2->GetLineColor());
st->SetX1NDC(0.64); st->SetX2NDC(0.99);
st->SetY1NDC(0.15); st->SetY2NDC(0.35);
}

mycanvas->Modified(); mycanvas->Update(); // make sure it’s really (re)drawn

//Draw arrows in canvas

TArrow *arrow_alpha = new TArrow(590, 190, 575, 985, 0.02, “>”);
arrow_alpha->SetLineColor(2) ;
arrow_alpha->SetFillColor(2) ;
arrow_alpha->SetLineWidth(3) ;
arrow_alpha->SetLineStyle(2) ;
arrow_alpha->Draw();

TArrow *arrow_Fe = new TArrow(617, 12376, 653, 14697, 0.02, “>”);
arrow_Fe->SetLineColor(4) ;
arrow_Fe->SetFillColor(4) ;
arrow_Fe->SetLineWidth(3) ;
arrow_Fe->SetLineStyle(2) ;
arrow_Fe->Draw();

//Add text in canvas

TLatex *text_alpha = new TLatex(583, 150, “#color[2]{sparks}”);
text_alpha->Draw();

TLatex *text_Fe = new TLatex(597, 11983, “#color[4]{sparks}”);
text_Fe->Draw();

mycanvas->Modified(); mycanvas->Update(); // make sure it’s really (re)drawn

mycanvas->Print(“Fe-alpha gain curves.pdf”);
}
[/code]

Again, compare the ORDER of appearances of “Draw” versus “FindObject”.
An additional hint: you do NOT put the cart before the horse, do you?

Oh my god…
You are absolutely right…
I hate when something like this is happening…
Thank you so much!!! =D>

Just for reference to any one who will have the same problem I am giving my code.

[code]# include “TCanvas.h”

include “TROOT.h”

include “TGraphErrors.h”

include “TStyle.h”

include “TMultiGraph.h”

include “TF1.h”

include “TLegend.h”

include “TPaveStats.h”

include “TArrow.h”

include “TLatex.h”

include “TPaveText.h”

include “TText.h”

include “TPavesText.h”

void gainCurves(){

gROOT->Reset();

TCanvas *mycanvas = new TCanvas("c","c",600, 400);
mycanvas->SetFillColor(kSpring-8);
mycanvas->SetFrameFillColor(10);
TMultiGraph * mg = new TMultiGraph("gain curves","GAIN CURVES");

// The values on the X,Y axes and error on Y axis

//Vdrift=700V
const int n_700=15;
double x_700[n_700]={500,505,510,515,520,525,530,535,540,545,550,555,560,565,570};
double y_700[n_700]={1409,1648.5,1918.5,2188,2578,2937.5,3417,3837,4796,5695.5,6924,8183,9712,11660.5,13728.5};
double errorY_700[n_700];
for (int i=0; i<n_700; i++){
	errorY_700[i]=300;
}

//Vdrift=750V

const int n_750=15;
double x_750[n_750]={500,505,510,515,520,525,530,535,540,545,550,555,560,565,570};
double y_750[n_750]={1349,1648.5,1918.5,2128,2518,3027.5,3567,4196.5,4916,5605.5,6864.5,8872.5,9622,11420.5,13608.5};
double errorY_750[n_750];
for (int i=0; i<n_750; i++){
	errorY_750[i]=300;
}

//Vdrift=850V

const int n_850=11;
double x_850[n_850]={500,510,520,530,540,550,555,560,565,570,575};
double y_850[n_850]={1259,1768.5,2398,3327,4616,6564.5,7733.5,9322,11061,13489,16366.5};
double errorY_850[n_850];
for (int i=0; i<n_850; i++){
	errorY_850[i]=300;
}

gROOT->SetStyle("Plain");
// See: http://root.cern.ch/root/html/TStyle.html#TStyle:SetOptFit
gStyle->SetOptFit(1111);

TGraphErrors * gr1 = new TGraphErrors( n_700, x_700, y_700, NULL, errorY_700);
gr1->SetName(“gr1”);
gr1->SetTitle(“V_{drift}=700V”);
gr1->SetMarkerStyle(20);
gr1->SetMarkerColor(4);
gr1->SetDrawOption(“AP”);
gr1->SetLineColor(4);
gr1->SetLineWidth(1);
gr1->SetFillStyle(0);

TGraphErrors * gr2 = new TGraphErrors( n_750, x_750, y_750, NULL, errorY_750);
gr2->SetName(“gr2”);
gr2->SetTitle(“V_{drift}=750V”);
gr2->SetMarkerStyle(20);
gr2->SetMarkerColor(2);
gr2->SetDrawOption(“AP”);
gr2->SetLineColor(2);
gr2->SetLineWidth(1);
gr2->SetFillStyle(0);

TGraphErrors * gr3 = new TGraphErrors( n_850, x_850, y_850, NULL, errorY_850);
gr3->SetName(“gr3”);
gr3->SetTitle(“V_{drift}=850V”);
gr3->SetMarkerStyle(20);
gr3->SetMarkerColor(3);
gr3->SetDrawOption(“AP”);
gr3->SetLineColor(3);
gr3->SetLineWidth(1);
gr3->SetFillStyle(0);

gr1->Fit(“expo”);
gr1->GetFunction(“expo”)->SetLineColor(gr1->GetLineColor());
gr1->GetFunction(“expo”)->SetLineWidth(2);
//gr1->Print(“all”);
gr2->Fit(“expo”);
gr2->GetFunction(“expo”)->SetLineColor(gr2->GetLineColor());
gr2->GetFunction(“expo”)->SetLineWidth(2);

gr3->Fit(“expo”);
gr3->GetFunction(“expo”)->SetLineColor(gr3->GetLineColor());
gr3->GetFunction(“expo”)->SetLineWidth(2);

mycanvas->Modified();
mycanvas->Update(); // make sure it’s really (re)drawn

mg->Add(gr2);
mg->Add(gr1);
mg->Add(gr3);
mg->Draw(“AP”);
mg->SetTitle(“Gain Curves for NTUASmm1;V_{mesh}[V];Gain”);
mycanvas->BuildLegend(0.85, 0.8, 0.99, 0.99,“Ar-CO2–>93%-7%”);
mycanvas->SetLogy(1);

mycanvas->Modified();
mycanvas->Update(); // make sure it’s really (re)drawn

TPaveStats st = ((TPaveStats)(gr1->GetListOfFunctions()->FindObject(“stats”)));
if (st) {
st->SetTextColor(gr1->GetLineColor());
st->SetX1NDC(0.3); st->SetX2NDC(0.7);
st->SetY1NDC(0.3); st->SetY2NDC(0.7);
}

mycanvas->Modified(); 
mycanvas->Update(); // make sure it's really (re)drawn
//st->Draw();
st = ((TPaveStats*)(gr2->GetListOfFunctions()->FindObject("stats")));

//TPaveStats st = ((TPaveStats)(gr1->GetListOfFunctions()->FindObject(“stats”)));
if (st) {
st->SetTextColor(gr2->GetLineColor());
st->SetX1NDC(0.64); st->SetX2NDC(0.99);
st->SetY1NDC(0.15); st->SetY2NDC(0.35);
}
mycanvas->Modified();
mycanvas->Update(); // make sure it’s really (re)drawn
//st1->Draw();
st = ((TPaveStats*)(gr3->GetListOfFunctions()->FindObject(“stats”)));
//TPaveStats st2 = ((TPaveStats)(gr1->GetListOfFunctions()->FindObject(“stats”)));
if (st) {
st->SetTextColor(gr3->GetLineColor());
st->SetX1NDC(0.64); st->SetX2NDC(0.99);
st->SetY1NDC(0.45); st->SetY2NDC(0.55);
}
//st2->Draw();

mycanvas->Modified();
mycanvas->Update(); // make sure it’s really (re)drawn

mycanvas->Print(“gainCurves.pdf”);

}
[/code]