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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
#include "CalibTracker/SiStripAPVAnalysis/interface/ApvAnalysisFactory.h"
//#include "DataFormats/SiStripCommon/interface/SiStripConstants.h"

#include "CalibTracker/SiStripAPVAnalysis/interface/TT6NTPedestalCalculator.h"

using namespace std;
ApvAnalysisFactory::ApvAnalysisFactory(string theAlgorithmType,
                                       int theNumCMstripsInGroup,
                                       int theMaskCalcFlag,
                                       float theMaskNoiseCut,
                                       float theMaskDeadCut,
                                       float theMaskTruncCut,
                                       float theCutToAvoidSignal,
                                       int theEventInitNumber,
                                       int theEventIterNumber) {
  theAlgorithmType_ = theAlgorithmType;
  theNumCMstripsInGroup_ = theNumCMstripsInGroup;
  theMaskCalcFlag_ = theMaskCalcFlag;
  theMaskNoiseCut_ = theMaskNoiseCut;
  theMaskDeadCut_ = theMaskDeadCut;
  theMaskTruncCut_ = theMaskTruncCut;
  theCutToAvoidSignal_ = theCutToAvoidSignal;
  theEventInitNumber_ = theEventInitNumber;
  theEventIterNumber_ = theEventIterNumber;
}

ApvAnalysisFactory::ApvAnalysisFactory(const edm::ParameterSet& pset) {
  theCMType_ = pset.getParameter<string>("CMType");
  useDB_ = pset.getParameter<bool>("useDB");

  theAlgorithmType_ = pset.getParameter<string>("CalculatorAlgorithm");
  theNumCMstripsInGroup_ = pset.getParameter<int>("NumCMstripsInGroup");
  theMaskCalcFlag_ = pset.getParameter<int>("MaskCalculationFlag");

  theMaskNoiseCut_ = pset.getParameter<double>("MaskNoiseCut");
  theMaskDeadCut_ = pset.getParameter<double>("MaskDeadCut");
  theMaskTruncCut_ = pset.getParameter<double>("MaskTruncationCut");
  theCutToAvoidSignal_ = pset.getParameter<double>("CutToAvoidSignal");

  theEventInitNumber_ = pset.getParameter<int>("NumberOfEventsForInit");
  theEventIterNumber_ = pset.getParameter<int>("NumberOfEventsForIteration");
  apvMap_.clear();
}
//----------------------------------
ApvAnalysisFactory::~ApvAnalysisFactory() {
  ApvAnalysisFactory::ApvAnalysisMap::iterator it = apvMap_.begin();
  for (; it != apvMap_.end(); it++) {
    vector<ApvAnalysis*>::iterator myApv = (*it).second.begin();
    for (; myApv != (*it).second.end(); myApv++)
      deleteApv(*myApv);
  }
  apvMap_.clear();
}

//----------------------------------

bool ApvAnalysisFactory::instantiateApvs(uint32_t detId, int numberOfApvs) {
  ApvAnalysisFactory::ApvAnalysisMap::iterator CPos = apvMap_.find(detId);
  if (CPos != apvMap_.end()) {
    cout << " APVs for Detector Id " << detId << " already created !!!" << endl;
    ;
    return false;
  }
  vector<ApvAnalysis*> temp;
  for (int i = 0; i < numberOfApvs; i++) {
    ApvAnalysis* apvTmp = new ApvAnalysis(theEventIterNumber_);
    //      constructAuxiliaryApvClasses(apvTmp);
    constructAuxiliaryApvClasses(apvTmp, detId, i);
    temp.push_back(apvTmp);
  }
  apvMap_.insert(pair<uint32_t, vector<ApvAnalysis*> >(detId, temp));
  return true;
}

std::vector<ApvAnalysis*> ApvAnalysisFactory::getApvAnalysis(const uint32_t nDET_ID) {
  ApvAnalysisMap::const_iterator _apvAnalysisIter = apvMap_.find(nDET_ID);

  return apvMap_.end() != _apvAnalysisIter ? _apvAnalysisIter->second : std::vector<ApvAnalysis*>();
}

