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  if ((typeid(T) == typeid(pat::Muon)) || (typeid(T) == typeid(pat::Electron)) ||
50  typeid(T) == typeid(pat::IsolatedTrack)) {
51  produces<edm::ValueMap<float>>("miniIsoChg");
52  produces<edm::ValueMap<float>>("miniIsoAll");
53  ea_miniiso_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_MiniIso")).fullPath()));
54  rho_miniiso_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho_MiniIso"));
55  }
56  if ((typeid(T) == typeid(pat::Electron))) {
57  produces<edm::ValueMap<float>>("PFIsoChg");
58  produces<edm::ValueMap<float>>("PFIsoAll");
59  produces<edm::ValueMap<float>>("PFIsoAll04");
60  ea_pfiso_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso")).fullPath()));
61  rho_pfiso_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho_PFIso"));
62  } else if ((typeid(T) == typeid(pat::Photon))) {
63  produces<edm::ValueMap<float>>("PFIsoChg");
64  produces<edm::ValueMap<float>>("PFIsoAll");
65  ea_pfiso_chg_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso_Chg")).fullPath()));
66  ea_pfiso_neu_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso_Neu")).fullPath()));
67  ea_pfiso_pho_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso_Pho")).fullPath()));
68  rho_pfiso_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho_PFIso"));
69  }
70  }
71  ~IsoValueMapProducer() override {}
72 
73  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
74 
75 private:
76  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
77 
78  // ----------member data ---------------------------
79 
81  bool relative_;
84  std::unique_ptr<EffectiveAreas> ea_miniiso_;
85  std::unique_ptr<EffectiveAreas> ea_pfiso_;
86  std::unique_ptr<EffectiveAreas> ea_pfiso_chg_;
87  std::unique_ptr<EffectiveAreas> ea_pfiso_neu_;
88  std::unique_ptr<EffectiveAreas> ea_pfiso_pho_;
89  float getEtaForEA(const T*) const;
90  void doMiniIso(edm::Event&) const;
91  void doPFIsoEle(edm::Event&) const;
92  void doPFIsoPho(edm::Event&) const;
93 };
94 
95 //
96 // constants, enums and typedefs
97 //
98 
99 //
100 // static data member definitions
101 //
102 
103 template <typename T>
105  return obj->eta();
106 }
107 template <>
109  return el->superCluster()->eta();
110 }
111 template <>
113  return ph->superCluster()->eta();
114 }
115 
116 template <typename T>
118  if ((typeid(T) == typeid(pat::Muon)) || (typeid(T) == typeid(pat::Electron)) ||
119  typeid(T) == typeid(pat::IsolatedTrack)) {
120  doMiniIso(iEvent);
121  };
122  if ((typeid(T) == typeid(pat::Electron))) {
123  doPFIsoEle(iEvent);
124  }
125  if ((typeid(T) == typeid(pat::Photon))) {
126  doPFIsoPho(iEvent);
127  }
128 }
129 
130 template <typename T>
133  iEvent.getByToken(src_, src);
135  iEvent.getByToken(rho_miniiso_, rho);
136 
137  unsigned int nInput = src->size();
138 
139  std::vector<float> miniIsoChg, miniIsoAll;
140  miniIsoChg.reserve(nInput);
141  miniIsoAll.reserve(nInput);
142 
143  for (const auto& obj : *src) {
144  auto iso = obj.miniPFIsolation();
145  auto chg = iso.chargedHadronIso();
146  auto neu = iso.neutralHadronIso();
147  auto pho = iso.photonIso();
148  auto ea = ea_miniiso_->getEffectiveArea(fabs(getEtaForEA(&obj)));
149  float R = 10.0 / std::min(std::max(obj.pt(), 50.0), 200.0);
150  ea *= std::pow(R / 0.3, 2);
151  float scale = relative_ ? 1.0 / obj.pt() : 1;
152  miniIsoChg.push_back(scale * chg);
153  miniIsoAll.push_back(scale * (chg + std::max(0.0, neu + pho - (*rho) * ea)));
154  }
155 
156  std::unique_ptr<edm::ValueMap<float>> miniIsoChgV(new edm::ValueMap<float>());
157  edm::ValueMap<float>::Filler fillerChg(*miniIsoChgV);
158  fillerChg.insert(src, miniIsoChg.begin(), miniIsoChg.end());
159  fillerChg.fill();
160  std::unique_ptr<edm::ValueMap<float>> miniIsoAllV(new edm::ValueMap<float>());
161  edm::ValueMap<float>::Filler fillerAll(*miniIsoAllV);
162  fillerAll.insert(src, miniIsoAll.begin(), miniIsoAll.end());
163  fillerAll.fill();
164 
165  iEvent.put(std::move(miniIsoChgV), "miniIsoChg");
166  iEvent.put(std::move(miniIsoAllV), "miniIsoAll");
167 }
168 
169 template <>
171 
172 template <typename T>
174 
175 template <>
178  iEvent.getByToken(src_, src);
180  iEvent.getByToken(rho_pfiso_, rho);
181 
182  unsigned int nInput = src->size();
183 
184  std::vector<float> PFIsoChg, PFIsoAll, PFIsoAll04;
185  PFIsoChg.reserve(nInput);
186  PFIsoAll.reserve(nInput);
187  PFIsoAll04.reserve(nInput);
188 
189  for (const auto& obj : *src) {
190  auto iso = obj.pfIsolationVariables();
191  auto chg = iso.sumChargedHadronPt;
192  auto neu = iso.sumNeutralHadronEt;
193  auto pho = iso.sumPhotonEt;
194  auto ea = ea_pfiso_->getEffectiveArea(fabs(getEtaForEA(&obj)));
195  float scale = relative_ ? 1.0 / obj.pt() : 1;
196  PFIsoChg.push_back(scale * chg);
197  PFIsoAll.push_back(scale * (chg + std::max(0.0, neu + pho - (*rho) * ea)));
198  PFIsoAll04.push_back(scale * (obj.chargedHadronIso() +
199  std::max(0.0, obj.neutralHadronIso() + obj.photonIso() - (*rho) * ea * 16. / 9.)));
200  }
201 
202  std::unique_ptr<edm::ValueMap<float>> PFIsoChgV(new edm::ValueMap<float>());
203  edm::ValueMap<float>::Filler fillerChg(*PFIsoChgV);
204  fillerChg.insert(src, PFIsoChg.begin(), PFIsoChg.end());
205  fillerChg.fill();
206  std::unique_ptr<edm::ValueMap<float>> PFIsoAllV(new edm::ValueMap<float>());
207  edm::ValueMap<float>::Filler fillerAll(*PFIsoAllV);
208  fillerAll.insert(src, PFIsoAll.begin(), PFIsoAll.end());
209  fillerAll.fill();
210  std::unique_ptr<edm::ValueMap<float>> PFIsoAll04V(new edm::ValueMap<float>());
211  edm::ValueMap<float>::Filler fillerAll04(*PFIsoAll04V);
212  fillerAll04.insert(src, PFIsoAll04.begin(), PFIsoAll04.end());
213  fillerAll04.fill();
214 
215  iEvent.put(std::move(PFIsoChgV), "PFIsoChg");
216  iEvent.put(std::move(PFIsoAllV), "PFIsoAll");
217  iEvent.put(std::move(PFIsoAll04V), "PFIsoAll04");
218 }
219 
220 template <typename T>
222 
223 template <>
226  iEvent.getByToken(src_, src);
228  iEvent.getByToken(rho_pfiso_, rho);
229 
230  unsigned int nInput = src->size();
231 
232  std::vector<float> PFIsoChg, PFIsoAll;
233  PFIsoChg.reserve(nInput);
234  PFIsoAll.reserve(nInput);
235 
236  for (const auto& obj : *src) {
237  auto chg = obj.chargedHadronIso();
238  auto neu = obj.neutralHadronIso();
239  auto pho = obj.photonIso();
240  auto ea_chg = ea_pfiso_chg_->getEffectiveArea(fabs(getEtaForEA(&obj)));
241  auto ea_neu = ea_pfiso_neu_->getEffectiveArea(fabs(getEtaForEA(&obj)));
242  auto ea_pho = ea_pfiso_pho_->getEffectiveArea(fabs(getEtaForEA(&obj)));
243  float scale = relative_ ? 1.0 / obj.pt() : 1;
244  PFIsoChg.push_back(scale * std::max(0.0, chg - (*rho) * ea_chg));
245  PFIsoAll.push_back(PFIsoChg.back() +
246  scale * (std::max(0.0, neu - (*rho) * ea_neu) + std::max(0.0, pho - (*rho) * ea_pho)));
247  }
248 
249  std::unique_ptr<edm::ValueMap<float>> PFIsoChgV(new edm::ValueMap<float>());
250  edm::ValueMap<float>::Filler fillerChg(*PFIsoChgV);
251  fillerChg.insert(src, PFIsoChg.begin(), PFIsoChg.end());
252  fillerChg.fill();
253  std::unique_ptr<edm::ValueMap<float>> PFIsoAllV(new edm::ValueMap<float>());
254  edm::ValueMap<float>::Filler fillerAll(*PFIsoAllV);
255  fillerAll.insert(src, PFIsoAll.begin(), PFIsoAll.end());
256  fillerAll.fill();
257 
258  iEvent.put(std::move(PFIsoChgV), "PFIsoChg");
259  iEvent.put(std::move(PFIsoAllV), "PFIsoAll");
260 }
261 
262 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
263 template <typename T>
266  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
267  desc.add<bool>("relative")->setComment("compute relative isolation instead of absolute one");
268  if ((typeid(T) == typeid(pat::Muon)) || (typeid(T) == typeid(pat::Electron)) ||
269  typeid(T) == typeid(pat::IsolatedTrack)) {
270  desc.add<edm::FileInPath>("EAFile_MiniIso")
271  ->setComment("txt file containing effective areas to be used for mini-isolation pileup subtraction");
272  desc.add<edm::InputTag>("rho_MiniIso")
273  ->setComment("rho to be used for effective-area based mini-isolation pileup subtraction");
274  }
275  if ((typeid(T) == typeid(pat::Electron))) {
276  desc.add<edm::FileInPath>("EAFile_PFIso")
277  ->setComment(
278  "txt file containing effective areas to be used for PF-isolation pileup subtraction for electrons");
279  desc.add<edm::InputTag>("rho_PFIso")
280  ->setComment("rho to be used for effective-area based PF-isolation pileup subtraction for electrons");
281  }
282  if ((typeid(T) == typeid(pat::Photon))) {
283  desc.add<edm::InputTag>("mapIsoChg")->setComment("input charged PF isolation calculated in VID for photons");
284  desc.add<edm::InputTag>("mapIsoNeu")->setComment("input neutral PF isolation calculated in VID for photons");
285  desc.add<edm::InputTag>("mapIsoPho")->setComment("input photon PF isolation calculated in VID for photons");
286  desc.add<edm::FileInPath>("EAFile_PFIso_Chg")
287  ->setComment(
288  "txt file containing effective areas to be used for charged PF-isolation pileup subtraction for photons");
289  desc.add<edm::FileInPath>("EAFile_PFIso_Neu")
290  ->setComment(
291  "txt file containing effective areas to be used for neutral PF-isolation pileup subtraction for photons");
292  desc.add<edm::FileInPath>("EAFile_PFIso_Pho")
293  ->setComment(
294  "txt file containing effective areas to be used for photon PF-isolation pileup subtraction for photons");
295  desc.add<edm::InputTag>("rho_PFIso")
296  ->setComment("rho to be used for effective-area based PF-isolation pileup subtraction for photons");
297  }
299  if (typeid(T) == typeid(pat::Muon))
300  modname += "Muon";
301  else if (typeid(T) == typeid(pat::Electron))
302  modname += "Ele";
303  else if (typeid(T) == typeid(pat::Photon))
304  modname += "Pho";
305  else if (typeid(T) == typeid(pat::IsolatedTrack))
306  modname += "IsoTrack";
307  modname += "IsoValueMapProducer";
308  descriptions.add(modname, desc);
309 }
310 
315 
316 //define this as a plug-in
T getParameter(std::string const &) const
Analysis-level Photon class.
Definition: Photon.h:46
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
IsoValueMapProducer(const edm::ParameterSet &iConfig)
const float chg[109]
Definition: CoreSimTrack.cc:5
IsoValueMapProducer< pat::Muon > MuonIsoValueMapProducer
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
edm::EDGetTokenT< double > rho_pfiso_
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
void doPFIsoEle(edm::Event &) const
reco::SuperClusterRef superCluster() const override
override the superCluster method from CaloJet, to access the internal storage of the supercluster ...
IsoValueMapProducer< pat::Photon > PhoIsoValueMapProducer
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void doMiniIso(edm::Event &) const
IsoValueMapProducer< pat::IsolatedTrack > IsoTrackIsoValueMapProducer
T min(T a, T b)
Definition: MathUtil.h:58
ParameterDescriptionBase * add(U const &iLabel, T const &value)
IsoValueMapProducer< pat::Electron > EleIsoValueMapProducer
std::unique_ptr< EffectiveAreas > ea_pfiso_chg_
std::unique_ptr< EffectiveAreas > ea_pfiso_pho_
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.
float getEtaForEA(const T *) const
void doPFIsoPho(edm::Event &) const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::EDGetTokenT< double > rho_miniiso_
std::unique_ptr< EffectiveAreas > ea_pfiso_
long double T
std::unique_ptr< EffectiveAreas > ea_miniiso_
Analysis-level muon class.
Definition: Muon.h:51
reco::SuperClusterRef superCluster() const override
override the reco::GsfElectron::superCluster method, to access the internal storage of the superclust...
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:30
def move(src, dest)
Definition: eostools.py:511