CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/OnlineDB/CSCCondDB/src/CSCFitAFEBThr.cc

Go to the documentation of this file.
00001 #include "Minuit2/VariableMetricMinimizer.h"
00002 #include "Minuit2/FunctionMinimum.h"
00003 
00004 #include <OnlineDB/CSCCondDB/interface/CSCFitAFEBThr.h>
00005 #include <OnlineDB/CSCCondDB/interface/CSCThrTurnOnFcn.h>
00006 
00007 #include <cmath>
00008 #include <vector>
00009 #include <iostream>
00010 
00011 using namespace ROOT::Minuit2;
00012 using namespace std;
00013 
00014 CSCFitAFEBThr::CSCFitAFEBThr() {
00015   theOBJfun = new CSCThrTurnOnFcn();
00016   theFitter = new VariableMetricMinimizer();
00017 }
00018 
00019 CSCFitAFEBThr::~CSCFitAFEBThr() {
00020   delete theFitter;
00021   delete theOBJfun;
00022 }
00023 
00024 bool CSCFitAFEBThr::ThresholdNoise(const std::vector<float> & inputx, 
00025                                    const std::vector<float> & inputy, 
00026                                    const int                & npulses,
00027                                    std::vector<int>         & dacoccup,
00028                                    std::vector<float>       & mypar,
00029                                    std::vector<float>       & ermypar,
00030                                    float                    & ercorr,
00031                                    float                    & chisq, 
00032                                    int                      & ndf,
00033                                    int                      & niter,
00034                                    float                    & edm
00035                                   ) const {
00036   bool status = false;                   
00037  
00038   std::vector<double> parinit(2,0);
00039   std::vector<double> erparinit(2,0);
00040 
00042   parinit[0] = 30.0;
00043   parinit[1] = 2.0;
00044   
00045   erparinit[0] = 20;
00046   erparinit[1] = 0.5;
00047 
00049   std::vector<float> x;
00050   std::vector<float> y;
00051   std::vector<float> ynorm;
00052   std::vector<float> ery;
00053   x.clear();
00054   y.clear();
00055   ynorm.clear();
00056   ery.clear();
00057 
00065   
00066   int sum=0;
00067   float r;
00068   for(size_t i=0;i<inputx.size();i++) {
00069      if(inputy[i]>0.0) sum++;
00070      r=inputy[i]/(float)dacoccup[i];
00071      ynorm.push_back(r);
00072 //     std::cout<<" "<<ynorm[i];
00073   }
00074 //  std::cout<<std::endl;
00075   if(sum==0) {
00076     ndf=-4;
00077     return status;
00078   }
00079    
00080 
00081   int nbeg=inputx.size();
00082   // for(size_t i=inputx.size()-1;i>=0;i--) // Wrong.
00083   // Because i is unsigned, i>=0 is always true, 
00084   // and the loop termination condition  is never reached.
00085   // We offset by 1.
00086   for(size_t i=inputx.size();i>0;i--) {
00087     if(ynorm[i-1]<1.0) nbeg--;
00088     if(ynorm[i-1]==1.0) break;
00089   }
00090 
00091   for(size_t i=nbeg;i<inputx.size();i++) {
00092     if(ynorm[i]>0.0) {
00093       x.push_back(inputx[i]); 
00094       y.push_back(ynorm[i]);
00095 
00096       float p=inputy[i]/(float)dacoccup[i];
00097       float s=(float)dacoccup[i] * p * (1.0-p);
00098       s=sqrt(s)/(float)dacoccup[i];
00099       ery.push_back(s);
00100     }                          
00101   }
00102 
00104   ndf=x.size()-2; 
00105   if(ndf <=0) return status;
00106 
00108   float half=0.5;
00109   float dmin=999999.0;
00110   float diff;
00111   for(size_t i=0;i<x.size();i++) {
00112     diff=y[i]-half; if(diff<0.0) diff=-diff;
00113     if(diff<dmin) {dmin=diff; parinit[0]=x[i];}   // par[0] from data    
00114     //std::cout<<i+1<<" "<<x[i]<<" "<<y[i]<<" "<<ery[i]<<std::endl;
00115   }
00116 
00118   theOBJfun->setData(x,y); 
00119   theOBJfun->setErrors(ery);   
00120   theOBJfun->setNorm(1.0);
00121 
00122  // for(size_t int i=0;i<x.size();i++) std::cout<<" "<<x[i]<<" "<<y[i]
00123  //                                               <<" "<<ery[i]<<std::endl; 
00124 
00126   FunctionMinimum fmin=theFitter->Minimize(*theOBJfun,parinit,erparinit,1,500,0.1);
00127 
00128   status = fmin.IsValid();
00129 
00130   if(status) { 
00131     mypar[0]=(float)fmin.Parameters().Vec()(0);
00132     mypar[1]=(float)fmin.Parameters().Vec()(1);
00133     ermypar[0]=(float)sqrt( fmin.Error().Matrix()(0,0) );
00134     ermypar[1]=(float)sqrt( fmin.Error().Matrix()(1,1) );
00135     ercorr=0;
00136     if(ermypar[0] !=0.0 && ermypar[1]!=0.0)
00137     ercorr=(float)fmin.Error().Matrix()(0,1)/(ermypar[0]*ermypar[1]);
00138 
00139     chisq  = fmin.Fval();
00140     ndf=y.size()-mypar.size();
00141     niter=fmin.NFcn();         
00142     edm=fmin.Edm();
00143   }
00144   else ndf=-3;
00145   return status;
00146 }