CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/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     std::vector<int>::const_iterator id = functionId_.begin();
00024     parVecVec_ = dbObject->parameters;
00025     // Needed for the tests in convertToArrays
00026     iterationNum_ = functionId_.size()-1;
00027   }
00028 
00030   std::vector<int> identifiers() const {
00031     return functionId_;
00032   }
00034   std::vector<double> parameters() const {
00035     return parVecVec_;
00036   }
00038   std::vector<double> fitQuality() const {
00039     return parVecVec_;
00040   }
00041 
00042 protected:
00044   template<class T>
00045   void convertToArrays(T **& function_, const std::vector<T*> & functionVec_);
00046 
00047   std::vector<int> functionId_;
00048   std::vector<double> parVecVec_;
00049   std::vector<double> fitQuality_;
00050   // We will use the array for the function calls because it is faster than the vector for random access.
00051   double ** parArray_;
00052   double ** fitQualityArray_;
00053   int iterationNum_;
00054 };
00055 
00056 template <class T>
00057 void BaseFunction::convertToArrays(T **& function_, const std::vector<T*> & functionVec_)
00058 {
00059   // Check for consistency of number of passed parameters and number of required parameters.
00060   int totParNums = 0;
00061   typename std::vector<T*>::const_iterator funcIt = functionVec_.begin();
00062   for( ; funcIt != functionVec_.end(); ++funcIt ) {
00063     totParNums += (*funcIt)->parNum();
00064   }
00065   int parVecVecSize = parVecVec_.size();
00066   int functionVecSize = functionVec_.size();
00067   if( functionVecSize != iterationNum_+1 ) {
00068     std::cout << "Error: inconsistent number of functions("<<functionVecSize<<") and iterations("<<iterationNum_+1<<")" << std::endl;
00069     exit(1);
00070   }
00071   else if( totParNums != parVecVecSize ) {
00072     std::cout << "Error: inconsistent total number of requested parameters("<<totParNums<<") and parameters read("<<parVecVecSize<<")" << std::endl;
00073     exit(1);
00074   }
00075 //   else if( parVecVecSize != functionVecSize ) {
00076 //     std::cout << "Error: inconsistent number of functions("<<functionVecSize<<") and parameter sets("<<parVecVecSize<<")" << std::endl;
00077 //     exit(1);
00078 //   }
00079 //   else if( parVecVecSize != iterationNum_+1 ) {
00080 //     std::cout << "Error: inconsistent number of parameter sets("<<parVecVecSize<<") and iterations("<<iterationNum_+1<<")" << std::endl;
00081 //     exit(1);
00082 //   }
00083   // parArray_ = new double*[parVecVecSize];
00084 
00085   parArray_ = new double*[functionVecSize];
00086 
00087 //  std::vector<double>::const_iterator parVec = parVecVec_.begin();
00088   // iterationNum_ starts from 0.
00089   function_ = new T*[functionVecSize];
00090   typename std::vector<T * >::const_iterator func = functionVec_.begin();
00091   std::vector<double>::const_iterator parVec = parVecVec_.begin();
00092 
00093   int iterationCounter = 0;
00094   for( ; func != functionVec_.end(); ++func, ++iterationCounter ) {
00095 
00096     // Loop on the parameters size for each function and create corresponding parameter arrays
00097     int parNum = (*func)->parNum();
00098     parArray_[iterationCounter] = new double[parNum];
00099     for( int par = 0; par < parNum; ++par ) {
00100       parArray_[iterationCounter][par] = *parVec;
00101       ++parVec;
00102     }
00103 
00104 //     parArray_[iterationCounter] = new double[parVec->size()];
00105 //     std::vector<double>::const_iterator par = parVec->begin();
00106 //     int parNum = 0;
00107 //     for ( ; par != parVec->end(); ++par, ++parNum ) {
00108 //       parArray_[iterationCounter][parNum] = *par;
00109 //       // std::cout << "parameter["<<parNum<<"] = " << parArray_[iterationCounter][parNum] << std::endl;
00110 //     }
00111 //     // return make_pair(parameters, parameterErrors);
00112 
00113     function_[iterationCounter] = *func;
00114   }
00115 }
00116 
00117 #endif // BaseFunction_h