00001
00002
00003
00004
00005
00006
00007
00008 #include <CalibCalorimetry/EcalLaserAnalyzer/interface/TSFit.h>
00009
00010 #include <cstdio>
00011 #include <cmath>
00012 #include <cstring>
00013
00014
00015
00016
00017
00018
00019
00020 TSFit::TSFit( int size, int size_sh ) {
00021 sdim = size;
00022 plshdim = size_sh;
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 }
00041
00042 void TSFit::set_params( int n_samples, int niter, int n_presmpl,
00043 int sample_min, int sample_max,
00044 double time_of_max, double chi2_max,
00045 int nsbm, int nsam ){
00046
00047
00048
00049 nbs = n_samples;
00050 nbr_iter_fit = niter;
00051 n_presamples = n_presmpl;
00052 iinf = sample_min;
00053 isup = sample_max;
00054 avtm = time_of_max;
00055 xki2_max = chi2_max;
00056 n_samples_bef_max = nsbm;
00057 n_samples_aft_max = nsam;
00058
00059 norme = 0.;
00060 alpha_th = 2.20;
00061 beta_th = 1.11;
00062
00063 int k;
00064
00065 for(k=0;k<=nbs;k++){
00066 sample_flag[k] = 0;
00067 }
00068
00069 for(k=sample_min;k<=sample_max;k++){
00070 sample_flag[k] = 1;
00071 }
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 }
00085
00086 void TSFit::init_errmat( double noise_initialvalue){
00087
00088
00089
00090
00091 int i, j;
00092
00093
00094
00095 for(i=0;i<sdim;i++ ){
00096 for(j=0;j<sdim;j++){
00097 errmat[i][j] = noise_initialvalue;
00098
00099 }
00100
00101 }
00102 }
00103
00104 double TSFit::fpol3dg ( int nmxul,
00105 double *parom,
00106 double *mask,
00107 double *adc){
00108
00109
00110
00111
00112
00113
00114 int i, k, l, iworst;
00115 double h, t2, tm, delta, tmp;
00116 double xki2, dif, difmx, deglib;
00117 double bv[4], s, deter;
00118
00119 deglib=(double)nmxul - 4.;
00120 for(i=0;i<nmxul;i++){
00121 t[i]=i;
00122 ff[i][0]=1.;
00123 ff[i][1]=t[i];
00124 ff[i][2]=t[i]*t[i];
00125 ff[i][3]=ff[i][2]*t[i];
00126 }
00127
00128 for(k=0;k<4;k++){
00129 for(l=0;l<4;l++){
00130 s=0.;
00131 for(i=0;i<nmxul;i++){
00132 s=s+ff[i][k]*ff[i][l]*errmat[i][i]*mask[i];
00133 }
00134 cov[k][l]=s;
00135 }
00136 s=0.;
00137 for(i=0;i<nmxul;i++){
00138 s=s+ff[i][k]*adc[i]*errmat[i][i]*mask[i];
00139 }
00140 bv[k]=s;
00141 }
00142
00143 deter = inverms( 4, cov, invcov );
00144 for(k=0;k<4;k++){
00145 s=0.;
00146 for(l=0;l<4;l++){
00147 s=s+bv[l]*invcov[l][k];
00148 }
00149 parom[k]=s;
00150 }
00151
00152 if( parom[3] == 0. ){
00153 parom[4] = -1000.;
00154 parom[5] = -1000.;
00155 parom[6] = -1000.;
00156 return 1000000.;
00157 }
00158
00159 xki2=0.;
00160 difmx=0.;
00161 for(i=0;i<nmxul;i++ ){
00162 t2=t[i]*t[i];
00163 h= parom[0]+parom[1]*t[i]+parom[2]*t2+parom[3]*t2*t[i];
00164 dif=(adc[i]-h)*mask[i];
00165 xki2=xki2+dif*dif*errmat[i][i];
00166 if(dif > difmx) {
00167 iworst=i;
00168 difmx=dif;
00169 }
00170 }
00171 if(deglib > 0.5) xki2=xki2/deglib;
00172
00173 delta=parom[2]*parom[2]-3.*parom[3]*parom[1];
00174 if(delta > 0.){
00175 delta=sqrt(delta);
00176 tm=-(delta+parom[2])/(3.*parom[3]);
00177 tmp=(delta-parom[2])/(3.*parom[3]);
00178 }
00179 else{
00180 parom[4] = -1000.;
00181 parom[5] = -1000.;
00182 parom[6] = -1000.;
00183 return xki2;
00184 }
00185 parom[4]=tm;
00186 parom[5]= parom[0]+parom[1]*tm+parom[2]*tm*tm+parom[3]*tm*tm*tm;
00187 parom[6]=tmp;
00188 return xki2;
00189 }
00190 double TSFit::inverms( int n, double g[matdim][matdim], double ginv[matdim][matdim] ){
00191
00192
00193 int i, j, k, jj;
00194 double r, s;
00195 double deter=0;
00196
00197
00198
00199 if( n > matdim ){
00200 printf(
00201 "ERROR : trying to use TSFit::inverms with size %d( max allowed %d\n",
00202 n, matdim );
00203 return -999.;
00204 }
00205
00206 int zero = 0;
00207 memset( (char *)al, zero, 8*n*n );
00208 memset( (char *)be, zero, 8*n*n );
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 al[0][0] = sqrt( g[0][0] );
00219 for(i=1;i<n;i++){
00220 al[i][0] = g[0][i] / al[0][0];
00221 for(j=1;j<=i;j++){
00222 s=0.;
00223 for(k=0;k<=j-1;k++){
00224 s = s + al[i][k] * al[j][k];
00225 }
00226 r= g[i][j] - s;
00227 if( j < i ) al[i][j] = r / al[j][j];
00228 if( j == i ) al[i][j] = sqrt( r );
00229 }
00230 }
00231
00232 be[0][0] = 1./al[0][0];
00233 for(i=1;i<n;i++){
00234 be[i][i] = 1. / al[i][i];
00235 for(j=0;j<i;j++){
00236 jj=i-j-1;
00237 s=0.;
00238 for(k=jj+1;k<=i;k++){
00239 s=s+ be[i][k] * al[k][jj];
00240 }
00241 be[i][jj]=-s/al[jj][jj];
00242 }
00243 }
00244
00245 for(i=0;i<n;i++){
00246 for(j=0;j<n;j++){
00247 s=0.;
00248 for(k=0;k<n;k++){
00249 s=s+ be[k][i] * be[k][j];
00250 }
00251 ginv[i][j]=s;
00252 }
00253 }
00254
00255 return deter;
00256 }
00257
00258 double TSFit::fit_third_degree_polynomial(
00259 double *bdc,
00260 double *ret_dat ){
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 int i;
00275 int nus;
00276 double xki2;
00277 double tm, tmp, amp;
00278
00279 static double nevt;
00280
00281 ret_dat[0] = -999.;
00282 ret_dat[1] = -999.;
00283
00284
00285 double val_max = 0.;
00286 int imax = 0;
00287 for(i=0;i<nbs;i++){
00288 if( sample_flag[i] == 0 )continue;
00289 if( bdc[i] > val_max ){
00290 val_max = bdc[i];
00291 imax = i;
00292 }
00293 }
00294
00295 if( (val_max*val_max) * errmat[imax][imax] < 16. )return -118;
00296
00297
00298
00299 if( norme == 0. )norme = val_max;
00300
00301
00302 double val2 = val_max / 2.;
00303 double val3 = val_max / 2.;
00304 int ilow = iinf;
00305 int ihig = 0;
00306
00307 for(i=iinf;i<=isup;i++){
00308 if( sample_flag[i] >= 1 ){
00309 if( ( bdc[i] < val3 ) && ( i < imax ) )ilow = i;
00310 if( bdc[i] > val2 )ihig = i;
00311 }
00312 }
00313
00314 ilow++;
00315
00316
00317
00318
00319
00320 if( ihig == ilow)return -105;
00321 if( ilow == imax )ilow = ilow-1;
00322
00323 ihig = ilow + 3;
00324
00325
00326 nus=0;
00327 int number_of_good_samples = 0;
00328 for(i=ilow;i<=ihig;i++){
00329 maskp3[nus] = 0;
00330 adfmx[nus] = 0.;
00331
00332 if( sample_flag[i] >= 1 ){
00333 adfmx[nus] = bdc[i];
00334 maskp3[nus] = 1.;
00335 number_of_good_samples++;
00336 }
00337 nus++;
00338 }
00339
00340 if( number_of_good_samples < 4 ){
00341 return( -106 );
00342 }
00343
00344 xki2 = fpol3dg( nus, &parfp3[0], &maskp3[0], &adfmx[0]);
00345
00346
00347
00348
00349 tm = parfp3[4] + (float)ilow;
00350 amp = parfp3[5];
00351
00352 if( amp * amp * errmat[0][0] < 2. )return -101.;
00353 tmp = parfp3[6] + (float)ilow;
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 if( xki2 > xki2_max ){
00364 return -102.;
00365 }
00366 if( (tm < (double)ilow ) || (tm > (double)ihig)){
00367 return -103.;
00368 }
00369
00370 if( (tmp > (double)ilow ) && (tmp < (double)ihig - 1.) ){
00371 return -104.;
00372 }
00373
00374 nevt += 1.;
00375
00376 ret_dat[0] = amp;
00377 ret_dat[1] = tm;
00378 ret_dat[2] = val_max;
00379 ret_dat[3] = (double)imax;
00380 ret_dat[4] = (double)ilow;
00381 ret_dat[5] = (double)ihig;
00382 ret_dat[6] = (double)tmp;
00383 int k;
00384 for(i=0;i<4;i++){
00385 k=i+7;
00386 ret_dat[k] = parfp3[i];
00387 }
00388
00389 return xki2;
00390 }
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403