Control over statistic box information in compiled code using ROOT


ROOT Version: 6.26/04
Platform: MacOS Monterey 12.5
Compiler: clang-1316.0.21.2.5


Hello,

When drawing the statistics box for a histogram with fit, I have noticed one can not select just the fitting parameters to be written into the box using st->SetOptFit(1), since the value „1“ corresponds to the default value „111“. To minimise the amount of information that I don’t need, I now use 1001 (so Probability and Parameters). I also have tried to directly access the box and remove the Probability line as a TText object but it seems that this does not work. Reading the documentation of SetOptStat (in TPaveStats), where there is also a default value for the reason of backwards compatibility, I understand the situation may be similar for SetOptFit too. Is there another way to show only the parameter values in the statistics box?

Below, I have added a minimum working example. Thanks a lot in advance.

#include <TCanvas.h>
#include <TPaveStats.h>
#include <TRandom.h>

void draw_hist(TH1F* hist) {
    TF1* fit_gaus = new TF1("fit_gaus", "gaus", -5, 5.);
    TCanvas* c = new TCanvas();
    c->cd();
    hist->Draw();
    c->Update();
    TPaveStats* st_hist1 = (TPaveStats*)hist->FindObject("stats");
    st_hist1->SetY1NDC(0.5);
    st_hist1->SetX1NDC(0.6);
    hist->Fit(fit_gaus, "R");
    st_hist1->SetOptFit(1001);
    TList* listOfLines = st_hist1->GetListOfLines();
    TText* remove_prob = st_hist1->GetLineWith("Prob");
    listOfLines->Remove(remove_prob);
    st_hist1->Draw();
    c->Update();
    c->SaveAs("test.png");
}

int main() {
    TH1F* h1 = new TH1F("h1", "h1", 200, -5, 5);

    for (int i = 0; i<1000; i++) {
        TRandom randgen;
        randgen.SetSeed(0);
        h1->Fill(randgen.Gaus(0, 1));
    }
    draw_hist(h1);

    return 0;
    
}

10001

That worked, thanks a lot!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.