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