CMS 3D CMS Logo

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