CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
PhotonMVAEstimator Class Reference

#include <PhotonMVAEstimator.h>

Inheritance diagram for PhotonMVAEstimator:
AnyMVAEstimatorRun2Base

Public Member Functions

int findCategory (const edm::Ptr< reco::Candidate > &candPtr) const override
 
float mvaValue (const edm::Ptr< reco::Candidate > &candPtr, const edm::EventBase &iEvent, int &iCategory) const override
 
 PhotonMVAEstimator (const edm::ParameterSet &conf)
 
void setConsumes (edm::ConsumesCollector &&) override
 
 ~PhotonMVAEstimator () override
 
- Public Member Functions inherited from AnyMVAEstimatorRun2Base
 AnyMVAEstimatorRun2Base (const edm::ParameterSet &conf)
 
const std::string & getName () const
 
int getNCategories () const
 
const std::string & getTag () const
 
bool isDebug () const
 
float mvaValue (const edm::Ptr< reco::Candidate > &candPtr, const edm::EventBase &iEvent) const
 
virtual ~AnyMVAEstimatorRun2Base ()
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 

Private Member Functions

int findCategory (const edm::Ptr< reco::Photon > &phoPtr) const
 

Private Attributes

std::vector< StringCutObjectSelector< reco::Photon > > categoryFunctions_
 
std::unique_ptr< EffectiveAreaseffectiveAreas_
 
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
 
MVAVariableManager< reco::PhotonmvaVarMngr_
 
int nCategories_
 
std::vector< int > nVariables_
 
double phoIsoCutoff_
 
std::vector< double > phoIsoPtScalingCoeff_
 
std::vector< std::vector< int > > variables_
 

Detailed Description

Definition at line 13 of file PhotonMVAEstimator.h.

Constructor & Destructor Documentation

PhotonMVAEstimator::PhotonMVAEstimator ( const edm::ParameterSet conf)

Definition at line 5 of file PhotonMVAEstimator.cc.

References categoryFunctions_, createGBRForest(), effectiveAreas_, Exception, gbrForests_, AnyMVAEstimatorRun2Base::getName(), AnyMVAEstimatorRun2Base::getNCategories(), edm::ParameterSet::getParameter(), AnyMVAEstimatorRun2Base::getTag(), MVAVariableManager< ParticleType >::getVarIndex(), mps_fire::i, mvaValue(), mvaVarMngr_, nVariables_, phoIsoCutoff_, phoIsoPtScalingCoeff_, TauGenJetsDecayModeSelectorAllHadrons_cfi::select, variables_, and mvaElectronID_Fall17_iso_V1_cff::weightFileNames.

7  , mvaVarMngr_ (conf.getParameter<std::string>("variableDefinition"))
8 {
9 
10  //
11  // Construct the MVA estimators
12  //
13  if (getTag() == "Run2Spring16NonTrigV1") {
14  effectiveAreas_ = std::make_unique<EffectiveAreas>((conf.getParameter<edm::FileInPath>("effAreasConfigFile")).fullPath());
15  phoIsoPtScalingCoeff_ = conf.getParameter<std::vector<double >>("phoIsoPtScalingCoeff");
16  phoIsoCutoff_ = conf.getParameter<double>("phoIsoCutoff");
17  }
18 
19  const std::vector <std::string> weightFileNames
20  = conf.getParameter<std::vector<std::string> >("weightFileNames");
21 
22  const std::vector <std::string> categoryCutStrings
23  = conf.getParameter<std::vector<std::string> >("categoryCuts");
24 
25  if( (int)(categoryCutStrings.size()) != getNCategories() )
26  throw cms::Exception("MVA config failure: ")
27  << "wrong number of category cuts in " << getName() << getTag() << std::endl;
28 
29  for (int i = 0; i < getNCategories(); ++i) {
30  StringCutObjectSelector<reco::Photon> select(categoryCutStrings[i]);
31  categoryFunctions_.push_back(select);
32  }
33 
34  // Initialize GBRForests
35  if( static_cast<int>(weightFileNames.size()) != getNCategories() )
36  throw cms::Exception("MVA config failure: ")
37  << "wrong number of weightfiles in " << getName() << getTag() << std::endl;
38 
39  gbrForests_.clear();
40  // Create a TMVA reader object for each category
41  for(int i=0; i<getNCategories(); i++){
42 
43  std::vector<int> variablesInCategory;
44 
45  std::vector<std::string> variableNamesInCategory;
46  gbrForests_.push_back(createGBRForest(weightFileNames[i], variableNamesInCategory));
47 
48  nVariables_.push_back(variableNamesInCategory.size());
49 
50  variables_.push_back(variablesInCategory);
51 
52  for (int j=0; j<nVariables_[i];++j) {
53  int index = mvaVarMngr_.getVarIndex(variableNamesInCategory[j]);
54  if(index == -1) {
55  throw cms::Exception("MVA config failure: ")
56  << "Concerning " << getName() << getTag() << std::endl
57  << "Variable " << variableNamesInCategory[j]
58  << " not found in variable definition file!" << std::endl;
59  }
60  variables_[i].push_back(index);
61 
62  }
63  }
64 }
T getParameter(std::string const &) const
AnyMVAEstimatorRun2Base(const edm::ParameterSet &conf)
MVAVariableManager< reco::Photon > mvaVarMngr_
const std::string & getName() const
const std::string & getTag() const
std::vector< StringCutObjectSelector< reco::Photon > > categoryFunctions_
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
std::vector< int > nVariables_
std::vector< double > phoIsoPtScalingCoeff_
int getVarIndex(const std::string &name)
std::unique_ptr< EffectiveAreas > effectiveAreas_
std::vector< std::vector< int > > variables_
std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightsFile)
PhotonMVAEstimator::~PhotonMVAEstimator ( )
inlineoverride

