Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:30:30

0001 #include <fstream>
0002 #include <iostream>
0003 #include <memory>
0004 #include <string>
0005 using std::cerr;
0006 using std::cout;
0007 using std::endl;
0008 using std::ostream;
0009 
0010 #include <iomanip>
0011 
0012 #include "FWCore/Framework/interface/EDAnalyzer.h"
0013 #include "FWCore/Framework/interface/Event.h"
0014 #include "FWCore/Framework/interface/Frameworkfwd.h"
0015 #include "FWCore/Framework/interface/MakerMacros.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 
0018 #include "L1Trigger/RegionalCaloTrigger/interface/L1RCTSaveInput.h"
0019 
0020 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
0021 #include "DataFormats/HcalDigi/interface/HcalDigiCollections.h"
0022 #include "DataFormats/L1CaloTrigger/interface/L1CaloCollections.h"
0023 
0024 #include "L1Trigger/RegionalCaloTrigger/interface/L1RCT.h"
0025 #include "L1Trigger/RegionalCaloTrigger/interface/L1RCTLookupTables.h"
0026 
0027 L1RCTSaveInput::L1RCTSaveInput(const edm::ParameterSet &conf)
0028     : fileName(conf.getUntrackedParameter<std::string>("rctTestInputFile")),
0029       rctLookupTables(new L1RCTLookupTables),
0030       rct(new L1RCT(rctLookupTables)),
0031       useEcal(conf.getParameter<bool>("useEcal")),
0032       useHcal(conf.getParameter<bool>("useHcal")),
0033       ecalDigisLabel(conf.getParameter<edm::InputTag>("ecalDigisLabel")),
0034       hcalDigisLabel(conf.getParameter<edm::InputTag>("hcalDigisLabel")),
0035       rctParametersToken_(esConsumes<L1RCTParameters, L1RCTParametersRcd>()),
0036       channelMaskToken_(esConsumes<L1RCTChannelMask, L1RCTChannelMaskRcd>()),
0037       emScaleToken_(esConsumes<L1CaloEtScale, L1EmEtScaleRcd>()),
0038       transcoderToken_(esConsumes<CaloTPGTranscoder, CaloTPGRecord>()),
0039       hcalScaleToken_(esConsumes<L1CaloHcalScale, L1CaloHcalScaleRcd>()),
0040       ecalScaleToken_(esConsumes<L1CaloEcalScale, L1CaloEcalScaleRcd>()),
0041       useDebugTpgScales(conf.getParameter<bool>("useDebugTpgScales")),
0042       tokens_(consumesCollector()) {
0043   ofs.open(fileName.c_str(), std::ios::app);
0044   if (!ofs) {
0045     std::cerr << "Could not create " << fileName << std::endl;
0046     exit(1);
0047   }
0048 }
0049 
0050 L1RCTSaveInput::~L1RCTSaveInput() {
0051   if (rct != nullptr)
0052     delete rct;
0053   if (rctLookupTables != nullptr)
0054     delete rctLookupTables;
0055 }
0056 
0057 void L1RCTSaveInput::analyze(const edm::Event &event, const edm::EventSetup &eventSetup) {
0058   edm::ESHandle<L1RCTParameters> rctParameters = eventSetup.getHandle(rctParametersToken_);
0059   const L1RCTParameters *r = rctParameters.product();
0060   edm::ESHandle<L1RCTChannelMask> channelMask = eventSetup.getHandle(channelMaskToken_);
0061   const L1RCTChannelMask *c = channelMask.product();
0062   edm::ESHandle<L1CaloEtScale> emScale = eventSetup.getHandle(emScaleToken_);
0063   const L1CaloEtScale *s = emScale.product();
0064 
0065   rctLookupTables->setRCTParameters(r);
0066   rctLookupTables->setChannelMask(c);
0067   rctLookupTables->setL1CaloEtScale(s);
0068 
0069   if (useDebugTpgScales) {
0070     // use old-style scales
0071     edm::ESHandle<CaloTPGTranscoder> transcoder = eventSetup.getHandle(transcoderToken_);
0072     const CaloTPGTranscoder *h_tpg = transcoder.product();
0073 
0074     EcalTPGScale e_tpg(tokens_, eventSetup);
0075 
0076     L1CaloEcalScale *ecalScale = new L1CaloEcalScale();
0077     L1CaloHcalScale *hcalScale = new L1CaloHcalScale();
0078 
0079     // create input scales, werner's code
0080     // ECAL
0081     std::cout << "ECAL Pos " << L1CaloEcalScale::nBinRank << std::endl;
0082     for (unsigned short ieta = 1; ieta <= L1CaloEcalScale::nBinEta; ++ieta) {
0083       for (unsigned short irank = 0; irank < L1CaloEcalScale::nBinRank; ++irank) {
0084         std::cout << ieta << " " << irank;
0085         EcalSubdetector subdet = (ieta <= 17) ? EcalBarrel : EcalEndcap;
0086         double etGeVPos = e_tpg.getTPGInGeV(irank,
0087                                             EcalTrigTowerDetId(1,  // +ve eta
0088                                                                subdet,
0089                                                                ieta,
0090                                                                1));  // dummy phi value
0091         ecalScale->setBin(irank, ieta, 1, etGeVPos);
0092         std::cout << etGeVPos << ", ";
0093       }
0094       std::cout << std::endl;
0095     }
0096     std::cout << std::endl;
0097 
0098     std::cout << "ECAL Neg" << std::endl;
0099     for (unsigned short ieta = 1; ieta <= L1CaloEcalScale::nBinEta; ++ieta) {
0100       for (unsigned short irank = 0; irank < L1CaloEcalScale::nBinRank; ++irank) {
0101         EcalSubdetector subdet = (ieta <= 17) ? EcalBarrel : EcalEndcap;
0102 
0103         std::cout << ieta << " " << irank;
0104         double etGeVNeg = e_tpg.getTPGInGeV(irank,
0105                                             EcalTrigTowerDetId(-1,  // -ve eta
0106                                                                subdet,
0107                                                                ieta,
0108                                                                2));  // dummy phi value
0109         ecalScale->setBin(irank, ieta, -1, etGeVNeg);
0110         std::cout << etGeVNeg << ", ";
0111       }
0112       std::cout << std::endl;
0113     }
0114     std::cout << std::endl;
0115 
0116     // HCAL
0117     std::cout << "HCAL" << std::endl;
0118     for (unsigned short ieta = 1; ieta <= L1CaloHcalScale::nBinEta; ++ieta) {
0119       for (unsigned short irank = 0; irank < L1CaloHcalScale::nBinRank; ++irank) {
0120         double etGeV = h_tpg->hcaletValue(ieta, irank);
0121 
0122         hcalScale->setBin(irank, ieta, 1, etGeV);
0123         hcalScale->setBin(irank, ieta, -1, etGeV);
0124         std::cout << etGeV << ", ";
0125         std::cout << std::endl;
0126       }
0127       std::cout << std::endl;
0128     }
0129 
0130     // set the input scales
0131     rctLookupTables->setEcalScale(ecalScale);
0132     rctLookupTables->setHcalScale(hcalScale);
0133 
0134   } else {
0135     edm::ESHandle<L1CaloHcalScale> hcalScale = eventSetup.getHandle(hcalScaleToken_);
0136     const L1CaloHcalScale *h = hcalScale.product();
0137     edm::ESHandle<L1CaloEcalScale> ecalScale = eventSetup.getHandle(ecalScaleToken_);
0138     const L1CaloEcalScale *e = ecalScale.product();
0139 
0140     rctLookupTables->setHcalScale(h);
0141     rctLookupTables->setEcalScale(e);
0142   }
0143 
0144   edm::Handle<EcalTrigPrimDigiCollection> ecal;
0145   edm::Handle<HcalTrigPrimDigiCollection> hcal;
0146   event.getByLabel(ecalDigisLabel, ecal);
0147   event.getByLabel(hcalDigisLabel, hcal);
0148   EcalTrigPrimDigiCollection ecalColl;
0149   HcalTrigPrimDigiCollection hcalColl;
0150   if (ecal.isValid()) {
0151     ecalColl = *ecal;
0152   }
0153   if (hcal.isValid()) {
0154     hcalColl = *hcal;
0155   }
0156   rct->digiInput(ecalColl, hcalColl);
0157   static int nEvents = 0;
0158   if (nEvents == 0) {
0159     ofs << "Crate = 0-17" << std::endl
0160         << "Card = 0-7 within the crate" << std::endl
0161         << "Tower = 0-31 covers 4 x 8 covered by the card" << std::endl
0162         << "EMAddr(0:8) = EMFGBit(0:0)+CompressedEMET(1:8)" << std::endl
0163         << "HDAddr(0:8) = HDFGBit(0:0)+CompressedHDET(1:8) - note: "
0164            "HDFGBit(0:0) is not part of the hardware LUT address"
0165         << std::endl
0166         << "LutOut(0:17)= "
0167            "LinearEMET(0:6)+HoEFGVetoBit(7:7)+LinearJetET(8:16)+ActivityBit(17:"
0168            "17)"
0169         << std::endl
0170         << "Event"
0171         << "\t"
0172         << "Crate"
0173         << "\t"
0174         << "Card"
0175         << "\t"
0176         << "Tower"
0177         << "\t"
0178         << "EMAddr"
0179         << "\t"
0180         << "HDAddr"
0181         << "\t"
0182         << "LUTOut" << std::endl;
0183   }
0184   if (nEvents < 64) {
0185     for (unsigned short iCrate = 0; iCrate < 18; iCrate++) {
0186       for (unsigned short iCard = 0; iCard < 7; iCard++) {
0187         // tower numbered from 0-31
0188         for (unsigned short iTower = 0; iTower < 32; iTower++) {
0189           unsigned short ecal = rct->ecalCompressedET(iCrate, iCard, iTower);
0190           unsigned short hcal = rct->hcalCompressedET(iCrate, iCard, iTower);
0191           unsigned short fgbit = rct->ecalFineGrainBit(iCrate, iCard, iTower);
0192           unsigned short mubit = rct->hcalFineGrainBit(iCrate, iCard, iTower);
0193           unsigned long lutOutput = rctLookupTables->lookup(ecal, hcal, fgbit, iCrate, iCard, iTower);
0194           ofs << std::hex << nEvents << "\t" << iCrate << "\t" << iCard << "\t" << iTower << "\t" << ecal * 2 + fgbit
0195               << "\t" << hcal * 2 + mubit << "\t" << lutOutput << std::dec << std::endl;
0196         }
0197       }
0198     }
0199   }
0200   nEvents++;
0201 }