Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-21 02:12:21

0001 #ifndef DataFormats_PatCandidates_PackedTriggerPrescales_h
0002 #define DataFormats_PatCandidates_PackedTriggerPrescales_h
0003 
0004 #include <cstring>
0005 #include <type_traits>
0006 
0007 #include "FWCore/Common/interface/TriggerNames.h"
0008 #include "DataFormats/Common/interface/TriggerResults.h"
0009 #include "DataFormats/Common/interface/Ref.h"
0010 
0011 namespace pat {
0012 
0013   class PackedTriggerPrescales {
0014   public:
0015     PackedTriggerPrescales() : triggerNames_(nullptr) {}
0016     PackedTriggerPrescales(const edm::Handle<edm::TriggerResults> &handle);
0017     ~PackedTriggerPrescales() = default;
0018 
0019     // get prescale by index
0020     //  method templated to force correct choice of output type
0021     //  (as part of deprecating integer types for trigger prescales)
0022     template <typename T = int>
0023     T getPrescaleForIndex(int index) const;
0024 
0025     // get prescale by name or name prefix (if setTriggerNames was called)
0026     //  method templated to force correct choice of output type
0027     //  (as part of deprecating integer types for trigger prescales)
0028     template <typename T = int>
0029     T getPrescaleForName(const std::string &name, bool prefixOnly = false) const;
0030 
0031     // return the TriggerResults associated with this
0032     const edm::TriggerResults &triggerResults() const { return *edm::getProduct<edm::TriggerResults>(triggerResults_); }
0033 
0034     // use this method first if you want to be able to access the prescales by name
0035     // you can get the TriggerNames from the TriggerResults and the Event (edm or fwlite)
0036     void setTriggerNames(const edm::TriggerNames &names) { triggerNames_ = &names; }
0037 
0038     // set that the trigger of given index has a given prescale
0039     void addPrescaledTrigger(int index, double prescale);
0040 
0041   protected:
0042     std::vector<double> prescaleValues_;
0043     edm::RefCore triggerResults_;
0044     const edm::TriggerNames *triggerNames_;
0045   };
0046 
0047   template <typename T>
0048   T PackedTriggerPrescales::getPrescaleForIndex(int index) const {
0049     static_assert(std::is_same_v<T, double>,
0050                   "\n\n\tPlease use getPrescaleForIndex<double> "
0051                   "(other types for trigger prescales are not supported anymore by PackedTriggerPrescales)");
0052     if (unsigned(index) >= triggerResults().size())
0053       throw cms::Exception("InvalidReference", "Index out of bounds");
0054     return prescaleValues_[index];
0055   }
0056 
0057   template <typename T>
0058   T PackedTriggerPrescales::getPrescaleForName(const std::string &name, bool prefixOnly) const {
0059     static_assert(std::is_same_v<T, double>,
0060                   "\n\n\tPlease use getPrescaleForName<double> "
0061                   "(other types for trigger prescales are not supported anymore by PackedTriggerPrescales)");
0062     if (triggerNames_ == nullptr)
0063       throw cms::Exception("LogicError", "getPrescaleForName called without having called setTriggerNames first");
0064     if (prefixOnly) {
0065       auto const &names = triggerNames_->triggerNames();
0066       if (name.empty())
0067         throw cms::Exception("EmptyName",
0068                              "getPrescaleForName called with invalid arguments (name is empty, prefixOnly=true");
0069       size_t siz = name.length() - 1;
0070       while (siz > 0 && (name[siz] == '*' || name[siz] == '\0'))
0071         siz--;
0072       for (unsigned int i = 0, n = names.size(); i < n; ++i) {
0073         if (strncmp(name.c_str(), names[i].c_str(), siz) == 0) {
0074           return getPrescaleForIndex<T>(i);
0075         }
0076       }
0077       throw cms::Exception("InvalidReference", "Index out of bounds");
0078     } else {
0079       int index = triggerNames_->triggerIndex(name);
0080       return getPrescaleForIndex<T>(index);
0081     }
0082   }
0083 
0084 }  // namespace pat
0085 
0086 #endif  // DataFormats_PatCandidates_PackedTriggerPrescales_h