2 #ifdef SiStripMonitorHardware_BuildEventMatchingCode
23 #include "boost/bind.hpp"
24 #include "boost/shared_ptr.hpp"
37 : eventId_(eventId), apvAddress_(apvAddress) {}
40 outputRawData_(outputRawData),
41 outputTotalEventCounters_(sistrip::
FED_ID_MAX+1),
45 SpyEventMatcher::~SpyEventMatcher() {}
48 : rawDataTag_(config.getParameter<edm::InputTag>(
"RawSpyDataTag")),
49 totalEventCountersTag_(config.getParameter<edm::InputTag>(
"SpyTotalEventCountersTag")),
50 l1aCountersTag_(config.getParameter<edm::InputTag>(
"SpyL1ACountersTag")),
51 apvAddressesTag_(config.getParameter<edm::InputTag>(
"SpyAPVAddressesTag")),
52 scopeDigisTag_(config.getParameter<edm::InputTag>(
"SpyScopeDigisTag")),
53 payloadDigisTag_(config.getParameter<edm::InputTag>(
"SpyPayloadDigisTag")),
54 reorderedDigisTag_(config.getParameter<edm::InputTag>(
"SpyReorderedDigisTag")),
55 virginRawDigisTag_(config.getParameter<edm::InputTag>(
"SpyVirginRawDigisTag")),
56 counterDiffMax_(config.getParameter<uint32_t>(
"CounterDiffMaxAllowed")),
58 source_(constructSource(config.getParameter<edm::
ParameterSet>(
"SpySource"))),
65 processConfiguration_->setParameterSetID(emptyPSet.
id());
66 source_->productRegistry()->setFrozen();
69 source_->branchIDListHelper(),
70 *processConfiguration_,
74 std::unique_ptr<SpyEventMatcher::Source> SpyEventMatcher::constructSource(
const edm::ParameterSet& sourceConfig)
85 void SpyEventMatcher::initialize()
90 std::ostringstream ss;
91 ss <<
"Events with possible matches (eventID,apvAddress): ";
92 for (std::map<EventKey,SpyEventList>::const_iterator iSpyEvent = eventMatches_.begin(); iSpyEvent != eventMatches_.end(); ++iSpyEvent) {
93 ss <<
"(" << iSpyEvent->first.eventId() <<
"," << uint16_t(iSpyEvent->first.apvAddress()) <<
") ";
102 CountersPtr totalEventCounters = getCounters(nextSpyEvent,totalEventCountersTag_);
103 CountersPtr l1aCounters = getCounters(nextSpyEvent,l1aCountersTag_);
104 CountersPtr apvAddresses = getCounters(nextSpyEvent,apvAddressesTag_,
false);
107 std::vector<uint32_t>::const_iterator iTotalEventCount = totalEventCounters->begin();
108 std::vector<uint32_t>::const_iterator iL1ACount = l1aCounters->begin();
109 std::vector<uint32_t>::const_iterator iAPVAddress = apvAddresses->begin();
111 std::map<EventKey,uint16_t> fedCounts;
112 unsigned int fedid = 0;
114 ( (iTotalEventCount != totalEventCounters->end()) && (iL1ACount != l1aCounters->end()) && (iAPVAddress != apvAddresses->end()) );
115 (++iTotalEventCount, ++iL1ACount, ++iAPVAddress, ++fedid)
117 if (*iAPVAddress == 0) {
121 if ( ((*iTotalEventCount) > (*iL1ACount) ) ||
122 ((*iL1ACount)-(*iTotalEventCount) > counterDiffMax_)
124 LogWarning(mlLabel_) <<
"Spy event " << spyEventId.
event()
125 <<
" error in counter values for FED " << fedid
126 <<
", totCount = " << *iTotalEventCount
127 <<
", L1Acount = " << *iL1ACount
133 for (uint32_t eventId = (*iTotalEventCount)+1; eventId <= (*iL1ACount)+1; ++eventId) {
134 EventKey
key(eventId,*iAPVAddress);
135 eventMatches_[
key].insert(spyEventId);
141 std::ostringstream ss;
142 ss <<
"Spy event " << spyEventId.
event() <<
" matches (eventID,apvAddress,nFEDs): ";
143 for (std::map<EventKey,uint16_t>::const_iterator iEventFEDCount = fedCounts.begin(); iEventFEDCount != fedCounts.end(); ++iEventFEDCount) {
144 ss <<
"(" << iEventFEDCount->first.eventId() <<
"," << uint16_t(iEventFEDCount->first.apvAddress()) <<
"," << iEventFEDCount->second <<
") ";
149 const SpyEventMatcher::SpyEventList* SpyEventMatcher::matchesForEvent(
const uint32_t eventId,
const uint8_t apvAddress)
const
151 EventKey eventKey(eventId,apvAddress);
152 std::map<EventKey,SpyEventList>::const_iterator iMatch = eventMatches_.find(eventKey);
153 if (iMatch == eventMatches_.end()) {
154 LogDebug(mlLabel_) <<
"No match found for event " << eventId <<
" with APV address " << uint16_t(apvAddress);
158 std::ostringstream ss;
159 ss <<
"Found matches to event " << eventId <<
" with address " << uint16_t(apvAddress) <<
" in spy events ";
160 for (SpyEventList::const_iterator iMatchingSpyEvent = iMatch->second.begin(); iMatchingSpyEvent != iMatch->second.end(); ++iMatchingSpyEvent) {
161 ss << iMatchingSpyEvent->event() <<
" ";
163 LogInfo(mlLabel_) << ss.str();
164 return &(iMatch->second);
170 MatchingOutput& mo) {
174 if (!inputRawDataPtr) {
175 throw cms::Exception(mlLabel_) <<
"Failed to get raw spy data with tag " << rawDataTag_ <<
" from spy event";
178 CountersPtr inputTotalEventCounters = getCounters(event,totalEventCountersTag_);
179 CountersPtr inputL1ACounters = getCounters(event,l1aCountersTag_);
180 CountersPtr inputAPVAddresses = getCounters(event,apvAddressesTag_,
false);
186 if (inputScopeDigis && !mo.outputScopeDigisVector_.get() ) mo.outputScopeDigisVector_.reset(
new std::vector<
edm::DetSet<SiStripRawDigi> >);
187 if (inputPayloadDigis && !mo.outputPayloadDigisVector_.get() ) mo.outputPayloadDigisVector_.reset(
new std::vector<
edm::DetSet<SiStripRawDigi> >);
188 if (inputReorderedDigis && !mo.outputReorderedDigisVector_.get() ) mo.outputReorderedDigisVector_.reset(
new std::vector<
edm::DetSet<SiStripRawDigi> >);
189 if (inputVirginRawDigis && !mo.outputVirginRawDigisVector_.get() ) mo.outputVirginRawDigisVector_.reset(
new std::vector<
edm::DetSet<SiStripRawDigi> >);
191 std::set<uint16_t> matchingFeds;
192 findMatchingFeds(eventId,apvAddress,inputTotalEventCounters,inputL1ACounters,inputAPVAddresses,matchingFeds);
193 LogInfo(mlLabel_) <<
"Spy event " <<
event.id() <<
" has " << matchingFeds.size() <<
" matching FEDs";
194 std::ostringstream ss;
195 ss <<
"Matching FEDs for event " <<
event.id() <<
": ";
196 for (std::set<uint16_t>::const_iterator iFedId = matchingFeds.begin(); iFedId != matchingFeds.end(); ++iFedId) {
197 ss << *iFedId <<
" ";
201 std::vector<uint16_t> duplicateFeds(
std::min(mo.alreadyMergedFeds_.size(),matchingFeds.size()) );
202 std::vector<uint16_t>::iterator duplicatesBegin = duplicateFeds.begin();
203 std::vector<uint16_t>::iterator duplicatesEnd = std::set_intersection(mo.alreadyMergedFeds_.begin(),mo.alreadyMergedFeds_.end(),
204 matchingFeds.begin(),matchingFeds.end(),
206 if ( (duplicatesEnd-duplicatesBegin) != 0 ) {
207 std::ostringstream ss;
208 ss <<
"Found a match for FEDs ";
209 for (std::vector<uint16_t>::const_iterator iDup = duplicatesBegin; iDup != duplicatesEnd; ++iDup) {
212 ss <<
". Output SetSetVectors will be unusable!";
213 LogError(mlLabel_) << ss.str();
216 mergeMatchingData(matchingFeds,inputRawData,inputTotalEventCounters,inputL1ACounters,inputAPVAddresses,
217 inputScopeDigis,inputPayloadDigis,inputReorderedDigis,inputVirginRawDigis,
218 mo.outputRawData_,mo.outputTotalEventCounters_,mo.outputL1ACounters_,mo.outputAPVAddresses_,
219 mo.outputScopeDigisVector_.get(),mo.outputPayloadDigisVector_.get(),
220 mo.outputReorderedDigisVector_.get(),mo.outputVirginRawDigisVector_.get(),
222 mo.alreadyMergedFeds_.insert(matchingFeds.begin(),matchingFeds.end());
225 void SpyEventMatcher::getMatchedCollections(
const uint32_t eventId,
const uint8_t apvAddress,
227 SpyDataCollections& collectionsToCreate)
229 if (!matchingEvents)
return;
231 MatchingOutput mo(outputRawData);
232 source_->loopSpecified(*eventPrincipal_,*matchingEvents,boost::bind(&SpyEventMatcher::getCollections,
this,_1,
233 eventId,apvAddress,boost::cref(cabling),boost::ref(mo)));
234 SpyDataCollections collections(mo.outputRawData_,mo.outputTotalEventCounters_,mo.outputL1ACounters_,mo.outputAPVAddresses_,
235 mo.outputScopeDigisVector_.get(),mo.outputPayloadDigisVector_.get(),
236 mo.outputReorderedDigisVector_.get(),mo.outputVirginRawDigisVector_.get());
237 collectionsToCreate = collections;
240 void SpyEventMatcher::findMatchingFeds(
const uint32_t eventId,
const uint8_t apvAddress,
241 SpyEventMatcher::CountersPtr totalEventCounters,
242 SpyEventMatcher::CountersPtr l1aCounters,
243 SpyEventMatcher::CountersPtr apvAddresses,
244 std::set<uint16_t>& matchingFeds)
247 std::vector<uint32_t>::const_iterator iTotalEventCount = totalEventCounters->begin();
248 std::vector<uint32_t>::const_iterator iL1ACount = l1aCounters->begin();
249 std::vector<uint32_t>::const_iterator iAPVAddress = apvAddresses->begin();
251 ( (iTotalEventCount != totalEventCounters->end()) && (iL1ACount != l1aCounters->end()) && (iAPVAddress != apvAddresses->end()) );
252 (++iTotalEventCount, ++iL1ACount, ++iAPVAddress)
254 if (*iAPVAddress == 0) {
257 if ( (eventId > *iTotalEventCount) && (eventId <= (*iL1ACount)+1) && (*iAPVAddress == apvAddress) ) {
258 matchingFeds.insert(matchingFeds.end(),iTotalEventCount-totalEventCounters->begin());
263 void SpyEventMatcher::mergeMatchingData(
const std::set<uint16_t>& matchingFeds,
265 SpyEventMatcher::CountersPtr inputTotalEventCounters,
266 SpyEventMatcher::CountersPtr inputL1ACounters,
267 SpyEventMatcher::CountersPtr inputAPVAddresses,
273 std::vector<uint32_t>& outputTotalEventCounters,
274 std::vector<uint32_t>& outputL1ACounters,
275 std::vector<uint32_t>& outputAPVAddresses,
283 if (inputScopeDigis) {
284 outputScopeDigisVector->reserve(outputScopeDigisVector->size()+matchingFeds.size()*
FEDCH_PER_FED);
286 if (inputPayloadDigis) {
287 outputPayloadDigisVector->reserve(outputPayloadDigisVector->size()+matchingFeds.size()*
FEDCH_PER_FED);
289 if (inputReorderedDigis) {
290 outputReorderedDigisVector->reserve(outputReorderedDigisVector->size()+matchingFeds.size()*
FEDCH_PER_FED);
292 if (inputVirginRawDigis) {
293 outputVirginRawDigisVector->reserve(outputVirginRawDigisVector->size()+matchingFeds.size()*
FEDCH_PER_FED/2);
296 std::set<uint32_t> usedDetIds;
297 for (std::set<uint16_t>::const_iterator iFedId = matchingFeds.begin(); iFedId != matchingFeds.end(); ++iFedId) {
298 const uint32_t fedId = *iFedId;
299 LogDebug(mlLabel_) <<
"Copying data for FED " << fedId;
303 outputTotalEventCounters[fedId] = (*inputTotalEventCounters)[fedId];
304 outputL1ACounters[fedId] = (*inputL1ACounters)[fedId];
305 outputAPVAddresses[fedId] = (*inputAPVAddresses)[fedId];
308 if (inputScopeDigis) {
310 if (iScopeDigis != inputScopeDigis->
end()) {
311 outputScopeDigisVector->push_back(*iScopeDigis);
314 if (inputPayloadDigis) {
316 if (iPayloadDigis != inputPayloadDigis->
end()) {
317 outputPayloadDigisVector->push_back(*iPayloadDigis);
320 if (inputReorderedDigis) {
322 if (iReorderedDigis != inputReorderedDigis->
end()) {
323 outputReorderedDigisVector->push_back(*iReorderedDigis);
327 if (inputVirginRawDigis) {
328 std::set<uint32_t> fedDetIds;
329 const std::vector<FedChannelConnection>& conns = cabling.
connections(fedId);
330 for (std::vector<FedChannelConnection>::const_iterator iConn = conns.begin(); iConn != conns.end(); ++iConn) {
331 if (!iConn->isConnected())
continue;
332 const uint32_t detId = iConn->detId();
333 if (usedDetIds.find(detId) != usedDetIds.end()) {
334 LogError(mlLabel_) <<
"Duplicate DetID found " << detId <<
" skipping data for this Det from FED " << fedId;
337 fedDetIds.insert(iConn->detId());
339 usedDetIds.insert(fedDetIds.begin(),fedDetIds.end());
340 for (std::set<uint32_t>::const_iterator iDetId = fedDetIds.begin(); iDetId != fedDetIds.end(); ++iDetId) {
342 if (iVirginRawDigis != inputVirginRawDigis->
end()) {
343 outputVirginRawDigisVector->push_back(*iVirginRawDigis);
352 const std::vector<uint32_t>* vectorFromEvent = getProduct< std::vector<uint32_t> >(
event,
tag);
353 if (vectorFromEvent) {
355 return CountersPtr(
new CountersWrapper(vectorFromEvent) );
357 const std::map<uint32_t,uint32_t>* mapFromEvent = getProduct< std::map<uint32_t,uint32_t> >(
event,
tag);
359 std::vector<uint32_t>* newVector =
new std::vector<uint32_t>(
FED_ID_MAX+1,0);
360 if (mapKeyIsByFedID) {
361 for (std::map<uint32_t,uint32_t>::const_iterator iIdValue = mapFromEvent->begin(); iIdValue != mapFromEvent->end(); ++iIdValue) {
362 newVector->at(iIdValue->first) = iIdValue->second;
365 SpyUtilities::fillFEDMajorities(*mapFromEvent,*newVector);
374 CountersPtr newCountersPtr(
new CountersWrapper(newVector,
true) );
375 return newCountersPtr;
377 throw cms::Exception(mlLabel_) <<
"Unable to get product " << tag <<
" from spy event";
383 std::vector<uint32_t>& theTotalEventCounters,
384 std::vector<uint32_t>& theL1ACounters,
385 std::vector<uint32_t>& theAPVAddresses,
391 totalEventCounters(new std::vector<uint32_t>),
392 l1aCounters(new std::vector<uint32_t>),
393 apvAddresses(new std::vector<uint32_t>),
394 scopeDigis(theScopeDigisVector ? new edm::DetSetVector<
SiStripRawDigi>(*theScopeDigisVector) :
NULL),
395 payloadDigis(thePayloadDigisVector ? new edm::DetSetVector<
SiStripRawDigi>(*thePayloadDigisVector) :
NULL),
396 reorderedDigis(theReorderedDigisVector ? new edm::DetSetVector<
SiStripRawDigi>(*theReorderedDigisVector) :
NULL),
397 virginRawDigis(theVirginRawDigisVector ? new edm::DetSetVector<
SiStripRawDigi>(*theVirginRawDigisVector) :
NULL)
399 rawData->swap(theRawData);
400 totalEventCounters->swap(theTotalEventCounters);
401 l1aCounters->swap(theL1ACounters);
402 apvAddresses->swap(theAPVAddresses);
405 SpyEventMatcher::SpyDataCollections::SpyDataCollections()
407 totalEventCounters(),
416 SpyEventMatcher::SpyDataCollections& SpyEventMatcher::SpyDataCollections::operator = (SpyDataCollections
original)
418 rawData = original.rawData;
419 totalEventCounters = original.totalEventCounters;
420 l1aCounters = original.l1aCounters;
421 apvAddresses = original.apvAddresses;
422 scopeDigis = original.scopeDigis;
423 payloadDigis = original.payloadDigis;
424 virginRawDigis = original.virginRawDigis;
428 SpyEventMatcher::CountersWrapper::CountersWrapper(
const Counters* theCounters)
429 : pConst(theCounters),
435 SpyEventMatcher::CountersWrapper::CountersWrapper(
Counters* theCounters,
const bool takeOwnership)
436 : pConst(theCounters),
438 deleteP(takeOwnership)
443 SpyEventMatcher::CountersWrapper::~CountersWrapper()
445 if (deleteP)
delete p;
450 #endif //SiStripMonitorHardware_BuildEventMatchingCode
EventNumber_t event() const
iterator find(det_id_type id)
ParameterSetID id() const
EventID const & id() const
static uint32_t fedIndex(const uint16_t &fed_id, const uint16_t &fed_ch)
size_t size() const
Lenght of the data buffer in bytes.
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
const T & max(const T &a, const T &b)
static const char * mlLabel_
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
iterator end()
Return the off-the-end iterator.
std::string getReleaseVersion()
EventKey(const uint32_t eventId, const uint8_t apvAddress)
Contains cabling info at the device level, including DetId, APV pair numbers, hardware addresses...
static const uint16_t FEDCH_PER_FED
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
static const uint16_t FED_ID_MAX
collection_type::const_iterator const_iterator
A Digi for the silicon strip detector, containing only adc information, and suitable for storing raw ...
ParameterSet const & registerIt()
const std::vector< FedChannelConnection > & connections(uint16_t fed_id) const