CMS 3D CMS Logo

SiStripSpyEventMatcherModule.cc
Go to the documentation of this file.
1 // Module to import spy channel data into matching events
2 
4 #ifdef SiStripMonitorHardware_BuildEventMatchingCode
5 
7 #include "boost/cstdint.hpp"
22 #include <memory>
23 #include <vector>
24 
25 using edm::LogError;
26 using edm::LogInfo;
27 
28 namespace sistrip {
29 
31  {
32  public:
34  virtual ~SpyEventMatcherModule();
35  virtual void beginJob() override;
36  virtual bool filter(edm::Event& event, const edm::EventSetup& eventSetup) override;
37  private:
38  void findL1IDandAPVAddress(const edm::Event& event, const SiStripFedCabling& cabling, uint32_t& l1ID, uint8_t& apvAddress) const;
39  void copyData(const uint32_t eventId, const uint8_t apvAddress, const SpyEventMatcher::SpyEventList* matches, edm::Event& event,
40  const SiStripFedCabling& cabling) const;
41 
42  static const char* messageLabel_;
44  const bool doMerge_;
47  std::unique_ptr<SpyEventMatcher> spyEventMatcher_;
48  std::unique_ptr<SpyUtilities> utils_;
49  };
50 
51 }
52 
53 namespace sistrip {
54 
55  const char* SpyEventMatcherModule::messageLabel_ = "SiStripSpyDataMergeModule";
56 
58  : filterNonMatchingEvents_( config.getParameter<bool>("FilterNonMatchingEvents") ),
59  doMerge_( config.getParameter<bool>("MergeData") ),
60  primaryStreamRawDataTag_( config.getParameter<edm::InputTag>("PrimaryEventRawDataTag") ),
61  spyEventMatcher_(new SpyEventMatcher(config)),
62  utils_(new SpyUtilities)
63  {
64  primaryStreamRawDataToken_ = consumes<FEDRawDataCollection>(primaryStreamRawDataTag_);
65  if (doMerge_) {
66  produces<FEDRawDataCollection>("RawSpyData");
67  produces< std::vector<uint32_t> >("SpyTotalEventCount");
68  produces< std::vector<uint32_t> >("SpyL1ACount");
69  produces< std::vector<uint32_t> >("SpyAPVAddress");
70  produces< edm::DetSetVector<SiStripRawDigi> >("SpyScope");
71  produces< edm::DetSetVector<SiStripRawDigi> >("SpyPayload");
72  produces< edm::DetSetVector<SiStripRawDigi> >("SpyReordered");
73  produces< edm::DetSetVector<SiStripRawDigi> >("SpyVirginRaw");
74  }
75  }
76 
78  {
79  }
80 
82  {
83  spyEventMatcher_->initialize();
84  }
85 
87  {
88  const SiStripFedCabling& cabling = *(utils_->getCabling(eventSetup));
89  uint8_t apvAddress = 0;
90  uint32_t eventId = 0;
91  try {
92  findL1IDandAPVAddress(event,cabling,eventId,apvAddress);
93  } catch (const cms::Exception& e) {
94  LogError(messageLabel_) << e.what();
95  return ( filterNonMatchingEvents_ ? false : true );
96  }
97  const SpyEventMatcher::SpyEventList* matches = spyEventMatcher_->matchesForEvent(eventId,apvAddress);
98  if (matches) {
99  if (doMerge_) {
100  copyData(eventId,apvAddress,matches,event,cabling);
101  }
102  return true;
103  } else {
104  return ( filterNonMatchingEvents_ ? false : true );
105  }
106  }
107 
108  void SpyEventMatcherModule::findL1IDandAPVAddress(const edm::Event& event, const SiStripFedCabling& cabling, uint32_t& l1ID, uint8_t& apvAddress) const
109  {
110  edm::Handle<FEDRawDataCollection> fedRawDataHandle;
111  event.getByToken(primaryStreamRawDataToken_,fedRawDataHandle);
112  const FEDRawDataCollection& fedRawData = *fedRawDataHandle;
113  for (auto iFedId = cabling.fedIds().begin(); iFedId != cabling.fedIds().end(); ++iFedId) {
114  const FEDRawData& data = fedRawData.FEDData(*iFedId);
115  if ( (!data.data()) || (!data.size()) ) {
116  LogDebug(messageLabel_) << "Failed to get FED data for FED ID " << *iFedId;
117  continue;
118  }
119  std::unique_ptr<FEDBuffer> buffer;
120  try {
121  buffer.reset(new FEDBuffer(data.data(),data.size()));
122  } catch (const cms::Exception& e) {
123  LogDebug(messageLabel_) << "Failed to build FED buffer for FED ID " << *iFedId << ". Exception was " << e.what();
124  continue;
125  }
126  if (!buffer->doChecks()) {
127  LogDebug(messageLabel_) << "Buffer check failed for FED ID " << *iFedId;
128  continue;
129  }
130  l1ID = buffer->daqLvl1ID();
131  apvAddress = buffer->trackerSpecialHeader().apveAddress();
132  if (apvAddress != 0) {
133  return;
134  } else {
135  if (buffer->trackerSpecialHeader().headerType() != HEADER_TYPE_FULL_DEBUG) {
136  continue;
137  }
138  const FEDFullDebugHeader* header = dynamic_cast<const FEDFullDebugHeader*>(buffer->feHeader());
139  auto connections = cabling.fedConnections(*iFedId);
140  for (auto iConn = connections.begin(); iConn != connections.end(); ++iConn) {
141  if (!iConn->isConnected()) {
142  continue;
143  }
144  if ( !buffer->channelGood(iConn->fedCh()) ) {
145  continue;
146  } else {
147  apvAddress = header->feUnitMajorityAddress(iConn->fedCh()/FEDCH_PER_FEUNIT);
148  return;
149  }
150  }
151  }
152  }
153  //if we haven't already found an acceptable alternative, throw an exception
154  throw cms::Exception(messageLabel_) << "Failed to get L1ID/APV address from any FED";
155  }
156 
157  void SpyEventMatcherModule::copyData(const uint32_t eventId, const uint8_t apvAddress, const SpyEventMatcher::SpyEventList* matches, edm::Event& event,
158  const SiStripFedCabling& cabling) const
159  {
160  SpyEventMatcher::SpyDataCollections matchedCollections;
161  spyEventMatcher_->getMatchedCollections(eventId,apvAddress,matches,cabling,matchedCollections);
162  if (matchedCollections.rawData.get()) event.put(std::move(matchedCollections.rawData),"RawSpyData");
163  if (matchedCollections.totalEventCounters.get()) event.put(std::move(matchedCollections.totalEventCounters),"SpyTotalEventCount");
164  if (matchedCollections.l1aCounters.get()) event.put(std::move(matchedCollections.l1aCounters),"SpyL1ACount");
165  if (matchedCollections.apvAddresses.get()) event.put(std::move(matchedCollections.apvAddresses),"SpyAPVAddress");
166  if (matchedCollections.scopeDigis.get()) event.put(std::move(matchedCollections.scopeDigis),"SpyScope");
167  if (matchedCollections.payloadDigis.get()) event.put(std::move(matchedCollections.payloadDigis),"SpyPayload");
168  if (matchedCollections.reorderedDigis.get()) event.put(std::move(matchedCollections.reorderedDigis),"SpyReordered");
169  if (matchedCollections.virginRawDigis.get()) event.put(std::move(matchedCollections.virginRawDigis),"SpyVirginRaw");
170  }
171 
172 }
173 
177 
178 #endif //SiStripMonitorHardware_BuildEventMatchingCode
#define LogDebug(id)
virtual char const * what() const
Definition: Exception.cc:141
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
void copyData(const uint32_t eventId, const uint8_t apvAddress, const SpyEventMatcher::SpyEventList *matches, edm::Event &event, const SiStripFedCabling &cabling) const
SpyEventMatcherModule(const edm::ParameterSet &config)
std::unique_ptr< std::vector< uint32_t > > l1aCounters
std::unique_ptr< SpyUtilities > utils_
std::unique_ptr< std::vector< uint32_t > > totalEventCounters
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
Definition: config.py:1
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
sistrip classes
std::unique_ptr< edm::DetSetVector< SiStripRawDigi > > virginRawDigis
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
edm::EDGetTokenT< FEDRawDataCollection > primaryStreamRawDataToken_
virtual bool filter(edm::Event &event, const edm::EventSetup &eventSetup) override
FedsConstIterRange fedIds() const
std::set< EventID > SpyEventList
std::unique_ptr< SpyEventMatcher > spyEventMatcher_
std::unique_ptr< FEDRawDataCollection > rawData
static const uint16_t FEDCH_PER_FEUNIT
std::unique_ptr< edm::DetSetVector< SiStripRawDigi > > scopeDigis
ConnsConstIterRange fedConnections(uint16_t fed_id) const
Contains cabling info at the device level, including DetId, APV pair numbers, hardware addresses...
uint8_t feUnitMajorityAddress(const uint8_t internalFEUnitNum) const
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::unique_ptr< std::vector< uint32_t > > apvAddresses
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
std::unique_ptr< edm::DetSetVector< SiStripRawDigi > > reorderedDigis
std::unique_ptr< edm::DetSetVector< SiStripRawDigi > > payloadDigis
def move(src, dest)
Definition: eostools.py:510
Definition: event.py:1
sistrip::SpyEventMatcherModule SiStripSpyEventMatcherModule
void findL1IDandAPVAddress(const edm::Event &event, const SiStripFedCabling &cabling, uint32_t &l1ID, uint8_t &apvAddress) const