Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:08:48

0001 #define SiStripMonitorHardware_BuildEventMatchingCode
0002 #ifdef SiStripMonitorHardware_BuildEventMatchingCode
0003 
0004 #ifndef DQM_SiStripMonitorHardware_SiStripSpyEventMatcher_H
0005 #define DQM_SiStripMonitorHardware_SiStripSpyEventMatcher_H
0006 
0007 #include "FWCore/Utilities/interface/InputTag.h"
0008 #include "FWCore/Sources/interface/VectorInputSource.h"
0009 #include "FWCore/Framework/interface/EventPrincipal.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 
0012 #include <set>
0013 #include <map>
0014 #include <memory>
0015 #include <vector>
0016 #include <cstdint>
0017 
0018 //forward declarations
0019 class FEDRawDataCollection;
0020 class SiStripRawDigi;
0021 namespace edm {
0022   template <class T>
0023   class DetSetVector;
0024   template <class T>
0025   struct DetSet;
0026   class EventID;
0027   class ParameterSet;
0028 }  // namespace edm
0029 class SiStripFedCabling;
0030 
0031 namespace sistrip {
0032 
0033   class SpyEventMatcher {
0034   public:
0035     class SpyDataCollections {
0036     public:
0037       std::unique_ptr<FEDRawDataCollection> rawData;
0038       std::unique_ptr<std::vector<uint32_t> > totalEventCounters;
0039       std::unique_ptr<std::vector<uint32_t> > l1aCounters;
0040       std::unique_ptr<std::vector<uint32_t> > apvAddresses;
0041       std::unique_ptr<edm::DetSetVector<SiStripRawDigi> > scopeDigis;
0042       std::unique_ptr<edm::DetSetVector<SiStripRawDigi> > payloadDigis;
0043       std::unique_ptr<edm::DetSetVector<SiStripRawDigi> > reorderedDigis;
0044       std::unique_ptr<edm::DetSetVector<SiStripRawDigi> > virginRawDigis;
0045       SpyDataCollections();
0046       //NB. This will remove all elements in the containers pasted in. It does not copy the data.
0047       SpyDataCollections(FEDRawDataCollection& theRawData,
0048                          std::vector<uint32_t>& theTotalEventCounters,
0049                          std::vector<uint32_t>& theL1ACounters,
0050                          std::vector<uint32_t>& theAPVAddresses,
0051                          std::vector<edm::DetSet<SiStripRawDigi> >* theScopeDigisVector,
0052                          std::vector<edm::DetSet<SiStripRawDigi> >* thePayloadDigisVector,
0053                          std::vector<edm::DetSet<SiStripRawDigi> >* theReorderedDigisVector,
0054                          std::vector<edm::DetSet<SiStripRawDigi> >* theVirginRawDigisVector);
0055     };
0056     struct MatchingOutput {
0057       explicit MatchingOutput(FEDRawDataCollection& outputRawData);
0058       FEDRawDataCollection& outputRawData_;
0059       std::vector<uint32_t> outputTotalEventCounters_;
0060       std::vector<uint32_t> outputL1ACounters_;
0061       std::vector<uint32_t> outputAPVAddresses_;
0062       std::shared_ptr<std::vector<edm::DetSet<SiStripRawDigi> > > outputScopeDigisVector_;
0063       std::shared_ptr<std::vector<edm::DetSet<SiStripRawDigi> > > outputPayloadDigisVector_;
0064       std::shared_ptr<std::vector<edm::DetSet<SiStripRawDigi> > > outputReorderedDigisVector_;
0065       std::shared_ptr<std::vector<edm::DetSet<SiStripRawDigi> > > outputVirginRawDigisVector_;
0066       std::set<uint16_t> alreadyMergedFeds_;
0067     };
0068 
0069     typedef edm::EventID EventID;
0070     typedef std::set<EventID> SpyEventList;
0071 
0072     SpyEventMatcher(const edm::ParameterSet& config);
0073     virtual ~SpyEventMatcher();
0074     //set up the internal map of eventID, to apvAddress
0075     void initialize();
0076     //check if there is any data for an event. Returns NULL if not or a pointer to a list of matches if they exist
0077     const SpyEventList* matchesForEvent(const uint32_t eventId, const uint8_t apvAddress) const;
0078     //get data for matching FEDs (non-const because reading events from the source modifies it)
0079     void getMatchedCollections(const uint32_t eventId,
0080                                const uint8_t apvAddress,
0081                                const SpyEventList* matchingEvents,
0082                                const SiStripFedCabling& cabling,
0083                                SpyDataCollections& collectionsToCreate);
0084     //helper for getMatchedCollections()
0085     void getCollections(const edm::EventPrincipal& event,
0086                         const uint32_t eventId,
0087                         const uint8_t apvAddress,
0088                         const SiStripFedCabling& cabling,
0089                         MatchingOutput& matchingOutput);
0090 
0091   private:
0092     class EventKey {
0093     public:
0094       EventKey(const uint32_t eventId, const uint8_t apvAddress);
0095       uint32_t eventId() const { return eventId_; }
0096       uint8_t apvAddress() const { return apvAddress_; }
0097       bool operator<(const EventKey& rhs) const;
0098 
0099     private:
0100       uint32_t eventId_;
0101       uint8_t apvAddress_;
0102     };
0103     typedef std::vector<uint32_t> Counters;
0104     //class to wrap counters that can take ownership of them or not.
0105     //It behaves like the counters themself but, it actualy holds a pointer to them and dletes them if necessary
0106     class CountersWrapper {
0107     public:
0108       CountersWrapper(const Counters* theCounters);
0109       CountersWrapper(Counters* theCounters, const bool takeOwnership);
0110       ~CountersWrapper();
0111       const Counters::value_type operator[](const size_t i) const { return (*pConst)[i]; };
0112       const Counters::value_type at(const size_t i) const { return pConst->at(i); };
0113       Counters::const_iterator begin() const { return pConst->begin(); }
0114       Counters::const_iterator end() const { return pConst->end(); }
0115 
0116     private:
0117       const Counters* pConst;
0118       Counters* p;
0119       bool deleteP;
0120     };
0121     typedef std::shared_ptr<CountersWrapper> CountersPtr;
0122 
0123     //source for spy events
0124     typedef edm::VectorInputSource Source;
0125     //reference counted pointer to an event
0126 
0127     std::unique_ptr<Source> constructSource(const edm::ParameterSet& sourceConfig);
0128     void addNextEventToMap(const edm::EventPrincipal& nextSpyEvent);
0129     template <class T>
0130     static const T* getProduct(const edm::EventPrincipal& event, const edm::InputTag& tag);
0131     static CountersPtr getCounters(const edm::EventPrincipal& event,
0132                                    const edm::InputTag& tag,
0133                                    const bool mapKeyIsByFedID = true);
0134     void operator()(const edm::EventPrincipal& event);
0135     static void findMatchingFeds(const uint32_t eventId,
0136                                  const uint8_t apvAddress,
0137                                  CountersPtr totalEventCounters,
0138                                  CountersPtr l1aCounters,
0139                                  CountersPtr apvAddresses,
0140                                  std::set<uint16_t>& matchingFeds);
0141     static void mergeMatchingData(const std::set<uint16_t>& matchingFeds,
0142                                   const FEDRawDataCollection& inputRawData,
0143                                   CountersPtr inputTotalEventCounters,
0144                                   CountersPtr inputL1ACounters,
0145                                   CountersPtr inputAPVAddresses,
0146                                   const edm::DetSetVector<SiStripRawDigi>* inputScopeDigis,
0147                                   const edm::DetSetVector<SiStripRawDigi>* inputPayloadDigis,
0148                                   const edm::DetSetVector<SiStripRawDigi>* inputReorderedDigis,
0149                                   const edm::DetSetVector<SiStripRawDigi>* inputVirginRawDigis,
0150                                   FEDRawDataCollection& outputRawData,
0151                                   std::vector<uint32_t>& outputTotalEventCounters,
0152                                   std::vector<uint32_t>& outputL1ACounters,
0153                                   std::vector<uint32_t>& outputAPVAddresses,
0154                                   std::vector<edm::DetSet<SiStripRawDigi> >* outputScopeDigisVector,
0155                                   std::vector<edm::DetSet<SiStripRawDigi> >* outputPayloadDigisVector,
0156                                   std::vector<edm::DetSet<SiStripRawDigi> >* outputReorderedDigisVector,
0157                                   std::vector<edm::DetSet<SiStripRawDigi> >* outputVirginRawDigisVector,
0158                                   const SiStripFedCabling& cabling);
0159 
0160     std::map<EventKey, SpyEventList> eventMatches_;
0161     edm::InputTag rawDataTag_;
0162     edm::InputTag totalEventCountersTag_;
0163     edm::InputTag l1aCountersTag_;
0164     edm::InputTag apvAddressesTag_;
0165     edm::InputTag scopeDigisTag_;
0166     edm::InputTag payloadDigisTag_;
0167     edm::InputTag reorderedDigisTag_;
0168     edm::InputTag virginRawDigisTag_;
0169     uint32_t counterDiffMax_;
0170     std::shared_ptr<edm::ProductRegistry> productRegistry_;
0171     std::unique_ptr<edm::VectorInputSource> const source_;
0172     std::unique_ptr<edm::ProcessConfiguration> processConfiguration_;
0173     std::unique_ptr<edm::EventPrincipal> eventPrincipal_;
0174     static const char* const mlLabel_;
0175   };
0176 
0177   template <class T>
0178   const T* SpyEventMatcher::getProduct(const edm::EventPrincipal& event, const edm::InputTag& tag) {
0179     LogDebug(mlLabel_) << "Retrieving product " << tag;
0180     // Note: The third argument to getProductByTag can be a nullptr
0181     // as long as unscheduled execution of an EDProducer cannot occur
0182     // as a result of this function call (and with the current implementation
0183     // of SpyEventMatcher unscheduled execution never happens).
0184     const std::shared_ptr<const edm::Wrapper<T> > productWrapper = edm::getProductByTag<T>(event, tag, nullptr);
0185     if (productWrapper) {
0186       return productWrapper->product();
0187     } else {
0188       return nullptr;
0189     }
0190   }
0191 
0192   inline bool SpyEventMatcher::EventKey::operator<(const SpyEventMatcher::EventKey& rhs) const {
0193     return ((this->eventId_ < rhs.eventId_)
0194                 ? true
0195                 : (((this->eventId_ == rhs.eventId_) && (this->apvAddress_ < rhs.apvAddress_)) ? true : false));
0196   }
0197 
0198 }  // namespace sistrip
0199 
0200 #endif  //ndef DQM_SiStripMonitorHardware_SiStripSpyEventMatcher_H
0201 
0202 #endif  //SiStripMonitorHardware_BuildEventMatchingCode