CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/CalibCalorimetry/EcalLaserAnalyzer/src/TMarkov.cc

Go to the documentation of this file.
00001 /* 
00002  *  \class TMarkov
00003  *
00004  *  $Date: 2012/02/09 10:08:10 $
00005  *  \author: Patrice Verrecchia - CEA/Saclay
00006  */
00007 
00008 #include <CalibCalorimetry/EcalLaserAnalyzer/interface/TMarkov.h>
00009 
00010 #include <iostream>
00011 #include "math.h"
00012 
00013 //ClassImp(TMarkov)
00014 
00015 
00016 // Constructor...
00017 TMarkov::TMarkov()
00018 { 
00019   // TMarkov
00020   // ------ calcule les distributions invariantes de la chaine de TMarkov
00021   //   correspondantes au spectre original et retourne la dimension de u.
00022   //
00023  
00024   fNPeakValues=3;
00025   fNbinu=101;
00026   init();
00027 }
00028 
00029 // Destructor
00030 TMarkov::~TMarkov()
00031 { 
00032 }
00033 
00034 void TMarkov::init()
00035 {
00036   int i;
00037   for(i=0;i<fNPeakValues;i++) peak[i]=0.;
00038   for(i=0;i<fNbinu;i++) u[i]=0.;
00039   for(i=0;i<=fNbinu;i++) binu[i]=0.;
00040   return ;
00041 }
00042 
00043 int TMarkov::computeChain(int *bing)
00044 {
00045   int i;int k;int nuprime;int offset=0;int m;int pass;
00046   double sumUprime,sumU;
00047   double jumpToNext,jumpToPrevious;
00048   double chainToNext,chainToPrevious;
00049   double aConst[101],uprime[101];  
00050 
00051   pass=0;
00052   for(m=3,i=1,nuprime=1;i<101;i++)
00053   {
00054        uprime[i]=0.;
00055        for(k=1,jumpToNext=0.,jumpToPrevious=0.;k<=m;k++)
00056        {
00057           if(i+k < 101)
00058             if(bing[i] > 0 || bing[i+k] > 0)        
00059               jumpToNext += exp( (double)(bing[i+k]-bing[i])
00060                                 /sqrt((double)(bing[i+k]+bing[i])));
00061           if(i-k > 0)
00062             if(bing[i] > 0 || bing[i-k] > 0)
00063               jumpToPrevious += exp( (double)(bing[i-k]-bing[i])
00064                                     /sqrt((double)(bing[i-k]+bing[i])));
00065         }
00066        //printf(" jump %d to %d = %f\n",i,i+1,jumpToNext);
00067        //printf(" jump %d to %d = %f\n",i,i-1,jumpToPrevious);
00068         if(jumpToNext > 0. && jumpToPrevious > 0.)
00069         {
00070           aConst[i] = -log(jumpToNext+jumpToPrevious);
00071           chainToNext = aConst[i]+log(jumpToNext);
00072           chainToPrevious = aConst[i]+log(jumpToPrevious);
00073           uprime[i]=chainToNext - chainToPrevious;
00074           nuprime++; u[nuprime] = uprime[i];
00075           if(pass == 0)
00076           {  offset=i-1; pass=1;}
00077         }                                                    
00078     }
00079     
00080   //for(i=1;i<101;i++)
00081   //printf(" bin numero %d   uprime = %f\n",i,uprime[i]);
00082     
00083     for(k=3,sumUprime=u[2],sumU=u[2];k<nuprime+1;k++)
00084     {
00085        sumU += u[k];  u[k] = sumU;
00086        sumUprime += log(1.+exp(u[k]-u[k-1]));
00087     }      
00088         
00089     u[1] = -sumUprime;
00090 
00091     for(k=2;k<nuprime+1;k++)
00092         u[k] += u[1];
00093 
00094     for(i=1;i<offset+1;i++)
00095        binu[i]=0.;
00096 
00097     for(i=1;i<nuprime+1;i++)
00098     {
00099        binu[i+offset] = exp(u[i]);
00100        //printf(" bin numero %d   log(u) = %f\n",i+offset,u[i]); 
00101        //printf(" bin numero %d   u = %f\n",i+offset,exp(u[i])); 
00102        
00103     }
00104   
00105     return nuprime+offset;
00106 }
00107 
00108 void TMarkov::peakFinder(int *bing)
00109 {
00110     int firstBin=0;int lastBin=0;
00111     double barycentre=0.;
00112     double sum=0.;
00113     double maximum=0.;
00114 
00115     int nu= computeChain(&bing[0]);
00116 
00117     for(int i=1;i<nu+1;i++)
00118     {
00119        sum += binu[i];
00120        barycentre += (double)i * binu[i];
00121        if(binu[i] > maximum)
00122        { maximum=binu[i]; imax=i; }
00123     }
00124     
00125     maximum *= 0.75;
00126     for(int i=1,pass=0;i<nu+1;i++) {
00127        if(binu[i] > maximum) {
00128          if(pass == 0) {
00129                 firstBin=i;
00130                 lastBin=i;
00131                 pass=1;
00132          } else {
00133                  lastBin=i;
00134          }
00135        }
00136     }
00137 
00138     peak[0] = (barycentre/sum);
00139     peak[1]= (double)(lastBin-firstBin+1);
00140     peak[2]= sum;
00141 }