Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-25 23:40:16

0001 ///
0002 /// \class L1TGlobalPrescalesVetosFractESProducer
0003 ///
0004 /// Description: Produces L1T Trigger Menu Condition Format
0005 ///
0006 /// Implementation:
0007 ///    Dummy producer for L1T uGT Trigger Menu
0008 ///
0009 
0010 // system include files
0011 #include <memory>
0012 #include <iostream>
0013 #include <fstream>
0014 #include <unistd.h>
0015 #include <cstdio>
0016 #include <cstdlib>
0017 
0018 #include "tmEventSetup/tmEventSetup.hh"
0019 #include "tmEventSetup/esTriggerMenu.hh"
0020 #include "tmEventSetup/esAlgorithm.hh"
0021 #include "tmEventSetup/esCondition.hh"
0022 #include "tmEventSetup/esObject.hh"
0023 #include "tmEventSetup/esCut.hh"
0024 #include "tmEventSetup/esScale.hh"
0025 #include "tmGrammar/Algorithm.hh"
0026 
0027 #include "FWCore/Framework/interface/ModuleFactory.h"
0028 #include "FWCore/Framework/interface/ESProducer.h"
0029 #include "FWCore/Framework/interface/ESProducts.h"
0030 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0031 #include "FWCore/ParameterSet/interface/FileInPath.h"
0032 
0033 #include "L1Trigger/L1TGlobal/interface/PrescalesVetosFractHelper.h"
0034 
0035 #include "CondFormats/L1TObjects/interface/L1TGlobalPrescalesVetosFract.h"
0036 #include "CondFormats/DataRecord/interface/L1TGlobalPrescalesVetosFractRcd.h"
0037 
0038 #include "L1Trigger/L1TCommon/interface/XmlConfigParser.h"
0039 #include "L1Trigger/L1TCommon/interface/TriggerSystem.h"
0040 #include "L1Trigger/L1TCommon/interface/Parameter.h"
0041 
0042 using namespace std;
0043 using namespace edm;
0044 using namespace l1t;
0045 //
0046 // class declaration
0047 //
0048 
0049 class L1TGlobalPrescalesVetosFractESProducer : public edm::ESProducer {
0050 public:
0051   L1TGlobalPrescalesVetosFractESProducer(const edm::ParameterSet&);
0052   ~L1TGlobalPrescalesVetosFractESProducer() override;
0053 
0054   using ReturnType = std::unique_ptr<L1TGlobalPrescalesVetosFract>;
0055 
0056   ReturnType produce(const L1TGlobalPrescalesVetosFractRcd&);
0057 
0058 private:
0059   PrescalesVetosFractHelper data_;
0060 
0061   unsigned int m_numberPhysTriggers;
0062   int m_verbosity;
0063   int m_bx_mask_default;
0064 
0065   std::vector<std::vector<double> > m_initialPrescaleFactorsAlgoTrig;
0066   std::vector<unsigned int> m_initialTriggerMaskAlgoTrig;
0067   std::vector<int> m_initialTriggerMaskVetoAlgoTrig;
0068   std::map<int, std::vector<int> > m_initialTriggerAlgoBxMaskAlgoTrig;
0069 };
0070 
0071 //
0072 // constants, enums and typedefs
0073 //
0074 
0075 //
0076 // static data member definitions
0077 //
0078 
0079 //
0080 // constructors and destructor
0081 //
0082 L1TGlobalPrescalesVetosFractESProducer::L1TGlobalPrescalesVetosFractESProducer(const edm::ParameterSet& conf)
0083     : data_(new L1TGlobalPrescalesVetosFract()) {
0084   //the following line is needed to tell the framework what
0085   // data is being produced
0086   setWhatProduced(this);
0087   //setWhatProduced(this, conf.getParameter<std::string>("label"));
0088 
0089   m_numberPhysTriggers = 512;
0090 
0091   // directory in /data/Luminosity for the trigger menu
0092   std::string menuDir = conf.getParameter<std::string>("TriggerMenuLuminosity");
0093   //std::string menuDir = "startup";
0094 
0095   m_verbosity = conf.getParameter<int>("Verbosity");
0096   m_bx_mask_default = conf.getParameter<int>("AlgoBxMaskDefault");
0097 
0098   // XML files
0099   std::string prescalesFileName = conf.getParameter<std::string>("PrescaleXMLFile");
0100   std::string algobxmaskFileName = conf.getParameter<std::string>("AlgoBxMaskXMLFile");
0101   std::string finormaskFileName = conf.getParameter<std::string>("FinOrMaskXMLFile");
0102   std::string vetomaskFileName = conf.getParameter<std::string>("VetoMaskXMLFile");
0103 
0104   // Full path
0105   edm::FileInPath f1_prescale("L1Trigger/L1TGlobal/data/Luminosity/" + menuDir + "/" + prescalesFileName);
0106   std::string m_prescaleFile = f1_prescale.fullPath();
0107 
0108   edm::FileInPath f1_mask_algobx("L1Trigger/L1TGlobal/data/Luminosity/" + menuDir + "/" + algobxmaskFileName);
0109   std::string m_algobxmaskFile = f1_mask_algobx.fullPath();
0110 
0111   edm::FileInPath f1_mask_finor("L1Trigger/L1TGlobal/data/Luminosity/" + menuDir + "/" + finormaskFileName);
0112   std::string m_finormaskFile = f1_mask_finor.fullPath();
0113 
0114   edm::FileInPath f1_mask_veto("L1Trigger/L1TGlobal/data/Luminosity/" + menuDir + "/" + vetomaskFileName);
0115   std::string m_vetomaskFile = f1_mask_veto.fullPath();
0116 
0117   // XML payloads
0118   std::string xmlPayload_prescale;
0119   std::string xmlPayload_mask_algobx;
0120   std::string xmlPayload_mask_finor;
0121   std::string xmlPayload_mask_veto;
0122 
0123   std::vector<std::vector<double> > prescales;
0124   std::vector<unsigned int> triggerMasks;
0125   std::vector<int> triggerVetoMasks;
0126   std::map<int, std::vector<int> > triggerAlgoBxMaskAlgoTrig;
0127 
0128   // Prescales
0129   std::ifstream input_prescale;
0130   input_prescale.open(m_prescaleFile);
0131   if (not m_prescaleFile.empty() and not input_prescale) {
0132     edm::LogError("L1TGlobalPrescalesVetosFractESProducer")
0133         << "\nCould not find prescale file: " << m_prescaleFile
0134         << "\nDeafulting to a single prescale column, with all prescales set to 1 (unprescaled)";
0135 
0136     const int inputDefaultPrescale = 1;
0137     // by default, fill a single prescale column
0138     prescales.push_back(std::vector<double>(m_numberPhysTriggers, inputDefaultPrescale));
0139   } else {
0140     while (!input_prescale.eof()) {
0141       string tmp;
0142       getline(input_prescale, tmp, '\n');
0143       xmlPayload_prescale.append(tmp);
0144     }
0145 
0146     l1t::XmlConfigParser xmlReader_prescale;
0147     l1t::TriggerSystem ts_prescale;
0148     ts_prescale.addProcessor("uGtProcessor", "uGtProcessor", "-1", "-1");
0149 
0150     // run the parser
0151     xmlReader_prescale.readDOMFromString(xmlPayload_prescale);  // initialize it
0152     xmlReader_prescale.readRootElement(ts_prescale, "uGT");     // extract all of the relevant context
0153     ts_prescale.setConfigured();
0154 
0155     const std::map<std::string, l1t::Parameter>& settings_prescale = ts_prescale.getParameters("uGtProcessor");
0156     std::map<std::string, unsigned int> prescaleColumns = settings_prescale.at("prescales").getColumnIndices();
0157 
0158     unsigned int numColumns_prescale = prescaleColumns.size();
0159 
0160     int NumPrescaleSets = numColumns_prescale - 1;
0161     ///  there may be "missing" rows/bits in the xml description meaning that triggers are not unused, so go for max
0162     std::vector<unsigned int> algoBits =
0163         settings_prescale.at("prescales").getTableColumn<unsigned int>("algo/prescale-index");
0164     int NumAlgos_prescale = *std::max_element(algoBits.begin(), algoBits.end()) + 1;
0165 
0166     if (NumPrescaleSets > 0) {
0167       // Fill default prescale set
0168       for (int iSet = 0; iSet < NumPrescaleSets; iSet++) {
0169         prescales.push_back(std::vector<double>());
0170         for (int iBit = 0; iBit < NumAlgos_prescale; ++iBit) {
0171           int inputDefaultPrescale = 0;  // only prescales that are set in the block below are used
0172           prescales[iSet].push_back(inputDefaultPrescale);
0173         }
0174       }
0175 
0176       for (auto& col : prescaleColumns) {
0177         if (col.second < 1)
0178           continue;  // we don't care for the algorithms' indicies in 0th column
0179         int iSet = col.second - 1;
0180         std::vector<double> prescalesForSet =
0181             settings_prescale.at("prescales").getTableColumn<double>(col.first.c_str());
0182         for (unsigned int row = 0; row < prescalesForSet.size(); row++) {
0183           double prescale = prescalesForSet[row];
0184           unsigned int algoBit = algoBits[row];
0185           prescales[iSet][algoBit] = prescale;
0186         }
0187       }
0188     }
0189   }
0190   input_prescale.close();
0191 
0192   ///////////////////////////////////////////////////////////////////////////////////////////////////////////
0193 
0194   // finor mask
0195   // set all masks to 1 (unmasked) by default
0196   triggerMasks.insert(triggerMasks.end(), m_numberPhysTriggers, 1);
0197 
0198   std::ifstream input_mask_finor;
0199   input_mask_finor.open(m_finormaskFile);
0200   if (not m_finormaskFile.empty() and not input_mask_finor) {
0201     edm::LogError("L1TGlobalPrescalesVetosFractESProducer")
0202         << "\nCould not find finor mask file: " << m_finormaskFile
0203         << "\nDeafulting the finor mask for all triggers to 1 (unmasked)";
0204   } else {
0205     while (!input_mask_finor.eof()) {
0206       string tmp;
0207       getline(input_mask_finor, tmp, '\n');
0208       xmlPayload_mask_finor.append(tmp);
0209     }
0210 
0211     l1t::XmlConfigParser xmlReader_mask_finor;
0212     l1t::TriggerSystem ts_mask_finor;
0213     ts_mask_finor.addProcessor("uGtProcessor", "uGtProcessor", "-1", "-1");
0214 
0215     // run the parser
0216     xmlReader_mask_finor.readDOMFromString(xmlPayload_mask_finor);  // initialize it
0217     xmlReader_mask_finor.readRootElement(ts_mask_finor, "uGT");     // extract all of the relevant context
0218     ts_mask_finor.setConfigured();
0219 
0220     const std::map<std::string, l1t::Parameter>& settings_mask_finor = ts_mask_finor.getParameters("uGtProcessor");
0221 
0222     std::vector<unsigned int> algo_mask_finor =
0223         settings_mask_finor.at("finorMask").getTableColumn<unsigned int>("algo");
0224     std::vector<unsigned int> mask_mask_finor =
0225         settings_mask_finor.at("finorMask").getTableColumn<unsigned int>("mask");
0226 
0227     for (unsigned int row = 0; row < algo_mask_finor.size(); row++) {
0228       unsigned int algoBit = algo_mask_finor[row];
0229       unsigned int mask = mask_mask_finor[row];
0230       if (algoBit < m_numberPhysTriggers)
0231         triggerMasks[algoBit] = mask;
0232     }
0233   }
0234   input_mask_finor.close();
0235 
0236   ///////////////////////////////////////////////////////////////////////////////////////////////////////////
0237 
0238   // veto mask
0239   // Setting veto mask to default 0 (no veto)
0240   for (unsigned int iAlg = 0; iAlg < m_numberPhysTriggers; iAlg++)
0241     triggerVetoMasks.push_back(0);
0242 
0243   std::ifstream input_mask_veto;
0244   input_mask_veto.open(m_vetomaskFile);
0245   if (not m_vetomaskFile.empty() and not input_mask_veto) {
0246     edm::LogError("L1TGlobalPrescalesVetosFractESProducer")
0247         << "\nCould not find veto mask file: " << m_vetomaskFile
0248         << "\nDeafulting the veto mask for all triggers to 1 (unmasked)";
0249   } else {
0250     while (!input_mask_veto.eof()) {
0251       string tmp;
0252       getline(input_mask_veto, tmp, '\n');
0253       xmlPayload_mask_veto.append(tmp);
0254     }
0255 
0256     l1t::XmlConfigParser xmlReader_mask_veto;
0257     l1t::TriggerSystem ts_mask_veto;
0258     ts_mask_veto.addProcessor("uGtProcessor", "uGtProcessor", "-1", "-1");
0259 
0260     // run the parser
0261     xmlReader_mask_veto.readDOMFromString(xmlPayload_mask_veto);  // initialize it
0262     xmlReader_mask_veto.readRootElement(ts_mask_veto, "uGT");     // extract all of the relevant context
0263     ts_mask_veto.setConfigured();
0264 
0265     const std::map<std::string, l1t::Parameter>& settings_mask_veto = ts_mask_veto.getParameters("uGtProcessor");
0266     std::vector<unsigned int> algo_mask_veto = settings_mask_veto.at("vetoMask").getTableColumn<unsigned int>("algo");
0267     std::vector<unsigned int> veto_mask_veto = settings_mask_veto.at("vetoMask").getTableColumn<unsigned int>("veto");
0268 
0269     for (unsigned int row = 0; row < algo_mask_veto.size(); row++) {
0270       unsigned int algoBit = algo_mask_veto[row];
0271       unsigned int veto = veto_mask_veto[row];
0272       if (algoBit < m_numberPhysTriggers)
0273         triggerVetoMasks[algoBit] = int(veto);
0274     }
0275   }
0276   input_mask_veto.close();
0277 
0278   ///////////////////////////////////////////////////////////////////////////////////////////////////////////
0279 
0280   // Algo bx mask
0281   std::ifstream input_mask_algobx;
0282   input_mask_algobx.open(m_algobxmaskFile);
0283   if (not m_algobxmaskFile.empty() and not input_mask_algobx) {
0284     edm::LogError("L1TGlobalPrescalesVetosFractESProducer")
0285         << "\nCould not find bx mask file: " << m_algobxmaskFile << "\nNot filling the bx mask map";
0286   } else {
0287     while (!input_mask_algobx.eof()) {
0288       string tmp;
0289       getline(input_mask_algobx, tmp, '\n');
0290       xmlPayload_mask_algobx.append(tmp);
0291     }
0292 
0293     l1t::XmlConfigParser xmlReader_mask_algobx;
0294     l1t::TriggerSystem ts_mask_algobx;
0295     ts_mask_algobx.addProcessor("uGtProcessor", "uGtProcessor", "-1", "-1");
0296 
0297     // run the parser
0298     xmlReader_mask_algobx.readDOMFromString(xmlPayload_mask_algobx);  // initialize it
0299     xmlReader_mask_algobx.readRootElement(ts_mask_algobx, "uGT");     // extract all of the relevant context
0300     ts_mask_algobx.setConfigured();
0301 
0302     const std::map<std::string, l1t::Parameter>& settings_mask_algobx = ts_mask_algobx.getParameters("uGtProcessor");
0303     std::map<std::string, unsigned int> mask_algobx_columns =
0304         settings_mask_algobx.at("algorithmBxMask").getColumnIndices();
0305     std::vector<unsigned int> bunches =
0306         settings_mask_algobx.at("algorithmBxMask").getTableColumn<unsigned int>("bx/algo");
0307 
0308     unsigned int numCol_mask_algobx = mask_algobx_columns.size();
0309 
0310     int NumAlgoBitsInMask = numCol_mask_algobx - 1;
0311     for (int iBit = 0; iBit < NumAlgoBitsInMask; iBit++) {
0312       std::vector<unsigned int> algo =
0313           settings_mask_algobx.at("algorithmBxMask").getTableColumn<unsigned int>(std::to_string(iBit).c_str());
0314       for (unsigned int bx = 0; bx < bunches.size(); bx++) {
0315         if (algo[bx] != unsigned(m_bx_mask_default))
0316           triggerAlgoBxMaskAlgoTrig[bunches[bx]].push_back(iBit);
0317       }
0318     }
0319   }
0320   input_mask_algobx.close();
0321 
0322   // Set prescales to zero if masked
0323   for (auto& prescale : prescales) {
0324     for (unsigned int iBit = 0; iBit < prescale.size(); iBit++) {
0325       // Add protection in case prescale table larger than trigger mask size
0326       if (iBit >= triggerMasks.size()) {
0327         edm::LogWarning("L1TGlobal") << "\nWarning: algoBit in prescale table >= triggerMasks.size() "
0328                                      << "\nWarning: no information on masking bit or not, setting as unmasked "
0329                                      << std::endl;
0330       } else {
0331         prescale[iBit] *= triggerMasks[iBit];
0332       }
0333     }
0334   }
0335 
0336   m_initialPrescaleFactorsAlgoTrig = prescales;
0337   m_initialTriggerMaskAlgoTrig = triggerMasks;
0338   m_initialTriggerMaskVetoAlgoTrig = triggerVetoMasks;
0339   m_initialTriggerAlgoBxMaskAlgoTrig = triggerAlgoBxMaskAlgoTrig;
0340 }
0341 
0342 L1TGlobalPrescalesVetosFractESProducer::~L1TGlobalPrescalesVetosFractESProducer() {
0343   // do anything here that needs to be done at desctruction time
0344   // (e.g. close files, deallocate resources etc.)
0345 }
0346 
0347 //
0348 // member functions
0349 //
0350 
0351 // ------------ method called to produce the data  ------------
0352 L1TGlobalPrescalesVetosFractESProducer::ReturnType L1TGlobalPrescalesVetosFractESProducer::produce(
0353     const L1TGlobalPrescalesVetosFractRcd& iRecord) {
0354   // configure the helper class parameters via its set funtions, e.g.:
0355   data_.setBxMaskDefault(m_bx_mask_default);
0356   data_.setPrescaleFactorTable(m_initialPrescaleFactorsAlgoTrig);
0357   data_.setTriggerMaskVeto(m_initialTriggerMaskVetoAlgoTrig);
0358   data_.setTriggerAlgoBxMask(m_initialTriggerAlgoBxMaskAlgoTrig);
0359 
0360   if (m_verbosity) {
0361     LogDebug("L1TGlobal") << " ====> Prescales table <=== " << std::endl;
0362     for (unsigned int ix = 0; ix < m_initialPrescaleFactorsAlgoTrig.size(); ix++) {
0363       LogDebug("L1TGlobal") << " Prescale set = " << ix << std::endl;
0364       for (unsigned int iy = 0; iy < m_initialPrescaleFactorsAlgoTrig[ix].size(); iy++) {
0365         LogDebug("L1TGlobal") << "\t Algo " << iy << ": " << m_initialPrescaleFactorsAlgoTrig[ix][iy] << std::endl;
0366       }
0367     }
0368 
0369     LogDebug("L1TGlobal") << " ====> Trigger mask veto <=== " << std::endl;
0370     for (unsigned int ix = 0; ix < m_initialTriggerMaskVetoAlgoTrig.size(); ix++) {
0371       LogDebug("L1TGlobal") << "\t Algo " << ix << ": " << m_initialTriggerMaskVetoAlgoTrig[ix] << std::endl;
0372     }
0373 
0374     LogDebug("L1TGlobal") << " ====> Algo bx mask <=== " << std::endl;
0375     if (m_initialTriggerAlgoBxMaskAlgoTrig.empty())
0376       LogDebug("L1TGlobal") << "\t(empty map)" << std::endl;
0377     for (auto& it : m_initialTriggerAlgoBxMaskAlgoTrig) {
0378       LogDebug("L1TGlobal") << " bx = " << it.first << " : iAlg =";
0379       std::vector<int> masked = it.second;
0380       for (int& iAlg : masked) {
0381         LogDebug("L1TGlobal") << " " << iAlg;
0382       }
0383       LogDebug("L1TGlobal") << " " << std::endl;
0384     }
0385   }
0386 
0387   // Return copy so that we don't give away our owned pointer to framework
0388   return std::make_unique<L1TGlobalPrescalesVetosFract>(*data_.getWriteInstance());
0389 }
0390 
0391 //define this as a plug-in
0392 DEFINE_FWK_EVENTSETUP_MODULE(L1TGlobalPrescalesVetosFractESProducer);