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<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 template<typename T>
185 inline void get_product(const edm::Event& evt,
187  std::unordered_map<unsigned, edm::Handle<edm::ValueMap<T> > >& map) {
188  evt.getByToken(tok,map[tok.index()]);
189 }
190 
192  eles_by_oop.clear();
193  phos_by_oop.clear();
194  ele_vmaps.clear();
195  ele_int_vmaps.clear();
196  pho_vmaps.clear();
197  pho_int_vmaps.clear();
198 
202 
203  for( unsigned i = 0; i < eles->size(); ++i ) {
204  edm::Ptr<pat::Electron> ptr = eles->ptrAt(i);
205  eles_by_oop[ptr->originalObjectRef().key()] = ptr;
206  }
207  }
208 
209  for (std::unordered_map<std::string, ValMapFloatTagTokenPair>::iterator imap = e_conf.tag_float_token_map.begin();
210  imap != e_conf.tag_float_token_map.end();
211  imap++) {
212  get_product(evt, imap->second.second, ele_vmaps);
213  }
214 
215  for (std::unordered_map<std::string, ValMapIntTagTokenPair>::iterator imap = e_conf.tag_int_token_map.begin();
216  imap != e_conf.tag_int_token_map.end();
217  imap++) {
218  get_product(evt, imap->second.second, ele_int_vmaps);
219  }
220 
224 
225  for( unsigned i = 0; i < phos->size(); ++i ) {
226  edm::Ptr<pat::Photon> ptr = phos->ptrAt(i);
227  phos_by_oop[ptr->originalObjectRef().key()] = ptr;
228  }
229  }
230 
231 
232  for (std::unordered_map<std::string, ValMapFloatTagTokenPair>::iterator imap = ph_conf.tag_float_token_map.begin();
233  imap != ph_conf.tag_float_token_map.end();
234  imap++) {
235  get_product(evt, imap->second.second, pho_vmaps);
236  }
237 
238  for (std::unordered_map<std::string, ValMapIntTagTokenPair>::iterator imap = ph_conf.tag_int_token_map.begin();
239  imap != ph_conf.tag_int_token_map.end();
240  imap++) {
241  get_product(evt, imap->second.second, pho_int_vmaps);
242  }
243 
245  if (evt.isRealData()) {
246  edm::RunNumber_t run = evt.run();
247  if (run == 178003 ||
248  run == 178004 ||
249  run == 209089 ||
250  run == 209106 ||
251  run == 209109 ||
252  run == 209146 ||
253  run == 209148 ||
254  run == 209151) {
255  bunchspacing_ = 25;
256  }
257  else if (run < 253000) {
258  bunchspacing_ = 50;
259  }
260  else {
261  bunchspacing_ = 25;
262  }
263  } else {
264  edm::Handle<int> bunchSpacingH;
265  evt.getByToken(bunchSpacingToken_,bunchSpacingH);
266  bunchspacing_ = *bunchSpacingH;
267  }
268  }
269 
270  edm::Handle<double> rhoH;
271  evt.getByToken(rhoToken_, rhoH);
272  rhoValue_ = *rhoH;
273 
274  evt.getByToken(vtxToken_, vtxH_);
275  nVtx_ = vtxH_->size();
276 }
277 
279 
280  edm::ESHandle<GBRForestD> forestDEH;
281  edm::ESHandle<GBRForest> forestEH;
282 
283  const std::vector<std::string> ph_condnames_mean = (bunchspacing_ == 25) ? ph_conf.condnames_mean_25ns : ph_conf.condnames_mean_50ns;
284  const std::vector<std::string> ph_condnames_sigma = (bunchspacing_ == 25) ? ph_conf.condnames_sigma_25ns : ph_conf.condnames_sigma_50ns;
285 
286  unsigned int ncor = ph_condnames_mean.size();
287  for (unsigned int icor=0; icor<ncor; ++icor) {
288  evs.get<GBRDWrapperRcd>().get(ph_condnames_mean[icor], forestDEH);
289  ph_forestH_mean_.push_back(forestDEH.product());
290  evs.get<GBRDWrapperRcd>().get(ph_condnames_sigma[icor], forestDEH);
291  ph_forestH_sigma_.push_back(forestDEH.product());
292  }
293 
294  const std::vector<std::string> e_condnames_mean = (bunchspacing_ == 25) ? e_conf.condnames_mean_25ns : e_conf.condnames_mean_50ns;
295  const std::vector<std::string> e_condnames_sigma = (bunchspacing_ == 25) ? e_conf.condnames_sigma_25ns : e_conf.condnames_sigma_50ns;
296  const std::string ep_condnames_weight = (bunchspacing_ == 25) ? e_conf.condnames_weight_25ns : e_conf.condnames_weight_50ns;
297 
298  unsigned int encor = e_condnames_mean.size();
299  evs.get<GBRWrapperRcd>().get(ep_condnames_weight, forestEH);
300  ep_forestH_weight_ = forestEH.product();
301 
302  for (unsigned int icor=0; icor<encor; ++icor) {
303  evs.get<GBRDWrapperRcd>().get(e_condnames_mean[icor], forestDEH);
304  e_forestH_mean_.push_back(forestDEH.product());
305  evs.get<GBRDWrapperRcd>().get(e_condnames_sigma[icor], forestDEH);
306  e_forestH_sigma_.push_back(forestDEH.product());
307  }
308 }
309 
310 template<typename T, typename U, typename V>
311 inline void make_consumes(T& tag,U& tok,V& sume) {
312  if(!(empty_tag == tag))
313  tok = sume.template consumes<edm::ValueMap<float> >(tag);
314 }
315 
316 template<typename T, typename U, typename V>
317 inline void make_int_consumes(T& tag,U& tok,V& sume) {
318  if(!(empty_tag == tag))
319  tok = sume.template consumes<edm::ValueMap<int> >(tag);
320 }
321 
323 
324  rhoToken_ = sumes.consumes<double>(rhoTag_);
326 
329 
330  //setup electrons
331  if(!(empty_tag == e_conf.electron_src))
333 
334  for ( std::unordered_map<std::string, ValMapFloatTagTokenPair>::iterator imap = e_conf.tag_float_token_map.begin();
335  imap != e_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 = e_conf.tag_int_token_map.begin();
341  imap != e_conf.tag_int_token_map.end();
342  imap++) {
343  make_int_consumes(imap->second.first, imap->second.second, sumes);
344  }
345 
346  // setup photons
347  if(!(empty_tag == ph_conf.photon_src))
349 
350  for ( std::unordered_map<std::string, ValMapFloatTagTokenPair>::iterator imap = ph_conf.tag_float_token_map.begin();
351  imap != ph_conf.tag_float_token_map.end();
352  imap++) {
353  make_consumes(imap->second.first, imap->second.second, sumes);
354  }
355 
356  for ( std::unordered_map<std::string, ValMapIntTagTokenPair>::iterator imap = ph_conf.tag_int_token_map.begin();
357  imap != ph_conf.tag_int_token_map.end();
358  imap++) {
359  make_int_consumes(imap->second.first, imap->second.second, sumes);
360  }
361 }
362 
363 template<typename T, typename U, typename V, typename Z>
364 inline void assignValue(const T& ptr, const U& tok, const V& map, Z& value) {
365  if( !tok.isUninitialized() ) value = map.find(tok.index())->second->get(ptr.id(),ptr.key());
366 }
367 
369  // we encounter two cases here, either we are running AOD -> MINIAOD
370  // and the value maps are to the reducedEG object, can use original object ptr
371  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
372 
375  auto key = eles_by_oop.find(ele.originalObjectRef().key());
376  if( key != eles_by_oop.end() ) {
377  ptr = key->second;
378  } else {
379  throw cms::Exception("BadElectronKey")
380  << "Original object pointer with key = " << ele.originalObjectRef().key()
381  << " not found in cache!";
382  }
383  }
384  std::array<float, 33> eval;
385 
387  edm::Ptr<reco::CaloCluster> theseed = sc->seed();
388 
389  // SET INPUTS
390  eval[0] = nVtx_;
391  eval[1] = sc->rawEnergy();
392  eval[2] = sc->eta();
393  eval[3] = sc->phi();
394  eval[4] = sc->etaWidth();
395  eval[5] = sc->phiWidth();
396  eval[6] = ele.r9();
397  eval[7] = theseed->energy()/sc->rawEnergy();
398 
399  float sieip=0, cryPhi=0, cryEta=0;
400  int iPhi=0, iEta=0;
401  float eMax=0, e2nd=0, eTop=0, eBottom=0, eLeft=0, eRight=0;
402  float clusterMaxDR=0, clusterMaxDRDPhi=0, clusterMaxDRDEta=0, clusterMaxDRRawEnergy=0;
403  float clusterRawEnergy0=0, clusterRawEnergy1=0, clusterRawEnergy2=0;
404  float clusterDPhiToSeed0=0, clusterDPhiToSeed1=0, clusterDPhiToSeed2=0;
405  float clusterDEtaToSeed0=0, clusterDEtaToSeed1=0, clusterDEtaToSeed2=0;
406 
407  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("sigmaIetaIphi"))->second.second, ele_vmaps, sieip);
408  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("eMax"))->second.second, ele_vmaps, eMax);
409  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("e2nd"))->second.second, ele_vmaps, e2nd);
410  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("eTop"))->second.second, ele_vmaps, eTop);
411  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("eBottom"))->second.second, ele_vmaps, eBottom);
412  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("eLeft"))->second.second, ele_vmaps, eLeft);
413  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("eRight"))->second.second, ele_vmaps, eRight);
414  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterMaxDR"))->second.second, ele_vmaps, clusterMaxDR);
415  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterMaxDRDPhi"))->second.second, ele_vmaps, clusterMaxDRDPhi);
416  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterMaxDRDEta"))->second.second, ele_vmaps, clusterMaxDRDEta);
417  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterMaxDRRawEnergy"))->second.second, ele_vmaps, clusterMaxDRRawEnergy);
418  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterRawEnergy0"))->second.second, ele_vmaps, clusterRawEnergy0);
419  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterRawEnergy1"))->second.second, ele_vmaps, clusterRawEnergy1);
420  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterRawEnergy2"))->second.second, ele_vmaps, clusterRawEnergy2);
421  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterDPhiToSeed0"))->second.second, ele_vmaps, clusterDPhiToSeed0);
422  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterDPhiToSeed1"))->second.second, ele_vmaps, clusterDPhiToSeed1);
423  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterDPhiToSeed2"))->second.second, ele_vmaps, clusterDPhiToSeed2);
424  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterDEtaToSeed0"))->second.second, ele_vmaps, clusterDEtaToSeed0);
425  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterDEtaToSeed1"))->second.second, ele_vmaps, clusterDEtaToSeed1);
426  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("clusterDEtaToSeed2"))->second.second, ele_vmaps, clusterDEtaToSeed2);
427  assignValue(ptr, e_conf.tag_int_token_map.find(std::string("iPhi"))->second.second, ele_int_vmaps, iPhi);
428  assignValue(ptr, e_conf.tag_int_token_map.find(std::string("iEta"))->second.second, ele_int_vmaps, iEta);
429  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("cryPhi"))->second.second, ele_vmaps, cryPhi);
430  assignValue(ptr, e_conf.tag_float_token_map.find(std::string("cryEta"))->second.second, ele_vmaps, cryEta);
431 
432  eval[8] = eMax/sc->rawEnergy();
433  eval[9] = e2nd/sc->rawEnergy();
434  eval[10] = (eLeft+eRight!=0. ? (eLeft-eRight)/(eLeft+eRight) : 0.);
435  eval[11] = (eTop+eBottom!=0. ? (eTop-eBottom)/(eTop+eBottom) : 0.);
436  eval[12] = ele.sigmaIetaIeta();
437  eval[13] = sieip;
438  eval[14] = ele.sigmaIphiIphi();
439  const int N_ECAL = sc->clustersEnd() - sc->clustersBegin();
440  eval[15] = std::max(0,N_ECAL - 1);
441  eval[16] = clusterMaxDR;
442  eval[17] = clusterMaxDRDPhi;
443  eval[18] = clusterMaxDRDEta;
444  eval[19] = clusterMaxDRRawEnergy/sc->rawEnergy();
445  eval[20] = clusterRawEnergy0/sc->rawEnergy();
446  eval[21] = clusterRawEnergy1/sc->rawEnergy();
447  eval[22] = clusterRawEnergy2/sc->rawEnergy();
448  eval[23] = clusterDPhiToSeed0;
449  eval[24] = clusterDPhiToSeed1;
450  eval[25] = clusterDPhiToSeed2;
451  eval[26] = clusterDEtaToSeed0;
452  eval[27] = clusterDEtaToSeed1;
453  eval[28] = clusterDEtaToSeed2;
454 
455  bool iseb = ele.isEB();
456 
457  if (iseb) {
458  eval[29] = cryEta;
459  eval[30] = cryPhi;
460  eval[31] = iEta;
461  eval[32] = iPhi;
462  } else {
463  eval[29] = sc->preshowerEnergy()/sc->rawEnergy();
464  }
465 
466  //magic numbers for MINUIT-like transformation of BDT output onto limited range
467  //(These should be stored inside the conditions object in the future as well)
468  constexpr double meanlimlow = 0.2;
469  constexpr double meanlimhigh = 2.0;
470  constexpr double meanoffset = meanlimlow + 0.5*(meanlimhigh-meanlimlow);
471  constexpr double meanscale = 0.5*(meanlimhigh-meanlimlow);
472 
473  constexpr double sigmalimlow = 0.0002;
474  constexpr double sigmalimhigh = 0.5;
475  constexpr double sigmaoffset = sigmalimlow + 0.5*(sigmalimhigh-sigmalimlow);
476  constexpr double sigmascale = 0.5*(sigmalimhigh-sigmalimlow);
477 
478  int coridx = 0;
479  if (!iseb)
480  coridx = 1;
481 
482  //these are the actual BDT responses
483  double rawmean = e_forestH_mean_[coridx]->GetResponse(eval.data());
484  double rawsigma = e_forestH_sigma_[coridx]->GetResponse(eval.data());
485 
486  //apply transformation to limited output range (matching the training)
487  double mean = meanoffset + meanscale*vdt::fast_sin(rawmean);
488  double sigma = sigmaoffset + sigmascale*vdt::fast_sin(rawsigma);
489 
490  //regression target is ln(Etrue/Eraw)
491  //so corrected energy is ecor=exp(mean)*e, uncertainty is exp(mean)*eraw*sigma=ecor*sigma
492  double ecor = mean*(eval[1]);
493  if (!iseb)
494  ecor = mean*(eval[1]+sc->preshowerEnergy());
495  const double sigmacor = sigma*ecor;
496 
497  ele.setCorrectedEcalEnergy(ecor);
498  ele.setCorrectedEcalEnergyError(sigmacor);
499 
500  // E-p combination
501  //std::array<float, 11> eval_ep;
502  float eval_ep[11];
503 
504  const float ep = ele.trackMomentumAtVtx().R();
505  const float tot_energy = sc->rawEnergy()+sc->preshowerEnergy();
506  const float momentumError = ele.trackMomentumError();
507  const float trkMomentumRelError = ele.trackMomentumError()/ep;
508  const float eOverP = tot_energy*mean/ep;
509  eval_ep[0] = tot_energy*mean;
510  eval_ep[1] = sigma/mean;
511  eval_ep[2] = ep;
512  eval_ep[3] = trkMomentumRelError;
513  eval_ep[4] = sigma/mean/trkMomentumRelError;
514  eval_ep[5] = tot_energy*mean/ep;
515  eval_ep[6] = tot_energy*mean/ep*sqrt(sigma/mean*sigma/mean+trkMomentumRelError*trkMomentumRelError);
516  eval_ep[7] = ele.ecalDriven();
517  eval_ep[8] = ele.trackerDrivenSeed();
518  eval_ep[9] = int(ele.classification());//eleClass;
519  eval_ep[10] = iseb;
520 
521  // CODE FOR FUTURE SEMI_PARAMETRIC
522  //double rawweight = ep_forestH_mean_[coridx]->GetResponse(eval_ep.data());
524  //double weight = meanoffset + meanscale*vdt::fast_sin(rawweight);
526 
527  // CODE FOR STANDARD BDT
528  double weight = 0.;
529  if ( eOverP > 0.025 &&
530  std::abs(ep-ecor) < 15.*std::sqrt( momentumError*momentumError + sigmacor*sigmacor ) ) {
531  // protection against crazy track measurement
532  weight = ep_forestH_weight_->GetResponse(eval_ep);
533  if(weight>1.)
534  weight = 1.;
535  else if(weight<0.)
536  weight = 0.;
537  }
538 
539  double combinedMomentum = weight*ele.trackMomentumAtVtx().R() + (1.-weight)*ecor;
540  double combinedMomentumError = sqrt(weight*weight*ele.trackMomentumError()*ele.trackMomentumError() + (1.-weight)*(1.-weight)*sigmacor*sigmacor);
541 
542  math::XYZTLorentzVector oldMomentum = ele.p4();
543  math::XYZTLorentzVector newMomentum = math::XYZTLorentzVector(oldMomentum.x()*combinedMomentum/oldMomentum.t(),
544  oldMomentum.y()*combinedMomentum/oldMomentum.t(),
545  oldMomentum.z()*combinedMomentum/oldMomentum.t(),
546  combinedMomentum);
547 
548  //ele.correctEcalEnergy(combinedMomentum, combinedMomentumError);
549  ele.correctMomentum(newMomentum, ele.trackMomentumError(), combinedMomentumError);
550 }
551 
553  // we encounter two cases here, either we are running AOD -> MINIAOD
554  // and the value maps are to the reducedEG object, can use original object ptr
555  // or we are running MINIAOD->MINIAOD and we need to fetch the pat objects to reference
557 
559  auto key = phos_by_oop.find(pho.originalObjectRef().key());
560  if( key != phos_by_oop.end() ) {
561  ptr = key->second;
562  } else {
563  throw cms::Exception("BadPhotonKey")
564  << "Original object pointer with key = " << pho.originalObjectRef().key() << " not found in cache!";
565  }
566  }
567 
568  std::array<float, 31> eval;
570  edm::Ptr<reco::CaloCluster> theseed = sc->seed();
571 
572  // SET INPUTS
573  eval[0] = sc->rawEnergy();
574  //eval[1] = sc->position().Eta();
575  //eval[2] = sc->position().Phi();
576  eval[1] = pho.r9();
577  eval[2] = sc->etaWidth();
578  eval[3] = sc->phiWidth();
579  const int N_ECAL = sc->clustersEnd() - sc->clustersBegin();
580  eval[4] = std::max(0,N_ECAL - 1);
581  eval[5] = pho.hadronicOverEm();
582  eval[6] = rhoValue_;
583  eval[7] = nVtx_;
584  eval[8] = theseed->eta()-sc->position().Eta();
585  eval[9] = reco::deltaPhi(theseed->phi(),sc->position().Phi());
586  eval[10] = pho.seedEnergy()/sc->rawEnergy();
587  eval[11] = pho.e3x3()/pho.e5x5();
588  eval[12] = pho.sigmaIetaIeta();
589 
590  float sipip=0, sieip=0, e2x5Max=0, e2x5Left=0, e2x5Right=0, e2x5Top=0, e2x5Bottom=0;
591  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("sigmaIetaIphi"))->second.second, pho_vmaps, sieip);
592  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("sigmaIphiIphi"))->second.second, pho_vmaps, sipip);
593  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("e2x5Max"))->second.second, pho_vmaps, e2x5Max);
594  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("e2x5Left"))->second.second, pho_vmaps, e2x5Left);
595  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("e2x5Right"))->second.second, pho_vmaps, e2x5Right);
596  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("e2x5Top"))->second.second, pho_vmaps, e2x5Top);
597  assignValue(ptr, ph_conf.tag_float_token_map.find(std::string("e2x5Bottom"))->second.second, pho_vmaps, e2x5Bottom);
598 
599  eval[13] = sipip;
600  eval[14] = sieip;
601  eval[15] = pho.maxEnergyXtal()/pho.e5x5();
602  eval[16] = pho.e2nd()/pho.e5x5();
603  eval[17] = pho.eTop()/pho.e5x5();
604  eval[18] = pho.eBottom()/pho.e5x5();
605  eval[19] = pho.eLeft()/pho.e5x5();
606  eval[20] = pho.eRight()/pho.e5x5();
607  eval[21] = e2x5Max/pho.e5x5();
608  eval[22] = e2x5Left/pho.e5x5();
609  eval[23] = e2x5Right/pho.e5x5();
610  eval[24] = e2x5Top/pho.e5x5();
611  eval[25] = e2x5Bottom/pho.e5x5();
612 
613  bool iseb = pho.isEB();
614 
615  if (iseb) {
616  EBDetId ebseedid(theseed->seed());
617  eval[26] = pho.e5x5()/pho.seedEnergy();
618  eval[27] = ebseedid.ieta();
619  eval[28] = ebseedid.iphi();
620  } else {
621  EEDetId eeseedid(theseed->seed());
622  eval[26] = sc->preshowerEnergy()/sc->rawEnergy();
623  eval[27] = sc->preshowerEnergyPlane1()/sc->rawEnergy();
624  eval[28] = sc->preshowerEnergyPlane2()/sc->rawEnergy();
625  eval[29] = eeseedid.ix();
626  eval[30] = eeseedid.iy();
627  }
628 
629  //magic numbers for MINUIT-like transformation of BDT output onto limited range
630  //(These should be stored inside the conditions object in the future as well)
631  const double meanlimlow = 0.2;
632  const double meanlimhigh = 2.0;
633  const double meanoffset = meanlimlow + 0.5*(meanlimhigh-meanlimlow);
634  const double meanscale = 0.5*(meanlimhigh-meanlimlow);
635 
636  const double sigmalimlow = 0.0002;
637  const double sigmalimhigh = 0.5;
638  const double sigmaoffset = sigmalimlow + 0.5*(sigmalimhigh-sigmalimlow);
639  const double sigmascale = 0.5*(sigmalimhigh-sigmalimlow);
640 
641  int coridx = 0;
642  if (!iseb)
643  coridx = 1;
644 
645  //these are the actual BDT responses
646  double rawmean = ph_forestH_mean_[coridx]->GetResponse(eval.data());
647  double rawsigma = ph_forestH_sigma_[coridx]->GetResponse(eval.data());
648  //apply transformation to limited output range (matching the training)
649  double mean = meanoffset + meanscale*vdt::fast_sin(rawmean);
650  double sigma = sigmaoffset + sigmascale*vdt::fast_sin(rawsigma);
651 
652  //regression target is ln(Etrue/Eraw)
653  //so corrected energy is ecor=exp(mean)*e, uncertainty is exp(mean)*eraw*sigma=ecor*sigma
654  double ecor = mean*eval[0];
655  if (!iseb)
656  ecor = mean*(eval[0]+sc->preshowerEnergy());
657 
658  double sigmacor = sigma*ecor;
659  pho.setCorrectedEnergy(reco::Photon::P4type::regression2, ecor, sigmacor, true);
660 }
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
void make_int_consumes(T &tag, U &tok, V &sume)
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 make_consumes(T &tag, U &tok, V &sume)
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
bool isRealData() const
Definition: EventBase.h:64
#define constexpr
float e2nd() const
Definition: Photon.h:241
bool isEB() const
Definition: GsfElectron.h:350
U second(std::pair< T, U > const &p)
edm::EDGetTokenT< int > bunchSpacingToken_
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
void get_product(const edm::Event &evt, const edm::EDGetTokenT< edm::ValueMap< T > > &tok, std::unordered_map< unsigned, edm::Handle< edm::ValueMap< T > > > &map)
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
RunNumber_t run() const
Definition: Event.h:85
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< 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
void assignValue(const T &ptr, const U &tok, const V &map, Z &value)
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)
unsigned int RunNumber_t
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