CMS 3D CMS Logo

L1RCTLutWriter.cc
Go to the documentation of this file.
2 
4 
11 
12 // default scales
17 
18 // debug scales
22 
25 
26 //
27 // constants, enums and typedefs
28 //
29 
30 //
31 // static data member definitions
32 //
33 
34 //
35 // constructors and destructor
36 //
38  : lookupTable_(new L1RCTLookupTables),
39  keyName_(iConfig.getParameter<std::string>("key")),
40  useDebugTpgScales_(iConfig.getParameter<bool>("useDebugTpgScales")) {
41  // now do what ever initialization is needed
42 }
43 
45 
46  // do anything here that needs to be done at destruction time
47  // (e.g. close files, deallocate resources etc.)
48 
49  if (lookupTable_ != nullptr)
50  delete lookupTable_;
51 }
52 
53 //
54 // member functions
55 //
56 
57 // ------------ method called to for each event ------------
59  const edm::EventSetup &iSetup) {
60 
61  // get all the configuration information from the event, set it
62  // in the lookuptable
63  edm::ESHandle<L1RCTParameters> rctParameters;
64  iSetup.get<L1RCTParametersRcd>().get(rctParameters);
65  rctParameters_ = rctParameters.product();
67  iSetup.get<L1EmEtScaleRcd>().get(emScale);
68  const L1CaloEtScale *s = emScale.product();
69 
70  // make dummy channel mask -- we don't want to mask
71  // any channels when writing LUTs, that comes afterwards
73  for (int i = 0; i < 18; i++) {
74  for (int j = 0; j < 2; j++) {
75  for (int k = 0; k < 28; k++) {
76  m->ecalMask[i][j][k] = false;
77  m->hcalMask[i][j][k] = false;
78  }
79  for (int k = 0; k < 4; k++) {
80  m->hfMask[i][j][k] = false;
81  }
82  }
83  }
84 
85  // Same for Noisy mask
86  // make dummy channel mask -- we don't want to mask
87  // any channels when writing LUTs, that comes afterwards
89  for (int i = 0; i < 18; i++) {
90  for (int j = 0; j < 2; j++) {
91  for (int k = 0; k < 28; k++) {
92  m2->ecalMask[i][j][k] = false;
93  m2->hcalMask[i][j][k] = false;
94  }
95  for (int k = 0; k < 4; k++) {
96  m2->hfMask[i][j][k] = false;
97  }
98  }
99  }
100  m2->ecalThreshold = 0.0;
101  m2->hcalThreshold = 0.0;
102  m2->hfThreshold = 0.0;
103 
104  // use these dummies to get the delete right when using old-style
105  // scales to create set of L1CaloXcalScales
106  L1CaloEcalScale *dummyE(nullptr);
107  L1CaloHcalScale *dummyH(nullptr);
108 
109  if (useDebugTpgScales_) // generate new-style scales from tpg scales
110  {
111 
112  std::cout << "Using old-style TPG scales!" << std::endl;
113 
114  // old version of hcal energy scale to convert input
116  iSetup.get<CaloTPGRecord>().get(transcoder);
117  const CaloTPGTranscoder *h_tpg = transcoder.product();
118 
119  // old version of ecal energy scale to convert input
120  EcalTPGScale *e_tpg = new EcalTPGScale();
121  e_tpg->setEventSetup(iSetup);
122 
123  L1CaloEcalScale *ecalScale = new L1CaloEcalScale();
124  L1CaloHcalScale *hcalScale = new L1CaloHcalScale();
125 
126  // generate L1CaloXcalScales from old-style scales (thanks, werner!)
127 
128  // ECAL
129  for (unsigned short ieta = 1; ieta <= L1CaloEcalScale::nBinEta; ++ieta) {
130  for (unsigned short irank = 0; irank < L1CaloEcalScale::nBinRank;
131  ++irank) {
132  EcalSubdetector subdet = (ieta <= 17) ? EcalBarrel : EcalEndcap;
133  double etGeVPos =
134  e_tpg->getTPGInGeV(irank, EcalTrigTowerDetId(1, // +ve eta
135  subdet, ieta,
136  1)); // dummy phi value
137  ecalScale->setBin(irank, ieta, 1, etGeVPos);
138  }
139  }
140 
141  for (unsigned short ieta = 1; ieta <= L1CaloEcalScale::nBinEta; ++ieta) {
142  for (unsigned short irank = 0; irank < L1CaloEcalScale::nBinRank;
143  ++irank) {
144  EcalSubdetector subdet = (ieta <= 17) ? EcalBarrel : EcalEndcap;
145 
146  double etGeVNeg =
147  e_tpg->getTPGInGeV(irank,
148  EcalTrigTowerDetId(-1, // -ve eta
149  subdet, ieta,
150  2)); // dummy phi value
151  ecalScale->setBin(irank, ieta, -1, etGeVNeg);
152  }
153  }
154 
155  // HCAL
156  for (unsigned short ieta = 1; ieta <= L1CaloHcalScale::nBinEta; ++ieta) {
157  for (unsigned short irank = 0; irank < L1CaloHcalScale::nBinRank;
158  ++irank) {
159  double etGeV = h_tpg->hcaletValue(ieta, irank);
160 
161  hcalScale->setBin(irank, ieta, 1, etGeV);
162  hcalScale->setBin(irank, ieta, -1, etGeV);
163  }
164  }
165 
166  // set the input scales
167  lookupTable_->setEcalScale(ecalScale);
168  lookupTable_->setHcalScale(hcalScale);
169 
170  dummyE = ecalScale;
171  dummyH = hcalScale;
172 
173  delete e_tpg;
174 
175  } else {
176 
177  // get energy scale to convert input from ECAL
179  iSetup.get<L1CaloEcalScaleRcd>().get(ecalScale);
180  const L1CaloEcalScale *e = ecalScale.product();
181 
182  // get energy scale to convert input from HCAL
184  iSetup.get<L1CaloHcalScaleRcd>().get(hcalScale);
185  const L1CaloHcalScale *h = hcalScale.product();
186 
187  // set scales
190  }
191 
195  // lookupTable_->setHcalScale(h);
196  // lookupTable_->setEcalScale(e);
198 
199  for (unsigned short nCard = 0; nCard <= 6; nCard = nCard + 2) {
200  writeRcLutFile(nCard);
201  writeEicLutFile(nCard);
202  }
203  writeJscLutFile();
204 
205  unsigned int eicThreshold = rctParameters_->eicIsolationThreshold();
206  unsigned int jscThresholdBarrel = rctParameters_->jscQuietThresholdBarrel();
207  unsigned int jscThresholdEndcap = rctParameters_->jscQuietThresholdEndcap();
208  writeThresholdsFile(eicThreshold, jscThresholdBarrel, jscThresholdEndcap);
209 
210  if (dummyE != nullptr)
211  delete dummyE;
212  if (dummyH != nullptr)
213  delete dummyH;
214 }
215 
216 // ------------ method called once each job just after ending the event loop
217 // ------------
219 
220 // ------------ method to write one receiver card lut file
221 void L1RCTLutWriter::writeRcLutFile(unsigned short card) {
222 
223  // don't mess yet with name
224  char filename[256];
225  char command[264];
226  if (card != 6) {
227  int card2 = card + 1;
228  sprintf(filename, "RC%i%i-%s.dat", card, card2, keyName_.c_str());
229  // sprintf(filename, "RC%i%i.dat", card, card2);
230  } else {
231  sprintf(filename, "RC6-%s.dat", keyName_.c_str());
232  // sprintf(filename, "RC6.dat");
233  }
234  // open file for writing, delete any existing content
235  lutFile_.open(filename, std::ios::trunc);
236  lutFile_ << "Emulator-parameter generated lut file, card " << card << " key "
237  << keyName_ << " ";
238 
239  // close to append timestamp info
240  lutFile_.close();
241  sprintf(command, "date >> %s", filename);
242  system(command);
243 
244  // reopen file for writing values
245  lutFile_.open(filename, std::ios::app);
246 
247  unsigned long data = 0;
248 
249  // write all memory addresses in increasing order
250  // address = (1<<22)+(nLUT<<19)+(eG?<18)+(hcalEt<<10)+(ecalfg<<9)+(ecalEt<<1)
251 
252  // loop through the physical LUTs on the card, 0-7
253  for (unsigned short nLUT = 0; nLUT < 8; nLUT++) {
254  // determine ieta, iphi, etc. everything
255  unsigned short iAbsEta = 0;
256  if (card != 6) {
257  iAbsEta = (card / 2) * 8 + nLUT + 1;
258  } else {
259  if (nLUT < 4) {
260  iAbsEta = (card / 2) * 8 + nLUT + 1;
261  } else {
262  iAbsEta = (card / 2) * 8 + (3 - (nLUT % 4)) + 1;
263  }
264  // std::cout << "LUT is " << nLUT << " iAbsEta is " << iAbsEta <<
265  // std::endl;
266  }
267 
268  // All RCT stuff uniform in phi, symmetric wrt eta = 0
269 
270  // below line always gives crate in +eta; makes no difference to us
271  unsigned short crate = rctParameters_->calcCrate(1, iAbsEta);
272  unsigned short tower = rctParameters_->calcTower(1, iAbsEta);
273 
274  // first do region sums half of LUTs, bit 18 of address is 0
275  // loop through 8 bits of hcal energy, 2^8 is 256
276  for (unsigned int hcalEt = 0; hcalEt < 256; hcalEt++) {
277  // loop through 1 bit of ecal fine grain
278  for (unsigned short ecalfg = 0; ecalfg < 2; ecalfg++) {
279  // loop through 8 bits of ecal energy
280  for (unsigned int ecalEt = 0; ecalEt < 256; ecalEt++) {
281  // assign 10-bit (9et,1mip) sums data here!
282  unsigned long output =
283  lookupTable_->lookup(ecalEt, hcalEt, ecalfg, crate, card, tower);
284  unsigned short etIn9Bits = (output >> 8) & 511;
285  unsigned short tauActivityBit = (output >> 17) & 1;
286  data = (tauActivityBit << 9) + etIn9Bits;
287  lutFile_ << std::hex << data << std::dec << std::endl;
288  }
289  }
290  }
291  // second do egamma half of LUTs, bit 18 of address is 1
292  // loop through 8 bits of hcal energy
293  for (unsigned int hcalEt = 0; hcalEt < 256; hcalEt++) {
294  // loop through 1 bit of ecal fine grain
295  for (unsigned short ecalfg = 0; ecalfg < 2; ecalfg++) {
296  // loop through 8 bits of ecal energy
297  for (unsigned int ecalEt = 0; ecalEt < 256; ecalEt++) {
298  // assign 8-bit (7et,1veto) egamma data here!
299  unsigned long output =
300  lookupTable_->lookup(ecalEt, hcalEt, ecalfg, crate, card, tower);
301  unsigned short etIn7Bits = output & 127;
302  unsigned short heFgVetoBit = (output >> 7) & 1;
303  data = (heFgVetoBit << 7) + etIn7Bits;
304  lutFile_ << std::hex << data << std::dec << std::endl;
305  }
306  }
307  }
308  }
309 
310  lutFile_.close();
311  return;
312 }
313 
314 // ------------ method to write one electron isolation card lut file
315 void L1RCTLutWriter::writeEicLutFile(unsigned short card) {
316  // try timestamp
317  char filename[256];
318  char command[264];
319  if (card != 6) {
320  int card2 = card + 1;
321  sprintf(filename, "EIC%i%i-%s.dat", card, card2, keyName_.c_str());
322  } else {
323  sprintf(filename, "EIC6-%s.dat", keyName_.c_str());
324  }
325  // open file for writing, delete any existing content
326  lutFile_.open(filename, std::ios::trunc);
327  lutFile_ << "Emulator-parameter generated EIC lut file, card " << card
328  << " key " << keyName_ << " ";
329  // close to append timestamp info
330  lutFile_.close();
331  sprintf(command, "date >> %s", filename);
332  system(command);
333 
334  // reopen file for writing values
335  lutFile_.open(filename, std::ios::app);
336 
337  unsigned long data = 0;
338 
339  // write all memory addresses in increasing order
340  // address = (1<<22) + (etIn7Bits<<1)
341 
342  // 2^7 = 0x7f = 128
343  for (int etIn7Bits = 0; etIn7Bits < 128; etIn7Bits++) {
344  data = lookupTable_->emRank(etIn7Bits);
345  if (data > 0x3f) {
346  data = 0x3f;
347  }
348  lutFile_ << std::hex << data << std::dec << std::endl;
349  }
350  lutFile_.close();
351  return;
352 }
353 
354 // ------------ method to write one jet summary card lut file
356  char filename[256];
357  char command[264];
358  sprintf(filename, "JSC-%s.dat", keyName_.c_str());
359 
360  // open file; if it already existed, delete existing content
361  lutFile_.open(filename, std::ios::trunc);
362  lutFile_ << "Emulator parameter-generated lut file, key " << keyName_
363  << " ";
364  // close to append time-stamp
365  lutFile_.close();
366  sprintf(command, "date >> %s", filename);
367  system(command);
368  // reopen file for writing
369  lutFile_.open(filename, std::ios::app);
370 
371  unsigned long data = 0;
372  unsigned long data0 = 0;
373  unsigned long data1 = 0;
374 
375  // write all memory addresses in increasing order
376  // address = (1<<22) + (lutbits<<17) + (phi1et<<9) + (phi0et<<1);
377 
378  // ecl and U93/U225 lut id bits, identify eta segment of hf
379  for (int lutbits = 0; lutbits < 4; lutbits++) {
380  // 8-bit phi_1 et for each eta partition
381  for (unsigned int phi1et = 0; phi1et < 256; phi1et++) {
382  // 8-bit phi_0 et for each eta
383  for (unsigned int phi0et = 0; phi0et < 256; phi0et++) {
384  // lookup takes "(hf_et, crate, card, tower)"
385  // "card" convention for hf is 999, tower is 0-7
386  // but equivalent to 0-3 == lutbits
387  // crate doesn't matter, take 0
388  // only |ieta| matters
389  data0 = lookupTable_->lookup(phi0et, 0, 999, lutbits);
390  if (data0 > 0xff) {
391  data0 = 0xff; // 8-bit output energy for each phi region
392  }
393  data1 = lookupTable_->lookup(phi1et, 0, 999, lutbits);
394  if (data1 > 0xff) {
395  data1 = 0xff; // 8-bit output energy for each phi region
396  }
397  data = (data1 << 8) + (data0);
398  lutFile_ << std::hex << data << std::dec << std::endl;
399  /*
400  if (phi0et < 10 && phi1et < 10)
401  {
402  std::cout << "Writer: jsc. lutbits=" << lutbits
403  << " phi0et=" << phi0et << " data0=" << data0
404  << " phi1et=" << phi1et << " data1=" << data1
405  << std::endl;
406  }
407  */
408  }
409  }
410  }
411 
412  lutFile_.close();
413  return;
414 }
415 
416 //-----------Write text file containing the 1 JSC and 2 EIC thresholds
417 void L1RCTLutWriter::writeThresholdsFile(unsigned int eicThreshold,
418  unsigned int jscThresholdBarrel,
419  unsigned int jscThresholdEndcap) {
420  //
421  std::ofstream thresholdsFile;
422  char filename[256];
423  sprintf(filename, "Thresholds-%s.dat", keyName_.c_str());
424  thresholdsFile.open(filename, std::ios::trunc);
425 
426  thresholdsFile << "key is " << keyName_ << std::endl << std::endl;
427  thresholdsFile << "eicIsolationThreshold " << eicThreshold << std::endl;
428  thresholdsFile << "jscQuietThresholdBarrel " << jscThresholdBarrel
429  << std::endl;
430  thresholdsFile << "jscQuietThresholdEndcap " << jscThresholdEndcap
431  << std::endl;
432 
433  thresholdsFile.close();
434 }
435 
436 // define this as a plug-in
437 // DEFINE_FWK_MODULE(L1RCTLutWriter); // done in SealModule.cc
const L1RCTParameters * rctParameters_
L1RCTLutWriter(const edm::ParameterSet &)
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
void setEventSetup(const edm::EventSetup &evtSetup)
Definition: EcalTPGScale.cc:19
void writeRcLutFile(unsigned short card)
unsigned eicIsolationThreshold() const
void analyze(const edm::Event &, const edm::EventSetup &) override
bool ecalMask[18][2][28]
void setBin(unsigned short rank, unsigned short eta, short etaSign, double et)
set scale element; use this to create non-linear scales
L1RCTLookupTables * lookupTable_
void setBin(unsigned short rank, unsigned short eta, short etaSign, double et)
set scale element; use this to create non-linear scales
double getTPGInGeV(const EcalTriggerPrimitiveDigi &tpDigi)
Definition: EcalTPGScale.cc:24
unsigned jscQuietThresholdEndcap() const
std::string keyName_
void endJob() override
int iEvent
Definition: GenABIO.cc:224
unsigned short calcTower(unsigned short rct_iphi, unsigned short absIeta) const
void setHcalScale(const L1CaloHcalScale *hcalScale)
static const unsigned short nBinRank
void writeThresholdsFile(unsigned int eicThreshold, unsigned int jscThresholdBarrel, unsigned int jscThresholdEndcap)
unsigned int emRank(unsigned short energy) const
void setL1CaloEtScale(const L1CaloEtScale *etScale)
int k[5][pyjets_maxn]
void setEcalScale(const L1CaloEcalScale *ecalScale)
std::ofstream lutFile_
list command
Definition: mps_check.py:25
void setRCTParameters(const L1RCTParameters *rctParameters)
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
static const unsigned short nBinRank
T get() const
Definition: EventSetup.h:71
void setNoisyChannelMask(const L1RCTNoisyChannelMask *channelMask)
static const unsigned short nBinEta
static const unsigned short nBinEta
bool hcalMask[18][2][28]
virtual double hcaletValue(const int &ieta, const int &iphi, const int &version, const int &compressedValue) const =0
void writeEicLutFile(unsigned short card)
~L1RCTLutWriter() override
EcalSubdetector
T const * product() const
Definition: ESHandle.h:86
bool hfMask[18][2][4]
void setChannelMask(const L1RCTChannelMask *channelMask)
unsigned short calcCrate(unsigned short rct_iphi, short ieta) const
unsigned jscQuietThresholdBarrel() const
unsigned int lookup(unsigned short ecalInput, unsigned short hcalInput, unsigned short fgbit, unsigned short crtNo, unsigned short crdNo, unsigned short twrNo) const