GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TSinglePeak.cxx
Go to the documentation of this file.
1#include "TSinglePeak.h"
2#include "TClass.h"
3
4bool TSinglePeak::IsBackgroundParameter(const Int_t& par) const
5{
6 try {
7 return fListOfBGPars.at(par);
8 } catch(const std::out_of_range& oor) {
9 std::cerr << "Parameter not in list: " << par << std::endl;
10 return true;
11 }
12 return true; // never gets here...appeals to some compilers.
13}
14
15bool TSinglePeak::IsPeakParameter(const Int_t& par) const
16{
17 return !IsBackgroundParameter(par);
18}
19
21{
22 if(fTotalFunction != nullptr) {
23 return fTotalFunction->GetNpar();
24 }
25 return 0;
26}
27
29{
30 if(fBackgroundFunction == nullptr) {
31 fBackgroundFunction = new TF1("peak_bg", this, &TSinglePeak::BackgroundFunction, 0, 1, fTotalFunction->GetNpar(), "TSinglePeak", "BackgroundFunction");
32 fBackgroundFunction->SetLineStyle(9);
33 }
35}
36
37void TSinglePeak::Print(Option_t*) const
38{
39 std::cout << IsA()->GetName() << ":" << std::endl;
40 std::cout << "Centroid = " << std::fixed << Centroid() << " +/- " << CentroidErr() << std::endl;
41 std::cout << "Area = " << Area() << " +/- " << AreaErr() << std::endl;
42 std::cout << std::endl;
43}
44
46{
47 if(fTotalFunction != nullptr) {
48 for(int i = 0; i < fTotalFunction->GetNpar(); ++i) {
49 std::cout << i << "/" << fTotalFunction->GetParName(i) << " = " << fTotalFunction->GetParameter(i) << " ";
50 }
51 } else {
52 std::cout << "no total function ";
53 }
54}
55
56Double_t TSinglePeak::TotalFunction(Double_t* dim, Double_t* par)
57{
58 return PeakFunction(dim, par) + BackgroundFunction(dim, par);
59}
60
62{
63 fPeakFunction->SetParameters(fTotalFunction->GetParameters());
64}
65
67{
68 fBackgroundFunction->SetParameters(fTotalFunction->GetParameters());
69}
70
72{
73 /// This behaves like the draw function except each daughter class decides how to break the draw into multiple components.
74 /// This means that we should delegate this task to the daughter class.
75}
76
78{
79 /// Return the full width at half-maximum.
80 if(fPeakFunction == nullptr) {
81 std::cerr << __PRETTY_FUNCTION__ << ": peak function (" << fPeakFunction << ") is null" << std::endl; // NOLINT(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-bounds-array-to-pointer-decay)
82 return 0.;
83 }
85 double low = Centroid() - 10. * Sigma();
86 double high = Centroid() + 10. * Sigma();
87 auto maxX = fPeakFunction->GetMaximumX(low, high);
88 auto max = fPeakFunction->Eval(maxX);
89 return (fPeakFunction->GetX(max / 2., maxX, maxX + 10. * Sigma()) - fPeakFunction->GetX(max / 2., maxX - 10. * Sigma(), maxX));
90}
91
93{
94 /// Return the uncertainty in the full width at half-maximum.
95 /// Since it is complicated to do this without knowledge of the underlying fit function, we simply assume that the (relative) uncertainty in the FWHM is the same as the uncertainty in sigma.
96 return FWHM() / Sigma() * SigmaErr();
97}
98
99Double_t TSinglePeak::PeakOnGlobalFunction(Double_t* dim, Double_t* par)
100{
101 if(fGlobalBackground == nullptr) { return 0.0; }
102
103 return PeakFunction(dim, par) + fGlobalBackground->EvalPar(dim, &par[fTotalFunction->GetNpar()]);
104}
105
106void TSinglePeak::Draw(Option_t* opt)
107{
108 // We need to draw this on top of the global background. Probably easiest to make another temporary TF1?
109 if(fGlobalBackground == nullptr) { return; }
110
111 Double_t low = 0.;
112 Double_t high = 0.;
113 fGlobalBackground->GetRange(low, high);
114 if(fPeakOnGlobal != nullptr) { fPeakOnGlobal->Delete(); }
115 // Make a copy of the total function, and then tack on the global background parameters.
116 fPeakOnGlobal = new TF1("draw_peak", this, &TSinglePeak::PeakOnGlobalFunction, low, high, fTotalFunction->GetNpar() + fGlobalBackground->GetNpar(), "TSinglePeak", "PeakOnGlobalFunction");
117 for(int i = 0; i < fTotalFunction->GetNpar(); ++i) {
118 fPeakOnGlobal->SetParameter(i, fTotalFunction->GetParameter(i));
119 }
120 for(int i = 0; i < fGlobalBackground->GetNpar(); ++i) {
121 fPeakOnGlobal->SetParameter(i + fTotalFunction->GetNpar(), fGlobalBackground->GetParameter(i));
122 }
123 // Draw a copy of this function
124 fPeakOnGlobal->SetLineColor(fTotalFunction->GetLineColor());
125 fPeakOnGlobal->SetLineStyle(fTotalFunction->GetLineStyle());
126 fPeakOnGlobal->Draw(opt);
127}
128
130{
131 /// This function checks if a parameter or its limits have been set to a non-zero value.
132 /// In case that the user fixed a parameter to be zero, the limits are non-zero, so this case is covered as well.
133 Double_t lowLimit = 0.;
134 Double_t highLimit = 0.;
135
136 fTotalFunction->GetParLimits(par, lowLimit, highLimit);
137 double value = fTotalFunction->GetParameter(par);
138
139 // if either one of the three has been set, we return true
140 return (value != 0 || lowLimit != 0 || highLimit != 0);
141}
Int_t GetNParameters() const
virtual Double_t CentroidErr() const =0
void Draw(Option_t *opt="") override
void UpdateBackgroundParameters()
virtual Double_t FWHMErr()
bool ParameterSetByUser(int par)
virtual Double_t Sigma() const =0
TF1 * fGlobalBackground
virtual Double_t BackgroundFunction(Double_t *, Double_t *)
TF1 * fPeakFunction
TF1 * fTotalFunction
bool IsPeakParameter(const Int_t &par) const
std::vector< bool > fListOfBGPars
Double_t AreaErr() const
Definition TSinglePeak.h:58
virtual void DrawComponents(Option_t *opt="")
virtual Double_t Centroid() const =0
virtual Double_t PeakOnGlobalFunction(Double_t *dim, Double_t *par)
bool IsBackgroundParameter(const Int_t &par) const
virtual Double_t PeakFunction(Double_t *, Double_t *)
Double_t TotalFunction(Double_t *dim, Double_t *par)
virtual Double_t SigmaErr() const =0
virtual Double_t FWHM()
TF1 * fBackgroundFunction
virtual void PrintParameters() const
TF1 * fPeakOnGlobal
TF1 * GetBackgroundFunction()
void Print(Option_t *="") const override
void UpdatePeakParameters()
Double_t Area() const
Definition TSinglePeak.h:57