Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 ///
0002 /// \class L1TGlobalPrescalesVetosESProducer
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/PrescalesVetosHelper.h"
0034 
0035 #include "CondFormats/L1TObjects/interface/L1TGlobalPrescalesVetos.h"
0036 #include "CondFormats/DataRecord/interface/L1TGlobalPrescalesVetosRcd.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 L1TGlobalPrescalesVetosESProducer : public edm::ESProducer {
0050 public:
0051   L1TGlobalPrescalesVetosESProducer(const edm::ParameterSet&);
0052   ~L1TGlobalPrescalesVetosESProducer() override;
0053 
0054   using ReturnType = std::unique_ptr<L1TGlobalPrescalesVetos>;
0055 
0056   ReturnType produce(const L1TGlobalPrescalesVetosRcd&);
0057 
0058 private:
0059   PrescalesVetosHelper data_;
0060 
0061   unsigned int m_numberPhysTriggers;
0062   int m_verbosity;
0063   int m_bx_mask_default;
0064 
0065   std::vector<std::vector<int> > 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 L1TGlobalPrescalesVetosESProducer::L1TGlobalPrescalesVetosESProducer(const edm::ParameterSet& conf)
0083     : data_(new L1TGlobalPrescalesVetos()) {
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<int> > 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("L1TGlobalPrescalesVetosESProducer")
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<int>(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<int>());
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<unsigned int> prescalesForSet =
0181             settings_prescale.at("prescales").getTableColumn<unsigned int>(col.first.c_str());
0182         for (unsigned int row = 0; row < prescalesForSet.size(); row++) {
0183           unsigned int 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("L1TGlobalPrescalesVetosESProducer")
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("L1TGlobalPrescalesVetosESProducer") << "\nCould not find veto mask file: " << m_vetomaskFile
0247                                                        << "\nDeafulting the veto mask for all triggers to 1 (unmasked)";
0248   } else {
0249     while (!input_mask_veto.eof()) {
0250       string tmp;
0251       getline(input_mask_veto, tmp, '\n');
0252       xmlPayload_mask_veto.append(tmp);
0253     }
0254 
0255     l1t::XmlConfigParser xmlReader_mask_veto;
0256     l1t::TriggerSystem ts_mask_veto;
0257     ts_mask_veto.addProcessor("uGtProcessor", "uGtProcessor", "-1", "-1");
0258 
0259     // run the parser
0260     xmlReader_mask_veto.readDOMFromString(xmlPayload_mask_veto);  // initialize it
0261     xmlReader_mask_veto.readRootElement(ts_mask_veto, "uGT");     // extract all of the relevant context
0262     ts_mask_veto.setConfigured();
0263 
0264     const std::map<std::string, l1t::Parameter>& settings_mask_veto = ts_mask_veto.getParameters("uGtProcessor");
0265     std::vector<unsigned int> algo_mask_veto = settings_mask_veto.at("vetoMask").getTableColumn<unsigned int>("algo");
0266     std::vector<unsigned int> veto_mask_veto = settings_mask_veto.at("vetoMask").getTableColumn<unsigned int>("veto");
0267 
0268     for (unsigned int row = 0; row < algo_mask_veto.size(); row++) {
0269       unsigned int algoBit = algo_mask_veto[row];
0270       unsigned int veto = veto_mask_veto[row];
0271       if (algoBit < m_numberPhysTriggers)
0272         triggerVetoMasks[algoBit] = int(veto);
0273     }
0274   }
0275   input_mask_veto.close();
0276 
0277   ///////////////////////////////////////////////////////////////////////////////////////////////////////////
0278 
0279   // Algo bx mask
0280   std::ifstream input_mask_algobx;
0281   input_mask_algobx.open(m_algobxmaskFile);
0282   if (not m_algobxmaskFile.empty() and not input_mask_algobx) {
0283     edm::LogError("L1TGlobalPrescalesVetosESProducer")
0284         << "\nCould not find bx mask file: " << m_algobxmaskFile << "\nNot filling the bx mask map";
0285   } else {
0286     while (!input_mask_algobx.eof()) {
0287       string tmp;
0288       getline(input_mask_algobx, tmp, '\n');
0289       xmlPayload_mask_algobx.append(tmp);
0290     }
0291 
0292     l1t::XmlConfigParser xmlReader_mask_algobx;
0293     l1t::TriggerSystem ts_mask_algobx;
0294     ts_mask_algobx.addProcessor("uGtProcessor", "uGtProcessor", "-1", "-1");
0295 
0296     // run the parser
0297     xmlReader_mask_algobx.readDOMFromString(xmlPayload_mask_algobx);  // initialize it
0298     xmlReader_mask_algobx.readRootElement(ts_mask_algobx, "uGT");     // extract all of the relevant context
0299     ts_mask_algobx.setConfigured();
0300 
0301     const std::map<std::string, l1t::Parameter>& settings_mask_algobx = ts_mask_algobx.getParameters("uGtProcessor");
0302     std::map<std::string, unsigned int> mask_algobx_columns =
0303         settings_mask_algobx.at("algorithmBxMask").getColumnIndices();
0304     std::vector<unsigned int> bunches =
0305         settings_mask_algobx.at("algorithmBxMask").getTableColumn<unsigned int>("bx/algo");
0306 
0307     unsigned int numCol_mask_algobx = mask_algobx_columns.size();
0308 
0309     int NumAlgoBitsInMask = numCol_mask_algobx - 1;
0310     for (int iBit = 0; iBit < NumAlgoBitsInMask; iBit++) {
0311       std::vector<unsigned int> algo =
0312           settings_mask_algobx.at("algorithmBxMask").getTableColumn<unsigned int>(std::to_string(iBit).c_str());
0313       for (unsigned int bx = 0; bx < bunches.size(); bx++) {
0314         if (algo[bx] != unsigned(m_bx_mask_default))
0315           triggerAlgoBxMaskAlgoTrig[bunches[bx]].push_back(iBit);
0316       }
0317     }
0318   }
0319   input_mask_algobx.close();
0320 
0321   // Set prescales to zero if masked
0322   for (auto& prescale : prescales) {
0323     for (unsigned int iBit = 0; iBit < prescale.size(); iBit++) {
0324       // Add protection in case prescale table larger than trigger mask size
0325       if (iBit >= triggerMasks.size()) {
0326         edm::LogWarning("L1TGlobal") << "\nWarning: algoBit in prescale table >= triggerMasks.size() "
0327                                      << "\nWarning: no information on masking bit or not, setting as unmasked "
0328                                      << std::endl;
0329       } else {
0330         prescale[iBit] *= triggerMasks[iBit];
0331       }
0332     }
0333   }
0334 
0335   m_initialPrescaleFactorsAlgoTrig = prescales;
0336   m_initialTriggerMaskAlgoTrig = triggerMasks;
0337   m_initialTriggerMaskVetoAlgoTrig = triggerVetoMasks;
0338   m_initialTriggerAlgoBxMaskAlgoTrig = triggerAlgoBxMaskAlgoTrig;
0339 }
0340 
0341 L1TGlobalPrescalesVetosESProducer::~L1TGlobalPrescalesVetosESProducer() {
0342   // do anything here that needs to be done at desctruction time
0343   // (e.g. close files, deallocate resources etc.)
0344 }
0345 
0346 //
0347 // member functions
0348 //
0349 
0350 // ------------ method called to produce the data  ------------
0351 L1TGlobalPrescalesVetosESProducer::ReturnType L1TGlobalPrescalesVetosESProducer::produce(
0352     const L1TGlobalPrescalesVetosRcd& iRecord) {
0353   // configure the helper class parameters via its set funtions, e.g.:
0354   data_.setBxMaskDefault(m_bx_mask_default);
0355   data_.setPrescaleFactorTable(m_initialPrescaleFactorsAlgoTrig);
0356   data_.setTriggerMaskVeto(m_initialTriggerMaskVetoAlgoTrig);
0357   data_.setTriggerAlgoBxMask(m_initialTriggerAlgoBxMaskAlgoTrig);
0358 
0359   if (m_verbosity) {
0360     LogDebug("L1TGlobal") << " ====> Prescales table <=== " << std::endl;
0361     for (unsigned int ix = 0; ix < m_initialPrescaleFactorsAlgoTrig.size(); ix++) {
0362       LogDebug("L1TGlobal") << " Prescale set = " << ix << std::endl;
0363       for (unsigned int iy = 0; iy < m_initialPrescaleFactorsAlgoTrig[ix].size(); iy++) {
0364         LogDebug("L1TGlobal") << "\t Algo " << iy << ": " << m_initialPrescaleFactorsAlgoTrig[ix][iy] << std::endl;
0365       }
0366     }
0367 
0368     LogDebug("L1TGlobal") << " ====> Trigger mask veto <=== " << std::endl;
0369     for (unsigned int ix = 0; ix < m_initialTriggerMaskVetoAlgoTrig.size(); ix++) {
0370       LogDebug("L1TGlobal") << "\t Algo " << ix << ": " << m_initialTriggerMaskVetoAlgoTrig[ix] << std::endl;
0371     }
0372 
0373     LogDebug("L1TGlobal") << " ====> Algo bx mask <=== " << std::endl;
0374     if (m_initialTriggerAlgoBxMaskAlgoTrig.empty())
0375       LogDebug("L1TGlobal") << "\t(empty map)" << std::endl;
0376     for (auto& it : m_initialTriggerAlgoBxMaskAlgoTrig) {
0377       LogDebug("L1TGlobal") << " bx = " << it.first << " : iAlg =";
0378       std::vector<int> masked = it.second;
0379       for (int& iAlg : masked) {
0380         LogDebug("L1TGlobal") << " " << iAlg;
0381       }
0382       LogDebug("L1TGlobal") << " " << std::endl;
0383     }
0384   }
0385 
0386   // Return copy so that we don't give away our owned pointer to framework
0387   return std::make_unique<L1TGlobalPrescalesVetos>(*data_.getWriteInstance());
0388 }
0389 
0390 //define this as a plug-in
0391 DEFINE_FWK_EVENTSETUP_MODULE(L1TGlobalPrescalesVetosESProducer);