Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
#include "RPCUnpackingModule.h"
#include "CondFormats/RPCObjects/interface/RPCReadOutMapping.h"
#include "EventFilter/RPCRawToDigi/interface/RPCRecordFormatter.h"
#include "DataFormats/FEDRawData/interface/FEDRawData.h"
#include "DataFormats/FEDRawData/interface/FEDNumbering.h"
#include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
#include "DataFormats/FEDRawData/interface/FEDHeader.h"
#include "DataFormats/FEDRawData/interface/FEDTrailer.h"
#include "DataFormats/RPCDigi/interface/RPCDigiCollection.h"
#include "DataFormats/RPCDigi/interface/RPCRawDataCounts.h"
#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/Framework/interface/Event.h"
//#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/ESTransientHandle.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/ESWatcher.h"

#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "DataFormats/RPCDigi/interface/DataRecord.h"
#include "DataFormats/RPCDigi/interface/ReadoutError.h"
#include "DataFormats/RPCDigi/interface/RPCRawSynchro.h"
#include "EventFilter/RPCRawToDigi/interface/EventRecords.h"
#include "EventFilter/RPCRawToDigi/interface/DebugDigisPrintout.h"

#include <sstream>
#include <bitset>

using namespace edm;
using namespace std;
using namespace rpcrawtodigi;

typedef uint64_t Word64;

RPCUnpackingModule::RPCUnpackingModule(const edm::ParameterSet& pset)
    : dataLabel_(pset.getParameter<edm::InputTag>("InputLabel")),
      doSynchro_(pset.getParameter<bool>("doSynchro")),
      eventCounter_(0),
      theReadoutMappingToken(esConsumes<edm::Transition::BeginRun>()),
      theCabling(nullptr) {
  produces<RPCDigiCollection>();
  produces<RPCRawDataCounts>();
  if (doSynchro_)
    produces<RPCRawSynchro::ProdItem>();
  fedToken_ = consumes<FEDRawDataCollection>(dataLabel_);
}

RPCUnpackingModule::~RPCUnpackingModule() { delete theCabling; }

void RPCUnpackingModule::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  edm::ParameterSetDescription desc;
  desc.add<edm::InputTag>("InputLabel", edm::InputTag("rawDataCollector"));
  desc.add<bool>("doSynchro", true);
  descriptions.add("rpcUnpackingModule", desc);
}

void RPCUnpackingModule::beginRun(const edm::Run& run, const edm::EventSetup& es) {
  if (theRecordWatcher.check(es)) {
    LogTrace("") << "record has CHANGED!!, (re)initialise readout map!";
    delete theCabling;
    ESTransientHandle<RPCEMap> readoutMapping = es.getTransientHandle(theReadoutMappingToken);
    theCabling = readoutMapping->convert();
    theReadoutMappingSearch.init(theCabling);
    LogTrace("") << " READOUT MAP VERSION: " << theCabling->version() << endl;
  }
}

