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 4 of file PhotonMVAEstimator.cc.

References categoryFunctions_, GBRForestTools::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.

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

References Exception, and nullptr.

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

109  {
110 
111  const edm::Ptr<reco::Photon> phoPtr{ candPtr };
112  if( phoPtr.get() == nullptr ) {
113  throw cms::Exception("MVA failure: ")
114  << " given particle is expected to be reco::Photon or pat::Photon," << std::endl
115  << " but appears to be neither" << std::endl;
116  }
117 
118  return findCategory(phoPtr);
119 
120 }
#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 122 of file PhotonMVAEstimator.cc.

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

122  {
123 
124  for (int i = 0; i < getNCategories(); ++i) {
125  if (categoryFunctions_[i](*phoPtr)) return i;
126  }
127 
128  edm::LogWarning ("MVA warning") <<
129  "category not defined for particle with pt " << phoPtr->pt() << " GeV, eta " <<
130  phoPtr->superCluster()->eta() << " in " << getName() << getTag();
131 
132  return -1;
133 
134 }
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 66 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().

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

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

Referenced by ~PhotonMVAEstimator().

136  {
137  // All tokens for event content needed by this MVA
138  // Tags from the variable helper
140 }
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().