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