CMS 3D CMS Logo

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

#include <PhotonMVAEstimator.h>

Inheritance diagram for PhotonMVAEstimator:
AnyMVAEstimatorRun2Base

Public Types

enum  mvaCategories { CAT_EB = 0, CAT_EE = 1 }
 

Public Member Functions

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

Public Attributes

const int nCategories_ = 2
 

Private Attributes

const bool debug_
 
const double ebeeSplit_
 
std::unique_ptr< EffectiveAreaseffectiveAreas_
 
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
 
const std::string methodName_
 
MVAVariableManager< reco::PhotonmvaVarMngr_
 
const std::string name_
 
std::vector< int > nVariables_
 
double phoIsoCutoff_
 
std::vector< double > phoIsoPtScalingCoeff_
 
const std::string tag_
 
std::vector< std::vector< int > > variables_
 

Detailed Description

Definition at line 22 of file PhotonMVAEstimator.h.

Member Enumeration Documentation

Enumerator
CAT_EB 
CAT_EE 

Definition at line 29 of file PhotonMVAEstimator.h.

Constructor & Destructor Documentation

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

Definition at line 3 of file PhotonMVAEstimator.cc.

References GBRForestTools::createGBRForest(), effectiveAreas_, Exception, gbrForests_, edm::ParameterSet::getParameter(), MVAVariableManager< ParticleType >::getVarIndex(), mps_fire::i, mvaVarMngr_, name_, nCategories_, nVariables_, phoIsoCutoff_, phoIsoPtScalingCoeff_, tag_, variables_, mvaElectronID_Fall17_iso_V1_cff::weightFileNames, and ~PhotonMVAEstimator().

3  :
5  name_(conf.getParameter<std::string>("mvaName")),
6  tag_(conf.getParameter<std::string>("mvaTag")),
7  methodName_("BDTG method"),
8  mvaVarMngr_(conf.getParameter<std::string>("variableDefinition")),
9  ebeeSplit_ (conf.getParameter<double> ("ebeeSplit")),
10  debug_(conf.getUntrackedParameter<bool>("debug", false))
11 {
12  //
13  // Construct the MVA estimators
14  //
15  if (tag_ == "Run2Spring16NonTrigV1") {
16  effectiveAreas_ = std::make_unique<EffectiveAreas>((conf.getParameter<edm::FileInPath>("effAreasConfigFile")).fullPath());
17  phoIsoPtScalingCoeff_ = conf.getParameter<std::vector<double >>("phoIsoPtScalingCoeff");
18  phoIsoCutoff_ = conf.getParameter<double>("phoIsoCutoff");
19  }
20 
21  const std::vector <std::string> weightFileNames
22  = conf.getParameter<std::vector<std::string> >("weightFileNames");
23 
24  // Initialize GBRForests
25  if( (int)(weightFileNames.size()) != nCategories_ )
26  throw cms::Exception("MVA config failure: ")
27  << "wrong number of weightfiles in " << name_ << tag_ << std::endl;
28 
29  gbrForests_.clear();
30  // Create a TMVA reader object for each category
31  for(int i=0; i<nCategories_; i++){
32 
33  std::vector<std::string> variableNamesInCategory;
34  std::vector<int> variablesInCategory;
35 
36  // Use unique_ptr so that all readers are properly cleaned up
37  // when the vector clear() is called in the destructor
38 
39  gbrForests_.push_back( GBRForestTools::createGBRForest( weightFileNames[i], variableNamesInCategory ) );
40 
41  nVariables_.push_back(variableNamesInCategory.size());
42 
43  variables_.push_back(variablesInCategory);
44 
45  for (int j=0; j<nVariables_[i];++j) {
46  int index = mvaVarMngr_.getVarIndex(variableNamesInCategory[j]);
47  if(index == -1) {
48  throw cms::Exception("MVA config failure: ")
49  << "Concerning " << name_ << tag_ << std::endl
50  << "Variable " << variableNamesInCategory[j]
51  << " not found in variable definition file!" << std::endl;
52  }
53  variables_[i].push_back(index);
54 
55  }
56  }
57 }
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
AnyMVAEstimatorRun2Base(const edm::ParameterSet &conf)
const std::string methodName_
MVAVariableManager< reco::Photon > mvaVarMngr_
const std::string tag_
static std::unique_ptr< const GBRForest > createGBRForest(const std::string &weightFile)
int getVarIndex(std::string &name)
std::vector< std::unique_ptr< const GBRForest > > gbrForests_
std::vector< int > nVariables_
std::vector< double > phoIsoPtScalingCoeff_
const std::string name_
std::unique_ptr< EffectiveAreas > effectiveAreas_
std::vector< std::vector< int > > variables_
PhotonMVAEstimator::~PhotonMVAEstimator ( )
override

Definition at line 60 of file PhotonMVAEstimator.cc.

References mvaValue().

Referenced by PhotonMVAEstimator().

60  {
61 }

Member Function Documentation

std::unique_ptr<const GBRForest> PhotonMVAEstimator::createSingleReader ( const int  iCategory,
const edm::FileInPath weightFile 
)
int PhotonMVAEstimator::findCategory ( const edm::Ptr< reco::Candidate > &  candPtr) const
overridevirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 107 of file PhotonMVAEstimator.cc.

References funct::abs(), CAT_EB, CAT_EE, ebeeSplit_, PVValHelper::eta, Exception, and edm::Ptr< T >::get().

Referenced by getTag(), and mvaValue().

