00001 #define SiStripMonitorHardware_BuildEventMatchingCode
00002 #ifdef SiStripMonitorHardware_BuildEventMatchingCode
00003
00004 #ifndef DQM_SiStripMonitorHardware_SiStripSpyEventMatcher_H
00005 #define DQM_SiStripMonitorHardware_SiStripSpyEventMatcher_H
00006
00007 #include "FWCore/Utilities/interface/InputTag.h"
00008 #include "FWCore/Sources/interface/VectorInputSource.h"
00009 #include "FWCore/Framework/interface/EventPrincipal.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "boost/cstdint.hpp"
00012 #include <set>
00013 #include <map>
00014 #include <memory>
00015 #include <vector>
00016
00017
00018 class FEDRawDataCollection;
00019 class SiStripRawDigi;
00020 namespace edm {
00021 template<class T> class DetSetVector;
00022 template<class T> class DetSet;
00023 class EventID;
00024 class ParameterSet;
00025 }
00026 class SiStripFedCabling;
00027
00028 namespace sistrip {
00029
00030 class SpyEventMatcher
00031 {
00032 public:
00033 class SpyDataCollections
00034 {
00035 public:
00036 std::auto_ptr< FEDRawDataCollection > rawData;
00037 std::auto_ptr< std::vector<uint32_t> > totalEventCounters;
00038 std::auto_ptr< std::vector<uint32_t> > l1aCounters;
00039 std::auto_ptr< std::vector<uint32_t> > apvAddresses;
00040 std::auto_ptr< edm::DetSetVector<SiStripRawDigi> > scopeDigis;
00041 std::auto_ptr< edm::DetSetVector<SiStripRawDigi> > payloadDigis;
00042 std::auto_ptr< edm::DetSetVector<SiStripRawDigi> > reorderedDigis;
00043 std::auto_ptr< edm::DetSetVector<SiStripRawDigi> > virginRawDigis;
00044 SpyDataCollections();
00045
00046 SpyDataCollections(FEDRawDataCollection& theRawData,
00047 std::vector<uint32_t>& theTotalEventCounters,
00048 std::vector<uint32_t>& theL1ACounters,
00049 std::vector<uint32_t>& theAPVAddresses,
00050 std::vector< edm::DetSet<SiStripRawDigi> >* theScopeDigisVector,
00051 std::vector< edm::DetSet<SiStripRawDigi> >* thePayloadDigisVector,
00052 std::vector< edm::DetSet<SiStripRawDigi> >* theReorderedDigisVector,
00053 std::vector< edm::DetSet<SiStripRawDigi> >* theVirginRawDigisVector);
00054
00055 SpyDataCollections& operator = (SpyDataCollections original);
00056 };
00057 struct MatchingOutput
00058 {
00059 explicit MatchingOutput(FEDRawDataCollection& outputRawData);
00060 FEDRawDataCollection& outputRawData_;
00061 std::vector<uint32_t> outputTotalEventCounters_;
00062 std::vector<uint32_t> outputL1ACounters_;
00063 std::vector<uint32_t> outputAPVAddresses_;
00064 boost::shared_ptr< std::vector< edm::DetSet<SiStripRawDigi> > > outputScopeDigisVector_;
00065 boost::shared_ptr< std::vector< edm::DetSet<SiStripRawDigi> > > outputPayloadDigisVector_;
00066 boost::shared_ptr< std::vector< edm::DetSet<SiStripRawDigi> > > outputReorderedDigisVector_;
00067 boost::shared_ptr< std::vector< edm::DetSet<SiStripRawDigi> > > outputVirginRawDigisVector_;
00068 std::set<uint16_t> alreadyMergedFeds_;
00069 };
00070
00071 typedef edm::EventID EventID;
00072 typedef std::set<EventID> SpyEventList;
00073
00074 SpyEventMatcher(const edm::ParameterSet& config);
00075 virtual ~SpyEventMatcher();
00076
00077 void initialize();
00078
00079 const SpyEventList* matchesForEvent(const uint32_t eventId, const uint8_t apvAddress) const;
00080
00081 void getMatchedCollections(const uint32_t eventId, const uint8_t apvAddress, const SpyEventList* matchingEvents,
00082 const SiStripFedCabling& cabling, SpyDataCollections& collectionsToCreate);
00083
00084 void getCollections(const edm::EventPrincipal& event, const uint32_t eventId,
00085 const uint8_t apvAddress, const SiStripFedCabling& cabling,
00086 MatchingOutput& matchingOutput);
00087
00088 private:
00089 class EventKey
00090 {
00091 public:
00092 EventKey(const uint32_t eventId, const uint8_t apvAddress);
00093 uint32_t eventId() const { return eventId_; }
00094 uint8_t apvAddress() const { return apvAddress_; }
00095 bool operator < (const EventKey& rhs) const;
00096 private:
00097 uint32_t eventId_;
00098 uint8_t apvAddress_;
00099 };
00100 typedef std::vector<uint32_t> Counters;
00101
00102
00103 class CountersWrapper
00104 {
00105 public:
00106 CountersWrapper(const Counters* theCounters);
00107 CountersWrapper(Counters* theCounters, const bool takeOwnership);
00108 ~CountersWrapper();
00109 const Counters::value_type operator [] (const size_t i) const { return (*pConst)[i]; };
00110 const Counters::value_type at(const size_t i) const { return pConst->at(i); };
00111 Counters::const_iterator begin() const { return pConst->begin(); }
00112 Counters::const_iterator end() const { return pConst->end(); }
00113 private:
00114 const Counters* pConst;
00115 Counters* p;
00116 bool deleteP;
00117 };
00118 typedef boost::shared_ptr<CountersWrapper> CountersPtr;
00119
00120
00121 typedef edm::VectorInputSource Source;
00122
00123
00124 static std::auto_ptr<Source> constructSource(const edm::ParameterSet& sourceConfig);
00125 void addNextEventToMap(const edm::EventPrincipal& nextSpyEvent);
00126 template <class T> static const T* getProduct(const edm::EventPrincipal& event, const edm::InputTag& tag);
00127 static CountersPtr getCounters(const edm::EventPrincipal& event, const edm::InputTag& tag, const bool mapKeyIsByFedID = true);
00128 void operator()(const edm::EventPrincipal& event);
00129 static void findMatchingFeds(const uint32_t eventId, const uint8_t apvAddress,
00130 CountersPtr totalEventCounters,
00131 CountersPtr l1aCounters,
00132 CountersPtr apvAddresses,
00133 std::set<uint16_t>& matchingFeds);
00134 static void mergeMatchingData(const std::set<uint16_t>& matchingFeds,
00135 const FEDRawDataCollection& inputRawData,
00136 CountersPtr inputTotalEventCounters,
00137 CountersPtr inputL1ACounters,
00138 CountersPtr inputAPVAddresses,
00139 const edm::DetSetVector<SiStripRawDigi>* inputScopeDigis,
00140 const edm::DetSetVector<SiStripRawDigi>* inputPayloadDigis,
00141 const edm::DetSetVector<SiStripRawDigi>* inputReorderedDigis,
00142 const edm::DetSetVector<SiStripRawDigi>* inputVirginRawDigis,
00143 FEDRawDataCollection& outputRawData,
00144 std::vector<uint32_t>& outputTotalEventCounters,
00145 std::vector<uint32_t>& outputL1ACounters,
00146 std::vector<uint32_t>& outputAPVAddresses,
00147 std::vector< edm::DetSet<SiStripRawDigi> >* outputScopeDigisVector,
00148 std::vector< edm::DetSet<SiStripRawDigi> >* outputPayloadDigisVector,
00149 std::vector< edm::DetSet<SiStripRawDigi> >* outputReorderedDigisVector,
00150 std::vector< edm::DetSet<SiStripRawDigi> >* outputVirginRawDigisVector,
00151 const SiStripFedCabling& cabling);
00152
00153 std::map<EventKey,SpyEventList> eventMatches_;
00154 edm::InputTag rawDataTag_;
00155 edm::InputTag totalEventCountersTag_;
00156 edm::InputTag l1aCountersTag_;
00157 edm::InputTag apvAddressesTag_;
00158 edm::InputTag scopeDigisTag_;
00159 edm::InputTag payloadDigisTag_;
00160 edm::InputTag reorderedDigisTag_;
00161 edm::InputTag virginRawDigisTag_;
00162 std::auto_ptr<Source> source_;
00163 uint32_t counterDiffMax_;
00164 static const char* mlLabel_;
00165 };
00166
00167 template <class T> const T* SpyEventMatcher::getProduct(const edm::EventPrincipal& event, const edm::InputTag& tag)
00168 {
00169 LogDebug(mlLabel_) << "Retrieving product " << tag;
00170 const boost::shared_ptr< const edm::Wrapper<T> > productWrapper = edm::getProductByTag<T>(event,tag);
00171 if (productWrapper) {
00172 return productWrapper->product();
00173 } else {
00174 return NULL;
00175 }
00176 }
00177
00178 inline bool SpyEventMatcher::EventKey::operator < (const SpyEventMatcher::EventKey& rhs) const
00179 {
00180 return ( (this->eventId_<rhs.eventId_) ? true : ( ((this->eventId_==rhs.eventId_) && (this->apvAddress_<rhs.apvAddress_)) ? true : false) );
00181 }
00182
00183 }
00184
00185 #endif //ndef DQM_SiStripMonitorHardware_SiStripSpyEventMatcher_H
00186
00187 #endif //SiStripMonitorHardware_BuildEventMatchingCode