Logo ROOT   6.08/07
Reference Guide
fitExclude.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_fit
3 /// \notebook -js
4 /// Illustrates how to fit excluding points in a given range.
5 ///
6 /// \macro_image
7 /// \macro_output
8 /// \macro_code
9 ///
10 /// \author Rene Brun
11 
12 #include "TH1.h"
13 #include "TF1.h"
14 #include "TList.h"
15 
16 Bool_t reject;
17 Double_t fline(Double_t *x, Double_t *par)
18 {
19  if (reject && x[0] > 2.5 && x[0] < 3.5) {
21  return 0;
22  }
23  return par[0] + par[1]*x[0];
24 }
25 
26 void fitExclude() {
27  //Create a source function
28  TF1 *f1 = new TF1("f1","[0] +[1]*x +gaus(2)",0,5);
29  f1->SetParameters(6,-1,5,3,0.2);
30  // create and fill histogram according to the source function
31  TH1F *h = new TH1F("h","background + signal",100,0,5);
32  h->FillRandom("f1",2000);
33  TF1 *fl = new TF1("fl",fline,0,5,2);
34  fl->SetParameters(2,-1);
35  //fit only the linear background excluding the signal area
36  reject = kTRUE;
37  h->Fit(fl,"0");
38  reject = kFALSE;
39  //store 2 separate functions for visualization
40  TF1 *fleft = new TF1("fleft",fline,0,2.5,2);
41  fleft->SetParameters(fl->GetParameters());
42  h->GetListOfFunctions()->Add(fleft);
43  gROOT->GetListOfFunctions()->Remove(fleft);
44  TF1 *fright = new TF1("fright",fline,3.5,5,2);
45  fright->SetParameters(fl->GetParameters());
46  h->GetListOfFunctions()->Add(fright);
47  gROOT->GetListOfFunctions()->Remove(fright);
48  h->Draw();
49 }
50 
double par[1]
Definition: unuranDistr.cxx:38
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:439
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:302
TH1 * h
Definition: legend2.C:5
#define gROOT
Definition: TROOT.h:364
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Double_t x[n]
Definition: legend1.C:17
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3293
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
static void RejectPoint(Bool_t reject=kTRUE)
Static function to set the global flag to reject points the fgRejectPoint global flag is tested by al...
Definition: TF1.cxx:3407
double Double_t
Definition: RtypesCore.h:55
virtual void Add(TObject *obj)
Definition: TList.h:81
1-Dim function class
Definition: TF1.h:149
TF1 * f1
Definition: legend1.C:11
virtual Double_t * GetParameters() const
Definition: TF1.h:365
const Bool_t kTRUE
Definition: Rtypes.h:91
TList * GetListOfFunctions() const
Definition: TH1.h:248
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition: TH1.cxx:3563