File indexing completed on 2024-04-06 12:26:32
0001 #include "FWCore/Framework/interface/stream/EDProducer.h"
0002
0003 #include "FWCore/Framework/interface/Event.h"
0004 #include "FWCore/Framework/interface/EventSetup.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007 #include "DataFormats/Common/interface/Handle.h"
0008 #include "DataFormats/Common/interface/DetSetVector.h"
0009 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
0010 #include "DataFormats/SiStripDigi/interface/SiStripRawDigi.h"
0011 #include "RecoLocalTracker/SiStripZeroSuppression/interface/SiStripRawProcessingAlgorithms.h"
0012 #include "RecoLocalTracker/SiStripZeroSuppression/interface/SiStripRawProcessingFactory.h"
0013
0014 class SiStripMergeZeroSuppression : public edm::stream::EDProducer<> {
0015 public:
0016 explicit SiStripMergeZeroSuppression(const edm::ParameterSet& conf);
0017 void produce(edm::Event& event, const edm::EventSetup& eventSetup) override;
0018
0019 private:
0020 std::unique_ptr<SiStripRawProcessingAlgorithms> m_algorithms;
0021
0022 using rawtoken_t = edm::EDGetTokenT<edm::DetSetVector<SiStripRawDigi>>;
0023 using zstoken_t = edm::EDGetTokenT<edm::DetSetVector<SiStripDigi>>;
0024 rawtoken_t m_rawDigisToMerge;
0025 zstoken_t m_zsDigisToMerge;
0026 };
0027
0028 SiStripMergeZeroSuppression::SiStripMergeZeroSuppression(const edm::ParameterSet& conf)
0029 : m_algorithms(SiStripRawProcessingFactory::create(conf.getParameter<edm::ParameterSet>("Algorithms"),
0030 consumesCollector())) {
0031 m_rawDigisToMerge = consumes<edm::DetSetVector<SiStripRawDigi>>(conf.getParameter<edm::InputTag>("DigisToMergeVR"));
0032 m_zsDigisToMerge = consumes<edm::DetSetVector<SiStripDigi>>(conf.getParameter<edm::InputTag>("DigisToMergeZS"));
0033 produces<edm::DetSetVector<SiStripDigi>>("ZeroSuppressed");
0034 }
0035
0036 void SiStripMergeZeroSuppression::produce(edm::Event& event, const edm::EventSetup& ) {
0037 LogTrace("SiStripMergeZeroSuppression::produce") << "Starting merging "
0038 << "\n";
0039 edm::Handle<edm::DetSetVector<SiStripDigi>> inputdigi;
0040 edm::Handle<edm::DetSetVector<SiStripRawDigi>> inputraw;
0041 event.getByToken(m_rawDigisToMerge, inputdigi);
0042 event.getByToken(m_zsDigisToMerge, inputraw);
0043
0044 LogTrace("SiStripMergeZeroSuppression::produce") << inputdigi->size() << " " << inputraw->size() << "\n";
0045 if (!inputraw->empty()) {
0046 std::vector<edm::DetSet<SiStripDigi>> outputdigi(inputdigi->begin(), inputdigi->end());
0047
0048 LogTrace("SiStripMergeZeroSuppression::produce") << "Looping over the raw data collection "
0049 << "\n";
0050 for (const auto& rawDigis : *inputraw) {
0051 edm::DetSet<SiStripRawDigi>::const_iterator itRawDigis = rawDigis.begin();
0052 uint16_t nAPV = rawDigis.size() / 128;
0053 uint32_t rawDetId = rawDigis.id;
0054
0055 std::vector<bool> restoredAPV(std::size_t(nAPV), false);
0056
0057 bool isModuleRestored = false;
0058 for (uint16_t strip = 0; strip < rawDigis.size(); ++strip) {
0059 if (itRawDigis[strip].adc() != 0) {
0060 restoredAPV[strip / 128] = true;
0061 isModuleRestored = true;
0062 }
0063 }
0064
0065 if (isModuleRestored) {
0066 LogTrace("SiStripMergeZeroSuppression::produce") << "Apply the ZS to the raw data collection "
0067 << "\n";
0068 edm::DetSet<SiStripDigi> suppressedDigis(rawDetId);
0069 m_algorithms->suppressVirginRawData(rawDigis, suppressedDigis);
0070
0071 if (!suppressedDigis.empty()) {
0072 LogTrace("SiStripMergeZeroSuppression::produce")
0073 << "Looking for the detId with the new ZS in the collection of the zero suppressed data"
0074 << "\n";
0075 auto zsModule = std::lower_bound(
0076 std::begin(outputdigi),
0077 std::end(outputdigi),
0078 rawDetId,
0079 [](const edm::DetSet<SiStripDigi>& elem, uint32_t testDetId) { return elem.id < testDetId; });
0080 const bool isModuleInZscollection = ((std::end(outputdigi) != zsModule) && (zsModule->id == rawDetId));
0081 LogTrace("SiStripMergeZeroSuppression::produce")
0082 << "After the look " << rawDetId << " ==== " << (isModuleInZscollection ? zsModule->id : 0) << "\n";
0083 LogTrace("SiStripMergeZeroSuppression::produce")
0084 << "Exiting looking for the detId with the new ZS in the collection of the zero suppressed data"
0085 << "\n";
0086
0087
0088 std::vector<uint16_t> mergedRawDigis(size_t(nAPV * 128), 0);
0089
0090 uint32_t count = 0;
0091 edm::DetSet<SiStripDigi> newDigiToInsert(rawDetId);
0092 if (!isModuleInZscollection) {
0093 LogTrace("SiStripMergeZeroSuppression::produce") << "WE HAVE A PROBLEM, THE MODULE IS NTOT FOUND"
0094 << "\n";
0095 zsModule = outputdigi.insert(zsModule, newDigiToInsert);
0096 LogTrace("SiStripMergeZeroSuppression::produce") << "new module id -1 " << (zsModule - 1)->id << "\n";
0097 LogTrace("SiStripMergeZeroSuppression::produce") << "new module id " << zsModule->id << "\n";
0098 LogTrace("SiStripMergeZeroSuppression::produce") << "new module id +1 " << (zsModule + 1)->id << "\n";
0099 } else {
0100 LogTrace("SiStripMergeZeroSuppression::produce") << "inserting only the digis for not restored APVs"
0101 << "\n";
0102 LogTrace("SiStripMergeZeroSuppression::produce") << "size : " << zsModule->size() << "\n";
0103 for (const auto itZsMod : *zsModule) {
0104 const uint16_t adc = itZsMod.adc();
0105 const uint16_t strip = itZsMod.strip();
0106 if (!restoredAPV[strip / 128]) {
0107 mergedRawDigis[strip] = adc;
0108 ++count;
0109 LogTrace("SiStripMergeZeroSuppression::produce")
0110 << "original count: " << count << " strip: " << strip << " adc: " << adc << "\n";
0111 }
0112 }
0113 }
0114
0115 LogTrace("SiStripMergeZeroSuppression::produce") << "size of digis to keep: " << count << "\n";
0116 LogTrace("SiStripMergeZeroSuppression::produce") << "inserting only the digis for the restored APVs"
0117 << "\n";
0118 LogTrace("SiStripMergeZeroSuppression::produce") << "size : " << suppressedDigis.size() << "\n";
0119 for (const auto itSuppDigi : suppressedDigis) {
0120 const uint16_t adc = itSuppDigi.adc();
0121 const uint16_t strip = itSuppDigi.strip();
0122 if (restoredAPV[strip / 128]) {
0123 mergedRawDigis[strip] = adc;
0124 LogTrace("SiStripMergeZeroSuppression::produce")
0125 << "new suppressed strip: " << strip << " adc: " << adc << "\n";
0126 }
0127 }
0128
0129 LogTrace("SiStripMergeZeroSuppression::produce") << "suppressing the raw digis"
0130 << "\n";
0131 zsModule->clear();
0132 for (uint16_t strip = 0; strip < mergedRawDigis.size(); ++strip) {
0133 uint16_t adc = mergedRawDigis[strip];
0134 if (adc)
0135 zsModule->push_back(SiStripDigi(strip, adc));
0136 }
0137 LogTrace("SiStripMergeZeroSuppression::produce")
0138 << "size zsModule after the merging: " << zsModule->size() << "\n";
0139 if ((count + suppressedDigis.size()) != zsModule->size())
0140 LogTrace("SiStripMergeZeroSuppression::produce")
0141 << "WE HAVE A PROBLEM!!!! THE NUMBER OF DIGIS IS NOT RIGHT=============="
0142 << "\n";
0143 LogTrace("SiStripMergeZeroSuppression::produce") << "exiting suppressing the raw digis"
0144 << "\n";
0145 }
0146 }
0147 }
0148
0149 uint32_t oldid = 0;
0150 for (const auto& dg : outputdigi) {
0151 uint32_t iddg = dg.id;
0152 if (iddg < oldid) {
0153 LogTrace("SiStripMergeZeroSuppression::produce") << "NOT IN THE RIGHT ORGER"
0154 << "\n";
0155 LogTrace("SiStripMergeZeroSuppression::produce") << "======================="
0156 << "\n";
0157 }
0158 oldid = iddg;
0159 }
0160
0161 LogTrace("SiStripMergeZeroSuppression::produce") << "write the output vector"
0162 << "\n";
0163 event.put(std::make_unique<edm::DetSetVector<SiStripDigi>>(outputdigi), "ZeroSuppressed");
0164 }
0165 }