CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/MuonAnalysis/MomentumScaleCalibration/interface/BaseFunction.h

Go to the documentation of this file.
00001 
00006 #ifndef BaseFunction_h
00007 #define BaseFunction_h
00008 
00009 #include <iostream>
00010 #include <vector>
00011 #include <cstdlib>
00012 #include "CondFormats/RecoMuonObjects/interface/MuScleFitDBobject.h"
00013 
00014 class BaseFunction
00015 {
00016 public:
00017   BaseFunction() {}
00018 
00020   BaseFunction( const MuScleFitDBobject * dbObject )
00021   {
00022     functionId_ = dbObject->identifiers;
00023     parVecVec_ = dbObject->parameters;
00024     // Needed for the tests in convertToArrays
00025     iterationNum_ = functionId_.size()-1;
00026   }
00027 
00029   std::vector<int> identifiers() const {
00030     return functionId_;
00031   }
00033   std::vector<double> parameters() const {
00034     return parVecVec_;
00035   }
00037   std::vector<double> fitQuality() const {
00038     return parVecVec_;
00039   }
00040 
00041 protected:
00043   template<class T>
00044   void convertToArrays(T **& function_, const std::vector<T*> & functionVec_);
00045 
00046   std::vector<int> functionId_;
00047   std::vector<double> parVecVec_;
00048   std::vector<double> fitQuality_;
00049   // We will use the array for the function calls because it is faster than the vector for random access.
00050   double ** parArray_;
00051   double ** fitQualityArray_;
00052   int iterationNum_;
00053 };
00054 
00055 template <class T>
00056 void BaseFunction::convertToArrays(T **& function_, const std::vector<T*> & functionVec_)
00057 {
00058   // Check for consistency of number of passed parameters and number of required parameters.
00059   int totParNums = 0;
00060   typename std::vector<T*>::const_iterator funcIt = functionVec_.begin();
00061   for( ; funcIt != functionVec_.end(); ++funcIt ) {
00062     totParNums += (*funcIt)->parNum();
00063   }
00064   int parVecVecSize = parVecVec_.size();
00065   int functionVecSize = functionVec_.size();
00066   if( functionVecSize != iterationNum_+1 ) {
00067     std::cout << "Error: inconsistent number of functions("<<functionVecSize<<") and iterations("<<iterationNum_+1<<")" << std::endl;
00068     exit(1);
00069   }
00070   else if( totParNums != parVecVecSize ) {
00071     std::cout << "Error: inconsistent total number of requested parameters("<<totParNums<<") and parameters read("<<parVecVecSize<<")" << std::endl;
00072     exit(1);
00073   }
00074 //   else if( parVecVecSize != functionVecSize ) {
00075 //     std::cout << "Error: inconsistent number of functions("<<functionVecSize<<") and parameter sets("<<parVecVecSize<<")" << std::endl;
00076 //     exit(1);
00077 //   }
00078 //   else if( parVecVecSize != iterationNum_+1 ) {
00079 //     std::cout << "Error: inconsistent number of parameter sets("<<parVecVecSize<<") and iterations("<<iterationNum_+1<<")" << std::endl;
00080 //     exit(1);
00081 //   }
00082   // parArray_ = new double*[parVecVecSize];
00083 
00084   parArray_ = new double*[functionVecSize];
00085 
00086 //  std::vector<double>::const_iterator parVec = parVecVec_.begin();
00087   // iterationNum_ starts from 0.
00088   function_ = new T*[functionVecSize];
00089   typename std::vector<T * >::const_iterator func = functionVec_.begin();
00090   std::vector<double>::const_iterator parVec = parVecVec_.begin();
00091 
00092   int iterationCounter = 0;
00093   for( ; func != functionVec_.end(); ++func, ++iterationCounter ) {
00094 
00095     // Loop on the parameters size for each function and create corresponding parameter arrays
00096     int parNum = (*func)->parNum();
00097     parArray_[iterationCounter] = new double[parNum];
00098     for( int par = 0; par < parNum; ++par ) {
00099       parArray_[iterationCounter][par] = *parVec;
00100       ++parVec;
00101     }
00102 
00103 //     parArray_[iterationCounter] = new double[parVec->size()];
00104 //     std::vector<double>::const_iterator par = parVec->begin();
00105 //     int parNum = 0;
00106 //     for ( ; par != parVec->end(); ++par, ++parNum ) {
00107 //       parArray_[iterationCounter][parNum] = *par;
00108 //       // std::cout << "parameter["<<parNum<<"] = " << parArray_[iterationCounter][parNum] << std::endl;
00109 //     }
00110 //     // return make_pair(parameters, parameterErrors);
00111 
00112     function_[iterationCounter] = *func;
00113   }
00114 }
00115 
00116 #endif // BaseFunction_h