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