GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TAB3Peak.cxx
Go to the documentation of this file.
1#include "TAB3Peak.h"
2#include "TH1.h"
3
4void TAB3Peak::Centroid(const Double_t& centroid)
5{
6 SetFitFunction(new TF1("ab_fit", this, &TAB3Peak::TotalFunction, 0, 1, 8, "TAB3Peak", "TotalFunction"));
7 SetPeakFunction(new TF1("ab_peak", this, &TAB3Peak::PeakFunction, 0, 1, 7, "TAB3Peak", "PeakFunction"));
9 GetFitFunction()->SetParameter(1, centroid);
10 SetListOfBGPar(std::vector<bool>{false, false, false, false, false, false, false, true});
11 GetFitFunction()->SetLineColor(kMagenta);
12}
13
15{
16 GetFitFunction()->SetParName(0, "Height");
17 GetFitFunction()->SetParName(1, "centroid");
18 GetFitFunction()->SetParName(2, "sigma");
19 GetFitFunction()->SetParName(3, "rel_height1");
20 GetFitFunction()->SetParName(4, "rel_sigma1");
21 GetFitFunction()->SetParName(5, "rel_height2");
22 GetFitFunction()->SetParName(6, "rel_sigma2");
23 GetFitFunction()->SetParName(7, "step");
24}
25
26void TAB3Peak::InitializeParameters(TH1* fit_hist, const double& rangeLow, const double& rangeHigh)
27{
28 /// Makes initial guesses at parameters for the fit base on the histogram.
29 // Make initial guesses
30 // Actually set the parameters in the photopeak function
31 // Fixing has to come after setting
32 // Might have to include bin widths eventually
33 // The centroid should already be set by this point in the ctor
34 Int_t bin = fit_hist->FindBin(GetFitFunction()->GetParameter(1));
35 if(!ParameterSetByUser(0)) {
36 GetFitFunction()->SetParameter("Height", fit_hist->GetBinContent(bin));
37 GetFitFunction()->SetParLimits(0, 0, fit_hist->GetMaximum() * 1.5);
38 }
39 if(!ParameterSetByUser(1)) {
40 GetFitFunction()->SetParLimits(1, rangeLow, rangeHigh);
41 }
42 if(!ParameterSetByUser(2)) {
43 GetFitFunction()->SetParameter("sigma", TMath::Sqrt(2.25 + 1.33 * GetFitFunction()->GetParameter("centroid") / 1000. + 0.9 * TMath::Power(GetFitFunction()->GetParameter("centroid") / 1000., 2)) / 2.35);
44 GetFitFunction()->SetParLimits(2, 0.1, 8);
45 }
46 if(!ParameterSetByUser(3)) {
47 GetFitFunction()->SetParameter("rel_height1", 0.25);
48 GetFitFunction()->SetParLimits(3, 0.000001, 1.0);
49 }
50 if(!ParameterSetByUser(4)) {
51 GetFitFunction()->SetParameter("rel_sigma1", 2.);
52 GetFitFunction()->SetParLimits(4, 1.0, 100);
53 }
54 if(!ParameterSetByUser(5)) {
55 GetFitFunction()->SetParameter("rel_height2", 0.25);
56 GetFitFunction()->SetParLimits(5, 0.000001, 1.0);
57 }
58 if(!ParameterSetByUser(6)) {
59 GetFitFunction()->SetParameter("rel_sigma2", 6.);
60 GetFitFunction()->SetParLimits(6, 1.0, 100);
61 }
62 if(!ParameterSetByUser(7)) {
63 // Step size is allow to vary to anything. If it goes below 0, the code will fix it to 0
64 GetFitFunction()->SetParameter("step", 1.0);
65 GetFitFunction()->SetParLimits(7, 0.0, 1.0E2);
66 }
67}
68
69Double_t TAB3Peak::Centroid() const
70{
71 return GetFitFunction()->GetParameter("centroid");
72}
73
74Double_t TAB3Peak::CentroidErr() const
75{
76 return GetFitFunction()->GetParError(1);
77}
78
79Double_t TAB3Peak::Width() const
80{
81 return GetFitFunction()->GetParameter("sigma") * GetFitFunction()->GetParameter("rel_sigma2");
82}
83
84Double_t TAB3Peak::Sigma() const
85{
86 return GetFitFunction()->GetParameter("sigma");
87}
88
89Double_t TAB3Peak::PeakFunction(Double_t* dim, Double_t* par)
90{
91 return OneHitPeakFunction(dim, par) + TwoHitPeakFunction(dim, par) + ThreeHitPeakFunction(dim, par);
92}
93
94Double_t TAB3Peak::OneHitPeakFunction(Double_t* dim, Double_t* par) // NOLINT(readability-non-const-parameter)
95{
96 Double_t x = dim[0]; // channel number used for fitting
97 Double_t height = par[0]; // height of photopeak
98 Double_t centroid = par[1]; // Peak Centroid of non skew gaus
99 Double_t sigma = par[2]; // standard deviation of gaussian
100
101 return height * TMath::Gaus(x, centroid, sigma);
102}
103
104Double_t TAB3Peak::TwoHitPeakFunction(Double_t* dim, Double_t* par) // NOLINT(readability-non-const-parameter)
105{
106 Double_t x = dim[0]; // channel number used for fitting
107 Double_t height = par[0]; // height of photopeak
108 Double_t centroid = par[1]; // Peak Centroid of non skew gaus
109 Double_t sigma = par[2]; // standard deviation of gaussian
110 Double_t rel_height = par[3]; // relative height of 2-hit peak
111 Double_t rel_sigma = par[4]; // relative sigma of 2-hit peak
112
113 return height * rel_height * TMath::Gaus(x, centroid, rel_sigma * sigma);
114}
115
116Double_t TAB3Peak::ThreeHitPeakFunction(Double_t* dim, Double_t* par) // NOLINT(readability-non-const-parameter)
117{
118 Double_t x = dim[0]; // channel number used for fitting
119 Double_t height = par[0]; // height of photopeak
120 Double_t centroid = par[1]; // Peak Centroid of non skew gaus
121 Double_t sigma = par[2]; // standard deviation of gaussian
122 Double_t rel_height = par[5]; // relative height of 2-hit peak
123 Double_t rel_sigma = par[6]; // relative sigma of 2-hit peak
124
125 return height * rel_height * TMath::Gaus(x, centroid, rel_sigma * sigma);
126}
127
128Double_t TAB3Peak::OneHitPeakOnGlobalFunction(Double_t* dim, Double_t* par)
129{
130 return OneHitPeakFunction(dim, par) + GetGlobalBackground()->EvalPar(dim, &par[GetFitFunction()->GetNpar()]);
131}
132
133Double_t TAB3Peak::TwoHitPeakOnGlobalFunction(Double_t* dim, Double_t* par)
134{
135 return TwoHitPeakFunction(dim, par) + GetGlobalBackground()->EvalPar(dim, &par[GetFitFunction()->GetNpar()]);
136}
137
138Double_t TAB3Peak::ThreeHitPeakOnGlobalFunction(Double_t* dim, Double_t* par)
139{
140 return ThreeHitPeakFunction(dim, par) + GetGlobalBackground()->EvalPar(dim, &par[GetFitFunction()->GetNpar()]);
141}
142
143Double_t TAB3Peak::BackgroundFunction(Double_t* dim, Double_t* par)
144{
145 Double_t x = dim[0]; // channel number used for fitting
146 Double_t height = par[0]; // height of photopeak
147 Double_t centroid = par[1]; // Peak Centroid of non skew gaus
148 Double_t sigma = par[2]; // standard deviation of gaussian
149 Double_t step = par[7]; // Size of the step function;
150
151 Double_t step_func = TMath::Abs(step) * height / 100.0 * TMath::Erfc((x - centroid) / (TMath::Sqrt(2.0) * sigma));
152
153 return step_func;
154}
155
156void TAB3Peak::DrawComponents(Option_t* opt)
157{
158 // We need to draw this on top of the global background. Probably easiest to make another temporary TF1?
159 if(GetGlobalBackground() == nullptr) { return; }
160
161 Double_t low = 0;
162 Double_t high = 0;
163 GetGlobalBackground()->GetRange(low, high);
164 if(fOneHitOnGlobal != nullptr) { fOneHitOnGlobal->Delete(); }
165 if(fTwoHitOnGlobal != nullptr) { fTwoHitOnGlobal->Delete(); }
166 if(fThreeHitOnGlobal != nullptr) { fThreeHitOnGlobal->Delete(); }
167 // Make a copy of the total function, and then tack on the global background parameters.
168 fOneHitOnGlobal = new TF1("draw_component1", this, &TAB3Peak::OneHitPeakOnGlobalFunction, low, high, GetFitFunction()->GetNpar() + GetGlobalBackground()->GetNpar(), "TAB3Peak", "OneHitPeakOnGlobalFunction");
169 fTwoHitOnGlobal = new TF1("draw_component2", this, &TAB3Peak::TwoHitPeakOnGlobalFunction, low, high, GetFitFunction()->GetNpar() + GetGlobalBackground()->GetNpar(), "TAB3Peak", "TwoHitPeakOnGlobalFunction");
170 fThreeHitOnGlobal = new TF1("draw_component2", this, &TAB3Peak::ThreeHitPeakOnGlobalFunction, low, high, GetFitFunction()->GetNpar() + GetGlobalBackground()->GetNpar(), "TAB3Peak", "ThreeHitPeakOnGlobalFunction");
171 for(int i = 0; i < GetFitFunction()->GetNpar(); ++i) {
172 fOneHitOnGlobal->SetParameter(i, GetFitFunction()->GetParameter(i));
173 fTwoHitOnGlobal->SetParameter(i, GetFitFunction()->GetParameter(i));
174 fThreeHitOnGlobal->SetParameter(i, GetFitFunction()->GetParameter(i));
175 }
176 for(int i = 0; i < GetGlobalBackground()->GetNpar(); ++i) {
177 fOneHitOnGlobal->SetParameter(i + GetFitFunction()->GetNpar(), GetGlobalBackground()->GetParameter(i));
178 fTwoHitOnGlobal->SetParameter(i + GetFitFunction()->GetNpar(), GetGlobalBackground()->GetParameter(i));
179 fThreeHitOnGlobal->SetParameter(i + GetFitFunction()->GetNpar(), GetGlobalBackground()->GetParameter(i));
180 }
181 // Draw a copy of this function
182 fOneHitOnGlobal->SetLineColor(GetFitFunction()->GetLineColor());
183 fTwoHitOnGlobal->SetLineColor(GetFitFunction()->GetLineColor());
184 fThreeHitOnGlobal->SetLineColor(GetFitFunction()->GetLineColor());
185 fOneHitOnGlobal->SetLineStyle(8);
186 fTwoHitOnGlobal->SetLineStyle(3);
187 fThreeHitOnGlobal->SetLineStyle(2);
188 fOneHitOnGlobal->Draw(opt);
189 fTwoHitOnGlobal->Draw(opt);
190 fThreeHitOnGlobal->Draw(opt);
191}
TF1 * fOneHitOnGlobal
Definition TAB3Peak.h:62
Double_t PeakFunction(Double_t *dim, Double_t *par) override
Definition TAB3Peak.cxx:89
Double_t Sigma() const override
Definition TAB3Peak.cxx:84
Double_t ThreeHitPeakOnGlobalFunction(Double_t *dim, Double_t *par)
Definition TAB3Peak.cxx:138
Double_t Centroid() const override
Definition TAB3Peak.cxx:69
void InitializeParameters(TH1 *hist, const double &rangeLow, const double &rangeHigh) override
Definition TAB3Peak.cxx:26
TF1 * fTwoHitOnGlobal
Definition TAB3Peak.h:63
Double_t Width() const override
Definition TAB3Peak.cxx:79
void DrawComponents(Option_t *opt="") override
Definition TAB3Peak.cxx:156
Double_t CentroidErr() const override
Definition TAB3Peak.cxx:74
TF1 * fThreeHitOnGlobal
Definition TAB3Peak.h:64
static Double_t OneHitPeakFunction(Double_t *dim, Double_t *par)
Definition TAB3Peak.cxx:94
static Double_t TwoHitPeakFunction(Double_t *dim, Double_t *par)
Definition TAB3Peak.cxx:104
static Double_t ThreeHitPeakFunction(Double_t *dim, Double_t *par)
Definition TAB3Peak.cxx:116
void InitParNames() override
Definition TAB3Peak.cxx:14
Double_t TwoHitPeakOnGlobalFunction(Double_t *dim, Double_t *par)
Definition TAB3Peak.cxx:133
Double_t OneHitPeakOnGlobalFunction(Double_t *dim, Double_t *par)
Definition TAB3Peak.cxx:128
Double_t BackgroundFunction(Double_t *dim, Double_t *par) override
Definition TAB3Peak.cxx:143
TF1 * GetFitFunction() const
Definition TSinglePeak.h:76
void SetPeakFunction(TF1 *function)
bool ParameterSetByUser(int par)
Double_t TotalFunction(Double_t *dim, Double_t *par)
void SetFitFunction(TF1 *function)
TF1 * GetGlobalBackground() const
Definition TSinglePeak.h:78
void SetListOfBGPar(const std::vector< bool > &list_of_bg_par)
Definition TSinglePeak.h:51