GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RecalEnergy.h
Go to the documentation of this file.
1 #ifndef GWRecal_h
2 #define GWRecal_h
3 
4 #include <ctime>
5 #include <csignal>
6 #include <fstream>
7 #include <sstream>
8 #include <iostream>
9 #include <iomanip>
10 #include <string>
11 #include <stdlib.h>
12 #include <cmath>
13 #include <memory.h>
14 
15 #include <vector>
16 #include <algorithm>
17 #include <functional>
18 
19 #include "TString.h"
20 
21 #include "ReadSpek.h"
22 #include "FitSpek.h"
23 
24 using namespace std;
25 
26 struct Fitted
27 {
28  Fitted() : NSubPeaks(0), BgdOff(0), BgdSlope(0), BgFrom(0), BgTo(0), area(0), ampli(0), posi(0), fw05(0), fw01(0), fwhm(0), tailL(0), tailR(0), Lambda(0), Rho(0), S(0), eref(-1), good(false) {;}
29  int NSubPeaks;
30  double BgdOff;
31  double BgdSlope;
32  double BgFrom;
33  double BgTo;
34  double area;
35  double ampli;
36  double posi;
37  double fw05;
38  double fw01;
39  double fwhm;
40  double tailL;
41  double tailR;
42  double Lambda;
43  double Rho;
44  double S;
45  int eref; // the corresponding calibration line, if good==true
46  bool good;
47 };
48 
49 struct NWA_t
50 {
51  NWA_t() : index(-1), width(0), ampli(0) {;}
52  int index;
53  float width;
54  float ampli;
55 };
56 
57 struct NLimits_t
58 {
59  NLimits_t() : index(-1), from(0), to(0) {;}
60  int index;
61  int from;
62  int to;
63 };
64 
66 {
67  bool operator()( Fitted elem1, Fitted elem2 ) {
68  return elem1.posi < elem2.posi;
69  }
70 };
71 struct largerArea
72 {
73  bool operator()( Fitted elem1, Fitted elem2 ) {
74  return elem1.area > elem2.area;
75  }
76 };
78 {
79  bool operator()( Fitted elem1, Fitted elem2 ) {
80  return elem1.ampli > elem2.ampli;
81  }
82 };
83 
84 class GWRecal
85 {
86 
87 public:
88 
89  GWRecal();
91 
92  void StartCalib();
93 
94 
95 public:
96 
97  void SetFileName(TString FileName){specName = FileName;}
98 
100  void SetSpectraFolder(TString name){fSpectraFolderName = name;}
101 
102  void SetNSpec(int N){specNN = N;} // number of subspectra to analyze [1]
103  void SetHistNumber(int HistNbr){specN0 = HistNbr;} //analysis starts from subspectrum [0]
104  void SetStep(int Step){specNs = Step;} //analysis proceeds incrementing subspectrum by [1]
105 
106  void SetChannelOffset(int Off){specOffset = Off;} //channel offset to subtract to the position of the peaks [0]
107  void SetGlobalChannelLimits(int ChFrom, int ChTo){specFromDef = ChFrom; specToDef = ChTo; nlim.clear();} //limit the search to this range in channels
108  void SetHistChannelLimits(int SpecNbr, float ChFrom, float ChTo);//limit the search to this range in channels for spectrum nn
109 
110  TString fSourceName;
111  void SetSource(TString SourceName){fSourceName = SourceName; AnalyseSources();}
112  void AnalyseSources();
113 
114  void AddPeak(double EPeak){Energies.push_back(EPeak);} //add this energy to the list of lines (can be given more than once)
115  void RemoveClosestPeakTo(double EPeak){Delendae.push_back(EPeak);} //remove the line closest to this energy from the list of lines
116  void SetRefPeak(double EPeak){refEner = EPeak;} //energy (keV) of the reference peak for extended printouts
117 
118  void SetGlobalPeaksLimits(float DefFWHM, float DefAmpli){specFWHMdef = DefFWHM; specAMPLdef = DefAmpli;nwa.clear();} //default fwhm and minmum amplitude for the peaksearch [10 5]
119  void SetHistPeaksLimits(int SpecNbr, float FWHM, float Ampli); //fwhm and minmum amplitude for spectrum nn
120 
121  void UseFlatBackGround(){fFlatBg = true; fAffineBg = false;} // Use a flat function background to fit the peaks
122  void UseAffineBackGround(){fAffineBg = true; fFlatBg = false;} // Use a affine function background to fit the peaks
123  void NoBackGround(){fAffineBg = false; fFlatBg = false;} // No background estimation to fit the peaks
124 
125  void UseFirstDerivativeSearch(){Dmode = 1;} // use the 1st-derivative search (default, always for 2-line sources)
126  void UseSecondDerivativeSearch(){Dmode = 2;} // use the 2nd-derivative search
127 
128  void UnsetLeftTail(){useTL = false;} // disable using the Left Tail in peak fit
129  void UnsetRightTail(){useTR = false;} // disable using the Right Tail in peak fit
130  void UseLinearCalib(){numZero++;} // add a fake peak at (0,0) to push calibration through origin
131  void UseAffineCalib(){doPoly1 = true;} // affine calibration y = offset + slope*x
132  void UseParabolicCalib(){doPoly1 = true; doPoly2 = true;} //parabolic calibration y = offset + slope*x + curv*x*x
133  void SetGain(float Gain){hGain = Gain;} //scaling factor for the slope [1]
134 
135  void PrintResForXTalk(int VerboseMod){modXtalk = VerboseMod;} //print results as when calculating xTalk coefficients mod==36/37/38
136  void SetVerbosityLevel(int Verb){Verbosity = Verb;} // verbosity bit0=fit_details, bit1=calib_details, bit2=more_calib_details [0]
137  void SetFullVerbosity(){Verbosity = 0xFFFF;} //Set full verbosity
138 
141 
143 
145 
146 public:
147 
148  vector<Fitted> Peaks;
149  vector<Fitted> GetFitResults(){return Peaks;}
150 
151  string specName;
152  string specFormat;
154  float *specData;
155  int specOffset; // subtracted immediately after peak fit
156 
157  ReadSpek Spek; // reading managed by this
158 
159  int specNN; // number of
160  int specN0; // from
161  int specNs; // step (todo)
162 
163  int specFromDef, specToDef;
164  int fCurrentspecFromDef, fCurrentspecToDef;
165  int *specFrom;
166  int *specTo;
167 
168  float specFWHMdef;
169  float specAMPLdef;
172  float *specFWHM;
173  float *specAMPL;
174 
175  FILE *testFP;
176  float *testData;
177  float testGain;
178  string testFileName;
179  string testSuff; // suffix for the output spectra
181 
182  vector<double> specPeaks;
183  vector<double> Energies;
184  vector<double> Delendae;
185 
186  double eBhead; // band head
187  double eBstep; // delta
188  int eBnumb; // numbero of peaks
189 
190  double refEner;
191  bool useTL;
192  bool useTR;
193 
194  bool fFlatBg;
195  bool fAffineBg;
196 
197  vector<NWA_t> nwa;
198  vector<NLimits_t> nlim;
199 
200  bool bTwoLines;
201  bool bOneLine;
202  bool doSlope;
203  bool doPoly1; // linear
204  bool doPoly2; // cubic
205  int numZero; // number of fake peaks at (0,0)
206  int modXtalk;
207 
208  bool useErrors ; // fit using position and energy errors
209  double errE;
210 
211  int Dmode; // peak search using first derivative
212  int xP_0, xP_1, xP_2, xP_3, xP_4, xP_5, xP_6;
213  float xP_minAmpl;
219 
221 
222  double slope, tshift;
223  double offset1, slope1; // should be generalized to polynomials
224  double offset2, slope2, curv2;
225 
226  float hGain;
227 
228  int Verbosity; // &1 PeakFit &2 CalibPeaks &4 CalibSort
229 
231 
232  // callback for SIGINT
233  void terminate(int /*sig*/) {
234  gterminate++;
235  if(gterminate > 3)
236  exit(EXIT_FAILURE);
237  }
238 
239  // functions of ReadATCA
240  void numparams (char *, int);
241  void initialize();
242  bool TestResults();
243  int PeakSearch1(float * data, int chA, int chB, float fwhm, float minAmpl, std::vector<double>&vPeaks);
244  int PeakSearch2(float * data, int chA, int chB, float fwhm, float minAmpl, std::vector<double>&vPeaks);
245  int LargePeaks1(float * data, int chA, int chB, float fwhm, float minAmpl, std::vector<double>&vPeaks, int maxNum);
246  void SmoothSpek(float *spek, int nchan, double sigma, int ndiff, float *& tSpek, int &start);
247  int xP_Next1(float *yVal, int yLen);
248  int xP_Next2(float *yVal, int yLen);
249  int FitPeaks(int verbose);
250  double ECalibration(int nspe, double& offset1, double& slope1_, double& offset2_, double& slope2_, double& curv2_, int verbose_); // energy calibration from position-energy pair(s)
251  double TCalibration(); // shift of the largest peak from its reference position (to be done)
252  bool InvertMatrix3(const double m[9], double invOut[9]);
253  double Calibrated(double x);
254 
255  clock_t startTime, stopTime;
256 };
257 
258 #endif
void UseFlatBackGround()
Definition: RecalEnergy.h:121
int numZero
Definition: RecalEnergy.h:205
float width
Definition: RecalEnergy.h:53
double fwhm
Definition: RecalEnergy.h:39
double tshift
Definition: RecalEnergy.h:222
void NoBackGround()
Definition: RecalEnergy.h:123
void UseAffineCalib()
Definition: RecalEnergy.h:131
string specFormat
Definition: RecalEnergy.h:152
void UseFirstDerivativeSearch()
Definition: RecalEnergy.h:125
void SetRefPeak(double EPeak)
Definition: RecalEnergy.h:116
bool useTR
Definition: RecalEnergy.h:192
int Dmode
Definition: RecalEnergy.h:211
double slope2
Definition: RecalEnergy.h:224
double BgFrom
Definition: RecalEnergy.h:32
bool doPoly2
Definition: RecalEnergy.h:204
int fCurrentspecToDef
Definition: RecalEnergy.h:164
bool useTL
Definition: RecalEnergy.h:191
int specNN
Definition: RecalEnergy.h:159
void AddPeak(double EPeak)
Definition: RecalEnergy.h:114
double errE
Definition: RecalEnergy.h:209
void terminate(int)
Definition: RecalEnergy.h:233
TString fSpectraFolderName
Definition: RecalEnergy.h:99
int specToDef
Definition: RecalEnergy.h:163
double posi
Definition: RecalEnergy.h:36
clock_t stopTime
Definition: RecalEnergy.h:255
void SetVerbosityLevel(int Verb)
Definition: RecalEnergy.h:136
double slope1
Definition: RecalEnergy.h:223
bool bOneLine
Definition: RecalEnergy.h:201
double BgTo
Definition: RecalEnergy.h:33
bool useErrors
Definition: RecalEnergy.h:208
string testFileName
Definition: RecalEnergy.h:178
double tailL
Definition: RecalEnergy.h:40
float fCurrentspecFWHMdef
Definition: RecalEnergy.h:170
int specNs
Definition: RecalEnergy.h:161
double BgdOff
Definition: RecalEnergy.h:30
bool good
Definition: RecalEnergy.h:46
int specOffset
Definition: RecalEnergy.h:155
float specAMPLdef
Definition: RecalEnergy.h:169
double eBstep
Definition: RecalEnergy.h:187
float specFWHMdef
Definition: RecalEnergy.h:168
double area
Definition: RecalEnergy.h:34
FILE * testFP
Definition: RecalEnergy.h:175
int Verbosity
Definition: RecalEnergy.h:228
int xP_6
Definition: RecalEnergy.h:212
void SetSource(TString SourceName)
Definition: RecalEnergy.h:111
void SetGain(float Gain)
Definition: RecalEnergy.h:133
void UnsetRightTail()
Definition: RecalEnergy.h:129
bool fFlatBg
Definition: RecalEnergy.h:194
bool doPoly1
Definition: RecalEnergy.h:203
int * specFrom
Definition: RecalEnergy.h:165
string testSuff
Definition: RecalEnergy.h:179
Fitted()
Definition: RecalEnergy.h:28
int modXtalk
Definition: RecalEnergy.h:206
float * testData
Definition: RecalEnergy.h:176
void SetFullVerbosity()
Definition: RecalEnergy.h:137
void SetGlobalChannelLimits(int ChFrom, int ChTo)
Definition: RecalEnergy.h:107
bool fAffineBg
Definition: RecalEnergy.h:195
double fw01
Definition: RecalEnergy.h:38
int index
Definition: RecalEnergy.h:52
int xP_nMinWidthP2
Definition: RecalEnergy.h:218
float * specFWHM
Definition: RecalEnergy.h:172
int xP_nMinWidthP1
Definition: RecalEnergy.h:217
int eref
Definition: RecalEnergy.h:45
ReadSpek Spek
Definition: RecalEnergy.h:157
double S
Definition: RecalEnergy.h:44
~GWRecal()
Definition: RecalEnergy.h:90
bool testResults
Definition: RecalEnergy.h:180
void RemoveClosestPeakTo(double EPeak)
Definition: RecalEnergy.h:115
float testGain
Definition: RecalEnergy.h:177
vector< double > specPeaks
Definition: RecalEnergy.h:182
void UseSecondDerivativeSearch()
Definition: RecalEnergy.h:126
void SetChannelOffset(int Off)
Definition: RecalEnergy.h:106
void SetFileName(TString FileName)
Definition: RecalEnergy.h:97
float xP_minAmpl
Definition: RecalEnergy.h:213
TString fSourceName
Definition: RecalEnergy.h:110
double Rho
Definition: RecalEnergy.h:43
CFitSpek * m_pCFit
Definition: RecalEnergy.h:220
double eBhead
Definition: RecalEnergy.h:186
float xP_fThreshCFA
Definition: RecalEnergy.h:214
void SetNSpec(int N)
Definition: RecalEnergy.h:102
int gterminate
Definition: RecalEnergy.h:230
float * specAMPL
Definition: RecalEnergy.h:173
void UnsetLeftTail()
Definition: RecalEnergy.h:128
bool operator()(Fitted elem1, Fitted elem2)
Definition: RecalEnergy.h:79
void SetGlobalPeaksLimits(float DefFWHM, float DefAmpli)
Definition: RecalEnergy.h:118
void PrintResForXTalk(int VerboseMod)
Definition: RecalEnergy.h:135
vector< double > Energies
Definition: RecalEnergy.h:183
bool operator()(Fitted elem1, Fitted elem2)
Definition: RecalEnergy.h:73
int NSubPeaks
Definition: RecalEnergy.h:29
int xP_nMinWidthN
Definition: RecalEnergy.h:216
double fw05
Definition: RecalEnergy.h:37
vector< double > Delendae
Definition: RecalEnergy.h:184
vector< Fitted > Peaks
void SetFormat(string type, int nchan){specFormat = type; specLength = nchan;} // format of the data ...
Definition: RecalEnergy.h:148
vector< Fitted > GetFitResults()
Definition: RecalEnergy.h:149
bool doSlope
Definition: RecalEnergy.h:202
NWA_t()
Definition: RecalEnergy.h:51
void UseParabolicCalib()
Definition: RecalEnergy.h:132
string specName
Definition: RecalEnergy.h:151
float fCurrentspecAMPLdef
Definition: RecalEnergy.h:171
void SetSpectraFolder(TString name)
Definition: RecalEnergy.h:100
double refEner
Definition: RecalEnergy.h:190
void UseLinearCalib()
Definition: RecalEnergy.h:130
void SetHistNumber(int HistNbr)
Definition: RecalEnergy.h:103
int eBnumb
Definition: RecalEnergy.h:188
vector< NWA_t > nwa
Definition: RecalEnergy.h:197
int specN0
Definition: RecalEnergy.h:160
float hGain
Definition: RecalEnergy.h:226
void UseAffineBackGround()
Definition: RecalEnergy.h:122
bool bTwoLines
Definition: RecalEnergy.h:200
double Lambda
Definition: RecalEnergy.h:42
int xP_nMinWidthP
Definition: RecalEnergy.h:215
void SetStep(int Step)
Definition: RecalEnergy.h:104
double ampli
Definition: RecalEnergy.h:35
double BgdSlope
Definition: RecalEnergy.h:31
int * specTo
Definition: RecalEnergy.h:166
float * specData
Definition: RecalEnergy.h:154
float ampli
Definition: RecalEnergy.h:54
int specLength
Definition: RecalEnergy.h:153
double tailR
Definition: RecalEnergy.h:41
bool operator()(Fitted elem1, Fitted elem2)
Definition: RecalEnergy.h:67
vector< NLimits_t > nlim
Definition: RecalEnergy.h:198