CMS 3D CMS Logo

BaseFunction.h
Go to the documentation of this file.
1 
6 #ifndef BaseFunction_h
7 #define BaseFunction_h
8 
9 #include <iostream>
10 #include <vector>
11 #include <cstdlib>
13 
14 class BaseFunction {
15 public:
17 
19  BaseFunction(const MuScleFitDBobject* dbObject) {
20  functionId_ = dbObject->identifiers;
21  parVecVec_ = dbObject->parameters;
22  // Needed for the tests in convertToArrays
23  iterationNum_ = functionId_.size() - 1;
24  }
25 
27  std::vector<int> identifiers() const { return functionId_; }
29  std::vector<double> parameters() const { return parVecVec_; }
31  std::vector<double> fitQuality() const { return parVecVec_; }
32 
33 protected:
35  template <class T>
36  void convertToArrays(T**& function_, const std::vector<T*>& functionVec_);
37 
38  std::vector<int> functionId_;
39  std::vector<double> parVecVec_;
40  std::vector<double> fitQuality_;
41  // We will use the array for the function calls because it is faster than the vector for random access.
42  double** parArray_;
43  double** fitQualityArray_;
45 };
46 
47 template <class T>
48 void BaseFunction::convertToArrays(T**& function_, const std::vector<T*>& functionVec_) {
49  // Check for consistency of number of passed parameters and number of required parameters.
50  int totParNums = 0;
51  typename std::vector<T*>::const_iterator funcIt = functionVec_.begin();
52  for (; funcIt != functionVec_.end(); ++funcIt) {
53  totParNums += (*funcIt)->parNum();
54  }
55  int parVecVecSize = parVecVec_.size();
56  int functionVecSize = functionVec_.size();
57  if (functionVecSize != iterationNum_ + 1) {
58  std::cout << "Error: inconsistent number of functions(" << functionVecSize << ") and iterations("
59  << iterationNum_ + 1 << ")" << std::endl;
60  exit(1);
61  } else if (totParNums != parVecVecSize) {
62  std::cout << "Error: inconsistent total number of requested parameters(" << totParNums << ") and parameters read("
63  << parVecVecSize << ")" << std::endl;
64  exit(1);
65  }
66  // else if( parVecVecSize != functionVecSize ) {
67  // std::cout << "Error: inconsistent number of functions("<<functionVecSize<<") and parameter sets("<<parVecVecSize<<")" << std::endl;
68  // exit(1);
69  // }
70  // else if( parVecVecSize != iterationNum_+1 ) {
71  // std::cout << "Error: inconsistent number of parameter sets("<<parVecVecSize<<") and iterations("<<iterationNum_+1<<")" << std::endl;
72  // exit(1);
73  // }
74  // parArray_ = new double*[parVecVecSize];
75 
76  parArray_ = new double*[functionVecSize];
77 
78  // std::vector<double>::const_iterator parVec = parVecVec_.begin();
79  // iterationNum_ starts from 0.
80  function_ = new T*[functionVecSize];
81  typename std::vector<T*>::const_iterator func = functionVec_.begin();
82  std::vector<double>::const_iterator parVec = parVecVec_.begin();
83 
84  int iterationCounter = 0;
85  for (; func != functionVec_.end(); ++func, ++iterationCounter) {
86  // Loop on the parameters size for each function and create corresponding parameter arrays
87  int parNum = (*func)->parNum();
88  parArray_[iterationCounter] = new double[parNum];
89  for (int par = 0; par < parNum; ++par) {
90  parArray_[iterationCounter][par] = *parVec;
91  ++parVec;
92  }
93 
94  // parArray_[iterationCounter] = new double[parVec->size()];
95  // std::vector<double>::const_iterator par = parVec->begin();
96  // int parNum = 0;
97  // for ( ; par != parVec->end(); ++par, ++parNum ) {
98  // parArray_[iterationCounter][parNum] = *par;
99  // // std::cout << "parameter["<<parNum<<"] = " << parArray_[iterationCounter][parNum] << std::endl;
100  // }
101  // // return make_pair(parameters, parameterErrors);
102 
103  function_[iterationCounter] = *func;
104  }
105 }
106 
107 #endif // BaseFunction_h
BaseFunction::convertToArrays
void convertToArrays(T **&function_, const std::vector< T * > &functionVec_)
Convert vectors to arrays for faster random access. The first pointer is replaced,...
Definition: BaseFunction.h:48
BaseFunction::BaseFunction
BaseFunction()
Definition: BaseFunction.h:16
BaseFunction::fitQuality
std::vector< double > fitQuality() const
Return the vector of fit quality values.
Definition: BaseFunction.h:31
gather_cfg.cout
cout
Definition: gather_cfg.py:144
MuScleFitDBobject
Definition: MuScleFitDBobject.h:8
MuScleFitDBobject::identifiers
std::vector< int > identifiers
Definition: MuScleFitDBobject.h:9
BaseFunction::parVecVec_
std::vector< double > parVecVec_
Definition: BaseFunction.h:39
MuScleFitDBobject::parameters
std::vector< double > parameters
Definition: MuScleFitDBobject.h:10
MuScleFitDBobject.h
BaseFunction::parArray_
double ** parArray_
Definition: BaseFunction.h:42
BaseFunction::fitQuality_
std::vector< double > fitQuality_
Definition: BaseFunction.h:40
BaseFunction
Definition: BaseFunction.h:14
BaseFunction::parameters
std::vector< double > parameters() const
Return the vector of parameters.
Definition: BaseFunction.h:29
BaseFunction::fitQualityArray_
double ** fitQualityArray_
Definition: BaseFunction.h:43
BaseFunction::BaseFunction
BaseFunction(const MuScleFitDBobject *dbObject)
Constructor when receiving database parameters.
Definition: BaseFunction.h:19
TrackCollections2monitor_cff.func
func
Definition: TrackCollections2monitor_cff.py:359
BaseFunction::iterationNum_
int iterationNum_
Definition: BaseFunction.h:44
T
long double T
Definition: Basic3DVectorLD.h:48
BaseFunction::functionId_
std::vector< int > functionId_
Definition: BaseFunction.h:38
BaseFunction::identifiers
std::vector< int > identifiers() const
Return the vector of function identifiers.
Definition: BaseFunction.h:27
beamvalidation.exit
def exit(msg="")
Definition: beamvalidation.py:53