107  {
108 
109  // Try to cast the particle into a reco particle.
110  // This should work for both reco and pat.
111  auto pho = dynamic_cast<reco::Photon const*>(candPtr.get());
112 
113  if( pho == 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  float eta = pho->superCluster()->eta();
120 
121  //
122  // Determine the category
123  //
124  if ( std::abs(eta) < ebeeSplit_)
125  return CAT_EB;
126  else
127  return CAT_EE;
128 }
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:159
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const std::string& PhotonMVAEstimator::getName ( void  ) const
inlinefinalvirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 45 of file PhotonMVAEstimator.h.

References name_.

Referenced by plotting.Plot::draw().

45 { return name_; }
const std::string name_
int PhotonMVAEstimator::getNCategories ( ) const
inlineoverridevirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 44 of file PhotonMVAEstimator.h.

References nCategories_.

44 { return nCategories_; }
const std::string& PhotonMVAEstimator::getTag ( ) const
inlinefinalvirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 46 of file PhotonMVAEstimator.h.

References findCategory(), setConsumes(), and tag_.

46 { return tag_; }
const std::string tag_
float PhotonMVAEstimator::mvaValue ( const edm::Ptr< reco::Candidate > &  candPtr,
const edm::EventBase iEvent 
) const
overridevirtual

Implements AnyMVAEstimatorRun2Base.

Definition at line 64 of file PhotonMVAEstimator.cc.

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

Referenced by ~PhotonMVAEstimator().

64  {
65 
66  const int iCategory = findCategory( candPtr );
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  std::vector<float> vars;
76 
77  for (int i = 0; i < nVariables_[iCategory]; ++i) {
78  vars.push_back(mvaVarMngr_.getValue(variables_[iCategory][i], phoPtr, iEvent));
79  }
80 
81  // Special case for Spring16!
82  if (tag_ == "Run2Spring16NonTrigV1" and iCategory == CAT_EE) {
83  // Raw value for EB only, because of loss of transparency in EE
84  // for endcap MVA only in 2016
85  double eA = effectiveAreas_->getEffectiveArea( std::abs(phoPtr->superCluster()->eta()) );
86  double phoIsoCorr = vars[10] - eA*(double)vars[9] - phoIsoPtScalingCoeff_.at(1) * phoPtr->pt();
87  vars[10] = TMath::Max( phoIsoCorr, phoIsoCutoff_);
88  }
89 
90  if(debug_) {
91  std::cout << " *** Inside " << name_ << tag_ << std::endl;
92  std::cout << " category " << iCategory << std::endl;
93  for (int i = 0; i < nVariables_[iCategory]; ++i) {
94  std::cout << " " << mvaVarMngr_.getName(variables_[iCategory][i]) << " " << vars[i] << std::endl;
95  }
96  }
97 
98  const float response = gbrForests_.at(iCategory)->GetResponse(vars.data());
99 
100  if(debug_) {
101  std::cout << " ### MVA " << response << std::endl << std::endl;
102  }
103 
104  return response;
105 }
const std::string getName(int index) const
MVAVariableManager< reco::Photon > mvaVarMngr_
const std::string tag_
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
const std::string name_
std::unique_ptr< EffectiveAreas > effectiveAreas_
std::vector< std::vector< int > > variables_
vars
Definition: DeepTauId.cc:77
void PhotonMVAEstimator::setConsumes ( edm::ConsumesCollector &&  cc) const
overridevirtual

Reimplemented from AnyMVAEstimatorRun2Base.

Definition at line 130 of file PhotonMVAEstimator.cc.

References DEFINE_EDM_PLUGIN, MVAVariableManager< ParticleType >::getGlobalInputTags(), MVAVariableManager< ParticleType >::getHelperInputTags(), mvaVarMngr_, and GlobalPosition_Frontier_DevDB_cff::tag.

Referenced by getTag().

130  {
131  // All tokens for event content needed by this MVA
132  // Tags from the variable helper
133  for (auto &tag : mvaVarMngr_.getHelperInputTags()) {
135  }
136  for (auto &tag : mvaVarMngr_.getGlobalInputTags()) {
137  cc.consumes<double>(tag);
138  }
139 }
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
MVAVariableManager< reco::Photon > mvaVarMngr_
std::vector< edm::InputTag > getHelperInputTags() const
std::vector< edm::InputTag > getGlobalInputTags() const

Member Data Documentation

const bool PhotonMVAEstimator::debug_
private

Definition at line 83 of file PhotonMVAEstimator.h.

Referenced by mvaValue().

const double PhotonMVAEstimator::ebeeSplit_
private

Definition at line 81 of file PhotonMVAEstimator.h.

Referenced by findCategory().

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

Definition at line 86 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), and PhotonMVAEstimator().

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

Definition at line 69 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), and PhotonMVAEstimator().

const std::string PhotonMVAEstimator::methodName_
private

Definition at line 72 of file PhotonMVAEstimator.h.

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

Definition at line 79 of file PhotonMVAEstimator.h.

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

const std::string PhotonMVAEstimator::name_
private

Definition at line 59 of file PhotonMVAEstimator.h.

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

const int PhotonMVAEstimator::nCategories_ = 2

Definition at line 28 of file PhotonMVAEstimator.h.

Referenced by getNCategories(), and PhotonMVAEstimator().

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

Definition at line 66 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), and PhotonMVAEstimator().

double PhotonMVAEstimator::phoIsoCutoff_
private

Definition at line 88 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), and PhotonMVAEstimator().

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

Definition at line 87 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), and PhotonMVAEstimator().

const std::string PhotonMVAEstimator::tag_
private

Definition at line 64 of file PhotonMVAEstimator.h.

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

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

Definition at line 76 of file PhotonMVAEstimator.h.

Referenced by mvaValue(), and PhotonMVAEstimator().