CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EGExtraInfoModifierFromDB.cc
Go to the documentation of this file.
6 
9 
14 
17 
18 #include <vdt/vdtMath.h>
19 
20 namespace {
21  const edm::InputTag empty_tag;
22 }
23 
24 #include <unordered_map>
25 
27 public:
30  typedef std::pair<edm::InputTag, ValMapFloatToken> ValMapFloatTagTokenPair;
31  typedef std::pair<edm::InputTag, ValMapIntToken> ValMapIntTagTokenPair;
32 
33  struct electron_config {
36  std::unordered_map<std::string, ValMapFloatTagTokenPair> tag_float_token_map;
37  std::unordered_map<std::string, ValMapIntTagTokenPair> tag_int_token_map;
38 
39  std::vector<std::string> condnames_mean_50ns;
40  std::vector<std::string> condnames_sigma_50ns;
41  std::vector<std::string> condnames_mean_25ns;
42  std::vector<std::string> condnames_sigma_25ns;
45  };
46 
47  struct photon_config {
50  std::unordered_map<std::string, ValMapFloatTagTokenPair> tag_float_token_map;
51  std::unordered_map<std::string, ValMapIntTagTokenPair> tag_int_token_map;
52 
53  std::vector<std::string> condnames_mean_50ns;
54  std::vector<std::string> condnames_sigma_50ns;
55  std::vector<std::string> condnames_mean_25ns;
56  std::vector<std::string> condnames_sigma_25ns;
57  };
58 
61 
62  void setEvent(const edm::Event&) override final;
63  void setEventContent(const edm::EventSetup&) override final;
64  void setConsumes(edm::ConsumesCollector&) override final;
65 
66  void modifyObject(pat::Electron&) const override final;
67  void modifyObject(pat::Photon&) const override final;
68 
69 private:
72  std::unordered_map<unsigned,edm::Ptr<reco::GsfElectron> > eles_by_oop; // indexed by original object ptr
73  std::unordered_map<unsigned,edm::Handle<edm::ValueMap<float> > > ele_vmaps;
74  std::unordered_map<unsigned,edm::Handle<edm::ValueMap<int> > > ele_int_vmaps;
75  std::unordered_map<unsigned,edm::Ptr<reco::Photon> > phos_by_oop;
76  std::unordered_map<unsigned,edm::Handle<edm::ValueMap<float> > > pho_vmaps;
77  std::unordered_map<unsigned,edm::Handle<edm::ValueMap<int> > > pho_int_vmaps;
78 
82  edm::EDGetTokenT<unsigned int> bunchSpacingToken_;
83  float rhoValue_;
85  edm::EDGetTokenT<double> rhoToken_;
86  int nVtx_;
88  edm::EDGetTokenT<reco::VertexCollection> vtxToken_;
90 
96 };
97 
100  "EGExtraInfoModifierFromDB");
101 
102 EGExtraInfoModifierFromDB::EGExtraInfoModifierFromDB(const edm::ParameterSet& conf) :
104 
105  bunchspacing_ = 450;
106  autoDetectBunchSpacing_ = conf.getParameter<bool>("autoDetectBunchSpacing");
107 
108  rhoTag_ = conf.getParameter<edm::InputTag>("rhoCollection");
109  vtxTag_ = conf.getParameter<edm::InputTag>("vertexCollection");
110 
111  if (autoDetectBunchSpacing_) {
112  bunchspacingTag_ = conf.getParameter<edm::InputTag>("bunchSpacingTag");
113  } else {
114  bunchspacing_ = conf.getParameter<int>("manualBunchSpacing");
115  }
116 
117  constexpr char electronSrc[] = "electronSrc";
118  constexpr char photonSrc[] = "photonSrc";
119 
120  if(conf.exists("electron_config")) {
121  const edm::ParameterSet& electrons = conf.getParameter<edm::ParameterSet>("electron_config");
122  if( electrons.exists(electronSrc) )
123  e_conf.electron_src = electrons.getParameter<edm::InputTag>(electronSrc);
124 
125  std::vector<std::string> intValueMaps;
126  if ( electrons.existsAs<std::vector<std::string> >("intValueMaps"))
127  intValueMaps = electrons.getParameter<std::vector<std::string> >("intValueMaps");
128 
129  const std::vector<std::string> parameters = electrons.getParameterNames();
130  for( const std::string& name : parameters ) {
131  if( std::string(electronSrc) == name )
132  continue;
133  if( electrons.existsAs<edm::InputTag>(name)) {
134  for (auto vmp : intValueMaps) {
135  if (name == vmp) {
137  break;
138  }
139  }
141  }
142  }
143 
144  e_conf.condnames_mean_50ns = electrons.getParameter<std::vector<std::string> >("regressionKey_50ns");
145  e_conf.condnames_sigma_50ns = electrons.getParameter<std::vector<std::string> >("uncertaintyKey_50ns");
146  e_conf.condnames_mean_25ns = electrons.getParameter<std::vector<std::string> >("regressionKey_25ns");
147  e_conf.condnames_sigma_25ns = electrons.getParameter<std::vector<std::string> >("uncertaintyKey_25ns");
148  e_conf.condnames_weight_50ns = electrons.getParameter<std::string>("combinationKey_50ns");
149  e_conf.condnames_weight_25ns = electrons.getParameter<std::string>("combinationKey_25ns");
150  }
151 
152  if( conf.exists("photon_config") ) {
153  const edm::ParameterSet& photons = conf.getParameter<edm::ParameterSet>("photon_config");
154 
155  if( photons.exists(photonSrc) )
156  ph_conf.photon_src = photons.getParameter<edm::InputTag>(photonSrc);
157 
158  std::vector<std::string> intValueMaps;
159  if ( photons.existsAs<std::vector<std::string> >("intValueMaps"))
160  intValueMaps = photons.getParameter<std::vector<std::string> >("intValueMaps");
161 
162  const std::vector<std::string> parameters = photons.getParameterNames();
163  for( const std::string& name : parameters ) {
164  if( std::string(photonSrc) == name )
165  continue;
166  if( photons.existsAs<edm::InputTag>(name)) {
167  for (auto vmp : intValueMaps) {
168  if (name == vmp) {
170  break;
171  }
172  }
174  }
175  }
176 
177  ph_conf.condnames_mean_50ns = photons.getParameter<std::vector<std::string>>("regressionKey_50ns");
178  ph_conf.condnames_sigma_50ns = photons.getParameter<std::vector<std::string>>("uncertaintyKey_50ns");
179  ph_conf.condnames_mean_25ns = photons.getParameter<std::vector<std::string>>("regressionKey_25ns");
180  ph_conf.condnames_sigma_25ns = photons.getParameter<std::vector<std::string>>("uncertaintyKey_25ns");
181  }
182 }
183 
184 namespace {
185 template<typename T>
186 inline void get_product(const edm::Event& evt,
188  std::unordered_map<unsigned, edm::Handle<edm::ValueMap<T> > >& map) {
189  evt.getByToken(tok,map[tok.index()]);
190 }
191 }
192 
194  eles_by_oop.clear();
195  phos_by_oop.clear();
196  ele_vmaps.clear();
197  ele_int_vmaps.clear();
198  pho_vmaps.clear();
199  pho_int_vmaps.clear();
200 
204 
205  for( unsigned i = 0; i < eles->size(); ++i ) {
206  edm::Ptr<pat::Electron> ptr = eles->ptrAt(i);
207  eles_by_oop[ptr->originalObjectRef().key()] = ptr;
208  }
209  }
210 
211  for (std::unordered_map<std::string, ValMapFloatTagTokenPair>::iterator imap = e_conf.tag_float_token_map.begin();
212  imap != e_conf.tag_float_token_map.end();
213  imap++) {
214  get_product(evt, imap->second.second, ele_vmaps);
215  }
216 
217  for (std::unordered_map<std::string, ValMapIntTagTokenPair>::iterator imap = e_conf.tag_int_token_map.begin();
218  imap != e_conf.tag_int_token_map.end();
219  imap++) {
220  get_product(evt, imap->second.second, ele_int_vmaps);
221  }
222 
226 
227  for( unsigned i = 0; i < phos->size(); ++i ) {
228  edm::Ptr<pat::Photon> ptr = phos->ptrAt(i);
229  phos_by_oop[ptr->originalObjectRef().key()] = ptr;
230  }
231  }
232 
233 
234  for (std::unordered_map<std::string, ValMapFloatTagTokenPair>::iterator imap = ph_conf.tag_float_token_map.begin();
235  imap != ph_conf.tag_float_token_map.end();
236  imap++) {
237  get_product(evt, imap->second.second, pho_vmaps);
238  }
239 
240  for (std::unordered_map<std::string, ValMapIntTagTokenPair>::iterator imap = ph_conf.tag_int_token_map.begin();
241  imap != ph_conf.tag_int_token_map.end();
242  imap++) {
243  get_product(evt, imap->second.second, pho_int_vmaps);
244  }
245 
247  edm::Handle<unsigned int> bunchSpacingH;
248  evt.getByToken(bunchSpacingToken_,bunchSpacingH);
249  bunchspacing_ = *bunchSpacingH;
250  }
251 
252  edm::Handle<double> rhoH;
253  evt.getByToken(rhoToken_, rhoH);
254  rhoValue_ = *rhoH;
255 
256  evt.getByToken(vtxToken_, vtxH_);
257  nVtx_ = vtxH_->size();
258 }
259 
261 
262  edm::ESHandle<GBRForestD> forestDEH;
263  edm::ESHandle<GBRForest> forestEH;
264 
265  const std::vector<std::string> ph_condnames_mean = (bunchspacing_ == 25) ? ph_conf.condnames_mean_25ns : ph_conf.condnames_mean_50ns;
266  const std::vector<std::string> ph_condnames_sigma = (bunchspacing_ == 25) ? ph_conf.condnames_sigma_25ns : ph_conf.condnames_sigma_50ns;
267 
268  unsigned int ncor = ph_condnames_mean.size();
269  for (unsigned int icor=0; icor<ncor; ++icor) {
270  evs.get<GBRDWrapperRcd>().get(ph_condnames_mean[icor], forestDEH);
271  ph_forestH_mean_.push_back(forestDEH.product());
272  evs.get<GBRDWrapperRcd>().get(ph_condnames_sigma[icor], forestDEH);
273  ph_forestH_sigma_.push_back(forestDEH.product());
274  }
275 
276  const std::vector<std::string> e_condnames_mean = (bunchspacing_ == 25) ? e_conf.condnames_mean_25ns : e_conf.condnames_mean_50ns;
277  const std::vector<std::string> e_condnames_sigma = (bunchspacing_ == 25) ? e_conf.condnames_sigma_25ns : e_conf.condnames_sigma_50ns;
278  const std::string ep_condnames_weight = (bunchspacing_ == 25) ? e_conf.condnames_weight_25ns : e_conf.condnames_weight_50ns;
279 
280  unsigned int encor = e_condnames_mean.size();
281  evs.get<GBRWrapperRcd>().get(ep_condnames_weight, forestEH);
282  ep_forestH_weight_ = forestEH.product();
283 
284  for (unsigned int icor=0; icor<encor; ++icor) {
285  evs.get<GBRDWrapperRcd>().get(e_condnames_mean[icor], forestDEH);
286  e_forestH_mean_.push_back(forestDEH.product());
287  evs.get<GBRDWrapperRcd>().get(e_condnames_sigma[icor], forestDEH);
288  e_forestH_sigma_.push_back(forestDEH.product());
289  }
290 }
291 
292 namespace {
293 template<typename T, typename U, typename V>
294 inline void make_consumes(T& tag,U& tok,V& sume) {
295  if(!(empty_tag == tag))
296  tok = sume.template consumes<edm::ValueMap<float> >(tag);
297 }
298 
299 template<typename T, typename U, typename V>
300 inline void make_int_consumes(T& tag,U& tok,V& sume) {
301  if(!(empty_tag == tag))
302  tok = sume.template consumes<edm::ValueMap<int> >(tag);
303 }
304 }
305 
307 
308  rhoToken_ = sumes.consumes<double>(rhoTag_);
310 
312  bunchSpacingToken_ = sumes.consumes<unsigned int>(bunchspacingTag_);
313 
314  //setup electrons
315  if(!(empty_tag == e_conf.electron_src))
317 
318  for ( std::unordered_map<std::string, ValMapFloatTagTokenPair>::iterator imap = e_conf.tag_float_token_map.begin();
319  imap != e_conf.tag_float_token_map.end();
320  imap++) {
321  make_consumes(imap->second.first, imap->second.second, sumes);
322  }
323 
324  for ( std::unordered_map<std::string, ValMapIntTagTokenPair>::iterator imap = e_conf.tag_int_token_map.begin();
325  imap != e_conf.tag_int_token_map.end();
326  imap++) {
327  make_int_consumes(imap->second.first, imap->second.second, sumes);
328  }
329 
330  // setup photons
331  if(!(empty_tag == ph_conf.photon_src))
333 
334  for ( std::unordered_map<std::string, ValMapFloatTagTokenPair>::iterator imap = ph_conf.tag_float_token_map.begin();
335  imap != ph_conf.tag_float_token_map.end();
336  imap++) {
337  make_consumes(imap->second.first, imap->second.second, sumes);
338  }
339 
340  for ( std::unordered_map<std::string, ValMapIntTagTokenPair>::iterator imap = ph_conf.tag_int_token_map.begin();
341  imap != ph_conf.tag_int_token_map.end();
342  imap++) {
343  make_int_consumes(imap->second.first, imap->second.second, sumes);
344  }
345 }
346 
347 namespace {
348 template<typename T, typename U, typename V, typename Z>
349 inline void assignValue(const T& ptr, const U& tok, const V& map, Z& value) {
350  if( !tok.isUninitialized() ) value = map.find(tok.index())->second->get(ptr.id(),ptr.key());
351 }
352 }
353 
355  // we encounter two cases here, either we are running AOD -> MINIAOD
356  // and the value maps are to the reducedEG object, can use original object ptr
357  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
358 
361  auto key = eles_by_oop.find(ele.originalObjectRef().key());
362  if( key != eles_by_oop.end() ) {
363  ptr = key->second;
364  } else {
365  throw cms::Exception("BadElectronKey")
366  << "Original object pointer with key = " << ele.originalObjectRef().key()
367  << " not found in cache!";
368  }
369  }
370  std::array<float, 33> eval;
371 
373  edm::Ptr<reco::CaloCluster> theseed = sc->seed();
374 
375  // SET INPUTS
376  eval[0] = nVtx_;
377  eval[1] = sc->rawEnergy();
378  eval[2] = sc->eta();
379  eval[3] = sc->phi();
380  eval[4] = sc->etaWidth();
381  eval[5] = sc->phiWidth();
382  eval[6] = ele.r9();
383  eval[7] = theseed->energy()/sc->rawEnergy();
384 
385  float sieip=0, cryPhi=0, cryEta=0;
386  int iPhi=0, iEta=0;
387  float eMax=0, e2nd=0, eTop=0, eBottom=0, eLeft=0, eRight=0;
388  float clusterMaxDR=0, clusterMaxDRDPhi=0, clusterMaxDRDEta=0, clusterMaxDRRawEnergy=0;
389  float clusterRawEnergy0=0, clusterRawEnergy1=0, clusterRawEnergy2=0;
390  float clusterDPhiToSeed0=0, clusterDPhiToSeed1=0, clusterDPhiToSeed2=0;
391  float clusterDEtaToSeed0=0, clusterDEtaToSeed1=0, clusterDEtaToSeed2=0;
392 
393  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("sigmaIetaIphi"))->second.second, ele_vmaps, sieip);
394  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("eMax"))->second.second, ele_vmaps, eMax);
395  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("e2nd"))->second.second, ele_vmaps, e2nd);
396  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("eTop"))->second.second, ele_vmaps, eTop);
397  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("eBottom"))->second.second, ele_vmaps, eBottom);
398  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("eLeft"))->second.second, ele_vmaps, eLeft);
399  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("eRight"))->second.second, ele_vmaps, eRight);
400  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterMaxDR"))->second.second, ele_vmaps, clusterMaxDR);
401  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterMaxDRDPhi"))->second.second, ele_vmaps, clusterMaxDRDPhi);
402  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterMaxDRDEta"))->second.second, ele_vmaps, clusterMaxDRDEta);
403  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterMaxDRRawEnergy"))->second.second, ele_vmaps, clusterMaxDRRawEnergy);
404  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterRawEnergy0"))->second.second, ele_vmaps, clusterRawEnergy0);
405  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterRawEnergy1"))->second.second, ele_vmaps, clusterRawEnergy1);
406  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterRawEnergy2"))->second.second, ele_vmaps, clusterRawEnergy2);
407  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterDPhiToSeed0"))->second.second, ele_vmaps, clusterDPhiToSeed0);
408  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterDPhiToSeed1"))->second.second, ele_vmaps, clusterDPhiToSeed1);
409  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterDPhiToSeed2"))->second.second, ele_vmaps, clusterDPhiToSeed2);
410  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterDEtaToSeed0"))->second.second, ele_vmaps, clusterDEtaToSeed0);
411  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterDEtaToSeed1"))->second.second, ele_vmaps, clusterDEtaToSeed1);
412  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterDEtaToSeed2"))->second.second, ele_vmaps, clusterDEtaToSeed2);
413  assignValue(ptr, e_conf.tag_int_token_map.find(std::string("iPhi"))->second.second, ele_int_vmaps, iPhi);
414  assignValue(ptr, e_conf.tag_int_token_map.find(std::string("iEta"))->second.second, ele_int_vmaps, iEta);
415  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("cryPhi"))->second.second, ele_vmaps, cryPhi);
416  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("cryEta"))->second.second, ele_vmaps, cryEta);
417 
418  eval[8] = eMax/sc->rawEnergy();
419  eval[9] = e2nd/sc->rawEnergy();
420  eval[10] = (eLeft+eRight!=0. ? (eLeft-eRight)/(eLeft+eRight) : 0.);
421  eval[11] = (eTop+eBottom!=0. ? (eTop-eBottom)/(eTop+eBottom) : 0.);
422  eval[12] = ele.sigmaIetaIeta();
423  eval[13] = sieip;
424  eval[14] = ele.sigmaIphiIphi();
425  const int N_ECAL = sc->clustersEnd() - sc->clustersBegin();
426  eval[15] = std::max(0,N_ECAL - 1);
427  eval[16] = clusterMaxDR;
428  eval[17] = clusterMaxDRDPhi;
429  eval[18] = clusterMaxDRDEta;
430  eval[19] = clusterMaxDRRawEnergy/sc->rawEnergy();
431  eval[20] = clusterRawEnergy0/sc->rawEnergy();
432  eval[21] = clusterRawEnergy1/sc->rawEnergy();
433  eval[22] = clusterRawEnergy2/sc->rawEnergy();
434  eval[23] = clusterDPhiToSeed0;
435  eval[24] = clusterDPhiToSeed1;
436  eval[25] = clusterDPhiToSeed2;
437  eval[26] = clusterDEtaToSeed0;
438  eval[27] = clusterDEtaToSeed1;
439  eval[28] = clusterDEtaToSeed2;
440 
441  bool iseb = ele.isEB();
442 
443  if (iseb) {
444  eval[29] = cryEta;
445  eval[30] = cryPhi;
446  eval[31] = iEta;
447  eval[32] = iPhi;
448  } else {
449  eval[29] = sc->preshowerEnergy()/sc->rawEnergy();
450  }
451 
452  //magic numbers for MINUIT-like transformation of BDT output onto limited range
453  //(These should be stored inside the conditions object in the future as well)
454  constexpr double meanlimlow = 0.2;
455  constexpr double meanlimhigh = 2.0;
456  constexpr double meanoffset = meanlimlow + 0.5*(meanlimhigh-meanlimlow);
457  constexpr double meanscale = 0.5*(meanlimhigh-meanlimlow);
458 
459  constexpr double sigmalimlow = 0.0002;
460  constexpr double sigmalimhigh = 0.5;
461  constexpr double sigmaoffset = sigmalimlow + 0.5*(sigmalimhigh-sigmalimlow);
462  constexpr double sigmascale = 0.5*(sigmalimhigh-sigmalimlow);
463 
464  int coridx = 0;
465  if (!iseb)
466  coridx = 1;
467 
468  //these are the actual BDT responses
469  double rawmean = e_forestH_mean_[coridx]->GetResponse(eval.data());
470  double rawsigma = e_forestH_sigma_[coridx]->GetResponse(eval.data());
471 
472  //apply transformation to limited output range (matching the training)
473  double mean = meanoffset + meanscale*vdt::fast_sin(rawmean);
474  double sigma = sigmaoffset + sigmascale*vdt::fast_sin(rawsigma);
475 
476  //regression target is ln(Etrue/Eraw)
477  //so corrected energy is ecor=exp(mean)*e, uncertainty is exp(mean)*eraw*sigma=ecor*sigma
478  double ecor = mean*(eval[1]);
479  if (!iseb)
480  ecor = mean*(eval[1]+sc->preshowerEnergy());
481  const double sigmacor = sigma*ecor;
482 
483  ele.setCorrectedEcalEnergy(ecor);
484  ele.setCorrectedEcalEnergyError(sigmacor);
485 
486  // E-p combination
487  //std::array<float, 11> eval_ep;
488  float eval_ep[11];
489 
490  const float ep = ele.trackMomentumAtVtx().R();
491  const float tot_energy = sc->rawEnergy()+sc->preshowerEnergy();
492  const float momentumError = ele.trackMomentumError();
493  const float trkMomentumRelError = ele.trackMomentumError()/ep;
494  const float eOverP = tot_energy*mean/ep;
495  eval_ep[0] = tot_energy*mean;
496  eval_ep[1] = sigma/mean;
497  eval_ep[2] = ep;
498  eval_ep[3] = trkMomentumRelError;
499  eval_ep[4] = sigma/mean/trkMomentumRelError;
500  eval_ep[5] = tot_energy*mean/ep;
501  eval_ep[6] = tot_energy*mean/ep*sqrt(sigma/mean*sigma/mean+trkMomentumRelError*trkMomentumRelError);
502  eval_ep[7] = ele.ecalDriven();
503  eval_ep[8] = ele.trackerDrivenSeed();
504  eval_ep[9] = int(ele.classification());//eleClass;
505  eval_ep[10] = iseb;
506 
507  // CODE FOR FUTURE SEMI_PARAMETRIC
508  //double rawweight = ep_forestH_mean_[coridx]->GetResponse(eval_ep.data());
510  //double weight = meanoffset + meanscale*vdt::fast_sin(rawweight);
512 
513  // CODE FOR STANDARD BDT
514  double weight = 0.;
515  if ( eOverP > 0.025 &&
516  std::abs(ep-ecor) < 15.*std::sqrt( momentumError*momentumError + sigmacor*sigmacor ) ) {
517  // protection against crazy track measurement
518  weight = ep_forestH_weight_->GetResponse(eval_ep);
519  if(weight>1.)
520  weight = 1.;
521  else if(weight<0.)
522  weight = 0.;
523  }
524 
525  double combinedMomentum = weight*ele.trackMomentumAtVtx().R() + (1.-weight)*ecor;
526  double combinedMomentumError = sqrt(weight*weight*ele.trackMomentumError()*ele.trackMomentumError() + (1.-weight)*(1.-weight)*sigmacor*sigmacor);
527 
528  math::XYZTLorentzVector oldMomentum = ele.p4();
529  math::XYZTLorentzVector newMomentum = math::XYZTLorentzVector(oldMomentum.x()*combinedMomentum/oldMomentum.t(),
530  oldMomentum.y()*combinedMomentum/oldMomentum.t(),
531  oldMomentum.z()*combinedMomentum/oldMomentum.t(),
532  combinedMomentum);
533 
534  //ele.correctEcalEnergy(combinedMomentum, combinedMomentumError);
535  ele.correctMomentum(newMomentum, ele.trackMomentumError(), combinedMomentumError);
536 }
537 
539  // we encounter two cases here, either we are running AOD -> MINIAOD
540  // and the value maps are to the reducedEG object, can use original object ptr
541  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
543 
545  auto key = phos_by_oop.find(pho.originalObjectRef().key());
546  if( key != phos_by_oop.end() ) {
547  ptr = key->second;
548  } else {
549  throw cms::Exception("BadPhotonKey")
550  << "Original object pointer with key = " << pho.originalObjectRef().key() << " not found in cache!";
551  }
552  }
553 
554  std::array<float, 31> eval;
556  edm::Ptr<reco::CaloCluster> theseed = sc->seed();
557 
558  // SET INPUTS
559  eval[0] = sc->rawEnergy();
560  //eval[1] = sc->position().Eta();
561  //eval[2] = sc->position().Phi();
562  eval[1] = pho.r9();
563  eval[2] = sc->etaWidth();
564  eval[3] = sc->phiWidth();
565  const int N_ECAL = sc->clustersEnd() - sc->clustersBegin();
566  eval[4] = std::max(0,N_ECAL - 1);
567  eval[5] = pho.hadronicOverEm();
568  eval[6] = rhoValue_;
569  eval[7] = nVtx_;
570  eval[8] = theseed->eta()-sc->position().Eta();
571  eval[9] = reco::deltaPhi(theseed->phi(),sc->position().Phi());
572  eval[10] = pho.seedEnergy()/sc->rawEnergy();
573  eval[11] = pho.e3x3()/pho.e5x5();
574  const float sieie = pho.sigmaIetaIeta();
575  eval[12] = sieie;
576 
577  float sipip=0, sieip=0, e2x5Max=0, e2x5Left=0, e2x5Right=0, e2x5Top=0, e2x5Bottom=0;
578  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("sigmaIetaIphi"))->second.second, pho_vmaps, sieip);
579  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("sigmaIphiIphi"))->second.second, pho_vmaps, sipip);
580  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("e2x5Max"))->second.second, pho_vmaps, e2x5Max);
581  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("e2x5Left"))->second.second, pho_vmaps, e2x5Left);
582  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("e2x5Right"))->second.second, pho_vmaps, e2x5Right);
583  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("e2x5Top"))->second.second, pho_vmaps, e2x5Top);
584  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("e2x5Bottom"))->second.second, pho_vmaps, e2x5Bottom);
585 
586  eval[13] = sipip;
587  eval[14] = sieip/(sieie*sipip);
588  eval[15] = pho.maxEnergyXtal()/pho.e5x5();
589  eval[16] = pho.e2nd()/pho.e5x5();
590  eval[17] = pho.eTop()/pho.e5x5();
591  eval[18] = pho.eBottom()/pho.e5x5();
592  eval[19] = pho.eLeft()/pho.e5x5();
593  eval[20] = pho.eRight()/pho.e5x5();
594  eval[21] = e2x5Max/pho.e5x5();
595  eval[22] = e2x5Left/pho.e5x5();
596  eval[23] = e2x5Right/pho.e5x5();
597  eval[24] = e2x5Top/pho.e5x5();
598  eval[25] = e2x5Bottom/pho.e5x5();
599 
600  bool iseb = pho.isEB();
601 
602  if (iseb) {
603  EBDetId ebseedid(theseed->seed());
604  eval[26] = pho.e5x5()/pho.seedEnergy();
605  eval[27] = ebseedid.ieta();
606  eval[28] = ebseedid.iphi();
607  } else {
608  EEDetId eeseedid(theseed->seed());
609  eval[26] = sc->preshowerEnergy()/sc->rawEnergy();
610  eval[27] = sc->preshowerEnergyPlane1()/sc->rawEnergy();
611  eval[28] = sc->preshowerEnergyPlane2()/sc->rawEnergy();
612  eval[29] = eeseedid.ix();
613  eval[30] = eeseedid.iy();
614  }
615 
616  //magic numbers for MINUIT-like transformation of BDT output onto limited range
617  //(These should be stored inside the conditions object in the future as well)
618  const double meanlimlow = 0.2;
619  const double meanlimhigh = 2.0;
620  const double meanoffset = meanlimlow + 0.5*(meanlimhigh-meanlimlow);
621  const double meanscale = 0.5*(meanlimhigh-meanlimlow);
622 
623  const double sigmalimlow = 0.0002;
624  const double sigmalimhigh = 0.5;
625  const double sigmaoffset = sigmalimlow + 0.5*(sigmalimhigh-sigmalimlow);
626  const double sigmascale = 0.5*(sigmalimhigh-sigmalimlow);
627 
628  int coridx = 0;
629  if (!iseb)
630  coridx = 1;
631 
632  //these are the actual BDT responses
633  double rawmean = ph_forestH_mean_[coridx]->GetResponse(eval.data());
634  double rawsigma = ph_forestH_sigma_[coridx]->GetResponse(eval.data());
635  //apply transformation to limited output range (matching the training)
636  double mean = meanoffset + meanscale*vdt::fast_sin(rawmean);
637  double sigma = sigmaoffset + sigmascale*vdt::fast_sin(rawsigma);
638 
639  //regression target is ln(Etrue/Eraw)
640  //so corrected energy is ecor=exp(mean)*e, uncertainty is exp(mean)*eraw*sigma=ecor*sigma
641  double ecor = mean*eval[0];
642  if (!iseb)
643  ecor = mean*(eval[0]+sc->preshowerEnergy());
644 
645  double sigmacor = sigma*ecor;
646  pho.setCorrectedEnergy(reco::Photon::P4type::regression2, ecor, sigmacor, true);
647 }
const double Z[kNumberCalorimeter]
float sigmaIphiIphi() const
Definition: GsfElectron.h:403
double GetResponse(const float *vector) const
Definition: GBRForest.h:59
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
Analysis-level Photon class.
Definition: Photon.h:47
dictionary parameters
Definition: Parameters.py:2
reco::SuperClusterRef superCluster() const
override the superCluster method from CaloJet, to access the internal storage of the supercluster ...
float trackMomentumError() const
Definition: GsfElectron.h:759
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:185
std::unordered_map< std::string, ValMapIntTagTokenPair > tag_int_token_map
const LorentzVector & p4(P4Kind kind) const
Definition: GsfElectron.cc:223
key_type key() const
Definition: Ptr.h:169
void setCorrectedEnergy(P4type type, float E, float dE, bool toCand=true)
float e5x5() const
Definition: Photon.h:192
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
std::unordered_map< std::string, ValMapIntTagTokenPair > tag_int_token_map
std::unordered_map< unsigned, edm::Ptr< reco::Photon > > phos_by_oop
void correctMomentum(const LorentzVector &p4, float trackMomentumError, float p4Error)
Definition: GsfElectron.h:781
math::XYZVectorF trackMomentumAtVtx() const
Definition: GsfElectron.h:289
std::unordered_map< std::string, ValMapFloatTagTokenPair > tag_float_token_map
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::vector< const GBRForestD * > ph_forestH_sigma_
float eBottom() const
Definition: Photon.h:247
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
std::unordered_map< std::string, ValMapFloatTagTokenPair > tag_float_token_map
std::unordered_map< unsigned, edm::Handle< edm::ValueMap< int > > > pho_int_vmaps
float eLeft() const
Definition: Photon.h:249
#define constexpr
float e2nd() const
Definition: Photon.h:241
bool isEB() const
Definition: GsfElectron.h:350
U second(std::pair< T, U > const &p)
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
std::unordered_map< unsigned, edm::Handle< edm::ValueMap< float > > > pho_vmaps
reco::SuperClusterRef superCluster() const
override the reco::GsfElectron::superCluster method, to access the internal storage of the superclust...
void setCorrectedEcalEnergyError(float newEnergyError)
Definition: GsfElectron.cc:177
float sigmaIetaIeta() const
Definition: GsfElectron.h:402
const std::string & name() const
float seedEnergy() const
input variables for regression energy corrections
Definition: Photon.h:236
void setConsumes(edm::ConsumesCollector &) overridefinal
std::pair< edm::InputTag, ValMapFloatToken > ValMapFloatTagTokenPair
T sqrt(T t)
Definition: SSEVec.h:48
std::unordered_map< unsigned, edm::Ptr< reco::GsfElectron > > eles_by_oop
float eTop() const
Definition: Photon.h:245
float sigmaIetaIeta() const
Definition: Photon.h:195
float e3x3() const
Definition: Photon.h:243
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
float hadronicOverEm() const
the total hadronic over electromagnetic fraction
Definition: Photon.h:174
std::vector< std::string > getParameterNames() const
std::unordered_map< unsigned, edm::Handle< edm::ValueMap< float > > > ele_vmaps
edm::EDGetTokenT< edm::ValueMap< int > > ValMapIntToken
const edm::Ptr< reco::Candidate > & originalObjectRef() const
reference to original object. Returns a null reference if not available
Definition: PATObject.h:484
std::vector< const GBRForestD * > ph_forestH_mean_
tuple conf
Definition: dbtoconf.py:185
void setEvent(const edm::Event &) overridefinal
double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:12
edm::EDGetTokenT< edm::View< pat::Photon > > tok_photon_src
std::pair< edm::InputTag, ValMapIntToken > ValMapIntTagTokenPair
Classification classification() const
Definition: GsfElectron.h:680
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:86
edm::EDGetTokenT< unsigned int > bunchSpacingToken_
edm::EDGetTokenT< edm::ValueMap< float > > ValMapFloatToken
Analysis-level electron class.
Definition: Electron.h:52
edm::Handle< reco::VertexCollection > vtxH_
string const
Definition: compareJSON.py:14
void modifyObject(pat::Electron &) const overridefinal
std::vector< const GBRForestD * > e_forestH_mean_
void setEventContent(const edm::EventSetup &) overridefinal
bool isEB() const
Definition: Photon.h:120
#define private
Definition: FWFileEntry.h:17
void setCorrectedEcalEnergy(float newEnergy)
Definition: GsfElectron.cc:180
float r9() const
Definition: GsfElectron.h:407
edm::EDGetTokenT< reco::VertexCollection > vtxToken_
float r9() const
Definition: Photon.h:198
bool trackerDrivenSeed() const
Definition: GsfElectron.h:187
bool isUninitialized() const
Definition: EDGetToken.h:71
std::unordered_map< unsigned, edm::Handle< edm::ValueMap< int > > > ele_int_vmaps
#define DEFINE_EDM_PLUGIN(factory, type, name)
int weight
Definition: histoStyle.py:50
long double T
bool ecalDriven() const
Definition: GsfElectron.cc:172
float eRight() const
Definition: Photon.h:251
std::vector< const GBRForestD * > e_forestH_sigma_
edm::EDGetTokenT< double > rhoToken_
EGExtraInfoModifierFromDB(const edm::ParameterSet &conf)
edm::EDGetTokenT< edm::View< pat::Electron > > tok_electron_src
float maxEnergyXtal() const
Definition: Photon.h:193