Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:53

0001 // system include files
0002 #include <iostream>
0003 #include <iomanip>
0004 #include <sstream>
0005 #include <bitset>
0006 #include <string>
0007 
0008 // user include files
0009 #include "FWCore/Framework/interface/stream/EDProducer.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/Framework/interface/EventSetup.h"
0013 #include "FWCore/Framework/interface/ESHandle.h"
0014 
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 #include "FWCore/Utilities/interface/InputTag.h"
0017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0018 
0019 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
0020 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0021 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0022 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
0023 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0024 
0025 #include "EventFilter/L1TRawToDigi/interface/AMC13Spec.h"
0026 #include "EventFilter/L1TRawToDigi/interface/AMCSpec.h"
0027 
0028 #include "DataFormats/CSCDigi/interface/CSCCorrelatedLCTDigiCollection.h"
0029 #include "DataFormats/L1TMuon/interface/RegionalMuonCand.h"
0030 #include "DataFormats/L1TMuon/interface/RegionalMuonCandFwd.h"
0031 #include "DataFormats/L1DTTrackFinder/interface/L1MuDTChambPhContainer.h"
0032 #include "DataFormats/L1DTTrackFinder/interface/L1MuDTChambThContainer.h"
0033 #include "DataFormats/RPCDigi/interface/RPCDigiCollection.h"
0034 
0035 #include "DataFormats/L1TMuon/interface/OMTF/OmtfDataWord64.h"
0036 #include "DataFormats/L1TMuon/interface/OMTF/OmtfCscDataWord64.h"
0037 #include "EventFilter/L1TRawToDigi/interface/OmtfDtDataWord64.h"
0038 #include "EventFilter/L1TRawToDigi/interface/OmtfRpcDataWord64.h"
0039 #include "EventFilter/L1TRawToDigi/interface/OmtfMuonDataWord64.h"
0040 
0041 #include "EventFilter/L1TRawToDigi/interface/OmtfRpcUnpacker.h"
0042 #include "EventFilter/L1TRawToDigi/interface/OmtfCscUnpacker.h"
0043 #include "EventFilter/L1TRawToDigi/interface/OmtfDtUnpacker.h"
0044 #include "EventFilter/L1TRawToDigi/interface/OmtfMuonUnpacker.h"
0045 
0046 #include "CondFormats/RPCObjects/interface/RPCEMap.h"
0047 #include "CondFormats/DataRecord/interface/RPCEMapRcd.h"
0048 #include "CondFormats/DataRecord/interface/RPCOMTFLinkMapRcd.h"
0049 
0050 namespace omtf {
0051 
0052   class OmtfUnpacker : public edm::stream::EDProducer<> {
0053   public:
0054     OmtfUnpacker(const edm::ParameterSet& pset);
0055 
0056     ~OmtfUnpacker() override {}
0057 
0058     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0059 
0060     void produce(edm::Event& ev, const edm::EventSetup& es) override;
0061 
0062     void beginRun(const edm::Run& run, const edm::EventSetup& es) override;
0063 
0064   private:
0065     unsigned long theEventCounter;
0066 
0067     edm::EDGetTokenT<FEDRawDataCollection> theFedDataToken;
0068 
0069     edm::ESGetToken<RPCEMap, RPCEMapRcd> theRPCEMapToken;
0070     edm::ESGetToken<RPCAMCLinkMap, RPCOMTFLinkMapRcd> theAmcMappingToken;
0071 
0072     RpcUnpacker theRpcUnpacker;
0073     CscUnpacker theCscUnpacker;
0074     DtUnpacker theDtUnpacker;
0075     MuonUnpacker theMuonUnpacker;
0076 
0077     edm::ParameterSet theConfig;
0078     std::string theOutputTag;
0079 
0080     bool theSkipRpc, theSkipCsc, theSkipDt, theSkipMuon;
0081   };
0082 
0083   OmtfUnpacker::OmtfUnpacker(const edm::ParameterSet& pset) : theConfig(pset) {
0084     theOutputTag = pset.getParameter<std::string>("outputTag");
0085 
0086     produces<RPCDigiCollection>(theOutputTag);
0087     produces<CSCCorrelatedLCTDigiCollection>(theOutputTag);
0088     produces<l1t::RegionalMuonCandBxCollection>(theOutputTag);
0089     produces<L1MuDTChambPhContainer>(theOutputTag);
0090     produces<L1MuDTChambThContainer>(theOutputTag);
0091 
0092     theSkipDt = pset.getParameter<bool>("skipDt");
0093     theSkipRpc = pset.getParameter<bool>("skipRpc");
0094     theSkipCsc = pset.getParameter<bool>("skipCsc");
0095     theSkipMuon = pset.getParameter<bool>("skipMuon");
0096 
0097     theFedDataToken = consumes<FEDRawDataCollection>(pset.getParameter<edm::InputTag>("inputLabel"));
0098 
0099     if (!theSkipRpc) {
0100       theRPCEMapToken = esConsumes<edm::Transition::BeginRun>();
0101       if (not theConfig.getParameter<bool>("useRpcConnectionFile")) {
0102         theAmcMappingToken = esConsumes<edm::Transition::BeginRun>();
0103       }
0104     }
0105   }
0106 
0107   void OmtfUnpacker::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0108     edm::ParameterSetDescription desc;
0109     desc.add<edm::InputTag>("inputLabel", edm::InputTag("rawDataCollector"));
0110     desc.add<bool>("skipRpc", false);
0111     desc.add<bool>("skipCsc", false);
0112     desc.add<bool>("skipDt", false);
0113     desc.add<bool>("skipMuon", false);
0114     desc.add<bool>("useRpcConnectionFile", false);
0115     desc.add<std::string>("rpcConnectionFile", "");
0116     desc.add<std::string>("outputTag", "");
0117     descriptions.add("omtfUnpacker", desc);
0118   }
0119 
0120   void OmtfUnpacker::beginRun(const edm::Run& run, const edm::EventSetup& es) {
0121     //
0122     // rpc unpacker
0123     //
0124     if (!theSkipRpc) {
0125       edm::ESTransientHandle<RPCEMap> readoutMapping = es.getTransientHandle(theRPCEMapToken);
0126       if (theConfig.getParameter<bool>("useRpcConnectionFile")) {
0127         theRpcUnpacker.init(*readoutMapping,
0128                             edm::FileInPath(theConfig.getParameter<std::string>("rpcConnectionFile")).fullPath());
0129       } else {
0130         auto const& amcMapping = es.getData(theAmcMappingToken);
0131         theRpcUnpacker.init(*readoutMapping, amcMapping);
0132       }
0133     }
0134 
0135     //
0136     // csc unpacker
0137     //
0138     if (!theSkipCsc)
0139       theCscUnpacker.init();
0140   }
0141 
0142   void OmtfUnpacker::produce(edm::Event& event, const edm::EventSetup& setup) {
0143     bool debug = edm::MessageDrop::instance()->debugEnabled;
0144     theEventCounter++;
0145     if (debug)
0146       LogDebug("OmtfUnpacker::produce") << "Beginning To Unpack Event: " << theEventCounter;
0147 
0148     edm::Handle<FEDRawDataCollection> allFEDRawData;
0149     event.getByToken(theFedDataToken, allFEDRawData);
0150 
0151     auto producedRPCDigis = std::make_unique<RPCDigiCollection>();
0152     auto producedCscLctDigis = std::make_unique<CSCCorrelatedLCTDigiCollection>();
0153     auto producedMuonDigis = std::make_unique<l1t::RegionalMuonCandBxCollection>();
0154     producedMuonDigis->setBXRange(-3, 4);
0155     auto producedDTPhDigis = std::make_unique<L1MuDTChambPhContainer>();
0156     auto producedDTThDigis = std::make_unique<L1MuDTChambThContainer>();
0157     std::vector<L1MuDTChambPhDigi> phi_Container;
0158     std::vector<L1MuDTChambThDigi> the_Container;
0159 
0160     for (int fedId = 1380; fedId <= 1381; ++fedId) {
0161       const FEDRawData& rawData = allFEDRawData->FEDData(fedId);
0162       unsigned int nWords = rawData.size() / sizeof(Word64);
0163       LogTrace("") << "FED : " << fedId << " words: " << nWords;
0164       if (nWords == 0)
0165         continue;
0166 
0167       //
0168       // FED header
0169       //
0170       const Word64* header = reinterpret_cast<const Word64*>(rawData.data());
0171       FEDHeader fedHeader(reinterpret_cast<const unsigned char*>(header));
0172       if (!fedHeader.check()) {
0173         LogTrace("") << " ** PROBLEM **, header.check() failed, break";
0174         break;
0175       }
0176       if (fedHeader.sourceID() != fedId) {
0177         LogTrace("") << " ** PROBLEM **, fedHeader.sourceID() != fedId"
0178                      << "fedId = " << fedId << " sourceID=" << fedHeader.sourceID();
0179       }
0180       int triggerBX = fedHeader.bxID();
0181       if (debug) {
0182         std::ostringstream str;
0183         str << "  header: " << *reinterpret_cast<const std::bitset<64>*>(header) << std::endl;
0184         str << "  header triggerType: " << fedHeader.triggerType() << std::endl;
0185         str << "  header lvl1ID:      " << fedHeader.lvl1ID() << std::endl;
0186         str << "  header bxID:        " << fedHeader.bxID() << std::endl;
0187         str << "  header sourceID:    " << fedHeader.sourceID() << std::endl;
0188         str << "  header version:     " << fedHeader.version() << std::endl;
0189         str << "  header more :     " << fedHeader.moreHeaders() << std::endl;
0190         str << " triggerBx " << triggerBX << std::endl;
0191         LogTrace("") << str.str();
0192       }
0193 
0194       //
0195       // FED trailer
0196       //
0197       const Word64* trailer = reinterpret_cast<const Word64*>(rawData.data()) + (nWords - 1);
0198       trailer++;
0199       bool moreTrailers = true;
0200       while (moreTrailers) {
0201         trailer--;
0202         FEDTrailer fedTrailer(reinterpret_cast<const unsigned char*>(trailer));
0203         if (!fedTrailer.check()) {
0204           if (debug)
0205             LogTrace("") << " ** PROBLEM **, trailer.check() failed, break";
0206           break;
0207         }
0208         if (fedTrailer.fragmentLength() != nWords) {
0209           if (debug)
0210             LogTrace("") << " ** PROBLEM **, fedTrailer.fragmentLength()!= nWords, break";
0211           break;
0212         }
0213         moreTrailers = fedTrailer.moreTrailers();
0214         if (debug) {
0215           std::ostringstream str;
0216           str << " trailer: " << *reinterpret_cast<const std::bitset<64>*>(trailer) << std::endl;
0217           str << "  trailer lenght:    " << fedTrailer.fragmentLength() << std::endl;
0218           str << "  trailer crc:       " << fedTrailer.crc() << std::endl;
0219           str << "  trailer evtStatus: " << fedTrailer.evtStatus() << std::endl;
0220           str << "  trailer ttsBits:   " << fedTrailer.ttsBits() << std::endl;
0221           LogTrace("") << str.str();
0222         }
0223       }
0224 
0225       //
0226       // dump all FED data for debug
0227       //
0228       if (debug) {
0229         std::ostringstream str;
0230         for (const Word64* word = header + 1; word != trailer; word++) {
0231           str << "    data: " << *reinterpret_cast<const std::bitset<64>*>(word) << std::endl;
0232         }
0233         LogTrace("") << str.str();
0234       }
0235 
0236       //
0237       // AMC13 header
0238       //
0239       const Word64* headerAmc13raw = header + 1;
0240       amc13::Header headerAmc13(headerAmc13raw);
0241       if (debug) {
0242         std::ostringstream str;
0243         str << " headerAMC13:  " << *reinterpret_cast<const std::bitset<64>*>(headerAmc13raw) << std::endl;
0244         str << " amc13 check:  " << headerAmc13.check() << std::endl;
0245         str << " amc13 format: " << headerAmc13.getFormatVersion() << std::endl;
0246         str << " amc13 nAMCs:  " << headerAmc13.getNumberOfAMCs() << std::endl;
0247         str << " amc13 orbit:  " << headerAmc13.getOrbitNumber() << std::endl;
0248         LogTrace("") << str.str();
0249       }
0250       //unsigned int nAMCs = headerAmc13.getNumberOfAMCs();
0251       //for (unsigned int iAMC = 0; iAMC <nAMCs; iAMC++) {
0252       // const Word64* raw = header+1 +(iAMC+1);
0253       //}
0254 
0255       //
0256       // AMC13 trailer
0257       //
0258       const Word64* trailerAmc13raw = trailer - 1;
0259       amc13::Trailer trailerAmc13(trailerAmc13raw);
0260       if (debug) {
0261         std::ostringstream str;
0262         str << " trailerAMC13:  " << *reinterpret_cast<const std::bitset<64>*>(trailerAmc13raw) << std::endl;
0263         str << " crc:  " << trailerAmc13.getCRC() << std::endl;
0264         str << " block: " << trailerAmc13.getBlock() << std::endl;
0265         str << " LV1ID:  " << trailerAmc13.getLV1ID() << std::endl;
0266         str << " BX:  " << trailerAmc13.getBX() << std::endl;
0267         LogTrace("") << str.str();
0268       }
0269 
0270       //
0271       // get AMC13 payload (-> AMC's data)
0272       //
0273       amc13::Packet packetAmc13;
0274       if (!packetAmc13.parse(header, header + 1, nWords - 2, fedHeader.lvl1ID(), fedHeader.bxID(), true, false)) {
0275         edm::LogError("OMTF") << "Could not extract AMC13 Packet.";
0276         return;
0277       }
0278       //LogTrace("") <<"AMC13 Packet: "<< packetAmc13.blocks() << " size "<<packetAmc13.size() << std::endl;
0279 
0280       //
0281       // loop over AMC's
0282       //
0283       unsigned int blockNum = 0;
0284       for (auto amc : packetAmc13.payload()) {
0285         amc::BlockHeader bh = amc.blockHeader();
0286         if (debug) {
0287           std::ostringstream str;
0288           str << " ----------- #" << blockNum++ << std::endl;
0289           str << " blockheader:  " << std::bitset<64>(bh.raw()) << std::endl;
0290           str << " boardID:  " << bh.getBoardID() << std::endl;
0291           str << " amcNumber:  " << bh.getAMCNumber() << std::endl;
0292           str << " size:  " << bh.getSize();  // << std::endl;
0293           LogTrace("") << str.str();
0294         }
0295 
0296         //
0297         // AMC header
0298         //
0299         amc::Header headerAmc = amc.header();
0300         if (debug) {
0301           std::ostringstream str;
0302           str << " AMC header[0]:  " << std::bitset<64>(headerAmc.raw()[0]) << std::endl;
0303           str << " AMC header[1]:  " << std::bitset<64>(headerAmc.raw()[1]) << std::endl;
0304           str << " AMC number:     " << headerAmc.getAMCNumber();  // << std::endl;
0305           LogTrace("") << str.str();
0306         }
0307 
0308         //
0309         // AMC trailer
0310         //
0311         //amc::Trailer trailerAmc = amc.trailer();              //this is the expected way but does not work
0312         amc::Trailer trailerAmc(amc.data().get() + amc.size() -
0313                                 1);  //FIXME: the above is prefered but this works (CMSSW900)
0314         if (debug) {
0315           std::ostringstream str;
0316           str << " AMC trailer:  " << std::bitset<64>(trailerAmc.raw()) << std::endl;
0317           str << " getLV1ID:     " << trailerAmc.getLV1ID() << std::endl;
0318           str << " size:         " << trailerAmc.getSize() << std::endl;
0319           LogTrace("") << str.str();
0320         }
0321 
0322         //
0323         // AMC payload
0324         //
0325         const auto& payload64 = amc.data();
0326         const Word64* word = payload64.get();
0327         for (unsigned int iWord = 1; iWord <= amc.size(); iWord++, word++) {
0328           if (iWord <= 2)
0329             continue;  // two header words for each AMC
0330           if (iWord == amc.size())
0331             continue;  // trailer for each AMC
0332 
0333           LogTrace("") << " payload: " << *reinterpret_cast<const std::bitset<64>*>(word);
0334           DataWord64::Type recordType = DataWord64::type(*word);
0335 
0336           unsigned int fedId = fedHeader.sourceID();
0337           unsigned int amcId = bh.getAMCNumber() / 2 + 1;  // in OMTF convertsion 1-6
0338           //
0339           // RPC data
0340           //
0341           if (DataWord64::rpc == recordType && !theSkipRpc) {
0342             theRpcUnpacker.unpack(triggerBX, fedId, amcId, RpcDataWord64(*word), producedRPCDigis.get());
0343           }
0344 
0345           //
0346           // CSC data
0347           //
0348           if (DataWord64::csc == recordType && !theSkipCsc) {
0349             theCscUnpacker.unpack(fedId, amcId, CscDataWord64(*word), producedCscLctDigis.get());
0350           }
0351 
0352           //
0353           // DT data
0354           //
0355           if (DataWord64::dt == recordType && !theSkipDt) {
0356             theDtUnpacker.unpack(fedId, amcId, DtDataWord64(*word), phi_Container, the_Container);
0357           }
0358 
0359           //
0360           // OMTF (muon) data
0361           //
0362           if (DataWord64::omtf == recordType && !theSkipMuon) {
0363             theMuonUnpacker.unpack(fedId, amcId, MuonDataWord64(*word), producedMuonDigis.get());
0364           }
0365         }
0366       }
0367     }
0368     event.put(std::move(producedRPCDigis), theOutputTag);
0369     event.put(std::move(producedCscLctDigis), theOutputTag);
0370     event.put(std::move(producedMuonDigis), theOutputTag);
0371     producedDTPhDigis->setContainer(phi_Container);
0372     event.put(std::move(producedDTPhDigis), theOutputTag);
0373     producedDTThDigis->setContainer(the_Container);
0374     event.put(std::move(producedDTThDigis), theOutputTag);
0375   }
0376 
0377 }  // namespace omtf
0378 
0379 using namespace omtf;
0380 DEFINE_FWK_MODULE(OmtfUnpacker);