CMS 3D CMS Logo

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