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