CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MinL3AlgoUniv.h
Go to the documentation of this file.
1 #ifndef MinL3AlgoUniv_H
2 #define MinL3AlgoUniv_H
3 
14 #include <vector>
15 #include <iostream>
16 #include <map>
17 #include <cmath>
18 
19 template <class IDdet>
21 public:
22  typedef std::map<IDdet, float> IDmap;
23  typedef typename IDmap::value_type IDmapvalue;
24  typedef typename IDmap::iterator iter_IDmap;
25  typedef typename IDmap::const_iterator citer_IDmap;
26 
29  MinL3AlgoUniv(float kweight_ = 0.);
30 
33 
39  IDmap iterate(const std::vector<std::vector<float> >& eventMatrix,
40  const std::vector<std::vector<IDdet> >& idMatrix,
41  const std::vector<float>& energyVector,
42  const int& nIter,
43  const bool& normalizeFlag = false);
44 
46  void addEvent(const std::vector<float>& myCluster, const std::vector<IDdet>& idCluster, const float& energy);
47 
49  std::vector<float> recalibrateEvent(const std::vector<float>& myCluster,
50  const std::vector<IDdet>& idCluster,
51  const IDmap& newCalibration);
52 
55  IDmap getSolution(const bool resetsolution = true);
56 
58  void resetSolution();
59 
60 private:
61  float kweight;
65 };
66 
67 template <class IDdet>
68 MinL3AlgoUniv<IDdet>::MinL3AlgoUniv(float kweight_) : kweight(kweight_), countEvents(0) {
69  resetSolution();
70 }
71 
72 template <class IDdet>
74 
75 template <class IDdet>
76 typename MinL3AlgoUniv<IDdet>::IDmap MinL3AlgoUniv<IDdet>::iterate(const std::vector<std::vector<float> >& eventMatrix,
77  const std::vector<std::vector<IDdet> >& idMatrix,
78  const std::vector<float>& energyVector,
79  const int& nIter,
80  const bool& normalizeFlag) {
81  int Nevents = eventMatrix.size(); // Number of events to calibrate with
82 
83  IDmap totalSolution;
84  IDmap iterSolution;
85  std::vector<std::vector<float> > myEventMatrix(eventMatrix);
86  std::vector<float> myEnergyVector(energyVector);
87 
88  int i;
89 
90  // Iterate the correction
91  for (int iter = 1; iter <= nIter; iter++) {
92  // if normalization flag is set, normalize energies
93  float sumOverEnergy;
94  if (normalizeFlag) {
95  float scale = 0.;
96 
97  for (i = 0; i < Nevents; i++) {
98  sumOverEnergy = 0.;
99  for (unsigned j = 0; j < myEventMatrix[i].size(); j++) {
100  sumOverEnergy += myEventMatrix[i][j];
101  }
102  sumOverEnergy /= myEnergyVector[i];
103  scale += sumOverEnergy;
104  }
105  scale /= Nevents;
106 
107  for (i = 0; i < Nevents; i++) {
108  myEnergyVector[i] *= scale;
109  }
110  } // end normalize energies
111 
112  // now the real work starts:
113  for (int iEvt = 0; iEvt < Nevents; iEvt++) {
114  addEvent(myEventMatrix[iEvt], idMatrix[iEvt], myEnergyVector[iEvt]);
115  }
116  iterSolution = getSolution();
117  if (iterSolution.empty())
118  return iterSolution;
119 
120  // re-calibrate eventMatrix with solution
121  for (int ievent = 0; ievent < Nevents; ievent++) {
122  myEventMatrix[ievent] = recalibrateEvent(myEventMatrix[ievent], idMatrix[ievent], iterSolution);
123  }
124 
125  // save solution into theCalibVector
126  for (iter_IDmap i = iterSolution.begin(); i != iterSolution.end(); i++) {
127  iter_IDmap itotal = totalSolution.find(i->first);
128  if (itotal == totalSolution.end()) {
129  totalSolution.insert(IDmapvalue(i->first, i->second));
130  } else {
131  itotal->second *= i->second;
132  }
133  }
134 
135  // resetSolution(); // reset for new iteration, now: getSolution does it automatically if not vetoed
136  } // end iterate correction
137 
138  return totalSolution;
139 }
140 
141 template <class IDdet>
142 void MinL3AlgoUniv<IDdet>::addEvent(const std::vector<float>& myCluster,
143  const std::vector<IDdet>& idCluster,
144  const float& energy) {
145  countEvents++;
146 
147  float w, invsumXmatrix;
148  float eventw;
149  // Loop over the crystal matrix to find the sum
150  float sumXmatrix = 0.;
151 
152  for (unsigned i = 0; i < myCluster.size(); i++) {
153  sumXmatrix += myCluster[i];
154  }
155 
156  // event weighting
157  eventw = 1 - fabs(1 - sumXmatrix / energy);
158  eventw = pow(eventw, kweight);
159 
160  if (sumXmatrix != 0.) {
161  invsumXmatrix = 1 / sumXmatrix;
162  // Loop over the crystal matrix (3x3,5x5,7x7) again and calculate the weights for each xtal
163  for (unsigned i = 0; i < myCluster.size(); i++) {
164  w = myCluster[i] * invsumXmatrix;
165 
166  // include the weights into wsum, Ewsum
167  iter_IDmap iwsum = wsum.find(idCluster[i]);
168  if (iwsum == wsum.end())
169  wsum.insert(IDmapvalue(idCluster[i], w * eventw));
170  else
171  iwsum->second += w * eventw;
172 
173  iter_IDmap iEwsum = Ewsum.find(idCluster[i]);
174  if (iEwsum == Ewsum.end())
175  Ewsum.insert(IDmapvalue(idCluster[i], (w * eventw * energy * invsumXmatrix)));
176  else
177  iEwsum->second += (w * eventw * energy * invsumXmatrix);
178  }
179  }
180  // else {std::cout << " Debug: dropping null event: " << countEvents << std::endl;}
181 }
182 
183 template <class IDdet>
185  IDmap solution;
186 
187  for (iter_IDmap i = wsum.begin(); i != wsum.end(); i++) {
188  iter_IDmap iEwsum = Ewsum.find(i->first);
189  float myValue = 1;
190  if (i->second != 0)
191  myValue = iEwsum->second / i->second;
192 
193  solution.insert(IDmapvalue(i->first, myValue));
194  }
195 
196  if (resetsolution)
197  resetSolution();
198 
199  return solution;
200 }
201 
202 template <class IDdet>
204  wsum.clear();
205  Ewsum.clear();
206 }
207 
208 template <class IDdet>
209 std::vector<float> MinL3AlgoUniv<IDdet>::recalibrateEvent(const std::vector<float>& myCluster,
210  const std::vector<IDdet>& idCluster,
211  const IDmap& newCalibration) {
212  std::vector<float> newCluster(myCluster);
213 
214  for (unsigned i = 0; i < myCluster.size(); i++) {
215  citer_IDmap icalib = newCalibration.find(idCluster[i]);
216  if (icalib != newCalibration.end()) {
217  newCluster[i] *= icalib->second;
218  } else {
219  std::cout << "No calibration available for this element." << std::endl;
220  }
221  }
222 
223  return newCluster;
224 }
225 
226 #endif // MinL3AlgoUniv_H
const double w
Definition: UKUtility.cc:23
void addEvent(const std::vector< float > &myCluster, const std::vector< IDdet > &idCluster, const float &energy)
add event to the calculation of the calibration vector
IDmap::value_type IDmapvalue
Definition: MinL3AlgoUniv.h:23
IDmap::iterator iter_IDmap
Definition: MinL3AlgoUniv.h:24
std::map< IDdet, float > IDmap
Definition: MinL3AlgoUniv.h:22
~MinL3AlgoUniv()
Destructor.
Definition: MinL3AlgoUniv.h:73
std::vector< float > recalibrateEvent(const std::vector< float > &myCluster, const std::vector< IDdet > &idCluster, const IDmap &newCalibration)
recalibrate before next iteration: give previous solution vector as argument
MinL3AlgoUniv(float kweight_=0.)
Definition: MinL3AlgoUniv.h:68
void resetSolution()
reset for new iteration
IDmap getSolution(const bool resetsolution=true)
tuple cout
Definition: gather_cfg.py:144
IDmap iterate(const std::vector< std::vector< float > > &eventMatrix, const std::vector< std::vector< IDdet > > &idMatrix, const std::vector< float > &energyVector, const int &nIter, const bool &normalizeFlag=false)
Definition: MinL3AlgoUniv.h:76
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
IDmap::const_iterator citer_IDmap
Definition: MinL3AlgoUniv.h:25