CMS 3D CMS Logo

RecoTauDiscriminantFromDiscriminator.cc
Go to the documentation of this file.
1 /*
2  * RecoTauDiscriminantFromDiscriminator
3  *
4  * Makes a discriminator function from a PFRecoTauDiscriminator stored in the
5  * event.
6  *
7  * Author: Evan K. Friis (UC Davis)
8  *
9  */
10 
11 #include <sstream>
15 
16 namespace reco::tau {
17 
19  public:
21  const edm::ParameterSet& pset);
22  void beginEvent() override;
23  std::vector<double> operator()(const reco::PFTauRef& tau) const override;
24  private:
25  bool takeAbs_;
26  double min_;
27  double max_;
28  typedef std::pair<edm::InputTag, edm::Handle<reco::PFTauDiscriminator> > DiscInfo;
29  std::vector<DiscInfo> discriminators_;
30 };
31 
34  takeAbs_ = pset.existsAs<bool>("takeAbs") ?
35  pset.getParameter<bool>("takeAbs") : false;
36  min_ = pset.existsAs<double>("minValue") ?
37  pset.getParameter<double>("minValue") : -1*std::numeric_limits<double>::max();
38  max_ = pset.existsAs<double>("maxValue") ?
39  pset.getParameter<double>("maxValue") : std::numeric_limits<double>::max();
40 
41  if (pset.existsAs<edm::InputTag>("discSrc")) {
42  discriminators_.push_back(std::make_pair(
43  pset.getParameter<edm::InputTag>("discSrc"),
45  } else {
46  // Get multiple discriminators. This supports the case when the MVAHelper
47  // class might be dealing with multiple tau collections (training)
48  std::vector<edm::InputTag> discriminators =
49  pset.getParameter<std::vector<edm::InputTag> >("discSrc");
50  for(auto const& tag : discriminators) {
51  discriminators_.push_back(std::make_pair(
53  }
54  }
55 }
56 
57 // Called by base class at the beginning of every event
59  for(auto& discInfo : discriminators_) {
60  evt()->getByLabel(discInfo.first, discInfo.second);
61  }
62 }
63 
65  const reco::PFTauRef& tau) const {
66  edm::ProductID tauProdId = tau.id();
67  double result = -999;
68  bool foundGoodDiscriminator = false;
69  for (size_t i = 0; i < discriminators_.size(); ++i) {
70  // Check if the discriminator actually exists
71  if (!discriminators_[i].second.isValid())
72  continue;
73  const reco::PFTauDiscriminator& disc = *(discriminators_[i].second);
74  if (tauProdId == disc.keyProduct().id()) {
75  foundGoodDiscriminator = true;
76  result = (disc)[tau];
77  break;
78  }
79  }
80  // In case no discriminator is found.
81  if (!foundGoodDiscriminator) {
82  std::stringstream error;
83  error << "Couldn't find a PFTauDiscriminator usable with given tau."
84  << std::endl << " Input tau has product id: " << tau.id() << std::endl;
85  for (size_t i = 0; i < discriminators_.size(); ++i ) {
86  error << "disc: " << discriminators_[i].first;
87  error << " isValid: " << discriminators_[i].second.isValid();
88  if (discriminators_[i].second.isValid()) {
89  error << " product: " << discriminators_[i].second->keyProduct().id();
90  }
91  error << std::endl;
92  }
93  edm::LogError("BadDiscriminatorConfiguration") << error.str();
94  }
95  if (result < min_)
96  result = min_;
97  if (result > max_)
98  result = max_;
99  return std::vector<double>(1, result);
100 }
101 
102 } // end namespace reco::tau
103 
107  "RecoTauDiscriminantFromDiscriminator");
T getParameter(std::string const &) const
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:185
std::vector< double > operator()(const reco::PFTauRef &tau) const override
ProductID id() const
Accessor for product ID.
Definition: Ref.h:257
U second(std::pair< T, U > const &p)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:535
KeyRefProd const & keyProduct() const
std::pair< edm::InputTag, edm::Handle< reco::PFTauDiscriminator > > DiscInfo
#define DEFINE_EDM_PLUGIN(factory, type, name)