ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fitNormSum.C
Go to the documentation of this file.
1 
2 #include <TMath.h>
3 #include <TCanvas.h>
4 #include <TF1NormSum.h>
5 #include <TF1.h>
6 #include <TH1.h>
7 
8 
9 using namespace std;
10 
11 
12 void fitNormSum()
13 {
14  //***************************************************************************************************
15 
16  // Tutorial for normalized sum of two functions
17  // Here: a background exponential and a crystalball function
18 
19 
20  // Parameters can be set:
21  // I. with the TF1 object before adding the function (for 3) and 4))
22  // II. with the TF1NormSum object (first two are the coefficients, then the non constant parameters)
23  // III. with the TF1 object after adding the function
24 
25  // Sum can be constructed by:
26  // 1) by a string containing the names of the functions and/or the coefficient in front
27  // 2) by a string containg formulas like expo, gaus...
28  // 3) by the list of functions and coefficients (which are 1 by default)
29  // 4) by a std::vector for functions and coefficients
30 
31  //***************************************************************************************************
32 
33 
34  const int nsig = 5.E4;
35  const int nbkg = 1.e6;
36  Int_t NEvents = nsig+nbkg;
37  Int_t NBins = 1e3;
38 
39  double signal_mean = 3;
40  TF1 *f_cb = new TF1("MyCrystalBall","crystalball",-5.,5.);
41  TF1 *f_exp = new TF1("MyExponential","expo",-5.,5.);
42 
43  // I.:
44  f_exp-> SetParameters(1.,-0.3);
45  f_cb -> SetParameters(1,signal_mean,0.3,2,1.5);
46 
47  // CONSTRUCTION OF THE TF1NORMSUM OBJECT ........................................
48  // 1) :
49  TF1NormSum *fnorm_exp_cb = new TF1NormSum(f_cb,f_exp,nsig,nbkg);
50  // 4) :
51 
52  TF1 * f_sum = new TF1("fsum", *fnorm_exp_cb, -5., 5., fnorm_exp_cb->GetNpar());
53  f_sum->Draw();
54 
55  // III.:
56  f_sum->SetParameters( fnorm_exp_cb->GetParameters().data() );
57  f_sum->SetParName(1,"NBackground");
58  f_sum->SetParName(0,"NSignal");
59  for (int i = 2; i < f_sum->GetNpar(); ++i)
60  f_sum->SetParName(i,fnorm_exp_cb->GetParName(i) );
61 
62  //GENERATE HISTOGRAM TO FIT ..............................................................
63  TStopwatch w;
64  w.Start();
65  TH1D *h_sum = new TH1D("h_ExpCB", "Exponential Bkg + CrystalBall function", NBins, -5., 5.);
66  for (int i=0; i<NEvents; i++)
67  {
68  h_sum -> Fill(f_sum -> GetRandom());
69  }
70  printf("Time to generate %d events: ",NEvents);
71  w.Print();
72  //TH1F *h_orig = new TH1F(*h_sum);
73 
74  // need to scale histogram with width since we are fitting a density
75  h_sum -> Sumw2();
76  h_sum -> Scale(1., "width");
77 
78  //fit - use Minuit2 if available
80  new TCanvas("Fit","Fit",800,1000);
81  // do a least-square fit of the spectrum
82  auto result = h_sum -> Fit("fsum","SQ");
83  result->Print();
84  h_sum -> Draw();
85  printf("Time to fit using ROOT TF1Normsum: ");
86  w.Print();
87 
88  // test if parameters are fine
89  std::vector<double> pref = {nsig, nbkg, signal_mean};
90  for (unsigned int i = 0; i< pref.size(); ++i) {
91  if (!TMath::AreEqualAbs(pref[i], f_sum->GetParameter(i), f_sum->GetParError(i)*10.) )
92  Error("testFitNormSum","Difference found in fitted %s - difference is %g sigma",f_sum->GetParName(i), (f_sum->GetParameter(i)-pref[i])/f_sum->GetParError(i));
93  }
94 
95  gStyle->SetOptStat(0);
96  // add parameters
97  auto t1 = new TLatex(-2.5, 300000, TString::Format("%s = %8.0f #pm %4.0f", "NSignal",f_sum->GetParameter(0), f_sum->GetParError(0) ) );
98  auto t2 = new TLatex(-2.5, 270000, TString::Format("%s = %8.0f #pm %4.0f", "Nbackgr",f_sum->GetParameter(1), f_sum->GetParError(1) ) );
99  t1->Draw();
100  t2->Draw();
101 }
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:217
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:56
void fitNormSum()
Definition: fitNormSum.C:12
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
int Int_t
Definition: RtypesCore.h:41
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:254
th1 Draw()
TLatex * t1
Definition: textangle.C:20
void SetParameters(TFitEditor::FuncParams_t &pars, TF1 *func)
Restore the parameters from pars into the function.
Definition: TFitEditor.cxx:287
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2321
std::vector< double > GetParameters() const
Return array of parameters.
Definition: TF1NormSum.cxx:293
To draw Mathematical Formula.
Definition: TLatex.h:33
const char * GetParName(Int_t ipar) const
Definition: TF1NormSum.h:70
void Error(const char *location, const char *msgfmt,...)
Int_t GetNpar() const
Definition: TF1NormSum.cxx:362
Bool_t AreEqualAbs(Double_t af, Double_t bf, Double_t epsilon)
Definition: TMath.h:192
tuple w
Definition: qtexample.py:51
Class adding two functions: c1*f1+c2*f2.
Definition: TF1NormSum.h:26
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
TText * t2
Definition: rootenv.C:28
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition: HFitImpl.cxx:133
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1252
double result[121]
static void SetDefaultMinimizer(const char *type, const char *algo=0)
Stopwatch class.
Definition: TStopwatch.h:30