CMS 3D CMS Logo

L1CTJetFileWriter.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <numeric>
3 
4 // user include files
7 
10 
13 
15 
21 
22 //
23 // class declaration
24 //
25 
26 class L1CTJetFileWriter : public edm::one::EDAnalyzer<edm::one::SharedResources> {
27 public:
28  explicit L1CTJetFileWriter(const edm::ParameterSet&);
29 
30  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
31 
32 private:
33  // ----------constants, enums and typedefs ---------
34  std::vector<edm::ParameterSet> collections_;
35 
36  size_t nFramesPerBX_;
40  std::map<l1t::demo::LinkId, std::pair<l1t::demo::ChannelSpec, std::vector<size_t>>> channelSpecsOutputToGT_;
41 
42  // ----------member functions ----------------------
43  void analyze(const edm::Event&, const edm::EventSetup&) override;
44  void endJob() override;
45  std::vector<ap_uint<64>> encodeJets(const std::vector<l1t::PFJet> jets, unsigned nJets);
46  std::vector<ap_uint<64>> encodeSums(const std::vector<l1t::EtSum> sums, unsigned nSums);
47 
49  std::vector<std::pair<edm::EDGetTokenT<edm::View<l1t::PFJet>>, edm::EDGetTokenT<edm::View<l1t::EtSum>>>> tokens_;
50  std::vector<std::pair<bool, bool>> tokensToWrite_;
51  std::vector<unsigned> nJets_;
52  std::vector<unsigned> nSums_;
53 };
54 
56  : collections_(iConfig.getParameter<std::vector<edm::ParameterSet>>("collections")),
57  nFramesPerBX_(iConfig.getParameter<unsigned>("nFramesPerBX")),
58  ctl2BoardTMUX_(iConfig.getParameter<unsigned>("TMUX")),
59  gapLengthOutput_(iConfig.getParameter<unsigned>("gapLengthOutput")),
60  maxLinesPerFile_(iConfig.getParameter<unsigned>("maxLinesPerFile")),
61  channelSpecsOutputToGT_{{{"jets", 0}, {{ctl2BoardTMUX_, gapLengthOutput_}, {0}}}},
62  fileWriterOutputToGT_(l1t::demo::parseFileFormat(iConfig.getParameter<std::string>("format")),
63  iConfig.getParameter<std::string>("outputFilename"),
64  iConfig.getParameter<std::string>("outputFileExtension"),
65  nFramesPerBX_,
66  ctl2BoardTMUX_,
67  maxLinesPerFile_,
68  channelSpecsOutputToGT_) {
69  for (const auto& pset : collections_) {
72  unsigned nJets = pset.getParameter<unsigned>("nJets");
73  unsigned nSums = pset.getParameter<unsigned>("nSums");
74  nJets_.push_back(nJets);
75  nSums_.push_back(nSums);
76  bool writeJetToken(false), writeMhtToken(false);
77  if (nJets > 0) {
78  jetToken = consumes<edm::View<l1t::PFJet>>(pset.getParameter<edm::InputTag>("jets"));
79  writeJetToken = true;
80  }
81  if (nSums > 0) {
82  mhtToken = consumes<edm::View<l1t::EtSum>>(pset.getParameter<edm::InputTag>("mht"));
83  writeMhtToken = true;
84  }
85  tokens_.emplace_back(jetToken, mhtToken);
86  tokensToWrite_.emplace_back(writeJetToken, writeMhtToken);
87  }
88 }
89 
91  using namespace edm;
92 
93  // 1) Pack collections in the order they're specified. jets then sums within collection
94  std::vector<ap_uint<64>> link_words;
95  for (unsigned iCollection = 0; iCollection < collections_.size(); iCollection++) {
96  if (tokensToWrite_.at(iCollection).first) {
97  const auto& jetToken = tokens_.at(iCollection).first;
98  // 2) Encode jet information onto vectors containing link data
100  std::vector<l1t::PFJet> sortedJets;
101  sortedJets.reserve(jets.size());
102  std::copy(jets.begin(), jets.end(), std::back_inserter(sortedJets));
103 
104  std::stable_sort(
105  sortedJets.begin(), sortedJets.end(), [](l1t::PFJet i, l1t::PFJet j) { return (i.hwPt() > j.hwPt()); });
106  const auto outputJets(encodeJets(sortedJets, nJets_.at(iCollection)));
107  link_words.insert(link_words.end(), outputJets.begin(), outputJets.end());
108  }
109 
110  if (tokensToWrite_.at(iCollection).second) {
111  // 3) Encode sums onto vectors containing link data
112  const auto& mhtToken = tokens_.at(iCollection).second;
113  const edm::View<l1t::EtSum>& mht = iEvent.get(mhtToken);
114  std::vector<l1t::EtSum> orderedSums;
115  std::copy(mht.begin(), mht.end(), std::back_inserter(orderedSums));
116  const auto outputSums(encodeSums(orderedSums, nSums_.at(iCollection)));
117  link_words.insert(link_words.end(), outputSums.begin(), outputSums.end());
118  }
119  }
120  // 4) Pack jet information into 'event data' object, and pass that to file writer
121  l1t::demo::EventData eventDataJets;
122  eventDataJets.add({"jets", 0}, link_words);
123  fileWriterOutputToGT_.addEvent(eventDataJets);
124 }
125 
126 // ------------ method called once each job just after ending the event loop ------------
128  // Writing pending events to file before exiting
130 }
131 
132 std::vector<ap_uint<64>> L1CTJetFileWriter::encodeJets(const std::vector<l1t::PFJet> jets, const unsigned nJets) {
133  // Encode up to nJets jets, padded with 0s
134  std::vector<ap_uint<64>> jet_words(2 * nJets, 0); // allocate 2 words per jet
135  for (unsigned i = 0; i < std::min(nJets, (uint)jets.size()); i++) {
136  const l1t::PFJet& j = jets.at(i);
137  jet_words[2 * i] = j.encodedJet()[0];
138  jet_words[2 * i + 1] = j.encodedJet()[1];
139  }
140  return jet_words;
141 }
142 
143 std::vector<ap_uint<64>> L1CTJetFileWriter::encodeSums(const std::vector<l1t::EtSum> sums, unsigned nSums) {
144  // Need two l1t::EtSum for each GT Sum
145  std::vector<ap_uint<64>> sum_words;
146  for (unsigned i = 0; i < nSums; i++) {
147  if (2 * i < sums.size()) {
148  l1gt::Sum gtSum;
149  gtSum.valid = 1; // if the sums are sent at all, they are valid
150  gtSum.vector_pt.V = sums.at(2 * i + 1).hwPt();
151  gtSum.vector_phi.V = sums.at(2 * i + 1).hwPhi();
152  gtSum.scalar_pt.V = sums.at(2 * i).hwPt();
153  sum_words.push_back(gtSum.pack_ap());
154  } else {
155  sum_words.push_back(0);
156  }
157  }
158  return sum_words;
159 }
160 
161 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
164  {
166  vpsd1.addOptional<edm::InputTag>("jets");
167  vpsd1.addOptional<edm::InputTag>("mht");
168  vpsd1.add<uint>("nJets", 0);
169  vpsd1.add<uint>("nSums", 0);
170  desc.addVPSet("collections", vpsd1);
171  }
172  desc.add<std::string>("outputFilename");
173  desc.add<std::string>("outputFileExtension", "txt");
174  desc.add<uint32_t>("nJets", 12);
175  desc.add<uint32_t>("nFramesPerBX", 9);
176  desc.add<uint32_t>("gapLengthOutput", 4);
177  desc.add<uint32_t>("TMUX", 6);
178  desc.add<uint32_t>("maxLinesPerFile", 1024);
179  desc.add<std::string>("format", "EMPv2");
180  descriptions.addDefault(desc);
181 }
182 
183 //define this as a plug-in
std::map< l1t::demo::LinkId, std::pair< l1t::demo::ChannelSpec, std::vector< size_t > > > channelSpecsOutputToGT_
void endJob() override
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
std::vector< std::pair< bool, bool > > tokensToWrite_
std::vector< std::pair< edm::EDGetTokenT< edm::View< l1t::PFJet > >, edm::EDGetTokenT< edm::View< l1t::EtSum > > > > tokens_
void addEvent(const EventData &data)
FileFormat parseFileFormat(const std::string &)
Definition: utilities.cc:73
void add(const LinkId &, const std::vector< ap_uint< 64 >> &)
Definition: EventData.cc:14
static constexpr int nJets
std::vector< ap_uint< 64 > > encodeSums(const std::vector< l1t::EtSum > sums, unsigned nSums)
L1CTJetFileWriter(const edm::ParameterSet &)
int iEvent
Definition: GenABIO.cc:224
void addDefault(ParameterSetDescription const &psetDescription)
l1t::demo::BoardDataWriter fileWriterOutputToGT_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ap_uint< BITWIDTH > pack_ap() const
Definition: gt_datatypes.h:163
ParameterDescriptionBase * add(U const &iLabel, T const &value)
std::vector< ap_uint< 64 > > encodeJets(const std::vector< l1t::PFJet > jets, unsigned nJets)
pt_t vector_pt
Definition: gt_datatypes.h:146
void analyze(const edm::Event &, const edm::EventSetup &) override
std::vector< unsigned > nSums_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< unsigned > nJets_
phi_t vector_phi
Definition: gt_datatypes.h:147
std::vector< edm::EDGetTokenT< int > > tokens_
std::vector< edm::ParameterSet > collections_
HLT enums.
pt_t scalar_pt
Definition: gt_datatypes.h:148
valid_t valid
Definition: gt_datatypes.h:145
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