Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

/** \file MinL3Algorithm.cc
 *
 * \author R.Ofierzynski, CERN
 */

#include "Calibration/Tools/interface/MinL3Algorithm.h"
#include <cmath>

MinL3Algorithm::MinL3Algorithm(float kweight_, int squareMode_, int mineta_, int maxeta_, int minphi_, int maxphi_)
    : kweight(kweight_),
      squareMode(squareMode_),
      mineta(mineta_),
      maxeta(maxeta_),
      minphi(minphi_),
      maxphi(maxphi_),
      countEvents(0) {
  int Neta = maxeta - mineta + 1;
  if (mineta * maxeta < 0)
    Neta--;  // there's no eta index = 0
  int Nphi = maxphi - minphi + 1;
  if (Nphi < 0)
    Nphi += 360;

  Nchannels = Neta * Nphi;  // no. of channels, get it from edges of the region

  Nxtals = squareMode * squareMode;  // no. of xtals in one event

  wsum.assign(Nchannels, 0.);
  Ewsum.assign(Nchannels, 0.);
}

MinL3Algorithm::~MinL3Algorithm() {}

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) {
  int Nevents = eventMatrix.size();  // Number of events to calibrate with

  std::vector<float> totalSolution(Nchannels, 1.);
  std::vector<float> iterSolution;
  std::vector<std::vector<float> > myEventMatrix(eventMatrix);
  std::vector<float> myEnergyVector(energyVector);

  int i, j;

  // Iterate the correction
  for (int iter = 1; iter <= nIter; iter++) {
    // if normalization flag is set, normalize energies
    float sumOverEnergy;
    if (normalizeFlag) {
      float scale = 0.;

      for (i = 0; i < Nevents; i++) {
        sumOverEnergy = 0.;
        for (j = 0; j < Nxtals; j++) {
          sumOverEnergy += myEventMatrix[i][j];
        }
        sumOverEnergy /= myEnergyVector[i];
        scale += sumOverEnergy;
      }
      scale /= Nevents;

      for (i = 0; i < Nevents; i++) {
        myEnergyVector[i] *= scale;
      }
    }  // end normalize energies

    // now the real work starts:
    for (int iEvt = 0; iEvt < Nevents; iEvt++) {
      addEvent(myEventMatrix[iEvt], VmaxCeta[iEvt], VmaxCphi[iEvt], myEnergyVector[iEvt]);
    }
    iterSolution = getSolution();
    if (iterSolution.empty())
      return iterSolution;

    // re-calibrate eventMatrix with solution
    for (int ievent = 0; ievent < Nevents; ievent++) {
      myEventMatrix[ievent] = recalibrateEvent(myEventMatrix[ievent], VmaxCeta[ievent], VmaxCphi[ievent], iterSolution);
    }

    for (int i = 0; i < Nchannels; i++) {
      // save solution into theCalibVector
      totalSolution[i] *= iterSolution[i];
    }
    //      resetSolution(); // reset for new iteration, now: getSolution does it automatically if not vetoed
  }  // end iterate correction

  return totalSolution;
}

void MinL3Algorithm::addEvent(const std::vector<float>& eventSquare,
                              const int& maxCeta,
                              const int& maxCphi,
                              const float& energy) {
  countEvents++;

  float w, invsumXmatrix;
  float eventw;
  int iFull, i;
  // Loop over the crystal matrix to find the sum
  float sumXmatrix = 0.;

  for (i = 0; i < Nxtals; i++) {
    sumXmatrix += eventSquare[i];
  }

  // event weighting
  eventw = 1 - fabs(1 - sumXmatrix / energy);
  eventw = pow(eventw, kweight);

  if (sumXmatrix != 0.) {
    invsumXmatrix = 1 / sumXmatrix;
    // Loop over the crystal matrix (3x3,5x5,7x7) again and calculate the weights for each xtal
    for (i = 0; i < Nxtals; i++) {
      w = eventSquare[i] * invsumXmatrix;

      iFull = indexSqr2Reg(i, maxCeta, maxCphi);
      if (iFull >= 0) {
        // with event weighting:
        wsum[iFull] += w * eventw;
        Ewsum[iFull] += (w * eventw * energy * invsumXmatrix);
        //	      wsum[iFull] += w;
        //	      Ewsum[iFull] += (w * energy * invsumXmatrix);
      }
    }
  }
  //  else {std::cout << " Debug: dropping null event: " << countEvents << std::endl;}
}

std::vector<float> MinL3Algorithm::getSolution(bool resetsolution) {
  std::vector<float> solution(Nchannels, 1.);

  for (int i = 0; i < Nchannels; i++) {
    if (wsum[i] != 0.) {
      solution[i] *= Ewsum[i] / wsum[i];
    }
    //      else
    //	{ std::cout << "warning - no event data for crystal index (reduced region) " << i << std::endl; }
  }

  if (resetsolution)
    resetSolution();

  return solution;
}

void MinL3Algorithm::resetSolution() {
  wsum.assign(Nchannels, 0.);
  Ewsum.assign(Nchannels, 0.);
}

std::vector<float> MinL3Algorithm::recalibrateEvent(const std::vector<float>& eventSquare,
                                                    const int& maxCeta,
                                                    const int& maxCphi,
                                                    const std::vector<float>& recalibrateVector) {
  std::vector<float> newEventSquare(eventSquare);
  int iFull;

  for (int i = 0; i < Nxtals; i++) {
    iFull = indexSqr2Reg(i, maxCeta, maxCphi);
    if (iFull >= 0)
      newEventSquare[i] *= recalibrateVector[iFull];
  }
  return newEventSquare;
}

int MinL3Algorithm::indexSqr2Reg(const int& sqrIndex, const int& maxCeta, const int& maxCphi) {
  int regionIndex;

  // get the current eta, phi indices
  int curr_eta = maxCeta - squareMode / 2 + sqrIndex % squareMode;
  if (curr_eta * maxCeta <= 0) {
    if (maxCeta > 0)
      curr_eta--;
    else
      curr_eta++;
  }  // JUMP over 0

  int curr_phi = maxCphi - squareMode / 2 + sqrIndex / squareMode;
  if (curr_phi < 1)
    curr_phi += 360;
  if (curr_phi > 360)
    curr_phi -= 360;

  bool negPhiDirection = (maxphi < minphi);
  int iFullphi;

  regionIndex = -1;

  if (curr_eta >= mineta && curr_eta <= maxeta)
    if ((!negPhiDirection && (curr_phi >= minphi && curr_phi <= maxphi)) ||
        (negPhiDirection && !(curr_phi >= minphi && curr_phi <= maxphi))) {
      iFullphi = curr_phi - minphi;
      if (iFullphi < 0)
        iFullphi += 360;
      regionIndex = (curr_eta - mineta) * (maxphi - minphi + 1 + 360 * negPhiDirection) + iFullphi;
    }

  return regionIndex;
}