Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:23:25

0001 #include <memory>
0002 #include <vector>
0003 #include <iostream>
0004 
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 #include "FWCore/Framework/interface/EDAnalyzer.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/ESHandle.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 
0011 // #include "L1Trigger/L1TMuonEndCap/interface/PtAssignmentEngine2016.h"
0012 #include "L1Trigger/L1TMuonEndCap/interface/PtAssignmentEngine2017.h"
0013 #include "L1Trigger/L1TMuonEndCap/interface/PtLUTWriter.h"
0014 
0015 #include "helper.h"
0016 #include "progress_bar.h"
0017 
0018 class MakePtLUT : public edm::EDAnalyzer {
0019 public:
0020   explicit MakePtLUT(const edm::ParameterSet&);
0021   virtual ~MakePtLUT();
0022 
0023 private:
0024   //virtual void beginJob();
0025   //virtual void endJob();
0026 
0027   //virtual void beginRun(const edm::Run&, const edm::EventSetup&);
0028   //virtual void endRun(const edm::Run&, const edm::EventSetup&);
0029 
0030   virtual void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup);
0031 
0032   void makeLUT();
0033 
0034   void checkAddresses();
0035 
0036 private:
0037   std::unique_ptr<PtAssignmentEngine> pt_assign_engine_;
0038 
0039   PtLUTWriter ptlut_writer_;
0040 
0041   const edm::ParameterSet config_;
0042 
0043   int verbose_;
0044   int num_;
0045   int denom_;
0046 
0047   std::string xml_dir_;
0048   std::string outfile_;
0049 
0050   bool onlyCheck_;
0051   std::vector<unsigned long long> addressesToCheck_;
0052 
0053   bool done_;
0054 };
0055 
0056 // _____________________________________________________________________________
0057 #define PTLUT_SIZE (1 << 30)
0058 
0059 MakePtLUT::MakePtLUT(const edm::ParameterSet& iConfig)
0060     :  // pt_assign_engine_(new PtAssignmentEngine2016()),
0061       pt_assign_engine_(new PtAssignmentEngine2017()),
0062       ptlut_writer_(),
0063       config_(iConfig),
0064       verbose_(iConfig.getUntrackedParameter<int>("verbosity")),
0065       num_(iConfig.getParameter<int>("numerator")),
0066       denom_(iConfig.getParameter<int>("denominator")),
0067       outfile_(iConfig.getParameter<std::string>("outfile")),
0068       onlyCheck_(iConfig.getParameter<bool>("onlyCheck")),
0069       addressesToCheck_(iConfig.getParameter<std::vector<unsigned long long> >("addressesToCheck")),
0070       done_(false) {
0071   auto ptLUTVersion = iConfig.getParameter<int>("PtLUTVersion");
0072 
0073   const edm::ParameterSet spPAParams16 = config_.getParameter<edm::ParameterSet>("spPAParams16");
0074   auto bdtXMLDir = spPAParams16.getParameter<std::string>("BDTXMLDir");
0075   auto readPtLUTFile = spPAParams16.getParameter<bool>("ReadPtLUTFile");
0076   auto fixMode15HighPt = spPAParams16.getParameter<bool>("FixMode15HighPt");
0077   auto bug9BitDPhi = spPAParams16.getParameter<bool>("Bug9BitDPhi");
0078   auto bugMode7CLCT = spPAParams16.getParameter<bool>("BugMode7CLCT");
0079   auto bugNegPt = spPAParams16.getParameter<bool>("BugNegPt");
0080 
0081   ptlut_writer_.set_version(ptLUTVersion);
0082 
0083   pt_assign_engine_->configure(verbose_, readPtLUTFile, fixMode15HighPt, bug9BitDPhi, bugMode7CLCT, bugNegPt);
0084 
0085   xml_dir_ = bdtXMLDir;
0086 }
0087 
0088 MakePtLUT::~MakePtLUT() {}
0089 
0090 void MakePtLUT::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0091   if (done_)
0092     return;
0093 
0094   if (onlyCheck_) {
0095     checkAddresses();
0096 
0097   } else {
0098     makeLUT();
0099   }
0100 
0101   done_ = true;
0102   return;
0103 }
0104 
0105 void MakePtLUT::makeLUT() {
0106   // Load XMLs inside function
0107   std::cout << "Inside makeLUT() - loading XMLs" << std::endl;
0108   pt_assign_engine_->read(config_.getParameter<int>("PtLUTVersion"), xml_dir_);
0109 
0110   std::cout << "Calculating pT for " << PTLUT_SIZE / denom_ << " addresses, please sit tight..." << std::endl;
0111 
0112   if (num_ - 1 < 0)
0113     std::cout << "ERROR: tried to fill address < 0.  KILL!!!" << std::endl;
0114   PtLUTWriter::address_t address = abs((num_ - 1) * (PTLUT_SIZE / denom_));
0115 
0116   float xmlpt = 0.;
0117   float pt = 0.;
0118   int gmt_pt = 0;
0119 
0120   for (; address < (PtLUTWriter::address_t)abs(num_ * (PTLUT_SIZE / denom_)); ++address) {
0121     if (address % (PTLUT_SIZE / (denom_ * 128)) == 0)
0122       show_progress_bar(address, PTLUT_SIZE);
0123 
0124     //int mode_inv = (address >> (30-4)) & ((1<<4)-1);
0125 
0126     // floats
0127     xmlpt = pt_assign_engine_->calculate_pt(address);
0128     pt = (xmlpt < 0.) ? 1. : xmlpt;             // Matt used fabs(-1) when mode is invalid
0129     pt *= pt_assign_engine_->scale_pt(pt, 15);  // Multiply by some factor to achieve 90% efficiency at threshold
0130 
0131     // integers
0132     gmt_pt = (pt * 2) + 1;
0133     gmt_pt = (gmt_pt > 511) ? 511 : gmt_pt;
0134 
0135     //if (address % (1<<20) == 0)
0136     //  std::cout << mode_inv << " " << address << " " << print_subaddresses(address) << " " << gmt_pt << std::endl;
0137 
0138     ptlut_writer_.push_back(gmt_pt);
0139   }
0140 
0141   std::cout << "\nAbout to write file " << outfile_ << " for part " << num_ << "/" << denom_ << std::endl;
0142   ptlut_writer_.write(outfile_, num_, denom_);
0143   std::cout << "Wrote file! DONE!" << std::endl;
0144 }
0145 
0146 void MakePtLUT::checkAddresses() {
0147   unsigned int n = addressesToCheck_.size();
0148   std::cout << "Calculating pT for " << n << " addresses, please sit tight..." << std::endl;
0149 
0150   PtLUTWriter::address_t address = 0;
0151 
0152   float xmlpt = 0.;
0153   float pt = 0.;
0154   int gmt_pt = 0;
0155 
0156   for (unsigned int i = 0; i < n; ++i) {
0157     //show_progress_bar(i, n);
0158 
0159     address = addressesToCheck_.at(i);
0160 
0161     int mode_inv = (address >> (30 - 4)) & ((1 << 4) - 1);
0162 
0163     // floats
0164     xmlpt = pt_assign_engine_->calculate_pt(address);
0165     pt = (xmlpt < 0.) ? 1. : xmlpt;  // Matt used fabs(-1) when mode is invalid
0166     pt *= 1.4;  // multiply by 1.4 to keep efficiency above 90% when the L1 trigger pT cut is applied
0167 
0168     // integers
0169     gmt_pt = (pt * 2) + 1;
0170     gmt_pt = (gmt_pt > 511) ? 511 : gmt_pt;
0171 
0172     std::cout << mode_inv << " " << address << " " << print_subaddresses(address) << " " << gmt_pt << std::endl;
0173   }
0174 }
0175 
0176 // DEFINE THIS AS A PLUG-IN
0177 #include "FWCore/Framework/interface/MakerMacros.h"
0178 DEFINE_FWK_MODULE(MakePtLUT);