CMS 3D CMS Logo

IsoValueMapProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PhysicsTools/NanoAOD
4 // Class: IsoValueMapProducer
5 //
13 //
14 // Original Author: Marco Peruzzi
15 // Created: Mon, 04 Sep 2017 22:43:53 GMT
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
25 
28 
31 
33 
38 
39 //
40 // class declaration
41 //
42 
43 template <typename T>
45 public:
46  explicit IsoValueMapProducer(const edm::ParameterSet& iConfig)
47  : src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
48  relative_(iConfig.getParameter<bool>("relative")),
49  doQuadratic_(iConfig.getParameter<bool>("doQuadratic")) {
50  if ((typeid(T) == typeid(pat::Muon)) || (typeid(T) == typeid(pat::Electron)) ||
51  typeid(T) == typeid(pat::IsolatedTrack)) {
52  produces<edm::ValueMap<float>>("miniIsoChg");
53  produces<edm::ValueMap<float>>("miniIsoAll");
54  ea_miniiso_ =
55  std::make_unique<EffectiveAreas>((iConfig.getParameter<edm::FileInPath>("EAFile_MiniIso")).fullPath());
56  rho_miniiso_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho_MiniIso"));
57  }
58  if ((typeid(T) == typeid(pat::Electron))) {
59  produces<edm::ValueMap<float>>("PFIsoChg");
60  produces<edm::ValueMap<float>>("PFIsoAll");
61  produces<edm::ValueMap<float>>("PFIsoAll04");
62  ea_pfiso_ = std::make_unique<EffectiveAreas>((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso")).fullPath());
63  rho_pfiso_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho_PFIso"));
64  } else if ((typeid(T) == typeid(pat::Photon))) {
65  rho_pfiso_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho_PFIso"));
66 
67  if (!doQuadratic_) {
68  produces<edm::ValueMap<float>>("PFIsoChg");
69  produces<edm::ValueMap<float>>("PFIsoAll");
70 
72  std::make_unique<EffectiveAreas>((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso_Chg")).fullPath());
74  std::make_unique<EffectiveAreas>((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso_Neu")).fullPath());
76  std::make_unique<EffectiveAreas>((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso_Pho")).fullPath());
77 
78  }
79 
80  else {
81  produces<edm::ValueMap<float>>("PFIsoChgQuadratic");
82  produces<edm::ValueMap<float>>("PFIsoAllQuadratic");
83 
84  quadratic_ea_pfiso_chg_ = std::make_unique<EffectiveAreas>(
85  (iConfig.getParameter<edm::FileInPath>("QuadraticEAFile_PFIso_Chg")).fullPath(), true);
86  quadratic_ea_pfiso_ecal_ = std::make_unique<EffectiveAreas>(
87  (iConfig.getParameter<edm::FileInPath>("QuadraticEAFile_PFIso_ECal")).fullPath(), true);
88  quadratic_ea_pfiso_hcal_ = std::make_unique<EffectiveAreas>(
89  (iConfig.getParameter<edm::FileInPath>("QuadraticEAFile_PFIso_HCal")).fullPath(), true);
90  }
91  }
92  }
93 
94  ~IsoValueMapProducer() override {}
95 
96  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
97 
98 private:
99  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
100 
101  // ----------member data ---------------------------
102 
104  bool relative_;
108  std::unique_ptr<EffectiveAreas> ea_miniiso_;
109  std::unique_ptr<EffectiveAreas> ea_pfiso_;
110  std::unique_ptr<EffectiveAreas> ea_pfiso_chg_;
111  std::unique_ptr<EffectiveAreas> ea_pfiso_neu_;
112  std::unique_ptr<EffectiveAreas> ea_pfiso_pho_;
113  std::unique_ptr<EffectiveAreas> quadratic_ea_pfiso_chg_;
114  std::unique_ptr<EffectiveAreas> quadratic_ea_pfiso_ecal_;
115  std::unique_ptr<EffectiveAreas> quadratic_ea_pfiso_hcal_;
116 
117  float getEtaForEA(const T*) const;
118  void doMiniIso(edm::Event&) const;
119  void doPFIsoEle(edm::Event&) const;
120  void doPFIsoPho(edm::Event&) const;
121  void doPFIsoPhoQuadratic(edm::Event&) const;
122 };
123 
124 //
125 // constants, enums and typedefs
126 //
127 
128 //
129 // static data member definitions
130 //
131 
132 template <typename T>
134  return obj->eta();
135 }
136 template <>
138  return el->superCluster()->eta();
139 }
140 template <>
142  return ph->superCluster()->eta();
143 }
144 
145 template <typename T>
147  if ((typeid(T) == typeid(pat::Muon)) || (typeid(T) == typeid(pat::Electron)) ||
148  typeid(T) == typeid(pat::IsolatedTrack)) {
149  doMiniIso(iEvent);
150  };
151  if ((typeid(T) == typeid(pat::Electron))) {
152  doPFIsoEle(iEvent);
153  }
154  if ((typeid(T) == typeid(pat::Photon))) {
155  if (!doQuadratic_)
156  doPFIsoPho(iEvent);
157  else
158  doPFIsoPhoQuadratic(iEvent);
159  }
160 }
161 
162 template <typename T>
164  auto src = iEvent.getHandle(src_);
165  const auto& rho = iEvent.get(rho_miniiso_);
166 
167  unsigned int nInput = src->size();
168 
169  std::vector<float> miniIsoChg, miniIsoAll;
170  miniIsoChg.reserve(nInput);
171  miniIsoAll.reserve(nInput);
172 
173  for (const auto& obj : *src) {
174  auto iso = obj.miniPFIsolation();
175  auto chg = iso.chargedHadronIso();
176  auto neu = iso.neutralHadronIso();
177  auto pho = iso.photonIso();
178  auto ea = ea_miniiso_->getEffectiveArea(fabs(getEtaForEA(&obj)));
179  float R = 10.0 / std::min(std::max(obj.pt(), 50.0), 200.0);
180  ea *= std::pow(R / 0.3, 2);
181  float scale = relative_ ? 1.0 / obj.pt() : 1;
182  miniIsoChg.push_back(scale * chg);
183  miniIsoAll.push_back(scale * (chg + std::max(0.0, neu + pho - rho * ea)));
184  }
185 
186  auto miniIsoChgV = std::make_unique<edm::ValueMap<float>>();
187  edm::ValueMap<float>::Filler fillerChg(*miniIsoChgV);
188  fillerChg.insert(src, miniIsoChg.begin(), miniIsoChg.end());
189  fillerChg.fill();
190  auto miniIsoAllV = std::make_unique<edm::ValueMap<float>>();
191  edm::ValueMap<float>::Filler fillerAll(*miniIsoAllV);
192  fillerAll.insert(src, miniIsoAll.begin(), miniIsoAll.end());
193  fillerAll.fill();
194 
195  iEvent.put(std::move(miniIsoChgV), "miniIsoChg");
196  iEvent.put(std::move(miniIsoAllV), "miniIsoAll");
197 }
198 
199 template <>
201 
202 template <typename T>
204 
205 template <>
208  iEvent.getByToken(src_, src);
209  const auto& rho = iEvent.get(rho_pfiso_);
210 
211  unsigned int nInput = src->size();
212 
213  std::vector<float> PFIsoChg, PFIsoAll, PFIsoAll04;
214  PFIsoChg.reserve(nInput);
215  PFIsoAll.reserve(nInput);
216  PFIsoAll04.reserve(nInput);
217 
218  for (const auto& obj : *src) {
219  auto iso = obj.pfIsolationVariables();
220  auto chg = iso.sumChargedHadronPt;
221  auto neu = iso.sumNeutralHadronEt;
222  auto pho = iso.sumPhotonEt;
223  auto ea = ea_pfiso_->getEffectiveArea(fabs(getEtaForEA(&obj)));
224  float scale = relative_ ? 1.0 / obj.pt() : 1;
225  PFIsoChg.push_back(scale * chg);
226  PFIsoAll.push_back(scale * (chg + std::max(0.0, neu + pho - rho * ea)));
227  PFIsoAll04.push_back(scale * (obj.chargedHadronIso() +
228  std::max(0.0, obj.neutralHadronIso() + obj.photonIso() - rho * ea * 16. / 9.)));
229  }
230 
231  auto PFIsoChgV = std::make_unique<edm::ValueMap<float>>();
232  edm::ValueMap<float>::Filler fillerChg(*PFIsoChgV);
233  fillerChg.insert(src, PFIsoChg.begin(), PFIsoChg.end());
234  fillerChg.fill();
235  auto PFIsoAllV = std::make_unique<edm::ValueMap<float>>();
236  edm::ValueMap<float>::Filler fillerAll(*PFIsoAllV);
237  fillerAll.insert(src, PFIsoAll.begin(), PFIsoAll.end());
238  fillerAll.fill();
239  auto PFIsoAll04V = std::make_unique<edm::ValueMap<float>>();
240  edm::ValueMap<float>::Filler fillerAll04(*PFIsoAll04V);
241  fillerAll04.insert(src, PFIsoAll04.begin(), PFIsoAll04.end());
242  fillerAll04.fill();
243 
244  iEvent.put(std::move(PFIsoChgV), "PFIsoChg");
245  iEvent.put(std::move(PFIsoAllV), "PFIsoAll");
246  iEvent.put(std::move(PFIsoAll04V), "PFIsoAll04");
247 }
248 
249 template <typename T>
251 
252 template <>
255  iEvent.getByToken(src_, src);
256  const auto& rho = iEvent.get(rho_pfiso_);
257 
258  unsigned int nInput = src->size();
259 
260  std::vector<float> PFIsoChg, PFIsoAll;
261 
262  PFIsoChg.reserve(nInput);
263  PFIsoAll.reserve(nInput);
264 
265  for (const auto& obj : *src) {
266  auto chg = obj.chargedHadronIso();
267  auto neu = obj.neutralHadronIso();
268  auto pho = obj.photonIso();
269 
270  auto ea_chg = ea_pfiso_chg_->getEffectiveArea(fabs(getEtaForEA(&obj)));
271  auto ea_neu = ea_pfiso_neu_->getEffectiveArea(fabs(getEtaForEA(&obj)));
272  auto ea_pho = ea_pfiso_pho_->getEffectiveArea(fabs(getEtaForEA(&obj)));
273 
274  float scale = relative_ ? 1.0 / obj.pt() : 1;
275  PFIsoChg.push_back(scale * std::max(0.0, chg - rho * ea_chg));
276  PFIsoAll.push_back(PFIsoChg.back() +
277  scale * (std::max(0.0, neu - rho * ea_neu) + std::max(0.0, pho - rho * ea_pho)));
278  }
279 
280  auto PFIsoChgV = std::make_unique<edm::ValueMap<float>>();
281  edm::ValueMap<float>::Filler fillerChg(*PFIsoChgV);
282  fillerChg.insert(src, PFIsoChg.begin(), PFIsoChg.end());
283  fillerChg.fill();
284  auto PFIsoAllV = std::make_unique<edm::ValueMap<float>>();
285  edm::ValueMap<float>::Filler fillerAll(*PFIsoAllV);
286  fillerAll.insert(src, PFIsoAll.begin(), PFIsoAll.end());
287  fillerAll.fill();
288 
289  iEvent.put(std::move(PFIsoChgV), "PFIsoChg");
290  iEvent.put(std::move(PFIsoAllV), "PFIsoAll");
291 }
292 
293 template <typename T>
295 
296 template <>
299  iEvent.getByToken(src_, src);
300  const auto& rho = iEvent.get(rho_pfiso_);
301 
302  unsigned int nInput = src->size();
303 
304  std::vector<float> PFIsoChgQuadratic, PFIsoAllQuadratic;
305 
306  PFIsoChgQuadratic.reserve(nInput);
307  PFIsoAllQuadratic.reserve(nInput);
308 
309  for (const auto& obj : *src) {
310  auto chg = obj.chargedHadronIso();
311  auto ecal = obj.ecalPFClusterIso();
312  auto hcal = obj.hcalPFClusterIso();
313 
314  auto quadratic_ea_chg = quadratic_ea_pfiso_chg_->getQuadraticEA(fabs(getEtaForEA(&obj)));
315  auto linear_ea_chg = quadratic_ea_pfiso_chg_->getLinearEA(fabs(getEtaForEA(&obj)));
316  auto quadratic_ea_ecal = quadratic_ea_pfiso_ecal_->getQuadraticEA(fabs(getEtaForEA(&obj)));
317  auto linear_ea_ecal = quadratic_ea_pfiso_ecal_->getLinearEA(fabs(getEtaForEA(&obj)));
318  auto quadratic_ea_hcal = quadratic_ea_pfiso_hcal_->getQuadraticEA(fabs(getEtaForEA(&obj)));
319  auto linear_ea_hcal = quadratic_ea_pfiso_hcal_->getLinearEA(fabs(getEtaForEA(&obj)));
320 
321  float scale = relative_ ? 1.0 / obj.pt() : 1;
322 
323  PFIsoChgQuadratic.push_back(scale * std::max(0.0, chg - (quadratic_ea_chg * rho * rho + linear_ea_chg * rho)));
324  PFIsoAllQuadratic.push_back(PFIsoChgQuadratic.back() +
325  scale * (std::max(0.0, ecal - (quadratic_ea_ecal * rho * rho + linear_ea_ecal * rho)) +
326  std::max(0.0, hcal - (quadratic_ea_hcal * rho * rho + linear_ea_hcal * rho))));
327  }
328 
329  auto PFIsoChgQuadraticV = std::make_unique<edm::ValueMap<float>>();
330  edm::ValueMap<float>::Filler fillerChgQuadratic(*PFIsoChgQuadraticV);
331  fillerChgQuadratic.insert(src, PFIsoChgQuadratic.begin(), PFIsoChgQuadratic.end());
332  fillerChgQuadratic.fill();
333 
334  auto PFIsoAllQuadraticV = std::make_unique<edm::ValueMap<float>>();
335  edm::ValueMap<float>::Filler fillerAllQuadratic(*PFIsoAllQuadraticV);
336  fillerAllQuadratic.insert(src, PFIsoAllQuadratic.begin(), PFIsoAllQuadratic.end());
337  fillerAllQuadratic.fill();
338 
339  iEvent.put(std::move(PFIsoChgQuadraticV), "PFIsoChgQuadratic");
340  iEvent.put(std::move(PFIsoAllQuadraticV), "PFIsoAllQuadratic");
341 }
342 
343 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
344 template <typename T>
347  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
348  desc.add<bool>("relative")->setComment("compute relative isolation instead of absolute one");
349  desc.add<bool>("doQuadratic", false)->setComment("flag to do quadratic EA corections for photons");
350  if ((typeid(T) == typeid(pat::Muon)) || (typeid(T) == typeid(pat::Electron)) ||
351  typeid(T) == typeid(pat::IsolatedTrack)) {
352  desc.add<edm::FileInPath>("EAFile_MiniIso")
353  ->setComment("txt file containing effective areas to be used for mini-isolation pileup subtraction");
354  desc.add<edm::InputTag>("rho_MiniIso")
355  ->setComment("rho to be used for effective-area based mini-isolation pileup subtraction");
356  }
357  if ((typeid(T) == typeid(pat::Electron))) {
358  desc.add<edm::FileInPath>("EAFile_PFIso")
359  ->setComment(
360  "txt file containing effective areas to be used for PF-isolation pileup subtraction for electrons");
361  desc.add<edm::InputTag>("rho_PFIso")
362  ->setComment("rho to be used for effective-area based PF-isolation pileup subtraction for electrons");
363  }
364  if ((typeid(T) == typeid(pat::Photon))) {
365  desc.addOptional<edm::InputTag>("mapIsoChg")
366  ->setComment("input charged PF isolation calculated in VID for photons");
367  desc.addOptional<edm::InputTag>("mapIsoNeu")
368  ->setComment("input neutral PF isolation calculated in VID for photons");
369  desc.addOptional<edm::InputTag>("mapIsoPho")->setComment("input photon PF isolation calculated in VID for photons");
370 
371  desc.addOptional<edm::FileInPath>("EAFile_PFIso_Chg")
372  ->setComment(
373  "txt file containing effective areas to be used for charged PF-isolation pileup subtraction for photons");
374  desc.addOptional<edm::FileInPath>("EAFile_PFIso_Neu")
375  ->setComment(
376  "txt file containing effective areas to be used for neutral PF-isolation pileup subtraction for photons");
377  desc.addOptional<edm::FileInPath>("EAFile_PFIso_Pho")
378  ->setComment(
379  "txt file containing effective areas to be used for photon PF-isolation pileup subtraction for photons");
380 
381  desc.add<edm::InputTag>("rho_PFIso")
382  ->setComment("rho to be used for effective-area based PF-isolation pileup subtraction for photons");
383 
384  desc.addOptional<edm::FileInPath>("QuadraticEAFile_PFIso_Chg")
385  ->setComment(
386  "txt file containing quadratic effective areas to be used for charged PF-isolation pileup subtraction for "
387  "photons");
388  desc.addOptional<edm::FileInPath>("QuadraticEAFile_PFIso_ECal")
389  ->setComment(
390  "txt file containing quadratic effective areas to be used for ecal PF-isolation pileup subtraction for "
391  "photons");
392  desc.addOptional<edm::FileInPath>("QuadraticEAFile_PFIso_HCal")
393  ->setComment(
394  "txt file containing quadratic effective areas to be used for hcal PF-isolation pileup subtraction for "
395  "photons");
396  }
397 
399  if (typeid(T) == typeid(pat::Muon))
400  modname += "Muon";
401  else if (typeid(T) == typeid(pat::Electron))
402  modname += "Ele";
403  else if (typeid(T) == typeid(pat::Photon))
404  modname += "Pho";
405  else if (typeid(T) == typeid(pat::IsolatedTrack))
406  modname += "IsoTrack";
407  modname += "IsoValueMapProducer";
408  descriptions.add(modname, desc);
409 }
410 
415 
416 //define this as a plug-in
Analysis-level Photon class.
Definition: Photon.h:46
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
float getEtaForEA(const T *) const
std::unique_ptr< EffectiveAreas > quadratic_ea_pfiso_chg_
IsoValueMapProducer(const edm::ParameterSet &iConfig)
const float chg[109]
Definition: CoreSimTrack.cc:5
IsoValueMapProducer< pat::Muon > MuonIsoValueMapProducer
std::unique_ptr< EffectiveAreas > quadratic_ea_pfiso_hcal_
edm::EDGetTokenT< double > rho_pfiso_
reco::SuperClusterRef superCluster() const override
override the reco::GsfElectron::superCluster method, to access the internal storage of the superclust...
IsoValueMapProducer< pat::Photon > PhoIsoValueMapProducer
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
int iEvent
Definition: GenABIO.cc:224
void doMiniIso(edm::Event &) const
IsoValueMapProducer< pat::IsolatedTrack > IsoTrackIsoValueMapProducer
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void doPFIsoEle(edm::Event &) const
IsoValueMapProducer< pat::Electron > EleIsoValueMapProducer
std::unique_ptr< EffectiveAreas > ea_pfiso_chg_
std::unique_ptr< EffectiveAreas > ea_pfiso_pho_
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
edm::EDGetTokenT< edm::View< T > > src_
std::unique_ptr< EffectiveAreas > ea_pfiso_neu_
Analysis-level electron class.
Definition: Electron.h:51
void add(std::string const &label, ParameterSetDescription const &psetDescription)
HLT enums.
void doPFIsoPho(edm::Event &) const
std::unique_ptr< EffectiveAreas > quadratic_ea_pfiso_ecal_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::EDGetTokenT< double > rho_miniiso_
std::unique_ptr< EffectiveAreas > ea_pfiso_
void doPFIsoPhoQuadratic(edm::Event &) const
long double T
std::unique_ptr< EffectiveAreas > ea_miniiso_
Analysis-level muon class.
Definition: Muon.h:51
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
def move(src, dest)
Definition: eostools.py:511