SpyEventMatcherModule

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
// Module to import spy channel data into matching events

#include "DQM/SiStripMonitorHardware/interface/SiStripSpyEventMatcher.h"
#ifdef SiStripMonitorHardware_BuildEventMatchingCode

#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Framework/interface/stream/EDFilter.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/ESWatcher.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
#include "CondFormats/DataRecord/interface/SiStripFedCablingRcd.h"
#include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
#include "DataFormats/FEDRawData/interface/FEDRawData.h"
#include "EventFilter/SiStripRawToDigi/interface/SiStripFEDBuffer.h"
#include "DataFormats/Common/interface/DetSetVector.h"
#include "DataFormats/SiStripDigi/interface/SiStripRawDigi.h"
#include "DataFormats/Common/interface/Handle.h"
#include "DQM/SiStripMonitorHardware/interface/SiStripSpyUtilities.h"
#include "FWCore/Utilities/interface/Exception.h"
#include <memory>
#include <vector>

using edm::LogError;
using edm::LogInfo;

namespace sistrip {

  class SpyEventMatcherModule : public edm::stream::EDFilter<> {
  public:
    SpyEventMatcherModule(const edm::ParameterSet& config);
    ~SpyEventMatcherModule() override;
    void beginStream(edm::StreamID) override;
    bool filter(edm::Event& event, const edm::EventSetup& eventSetup) override;

  private:
    void findL1IDandAPVAddress(const edm::Event& event,
                               const SiStripFedCabling& cabling,
                               uint32_t& l1ID,
                               uint8_t& apvAddress) const;
    void copyData(const uint32_t eventId,
                  const uint8_t apvAddress,
                  const SpyEventMatcher::SpyEventList* matches,
                  edm::Event& event,
                  const SiStripFedCabling& cabling) const;

    static const char* const messageLabel_;
    const bool filterNonMatchingEvents_;
    const bool doMerge_;
    const edm::InputTag primaryStreamRawDataTag_;
    edm::EDGetTokenT<FEDRawDataCollection> primaryStreamRawDataToken_;
    std::unique_ptr<SpyEventMatcher> spyEventMatcher_;
    edm::ESGetToken<SiStripFedCabling, SiStripFedCablingRcd> fedCablingToken_;
    const SiStripFedCabling* fedCabling_;
    edm::ESWatcher<SiStripFedCablingRcd> cablingWatcher_;
    void updateFedCabling(const SiStripFedCablingRcd& rcd);
  };

}  // namespace sistrip

namespace sistrip {

  const char* const SpyEventMatcherModule::messageLabel_ = "SiStripSpyDataMergeModule";

  SpyEventMatcherModule::SpyEventMatcherModule(const edm::ParameterSet& config)
      : filterNonMatchingEvents_(config.getParameter<bool>("FilterNonMatchingEvents")),
        doMerge_(config.getParameter<bool>("MergeData")),
        primaryStreamRawDataTag_(config.getParameter<edm::InputTag>("PrimaryEventRawDataTag")),
        spyEventMatcher_(new SpyEventMatcher(config)),
        fedCablingToken_(esConsumes<>()),
        cablingWatcher_(this, &SpyEventMatcherModule::updateFedCabling) {
    primaryStreamRawDataToken_ = consumes<FEDRawDataCollection>(primaryStreamRawDataTag_);
    if (doMerge_) {
      produces<FEDRawDataCollection>("RawSpyData");
      produces<std::vector<uint32_t> >("SpyTotalEventCount");
      produces<std::vector<uint32_t> >("SpyL1ACount");
      produces<std::vector<uint32_t> >("SpyAPVAddress");
      produces<edm::DetSetVector<SiStripRawDigi> >("SpyScope");
      produces<edm::DetSetVector<SiStripRawDigi> >("SpyPayload");
      produces<edm::DetSetVector<SiStripRawDigi> >("SpyReordered");
      produces<edm::DetSetVector<SiStripRawDigi> >("SpyVirginRaw");
    }
  }

  SpyEventMatcherModule::~SpyEventMatcherModule() {}

  void SpyEventMatcherModule::updateFedCabling(const SiStripFedCablingRcd& rcd) {
    fedCabling_ = &rcd.get(fedCablingToken_);
  }

  void SpyEventMatcherModule::beginStream(edm::StreamID) { spyEventMatcher_->initialize(); }

  bool SpyEventMatcherModule::filter(edm::Event& event, const edm::EventSetup& eventSetup) {
    cablingWatcher_.check(eventSetup);
    uint8_t apvAddress = 0;
    uint32_t eventId = 0;
    try {
      findL1IDandAPVAddress(event, *fedCabling_, eventId, apvAddress);
    } catch (const cms::Exception& e) {
      LogError(messageLabel_) << e.what();
      return (filterNonMatchingEvents_ ? false : true);
    }
    const SpyEventMatcher::SpyEventList* matches = spyEventMatcher_->matchesForEvent(eventId, apvAddress);
    if (matches) {
      if (doMerge_) {
        copyData(eventId, apvAddress, matches, event, *fedCabling_);
      }
      return true;
    } else {
      return (filterNonMatchingEvents_ ? false : true);
    }
  }

