Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-28 03:09:49

0001 #include <iostream>
0002 #include <iomanip>
0003 #include <bitset>
0004 #include <string>
0005 #include <map>
0006 
0007 #include "FWCore/Framework/interface/stream/EDProducer.h"
0008 #include "FWCore/Framework/interface/MakerMacros.h"
0009 #include "FWCore/Framework/interface/EventSetup.h"
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/ESHandle.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/Utilities/interface/InputTag.h"
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015 
0016 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
0017 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0018 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0019 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
0020 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0021 
0022 #include "EventFilter/L1TRawToDigi/interface/AMC13Spec.h"
0023 #include "EventFilter/L1TRawToDigi/interface/AMCSpec.h"
0024 #include "FWCore/Utilities/interface/CRC16.h"
0025 
0026 #include "DataFormats/L1TMuon/interface/OMTF/OmtfDataWord64.h"
0027 #include "DataFormats/L1TMuon/interface/OMTF/OmtfCscDataWord64.h"
0028 #include "EventFilter/L1TRawToDigi/interface/OmtfDtDataWord64.h"
0029 #include "EventFilter/L1TRawToDigi/interface/OmtfRpcDataWord64.h"
0030 #include "EventFilter/L1TRawToDigi/interface/OmtfMuonDataWord64.h"
0031 
0032 #include "DataFormats/CSCDigi/interface/CSCCorrelatedLCTDigi.h"
0033 #include "DataFormats/CSCDigi/interface/CSCCorrelatedLCTDigiCollection.h"
0034 
0035 #include "DataFormats/L1DTTrackFinder/interface/L1MuDTChambPhContainer.h"
0036 #include "DataFormats/L1DTTrackFinder/interface/L1MuDTChambThContainer.h"
0037 
0038 #include "DataFormats/RPCDigi/interface/RPCDigiCollection.h"
0039 
0040 #include "EventFilter/L1TRawToDigi/interface/OmtfRpcPacker.h"
0041 #include "EventFilter/L1TRawToDigi/interface/OmtfCscPacker.h"
0042 #include "EventFilter/L1TRawToDigi/interface/OmtfDtPacker.h"
0043 
0044 #include "CondFormats/RPCObjects/interface/RPCEMap.h"
0045 #include "CondFormats/DataRecord/interface/RPCEMapRcd.h"
0046 #include "CondFormats/DataRecord/interface/RPCOMTFLinkMapRcd.h"
0047 
0048 namespace omtf {
0049 
0050   class OmtfPacker : public edm::stream::EDProducer<> {
0051   public:
0052     OmtfPacker(const edm::ParameterSet& pset);
0053 
0054     ~OmtfPacker() override {}
0055 
0056     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0057 
0058     void produce(edm::Event& ev, const edm::EventSetup& es) override;
0059 
0060     void beginRun(const edm::Run& run, const edm::EventSetup& es) override;
0061 
0062   private:
0063     edm::ParameterSet theConfig;
0064     unsigned long theEventCounter;
0065 
0066     std::string theOutputTag;
0067 
0068     bool theSkipRpc, theSkipCsc, theSkipDt;
0069 
0070     edm::EDGetTokenT<RPCDigiCollection> theRpcToken;
0071     edm::EDGetTokenT<CSCCorrelatedLCTDigiCollection> theCscToken;
0072     edm::EDGetTokenT<L1MuDTChambThContainer> theDtThToken;
0073     edm::EDGetTokenT<L1MuDTChambPhContainer> theDtPhToken;
0074 
0075     edm::ESGetToken<RPCEMap, RPCEMapRcd> theRPCEMapToken;
0076     edm::ESGetToken<RPCAMCLinkMap, RPCOMTFLinkMapRcd> theAmcMappingToken;
0077 
0078     CscPacker theCscPacker;
0079     RpcPacker theRpcPacker;
0080     DtPacker theDtPacker;
0081   };
0082 
0083   OmtfPacker::OmtfPacker(const edm::ParameterSet& pset) : theConfig(pset), theEventCounter(0) {
0084     theOutputTag = pset.getParameter<std::string>("outputTag");
0085 
0086     produces<FEDRawDataCollection>(theOutputTag);
0087 
0088     theRpcToken = consumes<RPCDigiCollection>(pset.getParameter<edm::InputTag>("rpcInputLabel"));
0089     theCscToken = consumes<CSCCorrelatedLCTDigiCollection>(pset.getParameter<edm::InputTag>("cscInputLabel"));
0090     theDtPhToken = consumes<L1MuDTChambPhContainer>(pset.getParameter<edm::InputTag>("dtPhInputLabel"));
0091     theDtThToken = consumes<L1MuDTChambThContainer>(pset.getParameter<edm::InputTag>("dtThInputLabel"));
0092 
0093     theSkipDt = pset.getParameter<bool>("skipDt");
0094     theSkipRpc = pset.getParameter<bool>("skipRpc");
0095     theSkipCsc = pset.getParameter<bool>("skipCsc");
0096 
0097     if (!theSkipRpc) {
0098       theRPCEMapToken = esConsumes<edm::Transition::BeginRun>();
0099       if (not theConfig.getParameter<bool>("useRpcConnectionFile")) {
0100         theAmcMappingToken = esConsumes<edm::Transition::BeginRun>();
0101       }
0102     }
0103   }
0104 
0105   void OmtfPacker::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0106     edm::ParameterSetDescription desc;
0107     desc.add<edm::InputTag>("rpcInputLabel", edm::InputTag(""));
0108     desc.add<edm::InputTag>("cscInputLabel", edm::InputTag(""));
0109     desc.add<edm::InputTag>("dtPhInputLabel", edm::InputTag(""));
0110     desc.add<edm::InputTag>("dtThInputLabel", edm::InputTag(""));
0111     desc.add<bool>("skipRpc", false);
0112     desc.add<bool>("skipCsc", false);
0113     desc.add<bool>("skipDt", false);
0114     desc.add<bool>("useRpcConnectionFile", false);
0115     desc.add<std::string>("rpcConnectionFile", "");
0116     desc.add<std::string>("outputTag", "");
0117     descriptions.add("omtfPacker", desc);
0118   }
0119 
0120   void OmtfPacker::beginRun(const edm::Run& run, const edm::EventSetup& es) {
0121     //
0122     // initialise RPC packer
0123     //
0124     if (!theSkipRpc) {
0125       edm::ESTransientHandle<RPCEMap> readoutMapping = es.getTransientHandle(theRPCEMapToken);
0126       if (theConfig.getParameter<bool>("useRpcConnectionFile")) {
0127         theRpcPacker.init(*readoutMapping,
0128                           edm::FileInPath(theConfig.getParameter<std::string>("rpcConnectionFile")).fullPath());
0129       } else {
0130         auto const& amcMapping = es.getData(theAmcMappingToken);
0131         theRpcPacker.init(*readoutMapping, amcMapping);
0132       }
0133     }
0134 
0135     //
0136     // init CSC Link map
0137     //
0138     if (!theSkipCsc)
0139       theCscPacker.init();
0140   }
0141 
0142   void OmtfPacker::produce(edm::Event& ev, const edm::EventSetup& es) {
0143     LogDebug("OmtfPacker::produce") << "Beginning To Pack Event: " << ++theEventCounter;
0144 
0145     //
0146     // prepare FedAmcRawsMap structure to collect Word64 raws from digi packers
0147     //
0148     FedAmcRawsMap raws;
0149     std::list<unsigned int> amcIds = {1, 3, 5, 7, 9, 11};
0150     std::list<unsigned int> fedIds = {1380, 1381};
0151     for (auto& fedId : fedIds) {
0152       for (auto& amcId : amcIds) {
0153         raws[std::make_pair(fedId, amcId)];
0154       }
0155     }
0156 
0157     //
0158     // DT raws
0159     //
0160     if (!theSkipDt) {
0161       edm::Handle<L1MuDTChambPhContainer> digiCollectionDTPh;
0162       ev.getByToken(theDtPhToken, digiCollectionDTPh);
0163       edm::Handle<L1MuDTChambThContainer> digiCollectionDTTh;
0164       ev.getByToken(theDtThToken, digiCollectionDTTh);
0165       theDtPacker.pack(digiCollectionDTPh.product(), digiCollectionDTTh.product(), raws);
0166     }
0167 
0168     //
0169     // csc raws
0170     //
0171     if (!theSkipCsc) {
0172       edm::Handle<CSCCorrelatedLCTDigiCollection> digiCollectionCSC;
0173       ev.getByToken(theCscToken, digiCollectionCSC);
0174       theCscPacker.pack(digiCollectionCSC.product(), raws);
0175     }
0176 
0177     //
0178     // rpc raws
0179     //
0180     if (!theSkipRpc) {
0181       edm::Handle<RPCDigiCollection> digiCollectionRPC;
0182       ev.getByToken(theRpcToken, digiCollectionRPC);
0183       theRpcPacker.pack(digiCollectionRPC.product(), raws);
0184     }
0185 
0186     auto bxId = ev.bunchCrossing();
0187     auto evtId = ev.id().event();
0188     auto orbit = ev.eventAuxiliary().orbitNumber();
0189     std::unique_ptr<FEDRawDataCollection> raw_coll(new FEDRawDataCollection());
0190     for (auto& fedId : fedIds) {
0191       //
0192       // assign formatted raws to feds
0193       //
0194       amc13::Packet amc13;
0195       for (const auto& it : raws) {
0196         if (fedId != it.first.first)
0197           continue;
0198         const std::vector<Word64>& amcData = it.second;
0199         unsigned int amcId = it.first.second;
0200         for (const auto& raw : amcData) {
0201           std::ostringstream dataStr;
0202           if (DataWord64::csc == DataWord64::type(raw))
0203             dataStr << CscDataWord64(raw);
0204           if (DataWord64::dt == DataWord64::type(raw))
0205             dataStr << DtDataWord64(raw);
0206           if (DataWord64::rpc == DataWord64::type(raw))
0207             dataStr << RpcDataWord64(raw);
0208           if (DataWord64::omtf == DataWord64::type(raw))
0209             dataStr << MuonDataWord64(raw);
0210           LogTrace("") << " fed: " << fedId << " amcId: " << amcId << " RAW DATA: " << dataStr.str() << std::endl;
0211         }
0212         amc13.add(amcId, 43981, evtId, orbit, bxId, amcData);
0213       }
0214 
0215       FEDRawData& fed_data = raw_coll->FEDData(fedId);
0216 
0217       const unsigned int slinkHeaderSize = 8;
0218       const unsigned int slinkTrailerSize = 8;
0219       unsigned int size = (amc13.size()) * sizeof(Word64) + slinkHeaderSize + slinkTrailerSize;
0220       fed_data.resize(size);
0221       unsigned char* payload = fed_data.data();
0222       unsigned char* payload_start = payload;
0223 
0224       FEDHeader header(payload);
0225       const unsigned int evtType = 1;
0226       header.set(payload, evtType, evtId, bxId, fedId);
0227 
0228       amc13.write(ev, payload, slinkHeaderSize, size - slinkHeaderSize - slinkTrailerSize);
0229 
0230       payload += slinkHeaderSize;
0231       payload += amc13.size() * 8;
0232 
0233       FEDTrailer trailer(payload);
0234       trailer.set(payload, size / 8, evf::compute_crc(payload_start, size), 0, 0);
0235     }
0236     ev.put(std::move(raw_coll), theOutputTag);
0237   }
0238 
0239 }  // namespace omtf
0240 using namespace omtf;
0241 DEFINE_FWK_MODULE(OmtfPacker);