Definition at line 19 of file PhotonMVAEstimator.h.

References fillDescriptions(), findCategory(), iEvent, mvaValue(), and setConsumes().

19 {};

Member Function Documentation

static void PhotonMVAEstimator::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
static

Referenced by ~PhotonMVAEstimator().

int PhotonMVAEstimator::findCategory ( const edm::Ptr< reco::Candidate > &  candPtr) const
overridevirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 110 of file PhotonMVAEstimator.cc.

References Exception, and nullptr.

Referenced by mvaValue(), and ~PhotonMVAEstimator().

110  {
111 
112  const edm::Ptr<reco::Photon> phoPtr{ candPtr };
113  if( phoPtr.get() == nullptr ) {
114  throw cms::Exception("MVA failure: ")
115  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
116  << " but appears to be neither" << std::endl;
117  }
118 
119  return findCategory(phoPtr);
120 
121 }
#define nullptr
int findCategory(const edm::Ptr< reco::Candidate > &candPtr) const override
int PhotonMVAEstimator::findCategory ( const edm::Ptr< reco::Photon > &  phoPtr) const
private

Definition at line 123 of file PhotonMVAEstimator.cc.

References categoryFunctions_, AnyMVAEstimatorRun2Base::getName(), AnyMVAEstimatorRun2Base::getNCategories(), AnyMVAEstimatorRun2Base::getTag(), mps_fire::i, reco::LeafCandidate::pt(), and reco::Photon::superCluster().

123  {
124 
125  for (int i = 0; i < getNCategories(); ++i) {
126  if (categoryFunctions_[i](*phoPtr)) return i;
127  }
128 
129  edm::LogWarning ("MVA warning") <<
130  "category not defined for particle with pt " << phoPtr->pt() << " GeV, eta " <<
131  phoPtr->superCluster()->eta() << " in " << getName() << getTag();
132 
133  return -1;
134 
135 }
double pt() const final
transverse momentum
reco::SuperClusterRef superCluster() const override
Ref to SuperCluster.
const std::string & getName() const
const std::string & getTag() const
std::vector< StringCutObjectSelector< reco::Photon > > categoryFunctions_
float PhotonMVAEstimator::mvaValue ( const edm::Ptr< reco::Candidate > &  candPtr,
const edm::EventBase iEvent,
int &  iCategory 
) const
overridevirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 67 of file PhotonMVAEstimator.cc.

References funct::abs(), gather_cfg::cout, effectiveAreas_, Exception, findCategory(), gbrForests_, AnyMVAEstimatorRun2Base::getName(), MVAVariableManager< ParticleType >::getName(), AnyMVAEstimatorRun2Base::getTag(), MVAVariableManager< ParticleType >::getValue(), mps_fire::i, AnyMVAEstimatorRun2Base::isDebug(), Max(), mvaVarMngr_, nVariables_, phoIsoCutoff_, phoIsoPtScalingCoeff_, and variables_.

Referenced by PhotonMVAEstimator(), and ~PhotonMVAEstimator().

