CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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. / fSigma_ped;
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  else
135  un_sur_sigma = 1.;
136 
137  double func, delta;
138  // Loop on iterations
139  for (int iter = 0; iter < fNb_iter; iter++) {
140  // initialization inside iteration loop !
141  chi2 = 0.;
142  double d11 = 0.;
143  double d12 = 0.;
144  double d22 = 0.;
145  double z1 = 0.;
146  double z2 = 0.;
147  fFunc_max += variation_func_max;
148  fTim_max += variation_tim_max;
149  int nsamp_used = 0;
150  //
151 
152  // Then we loop on samples to be fitted
153 
154  for (int i = num_fit_min; i < num_fit_max + 1; i++) {
155  // calculate function to be fitted
156 
157  func = Electronic_shape((double)i);
158 
159  // then calculate derivatives of function to be fitted
160  double dt = (double)i - fTim_max;
161  double alpha_beta = fAlpha * fBeta;
162 
163  if (dt > -alpha_beta) {
164  double dt_sur_beta = dt / fBeta;
165 
166  double variable = (double)1. + dt / alpha_beta;
167  double expo = TMath::Exp(-dt_sur_beta);
168 
169  double puissance = TMath::Power(variable, fAlpha);
170  d_alpha = un_sur_sigma * puissance * expo;
171  d_beta = fFunc_max * d_alpha * dt_sur_beta / (alpha_beta * variable);
172  } else {
173  continue;
174  }
175 
176  nsamp_used++; // number of samples used inside the fit
177  // compute matrix elements D (symetric --> d12 = d21 )
178  d11 += d_alpha * d_alpha;
179  d12 += d_alpha * d_beta;
180  d22 += d_beta * d_beta;
181  // compute delta
182  delta = (adc_to_fit[i] - func) * un_sur_sigma;
183  // compute vector elements Z
184  z1 += delta * d_alpha;
185  z2 += delta * d_beta;
186  chi2 += delta * delta;
187  } // end of loop on samples
188  double denom = d11 * d22 - d12 * d12;
189  if (denom == 0.) {
190  //printf( "attention denom = 0 signal pas fitte \n") ;
191  return 101;
192  }
193  if (nsamp_used < 3) {
194  //printf( "Attention nsamp = %d ---> no function fit provided \n",nsamp_used) ;
195  return 102;
196  }
197  // compute variations of parameters fAmp_max and fTim_max
198  variation_func_max = (z1 * d22 - z2 * d12) / denom;
199  variation_tim_max = (-z1 * d12 + z2 * d11) / denom;
200  chi2 = chi2 / ((double)nsamp_used - 2.);
201  } // end of loop on iterations
202  // results of the fit are calculated
203  fAmp_fitted_max = fFunc_max + variation_func_max;
204  fTim_fitted_max = fTim_max + variation_tim_max;
205  //
206  return chi2;
207 }
208 
209 //-----------------------------------------------------------------------
210 //----------------------------------------------------------------------
211 
213  // electronic function (from simulation) to fit ECAL pulse shape
214  double func_electronic, dtsbeta, variable, puiss;
215  double albet = fAlpha * fBeta;
216  if (albet <= 0)
217  return ((Double_t)0.);
218  double dt = tim - fTim_max;
219  if (dt > -albet) {
220  dtsbeta = dt / fBeta;
221  variable = 1. + dt / albet;
222  puiss = TMath::Power(variable, fAlpha);
223  func_electronic = fFunc_max * puiss * TMath::Exp(-dtsbeta);
224  } else
225  func_electronic = 0.;
226  //
227  return func_electronic;
228 }
229 void PulseFitWithFunction::Fit_parab(Double_t *ampl, Int_t nmin, Int_t nmax, Double_t *parout) {
230  /* Now we calculate the parabolic adjustement in order to get */
231  /* maximum and time max */
232 
233  double denom, dt, amp1, amp2, amp3;
234  double ampmax = 0.;
235  int imax = 0;
236  int k;
237  /*
238  */
239  for (k = nmin; k < nmax; k++) {
240  //printf("ampl[%d]=%f\n",k,ampl[k]);
241  if (ampl[k] > ampmax) {
242  ampmax = ampl[k];
243  imax = k;
244  }
245  }
246  amp1 = ampl[imax - 1];
247  amp2 = ampl[imax];
248  amp3 = ampl[imax + 1];
249  denom = 2. * amp2 - amp1 - amp3;
250  /* */
251  if (denom > 0.) {
252  dt = 0.5 * (amp3 - amp1) / denom;
253  } else {
254  //printf("denom =%f\n",denom) ;
255  dt = 0.5;
256  }
257  /* */
258  /* ampmax correspond au maximum d'amplitude parabolique et dt */
259  /* decalage en temps par rapport au sample maximum soit k + dt */
260 
261  parout[0] = amp2 + (amp3 - amp1) * dt * 0.25;
262  parout[1] = (double)imax + dt;
263  parout[2] = (double)imax;
264  parout[3] = ampmax;
265  return;
266 }
float dt
Definition: AMPTWrapper.h:136
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t Func __host__ __device__ V int Func func
void Fit_parab(double *, int, int, double *)
Quality *__restrict__ uint16_t nmin
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