CMS 3D CMS Logo

L1GTEvaluationProducer.cc
Go to the documentation of this file.
1 
9 
12 
15 
20 
23 
25 
26 #include <vector>
27 #include <array>
28 #include <string>
29 #include <unordered_map>
30 #include <fstream>
31 #include <limits>
32 
33 #include <optional>
34 #include <random>
35 
36 using namespace l1t;
37 
39 public:
40  explicit L1GTEvaluationProducer(const edm::ParameterSet &);
41  ~L1GTEvaluationProducer() override = default;
42 
44 
45 private:
46  void produce(edm::Event &, const edm::EventSetup &) override;
47  int nextValue(int mean = 1000, bool sign = false, int max = std::numeric_limits<int>::max());
48 
49  int nextPt() {
50  return std::max<int>(0,
51  nextValue(300, false, (1 << P2GTCandidate::hwPT_t::width) - 1) +
52  std::normal_distribution<double>(0, 500)(randomGenerator_));
53  }
54  int nextEta() {
55  return std::uniform_int_distribution<int>(-(1 << (P2GTCandidate::hwEta_t::width - 1)),
56  (1 << (P2GTCandidate::hwEta_t::width - 1)) - 1)(randomGenerator_);
57  }
58  int nextPhi() {
59  return std::uniform_int_distribution<int>(-(1 << (P2GTCandidate::hwPhi_t::width - 1)),
60  (1 << (P2GTCandidate::hwPhi_t::width - 1)) - 1)(randomGenerator_);
61  }
62 
63  void writeInputPatterns(
64  const std::unordered_map<std::string, std::vector<std::unique_ptr<l1t::L1TGT_BaseInterface>>> &inputObjects);
65 
66  void writeOutputPatterns(
67  const std::unordered_map<std::string, std::vector<std::unique_ptr<l1t::L1TGT_BaseInterface>>> &inputObjects);
68 
69  void endJob() override;
70 
71  std::mt19937 randomGenerator_;
73  std::unordered_map<std::string, std::size_t> numChannels_;
75 };
76 
77 static constexpr std::array<const char *, 29> AVAILABLE_COLLECTIONS{{"GTTPromptJets",
78  "GTTDisplacedJets",
79  "GTTPromptHtSum",
80  "GTTDisplacedHtSum",
81  "GTTEtSum",
82  "GTTHadronicTaus",
83  "CL2JetsSC4",
84  "CL2JetsSC8",
85  "CL2Taus",
86  "CL2HtSum",
87  "CL2EtSum",
88  "GCTNonIsoEg",
89  "GCTIsoEg",
90  "GCTJets",
91  "GCTTaus",
92  "GCTHtSum",
93  "GCTEtSum",
94  "GMTSaPromptMuons",
95  "GMTSaDisplacedMuons",
96  "GMTTkMuons",
97  "GMTTopo",
98  "CL2Electrons",
99  "CL2Photons",
100  "GTTPhiCandidates",
101  "GTTRhoCandidates",
102  "GTTBsCandidates",
103  "GTTPromptTracks",
104  "GTTDisplacedTracks",
105  "GTTPrimaryVert"}};
106 
107 template <typename T1, typename T2>
108 static std::vector<T1> vconvert(std::vector<T2> ivec) {
109  return std::vector<T1>(ivec.begin(), ivec.end());
110 }
111 
113  : randomGenerator_(config.exists("random_seed") ? config.getUntrackedParameter<unsigned int>("random_seed")
114  : std::random_device()()),
115  inputBoardDataWriter_(
116  l1t::demo::parseFileFormat(config.getUntrackedParameter<std::string>("patternFormat")),
117  config.getUntrackedParameter<std::string>("inputFilename"),
118  config.getUntrackedParameter<std::string>("inputFileExtension"),
119  9,
120  1,
121  config.getUntrackedParameter<unsigned int>("maxFrames"),
122  [&]() {
123  const edm::ParameterSet &iChannels = config.getUntrackedParameterSet("InputChannels");
125 
126  channelMap.insert(
127  {{"GCT", 1},
128  {{6, 0}, vconvert<std::size_t>(iChannels.getUntrackedParameter<std::vector<unsigned int>>("GCT_1"))}});
129  channelMap.insert(
130  {{"GMT", 1},
131  {{18, 0},
132  vconvert<std::size_t>(iChannels.getUntrackedParameter<std::vector<unsigned int>>("GMT_1"))}});
133  channelMap.insert(
134  {{"GTT", 1},
135  {{6, 0}, vconvert<std::size_t>(iChannels.getUntrackedParameter<std::vector<unsigned int>>("GTT_1"))}});
136  channelMap.insert(
137  {{"GTT", 2},
138  {{6, 0}, vconvert<std::size_t>(iChannels.getUntrackedParameter<std::vector<unsigned int>>("GTT_2"))}});
139  channelMap.insert(
140  {{"GTT", 3},
141  {{6, 0}, vconvert<std::size_t>(iChannels.getUntrackedParameter<std::vector<unsigned int>>("GTT_3"))}});
142  channelMap.insert(
143  {{"GTT", 4},
144  {{6, 0}, vconvert<std::size_t>(iChannels.getUntrackedParameter<std::vector<unsigned int>>("GTT_4"))}});
145  channelMap.insert(
146  {{"CL2", 1},
147  {{6, 0}, vconvert<std::size_t>(iChannels.getUntrackedParameter<std::vector<unsigned int>>("CL2_1"))}});
148  channelMap.insert(
149  {{"CL2", 2},
150  {{6, 0}, vconvert<std::size_t>(iChannels.getUntrackedParameter<std::vector<unsigned int>>("CL2_2"))}});
151  channelMap.insert(
152  {{"CL2", 3},
153  {{6, 0}, vconvert<std::size_t>(iChannels.getUntrackedParameter<std::vector<unsigned int>>("CL2_3"))}});
154 
155  return channelMap;
156  }()),
157  numChannels_(),
158  outputBoardDataWriter_(l1t::demo::parseFileFormat(config.getUntrackedParameter<std::string>("patternFormat")),
159  config.getUntrackedParameter<std::string>("outputFilename"),
160  config.getUntrackedParameter<std::string>("outputFileExtension"),
161  9,
162  1,
163  config.getUntrackedParameter<unsigned int>("maxFrames"),
164  [&]() {
165  const edm::ParameterSet &oChannels = config.getUntrackedParameterSet("OutputChannels");
166  demo::BoardDataWriter::ChannelMap_t channelMap;
167  for (const char *name : AVAILABLE_COLLECTIONS) {
168  if (oChannels.exists(name)) {
169  std::vector<unsigned int> channels =
170  oChannels.getUntrackedParameter<std::vector<unsigned int>>(name);
171  for (std::size_t i = 0; i < channels.size(); i++) {
172  channelMap.insert({{name, i}, {{1, 0}, {channels.at(i)}}});
173  }
174 
175  numChannels_.insert({name, channels.size()});
176  } else {
177  numChannels_.insert({name, 0});
178  }
179  }
180  return channelMap;
181  }()) {
182  for (const char *name : AVAILABLE_COLLECTIONS) {
183  produces<P2GTCandidateCollection>(name);
184  }
185 }
186 
189  desc.addOptionalUntracked<unsigned int>("random_seed");
190  desc.addUntracked<unsigned int>("maxFrames", 1024);
191  desc.addUntracked<std::string>("inputFilename");
192  desc.addUntracked<std::string>("inputFileExtension", "txt");
193  desc.addUntracked<std::string>("outputFilename");
194  desc.addUntracked<std::string>("outputFileExtension", "txt");
195  desc.addUntracked<std::string>("patternFormat", "EMPv2");
196 
197  edm::ParameterSetDescription inputChannelDesc;
198  inputChannelDesc.addUntracked<std::vector<unsigned int>>("GCT_1");
199  inputChannelDesc.addUntracked<std::vector<unsigned int>>("GMT_1");
200  inputChannelDesc.addUntracked<std::vector<unsigned int>>("GTT_1");
201  inputChannelDesc.addUntracked<std::vector<unsigned int>>("GTT_2");
202  inputChannelDesc.addUntracked<std::vector<unsigned int>>("GTT_3");
203  inputChannelDesc.addUntracked<std::vector<unsigned int>>("GTT_4");
204  inputChannelDesc.addUntracked<std::vector<unsigned int>>("CL2_1");
205  inputChannelDesc.addUntracked<std::vector<unsigned int>>("CL2_2");
206  inputChannelDesc.addUntracked<std::vector<unsigned int>>("CL2_3");
207 
208  desc.addUntracked<edm::ParameterSetDescription>("InputChannels", inputChannelDesc);
209 
210  edm::ParameterSetDescription outputChannelDesc;
211  for (const char *name : AVAILABLE_COLLECTIONS) {
212  outputChannelDesc.addOptionalUntracked<std::vector<unsigned int>>(name);
213  }
214 
215  desc.addUntracked<edm::ParameterSetDescription>("OutputChannels", outputChannelDesc);
216 
217  description.addWithDefaultLabel(desc);
218 }
219 
221  bool positive = sign ? std::bernoulli_distribution(0.5)(randomGenerator_) : true;
222 
223  int result;
224  do {
225  result = std::poisson_distribution<int>(mean)(randomGenerator_);
226  } while (result > max);
227 
228  return positive ? result : -result;
229 }
230 
231 template <typename... Args>
232 static std::vector<ap_uint<64>> vpack(const Args &...vobjects) {
233  std::vector<ap_uint<64>> vpacked;
234 
235  (
236  [&vpacked](const std::vector<std::unique_ptr<l1t::L1TGT_BaseInterface>> &objects) {
237  std::optional<ap_uint<64>> next_packed;
238  for (const auto &object : objects) {
239  if (object->packed_width() == 64) {
240  const l1t::L1TGT_Interface<64> &interface_obj = dynamic_cast<const l1t::L1TGT_Interface<64> &>(*object);
241  vpacked.emplace_back(interface_obj.pack());
242  } else if (object->packed_width() == 96) {
243  const l1t::L1TGT_Interface<96> &interface_obj = dynamic_cast<const l1t::L1TGT_Interface<96> &>(*object);
244  ap_uint<96> packed = interface_obj.pack();
245  if (next_packed.has_value()) {
246  vpacked.emplace_back(packed(95, 64) << 32 | next_packed.value());
247  next_packed.reset();
248  } else {
249  next_packed = packed(95, 64);
250  }
251 
252  vpacked.emplace_back(packed(63, 0));
253 
254  } else if (object->packed_width() == 128) {
255  const l1t::L1TGT_Interface<128> &interface_obj = dynamic_cast<const l1t::L1TGT_Interface<128> &>(*object);
256  ap_uint<128> packed = interface_obj.pack();
257  vpacked.emplace_back(packed(63, 0));
258  vpacked.emplace_back(packed(127, 64));
259  }
260  }
261  }(vobjects),
262  ...);
263 
264  return vpacked;
265 }
266 
268  const std::unordered_map<std::string, std::vector<std::unique_ptr<l1t::L1TGT_BaseInterface>>> &inputObjects) {
270  l1t::demo::EventData{{{{"GTT", 1},
271  vpack(inputObjects.at("GTTPromptJets"),
272  inputObjects.at("GTTDisplacedJets"),
273  inputObjects.at("GTTPromptHtSum"),
274  inputObjects.at("GTTDisplacedHtSum"),
275  inputObjects.at("GTTEtSum"))},
276  {{"GTT", 2}, vpack(inputObjects.at("GTTHadronicTaus"))},
277  {{"CL2", 1},
278  vpack(inputObjects.at("CL2JetsSC4"),
279  inputObjects.at("CL2HtSum"),
280  inputObjects.at("CL2EtSum"),
281  inputObjects.at("CL2JetsSC8"))},
282  {{"CL2", 2}, vpack(inputObjects.at("CL2Taus"))},
283  {{"GCT", 1},
284  vpack(inputObjects.at("GCTNonIsoEg"),
285  inputObjects.at("GCTIsoEg"),
286  inputObjects.at("GCTJets"),
287  inputObjects.at("GCTTaus"),
288  inputObjects.at("GCTEtSum"),
289  inputObjects.at("GCTHtSum"))},
290  {{"GMT", 1},
291  vpack(inputObjects.at("GMTSaPromptMuons"),
292  inputObjects.at("GMTSaDisplacedMuons"),
293  inputObjects.at("GMTTkMuons"),
294  inputObjects.at("GMTTopo"))},
295  {{"CL2", 3}, vpack(inputObjects.at("CL2Electrons"), inputObjects.at("CL2Photons"))},
296  {{"GTT", 3},
297  vpack(inputObjects.at("GTTPhiCandidates"),
298  inputObjects.at("GTTRhoCandidates"),
299  inputObjects.at("GTTBsCandidates"))},
300  {{"GTT", 4},
301  vpack(inputObjects.at("GTTPromptTracks"),
302  inputObjects.at("GTTDisplacedTracks"),
303  inputObjects.at("GTTPrimaryVert"))}}});
304 }
305 
307  const std::unordered_map<std::string, std::vector<std::unique_ptr<l1t::L1TGT_BaseInterface>>> &outputObjects) {
308  std::map<demo::LinkId, std::vector<ap_uint<64>>> eventData;
309 
310  for (const char *name : AVAILABLE_COLLECTIONS) {
311  std::vector<ap_uint<64>> data = vpack(outputObjects.at(name));
312 
313  for (std::size_t i = 0; i < numChannels_.at(name); i++) {
314  for (std::size_t j = i; j < data.size(); j += numChannels_.at(name)) {
315  eventData[{name, i}].push_back(data[j]);
316  }
317 
318  while (eventData[{name, i}].size() < 9) {
319  eventData[{name, i}].push_back(0);
320  }
321  }
322  }
323 
324  outputBoardDataWriter_.addEvent(eventData);
325 }
326 
328  // Generate random input objects
329  std::unordered_map<std::string, std::vector<std::unique_ptr<l1t::L1TGT_BaseInterface>>> inputObjects;
330  for (std::size_t i = 0; i < 12; ++i) {
331  // Global Muon Trigger
332  inputObjects["GMTSaPromptMuons"].emplace_back(std::make_unique<l1t::L1TGT_GMT_PromptDisplacedMuon>(
333  true, nextPt(), nextEta(), nextPhi(), nextValue(), nextValue(), nextValue(), nextValue()));
334 
335  inputObjects["GMTSaDisplacedMuons"].emplace_back(std::make_unique<l1t::L1TGT_GMT_PromptDisplacedMuon>(
336  true, nextPt(), nextEta(), nextPhi(), nextValue(), nextValue(), nextValue(), nextValue()));
337  inputObjects["GMTTkMuons"].emplace_back(std::make_unique<l1t::L1TGT_GMT_TrackMatchedmuon>(true,
338  nextPt(),
339  nextEta(),
340  nextPhi(),
341  nextValue(),
342  nextValue(),
343  nextValue(),
344  nextValue(),
345  nextValue(),
346  nextValue()));
347  inputObjects["GMTTopo"].emplace_back(
348  std::make_unique<l1t::L1TGT_GMT_TopoObject>(true, nextPt(), nextEta(), nextPhi(), nextValue(), nextValue()));
349 
350  // Global Calorimeter Trigger
351  inputObjects["GCTNonIsoEg"].emplace_back(
352  std::make_unique<l1t::L1TGT_GCT_EgammaNonIsolated6p6>(true, nextPt(), nextEta(), nextPhi()));
353  inputObjects["GCTIsoEg"].emplace_back(
354  std::make_unique<l1t::L1TGT_GCT_EgammaIsolated6p6>(true, nextPt(), nextEta(), nextPhi()));
355  inputObjects["GCTJets"].emplace_back(std::make_unique<l1t::L1TGT_GCT_jet6p6>(true, nextPt(), nextEta(), nextPhi()));
356  inputObjects["GCTTaus"].emplace_back(
357  std::make_unique<l1t::L1TGT_GCT_tau6p6>(true, nextPt(), nextEta(), nextPhi(), nextValue()));
358 
359  // Global Track Trigger
360  inputObjects["GTTPromptTracks"].emplace_back(std::make_unique<l1t::L1TGT_GTT_Track>());
361  inputObjects["GTTDisplacedTracks"].emplace_back(std::make_unique<l1t::L1TGT_GTT_Track>());
362  inputObjects["GTTPrimaryVert"].emplace_back(
363  std::make_unique<l1t::L1TGT_GTT_PrimaryVert>(true, nextPt(), nextEta(), nextPhi(), nextValue(), nextValue()));
364  inputObjects["GTTPromptJets"].emplace_back(
365  std::make_unique<l1t::L1TGT_GTT_PromptJet>(true, nextPt(), nextEta(), nextPhi(), nextValue(), nextValue()));
366  inputObjects["GTTDisplacedJets"].emplace_back(std::make_unique<l1t::L1TGT_GTT_DisplacedJet>(
367  true, nextPt(), nextEta(), nextPhi(), nextValue(), nextValue(), nextValue()));
368  inputObjects["GTTHadronicTaus"].emplace_back(std::make_unique<l1t::L1TGT_GTT_HadronicTau>(
369  true, nextPt(), nextEta(), nextPhi(), nextValue(), nextValue(), nextValue(), nextValue()));
370  inputObjects["GTTPhiCandidates"].emplace_back(
371  std::make_unique<l1t::L1TGT_GTT_LightMeson>(true, nextPt(), nextEta(), nextPhi(), nextValue()));
372  inputObjects["GTTRhoCandidates"].emplace_back(
373  std::make_unique<l1t::L1TGT_GTT_LightMeson>(true, nextPt(), nextEta(), nextPhi(), nextValue()));
374  inputObjects["GTTBsCandidates"].emplace_back(
375  std::make_unique<l1t::L1TGT_GTT_LightMeson>(true, nextPt(), nextEta(), nextPhi(), nextValue()));
376 
377  // Correlator Layer-2
378  inputObjects["CL2JetsSC4"].emplace_back(
379  std::make_unique<l1t::L1TGT_CL2_Jet>(true, nextPt(), nextEta(), nextPhi(), nextValue()));
380  inputObjects["CL2JetsSC8"].emplace_back(
381  std::make_unique<l1t::L1TGT_CL2_Jet>(true, nextPt(), nextEta(), nextPhi(), nextValue()));
382  inputObjects["CL2Electrons"].emplace_back(std::make_unique<l1t::L1TGT_CL2_Electron>(
383  true, nextPt(), nextEta(), nextPhi(), nextValue(), nextValue(), nextValue(), nextValue()));
384  inputObjects["CL2Photons"].emplace_back(
385  std::make_unique<l1t::L1TGT_CL2_Photon>(true, nextPt(), nextEta(), nextPhi(), nextValue(), nextValue()));
386  inputObjects["CL2Taus"].emplace_back(std::make_unique<l1t::L1TGT_CL2_Tau>(
387  true, nextPt(), nextEta(), nextPhi(), nextValue(), nextValue(), nextValue(), nextValue()));
388  }
389 
390  inputObjects["CL2HtSum"].emplace_back(
391  std::make_unique<l1t::L1TGT_CL2_Sum>(true, nextValue(), nextValue(), nextValue()));
392  inputObjects["CL2EtSum"].emplace_back(
393  std::make_unique<l1t::L1TGT_CL2_Sum>(true, nextValue(), nextValue(), nextValue()));
394  inputObjects["GCTHtSum"].emplace_back(
395  std::make_unique<l1t::L1TGT_GCT_Sum2>(true, nextValue(), nextValue(), nextValue()));
396  inputObjects["GCTEtSum"].emplace_back(
397  std::make_unique<l1t::L1TGT_GCT_Sum2>(true, nextValue(), nextValue(), nextValue()));
398 
399  inputObjects["GTTPromptHtSum"].emplace_back(
400  std::make_unique<l1t::L1TGT_GTT_Sum>(true, nextValue(), nextValue(), nextValue()));
401  inputObjects["GTTDisplacedHtSum"].emplace_back(
402  std::make_unique<l1t::L1TGT_GTT_Sum>(true, nextValue(), nextValue(), nextValue()));
403  inputObjects["GTTEtSum"].emplace_back(
404  std::make_unique<l1t::L1TGT_GTT_Sum>(true, nextValue(), nextValue(), nextValue()));
405 
406  // Write them to a pattern file
409 
410  for (const auto &[key, inputCollection] : inputObjects) {
411  std::unique_ptr<P2GTCandidateCollection> gtCollection = std::make_unique<P2GTCandidateCollection>();
412  for (const auto &object : inputCollection) {
413  gtCollection->emplace_back(object->to_GTObject());
414  }
415 
416  event.put(std::move(gtCollection), key);
417  }
418 }
419 
423 }
424 
std::unordered_map< std::string, std::size_t > numChannels_
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
L1GTEvaluationProducer(const edm::ParameterSet &)
void addEvent(const EventData &data)
FileFormat parseFileFormat(const std::string &)
Definition: utilities.cc:73
delete x;
Definition: CaloConfig.h:22
Definition: config.py:1
T getUntrackedParameter(std::string const &, T const &) const
static constexpr std::array< const char *, 29 > AVAILABLE_COLLECTIONS
static std::vector< T1 > vconvert(std::vector< T2 > ivec)
key
prepare the HTCondor submission files and eventually submit them
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
l1t::demo::BoardDataWriter inputBoardDataWriter_
void writeOutputPatterns(const std::unordered_map< std::string, std::vector< std::unique_ptr< l1t::L1TGT_BaseInterface >>> &inputObjects)
void produce(edm::Event &, const edm::EventSetup &) override
std::map< LinkId, std::pair< ChannelSpec, std::vector< size_t > > > ChannelMap_t
l1t::demo::BoardDataWriter outputBoardDataWriter_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void writeInputPatterns(const std::unordered_map< std::string, std::vector< std::unique_ptr< l1t::L1TGT_BaseInterface >>> &inputObjects)
virtual ap_uint< N > pack() const =0
int nextValue(int mean=1000, bool sign=false, int max=std::numeric_limits< int >::max())
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
static void fillDescriptions(edm::ConfigurationDescriptions &)
ParameterDescriptionBase * addOptionalUntracked(U const &iLabel, T const &value)
static std::vector< ap_uint< 64 > > vpack(const Args &...vobjects)
Class representing information phase-2 ATCA I/O data corresponding to a single event, with logical channel IDs (essentially string-uint pairs, e.g. tracks-0 to tracks-17).
Definition: EventData.h:28
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1