Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:36

0001 /**
0002  * \class BaseFunction
0003  * This class is used as base from scale, resolution and background functions.
0004  */
0005 
0006 #ifndef BaseFunction_h
0007 #define BaseFunction_h
0008 
0009 #include <iostream>
0010 #include <vector>
0011 #include <cstdlib>
0012 #include "CondFormats/RecoMuonObjects/interface/MuScleFitDBobject.h"
0013 
0014 class BaseFunction {
0015 public:
0016   BaseFunction() {}
0017 
0018   /// Constructor when receiving database parameters
0019   BaseFunction(const MuScleFitDBobject* dbObject) {
0020     functionId_ = dbObject->identifiers;
0021     parVecVec_ = dbObject->parameters;
0022     // Needed for the tests in convertToArrays
0023     iterationNum_ = functionId_.size() - 1;
0024   }
0025 
0026   /// Return the vector of function identifiers
0027   std::vector<int> identifiers() const { return functionId_; }
0028   /// Return the vector of parameters
0029   std::vector<double> parameters() const { return parVecVec_; }
0030   /// Return the vector of fit quality values
0031   std::vector<double> fitQuality() const { return parVecVec_; }
0032 
0033 protected:
0034   /// Convert vectors to arrays for faster random access. The first pointer is replaced, thus it is taken by reference.
0035   template <class T>
0036   void convertToArrays(T**& function_, const std::vector<T*>& functionVec_);
0037 
0038   std::vector<int> functionId_;
0039   std::vector<double> parVecVec_;
0040   std::vector<double> fitQuality_;
0041   // We will use the array for the function calls because it is faster than the vector for random access.
0042   double** parArray_;
0043   double** fitQualityArray_;
0044   int iterationNum_;
0045 };
0046 
0047 template <class T>
0048 void BaseFunction::convertToArrays(T**& function_, const std::vector<T*>& functionVec_) {
0049   // Check for consistency of number of passed parameters and number of required parameters.
0050   int totParNums = 0;
0051   typename std::vector<T*>::const_iterator funcIt = functionVec_.begin();
0052   for (; funcIt != functionVec_.end(); ++funcIt) {
0053     totParNums += (*funcIt)->parNum();
0054   }
0055   int parVecVecSize = parVecVec_.size();
0056   int functionVecSize = functionVec_.size();
0057   if (functionVecSize != iterationNum_ + 1) {
0058     std::cout << "Error: inconsistent number of functions(" << functionVecSize << ") and iterations("
0059               << iterationNum_ + 1 << ")" << std::endl;
0060     exit(1);
0061   } else if (totParNums != parVecVecSize) {
0062     std::cout << "Error: inconsistent total number of requested parameters(" << totParNums << ") and parameters read("
0063               << parVecVecSize << ")" << std::endl;
0064     exit(1);
0065   }
0066   //   else if( parVecVecSize != functionVecSize ) {
0067   //     std::cout << "Error: inconsistent number of functions("<<functionVecSize<<") and parameter sets("<<parVecVecSize<<")" << std::endl;
0068   //     exit(1);
0069   //   }
0070   //   else if( parVecVecSize != iterationNum_+1 ) {
0071   //     std::cout << "Error: inconsistent number of parameter sets("<<parVecVecSize<<") and iterations("<<iterationNum_+1<<")" << std::endl;
0072   //     exit(1);
0073   //   }
0074   // parArray_ = new double*[parVecVecSize];
0075 
0076   parArray_ = new double*[functionVecSize];
0077 
0078   //  std::vector<double>::const_iterator parVec = parVecVec_.begin();
0079   // iterationNum_ starts from 0.
0080   function_ = new T*[functionVecSize];
0081   typename std::vector<T*>::const_iterator func = functionVec_.begin();
0082   std::vector<double>::const_iterator parVec = parVecVec_.begin();
0083 
0084   int iterationCounter = 0;
0085   for (; func != functionVec_.end(); ++func, ++iterationCounter) {
0086     // Loop on the parameters size for each function and create corresponding parameter arrays
0087     int parNum = (*func)->parNum();
0088     parArray_[iterationCounter] = new double[parNum];
0089     for (int par = 0; par < parNum; ++par) {
0090       parArray_[iterationCounter][par] = *parVec;
0091       ++parVec;
0092     }
0093 
0094     //     parArray_[iterationCounter] = new double[parVec->size()];
0095     //     std::vector<double>::const_iterator par = parVec->begin();
0096     //     int parNum = 0;
0097     //     for ( ; par != parVec->end(); ++par, ++parNum ) {
0098     //       parArray_[iterationCounter][parNum] = *par;
0099     //       // std::cout << "parameter["<<parNum<<"] = " << parArray_[iterationCounter][parNum] << std::endl;
0100     //     }
0101     //     // return make_pair(parameters, parameterErrors);
0102 
0103     function_[iterationCounter] = *func;
0104   }
0105 }
0106 
0107 #endif  // BaseFunction_h