CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/CalibCalorimetry/EcalLaserAnalyzer/src/TMatacq.cc

Go to the documentation of this file.
00001 /* 
00002  *  \class TMatacq
00003  *
00004  *  $Date: 2010/10/21 22:54:32 $
00005  *  \author: Patrice Verrecchia - CEA/Saclay
00006  */
00007 #include <CalibCalorimetry/EcalLaserAnalyzer/interface/TMatacq.h>
00008 
00009 #include <iostream>
00010 #include <math.h>
00011 #include "TVectorD.h"
00012 
00013 #include <CalibCalorimetry/EcalLaserAnalyzer/interface/TMarkov.h>
00014 
00015 using namespace std;
00016 //ClassImp(TMatacq)
00017 
00018 void TMatacq::init()
00019 {
00020   for(int k=0;k<NMAXSAMP;k++)
00021        bong[k]=0.;
00022 
00023   for(int k=0;k<=100;k++)
00024        bing[k]=0;
00025 
00026   for(int k=0;k<NSPARAB;k++)
00027        t[k]= (double) k;
00028 
00029   return ;
00030 }
00031 
00032 // Constructor...
00033 TMatacq::TMatacq(int Ntot, int Nsamp1, int Nsamp2, int cut, int Nbef, int Naft, int niv1, int niv2, int niv3, int nevl, int NSlide)
00034 {
00035   fNsamples= Ntot;
00036   presample= Nsamp1;
00037   endsample= Nsamp2;
00038   nsigcut= (double) cut;
00039   fNum_samp_bef_max= Nbef;
00040   fNum_samp_aft_max= Naft;
00041   level1= ((double) niv1)/100.;
00042   level2= ((double) niv2)/100.;
00043   level3= ((double) niv3)/100.;
00044   nevlasers= nevl;
00045   slidingmean=0.0;
00046   nslide=NSlide;
00047   for(int k=0;k<nevlasers;k++)
00048        status[k]=0;
00049   for(int k=0;k<nevlasers;k++)
00050        status[k+nevlasers]=0;
00051 
00052   nevmtq0=0; nevmtq1=0;
00053 }
00054 
00055 // Destructor
00056 TMatacq::~TMatacq()
00057 { 
00058 }
00059 
00060 int TMatacq::rawPulseAnalysis(Int_t Nsamp, Double_t *adc)  // GHM
00061 {
00062   using namespace std;
00063 
00064   //  std::cout << "Entering rawPulseAnalysis" << std::endl;
00065 
00066   int k,ithr;
00067   double dsum=0.,dsum2=0.;
00068   
00069   //  std::cout << "calling init" << std::endl;
00070   init();
00071   //  std::cout << ".......done" << std::endl;
00072 
00073   if(Nsamp != fNsamples) {
00074       printf("found different number of samples fNsamples=%d Nsamp=%d\n",fNsamples,Nsamp);
00075       return 100;
00076   }
00077 
00078   for(k=0;k<presample;k++) {
00079        dsum+= adc[k];
00080        dsum2+= adc[k]*adc[k];
00081   }
00082   bl=dsum/((double) presample);
00083   double ss= (dsum2/((double) presample)-bl*bl);
00084   if(ss<0.) ss=0.;
00085   sigbl=sqrt(ss);
00086   for(ithr=0,k=presample;k<endsample;k++) {
00087         if(adc[k] > (bl+nsigcut*sigbl) && ithr == 0) {
00088             ithr=1; firstsample=k;
00089         }
00090   }
00091 
00092   if(ithr == 0) return 101;
00093 
00094   for(ithr=0,k=firstsample;k<Nsamp;k++) {
00095        if(adc[k] < (bl+nsigcut*sigbl) && ithr == 0) {
00096              ithr=1; lastsample=k;
00097        }
00098   }
00099   if(ithr == 0) lastsample= Nsamp;
00100 
00101   if(lastsample > firstsample+NMAXSAMP) lastsample= firstsample+NMAXSAMP;
00102 
00103   val_max=0.; samplemax=0;
00104   for (Int_t is=firstsample;is<lastsample;is++) {
00105        bong[is-firstsample]= adc[is] - bl;
00106        if(bong[is-firstsample] > val_max) {
00107            val_max= bong[is-firstsample]; samplemax=is;
00108        }
00109   }
00110   if(samplemax == 0) return 103;
00111   if(samplemax > lastsample) return 104;
00112   if(samplemax < firstsample) return 105;
00113 
00114   
00115   int endslide=samplemax -nslide;
00116   int beginslide=nslide;
00117   int islidingmean=0;
00118   slidingmean=0.0;
00119   
00120   for(int i=beginslide;i<endslide;i++) {
00121     slidingmean+= adc[i];
00122     islidingmean+=1;
00123   }
00124   if( islidingmean!=0) slidingmean/=double(islidingmean);
00125 
00126   return 0;
00127 }
00128 int TMatacq::findPeak()
00129 {
00130    int k; int nbinf=0; int jfind=0;
00131    int nbsup= 0;
00132    double thres= val_max*level1;
00133 
00134    for(k=0,jfind=0;k<NMAXSAMP;k++) {
00135        if(jfind == 0) {
00136            if(bong[k] > thres) {
00137                nbinf=k; jfind=1;
00138            }
00139        }
00140    }
00141    if(jfind == 0) nbinf=0;
00142 
00143    for(k=NMAXSAMP,jfind=0;k>nbinf;k--) {
00144        if(jfind == 0) {
00145             if(bong[k] > thres) {
00146                 nbsup=k; jfind=1;
00147             }
00148        }
00149    }
00150    if(nbsup == 0) nbsup=nbinf;
00151 
00152    double sumpkval=1.;
00153    pkval= 0.;
00154    sigpkval=0.5;
00155    if(nbsup == nbinf) {
00156        return 301;
00157    } else {
00158        if(nbinf > 4) nbinf-=3;
00159        else nbinf=1;
00160        if(nbsup < NMAXSAMP-4) nbsup+=3;
00161        else nbsup=NMAXSAMP;
00162 
00163        for(k=0;k<nbinf;k++)
00164             bong[k]=0.;
00165        for(k=nbsup;k<NMAXSAMP;k++)
00166             bong[k]=0.;
00167 
00168        for(k=0,jfind=0;k<NMAXSAMP;k++) {
00169             if(bong[k] > 0.) jfind++;
00170        }
00171        if(jfind == 0) {
00172             return 302;
00173        } else if(jfind == 1) {
00174             return 303;
00175        } else {
00176 
00177             for(k=0;k<NMAXSAMP;k++) {
00178               if(k < 100) 
00179                   bing[k+1]= (int) bong[k];
00180             }
00181 
00182             TMarkov *peak = new TMarkov();
00183 
00184             peak -> peakFinder(&bing[0]);
00185             pkval= peak -> getPeakValue(0);
00186             sigpkval= peak -> getPeakValue(1);
00187 
00188             delete peak;
00189 
00190             sumpkval= 0.0;
00191         
00192             if(sumpkval > 1000.) 
00193                  sumpkval=10.;
00194 
00195             pkval+= (firstsample -1);
00196        }
00197    }
00198 
00199    return 0;
00200 }
00201 
00202 int TMatacq::doFit()
00203 {
00204   int testneg=0;
00205   ampl=0.; timeatmax=0.; 
00206   int heresamplemax= samplemax-firstsample;
00207 
00208   int beginfit= heresamplemax - fNum_samp_bef_max;
00209   int endfit= heresamplemax +  fNum_samp_aft_max;
00210 
00211   int nval= endfit-beginfit +1;
00212   if(nval != NSPARAB) return 201;
00213   for(int kn=beginfit;kn<=endfit;kn++) {
00214       if(bong[kn] <= 0) testneg=1;
00215   }
00216   if(testneg == 1) return 202;
00217 
00218   for(int i=0;i<nval;i++) {
00219      val[i]= bong[beginfit+i];
00220      fv1[i]= 1.;
00221      fv2[i]= (double) (i);
00222      fv3[i]= ((double)(i))*((double)(i));
00223   }
00224 
00225   TVectorD y(nval,val);
00226   TVectorD f1(nval,fv1);
00227   TVectorD f2(nval,fv2);
00228   TVectorD f3(nval,fv3);
00229  
00230   double bj[3];
00231   bj[0]= f1*y; bj[1]=f2*y; bj[2]= f3*y;
00232   TVectorD b(3,bj);
00233 
00234   double aij[9];
00235   aij[0]= f1*f1; aij[1]=f1*f2; aij[2]=f1*f3;
00236   aij[3]= f2*f1; aij[4]=f2*f2; aij[5]=f2*f3;
00237   aij[6]= f3*f1; aij[7]=f3*f2; aij[8]=f3*f3;
00238   TMatrixD a(3,3,aij);
00239  
00240   double det1;
00241   a.InvertFast(&det1);
00242 
00243   TVectorD c= a*b;
00244 
00245   double *par= c.GetMatrixArray();
00246   if(par[2] != 0.) {
00247     timeatmax= -par[1]/(2.*par[2]);
00248     ampl= par[0]-par[2]*(timeatmax*timeatmax);
00249   }
00250 
00251   if(ampl <= 0.) {
00252       ampl=bong[heresamplemax];
00253       return 203;
00254   }
00255 
00256   if((int)timeatmax > NSPARAB)
00257       return 204;
00258   if((int)timeatmax < 0)
00259       return 205;
00260 
00261   timeatmax+= (beginfit + firstsample);
00262 
00263   int tplus[3], tminus[3];
00264   double xampl[3];
00265   xampl[0]=0.2*ampl;
00266   xampl[1]=0.5*ampl;
00267   xampl[2]=0.8*ampl;
00268   
00269   int hitplus[3];
00270   int hitminus[3];
00271   double width[3];
00272   
00273   for(int i=0;i<3;i++){
00274     hitplus[i]=0;
00275     hitminus[i]=0;
00276     width[i]=0.0;
00277     tplus[i]=firstsample+lastsample;
00278     tminus[i]=0; 
00279   }
00280   
00281   // calculate first estimate of half width
00282   int im = heresamplemax;
00283   int iplusbound = firstsample+lastsample-im;
00284   
00285   for(int j=0;j<3;j++){
00286     
00287     for(int i=1;i<im;i++){
00288       
00289       
00290       if (bong[im-i]<xampl[j] && bong[im-i+1]>xampl[j]) {
00291         tminus[j]=im-i;
00292         hitminus[j]++;
00293         i=im;
00294       }
00295     }
00296     
00297 
00298     for(int i=0;i<iplusbound;i++){
00299 
00300       if (bong[im+i]>xampl[j] && bong[im+i+1]<xampl[j]){
00301         tplus[j]=im+i;
00302         hitplus[j]++;
00303         i=iplusbound;
00304       }
00305     }
00306     
00307     // interpolate to get a better estimate
00308     
00309     double slopeplus  = double( bong[tplus[j] +1] - bong[tplus[j] ] );
00310     double slopeminus = double( bong[tminus[j]+1] - bong[tminus[j]] );
00311   
00312   
00313     double timeminus=double(tminus[j])+0.5;
00314     if(slopeminus!=0) timeminus=tminus[j]+(xampl[j]-double(bong[tminus[j]]))/slopeminus;
00315     
00316     
00317     double timeplus=double(tplus[j])+0.5;
00318     if(slopeplus!=0) timeplus=tplus[j]+(xampl[j]-double(bong[tplus[j]]))/slopeplus;
00319     
00320     width[j]=timeplus-timeminus;
00321     
00322   }
00323   
00324   width20=width[0];
00325   width50=width[1];
00326   width80=width[2];
00327   
00328   return 0;
00329 }
00330 
00331 int TMatacq::compute_trise()
00332 {
00333   int error;
00334   trise= 0.;
00335 
00336   double t20= interpolate(ampl*level2);
00337   if(t20 < 0.) {
00338     error= (int) -t20;
00339     return error;
00340   }
00341   double t80= interpolate(ampl*level3);
00342   if(t80 < 0.) {
00343     error= (int) -t80;
00344     return error;
00345   }
00346   trise= t80 - t20;
00347 
00348   return 0;
00349 }
00350 
00351 
00352 
00353 
00354 
00355 double TMatacq::interpolate(double amplx)
00356 {
00357   double T;
00358   int kmax= (int) pkval - firstsample;
00359 
00360   int bin_low=0;
00361   for(Int_t k=0;k<kmax;k++)
00362       if(0. < bong[k] && bong[k] < amplx) {
00363           bin_low=k;
00364       }
00365   if(bin_low == 0) return -301.;
00366   int bin_high=0;
00367   for(Int_t k=kmax;k>=0;k--)
00368       if(bong[k] > amplx) {
00369           bin_high=k;
00370       }
00371   if(bin_high == 0) return -302.;
00372   if(bin_high < bin_low) return -303.;
00373 
00374 
00375   if(bin_low == bin_high) {
00376       T= (double) bin_high;
00377   } else {
00378       double slope= (bong[bin_high]-bong[bin_low])/((double) (bin_high-bin_low));
00379       T= (double) bin_high - (bong[bin_high] - amplx)/slope;
00380   }
00381 
00382   return T;
00383 }
00384 
00385 void TMatacq::enterdata(Int_t anevt)
00386 {
00387   if(anevt < 2*nevlasers) {
00388       if(anevt < nevlasers) {
00389           nevmtq0++;
00390           status[nevmtq0-1]= anevt;
00391           comp_trise[nevmtq0-1]= trise;
00392           comp_peak[nevmtq0-1]= pkval;
00393       } else {
00394           nevmtq1++;
00395           status[nevmtq0+nevmtq1-1]= anevt;
00396           comp_trise[nevmtq0+nevmtq1-1]= trise;
00397           comp_peak[nevmtq0+nevmtq1-1]= pkval;
00398       }
00399   } else {
00400       if(anevt < 3*nevlasers) {
00401           nevmtq0++;
00402           status[nevmtq0-1]= anevt;
00403           comp_trise[nevmtq0-1]= trise;
00404           comp_peak[nevmtq0-1]= pkval;
00405       } else {
00406           nevmtq1++;
00407           status[nevmtq0+nevmtq1-1]= anevt;
00408           comp_trise[nevmtq0+nevmtq1-1]= trise;
00409           comp_peak[nevmtq0+nevmtq1-1]= pkval;
00410       }
00411   }
00412 }
00413 
00414 void TMatacq::printmatacqData(int gRunNumber, int color, int timestart)
00415 {
00416      FILE *fmatacq;
00417      char filename[80];
00418      int i;
00419      double ss;
00420      sprintf(filename,"runMatacq%d.pedestal",gRunNumber);
00421      fmatacq = fopen(filename, "w");
00422      if(fmatacq == NULL) printf("Error while opening file : %s\n",filename);
00423 
00424      double sumtrise=0.; double sumtrise2=0.;
00425      int timestop= timestart+3;
00426      double mintrise=10000.;
00427      double maxtrise=0.;
00428      for(i=0;i<nevmtq0;i++) {
00429        if(comp_trise[i] > maxtrise) {
00430            maxtrise=comp_trise[i];
00431        }
00432        if(comp_trise[i] < mintrise) {
00433            mintrise= comp_trise[i];
00434        }
00435        sumtrise+=comp_trise[i];
00436        sumtrise2+=comp_trise[i]*comp_trise[i];
00437      }
00438      meantrise= sumtrise/((double) nevmtq0);
00439      ss= (sumtrise2/((double) nevmtq0) - meantrise*meantrise);
00440      if(ss < 0.) ss=0.;
00441      sigtrise=sqrt(ss);
00442      fprintf(fmatacq, "%d %d %d %d %f %f %f %f\n",
00443            nevmtq0,color,timestart,timestop,meantrise,sigtrise,mintrise,maxtrise);
00444 
00445      sumtrise=0.; sumtrise2=0.;
00446      mintrise=10000.;
00447      maxtrise=0.;
00448      for(i=nevmtq0;i<nevmtq0+nevmtq1;i++) {
00449        if(comp_trise[i] > maxtrise) {
00450            maxtrise=comp_trise[i];
00451        }
00452        if(comp_trise[i] < mintrise) {
00453            mintrise= comp_trise[i];
00454        }
00455        sumtrise+=comp_trise[i];
00456        sumtrise2+=comp_trise[i]*comp_trise[i];
00457      }
00458      meantrise= sumtrise/((double) nevmtq1);
00459      ss= (sumtrise2/((double) nevmtq1) - meantrise*meantrise);
00460      if(ss < 0.) ss=0.;
00461      sigtrise=sqrt(ss);
00462      fprintf(fmatacq, "%d %d %d %d %f %f %f %f\n",
00463            nevmtq1,color,timestart,timestop,meantrise,sigtrise,mintrise,maxtrise);
00464 
00465      int iret=fclose(fmatacq);
00466      printf(" Closing file : %d\n",iret);
00467 }
00468 
00469 int TMatacq::countBadPulses(int gRunNumber)
00470 {
00471   FILE *fmatacq;
00472   char filename[80];
00473   sprintf(filename,"badevtsMatacq%d.dat",gRunNumber);
00474   fmatacq = fopen(filename, "w");
00475   if(fmatacq == NULL) printf("Error while opening file : %s\n",filename);
00476 
00477   int nevbad=0;
00478   for(Int_t i=0;i<nevmtq0+nevmtq1;i++) {
00479        if(comp_trise[i] < meantrise - 3.*sigtrise) {
00480            nevbad++;
00481            fprintf(fmatacq,"%d \n",status[i]);
00482            status[i]=-1;
00483        }
00484        if(comp_trise[i] > meantrise + 3.*sigtrise) {
00485            nevbad++;
00486            fprintf(fmatacq,"%d \n",status[i]);
00487            status[i]=-1;
00488        }
00489   }
00490 
00491   int iret=fclose(fmatacq);
00492   printf(" Closing file : %d\n",iret);
00493   return nevbad;
00494 }
00495 
00496 void TMatacq::printitermatacqData(int gRunNumber, int color, int timestart)
00497 {
00498      FILE *fmatacq;
00499      char filename[80];
00500      int i;
00501      double ss;
00502      sprintf(filename,"runiterMatacq%d.pedestal",gRunNumber);
00503      fmatacq = fopen(filename, "w");
00504      if(fmatacq == NULL) printf("Error while opening file : %s\n",filename);
00505 
00506      int nevmtqgood=0;
00507      double sumtrise=0.; double sumtrise2=0.;
00508      int timestop= timestart+3;
00509      double mintrise=10000.;
00510      double maxtrise=0.;
00511      for(i=0;i<nevmtq0;i++) {
00512        if(status[i] >= 0) {
00513            nevmtqgood++;
00514            if(comp_trise[i] > maxtrise) {
00515                maxtrise=comp_trise[i];
00516            }
00517            if(comp_trise[i] < mintrise) {
00518                mintrise= comp_trise[i];
00519            }
00520            sumtrise+=comp_trise[i];
00521            sumtrise2+=comp_trise[i]*comp_trise[i];
00522        }
00523      }
00524      meantrise= sumtrise/((double) nevmtqgood);
00525      ss= (sumtrise2/((double) nevmtqgood) - meantrise*meantrise);
00526      if(ss < 0.) ss=0.;
00527      sigtrise=sqrt(ss);
00528      fprintf(fmatacq, "%d %d %d %d %f %f %f %f\n",
00529            nevmtqgood,color,timestart,timestop,meantrise,sigtrise,mintrise,maxtrise);
00530 
00531      nevmtqgood=0;
00532      sumtrise=0.; sumtrise2=0.;
00533      mintrise=10000.;
00534      maxtrise=0.;
00535      for(i=nevmtq0;i<nevmtq0+nevmtq1;i++) {
00536        if(status[i] >= 0) {
00537            nevmtqgood++;
00538            if(comp_trise[i] > maxtrise) {
00539                maxtrise=comp_trise[i];
00540            }
00541            if(comp_trise[i] < mintrise) {
00542                mintrise= comp_trise[i];
00543            }
00544            sumtrise+=comp_trise[i];
00545            sumtrise2+=comp_trise[i]*comp_trise[i];
00546        }
00547      }
00548      meantrise= sumtrise/((double) nevmtqgood);
00549      ss= (sumtrise2/((double) nevmtqgood) - meantrise*meantrise);
00550      if(ss < 0.) ss=0.;
00551      sigtrise=sqrt(ss);
00552      fprintf(fmatacq, "%d %d %d %d %f %f %f %f\n",
00553            nevmtqgood,color,timestart,timestop,meantrise,sigtrise,mintrise,maxtrise);
00554 
00555      int iret=fclose(fmatacq);
00556      printf(" Closing file : %d\n",iret);
00557 }