Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:17:17

0001 #ifndef RecoBTau_JetTagComputer_h
0002 #define RecoBTau_JetTagComputer_h
0003 
0004 #include <vector>
0005 #include <string>
0006 
0007 #include "FWCore/Framework/interface/EventSetup.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 #include "FWCore/Utilities/interface/Exception.h"
0010 #include "DataFormats/BTauReco/interface/BaseTagInfo.h"
0011 
0012 class JetTagComputerRecord;
0013 
0014 class JetTagComputer {
0015 public:
0016   class TagInfoHelper {
0017   public:
0018     TagInfoHelper(const std::vector<const reco::BaseTagInfo *> &infos, std::vector<std::string> &labels)
0019         : m_tagInfos(infos), m_labels(labels) {}
0020 
0021     TagInfoHelper(const std::vector<const reco::BaseTagInfo *> &infos) : m_tagInfos(infos), m_labels() {}
0022 
0023     ~TagInfoHelper() {}
0024 
0025     const reco::BaseTagInfo &getBase(unsigned int index) const {
0026       if (index >= m_tagInfos.size())
0027         throw cms::Exception("InvalidIndex") << "Invalid index " << index
0028                                              << " "
0029                                                 "in call to JetTagComputer::get."
0030                                              << std::endl;
0031 
0032       const reco::BaseTagInfo *info = m_tagInfos[index];
0033       if (!info)
0034         throw cms::Exception("ProductMissing") << "Missing TagInfo "
0035                                                   "in call to JetTagComputer::get."
0036                                                << std::endl;
0037 
0038       return *info;
0039     }
0040 
0041     template <class T>
0042     const T &get(unsigned int index = 0) const {
0043       const reco::BaseTagInfo *info = &getBase(index);
0044       const T *castInfo = dynamic_cast<const T *>(info);
0045       if (!castInfo)
0046         throw cms::Exception("InvalidCast") << "Invalid TagInfo cast "
0047                                                "in call to JetTagComputer::get( index="
0048                                             << index << " )." << std::endl;
0049 
0050       return *castInfo;
0051     }
0052 
0053     template <class T>
0054     const T &get(std::string label) const {
0055       size_t idx = 0;
0056       for (; idx <= m_labels.size(); idx++) {
0057         if (idx < m_labels.size() && m_labels[idx] == label)
0058           break;
0059       }
0060 
0061       if (idx == m_labels.size()) {
0062         throw cms::Exception("ProductMissing")
0063             << "Missing TagInfo with label: " << label << " in call to JetTagComputer::get." << std::endl;
0064       }
0065       return get<T>(idx);
0066     }
0067 
0068   private:
0069     const std::vector<const reco::BaseTagInfo *> &m_tagInfos;
0070     std::vector<std::string> m_labels;
0071   };
0072 
0073   // default constructor
0074   JetTagComputer() : m_setupDone(false) {}
0075   virtual ~JetTagComputer() {}
0076 
0077   // explicit constructor accepting a ParameterSet for configuration
0078   explicit JetTagComputer(const edm::ParameterSet &configuration) : m_setupDone(false) {}
0079 
0080   virtual void initialize(const JetTagComputerRecord &) {}
0081 
0082   float operator()(const reco::BaseTagInfo &info) const;
0083   inline float operator()(const TagInfoHelper &helper) const { return discriminator(helper); }
0084 
0085   inline const std::vector<std::string> &getInputLabels() const { return m_inputLabels; }
0086 
0087   void setupDone() { m_setupDone = true; }
0088 
0089 protected:
0090   void uses(unsigned int id, const std::string &label);
0091   void uses(const std::string &label) { uses(0, label); }
0092 
0093   virtual float discriminator(const reco::BaseTagInfo &) const;
0094   virtual float discriminator(const TagInfoHelper &) const;
0095 
0096 private:
0097   std::vector<std::string> m_inputLabels;
0098   bool m_setupDone;
0099 };
0100 
0101 #endif  // RecoBTau_JetTagComputer_h