Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-31 23:14:00

0001 #include "DQM/SiStripMonitorHardware/interface/SiStripSpyEventMatcher.h"
0002 #include "DQM/SiStripMonitorHardware/interface/SiStripFEDEmulator.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 
0005 #include <functional>
0006 
0007 using edm::LogError;
0008 using edm::LogInfo;
0009 using edm::LogWarning;
0010 
0011 namespace sistrip {
0012 
0013   const char* const FEDEmulator::messageLabel_ = "SiStripFEDEmulator";
0014 
0015   FEDEmulator::FEDEmulator() {
0016     byModule_ = false;
0017     minStrip_ = 0;
0018     maxStrip_ = 0;
0019     pedestals_.clear();
0020     noises_.clear();
0021     medians_.clear();
0022 
0023     detId_ = 0;
0024     nPairs_ = 0;
0025     pair_ = 0;
0026   }
0027 
0028   FEDEmulator::~FEDEmulator() {}
0029 
0030   void FEDEmulator::initialise(const bool byModule) {
0031     byModule_ = byModule;
0032     pedestals_.clear();
0033     noises_.clear();
0034     medians_.clear();
0035   }
0036 
0037   void FEDEmulator::initialiseModule(const uint32_t aDetId, const uint32_t aNPairs, const uint32_t aPair) {
0038     detId_ = aDetId;
0039     nPairs_ = aNPairs;
0040     pair_ = aPair;
0041 
0042     minStrip_ = 0;
0043     maxStrip_ = nPairs_ * sistrip::STRIPS_PER_FEDCH;
0044     if (!byModule_) {
0045       minStrip_ = sistrip::STRIPS_PER_FEDCH * pair_;
0046       maxStrip_ = sistrip::STRIPS_PER_FEDCH * (pair_ + 1);
0047     }
0048 
0049     //need resize because it will not be filled by push_back...
0050     pedestals_.clear();
0051     pedestals_.resize(nPairs_ * sistrip::STRIPS_PER_FEDCH);
0052 
0053     noises_.clear();
0054     noises_.resize(nPairs_ * sistrip::STRIPS_PER_FEDCH);
0055 
0056     medians_.clear();
0057     medians_.reserve(nPairs_ * 2);
0058   }
0059 
0060   void FEDEmulator::retrievePedestals(const edm::ESHandle<SiStripPedestals>& aHandle) {
0061     try {
0062       SiStripPedestals::Range pedestalsRange = aHandle->getRange(detId_);
0063       aHandle->allPeds(pedestals_, pedestalsRange);
0064     } catch (const cms::Exception& e) {
0065       LogError(messageLabel_) << " Something wrong whilst getting pedestals: size = " << pedestals_.size()
0066                               << " detId = " << detId_ << std::endl
0067                               << e.what();
0068       //throw cms::Exception("ProductNotFound") << "Problem with pedestals..." ;
0069     }
0070   }  //end of retrievePedestals method.
0071 
0072   void FEDEmulator::retrieveNoises(const edm::ESHandle<SiStripNoises>& aHandle) {
0073     try {
0074       SiStripNoises::Range noisesRange = aHandle->getRange(detId_);
0075       aHandle->allNoises(noises_, noisesRange);
0076     } catch (const cms::Exception& e) {
0077       LogError(messageLabel_) << " Something wrong whilst getting noises: size = " << noises_.size()
0078                               << " detId = " << detId_ << std::endl
0079                               << e.what();
0080       //throw cms::Exception("ProductNotFound") << "Problem with noises..." ;
0081     }
0082   }  //end of retrieveNoises method.
0083 
0084   void FEDEmulator::subtractPedestals(const edm::DetSetVector<SiStripRawDigi>::const_iterator& inputChannel,
0085                                       std::vector<SiStripRawDigi>& pedsDetSetData,
0086                                       std::vector<SiStripProcessedRawDigi>& noiseDetSetData,
0087                                       std::vector<SiStripRawDigi>& pedSubtrDetSetData,
0088                                       std::vector<uint32_t>& medsDetSetData,
0089                                       const bool fillApvsForCM) {
0090     edm::DetSet<SiStripRawDigi>::const_iterator digi_it = inputChannel->begin();
0091     ////for median calculation
0092     uint32_t lCount = 0;
0093     std::vector<std::vector<uint16_t> > apvs;
0094     apvs.reserve(nPairs_ * 2);
0095 
0096     //subtract pedestals and fill apv vector
0097     //for median calculation
0098     //in a module, 256*nPairs, but here we are dealing
0099     //channel by channel, so 256 values at a time in the pedestal object.
0100     //reordered = 0-127 APV0 128-255 APV1
0101 
0102     std::vector<uint16_t> apvVec;
0103     ;
0104     apvVec.reserve(sistrip::STRIPS_PER_APV);
0105 
0106     for (uint32_t st = minStrip_; st < maxStrip_; st++) {
0107       uint16_t ped = static_cast<uint16_t>(pedestals_[st]);
0108       pedsDetSetData.push_back(SiStripRawDigi(ped));
0109 
0110       float noise = noises_[st];
0111       noiseDetSetData.push_back(SiStripProcessedRawDigi(noise));
0112 
0113       if (digi_it == inputChannel->end()) {
0114         LogError(messageLabel_) << " Error, end of inputchannel reached for detid " << detId_ << "! Processed "
0115                                 << lCount << " strips." << std::endl;
0116         break;
0117       }
0118 
0119       int lVal = digi_it->adc() - ped;
0120       if (lVal < 0) {
0121         if (digi_it->adc() > 0)
0122           LogDebug(messageLabel_) << " Negative value after pedestal subtraction ! Detid = " << detId_ << ", strip "
0123                                   << st << ", digi = " << digi_it->adc() << ", ped = " << ped << std::endl;
0124         lVal = 0;
0125       }
0126       pedSubtrDetSetData.push_back(SiStripRawDigi(static_cast<uint16_t>(lVal)));
0127 
0128       if (fillApvsForCM) {
0129         apvVec.push_back(static_cast<uint16_t>(lVal));
0130       }
0131 
0132       lCount++;
0133       ++digi_it;
0134 
0135       if (lCount % sistrip::STRIPS_PER_APV == 0 && fillApvsForCM) {
0136         if (!apvVec.empty())
0137           apvs.push_back(apvVec);
0138         apvVec.clear();
0139         apvVec.reserve(sistrip::STRIPS_PER_APV);
0140       }
0141 
0142     }  //end of loop over strips
0143 
0144     if (fillApvsForCM) {
0145       if (apvs.size() != nPairs_ * 2) {
0146         LogError(messageLabel_) << " Error, wrong output size for median calculation for detid " << detId_
0147                                 << "! Processed " << apvs.size() << " APVs." << std::endl;
0148       }
0149 
0150       //calculate common mode values
0151       for (uint32_t iapv(0); iapv < apvs.size(); iapv++) {
0152         std::vector<uint16_t> lVec = apvs[iapv];
0153 
0154         std::vector<uint16_t>::iterator mid = lVec.begin() + (lVec.size() / 2 - 1);
0155         std::nth_element(lVec.begin(), mid, lVec.end());
0156 
0157         //std::sort(lVec.begin(),lVec.end());
0158         //get the 64th element
0159         //if (*mid != lVec.at(63)) exit(0);
0160         //medians_.push_back(lVec.at(63));
0161         //medsDetSetData.push_back(lVec.at(63));
0162         medians_.push_back(*mid);
0163         medsDetSetData.push_back(*mid);
0164       }
0165     }
0166 
0167   }  //end of FEDEmulator::subtractPedestals method.
0168 
0169   void FEDEmulator::subtractCM(const std::vector<SiStripRawDigi>& pedSubtrDetSetData,
0170                                std::vector<SiStripRawDigi>& cmSubtrDetSetData) {
0171     //subtract Medians
0172     std::vector<SiStripRawDigi>::const_iterator lDigi = pedSubtrDetSetData.begin();
0173     uint32_t lCount = 0;
0174 
0175     for (uint32_t st = minStrip_; st < maxStrip_; st++) {
0176       uint32_t lApvNum = static_cast<uint32_t>(lCount * 1. / sistrip::STRIPS_PER_APV);
0177 
0178       int value = lDigi->adc() - medians_.at(lApvNum);
0179       if (value < 0) {
0180         //FED doesn't handle negative values
0181         value = 0;
0182       }
0183       cmSubtrDetSetData.push_back(SiStripRawDigi(static_cast<uint16_t>(value)));
0184 
0185       ++lDigi;
0186       lCount++;
0187     }
0188   }
0189 
0190   void FEDEmulator::zeroSuppress(const std::vector<SiStripRawDigi>& cmSubtrDetSetData,
0191                                  edm::DetSet<SiStripDigi>& zsDetSetData,
0192                                  const std::unique_ptr<SiStripRawProcessingAlgorithms>& algorithms) {
0193     //transform the input digis to a vector of integers
0194     std::vector<int16_t> cmSubtrRawDigis;
0195     transform(cmSubtrDetSetData.begin(),
0196               cmSubtrDetSetData.end(),
0197               back_inserter(cmSubtrRawDigis),
0198               std::bind(&SiStripRawDigi::adc, std::placeholders::_1));
0199     algorithms->suppressor->suppress(cmSubtrRawDigis, 0, zsDetSetData);
0200 
0201   }  //end of FEDEmulator::zeroSuppress method.
0202 
0203   void FEDEmulator::fillPeds(const edm::DetSetVector<SiStripRawDigi>::const_iterator& peds) {
0204     for (edm::DetSet<SiStripRawDigi>::const_iterator iApv = peds->begin(); iApv != peds->end(); ++iApv) {
0205       pedestals_.push_back(iApv->adc());
0206     }
0207   }
0208 
0209   void FEDEmulator::fillNoises(const edm::DetSetVector<SiStripProcessedRawDigi>::const_iterator& noises) {
0210     for (edm::DetSet<SiStripProcessedRawDigi>::const_iterator iApv = noises->begin(); iApv != noises->end(); ++iApv) {
0211       noises_.push_back(iApv->adc());
0212     }
0213   }
0214 
0215   void FEDEmulator::fillMedians(const std::map<uint32_t, std::vector<uint32_t> >::const_iterator& meds) {
0216     std::vector<uint32_t>::const_iterator iApv = (meds->second).begin();
0217     for (; iApv != (meds->second).end(); ++iApv) {
0218       medians_.push_back(*iApv);
0219     }
0220   }
0221 
0222   void FEDEmulator::print(std::ostream& aOs) {
0223     aOs << "===============================" << std::endl
0224         << " ===== FEDEmulator::print =====" << std::endl
0225         << " === byModule = " << byModule_ << std::endl
0226         << " === minStrip = " << minStrip_ << std::endl
0227         << " === maxStrip = " << maxStrip_ << std::endl
0228         << " === size of pedestals = " << pedestals_.size() << std::endl
0229         << " === size of noises = " << noises_.size() << std::endl
0230         << " === size of medians = " << medians_.size() << std::endl
0231         << " === detId = " << detId_ << std::endl
0232         << " === nPairs = " << nPairs_ << std::endl
0233         << " === pair = " << pair_ << std::endl
0234         << "===============================" << std::endl;
0235   }
0236 
0237   void FEDEmulator::printPeds(std::ostream& aOs) {
0238     aOs << "=========================================" << std::endl
0239         << " ===== FEDEmulator::printPedestals =====" << std::endl;
0240     for (unsigned int i(0); i < pedestals_.size(); i++) {
0241       if (i % 32 == 0)
0242         aOs << std::endl;
0243       aOs << pedestals_[i] << " ";
0244     }
0245     aOs << "=========================================" << std::endl;
0246   }
0247 
0248   void FEDEmulator::printNoises(std::ostream& aOs) {
0249     aOs << "=========================================" << std::endl
0250         << " ===== FEDEmulator::printNoises =====" << std::endl;
0251     for (unsigned int i(0); i < noises_.size(); i++) {
0252       if (i % 32 == 0)
0253         aOs << std::endl;
0254       aOs << noises_[i] << " ";
0255     }
0256     aOs << "=========================================" << std::endl;
0257   }
0258 
0259   void FEDEmulator::printMeds(std::ostream& aOs) {
0260     aOs << "=========================================" << std::endl
0261         << " ===== FEDEmulator::printMedians =====" << std::endl;
0262     for (unsigned int i(0); i < medians_.size(); i++) {
0263       if (i % 32 == 0)
0264         aOs << std::endl;
0265       aOs << medians_[i] << " ";
0266     }
0267     aOs << "=========================================" << std::endl;
0268   }
0269 
0270 }  //namespace sistrip