void ApvAnalysisFactory::constructAuxiliaryApvClasses(ApvAnalysis* theAPV, uint32_t detId, int thisApv) {
  //----------------------------------------------------------------
  // Create the ped/noise/CMN calculators, zero suppressors etc.
  // (Is called by addDetUnitAndConstructApvs()).
  //
  // N.B. Don't call this twice for the same APV!
  //-----------------------------------------------------------------
  // cout<<"VirtualApvAnalysisFactory::constructAuxiliaryApvClasses"<<endl;
  TkPedestalCalculator* thePedestal = nullptr;
  TkNoiseCalculator* theNoise = nullptr;
  TkApvMask* theMask = nullptr;
  TkCommonModeCalculator* theCM = nullptr;

  TkCommonMode* theCommonMode = new TkCommonMode();
  TkCommonModeTopology* theTopology = new TkCommonModeTopology(128, theNumCMstripsInGroup_);
  theCommonMode->setTopology(theTopology);

  // Create desired algorithms.
  if (theAlgorithmType_ == "TT6") {
    theMask = new TT6ApvMask(theMaskCalcFlag_, theMaskNoiseCut_, theMaskDeadCut_, theMaskTruncCut_);
    theNoise = new TT6NoiseCalculator(theEventInitNumber_, theEventIterNumber_, theCutToAvoidSignal_);
    thePedestal = new TT6PedestalCalculator(theEventInitNumber_, theEventIterNumber_, theCutToAvoidSignal_);
    theCM = new TT6CommonModeCalculator(theNoise, theMask, theCutToAvoidSignal_);
  } else if ("TT6NT" == theAlgorithmType_) {
    theMask = new TT6ApvMask(theMaskCalcFlag_, theMaskNoiseCut_, theMaskDeadCut_, theMaskTruncCut_);
    theNoise = new TT6NoiseCalculator(theEventInitNumber_, theEventIterNumber_, theCutToAvoidSignal_);
    thePedestal = new TT6NTPedestalCalculator;
    theCM = new TT6CommonModeCalculator(theNoise, theMask, theCutToAvoidSignal_);
  } else if (theAlgorithmType_ == "MIX") {
    // the mask as to be defined also for SimplePedCalculator
    theMask = new TT6ApvMask(theMaskCalcFlag_, theMaskNoiseCut_, theMaskDeadCut_, theMaskTruncCut_);

    thePedestal = new SimplePedestalCalculator(theEventInitNumber_);

    theNoise = new SimpleNoiseCalculator(theEventInitNumber_, useDB_);

    if (theCMType_ == "Median") {
      theCM = new MedianCommonModeCalculator();
    } else {
      cout << "Sorry Only Median is available for now, Mean and FastLinear are coming soon" << endl;
      delete theCommonMode;
      delete theMask;
      delete thePedestal;
      delete theNoise;
      return;
    }
  } else {
    cout << "ApvAnalysisFactory: algorithm " << theAlgorithmType_ << " not supported" << endl;
    delete theCommonMode;
    return;
  }

  if (theCommonMode)
    theCM->setCM(theCommonMode);
  if (thePedestal)
    theAPV->setPedestalCalculator(*thePedestal);
  if (theNoise)
    theAPV->setNoiseCalculator(*theNoise);
  if (theMask)
    theAPV->setMask(*theMask);
  if (theCM)
    theAPV->setCommonModeCalculator(*theCM);
}

void ApvAnalysisFactory::updatePair(uint32_t detId, size_t pairNumber, const edm::DetSet<SiStripRawDigi>& in) {
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
  if (apvAnalysisIt != apvMap_.end()) {
    size_t iter = 0;

    for (vector<ApvAnalysis*>::const_iterator apvIt = (apvAnalysisIt->second).begin();
         apvIt != (apvAnalysisIt->second).end();
         apvIt++) {
      if (iter == pairNumber * 2 || iter == (2 * pairNumber + 1)) {
        //      cout << "ApvAnalysisFactory::updatePair pair number " << pairNumber << endl;
        //      cout << "ApvAnlysis will be updated for the apv # " << iter << endl;

        edm::DetSet<SiStripRawDigi> tmpRawDigi;
        tmpRawDigi.data.reserve(128);

        size_t startStrip = 128 * (iter % 2);
        size_t stopStrip = startStrip + 128;

        for (size_t istrip = startStrip; istrip < stopStrip; istrip++) {
          if (in.data.size() <= istrip)
            tmpRawDigi.data.push_back(SiStripRawDigi(0));
          else
            tmpRawDigi.data.push_back(in.data[istrip]);  //maybe dangerous
        }

        (*apvIt)->newEvent();
        (*apvIt)->updateCalibration(tmpRawDigi);
      }

      iter++;
    }
  }

}  // void

