Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-12 22:40:26

0001 #ifndef L1TGlobal_l1TGlobalUtilHelper_h
0002 #define L1TGlobal_l1TGlobalUtilHelper_h
0003 
0004 /**
0005  * \class L1TGlobalUtilHelper
0006  *
0007  * Does the same for L1TGlobalUtil as L1GtUtilsHelper does for L1GtUtils:
0008  * Description: Gets tokens for L1GtUtils to use when getting products
0009  *              from the Event and Run. This class was introduced
0010  *              when the consumes function calls were added for L1GtUtils.
0011  *              It preserves the special feature of L1GtUtils that allows
0012  *              it to run without configuration of InputTags, although it
0013  *              allows InputTags to be configured optionally or passed in
0014  *              via the constructor arguments.
0015  *
0016  * \author: W.David Dagenhart - Fermilab 30 April 2015
0017  *
0018  */
0019 
0020 #include "DataFormats/L1TGlobal/interface/GlobalAlgBlk.h"
0021 #include "DataFormats/L1TGlobal/interface/GlobalExtBlk.h"
0022 
0023 #include "FWCore/Framework/interface/ConsumesCollector.h"
0024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0025 #include "FWCore/Utilities/interface/EDGetToken.h"
0026 #include "FWCore/Utilities/interface/InputTag.h"
0027 
0028 #include <string>
0029 #include <utility>
0030 
0031 namespace edm {
0032   class BranchDescription;
0033   class ParameterSetDescription;
0034 }  // namespace edm
0035 
0036 namespace l1t {
0037 
0038   class L1TGlobalUtilHelper {
0039   public:
0040     // Using this constructor will require InputTags to be specified in the configuration
0041     L1TGlobalUtilHelper(edm::ParameterSet const& pset, edm::ConsumesCollector& iC);
0042 
0043     // Using this constructor will cause it to look for valid InputTags in
0044     // the following ways in the specified order until they are found.
0045     //   1. The configuration
0046     //   2. Search all products from the preferred input tags for the required type
0047     //   3. Search all products from any other process for the required type
0048     template <typename T>
0049     L1TGlobalUtilHelper(edm::ParameterSet const& pset, edm::ConsumesCollector& iC, T& module);
0050 
0051     // Using this constructor will cause it to look for valid InputTags in
0052     // the following ways in the specified order until they are found.
0053     //   1. The constructor arguments
0054     //   2. The configuration
0055     //   3. Search all products from the preferred input tags for the required type
0056     //   4. Search all products from any other process for the required type
0057     template <typename T>
0058     L1TGlobalUtilHelper(edm::ParameterSet const& pset,
0059                         edm::ConsumesCollector& iC,
0060                         T& module,
0061                         edm::InputTag const& l1tAlgBlkInputTag,
0062                         edm::InputTag const& l1tExtBlkInputTag);
0063 
0064     // A module defining its fillDescriptions function might want to use this
0065     static void fillDescription(edm::ParameterSetDescription& desc,
0066                                 edm::InputTag const& iAlg,
0067                                 edm::InputTag const& iExt,
0068                                 bool readPrescalesFromFile);
0069 
0070     edm::InputTag const& l1tAlgBlkInputTag() const { return m_l1tAlgBlkInputTag; }
0071     edm::InputTag const& l1tExtBlkInputTag() const { return m_l1tExtBlkInputTag; }
0072 
0073     bool const& readPrescalesFromFile() const { return m_readPrescalesFromFile; }
0074 
0075     edm::EDGetTokenT<GlobalAlgBlkBxCollection> const& l1tAlgBlkToken() const { return m_l1tAlgBlkToken; }
0076     edm::EDGetTokenT<GlobalExtBlkBxCollection> const& l1tExtBlkToken() const { return m_l1tExtBlkToken; }
0077 
0078   private:
0079     // Callback which will be registered with the Framework if the InputTags
0080     // are not specified in the configuration or constructor arguments. It
0081     // will get called for each product in the ProductRegistry.
0082     void checkToUpdateTags(edm::BranchDescription const& branchDescription,
0083                            edm::ConsumesCollector,
0084                            bool findL1TAlgBlk,
0085                            bool findL1TExtBlk);
0086 
0087     edm::InputTag m_l1tAlgBlkInputTag;
0088     edm::InputTag m_l1tExtBlkInputTag;
0089 
0090     edm::EDGetTokenT<GlobalAlgBlkBxCollection> m_l1tAlgBlkToken;
0091     edm::EDGetTokenT<GlobalExtBlkBxCollection> m_l1tExtBlkToken;
0092 
0093     bool m_readPrescalesFromFile;
0094   };
0095 
0096   template <typename T>
0097   L1TGlobalUtilHelper::L1TGlobalUtilHelper(edm::ParameterSet const& pset, edm::ConsumesCollector& iC, T& module)
0098       : L1TGlobalUtilHelper(pset, iC, module, edm::InputTag(), edm::InputTag()) {}
0099 
0100   template <typename T>
0101   L1TGlobalUtilHelper::L1TGlobalUtilHelper(edm::ParameterSet const& pset,
0102                                            edm::ConsumesCollector& iC,
0103                                            T& module,
0104                                            edm::InputTag const& l1tAlgBlkInputTag,
0105                                            edm::InputTag const& l1tExtBlkInputTag)
0106       :  // Set InputTags from arguments
0107         m_l1tAlgBlkInputTag(l1tAlgBlkInputTag),
0108         m_l1tExtBlkInputTag(l1tExtBlkInputTag),
0109         m_readPrescalesFromFile(false) {
0110     if (pset.existsAs<bool>("ReadPrescalesFromFile")) {
0111       m_readPrescalesFromFile = pset.getParameter<bool>("ReadPrescalesFromFile");
0112     }
0113     // If the InputTags are not set to valid values by the arguments, then
0114     // try to set them from the configuration.
0115     if (m_l1tAlgBlkInputTag.label().empty() && pset.existsAs<edm::InputTag>("l1tAlgBlkInputTag")) {
0116       m_l1tAlgBlkInputTag = pset.getParameter<edm::InputTag>("l1tAlgBlkInputTag");
0117     }
0118     if (m_l1tExtBlkInputTag.label().empty() && pset.existsAs<edm::InputTag>("l1tExtBlkInputTag")) {
0119       m_l1tExtBlkInputTag = pset.getParameter<edm::InputTag>("l1tExtBlkInputTag");
0120     }
0121 
0122     // If the InputTags were set to valid values, make the consumes calls.
0123     if (!m_l1tAlgBlkInputTag.label().empty()) {
0124       m_l1tAlgBlkToken = iC.consumes<GlobalAlgBlkBxCollection>(m_l1tAlgBlkInputTag);
0125     }
0126 
0127     if (!m_l1tExtBlkInputTag.label().empty()) {
0128       m_l1tExtBlkToken = iC.consumes<GlobalExtBlkBxCollection>(m_l1tExtBlkInputTag);
0129     }
0130 
0131     // Do we still need to search for each InputTag?
0132     bool findL1TAlgBlk = m_l1tAlgBlkInputTag.label().empty();
0133     bool findL1TExtBlk = m_l1tExtBlkInputTag.label().empty();
0134 
0135     // Register the callback function with the Framework
0136     // if any InputTags still need to be found.
0137     if (findL1TAlgBlk || findL1TExtBlk) {
0138       auto const* pModule = &module;
0139       module.callWhenNewProductsRegistered([this, findL1TAlgBlk, findL1TExtBlk, iC, pModule](auto iBranch) {
0140         try {
0141           checkToUpdateTags(iBranch, iC, findL1TAlgBlk, findL1TExtBlk);
0142         } catch (cms::Exception& iExcept) {
0143           auto const& label = pModule->moduleDescription().moduleLabel();
0144           iExcept.addContext(std::string("Running 'callWhenNewProductRegistered' for module ") + label);
0145           throw;
0146         }
0147       });
0148     }
0149   }
0150 
0151 }  // namespace l1t
0152 
0153 #endif