CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/CalibCalorimetry/EcalLaserAnalyzer/src/TPNFit.cc

Go to the documentation of this file.
00001 /* 
00002  *  \class TPNFit
00003  *
00004  *  $Date: 2012/02/09 10:08:10 $
00005  *  \author: Patrice Verrecchia - CEA/Saclay
00006  */
00007 
00008 #include <CalibCalorimetry/EcalLaserAnalyzer/interface/TPNFit.h>
00009 
00010 #include <iostream>
00011 #include "TVectorD.h"
00012 
00013 //ClassImp(TPNFit)
00014 
00015 
00016 // Constructor...
00017 TPNFit::TPNFit()
00018 { 
00019  
00020   fNsamples               =  0;
00021   fNum_samp_bef_max       =  0;
00022   fNum_samp_after_max     =  0;
00023 }
00024 
00025 // Destructor
00026 TPNFit::~TPNFit()
00027 { 
00028 }
00029 
00030 void TPNFit::init(int nsamples, int firstsample, int lastsample)
00031 {
00032   fNsamples   = nsamples ;
00033   fNum_samp_bef_max = firstsample ;
00034   fNum_samp_after_max = lastsample  ;
00035   //printf("nsamples=%d firstsample=%d lastsample=%d\n",nsamples,firstsample,lastsample);
00036 
00037   if(fNsamples > NMAXSAMP2)
00038           printf("number of PN samples exceed maximum\n");
00039 
00040   for(int k=0;k<NMAXSAMP2;k++)
00041        t[k]= (double) k;
00042 
00043   return ;
00044 }
00045 
00046 double TPNFit::doFit(int maxsample, double *adc)
00047 {
00048   double chi2=0.;
00049   ampl=0.; timeatmax=0.;
00050 
00051   //printf("maxsample=%d\n",maxsample);
00052   firstsample= maxsample - fNum_samp_bef_max;
00053   lastsample= maxsample +  fNum_samp_after_max;
00054 
00055   if(firstsample <= 0) return 101;
00056   if(lastsample >= fNsamples) lastsample=fNsamples-1;
00057   if(lastsample-firstsample < 1) return 102;
00058   int nval= lastsample-firstsample +1;
00059   //printf("firstsample=%d lastsample=%d nval=%d\n",
00060   //                        firstsample,lastsample,nval);
00061   int testneg=0;
00062   for(int kn=firstsample;kn<=lastsample;kn++) {
00063     //printf("adc[%d]=%f\n",kn,adc[kn]);
00064           if(adc[kn] < 0.) testneg=1;
00065   }
00066   if(testneg == 1) return 103;
00067 
00068   for(int i=firstsample;i<=lastsample;i++) {
00069      val[i-firstsample]= adc[i];
00070      fv1[i-firstsample]= 1.;
00071      fv2[i-firstsample]= (double) (i);
00072      fv3[i-firstsample]= ((double)(i))*((double)(i));
00073   }
00074 
00075   TVectorD y(nval,val);
00076   //y.Print();
00077   TVectorD f1(nval,fv1);
00078   //f1.Print();
00079   TVectorD f2(nval,fv2);
00080   //f2.Print();
00081   TVectorD f3(nval,fv3);
00082   //f3.Print();
00083  
00084   double bj[3];
00085   bj[0]= f1*y; bj[1]=f2*y; bj[2]= f3*y;
00086   TVectorD b(3,bj);
00087   //b.Print();
00088 
00089   double aij[9];
00090   aij[0]= f1*f1; aij[1]=f1*f2; aij[2]=f1*f3;
00091   aij[3]= f2*f1; aij[4]=f2*f2; aij[5]=f2*f3;
00092   aij[6]= f3*f1; aij[7]=f3*f2; aij[8]=f3*f3;
00093   TMatrixD a(3,3,aij);
00094   //a.Print();
00095  
00096   double det1;
00097   a.InvertFast(&det1);
00098   //a.Print();
00099 
00100   TVectorD c= a*b;
00101   //c.Print();
00102 
00103   double *par= c.GetMatrixArray();
00104   //printf("par0=%f par1=%f par2=%f\n",par[0],par[1],par[2]);
00105   if(par[2] != 0.) {
00106     timeatmax= -par[1]/(2.*par[2]);
00107     ampl= par[0]-par[2]*(timeatmax*timeatmax);
00108   }
00109   //printf("amp=%f time=%f\n",amp_max,timeatmax);
00110      
00111   if(ampl <= 0.) {
00112       ampl=adc[maxsample];
00113       return 1.;
00114   }
00115 
00116   if((int)timeatmax > lastsample)
00117       return 103;
00118   if((int)timeatmax < firstsample)
00119       return 103;
00120 
00121   return chi2;
00122 }