67  {
68 
69  const edm::Ptr<reco::Photon> phoPtr{ candPtr };
70  if( phoPtr.get() == nullptr) {
71  throw cms::Exception("MVA failure: ")
72  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
73  << " but appears to be neither" << std::endl;
74  }
75 
76  iCategory = findCategory( phoPtr );
77 
78  std::vector<float> vars;
79 
80  for (int i = 0; i < nVariables_[iCategory]; ++i) {
81  vars.push_back(mvaVarMngr_.getValue(variables_[iCategory][i], phoPtr, iEvent));
82  }
83 
84  // Special case for Spring16!
85  if (getTag() == "Run2Spring16NonTrigV1" and iCategory == 1) { // Endcap category
86  // Raw value for EB only, because of loss of transparency in EE
87  // for endcap MVA only in 2016
88  double eA = effectiveAreas_->getEffectiveArea( std::abs(phoPtr->superCluster()->eta()) );
89  double phoIsoCorr = vars[10] - eA*(double)vars[9] - phoIsoPtScalingCoeff_.at(1) * phoPtr->pt();
90  vars[10] = TMath::Max( phoIsoCorr, phoIsoCutoff_);
91  }
92 
93  if(isDebug()) {
94  std::cout << " *** Inside " << getName() << getTag() << std::endl;
95  std::cout << " category " << iCategory << std::endl;
96  for (int i = 0; i < nVariables_[iCategory]; ++i) {
97  std::cout << " " << mvaVarMngr_.getName(variables_[iCategory][i]) << " " << vars[i] << std::endl;
98  }
99  }
100 
101  const float response = gbrForests_.at(iCategory)->GetResponse(vars.data());
102 
103  if(isDebug()) {
104  std::cout << " ### MVA " << response << std::endl << std::endl;
105  }
106 
107  return response;
108 }
const std::string & getName(int index) const
MVAVariableManager< reco::Photon > mvaVarMngr_
const std::string & getName() const
const std::string & getTag() const
int findCategory(const edm::Ptr< reco::Candidate > &candPtr) const override
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
T Max(T a, T b)
Definition: MathUtil.h:44
std::vector< int > nVariables_
std::vector< double > phoIsoPtScalingCoeff_
float getValue(int index, const edm::Ptr< ParticleType > &ptclPtr, const edm::EventBase &iEvent) const
std::unique_ptr< EffectiveAreas > effectiveAreas_
std::vector< std::vector< int > > variables_
void PhotonMVAEstimator::setConsumes ( edm::ConsumesCollector &&  cc)
overridevirtual

Reimplemented from AnyMVAEstimatorRun2Base.

Definition at line 137 of file PhotonMVAEstimator.cc.

References DEFINE_EDM_PLUGIN, eostools::move(), mvaVarMngr_, and MVAVariableManager< ParticleType >::setConsumes().

Referenced by ~PhotonMVAEstimator().

137  {
138  // All tokens for event content needed by this MVA
139  // Tags from the variable helper
141 }
void setConsumes(edm::ConsumesCollector &&cc)
MVAVariableManager< reco::Photon > mvaVarMngr_
def move(src, dest)
Definition: eostools.py:511

Member Data Documentation

std::vector<StringCutObjectSelector<reco::Photon> > PhotonMVAEstimator::categoryFunctions_
private

Definition at line 38 of file PhotonMVAEstimator.h.

Referenced by findCategory(), and PhotonMVAEstimator().

std::unique_ptr<EffectiveAreas> PhotonMVAEstimator::effectiveAreas_
private

Definition at line 52 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), and PhotonMVAEstimator().

std::vector< std::unique_ptr<const GBRForest> > PhotonMVAEstimator::gbrForests_
private

Definition at line 42 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), and PhotonMVAEstimator().

MVAVariableManager<reco::Photon> PhotonMVAEstimator::mvaVarMngr_
private

Definition at line 49 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), PhotonMVAEstimator(), and setConsumes().

int PhotonMVAEstimator::nCategories_
private

Definition at line 37 of file PhotonMVAEstimator.h.

std::vector<int> PhotonMVAEstimator::nVariables_
private

Definition at line 39 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), and PhotonMVAEstimator().

double PhotonMVAEstimator::phoIsoCutoff_
private

Definition at line 54 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), and PhotonMVAEstimator().

std::vector<double> PhotonMVAEstimator::phoIsoPtScalingCoeff_
private

Definition at line 53 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), and PhotonMVAEstimator().

std::vector<std::vector<int> > PhotonMVAEstimator::variables_
private

Definition at line 46 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), and PhotonMVAEstimator().