Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "RPCUnpackingModule.h"
0002 #include "CondFormats/RPCObjects/interface/RPCReadOutMapping.h"
0003 #include "EventFilter/RPCRawToDigi/interface/RPCRecordFormatter.h"
0004 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0005 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0006 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0007 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
0008 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
0009 #include "DataFormats/RPCDigi/interface/RPCDigiCollection.h"
0010 #include "DataFormats/RPCDigi/interface/RPCRawDataCounts.h"
0011 #include "DataFormats/Common/interface/Handle.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 //#include "FWCore/Framework/interface/ESHandle.h"
0014 #include "FWCore/Framework/interface/ESTransientHandle.h"
0015 #include "FWCore/Framework/interface/EventSetup.h"
0016 #include "FWCore/Framework/interface/ESWatcher.h"
0017 
0018 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0019 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0022 
0023 #include "DataFormats/RPCDigi/interface/DataRecord.h"
0024 #include "DataFormats/RPCDigi/interface/ReadoutError.h"
0025 #include "DataFormats/RPCDigi/interface/RPCRawSynchro.h"
0026 #include "EventFilter/RPCRawToDigi/interface/EventRecords.h"
0027 #include "EventFilter/RPCRawToDigi/interface/DebugDigisPrintout.h"
0028 
0029 #include <sstream>
0030 #include <bitset>
0031 
0032 using namespace edm;
0033 using namespace std;
0034 using namespace rpcrawtodigi;
0035 
0036 typedef uint64_t Word64;
0037 
0038 RPCUnpackingModule::RPCUnpackingModule(const edm::ParameterSet& pset)
0039     : dataLabel_(pset.getParameter<edm::InputTag>("InputLabel")),
0040       doSynchro_(pset.getParameter<bool>("doSynchro")),
0041       eventCounter_(0),
0042       theReadoutMappingToken(esConsumes<edm::Transition::BeginRun>()),
0043       theCabling(nullptr) {
0044   produces<RPCDigiCollection>();
0045   produces<RPCRawDataCounts>();
0046   if (doSynchro_)
0047     produces<RPCRawSynchro::ProdItem>();
0048   fedToken_ = consumes<FEDRawDataCollection>(dataLabel_);
0049 }
0050 
0051 RPCUnpackingModule::~RPCUnpackingModule() { delete theCabling; }
0052 
0053 void RPCUnpackingModule::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0054   edm::ParameterSetDescription desc;
0055   desc.add<edm::InputTag>("InputLabel", edm::InputTag("rawDataCollector"));
0056   desc.add<bool>("doSynchro", true);
0057   descriptions.add("rpcUnpackingModule", desc);
0058 }
0059 
0060 void RPCUnpackingModule::beginRun(const edm::Run& run, const edm::EventSetup& es) {
0061   if (theRecordWatcher.check(es)) {
0062     LogTrace("") << "record has CHANGED!!, (re)initialise readout map!";
0063     delete theCabling;
0064     ESTransientHandle<RPCEMap> readoutMapping = es.getTransientHandle(theReadoutMappingToken);
0065     theCabling = readoutMapping->convert();
0066     theReadoutMappingSearch.init(theCabling);
0067     LogTrace("") << " READOUT MAP VERSION: " << theCabling->version() << endl;
0068   }
0069 }
0070 
0071 void RPCUnpackingModule::produce(Event& ev, const EventSetup& es) {
0072   bool debug = edm::MessageDrop::instance()->debugEnabled;
0073   eventCounter_++;
0074   if (debug)
0075     LogDebug("RPCUnpacker::produce") << "Beginning To Unpack Event: " << eventCounter_;
0076 
0077   Handle<FEDRawDataCollection> allFEDRawData;
0078   ev.getByToken(fedToken_, allFEDRawData);
0079 
0080   auto producedRPCDigis = std::make_unique<RPCDigiCollection>();
0081   auto producedRawDataCounts = std::make_unique<RPCRawDataCounts>();
0082   std::unique_ptr<RPCRawSynchro::ProdItem> producedRawSynchoCounts;
0083   if (doSynchro_)
0084     producedRawSynchoCounts = std::make_unique<RPCRawSynchro::ProdItem>();
0085 
0086   int status = 0;
0087   for (int fedId = FEDNumbering::MINRPCFEDID; fedId <= FEDNumbering::MAXRPCFEDID; ++fedId) {
0088     const FEDRawData& rawData = allFEDRawData->FEDData(fedId);
0089     RPCRecordFormatter interpreter =
0090         theCabling ? RPCRecordFormatter(fedId, &theReadoutMappingSearch) : RPCRecordFormatter(fedId, nullptr);
0091     int triggerBX = 0;
0092     unsigned int nWords = rawData.size() / sizeof(Word64);
0093     if (nWords == 0)
0094       continue;
0095 
0096     //
0097     // check headers
0098     //
0099     const Word64* header = reinterpret_cast<const Word64*>(rawData.data());
0100     header--;
0101     bool moreHeaders = true;
0102     while (moreHeaders) {
0103       header++;
0104       FEDHeader fedHeader(reinterpret_cast<const unsigned char*>(header));
0105       if (!fedHeader.check()) {
0106         producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::HeaderCheckFail));
0107         if (debug)
0108           LogTrace("") << " ** PROBLEM **, header.check() failed, break";
0109         break;
0110       }
0111       if (fedHeader.sourceID() != fedId) {
0112         producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::InconsitentFedId));
0113         if (debug)
0114           LogTrace("") << " ** PROBLEM **, fedHeader.sourceID() != fedId"
0115                        << "fedId = " << fedId << " sourceID=" << fedHeader.sourceID();
0116       }
0117       triggerBX = fedHeader.bxID();
0118       moreHeaders = fedHeader.moreHeaders();
0119       if (debug) {
0120         stringstream str;
0121         str << "  header: " << *reinterpret_cast<const bitset<64>*>(header) << endl;
0122         str << "  header triggerType: " << fedHeader.triggerType() << endl;
0123         str << "  header lvl1ID:      " << fedHeader.lvl1ID() << endl;
0124         str << "  header bxID:        " << fedHeader.bxID() << endl;
0125         str << "  header sourceID:    " << fedHeader.sourceID() << endl;
0126         str << "  header version:     " << fedHeader.version() << endl;
0127         LogTrace("") << str.str();
0128       }
0129     }
0130 
0131     //
0132     // check trailers
0133     //
0134     const Word64* trailer = reinterpret_cast<const Word64*>(rawData.data()) + (nWords - 1);
0135     trailer++;
0136     bool moreTrailers = true;
0137     while (moreTrailers) {
0138       trailer--;
0139       FEDTrailer fedTrailer(reinterpret_cast<const unsigned char*>(trailer));
0140       if (!fedTrailer.check()) {
0141         producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::TrailerCheckFail));
0142         if (debug)
0143           LogTrace("") << " ** PROBLEM **, trailer.check() failed, break";
0144         break;
0145       }
0146       if (fedTrailer.fragmentLength() != nWords) {
0147         producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::InconsistentDataSize));
0148         if (debug)
0149           LogTrace("") << " ** PROBLEM **, fedTrailer.fragmentLength()!= nWords, break";
0150         break;
0151       }
0152       moreTrailers = fedTrailer.moreTrailers();
0153       if (debug) {
0154         ostringstream str;
0155         str << " trailer: " << *reinterpret_cast<const bitset<64>*>(trailer) << endl;
0156         str << "  trailer lenght:    " << fedTrailer.fragmentLength() << endl;
0157         str << "  trailer crc:       " << fedTrailer.crc() << endl;
0158         str << "  trailer evtStatus: " << fedTrailer.evtStatus() << endl;
0159         str << "  trailer ttsBits:   " << fedTrailer.ttsBits() << endl;
0160         LogTrace("") << str.str();
0161       }
0162     }
0163 
0164     //
0165     // data records
0166     //
0167     if (debug) {
0168       ostringstream str;
0169       for (const Word64* word = header + 1; word != trailer; word++) {
0170         str << "    data: " << *reinterpret_cast<const bitset<64>*>(word) << endl;
0171       }
0172       LogTrace("") << str.str();
0173     }
0174     //    if (triggerBX != 51) continue;
0175     //    if (triggerBX != 2316) continue;
0176     EventRecords event(triggerBX);
0177     for (const Word64* word = header + 1; word != trailer; word++) {
0178       for (int iRecord = 1; iRecord <= 4; iRecord++) {
0179         const DataRecord::Data* pRecord = reinterpret_cast<const DataRecord::Data*>(word + 1) - iRecord;
0180         DataRecord record(*pRecord);
0181         event.add(record);
0182         if (debug) {
0183           std::ostringstream str;
0184           str << "record: " << record.print() << " hex: " << hex << *pRecord << dec;
0185           str << " type:" << record.type() << DataRecord::print(record);
0186           if (event.complete()) {
0187             str << " --> dccId: " << fedId << " rmb: " << event.recordSLD().rmb()
0188                 << " lnk: " << event.recordSLD().tbLinkInputNumber() << " lb: " << event.recordCD().lbInLink()
0189                 << " part: " << event.recordCD().partitionNumber() << " data: " << event.recordCD().partitionData()
0190                 << " eod: " << event.recordCD().eod();
0191           }
0192           LogTrace("") << str.str();
0193         }
0194         producedRawDataCounts->addDccRecord(fedId, record);
0195         int statusTMP = 0;
0196         if (event.complete())
0197           statusTMP = interpreter.recordUnpack(
0198               event, producedRPCDigis.get(), producedRawDataCounts.get(), producedRawSynchoCounts.get());
0199         if (statusTMP != 0)
0200           status = statusTMP;
0201       }
0202     }
0203   }
0204   if (status && debug)
0205     LogTrace("") << " RPCUnpackingModule - There was unpacking PROBLEM in this event" << endl;
0206   if (debug)
0207     LogTrace("") << DebugDigisPrintout()(producedRPCDigis.get()) << endl;
0208   ev.put(std::move(producedRPCDigis));
0209   ev.put(std::move(producedRawDataCounts));
0210   if (doSynchro_)
0211     ev.put(std::move(producedRawSynchoCounts));
0212 }