CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
L1TS2PFJetInputPatternWriter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: L1Trigger/L1CaloTrigger
4 // Class: L1TS2PFJetInputPatternWriter
5 //
13 //
14 // Original Author: Aaron Bundock
15 // Created: Fri, 26 Jul 2018 14:20:25 GMT
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
32 
33 #include <fstream>
34 #include <iostream>
35 #include <iomanip>
36 #include <cmath>
37 
38 //
39 // class declaration
40 //
41 
42 constexpr unsigned int bit_shift_phi = 16;
43 constexpr unsigned int bit_shift_eta = 26;
44 constexpr unsigned int mod_13_1 = 1;
45 constexpr unsigned int mod_13_2 = 2;
46 constexpr unsigned int n_latency_clocks = 13;
47 constexpr unsigned int n_first_empty_frames = 14;
48 constexpr unsigned int framesPerFile = 1015;
49 constexpr float eta_first_region_boundary = 0.75;
50 constexpr float eta_second_region_boundary = 1.5;
51 constexpr float phi_region_boundary = 0.7;
52 constexpr unsigned int hash_bitmask_16 = 0xffff; // (2^16)-1
53 constexpr unsigned int hash_bitmask_10 = 0x3ff; // (2^10)-1
54 constexpr float ptLSB = 0.25;
55 constexpr float etaLSB = 0.0043633231;
56 constexpr float phiLSB = 0.0043633231;
57 
59 public:
62 
63  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
64 
65 private:
66  void beginJob() override;
67  void analyze(const edm::Event&, const edm::EventSetup&) override;
68  void endJob() override;
69 
70  // ----------member data ---------------------------
74 
75  // constants
76  unsigned nChan_; // number of channels per quad
77  unsigned nQuad_;
78  unsigned nLink_;
79  unsigned nHeaderFrames_;
80  unsigned nPayloadFrames_;
81  unsigned nClearFrames_;
82  unsigned nFrame_;
83  unsigned nFrameFile_;
84  unsigned nEvents_;
85 
86  // data arranged by link and frame
87  std::vector<std::vector<uint64_t>> data_;
88 
89  // data valid flags (just one per frame for now)
90  std::vector<int> dataValid_;
91 
92  // map of towers onto links/frames
93  std::map<int, int> map_;
94 };
95 
96 //
97 // constants, enums and typedefs
98 //
99 
100 //
101 // static data member definitions
102 //
103 
104 //
105 // constructors and destructor
106 //
108  : pfToken_(consumes<std::vector<l1t::PFCandidate>>(iConfig.getParameter<edm::InputTag>("pfTag"))),
109  filename_(iConfig.getUntrackedParameter<std::string>("filename")),
110  outDir_(iConfig.getUntrackedParameter<std::string>("outDir")),
111  nChan_(iConfig.getUntrackedParameter<unsigned>("nChanPerQuad")),
112  nQuad_(iConfig.getUntrackedParameter<unsigned>("nQuads")),
113  nHeaderFrames_(iConfig.getUntrackedParameter<unsigned>("nHeaderFrames")),
114  nPayloadFrames_(iConfig.getUntrackedParameter<unsigned>("nPayloadFrames")),
115  nClearFrames_(iConfig.getUntrackedParameter<unsigned>("nClearFrames")) {
116  //now do what ever initialization is needed
117 
118  // register what you consume and keep token for later access:
119 
120  nFrame_ = 0;
121  nFrameFile_ = 0;
122  nEvents_ = 0;
123 
124  nLink_ = nChan_ * nQuad_;
125  data_.resize(nLink_);
126  LogDebug("L1TDebug") << "Preparing for " << nLink_ << " links" << std::endl;
127 }
128 
130  // do anything here that needs to be done at desctruction time
131  // (e.g. close files, deallocate resources etc.)
132 }
133 
134 //
135 // member functions
136 //
137 
138 // ------------ method called for each event ------------
140  using namespace edm;
141 
142  //count events
143  nEvents_++;
144 
146  iEvent.getByToken(pfToken_, pfHandle);
147  std::vector<l1t::PFCandidate> const& pfCands = *pfHandle;
148 
149  std::vector<l1t::PFCandidate> pfPartsA;
150  std::vector<l1t::PFCandidate> pfPartsB;
151 
152  for (auto const& pfc : pfCands) {
153  // select first two "small" regions for current fw
154  if (pfc.eta() >= 0 && pfc.eta() < eta_first_region_boundary && pfc.phi() >= 0 && pfc.phi() < phi_region_boundary)
155  pfPartsA.push_back(pfc);
156  if (pfc.eta() >= eta_first_region_boundary && pfc.eta() < eta_second_region_boundary && pfc.phi() >= 0 &&
157  pfc.phi() < phi_region_boundary)
158  pfPartsB.push_back(pfc);
159  }
160 
161  if (pfPartsA.empty() && pfPartsB.empty())
162  return;
163 
164  if (nFrame_ == 0 || nFrameFile_ == 0) {
165  //first empty frames
167  dataValid_.push_back(1);
168  for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
169  for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
170  uint iLink = (iQuad * nChan_) + iChan;
171  data_.at(iLink).push_back(0);
172  }
173  }
174  nFrame_++;
175  nFrameFile_++;
176  }
177  }
178 
179  // loop over frames
180  for (unsigned iFrame = 0; iFrame < nPayloadFrames_; ++iFrame) {
181  dataValid_.push_back(1);
182  // loop over links
183  for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
184  for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
185  // get tower ieta, iphi for link
186  uint iLink = (iQuad * nChan_) + iChan;
187 
188  uint64_t data = 0;
189 
191  if (iLink < 24 && pfPartsA.size() > iLink) {
192  data |= ((uint64_t)floor(pfPartsA.at(iLink).pt() / ptLSB) & hash_bitmask_16);
193  data |= ((uint64_t)floor(pfPartsA.at(iLink).phi() / phiLSB) & hash_bitmask_10) << bit_shift_phi;
194  data |= ((uint64_t)floor(pfPartsA.at(iLink).eta() / etaLSB) & hash_bitmask_10) << bit_shift_eta;
195  }
196  }
197  if ((nFrameFile_ % n_latency_clocks) == mod_13_2) {
198  if (iLink < 24 && pfPartsB.size() > iLink) {
199  data |= ((uint64_t)floor(pfPartsB.at(iLink).pt() / ptLSB) & hash_bitmask_16);
200  data |= ((uint64_t)floor(pfPartsB.at(iLink).phi() / phiLSB) & hash_bitmask_10) << bit_shift_phi;
201  data |= ((uint64_t)floor((pfPartsB.at(iLink).eta() - eta_first_region_boundary) / etaLSB) & hash_bitmask_10)
202  << bit_shift_eta;
203  }
204  }
205  // add data to output
206  data_.at(iLink).push_back(data);
207  }
208  }
209  nFrame_++;
210  nFrameFile_++;
211  if (nFrame_ % framesPerFile == 0)
212  nFrameFile_ = 0;
213  }
214 }
215 
216 // ------------ method called once each job just before starting event loop ------------
218 
219 // ------------ method called once each job just after ending the event loop ------------
221  //frames per event
222  unsigned int framesPerEv = nHeaderFrames_ + nPayloadFrames_ + nClearFrames_;
223 
224  //events per file
225  unsigned int evPerFile = floor(framesPerFile / framesPerEv);
226 
227  //number of output files
228  unsigned int nOutFiles = ceil((float)nEvents_ / (float)evPerFile);
229 
230  LogDebug("L1TDebug") << "Read " << nFrame_ << " frames" << std::endl;
231  LogDebug("L1TDebug") << "Read " << nEvents_ << " events" << std::endl;
232  LogDebug("L1TDebug") << "Writing " << nOutFiles << " files" << std::endl;
233  LogDebug("L1TDebug") << "Output directory: ./" << outDir_ << "/" << std::endl;
234 
235  //files
236  std::vector<std::ofstream> outFiles(nOutFiles);
237 
238  //make output files and write to them
239  for (uint itFile = 0; itFile < nOutFiles; ++itFile) {
240  std::stringstream outFilename;
241  outFilename << outDir_ << "/" << filename_ << "_" << itFile << ".txt";
242  outFiles[itFile] = std::ofstream(outFilename.str());
243  LogDebug("L1TDebug") << "Writing to file: ./" << outFilename.str() << std::endl;
244 
245  outFiles[itFile] << "Board SRNTY_TEST" << std::endl;
246 
247  // quad/chan numbers
248  outFiles[itFile] << " Quad/Chan : ";
249  for (unsigned i = 0; i < nQuad_; ++i) {
250  for (unsigned j = 0; j < nChan_; ++j) {
251  outFiles[itFile] << " q" << setfill('0') << setw(2) << i << "c" << j << " ";
252  }
253  }
254  outFiles[itFile] << std::endl;
255 
256  // link numbers
257  outFiles[itFile] << " Link : ";
258  for (unsigned i = 0; i < nQuad_; ++i) {
259  for (unsigned j = 0; j < nChan_; ++j) {
260  outFiles[itFile] << " " << setfill('0') << setw(2) << (i * nChan_) + j << " ";
261  }
262  }
263 
264  outFiles[itFile] << std::endl;
265 
266  // then the data
267  unsigned iFileFrame = 0;
268  for (unsigned iFrame = itFile * framesPerFile; iFrame < (itFile * framesPerFile + framesPerFile); ++iFrame) {
269  if (iFrame <= nFrame_ && iFrame < (framesPerEv * nEvents_)) {
270  outFiles[itFile] << "Frame " << std::dec << std::setw(4) << std::setfill('0') << iFileFrame << " : ";
271  for (unsigned iQuad = 0; iQuad < nQuad_; ++iQuad) {
272  for (unsigned iChan = 0; iChan < nChan_; ++iChan) {
273  unsigned iLink = (iQuad * nChan_) + iChan;
274  if (iLink < data_.size() && iFrame < data_.at(iLink).size()) {
275  outFiles[itFile] << std::hex << ::std::setw(1) << dataValid_.at(iFrame) << "v" << std::hex
276  << std::setw(16) << std::setfill('0') << data_.at(iLink).at(iFrame) << " ";
277  } else {
278  outFiles[itFile] << std::hex << ::std::setw(1) << 0 << "v" << std::hex << std::setw(16)
279  << std::setfill('0') << 0 << " ";
280  }
281  }
282  }
283  }
284  outFiles[itFile] << std::endl;
285  iFileFrame++;
286  }
287  outFiles[itFile].close();
288  }
289 }
290 
291 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
294  desc.addUntracked<unsigned int>("nPayloadFrames", 40);
295  desc.addUntracked<unsigned int>("nHeaderFrames", 1);
296  desc.add<edm::InputTag>("inputCollectionTag", edm::InputTag("l1pfCandidates", "Puppi", "IN"));
297  desc.addUntracked<std::string>("filename", "pattern.txt");
298  desc.addUntracked<unsigned int>("nChanPerQuad", 4);
299  desc.addUntracked<unsigned int>("nClearFrames", 13);
300  desc.addUntracked<unsigned int>("nQuads", 18);
301  descriptions.add("l1tS2PFJetInputPatternWriter", desc);
302 }
303 
304 //define this as a plug-in
std::vector< std::vector< uint64_t > > data_
constexpr int32_t ceil(float num)
constexpr float eta_second_region_boundary
constexpr float phi_region_boundary
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
constexpr unsigned int bit_shift_eta
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
constexpr float ptLSB
constexpr unsigned int n_latency_clocks
constexpr unsigned int hash_bitmask_10
void analyze(const edm::Event &, const edm::EventSetup &) override
int iEvent
Definition: GenABIO.cc:224
L1TS2PFJetInputPatternWriter(const edm::ParameterSet &)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
constexpr unsigned int framesPerFile
constexpr unsigned int bit_shift_phi
constexpr unsigned int hash_bitmask_16
constexpr float etaLSB
constexpr float eta_first_region_boundary
unsigned long long uint64_t
Definition: Time.h:13
constexpr unsigned int mod_13_1
void add(std::string const &label, ParameterSetDescription const &psetDescription)
constexpr unsigned int n_first_empty_frames
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
constexpr unsigned int mod_13_2
edm::EDGetTokenT< std::vector< l1t::PFCandidate > > pfToken_
constexpr float phiLSB
#define LogDebug(id)