CMS 3D CMS Logo

ElectronMVAEstimatorRun2Spring16HZZ.h
Go to the documentation of this file.
1 #ifndef RecoEgamma_ElectronIdentification_ElectronMVAEstimatorRun2Spring16HZZ_H
2 #define RecoEgamma_ElectronIdentification_ElectronMVAEstimatorRun2Spring16HZZ_H
3 
5 
7 
9 
13 
15 
16 #include <vector>
17 #include <string>
18 #include "TMVA/Factory.h"
19 #include "TMVA/Tools.h"
20 #include "TMVA/Reader.h"
21 
23 
24  public:
25 
26  // Define here the number and the meaning of the categories
27  // for this specific MVA
28  const int nCategories = 6;
30  UNDEFINED = -1,
37  };
38 
39  // Define the struct that contains all necessary for MVA variables
40  // Note: all variables have to be floats for TMVA Reader, even if
41  // the training was done with ints.
42  struct AllVariables {
43  // Pure ECAL -> shower shapes
44  float see; // 0
45  float spp; // 1
46  float OneMinusE1x5E5x5; // 2
47  float R9; // 3
48  float etawidth; // 4
49  float phiwidth; // 5
50  float HoE; // 6
51  // Endcap only variables
52  float PreShowerOverRaw; // 7
53  //Pure tracking variables
54  float kfhits; // 8
55  float kfchi2; // 9
56  float gsfchi2; // 10
57  // Energy matching
58  float fbrem; // 11
59 
60  float gsfhits; // 12
62  float convVtxFitProbability; // 14
63 
64  float EoP; // 15
65  float eleEoPout; // 16
66  float IoEmIoP; // 17
67  // Geometrical matchings
68  float deta; // 18
69  float dphi; // 19
70  float detacalo; // 20
71  // Spectator variables
72  float pt; // 21
73  float isBarrel; // 22
74  float isEndcap; // 23
75  float SCeta; // 24
76  //
77  float eClass; // 25
78  float pfRelIso; // 26
79  float expectedInnerHits; // 27
80  float vtxconv; // 28
81  float mcEventWeight; // 29
82  float mcCBmatchingCategory; // 30
83 
84  };
85 
86  // Constructor and destructor
88  ElectronMVAEstimatorRun2Spring16HZZ(); // For Reflex. Not to be used in reality
89  ElectronMVAEstimatorRun2Spring16HZZ(const std::string &mvaTag, const std::string &conversionsTag = "reducedEgamma:reducedConversions", const std::string &beamspotTag = "offlineBeamSpot");
90  void init(const std::vector <std::string> weightFileNames);
92 
93 
94 
95  // Calculation of the MVA value (VID accessor)
96  float mvaValue( const edm::Ptr<reco::Candidate>& particle, const edm::Event&) const override;
97  // Calculation of the MVA value (fwlite-compatible accessor)
98  float mvaValue( const reco::GsfElectron * particle, const edm::EventBase &) const ;
99  // Calculation of the MVA value (bare version)
100  float mvaValue( const int iCategory, const std::vector<float> & vars) const ;
101 
102  // Utility functions
103  std::unique_ptr<const GBRForest> createSingleReader(const int iCategory,
104  const edm::FileInPath &weightFile);
105 
106  virtual int getNCategories() const override { return nCategories; }
107  bool isEndcapCategory( int category ) const;
108  virtual const std::string& getName() const override final { return name_; }
109  virtual const std::string& getTag() const override final { return tag_; }
110 
111  // Functions that should work on both pat and reco electrons
112  // (use the fact that pat::Electron inherits from reco::GsfElectron)
113  std::vector<float> fillMVAVariables(const edm::Ptr<reco::Candidate>& particle, const edm::Event&) const override;
114  std::vector<float> fillMVAVariables( const reco::GsfElectron * particle, const edm::Handle<reco::ConversionCollection> conversions, const reco::BeamSpot *beamSpot) const ;
115  int findCategory( const edm::Ptr<reco::Candidate>& particle) const override;
116  int findCategory( const reco::GsfElectron * particle) const ;
117  // The function below ensures that the variables passed to MVA are
118  // within reasonable bounds
119  void constrainMVAVariables(AllVariables&) const;
120 
121  // Call this function once after the constructor to declare
122  // the needed event content pieces to the framework
123  void setConsumes(edm::ConsumesCollector&&) const override final;
124  // Call this function once per event to retrieve all needed
125  // event content pices
126 
127  private:
128 
129  // MVA name. This is a unique name for this MVA implementation.
130  // It will be used as part of ValueMap names.
131  // For simplicity, keep it set to the class name.
132  const std::string name_ = "ElectronMVAEstimatorRun2Spring16HZZ";
133  // MVA tag. This is an additional string variable to distinguish
134  // instances of the estimator of this class configured with different
135  // weight files.
137 
138  // Data members
139  std::vector< std::unique_ptr<const GBRForest> > gbrForest_s;
140 
141  // All variables needed by this MVA
144 
145  //
146  // Declare all tokens that will be needed to retrieve misc
147  // data from the event content required by this MVA
148  //
150  // Conversions in AOD and miniAOD have different names
153 
154 
155 };
156 
157 #endif
std::vector< std::unique_ptr< const GBRForest > > gbrForest_s
virtual const std::string & getTag() const override final
std::unique_ptr< const GBRForest > createSingleReader(const int iCategory, const edm::FileInPath &weightFile)
virtual const std::string & getName() const override final
void setConsumes(edm::ConsumesCollector &&) const override final
std::vector< float > fillMVAVariables(const edm::Ptr< reco::Candidate > &particle, const edm::Event &) const override
float mvaValue(const edm::Ptr< reco::Candidate > &particle, const edm::Event &) const override
int findCategory(const edm::Ptr< reco::Candidate > &particle) const override
void init(const std::vector< std::string > weightFileNames)