Re: Accessing stats panel from histo

From: Rene Brun <Rene.Brun_at_cern.ch>
Date: Sun, 17 Feb 2008 21:49:20 +0100


To force the generation of the "stats" object, you must update the canvas. See code below

Rene Brun

void test()
{

   TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);   

   // Fill histograms randomly
   gRandom->SetSeed();
   for (Int_t i = 0; i < 25000; i++) hpx->Fill(gRandom->Gaus());    TCanvas *c1 = new TCanvas;
   hpx->Draw();
   c1->Update();
   TPaveStats * st = (TPaveStats
*)hpx->GetListOfFunctions()->FindObject("stats");

   if( st ) st->SetTextColor(kRed); else std::cout << "Null pointer to TPaveStats: " << st << std::endl;

   hpx->Draw();
}

Jose Helder Lopes wrote:
> Dear rooters,
>
> I want to change some properties of the stat panel of a TH1F inside a
> macro. The following commands work interactively, with the final
> plot showing stat text in red:
>
> ROOT 5.17/08 (trunk_at_21355, De 28 2007, 13:45:00 on linuxx8664gcc)
> CINT/ROOT C/C++ Interpreter version 5.16.27, Oct 25, 2007
> Type ? for help. Commands must be C++ statements.
> Enclose multiple statements between { }.
> root [0] TH1F *hpx = new TH1F("hpx","This is the px
> distribution",100,-4,4);
> root [1] gRandom->SetSeed();
> root [2] for (Int_t i = 0; i < 25000; i++) hpx->Fill(gRandom->Gaus());
> root [3] hpx->Draw();
> <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
> root [4] TPaveStats * st = (TPaveStats
> *)hpx->GetListOfFunctions()->FindObject("stats");
> root [5] if( st ) st->SetTextColor(kRed); else std::cout << "Null
> pointer to TPaveStats: " << st << std::endl;
> root [6] hpx->Draw();
>
> However when I put them in a macro test.C they fail:
>
> The macro:
>
> #include <TROOT.h>
> #include <TSystem.h>
> #include <TH1F.h>
> #include <TRandom.h>
> #include <TPaveStats.h>
>
> #include <iostream>
>
> void test()
> {
> TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
>
> // Fill histograms randomly
> gRandom->SetSeed();
> for (Int_t i = 0; i < 25000; i++) hpx->Fill(gRandom->Gaus());
> hpx->Draw();
> TPaveStats * st = (TPaveStats *)hpx->GetListOfFunctions()->FindObject("stats");
> if( st ) st->SetTextColor(kRed); else std::cout << "Null pointer to TPaveStats: " << st << std::endl;
> hpx->Draw();
> }
>
> Executing :
>
> ROOT 5.17/08 (trunk_at_21355, De 28 2007, 13:45:00 on linuxx8664gcc)
> CINT/ROOT C/C++ Interpreter version 5.16.27, Oct 25, 2007
> Type ? for help. Commands must be C++ statements.
> Enclose multiple statements between { }.
> root [0] .x test.C+
> Info in <TUnixSystem::ACLiC>: creating shared library
> /home/helder/B02MuMuKstar/R_Kstar/./test_C.so
> <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
> Null pointer to TPaveStats: 0
> root [1] .q
>
> What can be wrong?
> By the way, is there a simpler way to do this? I dont like to have to do
> the first hpx->Draw().
>
> Thank you for any help.
> Best regards,
> Helder
>
Received on Sun Feb 17 2008 - 21:49:34 CET

This archive was generated by hypermail 2.2.0 : Mon Feb 18 2008 - 11:50:01 CET