CMS 3D CMS Logo

ElectronMVAVariableHelper.h
Go to the documentation of this file.
1 #ifndef ELECTRONMVAVARIABLEHELPER_H
2 #define ELECTRONMVAVARIABLEHELPER_H
3 
8 
12 
15 
17 
20 
23 
25 
28 
29 #include <TMath.h>
30 
32 
33 template <class ParticleType>
35  public:
36 
37  explicit ElectronMVAVariableHelper(const edm::ParameterSet & iConfig);
38  ~ElectronMVAVariableHelper() override ;
39 
40  void produce(edm::Event & iEvent, const edm::EventSetup & iSetup) override;
41 
42 private:
43  template<typename T>
44  void writeValueMap(edm::Event &iEvent,
46  const std::vector<T> & values,
47  const std::string & label) const ;
48 
49  // for AOD case
54 
55  // for miniAOD case
60 };
61 
62 template<class ParticleType>
64  electronsToken_(consumes<edm::View<ParticleType> >(iConfig.getParameter<edm::InputTag>("src"))),
65  vtxToken_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexCollection"))),
66  conversionsToken_(consumes<reco::ConversionCollection>(iConfig.getParameter<edm::InputTag>("conversions"))),
67  beamSpotToken_(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpot"))),
68  electronsTokenMiniAOD_(consumes<edm::View<ParticleType> >(iConfig.getParameter<edm::InputTag>("srcMiniAOD"))),
69  vtxTokenMiniAOD_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexCollectionMiniAOD"))),
70  conversionsTokenMiniAOD_(consumes<reco::ConversionCollection>(iConfig.getParameter<edm::InputTag>("conversionsMiniAOD"))),
71  beamSpotTokenMiniAOD_(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpotMiniAOD"))) {
72 
73  produces<edm::ValueMap<float>>("convVtxFitProb");
74  produces<edm::ValueMap<float>>("kfhits");
75  produces<edm::ValueMap<float>>("kfchi2");
76 }
77 
78 template<class ParticleType>
80 {}
81 
82 template<class ParticleType>
84 
85  // read input
89  edm::Handle<reco::BeamSpot> beamSpotHandle;
90 
91  bool isAOD = true;
92  // Retrieve the collection of particles from the event.
93  // If we fail to retrieve the collection with the standard AOD
94  // name, we next look for the one with the stndard miniAOD name.
95  iEvent.getByToken(electronsToken_, electrons);
96  if( !electrons.isValid() ){
97  isAOD = false;
98  iEvent.getByToken(electronsTokenMiniAOD_,electrons);
99  if( !electrons.isValid() )
100  throw cms::Exception(" Collection not found: ") << " failed to find a standard AOD or miniAOD particle collection " << std::endl;
101  }
102 
103  if (isAOD) {
104  iEvent.getByToken(vtxToken_, vtxH);
105  iEvent.getByToken(conversionsToken_, conversions);
106  iEvent.getByToken(beamSpotToken_, beamSpotHandle);
107  } else {
108  iEvent.getByToken(vtxTokenMiniAOD_, vtxH);
109  iEvent.getByToken(conversionsTokenMiniAOD_, conversions);
110  iEvent.getByToken(beamSpotTokenMiniAOD_, beamSpotHandle);
111  }
112 
113  // Make sure everything is retrieved successfully
114  if(! (beamSpotHandle.isValid() && conversions.isValid() && vtxH.isValid() ) ) {
115  throw cms::Exception("MVA failure: ")
116  << "Failed to retrieve event content needed for this MVA"
117  << std::endl
118  << "Check python MVA configuration file."
119  << std::endl;
120  }
121 
122  const reco::VertexRef vtx(vtxH, 0);
123  const reco::BeamSpot* beamSpot = &*(beamSpotHandle.product());
124 
125  // prepare vector for output
126  std::vector<float> convVtxFitProbVals;
127  std::vector<float> kfhitsVals;
128  std::vector<float> kfchi2Vals;
129 
130  for (size_t i = 0; i < electrons->size(); ++i){
131  auto iCand = electrons->ptrAt(i);
132 
133  // Conversion vertex fit
134  reco::ConversionRef convRef = ConversionTools::matchedConversion(*iCand, conversions, beamSpot->position());
135 
136  float convVtxFitProb = -1.;
137  if(!convRef.isNull()) {
138  const reco::Vertex &vtx = convRef.get()->conversionVertex();
139  if (vtx.isValid()) {
140  convVtxFitProb = TMath::Prob( vtx.chi2(), vtx.ndof());
141  }
142  }
143 
144  convVtxFitProbVals.push_back(convVtxFitProb);
145 
146  // kf track related variables
147  bool validKf=false;
148  reco::TrackRef trackRef = iCand->closestCtfTrackRef();
149  validKf = trackRef.isAvailable();
150  validKf &= trackRef.isNonnull();
151  float kfchi2 = validKf ? trackRef->normalizedChi2() : 0 ; //ielectron->track()->normalizedChi2() : 0 ;
152  float kfhits = validKf ? trackRef->hitPattern().trackerLayersWithMeasurement() : -1. ;
153 
154  kfchi2Vals.push_back(kfchi2);
155  kfhitsVals.push_back(kfhits);
156 
157  }
158 
159 
160  // convert into ValueMap and store
161  writeValueMap(iEvent, electrons, kfchi2Vals, "kfchi2" );
162  writeValueMap(iEvent, electrons, kfhitsVals, "kfhits" );
163  writeValueMap(iEvent, electrons, convVtxFitProbVals, "convVtxFitProb" );
164 }
165 
166 template<class ParticleType> template<typename T>
169  const std::vector<T> & values,
170  const std::string & label) const
171 {
172  auto valMap = std::make_unique<edm::ValueMap<T>>();
173  typename edm::ValueMap<T>::Filler filler(*valMap);
174  filler.insert(handle, values.begin(), values.end());
175  filler.fill();
176  iEvent.put(std::move(valMap), label);
177 }
178 
179 #endif
const edm::EDGetTokenT< reco::BeamSpot > beamSpotTokenMiniAOD_
bool isAvailable() const
Definition: Ref.h:577
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:253
const edm::EDGetTokenT< edm::View< ParticleType > > electronsTokenMiniAOD_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
bool isValid() const
Tells whether the vertex is valid.
Definition: Vertex.h:68
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
std::vector< Conversion > ConversionCollection
collectin of Conversion objects
Definition: ConversionFwd.h:9
int iEvent
Definition: GenABIO.cc:230
const edm::EDGetTokenT< reco::ConversionCollection > conversionsTokenMiniAOD_
const edm::EDGetTokenT< reco::VertexCollection > vtxTokenMiniAOD_
double chi2() const
chi-squares
Definition: Vertex.h:98
edm::View< reco::Candidate > CandView
ElectronMVAVariableHelper(const edm::ParameterSet &iConfig)
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:245
bool isValid() const
Definition: HandleBase.h:74
bool isNull() const
Checks for null.
Definition: Ref.h:250
double ndof() const
Definition: Vertex.h:105
const edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
T const * product() const
Definition: Handle.h:81
const edm::EDGetTokenT< reco::VertexCollection > vtxToken_
void writeValueMap(edm::Event &iEvent, const edm::Handle< edm::View< ParticleType > > &handle, const std::vector< T > &values, const std::string &label) const
fixed size matrix
HLT enums.
const edm::EDGetTokenT< reco::ConversionCollection > conversionsToken_
const edm::EDGetTokenT< edm::View< ParticleType > > electronsToken_
const Point & position() const
position
Definition: BeamSpot.h:62
static reco::ConversionRef matchedConversion(const reco::GsfElectron &ele, const edm::Handle< reco::ConversionCollection > &convCol, const math::XYZPoint &beamspot, bool allowCkfMatch=true, float lxyMin=2.0, float probMin=1e-6, unsigned int nHitsBeforeVtxMax=0)
def move(src, dest)
Definition: eostools.py:510
ParticleType
Definition of particle types.
Definition: ParticleCode.h:18
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override