Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:20:18

0001 // L1TCaloStage1LutWriter.cc
0002 // Author: Leonard Apanasevich
0003 //
0004 
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/MakerMacros.h"
0009 #include "FWCore/Framework/interface/ESHandle.h"
0010 #include "FWCore/Utilities/interface/ESGetToken.h"
0011 
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "CondFormats/L1TObjects/interface/CaloParams.h"
0014 #include "CondFormats/DataRecord/interface/L1TCaloParamsRcd.h"
0015 #include "L1Trigger/L1TCalorimeter/interface/CaloParamsHelper.h"
0016 
0017 #include "L1Trigger/L1TCalorimeter/interface/Stage1TauIsolationLUT.h"
0018 
0019 #include <iostream>
0020 #include <fstream>
0021 #include <sys/stat.h>
0022 
0023 //
0024 // class declaration
0025 //
0026 
0027 namespace l1t {
0028 
0029   class L1TCaloStage1LutWriter : public edm::one::EDAnalyzer<> {
0030   public:
0031     explicit L1TCaloStage1LutWriter(const edm::ParameterSet&);
0032     ~L1TCaloStage1LutWriter() override;
0033 
0034     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0035 
0036     void writeIsoTauLut(const std::string& fileName);
0037     bool openOutputFile(const std::string& fileName, std::ofstream& file);
0038 
0039   private:
0040     void beginJob() override;
0041     void analyze(const edm::Event&, const edm::EventSetup&) override;
0042     void endJob() override;
0043 
0044     CaloParamsHelper* m_params;
0045     std::string m_conditionsLabel;
0046 
0047     Stage1TauIsolationLUT* isoTauLut;
0048     edm::ESGetToken<CaloParams, L1TCaloParamsRcd> m_paramsToken;
0049     bool m_writeIsoTauLut;
0050     // output file names
0051     std::string m_isoTauLutName;
0052     // std::string m_EGammaLutName;
0053   };
0054 
0055   //
0056   // constructors and destructor
0057   //
0058   L1TCaloStage1LutWriter::L1TCaloStage1LutWriter(const edm::ParameterSet& iConfig) {
0059     //now do what ever initialization is needed
0060     m_writeIsoTauLut = iConfig.getUntrackedParameter<bool>("writeIsoTauLut", false);
0061     m_isoTauLutName = iConfig.getUntrackedParameter<std::string>("isoTauLutName", "isoTauLut.txt");
0062     m_conditionsLabel = iConfig.getParameter<std::string>("conditionsLabel");
0063     m_paramsToken = esConsumes<CaloParams, L1TCaloParamsRcd>(edm::ESInputTag("", m_conditionsLabel));
0064 
0065     m_params = new CaloParamsHelper;
0066     isoTauLut = new Stage1TauIsolationLUT(m_params);
0067   };
0068 
0069   L1TCaloStage1LutWriter::~L1TCaloStage1LutWriter() { delete isoTauLut; };
0070 
0071   // ------------ method called for each event  ------------
0072   void L1TCaloStage1LutWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0073     edm::ESHandle<CaloParams> paramsHandle = iSetup.getHandle(m_paramsToken);
0074     m_params = new (m_params) CaloParamsHelper(*paramsHandle.product());
0075     if (!m_params) {
0076       std::cout << "Could not retrieve params from Event Setup" << std::endl;
0077       return;
0078     }
0079     LogDebug("L1TDebug") << *m_params << std::endl;
0080 
0081     if (m_writeIsoTauLut)
0082       writeIsoTauLut(m_isoTauLutName);
0083   }
0084 
0085   bool L1TCaloStage1LutWriter::openOutputFile(const std::string& fileName, std::ofstream& file) {
0086     // Write to a new file
0087     struct stat buffer;
0088     if (!stat(fileName.c_str(), &buffer)) {
0089       std::cout << "File " << fileName << " already exists. It will not be overwritten." << std::endl;
0090       return false;
0091     } else {
0092       file.open(fileName.c_str());
0093       if (!file.good()) {
0094         std::cout << "Error opening file " << fileName << std::endl;
0095         return false;
0096       }
0097     }
0098     return true;
0099   }
0100 
0101   void L1TCaloStage1LutWriter::writeIsoTauLut(const std::string& fileName) {
0102     std::ofstream file;
0103     if (openOutputFile(fileName, file)) {
0104       std::cout << "Writing tau isolation LUT to: " << fileName << std::endl;
0105       file << "########################################\n"
0106            << "# tauIsolation LUT for ISOL(A)= " << m_params->tauMaxJetIsolationA()
0107            << "  ISOL(B)= " << m_params->tauMaxJetIsolationB() << "\n"
0108            << "# Switch to ISOLB value at pt= " << m_params->tauMinPtJetIsolationB() << "\n"
0109            << "#<header> V" << Stage1TauIsolationLUT::lut_version << " "
0110            << Stage1TauIsolationLUT::nbitsJet + Stage1TauIsolationLUT::nbitsTau << " "
0111            << Stage1TauIsolationLUT::nbits_data << " </header>\n"
0112            << "# Format:  Address  Payload  ## hwTauPt hwJetPt\n"
0113            << "########################################\n";
0114 
0115       unsigned maxAdd = pow(2, Stage1TauIsolationLUT::nbitsJet + Stage1TauIsolationLUT::nbitsTau);
0116       for (unsigned iAdd = 0; iAdd < maxAdd; iAdd++) {
0117         int isoFlag = isoTauLut->lutPayload(iAdd);
0118         file << iAdd << " " << isoFlag << "\n";
0119       }
0120     } else {
0121       std::cout << "%Error opening output file. Tau isolation LUT not written." << std::endl;
0122     }
0123     file.close();
0124   }
0125   // ------------ method called once each job just before starting event loop  ------------
0126   void L1TCaloStage1LutWriter::beginJob() {}
0127 
0128   // ------------ method called once each job just after ending the event loop  ------------
0129   void L1TCaloStage1LutWriter::endJob() {}
0130 
0131   // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0132   void L1TCaloStage1LutWriter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0133     //The following says we do not know what parameters are allowed so do no validation
0134     // Please change this to state exactly what you do use, even if it is no parameters
0135     edm::ParameterSetDescription desc;
0136     desc.setUnknown();
0137     descriptions.addDefault(desc);
0138   }
0139 
0140 }  // namespace l1t
0141 using namespace l1t;
0142 
0143 //define this as a plug-in
0144 DEFINE_FWK_MODULE(L1TCaloStage1LutWriter);