  void SpyEventMatcherModule::findL1IDandAPVAddress(const edm::Event& event,
                                                    const SiStripFedCabling& cabling,
                                                    uint32_t& l1ID,
                                                    uint8_t& apvAddress) const {
    edm::Handle<FEDRawDataCollection> fedRawDataHandle;
    event.getByToken(primaryStreamRawDataToken_, fedRawDataHandle);
    const FEDRawDataCollection& fedRawData = *fedRawDataHandle;
    for (auto iFedId = cabling.fedIds().begin(); iFedId != cabling.fedIds().end(); ++iFedId) {
      const FEDRawData& data = fedRawData.FEDData(*iFedId);
      const auto st_buffer = preconstructCheckFEDBuffer(data);
      if (FEDBufferStatusCode::SUCCESS != st_buffer) {
        LogInfo(messageLabel_) << "Failed to build FED buffer for FED ID " << *iFedId
                               << ". Exception was: An exception of category 'FEDBuffer' occurred.\n"
                               << st_buffer << " (see debug output for details)";
        continue;
      }
      FEDBuffer buffer{data};
      const auto st_chan = buffer.findChannels();
      if (FEDBufferStatusCode::SUCCESS != st_chan) {
        LogDebug(messageLabel_) << "Failed to build FED buffer for FED ID " << *iFedId << ". Exception was " << st_chan
                                << " (see above for more details)";
        continue;
      }
      if (!buffer.doChecks(true)) {
        LogDebug(messageLabel_) << "Buffer check failed for FED ID " << *iFedId;
        continue;
      }
      l1ID = buffer.daqLvl1ID();
      apvAddress = buffer.trackerSpecialHeader().apveAddress();
      if (apvAddress != 0) {
        return;
      } else {
        if (buffer.trackerSpecialHeader().headerType() != HEADER_TYPE_FULL_DEBUG) {
          continue;
        }
        const FEDFullDebugHeader* header = dynamic_cast<const FEDFullDebugHeader*>(buffer.feHeader());
        auto connections = cabling.fedConnections(*iFedId);
        for (auto iConn = connections.begin(); iConn != connections.end(); ++iConn) {
          if (!iConn->isConnected()) {
            continue;
          }
          if (!buffer.channelGood(iConn->fedCh(), true)) {
            continue;
          } else {
            apvAddress = header->feUnitMajorityAddress(iConn->fedCh() / FEDCH_PER_FEUNIT);
            return;
          }
        }
      }
    }
    //if we haven't already found an acceptable alternative, throw an exception
    throw cms::Exception(messageLabel_) << "Failed to get L1ID/APV address from any FED";
  }

  void SpyEventMatcherModule::copyData(const uint32_t eventId,
                                       const uint8_t apvAddress,
                                       const SpyEventMatcher::SpyEventList* matches,
                                       edm::Event& event,
                                       const SiStripFedCabling& cabling) const {
    SpyEventMatcher::SpyDataCollections matchedCollections;
    spyEventMatcher_->getMatchedCollections(eventId, apvAddress, matches, cabling, matchedCollections);
    if (matchedCollections.rawData.get())
      event.put(std::move(matchedCollections.rawData), "RawSpyData");
    if (matchedCollections.totalEventCounters.get())
      event.put(std::move(matchedCollections.totalEventCounters), "SpyTotalEventCount");
    if (matchedCollections.l1aCounters.get())
      event.put(std::move(matchedCollections.l1aCounters), "SpyL1ACount");
    if (matchedCollections.apvAddresses.get())
      event.put(std::move(matchedCollections.apvAddresses), "SpyAPVAddress");
    if (matchedCollections.scopeDigis.get())
      event.put(std::move(matchedCollections.scopeDigis), "SpyScope");
    if (matchedCollections.payloadDigis.get())
      event.put(std::move(matchedCollections.payloadDigis), "SpyPayload");
    if (matchedCollections.reorderedDigis.get())
      event.put(std::move(matchedCollections.reorderedDigis), "SpyReordered");
    if (matchedCollections.virginRawDigis.get())
      event.put(std::move(matchedCollections.virginRawDigis), "SpyVirginRaw");
  }

}  // namespace sistrip

#include "FWCore/Framework/interface/MakerMacros.h"
#include <cstdint>
typedef sistrip::SpyEventMatcherModule SiStripSpyEventMatcherModule;
DEFINE_FWK_MODULE(SiStripSpyEventMatcherModule);

#endif  //SiStripMonitorHardware_BuildEventMatchingCode