CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
30  class SpyEventMatcherModule : public edm::EDFilter
31  {
32  public:
33  SpyEventMatcherModule(const edm::ParameterSet& config);
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_;
43  const bool filterNonMatchingEvents_;
44  const bool doMerge_;
45  const edm::InputTag primaryStreamRawDataTag_;
46  edm::EDGetTokenT<FEDRawDataCollection> primaryStreamRawDataToken_;
47  std::auto_ptr<SpyEventMatcher> spyEventMatcher_;
48  std::auto_ptr<SpyUtilities> utils_;
49  };
50 
51 }
52 
53 namespace sistrip {
54 
55  const char* SpyEventMatcherModule::messageLabel_ = "SiStripSpyDataMergeModule";
56 
57  SpyEventMatcherModule::SpyEventMatcherModule(const edm::ParameterSet& config)
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 
77  SpyEventMatcherModule::~SpyEventMatcherModule()
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.getByLabel(primaryStreamRawDataTag_,fedRawDataHandle);
112  event.getByToken(primaryStreamRawDataToken_,fedRawDataHandle);
113  const FEDRawDataCollection& fedRawData = *fedRawDataHandle;
114  for (auto iFedId = cabling.fedIds().begin(); iFedId != cabling.fedIds().end(); ++iFedId) {
115  const FEDRawData& data = fedRawData.FEDData(*iFedId);
116  if ( (!data.data()) || (!data.size()) ) {
117  LogDebug(messageLabel_) << "Failed to get FED data for FED ID " << *iFedId;
118  continue;
119  }
120  std::auto_ptr<FEDBuffer> buffer;
121  try {
122  buffer.reset(new FEDBuffer(data.data(),data.size()));
123  } catch (const cms::Exception& e) {
124  LogDebug(messageLabel_) << "Failed to build FED buffer for FED ID " << *iFedId << ". Exception was " << e.what();
125  continue;
126  }
127  if (!buffer->doChecks()) {
128  LogDebug(messageLabel_) << "Buffer check failed for FED ID " << *iFedId;
129  continue;
130  }
131  l1ID = buffer->daqLvl1ID();
132  apvAddress = buffer->trackerSpecialHeader().apveAddress();
133  if (apvAddress != 0) {
134  return;
135  } else {
136  if (buffer->trackerSpecialHeader().headerType() != HEADER_TYPE_FULL_DEBUG) {
137  continue;
138  }
139  const FEDFullDebugHeader* header = dynamic_cast<const FEDFullDebugHeader*>(buffer->feHeader());
140  auto connections = cabling.fedConnections(*iFedId);
141  for (auto iConn = connections.begin(); iConn != connections.end(); ++iConn) {
142  if (!iConn->isConnected()) {
143  continue;
144  }
145  if ( !buffer->channelGood(iConn->fedCh()) ) {
146  continue;
147  } else {
148  apvAddress = header->feUnitMajorityAddress(iConn->fedCh()/FEDCH_PER_FEUNIT);
149  return;
150  }
151  }
152  }
153  }
154  //if we haven't already found an acceptable alternative, throw an exception
155  throw cms::Exception(messageLabel_) << "Failed to get L1ID/APV address from any FED";
156  }
157 
158  void SpyEventMatcherModule::copyData(const uint32_t eventId, const uint8_t apvAddress, const SpyEventMatcher::SpyEventList* matches, edm::Event& event,
159  const SiStripFedCabling& cabling) const
160  {
161  SpyEventMatcher::SpyDataCollections matchedCollections;
162  spyEventMatcher_->getMatchedCollections(eventId,apvAddress,matches,cabling,matchedCollections);
163  if (matchedCollections.rawData.get()) event.put(matchedCollections.rawData,"RawSpyData");
164  if (matchedCollections.totalEventCounters.get()) event.put(matchedCollections.totalEventCounters,"SpyTotalEventCount");
165  if (matchedCollections.l1aCounters.get()) event.put(matchedCollections.l1aCounters,"SpyL1ACount");
166  if (matchedCollections.apvAddresses.get()) event.put(matchedCollections.apvAddresses,"SpyAPVAddress");
167  if (matchedCollections.scopeDigis.get()) event.put(matchedCollections.scopeDigis,"SpyScope");
168  if (matchedCollections.payloadDigis.get()) event.put(matchedCollections.payloadDigis,"SpyPayload");
169  if (matchedCollections.reorderedDigis.get()) event.put(matchedCollections.reorderedDigis,"SpyReordered");
170  if (matchedCollections.virginRawDigis.get()) event.put(matchedCollections.virginRawDigis,"SpyVirginRaw");
171  }
172 
173 }
174 
176 typedef sistrip::SpyEventMatcherModule SiStripSpyEventMatcherModule;
177 DEFINE_FWK_MODULE(SiStripSpyEventMatcherModule);
178 
179 #endif //SiStripMonitorHardware_BuildEventMatchingCode
#define LogDebug(id)
virtual char const * what() const
Definition: Exception.cc:141
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
void beginJob()
Definition: Breakpoints.cc:15
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
FedsConstIterRange fedIds() const
std::set< EventID > SpyEventList
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
static const uint16_t FEDCH_PER_FEUNIT
ConnsConstIterRange fedConnections(uint16_t fed_id) const
Contains cabling info at the device level, including DetId, APV pair numbers, hardware addresses...
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28