GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
Fit.hh
Go to the documentation of this file.
1#ifndef GAMR_SPECT_FIT_HH
2#define GAMR_SPECT_FIT_HH
3
4#include <vector>
5
6#include <RtypesCore.h>
7#include <TCanvas.h>
8#include <TF1.h>
9#include <TGraph.h>
10#include <TROOT.h>
11#include <TSpectrum.h>
12
13#include <toolkit/Peak.hh>
14#include "FitGuesses.hh"
15
16namespace GamR {
17 namespace Spect {
18
19 class PeakFit {
20 public:
21 struct PeakParams { //for storing indices
24 int iWidth;
25 int iSkewWidth; // Beta
26 int iSkewAmplitude; // R
27 int iSkewWidth2; // Beta for tail 2
28 int iSkewAmplitude2; // R for tail 2
29 int iStepAmplitude; // Step
30 };
31
44
45 private :
46 TF1 *fBackground;
47 TF1 *fTotal;
48 double fChi2;
49 double fLow;
50 double fHigh;
51 int fNData;
52 int fNPars;
53
54 TGraph *gWidth = NULL;
55 TGraph *gStep = NULL;
56 TGraph *gSkew = NULL;
57 TGraph *gSkewAmp = NULL;
58 TGraph *gSkew2 = NULL;
59 TGraph *gSkewAmp2 = NULL;
60
61 GamR::TK::PeakType fPeakType;
62 //std::vector<GamR::TK::Peak*> fPeaks;
63 std::map<std::string,GamR::TK::FPeak*> fPeaks;
64 std::map<int, std::string> fPeakKeys;
65 std::map<std::string,int> fPeakEnFix;
66
67 int iParamCount;
68
69 Parameters parameters;
70
71 //std::vector<PeakParams> fPeakParamInds;
72 std::map<std::string,PeakParams> fPeakParamInds;
73 GamR::TK::FPeak *FindPeak (int i) {
74 int k=0;
75 for (auto &peak : fPeaks) {
76 auto key = peak.first;
77 auto val = peak.second;
78 if (i==k) {
79 return val;
80 }
81 ++k;
82 }
83 return NULL;
84 }
85 public:
87
88 public:
89 PeakFit();
90 PeakFit(double Low, double High);
91 PeakFit(double Low, double High, PeakFitGuesses *FitGuesses);
92 GamR::TK::PeakType GetPeakType() { return fPeakType; }
93 TF1 *GetBackground() { return fBackground; }
94 TF1 *GetTotal() { return fTotal; }
95 double GetChi2() { return fChi2; }
96 double GetLow() { return fLow; }
97 double GetHigh() { return fHigh; }
98 size_t GetNPeaks() { return fPeaks.size(); }
99
100 int GetParamCount() { return iParamCount ; }
101 //option flags
102 int GetFixWidths() { return parameters.iFixWidths ; }
103 int GetQuadBack() { return parameters.iQuadBack ; }
104 int GetStep() { return parameters.iStep ; }
105 int GetTails1() { return parameters.iTails1 ; }
106 int GetTails2() { return parameters.iTails2 ; }
107
108 void SetParamCount(int i) { iParamCount = i; }
109 //option flags
110 void SetFixWidths(int i) { parameters.iFixWidths = i; }
111 void SetQuadBack(int i) { parameters.iQuadBack = i; }
112 void SetStep(int i) { parameters.iStep = i; }
113 void SetTails1(int i) { parameters.iTails1 = i; }
114 void SetTails2(int i) { parameters.iTails2 = i; }
115
116 void SetOpts(Option_t *option);
117 void SetBackground();
118 void SetPeaks(std::vector<double> Peaks);
119 void SetPeaks(std::map<std::string,double> Peaks);
121 void SetGuesses(TH1* hist, std::map<std::string,GamR::TK::FPeak*> Peaks, std::vector<std::string> FixPeaks={});
122 void SetGuesses(TH1* hist, std::map<std::string,double> Peaks, std::vector<std::string> FixPeaks={});
123 void SetIndices(std::map<std::string,GamR::TK::FPeak*> Peaks);
124 void SetLimit(std::string peakKey, std::string parameterKey, double lowerLimit, double upperLimit);
125
126 void SetLow(double Low) { fLow = Low; }
127 void SetHigh(double High) { fHigh = High; }
128 void SetChi2(double Chi2) { fChi2 = Chi2; }
129 void SetUp(TH1 *hist, std::map<std::string,double> Peaks, Option_t *option, std::vector<std::string> FixPeaks={}) {
130 SetOpts(option);
132 SetPeaks(Peaks);
133 SetIndices(fPeaks);
134 SetGuesses(hist, Peaks, FixPeaks);
135 }
136 void Fit(TH1* hist, Option_t *foption);
137 void SetResults(TH1 *hist);
138 void SetFunction();
139 void PrintResults();
140
141 double GetAmp(std::string peakKey);
142 double GetAmpError(std::string peakKey);
143 double GetCent(std::string peakKey);
144 double GetCentError(std::string peakKey);
145 double GetWidth(std::string peakKey);
146 double GetWidthError(std::string peakKey);
147 double GetArea(std::string peakKey) { return fPeaks[peakKey]->GetArea(); }
148 double GetAreaError(std::string peakKey) { return fPeaks[peakKey]->GetAreaError(); }
149
150 double GetAmp(int i) { return GetAmp(fPeakKeys[i]); }
151 double GetAmpError(int i) { return GetAmpError(fPeakKeys[i]); }
152 double GetCent(int i) { return GetCent(fPeakKeys[i]); }
153 double GetCentError(int i) { return GetCentError(fPeakKeys[i]); }
154 double GetWidth(int i) { return GetWidth(fPeakKeys[i]); }
155 double GetWidthError(int i) { return GetWidthError(fPeakKeys[i]); }
156 double GetArea(int i) { return GetArea(fPeakKeys[i]); }
157 double GetAreaError(int i) { return GetAreaError(fPeakKeys[i]); }
158
159 void SetAmp(std::string peakKey, double amp);
160 void SetAmpError(std::string peakKey, double error);
161 void SetCent(std::string peakKey, double centroid);
162 void SetCentError(std::string peakKey, double error);
163 void SetWidth(std::string peakKey, double width);
164 void SetWidthError(std::string peakKey, double error);
165
166 void SetAmp(int i, double amp) { SetAmp(fPeakKeys[i], amp); }
167 void SetAmpError(int i, double error) { SetAmpError(fPeakKeys[i], error); }
168 void SetCent(int i, double centroid) { SetCent(fPeakKeys[i], centroid); }
169 void SetCentError(int i, double error) { SetCentError(fPeakKeys[i], error); }
170 void SetWidth(int i, double width) { SetWidth(fPeakKeys[i], width); }
171 void SetWidthError(int i, double error) { SetWidthError(fPeakKeys[i], error); }
172
173 void FixPeakEnergy(std::string peakKey) { fPeakEnFix[peakKey] = 1; }
174
175 void AddPeak(std::string peakKey, GamR::TK::FPeak* peak);
176 void AddPeak(std::string peakKey, GamR::TK::PeakType peaktype);
177 void AddPeak(std::string peakKey, GamR::TK::PeakType peaktype, double centroid);
178
179 void ToText(std::string filename, std::string delimiter=" ", int nPoints=1000);
180
181 TGraph *GetWidthGraph() { return gWidth; }
182 TGraph *GetStepGraph() { return gStep; }
183 TGraph *GetSkewAmpGraph() { return gSkewAmp; }
184 TGraph *GetSkewGraph() { return gSkew; }
185 TGraph *GetSkewAmp2Graph() { return gSkewAmp2; }
186 TGraph *GetSkew2Graph() { return gSkew2; }
187
188 GamR::TK::FPeak* Get(std::string peakKey) { return fPeaks[peakKey]; }
189 GamR::TK::FPeak* Get(int i) { return fPeaks[fPeakKeys[i]]; }
190 std::map<std::string,GamR::TK::FPeak*> GetPeaks() { return fPeaks; }
191
192 TF1 *GetPeakBG(std::string peakKey);
193 TF1 *GetPeakBG(int i) { return GetPeakBG(fPeakKeys[i]); }
194
195 TF1 *GetPeakStepBG(std::string peakKey);
196 TF1 *GetPeakStepBG(int i) { return GetPeakStepBG(fPeakKeys[i]); }
197
198 TF1 *GetPeakGaussBG(std::string peakKey);
199 TF1 *GetPeakGaussBG(int i) { return GetPeakGaussBG(fPeakKeys[i]); }
200
201 TF1 *GetFunc(std::string peakKey) { return fPeaks[peakKey]->GetFunc(); }
202 };
203
204 PeakFit *FitPeaks(TH1 *hist, double Low, double High, std::map<std::string,double> Peaks, Option_t *foption = "",
205 Option_t *option = "", std::vector<std::string> FixEnergies = {});
206 PeakFit *FitPeaks(TH1 *hist, double Low, double High, std::vector<double> Peaks, Option_t *foption = "",
207 Option_t *option = "", std::vector<std::string> FixEnergies = {});
208 PeakFit *FitPeaks(TCanvas *canvas = gPad->GetCanvas(), Option_t *foption = "", Option_t *option = "");
209
210 TF1 *FitGaussians(TH1 *hist, double Low, double High, std::map<std::string,double> Peaks, Option_t *foption = "",
211 Option_t *option = "");
212 TF1 *FitGaussians(TH1 *hist, double Low, double High, std::vector<double> Peaks, Option_t *foption = "",
213 Option_t *option = "");
214 TF1 *FitGaussians(TCanvas *canvas = gPad->GetCanvas(), Option_t *foption = "", Option_t *option = "");
215
216 TSpectrum *FindPeaks(TCanvas *canvas = gPad->GetCanvas(), double sigma = 2, Option_t *option="", double threshold = 0.05);
217
218 } /* namespace Spect */
219} /* namespace GamR */
220
221#endif // GAMR_SPECT_FIT_HH
double GetWidthError(int i)
Definition Fit.hh:155
void SetAmpError(int i, double error)
Definition Fit.hh:167
double GetArea(int i)
Definition Fit.hh:156
void SetWidth(std::string peakKey, double width)
Definition Fit.cc:1190
double GetArea(std::string peakKey)
Definition Fit.hh:147
void SetCent(int i, double centroid)
Definition Fit.hh:168
void ToText(std::string filename, std::string delimiter=" ", int nPoints=1000)
Definition Fit.cc:1558
TF1 * GetPeakBG(std::string peakKey)
Definition Fit.cc:1514
TF1 * GetPeakStepBG(std::string peakKey)
Definition Fit.cc:1543
void SetCentError(int i, double error)
Definition Fit.hh:169
double GetLow()
Definition Fit.hh:96
GamR::TK::PeakType GetPeakType()
Definition Fit.hh:92
void SetPeaks(std::vector< double > Peaks)
Definition Fit.cc:118
TF1 * GetBackground()
Definition Fit.hh:93
void SetOpts(Option_t *option)
Definition Fit.cc:39
TF1 * GetPeakBG(int i)
Definition Fit.hh:193
TF1 * GetPeakStepBG(int i)
Definition Fit.hh:196
TGraph * GetSkewAmpGraph()
Definition Fit.hh:183
void PrintResults()
Definition Fit.cc:746
TGraph * GetSkewGraph()
Definition Fit.hh:184
PeakFitGuesses * fFitGuesses
Definition Fit.hh:86
void SetCentError(std::string peakKey, double error)
Definition Fit.cc:1186
void SetAmp(int i, double amp)
Definition Fit.hh:166
double GetAreaError(int i)
Definition Fit.hh:157
double GetChi2()
Definition Fit.hh:95
void SetLimit(std::string peakKey, std::string parameterKey, double lowerLimit, double upperLimit)
Definition Fit.cc:424
double GetAmpError(std::string peakKey)
Definition Fit.cc:1153
double GetCent(std::string peakKey)
Definition Fit.cc:1157
std::map< std::string, GamR::TK::FPeak * > GetPeaks()
Definition Fit.hh:190
void SetParamCount(int i)
Definition Fit.hh:108
void SetWidthError(std::string peakKey, double error)
Definition Fit.cc:1194
void SetTails1(int i)
Definition Fit.hh:113
TGraph * GetSkew2Graph()
Definition Fit.hh:186
void SetCent(std::string peakKey, double centroid)
Definition Fit.cc:1182
TF1 * GetFunc(std::string peakKey)
Definition Fit.hh:201
void SetHigh(double High)
Definition Fit.hh:127
GamR::TK::FPeak * Get(int i)
Definition Fit.hh:189
void SetAmp(std::string peakKey, double amp)
Definition Fit.cc:1174
double GetCent(int i)
Definition Fit.hh:152
void AddPeak(std::string peakKey, GamR::TK::FPeak *peak)
Definition Fit.cc:1095
void SetWidthError(int i, double error)
Definition Fit.hh:171
TGraph * GetWidthGraph()
Definition Fit.hh:181
void SetUp(TH1 *hist, std::map< std::string, double > Peaks, Option_t *option, std::vector< std::string > FixPeaks={})
Definition Fit.hh:129
int GetParamCount()
Definition Fit.hh:100
void SetTails2(int i)
Definition Fit.hh:114
TGraph * GetSkewAmp2Graph()
Definition Fit.hh:185
size_t GetNPeaks()
Definition Fit.hh:98
void SetWidth(int i, double width)
Definition Fit.hh:170
void FixPeakEnergy(std::string peakKey)
Definition Fit.hh:173
double GetAmp(int i)
Definition Fit.hh:150
double GetWidthError(std::string peakKey)
Definition Fit.cc:1169
TGraph * GetStepGraph()
Definition Fit.hh:182
void SetResults(TH1 *hist)
Definition Fit.cc:551
void SetFixWidths(int i)
Definition Fit.hh:110
TF1 * GetTotal()
Definition Fit.hh:94
double GetWidth(int i)
Definition Fit.hh:154
TF1 * GetPeakGaussBG(std::string peakKey)
Definition Fit.cc:1528
double GetCentError(int i)
Definition Fit.hh:153
GamR::TK::FPeak * Get(std::string peakKey)
Definition Fit.hh:188
double GetHigh()
Definition Fit.hh:97
void SetIndices(std::map< std::string, GamR::TK::FPeak * > Peaks)
Definition Fit.cc:137
double GetAmpError(int i)
Definition Fit.hh:151
void SetChi2(double Chi2)
Definition Fit.hh:128
double GetCentError(std::string peakKey)
Definition Fit.cc:1161
double GetAmp(std::string peakKey)
Definition Fit.cc:1149
void Fit(TH1 *hist, Option_t *foption)
Definition Fit.cc:433
void SetStep(int i)
Definition Fit.hh:112
void SetQuadBack(int i)
Definition Fit.hh:111
TF1 * GetPeakGaussBG(int i)
Definition Fit.hh:199
void SetBackground()
Definition Fit.cc:98
double GetAreaError(std::string peakKey)
Definition Fit.hh:148
void SetLow(double Low)
Definition Fit.hh:126
void SetAmpError(std::string peakKey, double error)
Definition Fit.cc:1178
double GetWidth(std::string peakKey)
Definition Fit.cc:1165
PeakType
Definition Peak.hh:33
TF1 * FitGaussians(TH1 *hist, double Low, double High, std::vector< double > Peaks, Option_t *foption, Option_t *option)
Definition Fit.cc:1580
TSpectrum * FindPeaks(TCanvas *canvas, double sigma, Option_t *option, double threshold)
Definition Fit.cc:1593
GamR::Spect::PeakFit * FitPeaks(TH1 *hist, double Low, double High, std::vector< double > Peaks, Option_t *foption, Option_t *option, std::vector< std::string > FixPeaks)
Definition Fit.cc:787
Definition Gain.cc:19