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
92Double_t TSinglePeak::PeakOnGlobalFunction(Double_t* dim, Double_t* par)
93{
94 if(fGlobalBackground == nullptr) { return 0.0; }
95
96 return PeakFunction(dim, par) + fGlobalBackground->EvalPar(dim, &par[fTotalFunction->GetNpar()]);
97}
98
99void TSinglePeak::Draw(Option_t* opt)
100{
101 // We need to draw this on top of the global background. Probably easiest to make another temporary TF1?
102 if(fGlobalBackground == nullptr) { return; }
103
104 Double_t low = 0.;
105 Double_t high = 0.;
106 fGlobalBackground->GetRange(low, high);
107 if(fPeakOnGlobal != nullptr) { fPeakOnGlobal->Delete(); }
108 // Make a copy of the total function, and then tack on the global background parameters.
109 fPeakOnGlobal = new TF1("draw_peak", this, &TSinglePeak::PeakOnGlobalFunction, low, high, fTotalFunction->GetNpar() + fGlobalBackground->GetNpar(), "TSinglePeak", "PeakOnGlobalFunction");
110 for(int i = 0; i < fTotalFunction->GetNpar(); ++i) {
111 fPeakOnGlobal->SetParameter(i, fTotalFunction->GetParameter(i));
112 }
113 for(int i = 0; i < fGlobalBackground->GetNpar(); ++i) {
114 fPeakOnGlobal->SetParameter(i + fTotalFunction->GetNpar(), fGlobalBackground->GetParameter(i));
115 }
116 // Draw a copy of this function
117 fPeakOnGlobal->SetLineColor(fTotalFunction->GetLineColor());
118 fPeakOnGlobal->SetLineStyle(fTotalFunction->GetLineStyle());
119 fPeakOnGlobal->Draw(opt);
120}
121
123{
124 /// This function checks if a parameter or its limits have been set to a non-zero value.
125 /// In case that the user fixed a parameter to be zero, the limits are non-zero, so this case is covered as well.
126 Double_t lowLimit = 0.;
127 Double_t highLimit = 0.;
128
129 fTotalFunction->GetParLimits(par, lowLimit, highLimit);
130 double value = fTotalFunction->GetParameter(par);
131
132 // if either one of the three has been set, we return true
133 return (value != 0 || lowLimit != 0 || highLimit != 0);
134}
Int_t GetNParameters() const
virtual Double_t CentroidErr() const =0
void Draw(Option_t *opt="") override
void UpdateBackgroundParameters()
bool ParameterSetByUser(int par)
virtual Double_t Sigma() const =0
TF1 * fGlobalBackground
virtual Double_t BackgroundFunction(Double_t *, Double_t *)
Definition TSinglePeak.h:97
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 *)
Definition TSinglePeak.h:98
Double_t TotalFunction(Double_t *dim, Double_t *par)
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