CMS 3D CMS Logo

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  }
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
PFCandidate.h
phiLSB
constexpr float phiLSB
Definition: L1TS2PFJetInputPatternWriter.cc:56
L1TS2PFJetInputPatternWriter::nQuad_
unsigned nQuad_
Definition: L1TS2PFJetInputPatternWriter.cc:77
EDAnalyzer.h
mps_fire.i
i
Definition: mps_fire.py:428
bit_shift_eta
constexpr unsigned int bit_shift_eta
Definition: L1TS2PFJetInputPatternWriter.cc:43
CaloTools.h
n_first_empty_frames
constexpr unsigned int n_first_empty_frames
Definition: L1TS2PFJetInputPatternWriter.cc:47
L1TS2PFJetInputPatternWriter::endJob
void endJob() override
Definition: L1TS2PFJetInputPatternWriter.cc:220
ptLSB
constexpr float ptLSB
Definition: L1TS2PFJetInputPatternWriter.cc:54
mod_13_2
constexpr unsigned int mod_13_2
Definition: L1TS2PFJetInputPatternWriter.cc:45
L1TS2PFJetInputPatternWriter::nClearFrames_
unsigned nClearFrames_
Definition: L1TS2PFJetInputPatternWriter.cc:81
L1TS2PFJetInputPatternWriter::filename_
std::string filename_
Definition: L1TS2PFJetInputPatternWriter.cc:72
L1TS2PFJetInputPatternWriter::map_
std::map< int, int > map_
Definition: L1TS2PFJetInputPatternWriter.cc:93
edm::EDGetTokenT
Definition: EDGetToken.h:33
phi_region_boundary
constexpr float phi_region_boundary
Definition: L1TS2PFJetInputPatternWriter.cc:51
edm
HLT enums.
Definition: AlignableModifier.h:19
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89287
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
hash_bitmask_10
constexpr unsigned int hash_bitmask_10
Definition: L1TS2PFJetInputPatternWriter.cc:53
L1TS2PFJetInputPatternWriter::nHeaderFrames_
unsigned nHeaderFrames_
Definition: L1TS2PFJetInputPatternWriter.cc:79
L1TS2PFJetInputPatternWriter::dataValid_
std::vector< int > dataValid_
Definition: L1TS2PFJetInputPatternWriter.cc:90
edm::one::EDAnalyzer
Definition: EDAnalyzer.h:30
eta_first_region_boundary
constexpr float eta_first_region_boundary
Definition: L1TS2PFJetInputPatternWriter.cc:49
edm::Handle
Definition: AssociativeIterator.h:50
parallelization.uint
uint
Definition: parallelization.py:124
L1TS2PFJetInputPatternWriter::nPayloadFrames_
unsigned nPayloadFrames_
Definition: L1TS2PFJetInputPatternWriter.cc:80
L1TS2PFJetInputPatternWriter::nChan_
unsigned nChan_
Definition: L1TS2PFJetInputPatternWriter.cc:76
pfElectronTranslator_cfi.PFCandidate
PFCandidate
Definition: pfElectronTranslator_cfi.py:6
MakerMacros.h
hash_bitmask_16
constexpr unsigned int hash_bitmask_16
Definition: L1TS2PFJetInputPatternWriter.cc:52
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
reco::ceil
constexpr int32_t ceil(float num)
Definition: constexpr_cmath.h:7
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
L1TS2PFJetInputPatternWriter::~L1TS2PFJetInputPatternWriter
~L1TS2PFJetInputPatternWriter() override
Definition: L1TS2PFJetInputPatternWriter.cc:129
L1TS2PFJetInputPatternWriter::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: L1TS2PFJetInputPatternWriter.cc:292
Service.h
mod_13_1
constexpr unsigned int mod_13_1
Definition: L1TS2PFJetInputPatternWriter.cc:44
framesPerFile
constexpr unsigned int framesPerFile
Definition: L1TS2PFJetInputPatternWriter.cc:48
L1TS2PFJetInputPatternWriter::data_
std::vector< std::vector< uint64_t > > data_
Definition: L1TS2PFJetInputPatternWriter.cc:87
L1TS2PFJetInputPatternWriter::pfToken_
edm::EDGetTokenT< std::vector< l1t::PFCandidate > > pfToken_
Definition: L1TS2PFJetInputPatternWriter.cc:71
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
TFileService.h
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:223
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
bit_shift_phi
constexpr unsigned int bit_shift_phi
Definition: L1TS2PFJetInputPatternWriter.cc:42
l1t
delete x;
Definition: CaloConfig.h:22
etaLSB
constexpr float etaLSB
Definition: L1TS2PFJetInputPatternWriter.cc:55
L1TS2PFJetInputPatternWriter
Definition: L1TS2PFJetInputPatternWriter.cc:58
iEvent
int iEvent
Definition: GenABIO.cc:224
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
edm::EventSetup
Definition: EventSetup.h:57
L1TS2PFJetInputPatternWriter::outDir_
std::string outDir_
Definition: L1TS2PFJetInputPatternWriter.cc:73
L1TS2PFJetInputPatternWriter::nEvents_
unsigned nEvents_
Definition: L1TS2PFJetInputPatternWriter.cc:84
L1TS2PFJetInputPatternWriter::nFrameFile_
unsigned nFrameFile_
Definition: L1TS2PFJetInputPatternWriter.cc:83
eta_second_region_boundary
constexpr float eta_second_region_boundary
Definition: L1TS2PFJetInputPatternWriter.cc:50
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
std
Definition: JetResolutionObject.h:76
Frameworkfwd.h
L1TS2PFJetInputPatternWriter::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: L1TS2PFJetInputPatternWriter.cc:139
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
n_latency_clocks
constexpr unsigned int n_latency_clocks
Definition: L1TS2PFJetInputPatternWriter.cc:46
cond::uint64_t
unsigned long long uint64_t
Definition: Time.h:13
ParameterSet.h
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
L1TS2PFJetInputPatternWriter::nLink_
unsigned nLink_
Definition: L1TS2PFJetInputPatternWriter.cc:78
edm::Event
Definition: Event.h:73
TauDecayModes.dec
dec
Definition: TauDecayModes.py:143
edm::InputTag
Definition: InputTag.h:15
L1TS2PFJetInputPatternWriter::beginJob
void beginJob() override
Definition: L1TS2PFJetInputPatternWriter.cc:217
L1TS2PFJetInputPatternWriter::L1TS2PFJetInputPatternWriter
L1TS2PFJetInputPatternWriter(const edm::ParameterSet &)
Definition: L1TS2PFJetInputPatternWriter.cc:107
L1TS2PFJetInputPatternWriter::nFrame_
unsigned nFrame_
Definition: L1TS2PFJetInputPatternWriter.cc:82