Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:07:22

0001 #include "DQM/EcalMonitorTasks/interface/SelectiveReadoutTask.h"
0002 
0003 #include "FWCore/Framework/interface/ESHandle.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 
0006 #include "Geometry/CaloTopology/interface/EcalTrigTowerConstituentsMap.h"
0007 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0008 
0009 #include "DataFormats/EcalDetId/interface/EcalDetIdCollections.h"
0010 
0011 #include "DQM/EcalCommon/interface/FEFlags.h"
0012 #include "DQM/EcalCommon/interface/EcalDQMCommonUtils.h"
0013 #include "FWCore/Framework/interface/ConsumesCollector.h"
0014 
0015 namespace ecaldqm {
0016 
0017   SelectiveReadoutTask::SelectiveReadoutTask()
0018       : DQWorkerTask(),
0019         useCondDb_(false),
0020         iFirstSample_(0),
0021         ZSFIRWeights_(nFIRTaps, 0.),
0022         suppressed_(),
0023         flags_(nRU, -1) {}
0024 
0025   void SelectiveReadoutTask::setParams(edm::ParameterSet const& _params) {
0026     useCondDb_ = _params.getUntrackedParameter<bool>("useCondDb");
0027     iFirstSample_ = _params.getUntrackedParameter<int>("DCCZS1stSample");
0028 
0029     if (!useCondDb_) {
0030       std::vector<double> normWeights(_params.getUntrackedParameter<std::vector<double> >("ZSFIRWeights"));
0031       setFIRWeights_(normWeights);
0032     }
0033   }
0034 
0035   void SelectiveReadoutTask::setTokens(edm::ConsumesCollector& _collector) {
0036     hSr = _collector.esConsumes<edm::Transition::BeginRun>();
0037   }
0038 
0039   void SelectiveReadoutTask::addDependencies(DependencySet& _dependencies) {
0040     _dependencies.push_back(Dependency(kEBDigi, kEcalRawData, kEBSrFlag));
0041     _dependencies.push_back(Dependency(kEEDigi, kEcalRawData, kEESrFlag));
0042   }
0043 
0044   void SelectiveReadoutTask::beginRun(edm::Run const&, edm::EventSetup const& _es) {
0045     using namespace std;
0046 
0047     if (useCondDb_) {
0048       auto const& vSr = &_es.getData(hSr);
0049       vector<vector<float> > weights(vSr->dccNormalizedWeights_);
0050       if (weights.size() == 1) {
0051         vector<double> normWeights;
0052         for (vector<float>::iterator it(weights[0].begin()); it != weights[0].end(); it++)
0053           normWeights.push_back(*it);
0054 
0055         setFIRWeights_(normWeights);
0056       } else
0057         edm::LogWarning("EcalDQM") << "SelectiveReadoutTask: DCC weight set is not exactly 1.";
0058     }
0059   }
0060 
0061   void SelectiveReadoutTask::beginEvent(edm::Event const&, edm::EventSetup const&, bool const&, bool&) {
0062     flags_.assign(nRU, -1);
0063     suppressed_.clear();
0064   }
0065 
0066   void SelectiveReadoutTask::runOnSource(FEDRawDataCollection const& _fedRaw) {
0067     MESet& meDCCSize(MEs_.at("DCCSize"));
0068     MESet& meDCCSizeProf(MEs_.at("DCCSizeProf"));
0069     MESet& meEventSize(MEs_.at("EventSize"));
0070 
0071     float ebSize(0.), eemSize(0.), eepSize(0.);
0072 
0073     // DCC event size
0074     for (int iFED(601); iFED <= 654; iFED++) {
0075       float size(_fedRaw.FEDData(iFED).size() / 1024.);
0076       meDCCSize.fill(getEcalDQMSetupObjects(), iFED - 600, size);
0077       meDCCSizeProf.fill(getEcalDQMSetupObjects(), iFED - 600, size);
0078       if (iFED - 601 <= kEEmHigh)
0079         eemSize += size;
0080       else if (iFED - 601 >= kEEpLow)
0081         eepSize += size;
0082       else
0083         ebSize += size;
0084     }
0085 
0086     meEventSize.fill(getEcalDQMSetupObjects(), -EcalEndcap, eemSize / 9.);
0087     meEventSize.fill(getEcalDQMSetupObjects(), EcalEndcap, eepSize / 9.);
0088     meEventSize.fill(getEcalDQMSetupObjects(), EcalBarrel, ebSize / 36.);
0089   }
0090 
0091   void SelectiveReadoutTask::runOnRawData(EcalRawDataCollection const& _dcchs) {
0092     for (EcalRawDataCollection::const_iterator dcchItr(_dcchs.begin()); dcchItr != _dcchs.end(); ++dcchItr) {
0093       std::vector<short> const& feStatus(dcchItr->getFEStatus());
0094       unsigned nFE(feStatus.size());
0095       for (unsigned iFE(0); iFE < nFE; ++iFE)
0096         if (feStatus[iFE] == Disabled)
0097           suppressed_.insert(std::make_pair(dcchItr->id(), iFE + 1));
0098     }
0099   }
0100 
0101   template <typename SRFlagCollection>
0102   void SelectiveReadoutTask::runOnSrFlags(SRFlagCollection const& _srfs, Collections _col) {
0103     MESet& meFlagCounterMap(MEs_.at("FlagCounterMap"));
0104     MESet& meFullReadoutMap(MEs_.at("FullReadoutMap"));
0105     MESet& meZS1Map(MEs_.at("ZS1Map"));
0106     MESet& meZSMap(MEs_.at("ZSMap"));
0107     MESet& meRUForcedMap(MEs_.at("RUForcedMap"));
0108 
0109     double nFR(0.);
0110 
0111     std::for_each(_srfs.begin(), _srfs.end(), [&](typename SRFlagCollection::value_type const& srf) {
0112       DetId const& id(srf.id());
0113       int flag(srf.value());
0114 
0115       meFlagCounterMap.fill(getEcalDQMSetupObjects(), id);
0116 
0117       unsigned iRU(-1);
0118       if (id.subdetId() == EcalTriggerTower)
0119         iRU = EcalTrigTowerDetId(id).hashedIndex();
0120       else
0121         iRU = EcalScDetId(id).hashedIndex() + EcalTrigTowerDetId::kEBTotalTowers;
0122       flags_[iRU] = flag;
0123 
0124       switch (flag & ~EcalSrFlag::SRF_FORCED_MASK) {
0125         case EcalSrFlag::SRF_FULL:
0126           meFullReadoutMap.fill(getEcalDQMSetupObjects(), id);
0127           nFR += 1.;
0128           break;
0129         case EcalSrFlag::SRF_ZS1:
0130           meZS1Map.fill(getEcalDQMSetupObjects(), id);
0131           // fallthrough
0132         case EcalSrFlag::SRF_ZS2:
0133           meZSMap.fill(getEcalDQMSetupObjects(), id);
0134           break;
0135         default:
0136           break;
0137       }
0138 
0139       if (flag & EcalSrFlag::SRF_FORCED_MASK)
0140         meRUForcedMap.fill(getEcalDQMSetupObjects(), id);
0141     });
0142 
0143     MEs_.at("FullReadout").fill(getEcalDQMSetupObjects(), _col == kEBSrFlag ? EcalBarrel : EcalEndcap, nFR);
0144   }
0145 
0146   template <typename DigiCollection>
0147   void SelectiveReadoutTask::runOnDigis(DigiCollection const& _digis, Collections _collection) {
0148     MESet& meHighIntOutput(MEs_.at("HighIntOutput"));
0149     MESet& meLowIntOutput(MEs_.at("LowIntOutput"));
0150     MESet& meHighIntPayload(MEs_.at("HighIntPayload"));
0151     MESet& meLowIntPayload(MEs_.at("LowIntPayload"));
0152     MESet& meTowerSize(MEs_.at("TowerSize"));
0153     MESet& meZSFullReadoutMap(MEs_.at("ZSFullReadoutMap"));
0154     MESet& meFRDroppedMap(MEs_.at("FRDroppedMap"));
0155     MESet& meZSFullReadout(MEs_.at("ZSFullReadout"));
0156     MESet& meFRDropped(MEs_.at("FRDropped"));
0157 
0158     bool isEB(_collection == kEBDigi);
0159 
0160     unsigned const nTower(isEB ? unsigned(EcalTrigTowerDetId::kEBTotalTowers)
0161                                : unsigned(EcalScDetId::kSizeForDenseIndexing));
0162 
0163     std::vector<unsigned> sizes(nTower, 0);
0164 
0165     int nHighInt[] = {0, 0};
0166     int nLowInt[] = {0, 0};
0167 
0168     for (typename DigiCollection::const_iterator digiItr(_digis.begin()); digiItr != _digis.end(); ++digiItr) {
0169       DetId const& id(digiItr->id());
0170 
0171       unsigned iTower(-1);
0172       unsigned iRU(-1);
0173 
0174       if (isEB) {
0175         iTower = EBDetId(id).tower().hashedIndex();
0176         iRU = iTower;
0177       } else {
0178         iTower = EEDetId(id).sc().hashedIndex();
0179         iRU = iTower + EcalTrigTowerDetId::kEBTotalTowers;
0180       }
0181 
0182       if (flags_[iRU] < 0)
0183         continue;
0184 
0185       sizes[iTower] += 1;
0186 
0187       // SR filter output calculation
0188 
0189       EcalDataFrame frame(*digiItr);
0190 
0191       int ZSFIRValue(0);  // output
0192 
0193       bool gain12saturated(false);
0194       const int gain12(0x01);
0195 
0196       for (int iWeight(0); iWeight < nFIRTaps; ++iWeight) {
0197         int iSample(iFirstSample_ + iWeight - 1);
0198 
0199         if (iSample >= 0 && iSample < frame.size()) {
0200           EcalMGPASample sample(frame[iSample]);
0201           if (sample.gainId() != gain12) {
0202             gain12saturated = true;
0203             break;
0204           }
0205           ZSFIRValue += sample.adc() * ZSFIRWeights_[iWeight];
0206         } else {
0207           edm::LogWarning("EcalDQM") << "SelectiveReadoutTask: Not enough samples in data frame or "
0208                                         "'ecalDccZs1stSample' module parameter is not valid";
0209         }
0210       }
0211 
0212       if (gain12saturated)
0213         ZSFIRValue = std::numeric_limits<int>::max();
0214       else
0215         ZSFIRValue /= (0x1 << 8);  //discards the 8 LSBs
0216 
0217       //ZS passed if weighted sum above ZS threshold or if
0218       //one sample has a lower gain than gain 12 (that is gain 12 output
0219       //is saturated)
0220 
0221       bool highInterest((flags_[iRU] & ~EcalSrFlag::SRF_FORCED_MASK) == EcalSrFlag::SRF_FULL);
0222 
0223       if (highInterest) {
0224         meHighIntOutput.fill(getEcalDQMSetupObjects(), id, ZSFIRValue);
0225         if (isEB || dccId(id, GetElectronicsMap()) - 1 <= kEEmHigh)
0226           nHighInt[0] += 1;
0227         else
0228           nHighInt[1] += 1;
0229       } else {
0230         meLowIntOutput.fill(getEcalDQMSetupObjects(), id, ZSFIRValue);
0231         if (isEB || dccId(id, GetElectronicsMap()) - 1 <= kEEmHigh)
0232           nLowInt[0] += 1;
0233         else
0234           nLowInt[1] += 1;
0235       }
0236     }
0237 
0238     if (isEB) {
0239       meHighIntPayload.fill(getEcalDQMSetupObjects(), EcalBarrel, nHighInt[0] * bytesPerCrystal / 1024. / nEBDCC);
0240       meLowIntPayload.fill(getEcalDQMSetupObjects(), EcalBarrel, nLowInt[0] * bytesPerCrystal / 1024. / nEBDCC);
0241     } else {
0242       meHighIntPayload.fill(
0243           getEcalDQMSetupObjects(), -EcalEndcap, nHighInt[0] * bytesPerCrystal / 1024. / (nEEDCC / 2));
0244       meHighIntPayload.fill(getEcalDQMSetupObjects(), EcalEndcap, nHighInt[1] * bytesPerCrystal / 1024. / (nEEDCC / 2));
0245       meLowIntPayload.fill(getEcalDQMSetupObjects(), -EcalEndcap, nLowInt[0] * bytesPerCrystal / 1024. / (nEEDCC / 2));
0246       meLowIntPayload.fill(getEcalDQMSetupObjects(), EcalEndcap, nLowInt[1] * bytesPerCrystal / 1024. / (nEEDCC / 2));
0247     }
0248 
0249     unsigned iRU(isEB ? 0 : EcalTrigTowerDetId::kEBTotalTowers);
0250     for (unsigned iTower(0); iTower < nTower; ++iTower, ++iRU) {
0251       DetId id;
0252       if (isEB)
0253         id = EcalTrigTowerDetId::detIdFromDenseIndex(iTower);
0254       else
0255         id = EcalScDetId::unhashIndex(iTower);
0256 
0257       double towerSize(sizes[iTower] * bytesPerCrystal);
0258 
0259       meTowerSize.fill(getEcalDQMSetupObjects(), id, towerSize);
0260 
0261       if (flags_[iRU] < 0)
0262         continue;
0263 
0264       int dccid(dccId(id, GetElectronicsMap()));
0265       int towerid(towerId(id, GetElectronicsMap()));
0266 
0267       if (suppressed_.find(std::make_pair(dccid, towerid)) != suppressed_.end())
0268         continue;
0269 
0270       int flag(flags_[iRU] & ~EcalSrFlag::SRF_FORCED_MASK);
0271 
0272       bool ruFullyReadout(sizes[iTower] == GetElectronicsMap()->dccTowerConstituents(dccid, towerid).size());
0273 
0274       if (ruFullyReadout && (flag == EcalSrFlag::SRF_ZS1 || flag == EcalSrFlag::SRF_ZS2)) {
0275         meZSFullReadoutMap.fill(getEcalDQMSetupObjects(), id);
0276         meZSFullReadout.fill(getEcalDQMSetupObjects(), id);
0277       }
0278 
0279       if (sizes[iTower] == 0 && flag == EcalSrFlag::SRF_FULL) {
0280         meFRDroppedMap.fill(getEcalDQMSetupObjects(), id);
0281         meFRDropped.fill(getEcalDQMSetupObjects(), id);
0282       }
0283     }
0284   }
0285 
0286   void SelectiveReadoutTask::setFIRWeights_(std::vector<double> const& _normWeights) {
0287     if (_normWeights.size() < nFIRTaps)
0288       throw cms::Exception("InvalidConfiguration") << "weightsForZsFIR" << std::endl;
0289 
0290     bool notNormalized(false), notInt(false);
0291     for (std::vector<double>::const_iterator it(_normWeights.begin()); it != _normWeights.end(); ++it) {
0292       if (*it > 1.)
0293         notNormalized = true;
0294       if (int(*it) != *it)
0295         notInt = true;
0296     }
0297     if (notInt && notNormalized) {
0298       throw cms::Exception("InvalidConfiguration") << "weigtsForZsFIR paramater values are not valid: they "
0299                                                    << "must either be integer and uses the hardware representation "
0300                                                    << "of the weights or less or equal than 1 and used the normalized "
0301                                                    << "representation.";
0302     }
0303 
0304     ZSFIRWeights_.clear();
0305     ZSFIRWeights_.resize(_normWeights.size());
0306 
0307     if (notNormalized) {
0308       for (unsigned i(0); i < ZSFIRWeights_.size(); ++i)
0309         ZSFIRWeights_[i] = int(_normWeights[i]);
0310     } else {
0311       const int maxWeight(0xEFF);  //weights coded on 11+1 signed bits
0312       for (unsigned i(0); i < ZSFIRWeights_.size(); ++i) {
0313         ZSFIRWeights_[i] = lround(_normWeights[i] * (0x1 << 10));
0314         if (std::abs(ZSFIRWeights_[i]) > maxWeight)  //overflow
0315           ZSFIRWeights_[i] = ZSFIRWeights_[i] < 0 ? -maxWeight : maxWeight;
0316       }
0317     }
0318   }
0319 
0320   DEFINE_ECALDQM_WORKER(SelectiveReadoutTask);
0321 }  // namespace ecaldqm