How can one move stat box only for a given histogram?

Hello,
I want to move the stat box to the top left corner (instead of the top right default position)
only for one of my histograms using the command gStyle->SetStatX(0.3) inserted just before the
corresponding Draw command. Doing so, unfortunately the stat box is moved to the top left
corner also for all other histograms whose Draw command even precedes the above
gStyle->SetStatX(0.3) command (see the excerpt of my code below).
Apparently, the gStyle command acts globally, i.e.
it doesn’t respect the order of the C++ commands. How can I move the stat box just for a given
histogram so that for the others the stat boxes remain at their default place?
Thanks,
Elemer

// Plot statistics
TCanvas* LengthStat = new TCanvas(“LengthStat”, “LengthStat”, 750, 750);
LengthStat -> Divide(4,4);
TPad* pad;
pad = (TPad*)LengthStat->cd(1);
hMMLA->Draw("");
pad = (TPad*)LengthStat->cd(2);
hMMLNT->Draw("");
pad = (TPad*)LengthStat->cd(3);
hMMLT->Draw("");
pad = (TPad*)LengthStat->cd(4);
hMTLT->Draw("");
pad = (TPad*)LengthStat->cd(5);
hTrLC->Draw("");
pad = (TPad*)LengthStat->cd(6);
hTrLD->Draw("");
pad = (TPad*)LengthStat->cd(7);
hTrLH->Draw("");
pad = (TPad*)LengthStat->cd(8);
hTrLS->Draw("");
pad = (TPad*)LengthStat->cd(9);
hTrL->Draw("");
pad = (TPad*)LengthStat->cd(10);
hMLNT->Draw("");
pad = (TPad*)LengthStat->cd(11);
hMLT->Draw("");
gStyle->SetStatX(0.3); // set statbox to top left
pad = (TPad*)LengthStat->cd(12);
hLmaxVsD->Draw(“COLZ”);

You can get the individual statistics box like this:

auto h = new TH1F("h","h",100,0,100);
h->Draw();
auto stat = dynamic_cast<TPaveStats*>(h->FindObject("stats"));
if (stat) {
   stat->SetX1NDC(0.1); stat->SetX2NDC(0.3);
   stat->Draw()
} else {
   cerr << "No stats box found!\n";
}

Hello
stat->SetX1NDC(0.1); stat->SetX2NDC(0.3);
doesn’t seem to change the default position of the stat box. It remains at top right,
in contrast to gStyle->SetStatX(0.3) which moves the stat box to the top left.
Here is how I changed my code according to your suggestion, but the result is
that the stat box remains at the same position:
//gStyle->SetStatX(0.3); // set statbox top left
pad = (TPad*)LengthStat->cd(12);
auto stat = dynamic_cast<TPaveStats*>(hLmaxVsD->FindObject(“stats”));
if (stat) {
stat->SetX1NDC(0.1); stat->SetX2NDC(0.3));
stat->Draw();
} else {
cerr << “No stats box found!\n”;
}
hLmaxVsD->Draw(“COLZ”);

Hello again,
in fact in my modified code:
//gStyle->SetStatX(0.3); // set statbox top left
pad = (TPad*)LengthStat->cd(12);
hLmaxVsD->Draw(“COLZ”);
auto stat = dynamic_cast<TPaveStats*>(hLmaxVsD->FindObject(“stats”));
if (stat) {
std::cout << " X1NDC: " << stat->GetX1NDC() << " X2NDC: " << stat->GetX2NDC() << std::endl;
stat->SetX1NDC(0.1); stat->SetX2NDC(0.3);
stat->Draw();
} else {
cerr << “No stats box found!\n”;
}
the answer was:
No stats box found!
although the statsbox is plotted on the top right place.
I don’t understand why the stats box was not found.

Hello,
the correct code to move stats box individually which worked is:
gStyle->SetOptStat(“nemr”);
pad = (TPad*)LengthStat->cd(12);
hLmaxVsD->Draw(“COLZ”);
// To move stats box individually: https://root.cern/doc/master/classTPaveStats.html
gPad->Update();
auto stat = dynamic_cast<TPaveStats*>(hLmaxVsD->FindObject(“stats”));
if (stat) {
std::cout << " X1NDC: " << stat->GetX1NDC() << " X2NDC: " << stat->GetX2NDC() << std::endl;
stat->SetX1NDC(0.1); stat->SetX2NDC(0.3);
stat->Draw();
} else {
cerr << “No stats box found!\n”;
}
Note that the line
gPad->Update();
was essential.

2 Likes

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