CMS 3D CMS Logo

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