CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MinL3Algorithm.cc
Go to the documentation of this file.
1 
10 #include <math.h>
11 
12 MinL3Algorithm::MinL3Algorithm(float kweight_, int squareMode_, int mineta_, int maxeta_, int minphi_, int maxphi_)
13  :kweight(kweight_), squareMode(squareMode_), mineta(mineta_), maxeta(maxeta_), minphi(minphi_), maxphi(maxphi_), countEvents(0)
14 {
15  int Neta = maxeta - mineta + 1;
16  if (mineta * maxeta < 0) Neta--; // there's no eta index = 0
17  int Nphi = maxphi - minphi + 1;
18  if (Nphi <0) Nphi += 360;
19 
20  Nchannels = Neta * Nphi; // no. of channels, get it from edges of the region
21 
22  Nxtals = squareMode*squareMode; // no. of xtals in one event
23 
24  wsum.assign(Nchannels,0.);
25  Ewsum.assign(Nchannels,0.);
26 }
27 
28 
30 {
31 }
32 
33 
34 std::vector<float> MinL3Algorithm::iterate(const std::vector<std::vector<float> >& eventMatrix, const std::vector<int>& VmaxCeta, const std::vector<int>& VmaxCphi, const std::vector<float>& energyVector, const int& nIter, const bool& normalizeFlag)
35 {
36  int Nevents = eventMatrix.size(); // Number of events to calibrate with
37 
38  std::vector<float> totalSolution(Nchannels,1.);
39  std::vector<float> iterSolution;
40  std::vector<std::vector<float> > myEventMatrix(eventMatrix);
41  std::vector<float> myEnergyVector(energyVector);
42 
43  int i, j;
44 
45  // Iterate the correction
46  for (int iter=1;iter<=nIter;iter++)
47  {
48 
49  // if normalization flag is set, normalize energies
50  float sumOverEnergy;
51  if (normalizeFlag)
52  {
53  float scale = 0.;
54 
55  for (i=0; i<Nevents; i++)
56  {
57  sumOverEnergy = 0.;
58  for (j=0; j<Nxtals; j++) {sumOverEnergy += myEventMatrix[i][j];}
59  sumOverEnergy /= myEnergyVector[i];
60  scale += sumOverEnergy;
61  }
62  scale /= Nevents;
63 
64  for (i=0; i<Nevents; i++) {myEnergyVector[i] *= scale;}
65  } // end normalize energies
66 
67  // now the real work starts:
68  for (int iEvt=0; iEvt < Nevents; iEvt++)
69  {
70  addEvent(myEventMatrix[iEvt], VmaxCeta[iEvt], VmaxCphi[iEvt], myEnergyVector[iEvt]);
71  }
72  iterSolution = getSolution();
73  if (iterSolution.empty()) return iterSolution;
74 
75  // re-calibrate eventMatrix with solution
76  for (int ievent = 0; ievent<Nevents; ievent++)
77  {
78  myEventMatrix[ievent] = recalibrateEvent(myEventMatrix[ievent], VmaxCeta[ievent], VmaxCphi[ievent], iterSolution);
79  }
80 
81  for (int i=0; i<Nchannels; i++)
82  {
83  // save solution into theCalibVector
84  totalSolution[i] *= iterSolution[i];
85  }
86  // resetSolution(); // reset for new iteration, now: getSolution does it automatically if not vetoed
87  } // end iterate correction
88 
89  return totalSolution;
90 }
91 
92 
93 void MinL3Algorithm::addEvent(const std::vector<float>& eventSquare, const int& maxCeta, const int& maxCphi, const float& energy)
94 {
95  countEvents++;
96 
97  float w, invsumXmatrix;
98  float eventw;
99  int iFull, i;
100  // Loop over the crystal matrix to find the sum
101  float sumXmatrix=0.;
102 
103  for (i=0; i<Nxtals; i++) { sumXmatrix+=eventSquare[i]; }
104 
105  // event weighting
106  eventw = 1 - fabs(1 - sumXmatrix/energy);
107  eventw = pow(eventw,kweight);
108 
109  if (sumXmatrix != 0.)
110  {
111  invsumXmatrix = 1/sumXmatrix;
112  // Loop over the crystal matrix (3x3,5x5,7x7) again and calculate the weights for each xtal
113  for (i=0; i<Nxtals; i++)
114  {
115  w = eventSquare[i] * invsumXmatrix;
116 
117  iFull = indexSqr2Reg(i, maxCeta, maxCphi);
118  if (iFull >= 0)
119  {
120  // with event weighting:
121  wsum[iFull] += w*eventw;
122  Ewsum[iFull] += (w*eventw * energy * invsumXmatrix);
123  // wsum[iFull] += w;
124  // Ewsum[iFull] += (w * energy * invsumXmatrix);
125  }
126  }
127  }
128  // else {std::cout << " Debug: dropping null event: " << countEvents << std::endl;}
129 }
130 
131 
132 std::vector<float> MinL3Algorithm::getSolution(bool resetsolution)
133 {
134  std::vector<float> solution(Nchannels,1.);
135 
136  for (int i=0; i<Nchannels; i++)
137  {
138  if (wsum[i] != 0.)
139  { solution[i]*=Ewsum[i]/wsum[i];}
140  // else
141  // { std::cout << "warning - no event data for crystal index (reduced region) " << i << std::endl; }
142  }
143 
144  if (resetsolution) resetSolution();
145 
146  return solution;
147 }
148 
149 
151 {
152  wsum.assign(Nchannels,0.);
153  Ewsum.assign(Nchannels,0.);
154 }
155 
156 
157 std::vector<float> MinL3Algorithm::recalibrateEvent(const std::vector<float>& eventSquare, const int& maxCeta, const int& maxCphi, const std::vector<float>& recalibrateVector)
158 {
159  std::vector<float> newEventSquare(eventSquare);
160  int iFull;
161 
162  for (int i=0; i<Nxtals; i++)
163  {
164  iFull = indexSqr2Reg(i, maxCeta, maxCphi);
165  if (iFull >=0)
166  newEventSquare[i] *= recalibrateVector[iFull];
167  }
168  return newEventSquare;
169 }
170 
171 
172 int MinL3Algorithm::indexSqr2Reg(const int& sqrIndex, const int& maxCeta, const int& maxCphi)
173 {
174  int regionIndex;
175 
176  // get the current eta, phi indices
177  int curr_eta = maxCeta - squareMode/2 + sqrIndex%squareMode;
178  if (curr_eta * maxCeta <= 0) {if (maxCeta > 0) curr_eta--; else curr_eta++; } // JUMP over 0
179 
180  int curr_phi = maxCphi - squareMode/2 + sqrIndex/squareMode;
181  if (curr_phi < 1) curr_phi += 360;
182  if (curr_phi > 360) curr_phi -= 360;
183 
184  bool negPhiDirection = (maxphi < minphi);
185  int iFullphi;
186 
187  regionIndex = -1;
188 
189  if (curr_eta >= mineta && curr_eta <= maxeta)
190  if ( (!negPhiDirection && (curr_phi >= minphi && curr_phi <= maxphi)) ||
191  (negPhiDirection && !(curr_phi >= minphi && curr_phi <= maxphi)) )
192  {
193  iFullphi = curr_phi - minphi;
194  if (iFullphi < 0) iFullphi += 360;
195  regionIndex = (curr_eta - mineta) * (maxphi - minphi + 1 + 360*negPhiDirection) + iFullphi;
196  }
197 
198  return regionIndex;
199 }
int i
Definition: DBlmapReader.cc:9
MinL3Algorithm(float kweight_=0., int squareMode_=5, int mineta_=1, int maxeta_=85, int minphi_=1, int maxphi_=20)
std::vector< float > getSolution(bool resetsolution=true)
get the solution at the end of the calibration
int indexSqr2Reg(const int &sqrIndex, const int &maxCeta, const int &maxCphi)
method to translate from square indices to region indices
int j
Definition: DBlmapReader.cc:9
std::vector< float > wsum
~MinL3Algorithm()
Destructor.
void resetSolution()
reset for new iteration
void addEvent(const std::vector< float > &eventSquare, const int &maxCeta, const int &maxCphi, const float &energy)
add event to the calculation of the calibration vector
std::vector< float > Ewsum
std::vector< float > recalibrateEvent(const std::vector< float > &eventSquare, const int &maxCeta, const int &maxCphi, const std::vector< float > &recalibrateVector)
recalibrate before next iteration: give previous solution vector as argument
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
std::vector< float > iterate(const std::vector< std::vector< float > > &eventMatrix, const std::vector< int > &VmaxCeta, const std::vector< int > &VmaxCphi, const std::vector< float > &energyVector, const int &nIter, const bool &normalizeFlag=false)
T w() const