void ApvAnalysisFactory::update(uint32_t detId, const edm::DetSet<SiStripRawDigi>& in) {
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
  if (apvAnalysisIt != apvMap_.end()) {
    size_t i = 0;
    for (vector<ApvAnalysis*>::const_iterator apvIt = (apvAnalysisIt->second).begin();
         apvIt != (apvAnalysisIt->second).end();
         apvIt++) {
      edm::DetSet<SiStripRawDigi> tmpRawDigi;
      //it is missing the detId ...
      tmpRawDigi.data.reserve(128);
      size_t startStrip = 128 * i;
      size_t stopStrip = startStrip + 128;

      for (size_t istrip = startStrip; istrip < stopStrip; istrip++) {
        if (in.data.size() <= istrip)
          tmpRawDigi.data.push_back(SiStripRawDigi(0));
        else
          tmpRawDigi.data.push_back(in.data[istrip]);  //maybe dangerous
      }

      (*apvIt)->newEvent();
      (*apvIt)->updateCalibration(tmpRawDigi);
      i++;
    }
  }
}

void ApvAnalysisFactory::getPedestal(uint32_t detId, int apvNumber, ApvAnalysis::PedestalType& peds) {
  //Get the pedestal for a given apv
  peds.clear();
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
  if (apvAnalysisIt != apvMap_.end()) {
    vector<ApvAnalysis*> myApvs = apvAnalysisIt->second;
    peds = myApvs[apvNumber]->pedestalCalculator().pedestal();
  }
}

void ApvAnalysisFactory::getPedestal(uint32_t detId, ApvAnalysis::PedestalType& peds) {
  //Get the pedestal for a given apv
  peds.clear();
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
  if (apvAnalysisIt != apvMap_.end()) {
    vector<ApvAnalysis*> theApvs = apvAnalysisIt->second;
    for (vector<ApvAnalysis*>::const_iterator it = theApvs.begin(); it != theApvs.end(); it++) {
      ApvAnalysis::PedestalType tmp = (*it)->pedestalCalculator().pedestal();
      for (ApvAnalysis::PedestalType::const_iterator pit = tmp.begin(); pit != tmp.end(); pit++)
        peds.push_back(*pit);
    }
  }
}
float ApvAnalysisFactory::getStripPedestal(uint32_t detId, int stripNumber) {
  //Get the pedestal for a given apv
  ApvAnalysis::PedestalType temp;
  int apvNumb = int(stripNumber / 128.);
  int stripN = (stripNumber - apvNumb * 128);

  getPedestal(detId, apvNumb, temp);
  return temp[stripN];
}
void ApvAnalysisFactory::getNoise(uint32_t detId, int apvNumber, ApvAnalysis::PedestalType& noise) {
  //Get the pedestal for a given apv
  noise.clear();
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
  if (apvAnalysisIt != apvMap_.end()) {
    vector<ApvAnalysis*> theApvs = apvAnalysisIt->second;

    noise = theApvs[apvNumber]->noiseCalculator().noise();
  }
}

float ApvAnalysisFactory::getStripNoise(uint32_t detId, int stripNumber) {
  //Get the pedestal for a given apv
  ApvAnalysis::PedestalType temp;
  int apvNumb = int(stripNumber / 128.);
  int stripN = (stripNumber - apvNumb * 128);

  getNoise(detId, apvNumb, temp);
  return temp[stripN];
}

void ApvAnalysisFactory::getNoise(uint32_t detId, ApvAnalysis::PedestalType& peds) {
  //Get the pedestal for a given apv
  peds.clear();
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator theApvs_map = apvMap_.find(detId);
  if (theApvs_map != apvMap_.end()) {
    vector<ApvAnalysis*>::const_iterator theApvs = (theApvs_map->second).begin();
    for (; theApvs != (theApvs_map->second).end(); theApvs++) {
      ApvAnalysis::PedestalType tmp = (*theApvs)->noiseCalculator().noise();
      for (ApvAnalysis::PedestalType::const_iterator pit = tmp.begin(); pit != tmp.end(); pit++)
        peds.push_back(*pit);
    }
  }
}

void ApvAnalysisFactory::getRawNoise(uint32_t detId, int apvNumber, ApvAnalysis::PedestalType& noise) {
  //Get the pedestal for a given apv
  noise.clear();
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
  if (apvAnalysisIt != apvMap_.end()) {
    vector<ApvAnalysis*> theApvs = apvAnalysisIt->second;

    noise = theApvs[apvNumber]->pedestalCalculator().rawNoise();
  }
}

float ApvAnalysisFactory::getStripRawNoise(uint32_t detId, int stripNumber) {
  //Get the pedestal for a given apv
  ApvAnalysis::PedestalType temp;
  int apvNumb = int(stripNumber / 128.);
  int stripN = (stripNumber - apvNumb * 128);

  getRawNoise(detId, apvNumb, temp);
  return temp[stripN];
}

