CMS 3D CMS Logo

HGCalSlinkEmulator.cc
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * This is a part of HGCAL offline software.
4  * Authors:
5  * Pedro Silva, CERN
6  * Laurent Forthomme, CERN
7  *
8  ****************************************************************************/
9 
10 #include <memory>
11 
22 
26 
30 
32 public:
33  explicit HGCalSlinkEmulator(const edm::ParameterSet&);
34 
36 
37 private:
38  void produce(edm::Event&, const edm::EventSetup&) override;
39 
40  const unsigned int fed_id_;
41 
42  const bool store_emul_info_;
44 
46  std::unique_ptr<hgcal::econd::Emulator> emulator_;
47 
51 };
52 
54  : fed_id_(iConfig.getParameter<unsigned int>("fedId")),
55  store_emul_info_(iConfig.getParameter<bool>("storeEmulatorInfo")),
56  store_fed_header_trailer_(iConfig.getParameter<bool>("fedHeaderTrailer")),
57  fedRawToken_(produces<FEDRawDataCollection>()),
58  frame_gen_(iConfig) {
59  // figure out which emulator is to be used
60  const auto& emul_type = iConfig.getParameter<std::string>("emulatorType");
61  if (frame_gen_.econdParams().empty())
62  throw cms::Exception("HGCalSlinkEmulator")
63  << "No ECON-D parameters were retrieved from the configuration. Please add at least one.";
64  const auto& econd_params = frame_gen_.econdParams().begin()->second;
65  if (emul_type == "trivial")
66  emulator_ = std::make_unique<hgcal::econd::TrivialEmulator>(econd_params);
67  else if (emul_type == "hgcmodule")
68  emulator_ = std::make_unique<hgcal::econd::HGCalModuleTreeReader>(
69  econd_params,
70  iConfig.getUntrackedParameter<std::string>("treeName"),
71  iConfig.getUntrackedParameter<std::vector<std::string>>("inputs"));
72  else
73  throw cms::Exception("HGCalSlinkEmulator") << "Invalid emulator type chosen: '" << emul_type << "'.";
74 
76 
77  // ensure the random number generator service is present in configuration
78  if (!rng_.isAvailable())
79  throw cms::Exception("HGCalSlinkEmulator") << "The HGCalSlinkEmulator module requires the "
80  "RandomNumberGeneratorService,\n"
81  "which appears to be absent. Please add that service to your "
82  "configuration\n"
83  "or remove the modules that require it.";
84 
85  if (store_emul_info_)
86  fedEmulInfoToken_ = produces<HGCalSlinkEmulatorInfo>();
87 }
88 
91 
92  // build the S-link payload
93  auto slink_event = frame_gen_.produceSlinkEvent(fed_id_);
94  const auto slink_event_size = slink_event.size() * sizeof(slink_event.at(0));
95 
96  // compute the total S-link payload size
97  size_t total_event_size = slink_event_size;
99  total_event_size += FEDHeader::length + FEDTrailer::length;
100 
101  // fill the output FED raw data collection
102  FEDRawDataCollection raw_data;
103  auto& fed_data = raw_data.FEDData(fed_id_);
104  fed_data.resize(total_event_size);
105  auto* ptr = fed_data.data();
106 
108  const auto& last_event = frame_gen_.lastECONDEmulatedInput();
109  const auto event_id = std::get<0>(last_event.first), bx_id = std::get<1>(last_event.first);
110  int trg_type = 0;
111  // compose 2*32-bit FED header word
112  FEDHeader::set(ptr, trg_type, event_id, bx_id, fed_id_);
113  LogDebug("HGCalSlinkEmulator").log([&](auto& log) {
114  const FEDHeader hdr(ptr);
115  log << "FED header: lvl1ID=" << hdr.lvl1ID() << ", bxID=" << hdr.bxID() << ", source ID=" << hdr.sourceID()
116  << ".";
117  });
118  ptr += FEDHeader::length;
119  }
120 
121  // insert ECON-D payload
122  std::memcpy(ptr, slink_event.data(), slink_event_size);
123  ptr += slink_event_size;
124  LogDebug("HGCalSlinkEmulator") << "Wrote " << slink_event.size() << " 64-bit words = " << slink_event_size
125  << " 8-bit words.";
126 
128  // compose 2*32-bit FED trailer word
129  FEDTrailer::set(ptr,
130  slink_event.size() + 2,
131  evf::compute_crc(reinterpret_cast<uint8_t*>(slink_event.data()), slink_event_size),
132  0,
133  0);
134  LogDebug("HGCalSlinkEmulator").log([&](auto& log) {
135  const FEDTrailer trl(ptr);
136  log << "FED trailer: fragment length: " << trl.fragmentLength() << ", CRC=0x" << std::hex << trl.crc() << std::dec
137  << ", status: " << trl.evtStatus() << ".";
138  });
139  ptr += FEDTrailer::length;
140  }
141 
142  iEvent.emplace(fedRawToken_, std::move(raw_data));
143 
144  // store the emulation information if requested
145  if (store_emul_info_)
147 }
148 
149 //
152  desc.ifValue(
153  edm::ParameterDescription<std::string>("emulatorType", "trivial", true),
154  // trivial emulator
155  "trivial" >> edm::EmptyGroupDescription() or
156  // test beam tree content
157  "hgcmodule" >> (edm::ParameterDescription<std::string>("treeName", "hgcroc_rawdata/eventdata", false) and
158  edm::ParameterDescription<std::vector<std::string>>("inputs", {}, false)))
159  ->setComment("emulator mode (trivial, or hgcmodule)");
160  desc.add<unsigned int>("fedId", 0)->setComment("FED number delivering the emulated frames");
161  desc.add<bool>("fedHeaderTrailer", false)->setComment("also add FED header/trailer info");
162  desc.add<bool>("storeEmulatorInfo", false)
163  ->setComment("also append a 'truth' auxiliary info to the output event content");
164  descriptions.add("hgcalEmulatedSlinkRawData", desc);
165 }
166 
167 // define this as a plug-in
const bool store_fed_header_trailer_
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
const std::map< unsigned int, econd::EmulatorParameters > & econdParams() const
List of ECON-D operational parameters for emulation.
HGCalSlinkEmulator(const edm::ParameterSet &)
void setEmulator(econd::Emulator &)
Set the emulation source for ECON-D frames.
static const uint32_t length
Definition: FEDTrailer.h:57
const econd::ECONDInput & lastECONDEmulatedInput() const
Retrieve the last ECON-D event emulated.
static const uint32_t length
Definition: FEDHeader.h:54
uint16_t sourceID() const
Identifier of the FED.
Definition: FEDHeader.cc:19
void produce(edm::Event &, const edm::EventSetup &) override
const edm::EDPutTokenT< FEDRawDataCollection > fedRawToken_
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
std::vector< uint64_t > produceSlinkEvent(unsigned int fed_id) const
Produce a S-link event from an emulated event.
uint32_t fragmentLength() const
The length of the event fragment counted in 64-bit words including header and trailer.
Definition: FEDTrailer.cc:13
T getUntrackedParameter(std::string const &, T const &) const
int iEvent
Definition: GenABIO.cc:224
static void set(unsigned char *trailer, uint32_t lenght, uint16_t crc, uint8_t evt_stat, uint8_t tts, bool moreTrailers=false)
Set all fields in the trailer.
Definition: FEDTrailer.cc:31
void resize(size_t newsize)
Definition: FEDRawData.cc:28
std::unique_ptr< hgcal::econd::Emulator > emulator_
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
unsigned short compute_crc(unsigned char *buffer, unsigned int bufSize)
Definition: CRC16.h:46
A S-link/ECON-D payload generator helper.
uint16_t crc() const
Cyclic Redundancy Code of the event fragment including header and trailer.
Definition: FEDTrailer.cc:15
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
uint8_t evtStatus() const
Event fragment status information.
Definition: FEDTrailer.cc:17
const HGCalSlinkEmulatorInfo & lastSlinkEmulatedInfo() const
Retrieve the metadata generated along with the last S-link emulated payload.
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
uint16_t bxID() const
The bunch crossing number.
Definition: FEDHeader.cc:17
static edm::ParameterSetDescription description()
edm::EDPutTokenT< HGCalSlinkEmulatorInfo > fedEmulInfoToken_
hgcal::HGCalFrameGenerator frame_gen_
static void fillDescriptions(edm::ConfigurationDescriptions &)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void setRandomEngine(CLHEP::HepRandomEngine &rng)
Set the random number generator engine.
edm::Service< edm::RandomNumberGenerator > rng_
bool isAvailable() const
Definition: Service.h:40
uint32_t lvl1ID() const
Level-1 event number generated by the TTC system.
Definition: FEDHeader.cc:15
static void set(unsigned char *header, uint8_t triggerType, uint32_t lvl1ID, uint16_t bxID, uint16_t sourceID, uint8_t version=0, bool moreHeaders=false)
Set all fields in the header.
Definition: FEDHeader.cc:25
const unsigned int fed_id_
def move(src, dest)
Definition: eostools.py:511
#define LogDebug(id)