00001 #include "L1Trigger/RegionalCaloTrigger/interface/L1RCTLutWriter.h"
00002
00003 #include "L1Trigger/RegionalCaloTrigger/interface/L1RCTLookupTables.h"
00004
00005 #include "CondFormats/L1TObjects/interface/L1RCTParameters.h"
00006 #include "CondFormats/DataRecord/interface/L1RCTParametersRcd.h"
00007 #include "CondFormats/L1TObjects/interface/L1RCTChannelMask.h"
00008 #include "CondFormats/DataRecord/interface/L1RCTChannelMaskRcd.h"
00009 #include "CondFormats/L1TObjects/interface/L1RCTNoisyChannelMask.h"
00010 #include "CondFormats/DataRecord/interface/L1RCTNoisyChannelMaskRcd.h"
00011
00012
00013 #include "CondFormats/DataRecord/interface/L1CaloEcalScaleRcd.h"
00014 #include "CondFormats/L1TObjects/interface/L1CaloEcalScale.h"
00015 #include "CondFormats/DataRecord/interface/L1CaloHcalScaleRcd.h"
00016 #include "CondFormats/L1TObjects/interface/L1CaloHcalScale.h"
00017
00018
00019 #include "CalibCalorimetry/EcalTPGTools/interface/EcalTPGScale.h"
00020 #include "CalibFormats/CaloTPG/interface/CaloTPGTranscoder.h"
00021 #include "CalibFormats/CaloTPG/interface/CaloTPGRecord.h"
00022
00023 #include "CondFormats/DataRecord/interface/L1EmEtScaleRcd.h"
00024 #include "CondFormats/L1TObjects/interface/L1CaloEtScale.h"
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 L1RCTLutWriter::L1RCTLutWriter(const edm::ParameterSet& iConfig) :
00038 lookupTable_(new L1RCTLookupTables),
00039 keyName_(iConfig.getParameter<std::string>("key")),
00040 useDebugTpgScales_(iConfig.getParameter<bool>("useDebugTpgScales"))
00041 {
00042
00043
00044 }
00045
00046
00047 L1RCTLutWriter::~L1RCTLutWriter()
00048 {
00049
00050
00051
00052
00053 if (lookupTable_ != 0) delete lookupTable_;
00054 }
00055
00056
00057
00058
00059
00060
00061
00062 void
00063 L1RCTLutWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00064 {
00065
00066
00067
00068 edm::ESHandle<L1RCTParameters> rctParameters;
00069 iSetup.get<L1RCTParametersRcd>().get(rctParameters);
00070 rctParameters_ = rctParameters.product();
00071 edm::ESHandle<L1CaloEtScale> emScale;
00072 iSetup.get<L1EmEtScaleRcd>().get(emScale);
00073 const L1CaloEtScale* s = emScale.product();
00074
00075
00076
00077 L1RCTChannelMask* m = new L1RCTChannelMask;
00078 for (int i = 0; i < 18; i++)
00079 {
00080 for (int j = 0; j < 2; j++)
00081 {
00082 for (int k = 0; k < 28; k++)
00083 {
00084 m->ecalMask[i][j][k] = false;
00085 m->hcalMask[i][j][k] = false;
00086 }
00087 for (int k = 0; k < 4; k++)
00088 {
00089 m->hfMask[i][j][k] = false;
00090 }
00091 }
00092 }
00093
00094
00095
00096
00097
00098 L1RCTNoisyChannelMask* m2 = new L1RCTNoisyChannelMask;
00099 for (int i = 0; i < 18; i++)
00100 {
00101 for (int j = 0; j < 2; j++)
00102 {
00103 for (int k = 0; k < 28; k++)
00104 {
00105 m2->ecalMask[i][j][k] = false;
00106 m2->hcalMask[i][j][k] = false;
00107 }
00108 for (int k = 0; k < 4; k++)
00109 {
00110 m2->hfMask[i][j][k] = false;
00111 }
00112 }
00113 }
00114 m2->ecalThreshold = 0.0;
00115 m2->hcalThreshold = 0.0;
00116 m2->hfThreshold = 0.0;
00117
00118
00119
00120
00121
00122 L1CaloEcalScale* dummyE(0);
00123 L1CaloHcalScale* dummyH(0);
00124
00125 if (useDebugTpgScales_)
00126 {
00127
00128 std::cout << "Using old-style TPG scales!" << std::endl;
00129
00130
00131 edm::ESHandle<CaloTPGTranscoder> transcoder;
00132 iSetup.get<CaloTPGRecord>().get(transcoder);
00133 const CaloTPGTranscoder* h_tpg = transcoder.product();
00134
00135
00136 EcalTPGScale* e_tpg = new EcalTPGScale();
00137 e_tpg->setEventSetup(iSetup);
00138
00139 L1CaloEcalScale* ecalScale = new L1CaloEcalScale();
00140 L1CaloHcalScale* hcalScale = new L1CaloHcalScale();
00141
00142
00143
00144
00145 for( unsigned short ieta = 1 ; ieta <= L1CaloEcalScale::nBinEta; ++ieta )
00146 {
00147 for( unsigned short irank = 0 ; irank < L1CaloEcalScale::nBinRank; ++irank )
00148 {
00149 EcalSubdetector subdet = ( ieta <= 17 ) ? EcalBarrel : EcalEndcap ;
00150 double etGeVPos =
00151 e_tpg->getTPGInGeV
00152 ( irank, EcalTrigTowerDetId(1,
00153 subdet,
00154 ieta,
00155 1 ));
00156 ecalScale->setBin( irank, ieta, 1, etGeVPos ) ;
00157 }
00158 }
00159
00160 for( unsigned short ieta = 1 ; ieta <= L1CaloEcalScale::nBinEta; ++ieta )
00161 {
00162 for( unsigned short irank = 0 ; irank < L1CaloEcalScale::nBinRank; ++irank )
00163 {
00164 EcalSubdetector subdet = ( ieta <= 17 ) ? EcalBarrel : EcalEndcap ;
00165
00166 double etGeVNeg =
00167 e_tpg->getTPGInGeV
00168 ( irank,
00169 EcalTrigTowerDetId(-1,
00170 subdet,
00171 ieta,
00172 2 ));
00173 ecalScale->setBin( irank, ieta, -1, etGeVNeg ) ;
00174 }
00175 }
00176
00177
00178 for( unsigned short ieta = 1 ; ieta <= L1CaloHcalScale::nBinEta; ++ieta )
00179 {
00180 for( unsigned short irank = 0 ; irank < L1CaloHcalScale::nBinRank; ++irank )
00181 {
00182 double etGeV = h_tpg->hcaletValue( ieta, irank ) ;
00183
00184 hcalScale->setBin( irank, ieta, 1, etGeV ) ;
00185 hcalScale->setBin( irank, ieta, -1, etGeV ) ;
00186 }
00187 }
00188
00189
00190 lookupTable_->setEcalScale(ecalScale);
00191 lookupTable_->setHcalScale(hcalScale);
00192
00193 dummyE = ecalScale;
00194 dummyH = hcalScale;
00195
00196 delete e_tpg;
00197
00198 }
00199 else
00200 {
00201
00202
00203 edm::ESHandle<L1CaloEcalScale> ecalScale;
00204 iSetup.get<L1CaloEcalScaleRcd>().get(ecalScale);
00205 const L1CaloEcalScale* e = ecalScale.product();
00206
00207
00208 edm::ESHandle<L1CaloHcalScale> hcalScale;
00209 iSetup.get<L1CaloHcalScaleRcd>().get(hcalScale);
00210 const L1CaloHcalScale* h = hcalScale.product();
00211
00212
00213 lookupTable_->setEcalScale(e);
00214 lookupTable_->setHcalScale(h);
00215
00216 }
00217
00218 lookupTable_->setRCTParameters(rctParameters_);
00219 lookupTable_->setChannelMask(m);
00220 lookupTable_->setNoisyChannelMask(m2);
00221
00222
00223 lookupTable_->setL1CaloEtScale(s);
00224
00225 for (unsigned short nCard = 0; nCard <= 6; nCard = nCard + 2)
00226 {
00227 writeRcLutFile(nCard);
00228 writeEicLutFile(nCard);
00229 }
00230 writeJscLutFile();
00231
00232 unsigned int eicThreshold = rctParameters_->eicIsolationThreshold();
00233 unsigned int jscThresholdBarrel = rctParameters_->jscQuietThresholdBarrel();
00234 unsigned int jscThresholdEndcap = rctParameters_->jscQuietThresholdEndcap();
00235 writeThresholdsFile(eicThreshold, jscThresholdBarrel, jscThresholdEndcap);
00236
00237 if (dummyE != 0) delete dummyE;
00238 if (dummyH != 0) delete dummyH;
00239
00240 }
00241
00242
00243
00244
00245
00246 void
00247 L1RCTLutWriter::endJob() {
00248 }
00249
00250
00251
00252 void
00253 L1RCTLutWriter::writeRcLutFile(unsigned short card)
00254 {
00255
00256
00257 char filename[256];
00258 char command[64];
00259 if (card != 6)
00260 {
00261 int card2 = card + 1;
00262 sprintf(filename,"RC%i%i-%s.dat",card,card2,keyName_.c_str() );
00263
00264 }
00265 else
00266 {
00267 sprintf(filename,"RC6-%s.dat",keyName_.c_str() );
00268
00269 }
00270
00271 lutFile_.open(filename, std::ios::trunc);
00272 lutFile_ << "Emulator-parameter generated lut file, card "
00273 << card << " key " << keyName_ << " ";
00274
00275
00276 lutFile_.close();
00277 sprintf(command, "date >> %s", filename);
00278 system(command);
00279
00280
00281 lutFile_.open(filename, std::ios::app);
00282
00283 unsigned long data = 0;
00284
00285
00286
00287
00288
00289 for (unsigned short nLUT = 0; nLUT < 8; nLUT++)
00290 {
00291
00292 unsigned short iAbsEta = 0;
00293 if (card != 6)
00294 {
00295 iAbsEta = (card/2)*8 + nLUT + 1;
00296 }
00297 else
00298 {
00299 if (nLUT < 4)
00300 {
00301 iAbsEta = (card/2)*8 + nLUT + 1;
00302 }
00303 else
00304 {
00305 iAbsEta = (card/2)*8 + (3 - (nLUT%4) ) + 1;
00306 }
00307
00308 }
00309
00310
00311
00312
00313
00314 unsigned short crate = rctParameters_->calcCrate(1, iAbsEta);
00315 unsigned short tower = rctParameters_->calcTower(1, iAbsEta);
00316
00317
00318
00319 for (unsigned int hcalEt = 0; hcalEt < 256; hcalEt++)
00320 {
00321
00322 for (unsigned short ecalfg = 0; ecalfg < 2; ecalfg++)
00323 {
00324
00325 for (unsigned int ecalEt = 0; ecalEt < 256; ecalEt++)
00326 {
00327
00328 unsigned long output = lookupTable_->
00329 lookup(ecalEt, hcalEt, ecalfg, crate, card, tower);
00330 unsigned short etIn9Bits = (output>>8)&511;
00331 unsigned short tauActivityBit = (output>>17)&1;
00332 data = (tauActivityBit<<9)+etIn9Bits;
00333 lutFile_ << std::hex << data << std::dec << std::endl;
00334 }
00335 }
00336 }
00337
00338
00339 for (unsigned int hcalEt = 0; hcalEt < 256; hcalEt++)
00340 {
00341
00342 for (unsigned short ecalfg = 0; ecalfg < 2; ecalfg++)
00343 {
00344
00345 for (unsigned int ecalEt = 0; ecalEt < 256; ecalEt++)
00346 {
00347
00348 unsigned long output = lookupTable_->
00349 lookup(ecalEt, hcalEt, ecalfg, crate, card, tower);
00350 unsigned short etIn7Bits = output&127;
00351 unsigned short heFgVetoBit = (output>>7)&1;
00352 data = (heFgVetoBit<<7)+etIn7Bits;
00353 lutFile_ << std::hex << data << std::dec << std::endl;
00354 }
00355 }
00356 }
00357 }
00358
00359 lutFile_.close();
00360 return;
00361
00362 }
00363
00364
00365 void
00366 L1RCTLutWriter::writeEicLutFile(unsigned short card)
00367 {
00368
00369 char filename[256];
00370 char command[64];
00371 if (card != 6)
00372 {
00373 int card2 = card + 1;
00374 sprintf(filename,"EIC%i%i-%s.dat", card, card2, keyName_.c_str() );
00375 }
00376 else
00377 {
00378 sprintf(filename,"EIC6-%s.dat", keyName_.c_str() );
00379 }
00380
00381 lutFile_.open(filename, std::ios::trunc);
00382 lutFile_ << "Emulator-parameter generated EIC lut file, card "
00383 << card << " key " << keyName_ << " ";
00384
00385 lutFile_.close();
00386 sprintf(command, "date >> %s", filename);
00387 system(command);
00388
00389
00390 lutFile_.open(filename, std::ios::app);
00391
00392 unsigned long data = 0;
00393
00394
00395
00396
00397
00398 for (int etIn7Bits = 0; etIn7Bits < 128; etIn7Bits++)
00399 {
00400 data = lookupTable_->emRank(etIn7Bits);
00401 if (data > 0x3f)
00402 {
00403 data = 0x3f;
00404 }
00405 lutFile_ << std::hex << data << std::dec << std::endl;
00406 }
00407 lutFile_.close();
00408 return;
00409 }
00410
00411
00412 void
00413 L1RCTLutWriter::writeJscLutFile()
00414 {
00415 char filename[256];
00416 char command[64];
00417 sprintf(filename, "JSC-%s.dat", keyName_.c_str() );
00418
00419
00420 lutFile_.open(filename, std::ios::trunc);
00421 lutFile_ << "Emulator parameter-generated lut file, key " << keyName_
00422 << " ";
00423
00424 lutFile_.close();
00425 sprintf(command, "date >> %s", filename);
00426 system(command);
00427
00428 lutFile_.open(filename, std::ios::app);
00429
00430 unsigned long data = 0;
00431 unsigned long data0 = 0;
00432 unsigned long data1 = 0;
00433
00434
00435
00436
00437
00438 for (int lutbits = 0; lutbits < 4; lutbits++)
00439 {
00440
00441 for (unsigned int phi1et = 0; phi1et < 256; phi1et++)
00442 {
00443
00444 for (unsigned int phi0et = 0; phi0et < 256; phi0et++)
00445 {
00446
00447
00448
00449
00450
00451 data0 = lookupTable_->lookup(phi0et, 0, 999, lutbits);
00452 if (data0 > 0xff)
00453 {
00454 data0 = 0xff;
00455 }
00456 data1 = lookupTable_->lookup(phi1et, 0, 999, lutbits);
00457 if (data1 > 0xff)
00458 {
00459 data1 = 0xff;
00460 }
00461 data = (data1<<8) + (data0);
00462 lutFile_ << std::hex << data << std::dec << std::endl;
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472 }
00473 }
00474 }
00475
00476 lutFile_.close();
00477 return;
00478 }
00479
00480
00481 void
00482 L1RCTLutWriter::writeThresholdsFile(unsigned int eicThreshold,
00483 unsigned int jscThresholdBarrel,
00484 unsigned int jscThresholdEndcap)
00485 {
00486
00487 std::ofstream thresholdsFile;
00488 char filename[256];
00489 sprintf(filename, "Thresholds-%s.dat", keyName_.c_str() );
00490 thresholdsFile.open(filename, std::ios::trunc);
00491
00492 thresholdsFile << "key is " << keyName_ << std::endl << std::endl;
00493 thresholdsFile << "eicIsolationThreshold " << eicThreshold << std::endl;
00494 thresholdsFile << "jscQuietThresholdBarrel " << jscThresholdBarrel << std::endl;
00495 thresholdsFile << "jscQuietThresholdEndcap " << jscThresholdEndcap << std::endl;
00496
00497 thresholdsFile.close();
00498 }
00499
00500
00501