void ApvAnalysisFactory::getRawNoise(uint32_t detId, ApvAnalysis::PedestalType& peds) {
  //Get the pedestal for a given apv
  peds.clear();
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator theApvs_map = apvMap_.find(detId);
  if (theApvs_map != apvMap_.end()) {
    vector<ApvAnalysis*>::const_iterator theApvs = (theApvs_map->second).begin();
    for (; theApvs != (theApvs_map->second).end(); theApvs++) {
      ApvAnalysis::PedestalType tmp = (*theApvs)->pedestalCalculator().rawNoise();
      for (ApvAnalysis::PedestalType::const_iterator pit = tmp.begin(); pit != tmp.end(); pit++)
        peds.push_back(*pit);
    }
  }
}

vector<float> ApvAnalysisFactory::getCommonMode(uint32_t detId, int apvNumber) {
  vector<float> tmp;
  tmp.clear();
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator theApvs_map = apvMap_.find(detId);
  if (theApvs_map != apvMap_.end()) {
    vector<ApvAnalysis*> theApvs = theApvs_map->second;

    tmp = theApvs[apvNumber]->commonModeCalculator().commonMode()->returnAsVector();
  }
  return tmp;
}
void ApvAnalysisFactory::getCommonMode(uint32_t detId, ApvAnalysis::PedestalType& tmp) {
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
  if (apvAnalysisIt != apvMap_.end()) {
    vector<ApvAnalysis*> theApvs = apvAnalysisIt->second;
    for (unsigned int i = 0; i < theApvs.size(); i++) {
      //To be fixed. We return only the first one in the vector.
      vector<float> tmp_cm = theApvs[i]->commonModeCalculator().commonMode()->returnAsVector();
      for (unsigned int it = 0; it < tmp_cm.size(); it++)
        tmp.push_back(tmp_cm[it]);
    }
  }
}

void ApvAnalysisFactory::getMask(uint32_t det_id, TkApvMask::MaskType& tmp) {
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(det_id);
  if (apvAnalysisIt != apvMap_.end()) {
    vector<ApvAnalysis*> theApvs = apvAnalysisIt->second;
    for (unsigned int i = 0; i < theApvs.size(); i++) {
      TkApvMask::MaskType theMaskType = (theApvs[i]->mask()).mask();
      //cout <<"theMaskType size "<<theMaskType.size()<<endl;

      for (unsigned int ii = 0; ii < theMaskType.size(); ii++) {
        tmp.push_back(theMaskType[ii]);
        //cout <<"The Value "<<theMaskType[ii]<<" "<<ii<<endl;
      }
    }
  }
}
bool ApvAnalysisFactory::isUpdating(uint32_t detId) {
  bool updating = true;
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
  if (apvAnalysisIt != apvMap_.end()) {
    for (vector<ApvAnalysis*>::const_iterator apvIt = (apvAnalysisIt->second).begin();
         apvIt != (apvAnalysisIt->second).end();
         apvIt++) {
      if (!((*apvIt)->pedestalCalculator().status()->isUpdating()))
        updating = false;
    }
  }
  return updating;
}

void ApvAnalysisFactory::deleteApv(ApvAnalysis* apv) {
  delete &(apv->pedestalCalculator());
  delete &(apv->noiseCalculator());
  delete &(apv->mask());
  delete &(apv->commonModeCalculator().commonMode()->topology());
  delete (apv->commonModeCalculator().commonMode());
  delete &(apv->commonModeCalculator());
  delete apv;
}
//
// -- Get Common Mode Slope
//
float ApvAnalysisFactory::getCommonModeSlope(uint32_t detId, int apvNumber) {
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator theApvs_map = apvMap_.find(detId);
  float tmp = -100.0;
  if (theApvs_map != apvMap_.end()) {
    vector<ApvAnalysis*> theApvs = theApvs_map->second;
    tmp = theApvs[apvNumber]->commonModeCalculator().getCMSlope();
    return tmp;
  }
  return tmp;
}
void ApvAnalysisFactory::getCommonModeSlope(uint32_t detId, ApvAnalysis::PedestalType& tmp) {
  tmp.clear();
  map<uint32_t, vector<ApvAnalysis*> >::const_iterator apvAnalysisIt = apvMap_.find(detId);
  if (apvAnalysisIt != apvMap_.end()) {
    vector<ApvAnalysis*> theApvs = apvAnalysisIt->second;
    for (unsigned int i = 0; i < theApvs.size(); i++) {
      tmp.push_back(theApvs[i]->commonModeCalculator().getCMSlope());
    }
  }
}