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
#include "EventFilter/RPCRawToDigi/interface/RPCPackingModule.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"

#include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
#include "DataFormats/FEDRawData/interface/FEDHeader.h"
#include "DataFormats/FEDRawData/interface/FEDTrailer.h"

#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/Framework/interface/ESHandle.h"

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

#include "CondFormats/RPCObjects/interface/RPCReadOutMapping.h"

#include "EventFilter/RPCRawToDigi/interface/RPCRecordFormatter.h"
#include "EventFilter/RPCRawToDigi/interface/DebugDigisPrintout.h"
#include "DataFormats/RPCDigi/interface/EmptyWord.h"

#include <string>
#include <sstream>

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

typedef uint64_t Word64;

RPCPackingModule::RPCPackingModule(const ParameterSet& pset) {
  dataLabel_ = consumes<RPCDigiCollection>(pset.getParameter<edm::InputTag>("InputLabel"));
  readoutMappingToken_ = esConsumes<RPCEMap, RPCEMapRcd>();
  theCabling = new RPCReadOutMapping("");
  produces<FEDRawDataCollection>();
}

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

void RPCPackingModule::produce(edm::Event& ev, const edm::EventSetup& es) {
  Handle<RPCDigiCollection> digiCollection;
  ev.getByToken(dataLabel_, digiCollection);
  LogDebug("") << DebugDigisPrintout()(digiCollection.product());

  if (recordWatcher_.check(es)) {
    delete theCabling;
    LogTrace("") << "record has CHANGED!!, initialise readout map!";
    ESHandle<RPCEMap> readoutMapping = es.getHandle(readoutMappingToken_);
    theCabling = readoutMapping->convert();
    LogTrace("") << " READOUT MAP VERSION: " << theCabling->version() << endl;
  }

  auto buffers = std::make_unique<FEDRawDataCollection>();

  //  pair<int,int> rpcFEDS=FEDNumbering::getRPCFEDIds();
  pair<int, int> rpcFEDS(790, 792);
  for (int id = rpcFEDS.first; id <= rpcFEDS.second; ++id) {
    RPCRecordFormatter formatter(id, theCabling);
    unsigned int lvl1_ID = ev.id().event();
    FEDRawData* rawData = RPCPackingModule::rawData(id, lvl1_ID, digiCollection.product(), formatter);
    FEDRawData& fedRawData = buffers->FEDData(id);

    fedRawData = *rawData;
    delete rawData;
  }
  ev.put(std::move(buffers));
}

FEDRawData* RPCPackingModule::rawData(int fedId,
                                      unsigned int lvl1_ID,
                                      const RPCDigiCollection* digis,
                                      const RPCRecordFormatter& formatter) const {
  //
  // get merged records
  //
  int trigger_BX = 200;  // FIXME - set event by event but correct bx assigment in digi
  vector<EventRecords> merged = RPCPackingModule::eventRecords(fedId, trigger_BX, digis, formatter);

  //
  // create data words
  //
  vector<Word64> dataWords;
  EmptyWord empty;
  typedef vector<EventRecords>::const_iterator IR;
  for (IR ir = merged.begin(), irEnd = merged.end(); ir != irEnd; ++ir) {
    Word64 w = (((Word64(ir->recordBX().data()) << 16) | ir->recordSLD().data()) << 16 | ir->recordCD().data()) << 16 |
               empty.data();
    dataWords.push_back(w);
  }

  //
  // create raw data
  //
  int nHeaders = 1;
  int nTrailers = 1;
  int dataSize = (nHeaders + nTrailers + dataWords.size()) * sizeof(Word64);
  FEDRawData* raw = new FEDRawData(dataSize);

  //
  // add header
  //
  unsigned char* pHeader = raw->data();
  int evt_ty = 3;
  int source_ID = fedId;
  FEDHeader::set(pHeader, evt_ty, lvl1_ID, trigger_BX, source_ID);

  //
  // add datawords
  //
  for (unsigned int idata = 0; idata < dataWords.size(); idata++) {
    Word64* word = reinterpret_cast<Word64*>(pHeader + (idata + 1) * sizeof(Word64));
    *word = dataWords[idata];
  }

  //
  // add trailer
  //
  unsigned char* pTrailer = pHeader + raw->size() - sizeof(Word64);
  int crc = 0;
  int evt_stat = 15;
  int tts = 0;
  int datasize = raw->size() / sizeof(Word64);
  FEDTrailer::set(pTrailer, datasize, crc, evt_stat, tts);

  return raw;
}

vector<EventRecords> RPCPackingModule::eventRecords(int fedId,
                                                    int trigger_BX,
                                                    const RPCDigiCollection* digis,
                                                    const RPCRecordFormatter& formatter) {
  typedef DigiContainerIterator<RPCDetId, RPCDigi> DigiRangeIterator;
  vector<EventRecords> dataRecords;

  LogDebug("RPCRawDataPacker") << "Packing Fed id=" << fedId;
  for (DigiRangeIterator it = digis->begin(); it != digis->end(); it++) {
    RPCDetId rpcDetId = (*it).first;
    uint32_t rawDetId = rpcDetId.rawId();
    RPCDigiCollection::Range range = digis->get(rpcDetId);
    for (vector<RPCDigi>::const_iterator id = range.first; id != range.second; id++) {
      const RPCDigi& digi = (*id);
      vector<EventRecords> rawFromDigi = formatter.recordPack(rawDetId, digi, trigger_BX);
      dataRecords.insert(dataRecords.end(), rawFromDigi.begin(), rawFromDigi.end());
    }
  }

  //
  // merge data words
  //
  LogTrace("RPCRawDataPacker") << " size of   data: " << dataRecords.size();
  vector<EventRecords> merged = EventRecords::mergeRecords(dataRecords);
  LogTrace("") << " size of megred: " << merged.size();

  return merged;
}