CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GenericMinL3Algorithm.cc
Go to the documentation of this file.
1 
11 
12 
14  :normaliseFlag(normalise)
15 // if normalize=true: for all events sum the e25ovTRP. then take the mean value and rescale all TrP
16 {
17 }
18 
19 
21 {
22 }
23 
24 
25 std::vector<float> GenericMinL3Algorithm::iterate(const std::vector<std::vector<float> >& eventMatrix, const std::vector<float>& energyVector, const int nIter)
26 {
27  std::vector<float> solution;
28  std::vector<std::vector<float> > myEventMatrix(eventMatrix);
29  int Nevents = eventMatrix.size(); // Number of events to calibrate with
30  int Nchannels = eventMatrix[0].size(); // Number of channel coefficients
31  std::vector<float> theCalibVector(Nchannels,1.);
32 
33  // Iterate the correction
34  for (int iter=1;iter<=nIter;iter++)
35  {
36  // make one iteration
37  solution = iterate(myEventMatrix, energyVector);
38 
39  if (solution.empty()) return solution;
40  // R.O.: or throw an exception, what's the standard CMS way ?
41 
42  // re-calibrate eventMatrix with solution
43  for (int i=0; i<Nchannels; i++)
44  {
45  for (int ievent = 0; ievent<Nevents; ievent++)
46  {
47  myEventMatrix[ievent][i] *= solution[i];
48  }
49  // save solution into theCalibVector
50  theCalibVector[i] *= solution[i];
51  }
52 
53  } // end iterate the correction
54 
55  return theCalibVector;
56 }
57 
58 
59 std::vector<float> GenericMinL3Algorithm::iterate(const std::vector<std::vector<float> >& eventMatrix, const std::vector<float>& energyVector)
60 {
61  std::vector<float> solution;
62  std::vector<float> myEnergyVector(energyVector);
63 
64  int Nevents = eventMatrix.size(); // Number of events to calibrate with
65  int Nchannels = eventMatrix[0].size(); // Number of channel coefficients
66 
67  // Sanity check
68  if (Nevents != int(myEnergyVector.size()))
69  {
70  std::cout << "GenericMinL3Algorithm::iterate(): Error: bad matrix dimensions. Dropping out." << std::endl;
71  return solution; // empty vector !
72  }
73 
74  // initialize the solution vector with 1.
75  solution.assign(Nchannels,1.);
76 
77  int ievent, i, j;
78 
79  // if normalization flag is set, normalize energies
80  float sumOverEnergy;
81  if (normaliseFlag)
82  {
83  float scale = 0.;
84 
85  std::cout << "GenericMinL3Algorithm::iterate(): Normalising event data" << std::endl;
86 
87  for (i=0; i<Nevents; i++)
88  {
89  sumOverEnergy = 0.;
90  for (j=0; j<Nchannels; j++) {sumOverEnergy += eventMatrix[i][j];}
91  sumOverEnergy /= myEnergyVector[i];
92  scale += sumOverEnergy;
93  }
94  scale /= Nevents;
95  std::cout << " Normalisation = " << scale << std::endl;
96 
97  for (i=0; i<Nevents; i++) {myEnergyVector[i] *= scale;}
98  } // end normalize energies
99 
100 
101  // This is where the real work goes on...
102  float sum25, invsum25;
103  float w; // weight for event
104  std::vector<float> wsum(Nchannels,0.); // sum of weights for a crystal
105  std::vector<float> Ewsum(Nchannels,0.); // sum of products of weight*Etrue/E25
106 
107  // Loop over events
108  for (ievent = 0; ievent<Nevents; ievent++)
109  {
110  // Loop over the 5x5 to find the sum
111  sum25=0.;
112 
113  for (i=0; i<Nchannels; i++) { sum25+=eventMatrix[ievent][i]; } //*calibs[i];
114 
115  if (sum25 != 0.)
116  {
117  invsum25 = 1/sum25;
118  // Loop over the 5x5 again and calculate the weights for each xtal
119  for (i=0; i<Nchannels; i++)
120  {
121  w = eventMatrix[ievent][i] * invsum25; // * calibs[i]
122  wsum[i] += w;
123  Ewsum[i] += (w * myEnergyVector[ievent] * invsum25);
124  }
125  }
126  else {std::cout << " Debug: dropping null event: " << ievent << std::endl;}
127 
128  } // end Loop over events
129 
130  // Apply correction factors to all channels not in the margin
131  for (i=0; i<Nchannels; i++)
132  {
133  if (wsum[i] != 0.)
134  { solution[i]*=Ewsum[i]/wsum[i]; }
135  else
136  { std::cout << "warning - no event data for crystal index " << i << std::endl; }
137  }
138 
139  return solution;
140 }
int i
Definition: DBlmapReader.cc:9
std::vector< float > iterate(const std::vector< std::vector< float > > &eventMatrix, const std::vector< float > &energyVector, int nIter)
int j
Definition: DBlmapReader.cc:9
~GenericMinL3Algorithm()
Destructor.
tuple cout
Definition: gather_cfg.py:121
GenericMinL3Algorithm(bool normalise=false)
T w() const