void RPCUnpackingModule::produce(Event& ev, const EventSetup& es) {
  bool debug = edm::MessageDrop::instance()->debugEnabled;
  eventCounter_++;
  if (debug)
    LogDebug("RPCUnpacker::produce") << "Beginning To Unpack Event: " << eventCounter_;

  Handle<FEDRawDataCollection> allFEDRawData;
  ev.getByToken(fedToken_, allFEDRawData);

  auto producedRPCDigis = std::make_unique<RPCDigiCollection>();
  auto producedRawDataCounts = std::make_unique<RPCRawDataCounts>();
  std::unique_ptr<RPCRawSynchro::ProdItem> producedRawSynchoCounts;
  if (doSynchro_)
    producedRawSynchoCounts = std::make_unique<RPCRawSynchro::ProdItem>();

  int status = 0;
  for (int fedId = FEDNumbering::MINRPCFEDID; fedId <= FEDNumbering::MAXRPCFEDID; ++fedId) {
    const FEDRawData& rawData = allFEDRawData->FEDData(fedId);
    RPCRecordFormatter interpreter =
        theCabling ? RPCRecordFormatter(fedId, &theReadoutMappingSearch) : RPCRecordFormatter(fedId, nullptr);
    int triggerBX = 0;
    unsigned int nWords = rawData.size() / sizeof(Word64);
    if (nWords == 0)
      continue;

    //
    // check headers
    //
    const Word64* header = reinterpret_cast<const Word64*>(rawData.data());
    header--;
    bool moreHeaders = true;
    while (moreHeaders) {
      header++;
      FEDHeader fedHeader(reinterpret_cast<const unsigned char*>(header));
      if (!fedHeader.check()) {
        producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::HeaderCheckFail));
        if (debug)
          LogTrace("") << " ** PROBLEM **, header.check() failed, break";
        break;
      }
      if (fedHeader.sourceID() != fedId) {
        producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::InconsitentFedId));
        if (debug)
          LogTrace("") << " ** PROBLEM **, fedHeader.sourceID() != fedId"
                       << "fedId = " << fedId << " sourceID=" << fedHeader.sourceID();
      }
      triggerBX = fedHeader.bxID();
      moreHeaders = fedHeader.moreHeaders();
      if (debug) {
        stringstream str;
        str << "  header: " << *reinterpret_cast<const bitset<64>*>(header) << endl;
        str << "  header triggerType: " << fedHeader.triggerType() << endl;
        str << "  header lvl1ID:      " << fedHeader.lvl1ID() << endl;
        str << "  header bxID:        " << fedHeader.bxID() << endl;
        str << "  header sourceID:    " << fedHeader.sourceID() << endl;
        str << "  header version:     " << fedHeader.version() << endl;
        LogTrace("") << str.str();
      }
    }

    //
    // check trailers
    //
    const Word64* trailer = reinterpret_cast<const Word64*>(rawData.data()) + (nWords - 1);
    trailer++;
    bool moreTrailers = true;
    while (moreTrailers) {
      trailer--;
      FEDTrailer fedTrailer(reinterpret_cast<const unsigned char*>(trailer));
      if (!fedTrailer.check()) {
        producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::TrailerCheckFail));
        if (debug)
          LogTrace("") << " ** PROBLEM **, trailer.check() failed, break";
        break;
      }
      if (fedTrailer.fragmentLength() != nWords) {
        producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::InconsistentDataSize));
        if (debug)
          LogTrace("") << " ** PROBLEM **, fedTrailer.fragmentLength()!= nWords, break";
        break;
      }
      moreTrailers = fedTrailer.moreTrailers();
      if (debug) {
        ostringstream str;
        str << " trailer: " << *reinterpret_cast<const bitset<64>*>(trailer) << endl;
        str << "  trailer lenght:    " << fedTrailer.fragmentLength() << endl;
        str << "  trailer crc:       " << fedTrailer.crc() << endl;
        str << "  trailer evtStatus: " << fedTrailer.evtStatus() << endl;
        str << "  trailer ttsBits:   " << fedTrailer.ttsBits() << endl;
        LogTrace("") << str.str();
      }
    }

    //
    // data records
    //
    if (debug) {
      ostringstream str;
      for (const Word64* word = header + 1; word != trailer; word++) {
        str << "    data: " << *reinterpret_cast<const bitset<64>*>(word) << endl;
      }
      LogTrace("") << str.str();
    }
    //    if (triggerBX != 51) continue;
    //    if (triggerBX != 2316) continue;
    EventRecords event(triggerBX);
    for (const Word64* word = header + 1; word != trailer; word++) {
      for (int iRecord = 1; iRecord <= 4; iRecord++) {
        const DataRecord::Data* pRecord = reinterpret_cast<const DataRecord::Data*>(word + 1) - iRecord;
        DataRecord record(*pRecord);
        event.add(record);
        if (debug) {
          std::ostringstream str;
          str << "record: " << record.print() << " hex: " << hex << *pRecord << dec;
          str << " type:" << record.type() << DataRecord::print(record);
          if (event.complete()) {
            str << " --> dccId: " << fedId << " rmb: " << event.recordSLD().rmb()
                << " lnk: " << event.recordSLD().tbLinkInputNumber() << " lb: " << event.recordCD().lbInLink()
                << " part: " << event.recordCD().partitionNumber() << " data: " << event.recordCD().partitionData()
                << " eod: " << event.recordCD().eod();
          }
          LogTrace("") << str.str();
        }
        producedRawDataCounts->addDccRecord(fedId, record);
        int statusTMP = 0;
        if (event.complete())
          statusTMP = interpreter.recordUnpack(
              event, producedRPCDigis.get(), producedRawDataCounts.get(), producedRawSynchoCounts.get());
        if (statusTMP != 0)
          status = statusTMP;
      }
    }
  }
  if (status && debug)
    LogTrace("") << " RPCUnpackingModule - There was unpacking PROBLEM in this event" << endl;
  if (debug)
    LogTrace("") << DebugDigisPrintout()(producedRPCDigis.get()) << endl;
  ev.put(std::move(producedRPCDigis));
  ev.put(std::move(producedRawDataCounts));
  if (doSynchro_)
    ev.put(std::move(producedRawSynchoCounts));
}