GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FitSpek.h
Go to the documentation of this file.
1 #ifndef CFitSpek_h
2 #define CFitSpek_h
3 
4 // FitSpek.h: interface for the CFitSpek class.
5 //
7 
8 #if _MSC_VER > 1000
9 #pragma once
10 #endif // _MSC_VER > 1000
11 
12 #include <vector>
13 
14 // Numero di parametri del fit gaussiano
15 #define NUMFITPAR 8
16 
18 // Macro per l'indirizzamento C-style [i1][i2] di matrice dimensionata [..][l2]
19 // e allocata come array in un blocco contiguo di memoria.
20 // In C++ si puo' fare in modo piu' elegante, ma cosi' e' facile
21 #define M12(i1, i2, l2) (i1*l2+i2)
22 
23 const int p_Free = 0;
24 const int p_Linked = 1;
25 const int p_Fixed = 2;
26 
27 const int p_BNone = 0;
28 const int p_BLow = 1;
29 const int p_BHig = 2;
30 const int p_BBoth = 3;
31 
33 // In questa classe si usano ( in Curfit() ) puntatori a funzioni non statiche.
34 // La sintassi e' complicata
35 // double (CFitSpek::*pfun)(double); // nella dichiarazione si deve specificare classe e segnatura
36 // pfun = ExpFunc; // niente da segnalare nella assegnazione
37 // double aa = ExpFunc(10.); // la funzione usata di per se
38 // double bb = (*this.*pfun)(10.); // usata come puntatore, ci si deve accedere attraverso un oggetto !!
39 // double bb = (this->*pfun)(10.); // ""
40 
41 const double m_pi = 4.*atan(1.);
42 const double m_sqrt2 = sqrt(2.);
43 const double sqrt2pi = sqrt(8.*atan(1.));
44 const double s2fwhm = sqrt(8.*log(2.));
45 const double s2fwtm = sqrt(8.*log(10.));
46 
47 class CFitSpek
48 {
49 public:
50  CFitSpek();
51  virtual ~CFitSpek();
52 
53 public:
54  void Reset();
55  bool Valid() {return m_bValid;}
56  bool Function() {return m_bValid && m_bFunct;}
57  bool Background() {return m_bValid && m_bBckgr;}
58 
59  double Chi2() {return (m_bValid) ? m_dChi2 : -1.;}
60 
61  int From() {return (m_bValid) ? m_nChanA : 0;}
62  int To() {return (m_bValid) ? m_nChanB : 0;}
63 
64  int BgFrom() {return (m_bValid) ? m_nBgChanA : 0;}
65  int BgTo() {return (m_bValid) ? m_nBgChanB : 0;}
66 
67  int Npeaks() {return (m_bValid) ? m_nNpeaks : 0;}
68 
69  bool BackFlat() {return m_bFlat;}
70  bool BackSlope() {return m_bSlope;}
71 
72  double Back0 () {return (m_bBckgr) ? m_dBack0 : 0.;}
73  double Back0Err() {return (m_bBckgr) ? m_dBack0Err : 0.;}
74 
75  double Back1 () {return (m_bBckgr) ? m_dBack1 : 0.;}
76  double Back1Err() {return (m_bBckgr) ? m_dBack1Err : 0.;}
77 
78  double BackL1 () {return (m_bBckgr) ? m_dBackL1 : 0.;}
79  double BackL2 () {return (m_bBckgr) ? m_dBackL2 : 0.;}
80 
81  double Area (int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pRes[np].Area : 0.;}
82  double AreaErr(int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pErr[np].Area : 0.;}
83 
84  double Amplitude (int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pRes[np].Ampli : 0.;}
85  double AmplitudeErr(int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pErr[np].Ampli : 0.;}
86 
87  double Position (int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pRes[np].Posi : 0.;}
88  double PositionErr(int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pErr[np].Posi : 0.;}
89 
90  double Sigma (int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pRes[np].Sigma : 0.;}
91  double SigmaErr(int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pErr[np].Sigma : 0.;}
92 
93  double Fwhm (int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pRes[np].Fwhm : 0.;}
94  double FwhmErr(int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pErr[np].Fwhm : 0.;}
95 
96  double Fw05 (int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pRes[np].Fw05 : 0.;}
97  double Fw01 (int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pRes[np].Fw01 : 0.;}
98 
99  double W01L (int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pRes[np].W01L : 0.;}
100  double W01R (int np) {return (m_bGauss && np >=0 && np < m_nNpeaks) ? m_pRes[np].W01R : 0.;}
101 
102  double Step (int np) {return (m_bStep && np >=0 && np < m_nNpeaks) ? m_pRes[np].Step : 0.;}
103  double StepErr(int np) {return (m_bStep && np >=0 && np < m_nNpeaks) ? m_pErr[np].Step : 0.;}
104 
105  double TailLeft (int np) {return (m_bTailL && np >=0 && np < m_nNpeaks) ? m_pRes[np].TailL : 0.;}
106  double TailLeftErr (int np) {return (m_bTailL && np >=0 && np < m_nNpeaks) ? m_pErr[np].TailL : 0.;}
107 
108  double TailRight (int np) {return (m_bTailR && np >=0 && np < m_nNpeaks) ? m_pRes[np].TailR : 0.;}
109  double TailRightErr(int np) {return (m_bTailR && np >=0 && np < m_nNpeaks) ? m_pErr[np].TailR : 0.;}
110 
111  double ExpAmpli() {return (m_bExp) ? m_dExpAmpli : 0.;}
112  double ExpDecay() {return (m_bExp) ? m_dExpDecay : 0.;}
113 
114  double SinOmega () {return (m_bSin) ? m_pRes[0].Omega : 0.;}
115  double SinOmegaErr() {return (m_bSin) ? m_pErr[0].Omega : 0.;}
116 
117  double SinPhase () {return (m_bSin) ? m_pRes[0].Phase : 0.;}
118  double SinPhaseErr() {return (m_bSin) ? m_pErr[0].Phase : 0.;}
119 
120  double SinAmpli () {return (m_bSin) ? m_pRes[0].Ampli : 0.;}
121  double SinAmpliErr() {return (m_bSin) ? m_pErr[0].Ampli : 0.;}
122 
123  double FitFunc (double x) {return (m_bValid) ? (this->*m_pfFitFunc)(x) : 0;}
124  double FitFuncS(double x) {
125  if(m_bGauss) {
126  bool tTailL = m_bTailL; bool tTailR = m_bTailR;
127  m_bTailL = m_bTailR = false;
128  double res = (m_bValid) ? (this->*m_pfFitPeak)(x,0) + FitBack(x) : 0;
129  m_bTailL = tTailL; m_bTailR = tTailR;
130  return res;
131  }
132  else {
133  return (m_bValid) ? (this->*m_pfFitFunc)(x) : 0;
134  }
135  }
136  double FitBack(double x) {return (m_bBckgr) ? (this->*m_pfFitBack)(x) : 0;}
137  double FitPeak(double x, int n=0) {return (m_bFunct) ? (this->*m_pfFitPeak)(x, n) : 0;}
138 
139  bool CalcStrightLine(float *pSpek, int chanA, int chanB, int doSlope);
140  bool CalcPeakMoments(float *pSpek, int chanA, int chanB, int nChanBack);
141  int CalcGaussianFit(float *pSpek, int chanA, int chanB, std::vector<double>&vPeaks);
142  int CalcGaussianFit(float *pSpek, int chanA, int chanB); // one peak autolocated,
143  bool CalcExponential(float *pSpek, int chanA, int chanB, int bgA = -1, int bgB = -1);
144  int CalcSinusoid (float *pSpek, int chanA, int chanB, double period, bool bFlat, bool bSlope);
145 
146 public:
147  int GFitParNumber() {return NUMFITPAR;}
148  int GFitParB0 () {return 0;}
149  int GFitParB1 () {return 1;}
150  int GFitParAMP() {return 2;}
151  int GFitParPOS() {return 3;}
152  int GFitParSIG() {return 4;}
153  int GFitParSTP() {return 5;}
154  int GFitParTL () {return 6;}
155  int GFitParTR () {return 7;}
156  void GFitParGet(int *pP) {for(int ii = 0; ii < NUMFITPAR; ii++) pP[ii] = m_pGFitPar[ii];}
157  void GFitParSet(int *pP) {for(int ii = 0; ii < NUMFITPAR; ii++) m_pGFitPar[ii] = pP[ii] & 3;}
158  void GFitParDlg();
159 
160 public:
161 
162  class CFitRes
163  {
164  public:
165  double Ampli;
166  double Area;
167  double Posi;
168  double Sigma;
169  double Fwhm; // simply Sigma*s2fwhm
170  double Fw05; // FW at 1/2 considering tails
171  double Fw01; // FW at 1/10 considering tails
172  double W01L; // left width at 1/10 normalized to Fwhm/2
173  double W01R; // right width at 1/10 normalized to Fwhm/2
174  double Step;
175  double TailL;
176  double TailR;
177  double Omega;
178  double Phase;
179  CFitRes() : Ampli(0), Area(0), Posi(0), Sigma(0), Fwhm(0), Fw05(0), Fw01(0), W01L(0), W01R(0),
180  Step(0), TailL(0), TailR(0), Omega(0), Phase(0)
181  {
182  }
183  };
184 
185  class CFitPar
186  {
187  public:
188  double Value;
189  double Error;
190  double Valmin;
191  double Valmax;
192  int Status;
193  int Bounds;
194  int LinkTo;
195  int PackId;
196  CFitPar() : Value(0), Error(0), Valmin(0), Valmax(0),
197  Status(0), Bounds(0), LinkTo(0), PackId(0)
198  {
199  }
200  };
201 
202  // i membri puntatori alle funzioni del risultato del fit
203  double (CFitSpek::*m_pfFitFunc)(double);
204  double (CFitSpek::*m_pfFitBack)(double);
205  double (CFitSpek::*m_pfFitPeak)(double, int);
206 
207  // i membri puntatori alle funzioni del fit per Curfit()
208  double (CFitSpek::*m_pfFunct)(double, double *);
209  void (CFitSpek::*m_pfDeriv)(int, double *, double *, double &, double &);
210 
211 private:
212  double LineFunc(double xx);
213 
214  double ExpFunc(double xx);
215  double ExpPeak(double xx, int);
216 
217  double GFitFunc(double xx);
218  double GFitBack(double xx);
219  double GFitPeak(double xx, int nn);
220  double GFfunF(double xx, double *par);
221  void GFderF(int id, double *fpar, double *deriv, double &deltay, double &weight);
222  void GF_res(void);
223  double GF_erf(double);
224  double GF_y2w(int np, double yval, int side = 0); // -1=Left, 0=full, 1=Right
225 
226  double SFitFunc(double xx);
227  double SFitWave(double xx, int nn);
228  double SFfunF(double xx, double *par);
229  void SFderF(int id, double *fpar, double *deriv, double &deltay, double &weight);
230 
231  double Curfit(int ndat, int npar, int mpar);
232  double Chisqr(double *par);
233  void Manage(double *par, int type);
234  double Matinv(double *array, int nord, int mpar);
235 
236  bool m_bValid;
237  bool m_bFunct;
238  bool m_bBckgr;
239 
240  bool m_bLine;
241  bool m_bFlat;
242  bool m_bSlope;
243 
244  bool m_bGauss;
245  bool m_bStep;
246  bool m_bTailL;
247  bool m_bTailR;
248 
249  int m_nChanA;
250  int m_nChanB;
251  int m_nBgChanA;
252  int m_nBgChanB;
253  int m_nNpeaks;
254 
255  // control of Gauss Fit parameters
256  // [Bit1 enabled] [Bit2 all equal]
257  int m_pGFitPar[NUMFITPAR];
258 
259  int m_nNdat;
260  int m_nNpar;
261  int m_nFpar;
262  int m_nOffB;
263  int m_nOffN;
264 
265  double m_dChi2;
266 
267  double m_dBack0;
268  double m_dBack0Err;
269  double m_dBack1;
270  double m_dBack1Err;
271  double m_dBackL1;
272  double m_dBackL2;
273 
274  CFitRes *m_pRes;
275  CFitRes *m_pErr;
276 
277  bool m_bExp;
278  double m_dExpAmpli;
279  double m_dExpDecay;
280 
281  bool m_bSin;
282 
283  CFitPar *m_pPar; // the parameters of the fit
284  double *m_pDatVal; // the data to fit
285  double *m_pDatErr; // their error (1/sig2)
286 
287 };
288 
289 #endif // !defined(AFX_FITSPEK_H__F2A2E380_9934_11D5_B3CF_000000000000__INCLUDED_)
double StepErr(int np)
Definition: FitSpek.h:103
bool Background()
Definition: FitSpek.h:57
double TailLeftErr(int np)
Definition: FitSpek.h:106
int GFitParNumber()
Definition: FitSpek.h:147
double(CFitSpek::* m_pfFunct)(double, double *)
Definition: FitSpek.h:208
double(CFitSpek::* m_pfFitFunc)(double)
Definition: FitSpek.h:203
#define NUMFITPAR
Definition: FitSpek.h:15
double Position(int np)
Definition: FitSpek.h:87
int GFitParSIG()
Definition: FitSpek.h:152
const int p_BHig
Definition: FitSpek.h:29
double FitFuncS(double x)
Definition: FitSpek.h:124
double FitBack(double x)
Definition: FitSpek.h:136
double AmplitudeErr(int np)
Definition: FitSpek.h:85
bool Function()
Definition: FitSpek.h:56
double Back0()
Definition: FitSpek.h:72
void GFitParGet(int *pP)
Definition: FitSpek.h:156
double FitFunc(double x)
Definition: FitSpek.h:123
double(CFitSpek::* m_pfFitBack)(double)
Definition: FitSpek.h:204
const double m_pi
Definition: FitSpek.h:41
double BackL1()
Definition: FitSpek.h:78
const int p_Fixed
Definition: FitSpek.h:25
double TailLeft(int np)
Definition: FitSpek.h:105
double ExpAmpli()
Definition: FitSpek.h:111
const double m_sqrt2
Definition: FitSpek.h:42
int GFitParAMP()
Definition: FitSpek.h:150
int CalcGaussianFit(float *pSpek, int chanA, int chanB, std::vector< double > &vPeaks)
Definition: FitSpek.cpp:426
int BgFrom()
Definition: FitSpek.h:64
bool CalcStrightLine(float *pSpek, int chanA, int chanB, int doSlope)
Definition: FitSpek.cpp:112
int BgTo()
Definition: FitSpek.h:65
double W01R(int np)
Definition: FitSpek.h:100
int CalcSinusoid(float *pSpek, int chanA, int chanB, double period, bool bFlat, bool bSlope)
Definition: FitSpek.cpp:1712
double SigmaErr(int np)
Definition: FitSpek.h:91
const double s2fwtm
Definition: FitSpek.h:45
const int p_BBoth
Definition: FitSpek.h:30
int GFitParTR()
Definition: FitSpek.h:155
void(CFitSpek::* m_pfDeriv)(int, double *, double *, double &, double &)
Definition: FitSpek.h:209
double Fw01(int np)
Definition: FitSpek.h:97
double Sigma(int np)
Definition: FitSpek.h:90
int GFitParSTP()
Definition: FitSpek.h:153
double SinPhase()
Definition: FitSpek.h:117
bool CalcPeakMoments(float *pSpek, int chanA, int chanB, int nChanBack)
Definition: FitSpek.cpp:248
bool BackSlope()
Definition: FitSpek.h:70
void GFitParDlg()
Definition: FitSpek.cpp:72
bool BackFlat()
Definition: FitSpek.h:69
double SinPhaseErr()
Definition: FitSpek.h:118
int GFitParB0()
Definition: FitSpek.h:148
double PositionErr(int np)
Definition: FitSpek.h:88
double Back0Err()
Definition: FitSpek.h:73
int GFitParB1()
Definition: FitSpek.h:149
double SinOmegaErr()
Definition: FitSpek.h:115
int To()
Definition: FitSpek.h:62
double TailRightErr(int np)
Definition: FitSpek.h:109
double FwhmErr(int np)
Definition: FitSpek.h:94
double Fw05(int np)
Definition: FitSpek.h:96
double BackL2()
Definition: FitSpek.h:79
double Area(int np)
Definition: FitSpek.h:81
bool Valid()
Definition: FitSpek.h:55
const int p_Linked
Definition: FitSpek.h:24
double FitPeak(double x, int n=0)
Definition: FitSpek.h:137
double Back1()
Definition: FitSpek.h:75
int GFitParPOS()
Definition: FitSpek.h:151
double Step(int np)
Definition: FitSpek.h:102
double Back1Err()
Definition: FitSpek.h:76
double AreaErr(int np)
Definition: FitSpek.h:82
CFitSpek()
Definition: FitSpek.cpp:30
double SinAmpli()
Definition: FitSpek.h:120
double(CFitSpek::* m_pfFitPeak)(double, int)
Definition: FitSpek.h:205
const int p_BLow
Definition: FitSpek.h:28
virtual ~CFitSpek()
Definition: FitSpek.cpp:54
double SinOmega()
Definition: FitSpek.h:114
const int p_BNone
Definition: FitSpek.h:27
const double s2fwhm
Definition: FitSpek.h:44
double SinAmpliErr()
Definition: FitSpek.h:121
bool CalcExponential(float *pSpek, int chanA, int chanB, int bgA=-1, int bgB=-1)
Definition: FitSpek.cpp:1591
double Fwhm(int np)
Definition: FitSpek.h:93
const int p_Free
Definition: FitSpek.h:23
void Reset()
Definition: FitSpek.cpp:59
double Chi2()
Definition: FitSpek.h:59
void GFitParSet(int *pP)
Definition: FitSpek.h:157
double Amplitude(int np)
Definition: FitSpek.h:84
int GFitParTL()
Definition: FitSpek.h:154
int From()
Definition: FitSpek.h:61
double TailRight(int np)
Definition: FitSpek.h:108
const double sqrt2pi
Definition: FitSpek.h:43
int Npeaks()
Definition: FitSpek.h:67
double ExpDecay()
Definition: FitSpek.h:112
double W01L(int np)
Definition: FitSpek.h:99