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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
/* 
 *  \class TAPDPulse
 *
 *  \author: Julie Malcles - CEA/Saclay
 */

#include <CalibCalorimetry/EcalLaserAnalyzer/interface/TAPDPulse.h>
#include <TMath.h>
#include <iostream>
#include <cassert>
using namespace std;

//ClassImp(TAPDPulse)

// Default Constructor...
TAPDPulse::TAPDPulse() { init(10, 3, 1, 2, 2, 9, 3, 8, 0.4, 0.95, 0.8); }

// Constructor...
TAPDPulse::TAPDPulse(int nsamples,
                     int presample,
                     int firstsample,
                     int lastsample,
                     int timingcutlow,
                     int timingcuthigh,
                     int timingquallow,
                     int timingqualhigh,
                     double ratiomincutlow,
                     double ratiomincuthigh,
                     double ratiomaxcutlow) {
  init(nsamples,
       presample,
       firstsample,
       lastsample,
       timingcutlow,
       timingcuthigh,
       timingquallow,
       timingqualhigh,
       ratiomincutlow,
       ratiomincuthigh,
       ratiomaxcutlow);
}

// Destructor
TAPDPulse::~TAPDPulse() {}

void TAPDPulse::init(int nsamples,
                     int presample,
                     int firstsample,
                     int lastsample,
                     int timingcutlow,
                     int timingcuthigh,
                     int timingquallow,
                     int timingqualhigh,
                     double ratiomincutlow,
                     double ratiomincuthigh,
                     double ratiomaxcutlow) {
  _nsamples = 10;
  assert(nsamples == _nsamples);
  assert(presample != 0);
  adc_ = new double[10];

  _presample = presample;
  _firstsample = firstsample;
  _lastsample = lastsample;

  _timingcutlow = timingcutlow;
  _timingcuthigh = timingcuthigh;
  _timingquallow = timingquallow;
  _timingqualhigh = timingqualhigh;
  _ratiomincutlow = ratiomincutlow;
  _ratiomincuthigh = ratiomincuthigh;
  _ratiomaxcutlow = ratiomaxcutlow;

  for (int i = 0; i < _nsamples; i++) {
    adc_[i] = 0.0;
  }

  adcMax_ = 0;
  iadcMax_ = 0;
  pedestal_ = 0;

  isMaxFound_ = false;
  isPedCalc_ = false;
}

bool TAPDPulse::setPulse(double *adc) {
  bool done = false;
  adc_ = adc;
  done = true;
  isMaxFound_ = false;
  isPedCalc_ = false;
  return done;
}
double TAPDPulse::getMax() {
  if (isMaxFound_)
    return adcMax_;

  int iadcmax = 0;
  double adcmax = 0.0;
  for (int i = 0; i < _nsamples; i++) {
    if (adc_[i] > adcmax) {
      iadcmax = i;
      adcmax = adc_[i];
    }
  }
  iadcMax_ = iadcmax;
  adcMax_ = adcmax;
  return adcMax_;
}

int TAPDPulse::getMaxSample() {
  if (!isMaxFound_)
    getMax();
  return iadcMax_;
}
double TAPDPulse::getDelta(int n1, int n2) {
  assert(n1 < _nsamples && n1 >= 0);
  assert(n2 < _nsamples && n2 >= 0);

  double delta = adc_[n1] - adc_[n2];
  return delta;
}
double TAPDPulse::getRatio(int n1, int n2) {
  assert(n1 < _nsamples && n1 >= 0);
  assert(n2 < _nsamples && n2 >= 0);

  double ped = 0;
  if (isPedCalc_)
    ped = pedestal_;
  else
    ped = adc_[0];

  double ratio = (adc_[n1] - ped) / (adc_[n2] - ped);
  return ratio;
}

bool TAPDPulse::isTimingOK() {
  bool ok = true;
  if (!isMaxFound_)
    getMax();
  if (iadcMax_ <= _timingcutlow || iadcMax_ >= _timingcuthigh)
    ok = false;
  return ok;
}
bool TAPDPulse::isTimingQualOK() {
  bool ok = true;
  if (!isMaxFound_)
    getMax();
  if (iadcMax_ <= _timingquallow || iadcMax_ >= _timingqualhigh)
    ok = false;
  return ok;
}

bool TAPDPulse::areFitSamplesOK() {
  bool ok = true;
  if (!isMaxFound_)
    getMax();
  if ((iadcMax_ - _firstsample) < _presample || (iadcMax_ + _lastsample) > _nsamples - 1)
    ok = false;
  return ok;
}
bool TAPDPulse::isPulseOK() {
  bool okSamples = areFitSamplesOK();
  bool okTiming = isTimingOK();
  bool okPulse = arePulseRatioOK();

  bool ok = (okSamples && okTiming && okPulse);

  return ok;
}
bool TAPDPulse::arePulseRatioOK() {
  bool ok = true;

  if (!isMaxFound_)
    getMax();
  if (iadcMax_ < 1 || iadcMax_ >= _nsamples - 1)
    return false;

  double ratioNm1 = getRatio(iadcMax_ - 1, iadcMax_);
  double ratioNp1 = getRatio(iadcMax_ + 1, iadcMax_);
  double ratioMax = TMath::Max(ratioNm1, ratioNp1);
  double ratioMin = TMath::Min(ratioNm1, ratioNp1);

  if (ratioMax < _ratiomaxcutlow)
    ok = false;
  if (ratioMin < _ratiomincutlow || ratioMin > _ratiomincuthigh)
    ok = false;

  return ok;
}
bool TAPDPulse::isPulseRatioMaxOK() {
  bool ok = true;

  if (!isMaxFound_)
    getMax();
  if (iadcMax_ < 1 || iadcMax_ >= _nsamples - 1)
    return false;

  double ratioNm1 = getRatio(iadcMax_ - 1, iadcMax_);
  double ratioNp1 = getRatio(iadcMax_ + 1, iadcMax_);
  double ratioMax = TMath::Max(ratioNm1, ratioNp1);

  if (ratioMax < _ratiomaxcutlow)
    ok = false;
  return ok;
}
bool TAPDPulse::isPulseRatioMinOK() {
  bool ok = true;

  if (!isMaxFound_)
    getMax();
  if (iadcMax_ < 1 || iadcMax_ >= _nsamples - 1)
    return false;

  double ratioNm1 = getRatio(iadcMax_ - 1, iadcMax_);
  double ratioNp1 = getRatio(iadcMax_ + 1, iadcMax_);
  double ratioMin = TMath::Min(ratioNm1, ratioNp1);

  if (ratioMin < _ratiomincutlow || ratioMin > _ratiomincuthigh)
    ok = false;
  return ok;
}

double TAPDPulse::getPedestal() {
  if (isPedCalc_)
    return pedestal_;
  double ped = 0;
  for (int i = 0; i < _presample; i++) {
    ped += adc_[i];
  }
  ped /= double(_presample);
  pedestal_ = ped;
  isPedCalc_ = true;
  return pedestal_;
}

double *TAPDPulse::getAdcWithoutPedestal() {
  double ped;
  if (!isPedCalc_)
    ped = getPedestal();
  else
    ped = pedestal_;

  double *adcNoPed = new double[10];
  for (int i = 0; i < _nsamples; i++) {
    adcNoPed[i] = adc_[i] - ped;
  }
  return adcNoPed;
}

void TAPDPulse::setPresamples(int presample) {
  isPedCalc_ = false;
  _presample = presample;
}