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