Hi Sergei,
The "stats" box is only created when the histogram is effectively drawn
in the canvas. In a macro you must force an update of the canvas.
You will find below two examples of macros.
Note that there are two TH1::Fillrandom functions. You can use a
function name
directly.
Rene Brun
void ran1()
{
//macro generating two histograms from two functions
//the two histograms are drawn in the same canvas
f1=new TF1("f1","x**3");
f2=new TF1("f2","x**2");
h1=new TH1F("h1","",100,0.,1.);
h2=new TH1F("h2","",100,0.,1.);
h1->FillRandom("f1", 100000);
h1->Draw();
//force canvas update. This will generate the stats object
gPad->Update();
TPaveStats *stats = (TPaveStats*)gPad->GetPrimitive("stats");
stats->SetName("h1stats");
h2->FillRandom("f2",100000);
h2->Draw("sames");
}
void ran2()
{
//same as ran1. In addition the canvas is divided in two pads
//the two functions are drawn in the first pad, the histograms in the
2nd pad
//the first stats box is shifted to not overlap with the 2nd stats.
c1 = new TCanvas("c1");
c1->Divide(2,1);
c1->cd(1);
f1=new TF1("f1","x**3");
f2=new TF1("f2","x**2");
f1->Draw();
f2->Draw("same");
c1->cd(2);
h1=new TH1F("h1","",100,0.,1.);
h2=new TH1F("h2","",100,0.,1.);
h1->FillRandom("f1", 100000);
h1->Draw();
//force canvas update. This will generate the stats object
gPad->Update();
TPaveStats *stats = (TPaveStats*)gPad->GetPrimitive("stats");
stats->SetName("h1stats");
stats->SetX1NDC(.5);
stats->SetX2NDC(.7);
h2->FillRandom("f2",100000);
h2->Draw("sames");
}
Sergei Avvakumov wrote:
>
> Hi,
>
> I was trying to use this new option as described in the
> release notes of version 2.23_10, but the following macro:
>
> {
> f1=TF1("f1","x**3");
> f2=TF1("f2","x**2");
> f1->Draw();
> f2->Draw("same");
> h1=TH1F("h1","",100,0.,1.);
> h2=TH1F("h2","",100,0.,1.);
> h1->FillRandom(f1->GetHistogram(), 1000000);
> h1->Draw();
> TPaveStats *stats = (TPaveStats*)gPad->GetPrimitive("stats");
> stats->SetName("h1stats");
> h2->FillRandom(f2->GetHistogram(),1000000);
> h2->Draw("sames");
> }
>
> fails with an error message:
>
> root [0] .x testsames.C
> <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
> Error: illegal pointer to class object stats 0x0 94 FILE:testsames.C
> LINE:11
> *** Interpreter error recovered ***
>
> If I type this macro command by command it works.
>
> Thanks,
>
> Sergei Avvakumov
This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:16 MET