Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-15 23:40:38

0001 // L1TGlobalUtil:  Utility class for parsing the L1 Trigger Menu
0002 
0003 #ifndef L1TGlobal_L1TGlobalUtil_h
0004 #define L1TGlobal_L1TGlobalUtil_h
0005 
0006 // system include files
0007 #include <memory>
0008 #include <string_view>
0009 
0010 #include <vector>
0011 
0012 #include "CondFormats/DataRecord/interface/L1TUtmTriggerMenuRcd.h"
0013 #include "CondFormats/L1TObjects/interface/L1TUtmTriggerMenu.h"
0014 
0015 #include "CondFormats/DataRecord/interface/L1TGlobalPrescalesVetosFractRcd.h"
0016 #include "CondFormats/L1TObjects/interface/L1TGlobalPrescalesVetosFract.h"
0017 
0018 // Objects to produce for the output record.
0019 #include "DataFormats/L1TGlobal/interface/GlobalAlgBlk.h"
0020 #include "DataFormats/L1TGlobal/interface/GlobalExtBlk.h"
0021 #include "CondFormats/L1TObjects/interface/L1TUtmAlgorithm.h"
0022 
0023 #include "FWCore/Framework/interface/ConsumesCollector.h"
0024 #include "FWCore/Framework/interface/Event.h"
0025 #include "FWCore/Utilities/interface/ESGetToken.h"
0026 #include "FWCore/Utilities/interface/InputTag.h"
0027 
0028 #include "FWCore/Framework/interface/EventSetup.h"
0029 
0030 #include "L1Trigger/L1TGlobal/interface/L1TGlobalUtilHelper.h"
0031 
0032 #include "L1Trigger/L1TGlobal/interface/PrescalesVetosFractHelper.h"
0033 
0034 // forward declarations
0035 
0036 // class declaration
0037 
0038 namespace l1t {
0039 
0040   // Use this to tell the EventSetup whether it should prefetch
0041   // data when processing beginRun or an Event or both. (This
0042   // depends on when retrieveL1 and retrieveL1Setup are called)
0043   enum class UseEventSetupIn { Run, Event, RunAndEvent };
0044 
0045   class L1TGlobalUtil {
0046   public:
0047     // Using this constructor will require InputTags to be specified in the configuration
0048     L1TGlobalUtil(edm::ParameterSet const& pset,
0049                   edm::ConsumesCollector&& iC,
0050                   UseEventSetupIn use = UseEventSetupIn::Run);
0051 
0052     L1TGlobalUtil(edm::ParameterSet const& pset,
0053                   edm::ConsumesCollector& iC,
0054                   UseEventSetupIn use = UseEventSetupIn::Run);
0055 
0056     // Using this constructor will cause it to look for valid InputTags in
0057     // the following ways in the specified order until they are found.
0058     //   1. The configuration
0059     //   2. Search all products from the preferred input tags for the required type
0060     //   3. Search all products from any other process for the required type
0061     template <typename T>
0062     L1TGlobalUtil(edm::ParameterSet const& pset,
0063                   edm::ConsumesCollector&& iC,
0064                   T& module,
0065                   UseEventSetupIn use = UseEventSetupIn::Run);
0066 
0067     template <typename T>
0068     L1TGlobalUtil(edm::ParameterSet const& pset,
0069                   edm::ConsumesCollector& iC,
0070                   T& module,
0071                   UseEventSetupIn use = UseEventSetupIn::Run);
0072 
0073     // Using this constructor will cause it to look for valid InputTags in
0074     // the following ways in the specified order until they are found.
0075     //   1. The constructor arguments
0076     //   2. The configuration
0077     //   3. Search all products from the preferred input tags for the required type
0078     //   4. Search all products from any other process for the required type
0079     template <typename T>
0080     L1TGlobalUtil(edm::ParameterSet const& pset,
0081                   edm::ConsumesCollector&& iC,
0082                   T& module,
0083                   edm::InputTag const& l1tAlgBlkInputTag,
0084                   edm::InputTag const& l1tExtBlkInputTag,
0085                   UseEventSetupIn use = UseEventSetupIn::Run);
0086 
0087     template <typename T>
0088     L1TGlobalUtil(edm::ParameterSet const& pset,
0089                   edm::ConsumesCollector& iC,
0090                   T& module,
0091                   edm::InputTag const& l1tAlgBlkInputTag,
0092                   edm::InputTag const& l1tExtBlkInputTag,
0093                   UseEventSetupIn use = UseEventSetupIn::Run);
0094 
0095     /// destructor
0096     virtual ~L1TGlobalUtil();
0097 
0098     /// check that the L1TGlobalUtil has been properly initialised
0099     bool valid() const;
0100 
0101     static void fillDescription(edm::ParameterSetDescription& desc,
0102                                 edm::InputTag const& iAlg,
0103                                 edm::InputTag const& iExt,
0104                                 bool readPrescalesFromFile) {
0105       L1TGlobalUtilHelper::fillDescription(desc, iAlg, iExt, readPrescalesFromFile);
0106     }
0107 
0108     // OverridePrescalesAndMasks
0109     // The ability to override the prescale/mask file will not be part of the permanent interface of this class.
0110     // It is provided only until prescales and masks are available as CondFormats...
0111     // Most users should simply ignore this method and use the default ctor only!
0112     // Will look for prescale csv file in L1Trigger/L1TGlobal/data/Luminosity/startup/<filename>
0113     void OverridePrescalesAndMasks(std::string filename, unsigned int psColumn = 1.);
0114 
0115     /// initialize the class (mainly reserve)
0116     void retrieveL1(const edm::Event& iEvent, const edm::EventSetup& evSetup);  // using helper
0117     void retrieveL1(const edm::Event& iEvent, const edm::EventSetup& evSetup, edm::EDGetToken gtAlgToken);
0118     void retrieveL1Setup(const edm::EventSetup& evSetup);  // Use this one only during beginRun
0119     void retrieveL1Event(const edm::Event& iEvent, const edm::EventSetup& evSetup);  // using helper
0120     void retrieveL1Event(const edm::Event& iEvent, const edm::EventSetup& evSetup, edm::EDGetToken gtAlgToken);
0121 
0122     inline void setVerbosity(const int verbosity) { m_verbosity = verbosity; }
0123 
0124     inline bool getFinalOR() const { return m_finalOR; }
0125 
0126     // get the trigger bit from the name
0127     const bool getAlgBitFromName(const std::string& AlgName, int& bit) const;
0128 
0129     // get the name from the trigger bit
0130     const bool getAlgNameFromBit(int& bit, std::string_view& AlgName) const;
0131 
0132     // Access results for particular trigger bit
0133     const bool getInitialDecisionByBit(int& bit, bool& decision) const;
0134     const bool getIntermDecisionByBit(int& bit, bool& decision) const;
0135     const bool getFinalDecisionByBit(int& bit, bool& decision) const;
0136 
0137     // Access Prescale
0138     const bool getPrescaleByBit(int& bit, double& prescale) const;
0139 
0140     // Access Masks:
0141     // follows logic of uGT board:
0142     //       finalDecision[AlgBit]
0143     //           Final word is after application of prescales.
0144     //           A prescale = 0 effectively masks out the algorithm in the final decision word
0145     //
0146     const bool getMaskByBit(int& bit, std::vector<int>& mask) const;
0147 
0148     // Access results for particular trigger name
0149     const bool getInitialDecisionByName(const std::string& algName, bool& decision) const;
0150     const bool getIntermDecisionByName(const std::string& algName, bool& decision) const;
0151     const bool getFinalDecisionByName(const std::string& algName, bool& decision) const;
0152 
0153     // Access Prescales
0154     const bool getPrescaleByName(const std::string& algName, double& prescale) const;
0155 
0156     // Access Masks (see note) above
0157     const bool getMaskByName(const std::string& algName, std::vector<int>& mask) const;
0158 
0159     // Some inline commands to return the full vectors
0160     inline const std::vector<std::pair<std::string_view, bool>>& decisionsInitial() { return m_decisionsInitial; }
0161     inline const std::vector<std::pair<std::string_view, bool>>& decisionsInterm() { return m_decisionsInterm; }
0162     inline const std::vector<std::pair<std::string_view, bool>>& decisionsFinal() { return m_decisionsFinal; }
0163 
0164     // Access all prescales
0165     inline const std::vector<std::pair<std::string_view, double>>& prescales() { return m_prescales; }
0166 
0167     // Access Masks (see note) above
0168     inline const std::vector<std::pair<std::string_view, std::vector<int>>>& masks() { return m_masks; }
0169 
0170     // Menu names
0171     inline const std::string& gtTriggerMenuName() const { return m_l1GtMenu->getName(); }
0172     inline const std::string& gtTriggerMenuVersion() const { return m_l1GtMenu->getVersion(); }
0173     inline const std::string& gtTriggerMenuComment() const { return m_l1GtMenu->getComment(); }
0174 
0175     // Prescale Column
0176     inline unsigned int prescaleColumn() const { return m_PreScaleColumn; }
0177     inline unsigned int numberOfPreScaleColumns() const { return m_numberOfPreScaleColumns; }
0178 
0179   private:
0180     L1TGlobalUtil();
0181 
0182     void retrieveL1Setup(const edm::EventSetup& evSetup, bool isRun);
0183     void eventSetupConsumes(edm::ConsumesCollector& iC, UseEventSetupIn useEventSetupIn);
0184 
0185     /// clear decision vectors on a menu change
0186     void resetDecisionVectors();
0187     void resetPrescaleVectors();
0188     void resetMaskVectors();
0189     void loadPrescalesAndMasks();
0190 
0191     // trigger menu
0192     const L1TUtmTriggerMenu* m_l1GtMenu;
0193     unsigned long long m_l1GtMenuCacheID;
0194 
0195     // prescale factors
0196     bool m_readPrescalesFromFile;
0197     const l1t::PrescalesVetosFractHelper* m_l1GtPrescalesVetoes;
0198     unsigned long long m_l1GtPfAlgoCacheID;
0199 
0200     // prescales and masks
0201     bool m_filledPrescales;
0202 
0203     // algorithm maps
0204     //const AlgorithmMap* m_algorithmMap;
0205     const std::map<std::string, L1TUtmAlgorithm>* m_algorithmMap;
0206 
0207     // Number of physics triggers
0208     unsigned int m_numberPhysTriggers;
0209     const unsigned int m_maxNumberPhysTriggers = 512;
0210 
0211     //file  and container for prescale factors
0212     std::string m_preScaleFileName;
0213     unsigned int m_PreScaleColumn;
0214     unsigned int m_numberOfPreScaleColumns;
0215 
0216     std::vector<std::vector<double>> m_initialPrescaleFactorsAlgoTrig;
0217     const std::vector<std::vector<double>>* m_prescaleFactorsAlgoTrig;
0218     const std::map<int, std::vector<int>> m_initialTriggerMaskAlgoTrig;
0219     const std::map<int, std::vector<int>>* m_triggerMaskAlgoTrig;  // vector stores the BX
0220 
0221     // access to the results block from uGT
0222     edm::Handle<BXVector<GlobalAlgBlk>> m_uGtAlgBlk;
0223 
0224     // final OR
0225     bool m_finalOR;
0226 
0227     // Vectors containing the trigger name and information about that trigger
0228     std::vector<std::pair<std::string_view, bool>> m_decisionsInitial;
0229     std::vector<std::pair<std::string_view, bool>> m_decisionsInterm;
0230     std::vector<std::pair<std::string_view, bool>> m_decisionsFinal;
0231     std::vector<std::pair<std::string_view, double>> m_prescales;
0232     std::vector<std::pair<std::string_view, std::vector<int>>>
0233         m_masks;  // vector stores the bx's that are mask for given algo
0234 
0235     /// verbosity level
0236     int m_verbosity;
0237 
0238     std::unique_ptr<L1TGlobalUtilHelper> m_l1tGlobalUtilHelper;
0239 
0240     edm::ESGetToken<L1TUtmTriggerMenu, L1TUtmTriggerMenuRcd> m_L1TUtmTriggerMenuRunToken;
0241     edm::ESGetToken<L1TGlobalPrescalesVetosFract, L1TGlobalPrescalesVetosFractRcd>
0242         m_L1TGlobalPrescalesVetosFractRunToken;
0243 
0244     edm::ESGetToken<L1TUtmTriggerMenu, L1TUtmTriggerMenuRcd> m_L1TUtmTriggerMenuEventToken;
0245     edm::ESGetToken<L1TGlobalPrescalesVetosFract, L1TGlobalPrescalesVetosFractRcd>
0246         m_L1TGlobalPrescalesVetosFractEventToken;
0247   };
0248 
0249   template <typename T>
0250   L1TGlobalUtil::L1TGlobalUtil(edm::ParameterSet const& pset,
0251                                edm::ConsumesCollector&& iC,
0252                                T& module,
0253                                UseEventSetupIn useEventSetupIn)
0254       : L1TGlobalUtil(pset, iC, module, useEventSetupIn) {}
0255 
0256   template <typename T>
0257   L1TGlobalUtil::L1TGlobalUtil(edm::ParameterSet const& pset,
0258                                edm::ConsumesCollector& iC,
0259                                T& module,
0260                                UseEventSetupIn useEventSetupIn)
0261       : L1TGlobalUtil() {
0262     m_l1tGlobalUtilHelper = std::make_unique<L1TGlobalUtilHelper>(pset, iC, module);
0263     m_readPrescalesFromFile = m_l1tGlobalUtilHelper->readPrescalesFromFile();
0264     eventSetupConsumes(iC, useEventSetupIn);
0265   }
0266 
0267   template <typename T>
0268   L1TGlobalUtil::L1TGlobalUtil(edm::ParameterSet const& pset,
0269                                edm::ConsumesCollector&& iC,
0270                                T& module,
0271                                edm::InputTag const& l1tAlgBlkInputTag,
0272                                edm::InputTag const& l1tExtBlkInputTag,
0273                                UseEventSetupIn useEventSetupIn)
0274       : L1TGlobalUtil(pset, iC, module, l1tAlgBlkInputTag, l1tExtBlkInputTag, useEventSetupIn) {}
0275 
0276   template <typename T>
0277   L1TGlobalUtil::L1TGlobalUtil(edm::ParameterSet const& pset,
0278                                edm::ConsumesCollector& iC,
0279                                T& module,
0280                                edm::InputTag const& l1tAlgBlkInputTag,
0281                                edm::InputTag const& l1tExtBlkInputTag,
0282                                UseEventSetupIn useEventSetupIn)
0283       : L1TGlobalUtil() {
0284     m_l1tGlobalUtilHelper =
0285         std::make_unique<L1TGlobalUtilHelper>(pset, iC, module, l1tAlgBlkInputTag, l1tExtBlkInputTag);
0286     m_readPrescalesFromFile = m_l1tGlobalUtilHelper->readPrescalesFromFile();
0287     eventSetupConsumes(iC, useEventSetupIn);
0288   }
0289 }  // namespace l1t
0290 #endif