#include "TCanvas.h" #include "TH1.h" #include "TStyle.h" #include "TPaveStats.h" #include "TLatex.h" void addCustomStat() { TCanvas *se = new TCanvas; se->SetFillStyle(0); se->SetFrameFillStyle(0); se->Print("addCustomStat.pdf["); TH1F *h1 = new TH1F("Gaus","Gaus",100,-3,3); TH1F *h2 = new TH1F("Poly","Poly",100,-3,3); h1->FillRandom("gaus",3000); h2->FillRandom("landau",3000); h1->Scale(1.0/h1->Integral()); h2->Scale(1.0/h2->Integral()); gStyle->SetOptStat(); // Enable the statistics box h1->Draw("hist"); if(h1->GetMaximum()GetMaximum()) h1->GetYaxis()->SetRangeUser(0.0, h2->GetMaximum()*1.1); gPad->Update(); TPaveStats *ps = (TPaveStats*)h1->FindObject("stats"); ps->SetFillStyle(0); ps->SetX1NDC(1.0-gPad->GetRightMargin()-0.2); ps->SetX2NDC(1.0-gPad->GetRightMargin()); ps->SetY1NDC(1.0-gPad->GetTopMargin()-0.2); ps->SetY2NDC(1.0-gPad->GetTopMargin()); ps->SetBorderSize(0); ps->SetTextColor(kBlue); ps->SetLineColor(kBlue); TList *listOfLines = ps->GetListOfLines(); TLatex *myt = new TLatex(0,0,"New Entry = 123.45"); myt->SetTextFont(42); myt->SetTextSize(0.04); myt->SetTextColor(kRed); listOfLines->Add(myt); listOfLines->ls(); h2->Draw("hist sames"); h2->SetLineColor(kRed); gPad->Update(); ps = (TPaveStats*)h2->FindObject("stats"); ps->SetFillStyle(0); ps->SetX1NDC(1.0-gPad->GetRightMargin()-0.2); ps->SetX2NDC(1.0-gPad->GetRightMargin()); ps->SetY1NDC(1.0-gPad->GetTopMargin()-0.4); ps->SetY2NDC(1.0-gPad->GetTopMargin()-0.2); ps->SetBorderSize(0); ps->SetTextColor(kRed); ps->SetLineColor(kRed); // Prevent the statistics box from being redrawn automatically //h->SetStats(0); se->Modified(); se->Update(); // Update the canvas again to display the changes se->Print("addCustomStat.pdf"); se->Print("addCustomStat.pdf]"); }