CbmRoot
Loading...
Searching...
No Matches
PairAnalysisSignalFunc.cxx
Go to the documentation of this file.
1/* Copyright (C) 2015-2016 Justus-Liebig-Universitaet Giessen, Giessen
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Julian Book [committer] */
4
6// Dielectron Function //
7// //
8// //
9/*
10
11 1) add any background function you like
12 TF1 *fB = new TF1("fitBgrd","pol3",minFit,maxFit);
13
14 2) configure the signal extraction
15
16 2.1) chose one of the signal functions (MCshape, CrystalBall, Gauss)
17 TF1 *fS = new TF1("fitSign",PairAnalysisFunction::PeakFunCB,minFit,maxFit,5); // has 5 parameters
18 // TF1 *fS = new TF1("fitSign",PairAnalysisFunction::PeakFunGaus,minFit,maxFit,3); // has 3 parameters
19 // sig->SetMCSignalShape(hMCsign);
20 // TF1 *fS = new TF1("fitSign",PairAnalysisFunction::PeakFunMC,minFit,maxFit,1); // requires a MC shape
21
22
23 3) combined fit of bgrd+signal
24
25 3.1) combine the two functions
26 sig->CombineFunc(fS,fB);
27 3.2) apply fitting ranges and the fit options
28 sig->SetFitRange(minFit, maxFit);
29 sig->SetFitOption("NR");
30
31
32 6) access the spectra and values created
33
34 6.1) fit function
35 TF1 *fFitSign = sig->GetCombinedFunction(); // combined fit function
36 TF1 *fFitExtr = sig->GetSignalFunction(); // signal function
37 TF1 *fFitBgrd = sig->GetBackgroundFunction(); // background function
38
39*/
40// //
42
43#include <TF1.h>
44#include <TFitResult.h>
45#include <TGraph.h>
46#include <TH1.h>
47#include <TList.h>
48#include <TMath.h>
49#include <TPaveText.h>
50#include <TString.h>
51//#include <../hist/hist/src/TF1Helper.h>
52
54
56
57
59 : TNamed("function", "function")
60{
61 //
62 // Default Constructor
63 //
64}
65
66//______________________________________________
67PairAnalysisFunction::PairAnalysisFunction(const char* name, const char* title) : TNamed(name, title)
68{
69 //
70 // Named Constructor
71 //
72}
73
74//______________________________________________
76{
77 //
78 // Default Destructor
79 //
80 if (fFuncSigBack) delete fFuncSigBack;
81 if (fFuncSignal) delete fFuncSignal;
83}
84
85
86//______________________________________________________________________________
87Double_t PairAnalysisFunction::PeakFunMC(const Double_t* x, const Double_t* par)
88{
89 // Fit MC signal shape
90 // parameters
91 // [0]: scale for simpeak
92
93 Double_t xx = x[0];
94
95 TH1F* hPeak = fgHistSimPM;
96 if (!hPeak) {
97 printf("E-PairAnalysisFunction::PeakFun: No histogram for peak fit defined!\n");
98 return 0.0;
99 }
100
101 Int_t idx = hPeak->FindBin(xx);
102 if ((idx <= 1) || (idx >= hPeak->GetNbinsX())) { return 0.0; }
103
104 return (par[0] * hPeak->GetBinContent(idx));
105}
106
107//______________________________________________________________________________
108Double_t PairAnalysisFunction::PeakFunCB(const Double_t* x, const Double_t* par)
109{
110 // Crystal Ball fit function
111
112 Double_t n = par[0];
113 Double_t alpha = par[1];
114 Double_t meanx = par[2];
115 Double_t sigma = par[3];
116 Double_t nn = par[4];
117
118 Double_t a = TMath::Power((n / TMath::Abs(alpha)), n) * TMath::Exp(-.5 * alpha * alpha);
119 Double_t b = n / TMath::Abs(alpha) - TMath::Abs(alpha);
120
121 Double_t arg = (x[0] - meanx) / sigma;
122 Double_t fitval = 0;
123
124 if (arg > -1. * alpha) { fitval = nn * TMath::Exp(-.5 * arg * arg); }
125 else {
126 fitval = nn * a * TMath::Power((b - arg), (-1 * n));
127 }
128
129 return fitval;
130}
131
132//______________________________________________________________________________
133Double_t PairAnalysisFunction::PeakFunGaus(const Double_t* x, const Double_t* par)
134{
135 // Gaussian fit function
136 //printf("fNparBgrd %d \n",fNparBgnd);
137 Double_t n = par[0];
138 Double_t mean = par[1];
139 Double_t sigma = par[2];
140 Double_t xx = x[0];
141
142 return (n * TMath::Exp(-0.5 * TMath::Power((xx - mean) / sigma, 2)));
143}
144
145//______________________________________________
146void PairAnalysisFunction::SetFunctions(TF1* const combined, TF1* const sig, TF1* const back, Int_t parM, Int_t parMres)
147{
148 //
149 // Set the signal, background functions and combined fit function
150 // Note: The process method assumes that the first n parameters in the
151 // combined fit function correspond to the n parameters of the signal function
152 // and the n+1 to n+m parameters to the m parameters of the background function!!!
153
154 if (!sig || !back || !combined) {
155 Error("SetFunctions", "Both, signal and background function need to be set!");
156 return;
157 }
158 fFuncSignal = sig;
159 fFuncBackground = back;
160 fFuncSigBack = combined;
161 fParMass = parM;
162 fParMassWidth = parMres;
163}
164
165//______________________________________________
167{
168 //
169 // Setup some default functions:
170 // type = 0: gaus signal + linear background in 2.5 - 4 GeV inv. mass
171 // type = 1: gaus signal + exponential background in 2.5 - 4 GeV inv. mass
172 // type = 2: half gaussian, half exponential signal function
173 // type = 3: Crystal-Ball function
174 // type = 4: Crystal-Ball signal + exponential background
175 //
176 // TODO: use PDG mass and width + fPOI for parameter settings
177
178 if (type == 0) {
179 fFuncSignal = new TF1("DieleSignal", "gaus", 2.5, 4);
180 fFuncBackground = new TF1("DieleBackground", "pol1", 2.5, 4);
181 fFuncSigBack = new TF1("DieleCombined", "gaus+pol1(3)", 2.5, 4);
182
183 fFuncSigBack->SetParameters(1, 3.1, .05, 2.5, 1);
184 fFuncSigBack->SetParLimits(0, 0, 10000000);
185 fFuncSigBack->SetParLimits(1, 3.05, 3.15);
186 fFuncSigBack->SetParLimits(2, .02, .1);
187 }
188 else if (type == 1) {
189 fFuncSignal = new TF1("DieleSignal", "gaus", 2.5, 4);
190 fFuncBackground = new TF1("DieleBackground", "[0]*exp(-(x-[1])/[2])", 2.5, 4);
191 fFuncSigBack = new TF1("DieleCombined", "gaus+[3]*exp(-(x-[4])/[5])", 2.5, 4);
192
193 fFuncSigBack->SetParameters(1, 3.1, .05, 1, 2.5, 1);
194 fFuncSigBack->SetParLimits(0, 0, 10000000);
195 fFuncSigBack->SetParLimits(1, 3.05, 3.15);
196 fFuncSigBack->SetParLimits(2, .02, .1);
197 }
198 else if (type == 2) {
199 // half gaussian, half exponential signal function
200 // exponential background
201 fFuncSignal = new TF1("DieleSignal",
202 "(x<[1])*([0]*(exp(-0.5*((x-[1])/[2])^2)+exp((x-[1])/"
203 "[3])*(1-exp(-0.5*((x-[1])/"
204 "[2])^2))))+(x>=[1])*([0]*exp(-0.5*((x-[1])/[2])^2))",
205 2.5, 4);
206 fFuncBackground = new TF1("DieleBackground", "[0]*exp(-(x-[1])/[2])+[3]", 2.5, 4);
207 fFuncSigBack = new TF1("DieleCombined",
208 "(x<[1])*([0]*(exp(-0.5*((x-[1])/[2])^2)+exp((x-[1])/"
209 "[3])*(1-exp(-0.5*((x-[1])/[2])^2))))+(x>=[1])*([0]*exp(-0.5*((x-[1])/"
210 "[2])^2))+[4]*exp(-(x-[5])/[6])+[7]",
211 2.5, 4);
212 fFuncSigBack->SetParameters(1., 3.1, .05, .1, 1, 2.5, 1, 0);
213
214 fFuncSigBack->SetParLimits(0, 0, 10000000);
215 fFuncSigBack->SetParLimits(1, 3.05, 3.15);
216 fFuncSigBack->SetParLimits(2, .02, .1);
217 fFuncSigBack->FixParameter(6, 2.5);
218 fFuncSigBack->FixParameter(7, 0);
219 }
220}
221
222//______________________________________________________________________________
223void PairAnalysisFunction::CombineFunc(TF1* const peak, TF1* const bgnd)
224{
225 //
226 // combine the bgnd and the peak function
227 //
228
229 if (!peak || !bgnd) {
230 Error("CombineFunc", "Both, signal and background function need to be set!");
231 return;
232 }
233 fFuncSignal = peak;
234 fFuncBackground = bgnd;
235
236 fNparPeak = fFuncSignal->GetNpar();
237 fNparBgnd = fFuncBackground->GetNpar();
238
240 return;
241}
242
243//______________________________________________________________________________
244Double_t PairAnalysisFunction::PeakBgndFun(const Double_t* x, const Double_t* par)
245{
246 //
247 // merge peak and bgnd functions
248 //
249 return (fFuncSignal->EvalPar(x, par) + (fFuncBackground ? fFuncBackground->EvalPar(x, par + fNparPeak) : 0.));
250}
251
252//______________________________________________
253void PairAnalysisFunction::Print(Option_t* /*option*/) const
254{
255 //
256 // Print the statistics
257 //
258 printf("Fit int. : %.5g - %.5g \n", fFitMin, fFitMax);
259 printf("Fit chi/%d: %.5g \n", fDof, fChi2Dof);
260}
ClassImp(PairAnalysisFunction) PairAnalysisFunction
void SetFunctions(TF1 *const combined, TF1 *const sig=0, TF1 *const back=0, Int_t parM=1, Int_t parMres=2)
Double_t PeakBgndFun(const Double_t *x, const Double_t *par)
Double_t PeakFunMC(const Double_t *x, const Double_t *par)
void CombineFunc(TF1 *const peak=0, TF1 *const bgnd=0)
Double_t PeakFunCB(const Double_t *x, const Double_t *par)
Double_t PeakFunGaus(const Double_t *x, const Double_t *par)
virtual void Print(Option_t *option="") const