CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
L1TStage2InputPatternWriter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: L1Trigger/L1TCalorimeter
4 // Class: L1TStage2InputPatternWriter
5 //
13 //
14 // Original Author: James Brooke
15 // Created: Tue, 11 Mar 2014 14:55:45 GMT
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 
23 // user include files
26 
29 
31 
34 
37 
39 
41 
42 #include <fstream>
43 #include <iostream>
44 #include <iomanip>
45 
46 //
47 // class declaration
48 //
49 
51 public:
54 
55  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
56 
57 
58 private:
59  virtual void beginJob() override;
60  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
61  virtual void endJob() override;
62 
63  //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
64  //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
65  //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
66  //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
67 
68  // ----------member data ---------------------------
70 
72 
73  // constants
74  unsigned nChan_; // number of channels per quad
75  unsigned nQuad_;
76  unsigned nLink_;
77  unsigned nHeaderFrames_;
78  unsigned nPayloadFrames_;
79  unsigned nClearFrames_;
80  unsigned nFrame_;
81 
82  // data arranged by link and frame
83  std::vector< std::vector<int> > data_;
84 
85  // data valid flags (just one per frame for now)
86  std::vector<int> dataValid_;
87 
88  // map of towers onto links/frames
89  std::map< int, int > map_;
90 
91 };
92 
93 //
94 // constants, enums and typedefs
95 //
96 
97 //
98 // static data member definitions
99 //
100 
101 //
102 // constructors and destructor
103 //
105 {
106  //now do what ever initialization is needed
107 
108  // register what you consume and keep token for later access:
109  m_towerToken = consumes<l1t::CaloTowerBxCollection> (iConfig.getParameter<edm::InputTag>("towerToken"));
110 
111  filename_ = iConfig.getUntrackedParameter<std::string>("filename", "pattern.txt");
112 
113  nChan_ = iConfig.getUntrackedParameter<unsigned>("nChanPerQuad", 4);
114  nQuad_ = iConfig.getUntrackedParameter<unsigned>("nQuads", 18);
115 
116  nHeaderFrames_ = iConfig.getUntrackedParameter<unsigned>("nHeaderFrames", 1);
117  nPayloadFrames_ = iConfig.getUntrackedParameter<unsigned>("nPayloadFrames", 39);
118  nClearFrames_ = iConfig.getUntrackedParameter<unsigned>("nClearFrames", 6);
119  nFrame_ = 0;
120 
121  nLink_ = nChan_ * nQuad_;
122  data_.resize(nLink_);
123  LogDebug("L1TDebug") << "Preparing for " << nLink_ << " links" << std::endl;
124 
125 }
126 
127 
129 {
130 
131  // do anything here that needs to be done at desctruction time
132  // (e.g. close files, deallocate resources etc.)
133 
134 }
135 
136 
137 //
138 // member functions
139 //
140 
141 // ------------ method called for each event ------------
142 void
144 {
145  using namespace edm;
146 
147  // get towers
149  iEvent.getByToken(m_towerToken,towHandle);
150 
151  std::vector<l1t::CaloTower> towers;
152 
153  for(std::vector<l1t::CaloTower>::const_iterator tower = towHandle->begin(0);
154  tower != towHandle->end(0);
155  ++tower) {
156  towers.push_back(*tower);
157  }
158 
159 
160  // insert header frames
161  for ( unsigned iFrame=0; iFrame<nHeaderFrames_; ++iFrame ) {
162 
163  dataValid_.push_back( 1 );
164 
165  // loop over links
166  for ( unsigned iQuad=0; iQuad<nQuad_; ++iQuad ) {
167  for ( unsigned iChan=0; iChan<nChan_; ++iChan ) {
168 
169  int data=0;
170 
171  // get tower ieta, iphi for link
172  unsigned iLink = (iQuad*nChan_)+iChan;
173 
174  // add data to output
175  data_.at(iLink).push_back( data );
176 
177  }
178 
179  }
180 
181  nFrame_++;
182 
183  }
184 
185 
186 
187  // loop over frames
188  for ( unsigned iFrame=0; iFrame<nPayloadFrames_; ++iFrame ) {
189 
190  dataValid_.push_back( 1 );
191 
192  // loop over links
193  for ( unsigned iQuad=0; iQuad<nQuad_; ++iQuad ) {
194  for ( unsigned iChan=0; iChan<nChan_; ++iChan ) {
195 
196  int data=0;
197 
198  // get tower ieta, iphi for link
199  int iLink = (iQuad*nChan_)+iChan;
200  int ietaSgn = (iLink % 2==0 ? +1 : -1);
201  int ieta = ietaSgn * (iFrame + 1);
202  int iphi = 1+(iLink % 2==0 ? iLink : iLink-1);
203 
204  // get tower 1 data
205  l1t::CaloTower tower = l1t::CaloTools::getTower(towers, ieta, iphi);
206  data |= tower.hwPt() & 0xff;
207  data |= (tower.hwEtRatio() & 0x7)<<8;
208  data |= (tower.hwQual() & 0xf)<<12;
209 
210  // get tower 2
211  iphi = iphi + 1;
212  tower = l1t::CaloTools::getTower(towers, ieta, iphi);
213  data |= (tower.hwPt() & 0xff)<<16;
214  data |= (tower.hwEtRatio() & 0x7)<<24;
215  data |= (tower.hwQual() & 0xf)<<28;
216 
217  // add data to output
218  data_.at(iLink).push_back( data );
219 
220  }
221 
222  }
223 
224  nFrame_++;
225 
226  }
227 
228 
229  // loop over clear frames
230  for ( unsigned iFrame=0; iFrame<nClearFrames_; ++iFrame ) {
231 
232  dataValid_.push_back( 0 );
233 
234  // loop over links
235  for ( unsigned iQuad=0; iQuad<nQuad_; ++iQuad ) {
236  for ( unsigned iChan=0; iChan<nChan_; ++iChan ) {
237 
238  int data=0;
239 
240  // get tower ieta, iphi for link
241  unsigned iLink = (iQuad*nChan_)+iChan;
242 
243  // add data to output
244  data_.at(iLink).push_back( data );
245 
246  }
247 
248  }
249 
250  nFrame_++;
251 
252  }
253 
254 
255 }
256 
257 
258 // ------------ method called once each job just before starting event loop ------------
259 void
261 {
262 
263 
264 }
265 
266 // ------------ method called once each job just after ending the event loop ------------
267 void
269 {
270 
271  LogDebug("L1TDebug") << "Read " << nFrame_ << " frames" << std::endl;
272 
273  // write file
274  std::ofstream file( filename_ );
275 
276  file << "Board MP7_TEST" << std::endl;
277 
278  // quad/chan numbers
279  file << " Quad/Chan : ";
280  for ( unsigned i=0; i<nQuad_; ++i ) {
281  for ( unsigned j=0; j<nChan_; ++j ) {
282  file << " q" << i << "c" << j << " ";
283  }
284  }
285  file << std::endl;
286 
287  // link numbers
288  file << " Link : ";
289  for ( unsigned i=0; i<nQuad_; ++i ) {
290  for ( unsigned j=0; j<nChan_; ++j ) {
291  file << " " << (i*nChan_)+j << " ";
292  }
293  }
294 
295  file << std::endl;
296 
297  // then the data
298  for ( unsigned iFrame=0; iFrame<nFrame_; ++iFrame ) {
299  file << "Frame " << std::dec << std::setw(4) << std::setfill('0') << iFrame << " : ";
300  for ( unsigned iQuad=0; iQuad<nQuad_; ++iQuad ) {
301  for ( unsigned iChan=0; iChan<nChan_; ++iChan ) {
302  unsigned iLink = (iQuad*nChan_)+iChan;
303  if (iLink<data_.size() && iFrame<data_.at(iLink).size()) {
304  file << std::hex << ::std::setw(1) << dataValid_.at(iFrame) << "v" << std::hex << std::setw(8) << std::setfill('0') << data_.at(iLink).at(iFrame) << " ";
305  }
306  else {
307  std::cerr << "Out of range : " << iLink << ", " << iFrame << std::endl;
308  }
309  }
310  }
311  file << std::endl;
312  }
313 
314  file.close();
315 
316 }
317 
318 // ------------ method called when starting to processes a run ------------
319 /*
320 void
321 L1TStage2InputPatternWriter::beginRun(edm::Run const&, edm::EventSetup const&)
322 {
323 }
324 */
325 
326 // ------------ method called when ending the processing of a run ------------
327 /*
328 void
329 L1TStage2InputPatternWriter::endRun(edm::Run const&, edm::EventSetup const&)
330 {
331 }
332 */
333 
334 // ------------ method called when starting to processes a luminosity block ------------
335 /*
336 void
337 L1TStage2InputPatternWriter::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
338 {
339 }
340 */
341 
342 // ------------ method called when ending the processing of a luminosity block ------------
343 /*
344 void
345 L1TStage2InputPatternWriter::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
346 {
347 }
348 */
349 
350 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
351 void
353  //The following says we do not know what parameters are allowed so do no validation
354  // Please change this to state exactly what you do use, even if it is no parameters
356  desc.setUnknown();
357  descriptions.addDefault(desc);
358 }
359 
360 //define this as a plug-in
#define LogDebug(id)
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:464
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
int hwEtRatio() const
Definition: CaloTower.cc:74
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
int iEvent
Definition: GenABIO.cc:230
L1TStage2InputPatternWriter(const edm::ParameterSet &)
void addDefault(ParameterSetDescription const &psetDescription)
int j
Definition: DBlmapReader.cc:9
int hwQual() const
Definition: L1Candidate.cc:89
int hwPt() const
Definition: L1Candidate.cc:69
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
static const l1t::CaloTower & getTower(const std::vector< l1t::CaloTower > &towers, int iEta, int iPhi)
Definition: CaloTools.cc:11
std::vector< std::vector< int > > data_