Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-12-20 03:13:53

0001 #include "L1Trigger/GlobalCaloTrigger/plugins/L1GctPrintLuts.h"
0002 
0003 #include "FWCore/PluginManager/interface/ModuleDef.h"
0004 #include "FWCore/Framework/interface/MakerMacros.h"
0005 #include "FWCore/Framework/interface/ESHandle.h"
0006 
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 
0009 // GCT include files
0010 #include "L1Trigger/GlobalCaloTrigger/interface/L1GctJetEtCalibrationLut.h"
0011 #include "L1Trigger/GlobalCaloTrigger/interface/L1GctWheelJetFpga.h"
0012 #include "L1Trigger/GlobalCaloTrigger/interface/L1GctGlobalEnergyAlgos.h"
0013 #include "L1Trigger/GlobalCaloTrigger/interface/L1GctGlobalHfSumAlgos.h"
0014 #include "L1Trigger/GlobalCaloTrigger/interface/L1GctHfBitCountsLut.h"
0015 #include "L1Trigger/GlobalCaloTrigger/interface/L1GctHtMissLut.h"
0016 #include "L1Trigger/GlobalCaloTrigger/interface/L1GctHfEtSumsLut.h"
0017 #include "L1Trigger/GlobalCaloTrigger/interface/L1GlobalCaloTrigger.h"
0018 
0019 #include <fstream>
0020 #include <iostream>
0021 #include <memory>
0022 #include <sys/stat.h>
0023 
0024 L1GctPrintLuts::L1GctPrintLuts(const edm::ParameterSet& iConfig)
0025     : m_jetRanksOutFileName(iConfig.getUntrackedParameter<std::string>("jetRanksFilename", "gctJetRanksContents.txt")),
0026       m_hfSumLutOutFileName(iConfig.getUntrackedParameter<std::string>("hfSumLutFilename", "gctHfSumLutContents.txt")),
0027       m_htMissLutOutFileName(
0028           iConfig.getUntrackedParameter<std::string>("htMissLutFilename", "gctHtMissLutContents.txt")),
0029       m_gct(new L1GlobalCaloTrigger(L1GctJetLeafCard::hardwareJetFinder)),
0030       m_jetEtCalibLuts() {
0031   // Fill the jetEtCalibLuts vector
0032   lutPtr nextLut(new L1GctJetEtCalibrationLut());
0033 
0034   for (unsigned ieta = 0; ieta < L1GctJetFinderBase::COL_OFFSET; ieta++) {
0035     nextLut->setEtaBin(ieta);
0036     m_jetEtCalibLuts.push_back(nextLut);
0037     nextLut = std::make_shared<L1GctJetEtCalibrationLut>();
0038   }
0039   m_jfParsToken = esConsumes<L1GctJetFinderParams, L1GctJetFinderParamsRcd>();
0040   m_etScaleToken = esConsumes<L1CaloEtScale, L1JetEtScaleRcd>();
0041   m_htMissScaleToken = esConsumes<L1CaloEtScale, L1HtMissScaleRcd>();
0042   m_hfRingEtScaleToken = esConsumes<L1CaloEtScale, L1HfRingEtScaleRcd>();
0043 }
0044 
0045 L1GctPrintLuts::~L1GctPrintLuts() {
0046   // do anything here that needs to be done at desctruction time
0047   // (e.g. close files, deallocate resources etc.)
0048 }
0049 
0050 //
0051 // member functions
0052 //
0053 
0054 // ------------ method called to for each event  ------------
0055 void L1GctPrintLuts::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0056   // get config data from EventSetup
0057   // check this has been done successfully before proceeding
0058   if (configureGct(iSetup) == 0) {
0059     // Write to a new file
0060     struct stat buffer;
0061     if (!stat(m_jetRanksOutFileName.c_str(), &buffer)) {
0062       edm::LogWarning("LutFileExists") << "File " << m_jetRanksOutFileName
0063                                        << " already exists. It will not be overwritten." << std::endl;
0064     } else {
0065       std::ofstream file;
0066       file.open(m_jetRanksOutFileName.c_str());
0067 
0068       if (file.good()) {
0069         // Print the calibration lut contents
0070         file << " Gct lookup table printout \n"
0071              << "===========================\n\n"
0072              << "Jet Et Calibration lut contents\n"
0073              << std::endl;
0074         for (unsigned ieta = 0; ieta < m_jetEtCalibLuts.size(); ieta++) {
0075           file << *m_jetEtCalibLuts.at(ieta) << std::endl;
0076         }
0077       } else {
0078         edm::LogWarning("LutFileError") << "Error opening file " << m_jetRanksOutFileName
0079                                         << ". No lookup tables written." << std::endl;
0080       }
0081       file.close();
0082     }
0083 
0084     if (!stat(m_hfSumLutOutFileName.c_str(), &buffer)) {
0085       edm::LogWarning("LutFileExists") << "File " << m_hfSumLutOutFileName
0086                                        << " already exists. It will not be overwritten." << std::endl;
0087     } else {
0088       std::ofstream file;
0089       file.open(m_hfSumLutOutFileName.c_str());
0090 
0091       if (file.good()) {
0092         // Print the Hf luts
0093         file << "\n\n Hf ring jet bit count luts:" << std::endl;
0094         file << "\n Positive eta, ring1" << std::endl;
0095         file << *m_gct->getEnergyFinalStage()->getHfSumProcessor()->getBCLut(L1GctHfEtSumsLut::bitCountPosEtaRing1)
0096              << std::endl;
0097         file << "\n Positive eta, ring2" << std::endl;
0098         file << *m_gct->getEnergyFinalStage()->getHfSumProcessor()->getBCLut(L1GctHfEtSumsLut::bitCountPosEtaRing2)
0099              << std::endl;
0100         file << "\n Negative eta, ring1" << std::endl;
0101         file << *m_gct->getEnergyFinalStage()->getHfSumProcessor()->getBCLut(L1GctHfEtSumsLut::bitCountNegEtaRing1)
0102              << std::endl;
0103         file << "\n Negative eta, ring2" << std::endl;
0104         file << *m_gct->getEnergyFinalStage()->getHfSumProcessor()->getBCLut(L1GctHfEtSumsLut::bitCountNegEtaRing2)
0105              << std::endl;
0106         file << "\n\n Hf Et sum luts:" << std::endl;
0107         file << "\n Positive eta, ring1" << std::endl;
0108         file << *m_gct->getEnergyFinalStage()->getHfSumProcessor()->getESLut(L1GctHfEtSumsLut::etSumPosEtaRing1)
0109              << std::endl;
0110         file << "\n Positive eta, ring2" << std::endl;
0111         file << *m_gct->getEnergyFinalStage()->getHfSumProcessor()->getESLut(L1GctHfEtSumsLut::etSumPosEtaRing2)
0112              << std::endl;
0113         file << "\n Negative eta, ring1" << std::endl;
0114         file << *m_gct->getEnergyFinalStage()->getHfSumProcessor()->getESLut(L1GctHfEtSumsLut::etSumNegEtaRing1)
0115              << std::endl;
0116         file << "\n Negative eta, ring2" << std::endl;
0117         file << *m_gct->getEnergyFinalStage()->getHfSumProcessor()->getESLut(L1GctHfEtSumsLut::etSumNegEtaRing2)
0118              << std::endl;
0119       } else {
0120         edm::LogWarning("LutFileError") << "Error opening file " << m_hfSumLutOutFileName
0121                                         << ". No lookup tables written." << std::endl;
0122       }
0123       file.close();
0124     }
0125 
0126     if (!stat(m_htMissLutOutFileName.c_str(), &buffer)) {
0127       edm::LogWarning("LutFileExists") << "File " << m_htMissLutOutFileName
0128                                        << " already exists. It will not be overwritten." << std::endl;
0129     } else {
0130       std::ofstream file;
0131       file.open(m_htMissLutOutFileName.c_str());
0132 
0133       if (file.good()) {
0134         // Print the HtMiss luts
0135         file << "\n\n missing Ht lut:" << std::endl;
0136         file << *m_gct->getEnergyFinalStage()->getHtMissLut() << std::endl;
0137       } else {
0138         edm::LogWarning("LutFileError") << "Error opening file " << m_hfSumLutOutFileName
0139                                         << ". No lookup tables written." << std::endl;
0140       }
0141       file.close();
0142     }
0143   }
0144 }
0145 
0146 // ------------ method called once each job just before starting event loop  ------------
0147 // This is where we work ...
0148 void L1GctPrintLuts::beginJob() {}
0149 
0150 // The configuration method for the Gct - copied from L1GctEmulator
0151 int L1GctPrintLuts::configureGct(const edm::EventSetup& c) {
0152   int success = 0;
0153 
0154   if (success == 0) {
0155     // get data from EventSetup
0156     edm::ESHandle<L1GctJetFinderParams> jfPars = c.getHandle(m_jfParsToken);
0157     edm::ESHandle<L1CaloEtScale> hfRingEtScale = c.getHandle(m_hfRingEtScaleToken);
0158     edm::ESHandle<L1CaloEtScale> htMissScale = c.getHandle(m_htMissScaleToken);
0159     edm::ESHandle<L1CaloEtScale> etScale = c.getHandle(m_etScaleToken);
0160 
0161     if (jfPars.product() == nullptr) {
0162       success = -1;
0163       edm::LogWarning("L1GctConfigFailure")
0164           << "Failed to find a L1GctJetFinderParamsRcd:L1GctJetFinderParams in EventSetup!" << std::endl;
0165     }
0166 
0167     if (hfRingEtScale.product() == nullptr) {
0168       success = -1;
0169       edm::LogWarning("L1GctConfigFailure")
0170           << "Failed to find a L1HfRingEtScaleRcd:L1CaloEtScale in EventSetup!" << std::endl;
0171     }
0172 
0173     if (htMissScale.product() == nullptr) {
0174       success = -1;
0175       edm::LogWarning("L1GctConfigFailure")
0176           << "Failed to find a L1HtMissScaleRcd:L1CaloEtScale in EventSetup!" << std::endl;
0177     }
0178 
0179     //     if (chanMask.product() == 0) {
0180     //       success = -1;
0181     //       edm::LogWarning("L1GctConfigFailure")
0182     //  << "Failed to find a L1GctChannelMaskRcd:L1GctChannelMask in EventSetup!" << std::endl;
0183     //     }
0184 
0185     if (success == 0) {
0186       // tell the jet Et Luts about the scales
0187       for (unsigned ieta = 0; ieta < m_jetEtCalibLuts.size(); ieta++) {
0188         m_jetEtCalibLuts.at(ieta)->setFunction(jfPars.product());
0189         m_jetEtCalibLuts.at(ieta)->setOutputEtScale(etScale.product());
0190       }
0191 
0192       // pass all the setup info to the gct
0193       m_gct->setJetEtCalibrationLuts(m_jetEtCalibLuts);
0194       m_gct->setJetFinderParams(jfPars.product());
0195       m_gct->setHtMissScale(htMissScale.product());
0196       m_gct->setupHfSumLuts(hfRingEtScale.product());
0197       //      m_gct->setChannelMask(chanMask.product());
0198     }
0199   }
0200 
0201   if (success != 0) {
0202     edm::LogError("L1GctConfigError") << "Configuration failed - GCT emulator will not be run" << std::endl;
0203   }
0204   return success;
0205 }
0206 
0207 // ------------ method called once each job just after ending the event loop  ------------
0208 void L1GctPrintLuts::endJob() {}
0209 
0210 DEFINE_FWK_MODULE(L1GctPrintLuts);