Re: Multiple stat boxes for multiple TF1s in a single histogram

From: Rene Brun <Rene.Brun_at_cern.ch>
Date: Thu, 16 Mar 2006 12:40:46 +0100 (MET)


Hi Chris,

No, you do not need to refit. You can use the functions already stored in your histogram, eg

    TCanvas *c1 = new TCanvas;
    TFile *f = new TFile("myfile.root");     TH1F *h = (TH1F*)f->Get("h");
    TList *lf = h->GetListOfFunctions();     TH1F *hf1 = (TH1F*)h->Clone("hf1");
    TList *lf1 = hf1->GetListOfFunctions();     lf1->Delete();
    TF1 *f1 = h->GetFunction("name of first function");     lf->Remove(f1);
    lf1->Add(f1);
    hf1->Draw();
    TH1F *hf2 = (TH1F*)h->Clone("hf2");
    TList *lf2 = hf2->GetListOfFunctions();     lf2->Delete();
    TF1 *f2 = h->GetFunction("name of second function");     lf->Remove(f2);

    lf2->Add(f2);
    lf2->SetLineColor(kRed);
    hf2->Draw("sames");

    c1->Update(); //to force the generation of the stats box     TPaveStats *p2 = (TPaveStats*)hf2->FindObject("stats");
    p2->SetY1NDC(0.05);
    p2->SetY2NDC(0.45);
    p2->SetTextColor(kRed);
    c1->Modified();

Rene

On Thu, 16
Mar 2006, Chris Schilling wrote:

> Hi Rene,
>
> Thanks. I have seen this solution, and I think that my case is complicated
> by the fact that I first store the histogram in a file with all n functions
> stored in the TH1. I then access the histograms from the file and do all the
> drawing. I guess it looks like my only choice is to either create the box
> manually or to refit the histograms using this procedure.
>
> Thanks again
> Chris
>
>
> Rene Brun wrote:
>> Hi Chris,
>>
>> There is only one "stats" box per histogram.
>> To achieve what you want see example below.
>>
>> Rene Brun
>>
>>
>> void fitmany() {
>> TCanvas *c1 = new TCanvas;
>> TH1F *h = new TH1F("h","test",100,-3,3);
>> h->FillRandom("gaus",50000);
>> gStyle->SetOptFit();
>> TH1F *hf1 = (TH1F*)h->Clone("hf1");
>> hf1->Fit("pol3","","",-3,-1);
>> TH1F *hf2 = (TH1F*)h->Clone("hf2");
>> hf2->Fit("gaus","","sames",-0.8,0.8);
>> hf2->GetFunction("gaus")->SetLineColor(kRed);
>> c1->Update(); //to force the generation of the stats box
>> TPaveStats *p2 = (TPaveStats*)hf2->FindObject("stats");
>> p2->SetY1NDC(0.05);
>> p2->SetY2NDC(0.45);
>> p2->SetTextColor(kRed);
>> c1->Modified();
>> }
>>
>>
>>
>> On Wed, 15 Mar 2006, Chris Schilling wrote:
>>
>>> Hi,
>>>
>>> I am fitting a TH1 with multiple functions (using the '+') option. Then I
>>> store the histogram in a root file.
>>>
>>> I know how to access the various functions in the TH1, but I wonder how to
>>> get the fit boxes belonging to each TF1 and plotting them on the same
>>> Canvas.
>>>
>>> Currently the fit box for the first fit in the list of functions is
>>> displayed, even though that is not necessarily the function I am drawing.
>>> I would like to know how to draw the fit box corresponding to the function
>>> that I am drawing.
>>>
>>> Thanks for the advice
>>> Chris
>>>
>
Received on Thu Mar 16 2006 - 12:40:56 MET

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:31:57 MET