Adding fit parameters into statistical box

Hi, Rooters,

For a histogram will be saved in a file, I’d like to do :
1). add fit parameters(including errors) into its statistical box.
2). adjust the statistical box position to the lower right of a canvas instead of the default position which is upper right.

Task 1) sounds easy because there is a example here : root.cern.ch/root/html/tutorial … ing.C.html.
However, my case is not exactly the same one as the example in the sense I don’t need to define a canvas explicitly. Therefore, I modified a little bit to adapt my case. This “little bit” turns out a big stone in front of me, :frowning:, I just can’t modify anything in the box .
Could any of you please help me out ?


              h_ccd_CTE_L->SetOption("p");
              h_ccd_CTE_L->Draw();

              TF1 *pol1_LCCD = new TF1("pol1","pol1",lower_x_hist_cte,upper_x_hist_cte);
              h_ccd_CTE_L->Fit(pol1_LCCD,"R");

              gPad->Update();
              TPaveStats *st = (TPaveStats*)h_ccd_CTE_L->FindObject("stats");

              TList *list = st->GetListOfLines();

              st->SetOptFit(1);
              st->Draw();

             TLatex *myt = new TLatex(0,0,"Test = 10");
             myt ->SetTextFont(42);
             myt ->SetTextSize(0.04);
             myt ->SetTextColor(kRed);
             list->Add(myt);

            gPad->Modified();

Task 2) , is there any way to make the statistical box move to any desired place relative to the canvas ? Obviously I’m not meaning an absolute position on a canvas.

Thanks !

Best,
Junhui

1 Like

[code] gStyle->SetOptFit(1);
h_ccd_CTE_L->SetOption(“p”);

TF1 *pol1_LCCD = new TF1(“pol1”,“pol1”,lower_x_hist_cte,upper_x_hist_cte);
h_ccd_CTE_L->Fit(pol1_LCCD,“R”);

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

// https://root.cern.ch/root/html/TPaveStats.html
TPaveStats st = (TPaveStats)h_ccd_CTE_L->FindObject(“stats”);
st->SetX1NDC(0.7);
st->SetX2NDC(0.99);
st->SetY1NDC(0.2);
st->SetY2NDC(0.5);

st->SetName(“mystats”);
h_ccd_CTE_L->SetStats(0);
TList *list = st->GetListOfLines();
TLatex *myt = new TLatex(0, 0, “Test = 10”);
myt ->SetTextFont(42);
myt ->SetTextSize(0.04);
myt ->SetTextColor(kRed);
list->Add(myt);

gPad->Modified(); gPad->Update(); // make sure it’s (re)drawn[/code]

Hi, Wille,

It works perfectly, although not sure understand
1). why the line of gStyle->SetOptFit(1); should be moved ahead ?

2). why an pointer of “TPaveStats”, “st”, can’t call “SetOptFit(1)” successfully here ?

Could you please explain a little bit on these ?
Thanks !

Best,
Junhui

[code] h_ccd_CTE_L->SetOption(“p”);

TF1 *pol1_LCCD = new TF1(“pol1”,“pol1”,lower_x_hist_cte,upper_x_hist_cte);
h_ccd_CTE_L->Fit(pol1_LCCD,“R”);

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

// https://root.cern.ch/root/html/TPaveStats.html
TPaveStats st = (TPaveStats)h_ccd_CTE_L->FindObject(“stats”);
st->SetOptFit(1);
gPad->Modified(); gPad->Update(); // make sure it’s (re)drawn
st->SetX1NDC(0.7);
st->SetX2NDC(0.99);
st->SetY1NDC(0.2);
st->SetY2NDC(0.5);

st->SetName(“mystats”);
h_ccd_CTE_L->SetStats(0);
TList *list = st->GetListOfLines();
TLatex *myt = new TLatex(0, 0, “Test = 10”);
myt ->SetTextFont(42);
myt ->SetTextSize(0.04);
myt ->SetTextColor(kRed);
list->Add(myt);

gPad->Modified(); gPad->Update(); // make sure it’s (re)drawn[/code]

Hi, Wile,

Thanks for your last reply !

I’ve tried it, it worked well.

However, there is another small question :
Instead of displaying the line of “Test 10” on a statistical box, I’d like to display another parameter based on the value of my fit.
Specifically, how to display something like this " CTI Value " on statistical box where “CTE” is a “constant string” should not be changed, while the “Value” should change according to fit results of each data file like a calculation of p0 divided by p1(p0 and p1 are two fit parameters.)

Thanks !

Best,
Junhui

Try: TLatex *myt = new TLatex(0, 0, TString::Format("Ratio = %g", pol1_LCCD->GetParameter("p0") / pol1_LCCD->GetParameter("p1"))); or: TLatex *myt = new TLatex(0, 0, TString::Format("Ratio = %g", pol1_LCCD->GetParameter(0) / pol1_LCCD->GetParameter(1)));

Hi, Wile,

Both options worked just perfect !

Appreciate it !

Best,
Junhui