CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/OnlineDB/CSCCondDB/interface/CSCxTalk.h

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <math.h>
00003 
00004 
00005 class Conv{
00006   
00007  public:
00008   
00009   Conv(){}
00010   
00011   //square wave fcn convoluted with 1/(t+2.5)
00012   float elec(float t,float vs){
00013     float f;
00014     if (t<=vs){
00015       f=log(t+2.5)-log(2.5);
00016     }
00017     else{
00018       f=log(t+2.5)-log(t-50+2.5);
00019     }
00020     return f;
00021   }//end elec
00022   
00023   
00024   //calculate single electron distribution in 6.25 ns steps
00025   void mkbins(float vs){
00026     int i,k;
00027     float t;
00028     for(i=0;i<120;i++) conve[i]=0.0;
00029     for(i=0;i<120;i++){
00030       for(k=0;k<16;k++){
00031         t=(6.25*i)+(k*0.625);
00032         conve[i]=conve[i]+elec(t,vs);
00033       }
00034     }
00035   } //end mkbins
00036   
00037 
00038   //convolution function
00039   void convolution(float *xleft_a, float *xleft_b, float *min_left, float *xright_a, float *xright_b, float *min_right, float *pTime){
00040     //void(convolution){  
00041     
00042     float max, cross0,cross2,min_l,min_r,sum_x=0.0,sumx2=0.;
00043     float sum_y_left=0.0,sum_y_right=0.0,sum_xy_left=0.0,sum_xy_right=0.0;
00044     float a_left=0.0,a_right=0.0,b_left=0.0,b_right=0.0,chi2_left=0.0,chi2_right=0.0,chi_left=0.0,chi_right=0.0;
00045     float aleft=0.0,aright=0.0,bleft=0.0,bright=0.0;
00046     int i,j,k,l,imax=0;
00047     
00048     for(l=0;l<3;l++){
00049       for(i=0;i<119;i++)conv[l][i]=0.0;
00050       for(j=0;j<119;j++){
00051         for(k=0;k<119;k++){
00052           if(j+k<119)conv[l][j+k]=conv[l][j+k]+convd[l][j]*conve[k];
00053         }
00054       }
00055     }
00056     max=0;
00057     min_l=9999999999999999.0;
00058     min_r=9999999999999999.0;
00059     for(i=0;i<119;i++){
00060       if(conv[1][i]>max){ 
00061         max=conv[1][i];
00062         imax=i;
00063       }
00064     }
00065     
00066     //find the max peak time from 3 timebins when line intersects x axis a+b*x=0 -> x=-a/b 
00067     float time1=-999.0, time2=-999.0, time3=-999.0;
00068     float data1=-999.0, data2=-999.0, data3=-999.0;
00069     float peakTime=0.0;
00070     
00071     time1=imax-1;
00072     time2=imax;
00073     time3=imax+1;
00074     
00075     data1=conv[1][imax-1];
00076     data2=conv[1][imax];
00077     data3=conv[1][imax+1];
00078     
00079     peakTime=(0.5)*((time1*time1*(data3-data2)+time2*time2*(data1-data3)+time3*time3*(data2-data1))/(time1*(data3-data2)+time2*(data1-data3)+time3*(data2-data1)))*6.25;
00080     
00081     for(l=0;l<3;l++){
00082       for(i=0;i<119;i++)conv[l][i]=conv[l][i]/max;
00083     }
00084     
00085     int nobs = 0;
00086     for (int j=0; j<119; j++){
00087       if (conv[1][j]>0.6) nobs++;
00088     }
00089     
00090     for(i=0;i<119;i++){
00091       cross0=0.0;
00092       cross2=0.0;
00093       
00094       if(conv[1][i]>0.6){
00095         cross0=conv[0][i]/(conv[0][i]+conv[1][i]+conv[2][i]);
00096         cross2=conv[2][i]/(conv[0][i]+conv[1][i]+conv[2][i]);
00097         
00098         sum_x += i;
00099         sum_y_left += cross0;
00100         sum_y_right += cross2;
00101         sumx2 += i*i;
00102         sum_xy_left += i*cross0;
00103         sum_xy_right += i*cross2;
00104       }
00105     }  
00106     
00107     //LMS fitting straight line y=a+b*x
00108     
00109     bleft  = ((nobs*sum_xy_left) - (sum_x * sum_y_left))/((nobs*sumx2) - (sum_x*sum_x));
00110     bright = ((nobs*sum_xy_right) - (sum_x * sum_y_right))/((nobs*sumx2) - (sum_x*sum_x));
00111     
00112     aleft  = ((sum_y_left*sumx2)-(sum_x*sum_xy_left))/((nobs*sumx2)-(sum_x*sum_x));
00113     aright = ((sum_y_right*sumx2)-(sum_x*sum_xy_right))/((nobs*sumx2)-(sum_x*sum_x));
00114     
00115     for(i=0;i<119;i++ ){
00116       chi2_left  += (cross0 -(aleft+(bleft*i)))*(cross0 -(aleft+(bleft*i)));
00117       chi2_right += (cross2 -(aright+(bright*i)))*(cross2 -(aright+(bright*i)));
00118     }   
00119     
00120     if(chi_left<min_l){
00121       min_l=chi_left;
00122       bleft=bleft;
00123       aleft=aleft;
00124     }
00125     if(chi_right<min_r){
00126       min_r=chi_right;
00127       bright=bright;
00128       aright=aright;
00129     }
00130     
00131     
00132     //Now calculating parameters in ns to compensate for drift in peak time  
00133     b_left  = bleft/6.25;
00134     b_right = bright/6.25;
00135     
00136     a_left  = aleft  + (bleft*peakTime/6.25);
00137     a_right = aright + (bright*peakTime/6.25);
00138     
00139     *xleft_a   = a_left; 
00140     *xleft_b   = b_left;
00141     *min_left  = chi2_left;
00142     *xright_a  = a_right;
00143     *xright_b  = b_right;
00144     *min_right = chi2_right;
00145     *pTime     = peakTime;
00146     
00147   } //end CONVOLUTION  
00148   
00149   ~Conv(){}
00150 
00151 
00152   float convd[3][120];
00153   float nconvd[3][120];
00154   float conve[120];
00155   float conv[3][120];
00156  
00157 private:
00158   
00159   
00160 
00161 } ;
00162