CMS 3D CMS Logo

PulseFitWithFunction.cc
Go to the documentation of this file.
1 /*
2  * \class PulseFitWithFunction
3  *
4  * \author: Patrick Jarry - CEA/Saclay
5  */
6 
7 // File PulseFitWithFunction.cxx
8 // ===========================================================
9 // == ==
10 // == Class for a function fit method ==
11 // == ==
12 // == Date: July 17th 2003 ==
13 // == Author: Patrick Jarry ==
14 // == ==
15 // == ==
16 // ===========================================================
17 
19 
20 #include <iostream>
21 #include "TMath.h"
22 
23 //ClassImp(PulseFitWithFunction)
24 
25 // Constructor...
27  fNsamples = 0;
30 }
31 
32 // Destructor
34 
35 // Initialisation
36 
37 void PulseFitWithFunction::init(int n_samples, int samplb, int sampla, int niter, double alfa, double beta) {
38  //printf("\n");
39  //printf(" =========================================================\n");
40  //printf(" == Initialising the function fit method ==\n");
41  //printf(" == PulseFitWithFunction::init ==\n");
42  //printf(" == ==\n");
43 
44  fNsamples = n_samples;
45  fAlpha_laser = alfa;
46  fBeta_laser = beta;
47  fAlpha_beam = 0.98;
48  fBeta_beam = 2.04;
49  fNb_iter = niter;
50  fNum_samp_bef_max = samplb;
51  fNum_samp_after_max = sampla;
52  //printf(" == # samples used = %3d ==\n",fNsamples);
53  //printf(" == #sample before max= %1d and #sample after maximum= %1d ==\n",fNum_samp_bef_max,fNum_samp_after_max);
54  //printf(" == alpha= %5.4f beta= %5.4f ==\n",fAlpha_laser,fBeta_laser);
55  //printf(" =========================================================\n\n");
56  return;
57 }
58 
59 // Compute the amplitude using as input the Crystaldata
61  double parout[4]; // amp_max ;
62  //double amp_parab , tim_parab ;
63  double chi2;
64  //int imax ;
65  //
66  // first one has to get starting point first with parabolic fun// //
67  Fit_parab(&adc[0], 3, fNsamples, parout);
68  amp_parab = parout[0];
69  tim_parab = parout[1];
70  imax = (int)parout[2];
71  amp_max = parout[3];
72  //printf("amp_parab= %f tim_parab=%f amp_max=%f imax=%d\n",amp_parab,tim_parab,amp_max,imax);
74  //
75 
76  if (amp_parab < 1.) {
77  tim_parab = (double)imax;
79  }
80  //
84 
85  // here to fit maximum amplitude and time of arrival ...
86 
87  chi2 = Fit_electronic(0, &adc[0], 8.);
88  // adc is an array to be filled with samples
89  // 0 is for Laser (1 for electron)
90  // 8 is for sigma of pedestals
91  // which (can be computed)
92 
93  // double amplitude = fAmp_fitted_max ; // amplitude fitted
94  // double time = fTim_fitted_max ; // time fitted
95 
96  return chi2; // return amplitude fitted
97 }
98 
99 //-----------------------------------------------------------------------
100 //----------------------------------------------------------------------
101 
102 /*************************************************/
103 double PulseFitWithFunction::Fit_electronic(int data, double *adc_to_fit, double sigmas_sample) {
104  // fit electronic function from simulation
105  // parameters fAlpha and fBeta are fixed and fit is providing
106  // the maximum amplitude ( fAmp_fitted_max ) and the time of
107  // the maximum amplitude ( fTim_fitted_max)
108  // initialization of parameters
109  double chi2 = 0;
110  double d_alpha, d_beta;
111  // first initialize parameters fAlpha and fBeta ( depending of beam or laser)
113  fBeta = fBeta_laser;
114  if (data == 1) {
116  fBeta = fBeta_beam;
117  }
118  //
119  fAmp_fitted_max = 0.;
120  fTim_fitted_max = 0.;
121  double un_sur_sigma = 1.;
122  double variation_func_max = 0.;
123  double variation_tim_max = 0.;
124  //
125  if (fValue_tim_max > 20. || fValue_tim_max < 3.) {
127  }
128  int num_fit_min = (int)(fValue_tim_max - fNum_samp_bef_max);
129  int num_fit_max = (int)(fValue_tim_max + fNum_samp_after_max);
130  //
131 
132  if (sigmas_sample > 0.)
133  un_sur_sigma = 1. / sigmas_sample;
134 
135  double func, delta;
136  // Loop on iterations
137  for (int iter = 0; iter < fNb_iter; iter++) {
138  // initialization inside iteration loop !
139  chi2 = 0.;
140  double d11 = 0.;
141  double d12 = 0.;
142  double d22 = 0.;
143  double z1 = 0.;
144  double z2 = 0.;
145  fFunc_max += variation_func_max;
146  fTim_max += variation_tim_max;
147  int nsamp_used = 0;
148  //
149 
150  // Then we loop on samples to be fitted
151 
152  for (int i = num_fit_min; i < num_fit_max + 1; i++) {
153  // calculate function to be fitted
154 
155  func = Electronic_shape((double)i);
156 
157  // then calculate derivatives of function to be fitted
158  double dt = (double)i - fTim_max;
159  double alpha_beta = fAlpha * fBeta;
160 
161  if (dt > -alpha_beta) {
162  double dt_sur_beta = dt / fBeta;
163 
164  double variable = (double)1. + dt / alpha_beta;
165  double expo = TMath::Exp(-dt_sur_beta);
166 
167  double puissance = TMath::Power(variable, fAlpha);
168  d_alpha = un_sur_sigma * puissance * expo;
169  d_beta = fFunc_max * d_alpha * dt_sur_beta / (alpha_beta * variable);
170  } else {
171  continue;
172  }
173 
174  nsamp_used++; // number of samples used inside the fit
175  // compute matrix elements D (symetric --> d12 = d21 )
176  d11 += d_alpha * d_alpha;
177  d12 += d_alpha * d_beta;
178  d22 += d_beta * d_beta;
179  // compute delta
180  delta = (adc_to_fit[i] - func) * un_sur_sigma;
181  // compute vector elements Z
182  z1 += delta * d_alpha;
183  z2 += delta * d_beta;
184  chi2 += delta * delta;
185  } // end of loop on samples
186  double denom = d11 * d22 - d12 * d12;
187  if (denom == 0.) {
188  //printf( "attention denom = 0 signal pas fitte \n") ;
189  return 101;
190  }
191  if (nsamp_used < 3) {
192  //printf( "Attention nsamp = %d ---> no function fit provided \n",nsamp_used) ;
193  return 102;
194  }
195  // compute variations of parameters fAmp_max and fTim_max
196  variation_func_max = (z1 * d22 - z2 * d12) / denom;
197  variation_tim_max = (-z1 * d12 + z2 * d11) / denom;
198  chi2 = chi2 / ((double)nsamp_used - 2.);
199  } // end of loop on iterations
200  // results of the fit are calculated
201  fAmp_fitted_max = fFunc_max + variation_func_max;
202  fTim_fitted_max = fTim_max + variation_tim_max;
203  //
204  return chi2;
205 }
206 
207 //-----------------------------------------------------------------------
208 //----------------------------------------------------------------------
209 
211  // electronic function (from simulation) to fit ECAL pulse shape
212  double func_electronic, dtsbeta, variable, puiss;
213  double albet = fAlpha * fBeta;
214  if (albet <= 0)
215  return ((Double_t)0.);
216  double dt = tim - fTim_max;
217  if (dt > -albet) {
218  dtsbeta = dt / fBeta;
219  variable = 1. + dt / albet;
220  puiss = TMath::Power(variable, fAlpha);
221  func_electronic = fFunc_max * puiss * TMath::Exp(-dtsbeta);
222  } else
223  func_electronic = 0.;
224  //
225  return func_electronic;
226 }
227 void PulseFitWithFunction::Fit_parab(Double_t *ampl, Int_t nmin, Int_t nmax, Double_t *parout) {
228  /* Now we calculate the parabolic adjustement in order to get */
229  /* maximum and time max */
230 
231  double denom, dt, amp1, amp2, amp3;
232  double ampmax = 0.;
233  int imax = 0;
234  int k;
235  /*
236  */
237  for (k = nmin; k < nmax; k++) {
238  //printf("ampl[%d]=%f\n",k,ampl[k]);
239  if (ampl[k] > ampmax) {
240  ampmax = ampl[k];
241  imax = k;
242  }
243  }
244  amp1 = ampl[imax - 1];
245  amp2 = ampl[imax];
246  amp3 = ampl[imax + 1];
247  denom = 2. * amp2 - amp1 - amp3;
248  /* */
249  if (denom > 0.) {
250  dt = 0.5 * (amp3 - amp1) / denom;
251  } else {
252  //printf("denom =%f\n",denom) ;
253  dt = 0.5;
254  }
255  /* */
256  /* ampmax correspond au maximum d'amplitude parabolique et dt */
257  /* decalage en temps par rapport au sample maximum soit k + dt */
258 
259  parout[0] = amp2 + (amp3 - amp1) * dt * 0.25;
260  parout[1] = (double)imax + dt;
261  parout[2] = (double)imax;
262  parout[3] = ampmax;
263  return;
264 }
float dt
Definition: AMPTWrapper.h:136
void Fit_parab(double *, int, int, double *)
virtual void init(int, int, int, int, double, double)
double Fit_electronic(int, double *, double)
virtual double doFit(double *)
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
uint16_t *__restrict__ uint16_t const *__restrict__ adc