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
#include "CalibTracker/SiStripAPVAnalysis/interface/TT6PedestalCalculator.h"

#include <cmath>
#include <numeric>
#include <algorithm>
using namespace std;
TT6PedestalCalculator::TT6PedestalCalculator(int evnt_ini, int evnt_iter, float sig_cut)
    : numberOfEvents(0), alreadyUsedEvent(false) {
  if (false)
    cout << "Constructing TT6PedestalCalculator " << endl;
  eventsRequiredToCalibrate = evnt_ini;
  eventsRequiredToUpdate = evnt_iter;
  cutToAvoidSignal = sig_cut;
  init();
}
//
// Initialization.
//
void TT6PedestalCalculator::init() {
  theRawNoise.clear();
  thePedestal.clear();
  thePedSum.clear();
  thePedSqSum.clear();
  theEventPerStrip.clear();
  theStatus.setCalibrating();
}
//
//  -- Destructor
//
TT6PedestalCalculator::~TT6PedestalCalculator() {
  if (false)
    cout << "Destructing TT6PedestalCalculator " << endl;
}
//
// -- Set Pedestal Update Status
//
void TT6PedestalCalculator::updateStatus() {
  if (theStatus.isCalibrating() && numberOfEvents >= eventsRequiredToCalibrate) {
    theStatus.setUpdating();
  }
}
//
// -- Initialize or Update (when needed) Pedestal Values
//
void TT6PedestalCalculator::updatePedestal(ApvAnalysis::RawSignalType& in) {
  if (alreadyUsedEvent == false) {
    alreadyUsedEvent = true;
    numberOfEvents++;
    if (theStatus.isCalibrating()) {
      initializePedestal(in);
    } else if (theStatus.isUpdating()) {
      refinePedestal(in);
    }
    updateStatus();
  }
}
//
// -- Initialize Pedestal Values using a set of events (eventsRequiredToCalibrate)
//
void TT6PedestalCalculator::initializePedestal(ApvAnalysis::RawSignalType& in) {
  if (numberOfEvents == 1) {
    thePedSum.clear();
    thePedSqSum.clear();
    theEventPerStrip.clear();

    thePedSum.reserve(128);
    thePedSqSum.reserve(128);
    theEventPerStrip.reserve(128);

    thePedSum.resize(in.data.size(), 0.0);
    thePedSqSum.resize(in.data.size(), 0.0);
    theEventPerStrip.resize(in.data.size(), 0);
  }
  if (numberOfEvents <= eventsRequiredToCalibrate) {
    edm::DetSet<SiStripRawDigi>::const_iterator i = in.data.begin();
    int ii = 0;
    for (; i != in.data.end(); i++) {
      thePedSum[ii] += (*i).adc();
      thePedSqSum[ii] += ((*i).adc()) * ((*i).adc());
      theEventPerStrip[ii]++;
      ii++;
    }
  }
  if (numberOfEvents == eventsRequiredToCalibrate) {
    thePedestal.clear();
    theRawNoise.clear();
    edm::DetSet<SiStripRawDigi>::const_iterator i = in.data.begin();
    int ii = 0;
    for (; i != in.data.end(); i++) {
      double avVal = (theEventPerStrip[ii]) ? thePedSum[ii] / theEventPerStrip[ii] : 0.0;
      double sqAvVal = (theEventPerStrip[ii]) ? thePedSqSum[ii] / theEventPerStrip[ii] : 0.0;
      double corr_fac = (theEventPerStrip[ii] > 1) ? (theEventPerStrip[ii] / (theEventPerStrip[ii] - 1)) : 1.0;
      double rmsVal = (sqAvVal - avVal * avVal > 0.0) ? sqrt(corr_fac * (sqAvVal - avVal * avVal)) : 0.0;

      thePedestal.push_back(static_cast<float>(avVal));
      theRawNoise.push_back(static_cast<float>(rmsVal));
      ii++;
    }
  }
}
//
// -- Update Pedestal Values when needed.
//
void TT6PedestalCalculator::refinePedestal(ApvAnalysis::RawSignalType& in) {
  if (((numberOfEvents - eventsRequiredToCalibrate) % eventsRequiredToUpdate) == 1) {
    thePedSum.clear();
    thePedSqSum.clear();
    theEventPerStrip.clear();

    thePedSum.reserve(128);
    thePedSqSum.reserve(128);
    theEventPerStrip.reserve(128);

    thePedSum.resize(in.data.size(), 0.0);
    thePedSqSum.resize(in.data.size(), 0.0);
    theEventPerStrip.resize(in.data.size(), 0);
  }
  unsigned int ii = 0;
  ApvAnalysis::RawSignalType::const_iterator i = in.data.begin();
  for (; i < in.data.end(); i++) {
    if (fabs((*i).adc() - thePedestal[ii]) < cutToAvoidSignal * theRawNoise[ii]) {
      thePedSum[ii] += (*i).adc();
      thePedSqSum[ii] += ((*i).adc()) * ((*i).adc());
      theEventPerStrip[ii]++;
    }
    ii++;
  }
  if (((numberOfEvents - eventsRequiredToCalibrate) % eventsRequiredToUpdate) == 0) {
    for (unsigned int iii = 0; iii < in.data.size(); iii++) {
      if (theEventPerStrip[iii] > 10) {
        double avVal = (theEventPerStrip[iii]) ? thePedSum[iii] / theEventPerStrip[iii] : 0.0;
        double sqAvVal = (theEventPerStrip[iii]) ? thePedSqSum[iii] / theEventPerStrip[iii] : 0.0;
        double rmsVal = (sqAvVal - avVal * avVal > 0.0) ? sqrt(sqAvVal - avVal * avVal) : 0.0;

        if (avVal != 0) {
          thePedestal[iii] = static_cast<float>(avVal);
          theRawNoise[iii] = static_cast<float>(rmsVal);
        }
      }
    }
    thePedSum.clear();
    thePedSqSum.clear();
    theEventPerStrip.clear();
  }
}
//
// Define New Event
//
void TT6PedestalCalculator::newEvent() { alreadyUsedEvent = false; }