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 
20 // system include files
21 #include <memory>
22 
23 // user include files
26 
29 
32 
34 
39 
40 //
41 // class declaration
42 //
43 
44 template <typename T>
46  public:
47  explicit IsoValueMapProducer(const edm::ParameterSet &iConfig):
48  src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
49  relative_(iConfig.getParameter<bool>("relative"))
50  {
51  if ((typeid(T) == typeid(pat::Muon)) || (typeid(T) == typeid(pat::Electron)) || typeid(T) == typeid(pat::IsolatedTrack)) {
52  produces<edm::ValueMap<float>>("miniIsoChg");
53  produces<edm::ValueMap<float>>("miniIsoAll");
54  ea_miniiso_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_MiniIso")).fullPath()));
55  rho_miniiso_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho_MiniIso"));
56  }
57  if ((typeid(T) == typeid(pat::Electron))) {
58  produces<edm::ValueMap<float>>("PFIsoChg");
59  produces<edm::ValueMap<float>>("PFIsoAll");
60  produces<edm::ValueMap<float>>("PFIsoAll04");
61  ea_pfiso_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso")).fullPath()));
62  rho_pfiso_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho_PFIso"));
63  }
64  else if ((typeid(T) == typeid(pat::Photon))) {
65  produces<edm::ValueMap<float>>("PFIsoChg");
66  produces<edm::ValueMap<float>>("PFIsoAll");
67  mapIsoChg_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("mapIsoChg"));
68  mapIsoNeu_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("mapIsoNeu"));
69  mapIsoPho_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("mapIsoPho"));
70  ea_pfiso_chg_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso_Chg")).fullPath()));
71  ea_pfiso_neu_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso_Neu")).fullPath()));
72  ea_pfiso_pho_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso_Pho")).fullPath()));
73  rho_pfiso_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho_PFIso"));
74  }
75  }
76  ~IsoValueMapProducer() override {}
77 
78  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
79 
80  private:
81 
82  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
83 
84  // ----------member data ---------------------------
85 
87  bool relative_;
93  std::unique_ptr<EffectiveAreas> ea_miniiso_;
94  std::unique_ptr<EffectiveAreas> ea_pfiso_;
95  std::unique_ptr<EffectiveAreas> ea_pfiso_chg_;
96  std::unique_ptr<EffectiveAreas> ea_pfiso_neu_;
97  std::unique_ptr<EffectiveAreas> ea_pfiso_pho_;
98  float getEtaForEA(const T*) const;
99  void doMiniIso(edm::Event&) const;
100  void doPFIsoEle(edm::Event&) const;
101  void doPFIsoPho(edm::Event&) const;
102 
103 };
104 
105 //
106 // constants, enums and typedefs
107 //
108 
109 
110 //
111 // static data member definitions
112 //
113 
114 template<typename T> float IsoValueMapProducer<T>::getEtaForEA(const T *obj) const{
115  return obj->eta();
116 }
118  return el->superCluster()->eta();
119 }
121  return ph->superCluster()->eta();
122 }
123 
124 template <typename T>
125 void
127 {
128 
129  if ((typeid(T) == typeid(pat::Muon)) || (typeid(T) == typeid(pat::Electron)) || typeid(T) == typeid(pat::IsolatedTrack)) { doMiniIso(iEvent); };
130  if ((typeid(T) == typeid(pat::Electron))) { doPFIsoEle(iEvent); }
131  if ((typeid(T) == typeid(pat::Photon))) { doPFIsoPho(iEvent); }
132 
133 }
134 
135 template<typename T>
136 void
138 
140  iEvent.getByToken(src_, src);
142  iEvent.getByToken(rho_miniiso_,rho);
143 
144  unsigned int nInput = src->size();
145 
146  std::vector<float> miniIsoChg, miniIsoAll;
147  miniIsoChg.reserve(nInput);
148  miniIsoAll.reserve(nInput);
149 
150  for (const auto & obj : *src) {
151  auto iso = obj.miniPFIsolation();
152  auto chg = iso.chargedHadronIso();
153  auto neu = iso.neutralHadronIso();
154  auto pho = iso.photonIso();
155  auto ea = ea_miniiso_->getEffectiveArea(fabs(getEtaForEA(&obj)));
156  float R = 10.0/std::min(std::max(obj.pt(), 50.0),200.0);
157  ea *= std::pow(R/0.3,2);
158  float scale = relative_ ? 1.0/obj.pt() : 1;
159  miniIsoChg.push_back(scale*chg);
160  miniIsoAll.push_back(scale*(chg+std::max(0.0,neu+pho-(*rho)*ea)));
161  }
162 
163  std::unique_ptr<edm::ValueMap<float>> miniIsoChgV(new edm::ValueMap<float>());
164  edm::ValueMap<float>::Filler fillerChg(*miniIsoChgV);
165  fillerChg.insert(src,miniIsoChg.begin(),miniIsoChg.end());
166  fillerChg.fill();
167  std::unique_ptr<edm::ValueMap<float>> miniIsoAllV(new edm::ValueMap<float>());
168  edm::ValueMap<float>::Filler fillerAll(*miniIsoAllV);
169  fillerAll.insert(src,miniIsoAll.begin(),miniIsoAll.end());
170  fillerAll.fill();
171 
172  iEvent.put(std::move(miniIsoChgV),"miniIsoChg");
173  iEvent.put(std::move(miniIsoAllV),"miniIsoAll");
174 }
175 
176 template<>
177 void
179 
180 
181 template<typename T>
182 void
184 
185 template<>
186 void
188 
190  iEvent.getByToken(src_, src);
192  iEvent.getByToken(rho_pfiso_,rho);
193 
194  unsigned int nInput = src->size();
195 
196  std::vector<float> PFIsoChg, PFIsoAll, PFIsoAll04;
197  PFIsoChg.reserve(nInput);
198  PFIsoAll.reserve(nInput);
199  PFIsoAll04.reserve(nInput);
200 
201  for (const auto & obj : *src) {
202  auto iso = obj.pfIsolationVariables();
203  auto chg = iso.sumChargedHadronPt;
204  auto neu = iso.sumNeutralHadronEt;
205  auto pho = iso.sumPhotonEt;
206  auto ea = ea_pfiso_->getEffectiveArea(fabs(getEtaForEA(&obj)));
207  float scale = relative_ ? 1.0/obj.pt() : 1;
208  PFIsoChg.push_back(scale*chg);
209  PFIsoAll.push_back(scale*(chg+std::max(0.0,neu+pho-(*rho)*ea)));
210  PFIsoAll04.push_back(scale*(obj.chargedHadronIso()+std::max(0.0,obj.neutralHadronIso()+obj.photonIso()-(*rho)*ea*16./9.)));
211  }
212 
213  std::unique_ptr<edm::ValueMap<float>> PFIsoChgV(new edm::ValueMap<float>());
214  edm::ValueMap<float>::Filler fillerChg(*PFIsoChgV);
215  fillerChg.insert(src,PFIsoChg.begin(),PFIsoChg.end());
216  fillerChg.fill();
217  std::unique_ptr<edm::ValueMap<float>> PFIsoAllV(new edm::ValueMap<float>());
218  edm::ValueMap<float>::Filler fillerAll(*PFIsoAllV);
219  fillerAll.insert(src,PFIsoAll.begin(),PFIsoAll.end());
220  fillerAll.fill();
221  std::unique_ptr<edm::ValueMap<float>> PFIsoAll04V(new edm::ValueMap<float>());
222  edm::ValueMap<float>::Filler fillerAll04(*PFIsoAll04V);
223  fillerAll04.insert(src,PFIsoAll04.begin(),PFIsoAll04.end());
224  fillerAll04.fill();
225 
226  iEvent.put(std::move(PFIsoChgV),"PFIsoChg");
227  iEvent.put(std::move(PFIsoAllV),"PFIsoAll");
228  iEvent.put(std::move(PFIsoAll04V),"PFIsoAll04");
229 
230 }
231 
232 template<typename T>
233 void
235 
236 template<>
237 void
239 
241  iEvent.getByToken(src_, src);
243  iEvent.getByToken(rho_pfiso_,rho);
245  iEvent.getByToken(mapIsoChg_, mapIsoChg);
247  iEvent.getByToken(mapIsoNeu_, mapIsoNeu);
249  iEvent.getByToken(mapIsoPho_, mapIsoPho);
250 
251  unsigned int nInput = src->size();
252 
253  std::vector<float> PFIsoChg, PFIsoAll;
254  PFIsoChg.reserve(nInput);
255  PFIsoAll.reserve(nInput);
256 
257  for (unsigned int i=0; i<nInput; i++){
258  auto obj = src->ptrAt(i);
259  auto chg = (*mapIsoChg)[obj];
260  auto neu = (*mapIsoNeu)[obj];
261  auto pho = (*mapIsoPho)[obj];
262  auto ea_chg = ea_pfiso_chg_->getEffectiveArea(fabs(getEtaForEA(obj.get())));
263  auto ea_neu = ea_pfiso_neu_->getEffectiveArea(fabs(getEtaForEA(obj.get())));
264  auto ea_pho = ea_pfiso_pho_->getEffectiveArea(fabs(getEtaForEA(obj.get())));
265  float scale = relative_ ? 1.0/obj->pt() : 1;
266  PFIsoChg.push_back(scale*std::max(0.0,chg-(*rho)*ea_chg));
267  PFIsoAll.push_back(PFIsoChg.back()+scale*(std::max(0.0,neu-(*rho)*ea_neu)+std::max(0.0,pho-(*rho)*ea_pho)));
268  }
269 
270  std::unique_ptr<edm::ValueMap<float>> PFIsoChgV(new edm::ValueMap<float>());
271  edm::ValueMap<float>::Filler fillerChg(*PFIsoChgV);
272  fillerChg.insert(src,PFIsoChg.begin(),PFIsoChg.end());
273  fillerChg.fill();
274  std::unique_ptr<edm::ValueMap<float>> PFIsoAllV(new edm::ValueMap<float>());
275  edm::ValueMap<float>::Filler fillerAll(*PFIsoAllV);
276  fillerAll.insert(src,PFIsoAll.begin(),PFIsoAll.end());
277  fillerAll.fill();
278 
279  iEvent.put(std::move(PFIsoChgV),"PFIsoChg");
280  iEvent.put(std::move(PFIsoAllV),"PFIsoAll");
281 
282 }
283 
284 
285 
286 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
287 template <typename T>
288 void
291  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
292  desc.add<bool>("relative")->setComment("compute relative isolation instead of absolute one");
293  if ((typeid(T) == typeid(pat::Muon)) || (typeid(T) == typeid(pat::Electron)) || typeid(T) == typeid(pat::IsolatedTrack)) {
294  desc.add<edm::FileInPath>("EAFile_MiniIso")->setComment("txt file containing effective areas to be used for mini-isolation pileup subtraction");
295  desc.add<edm::InputTag>("rho_MiniIso")->setComment("rho to be used for effective-area based mini-isolation pileup subtraction");
296  }
297  if ((typeid(T) == typeid(pat::Electron))) {
298  desc.add<edm::FileInPath>("EAFile_PFIso")->setComment("txt file containing effective areas to be used for PF-isolation pileup subtraction for electrons");
299  desc.add<edm::InputTag>("rho_PFIso")->setComment("rho to be used for effective-area based PF-isolation pileup subtraction for electrons");
300  }
301  if ((typeid(T) == typeid(pat::Photon))) {
302  desc.add<edm::InputTag>("mapIsoChg")->setComment("input charged PF isolation calculated in VID for photons");
303  desc.add<edm::InputTag>("mapIsoNeu")->setComment("input neutral PF isolation calculated in VID for photons");
304  desc.add<edm::InputTag>("mapIsoPho")->setComment("input photon PF isolation calculated in VID for photons");
305  desc.add<edm::FileInPath>("EAFile_PFIso_Chg")->setComment("txt file containing effective areas to be used for charged PF-isolation pileup subtraction for photons");
306  desc.add<edm::FileInPath>("EAFile_PFIso_Neu")->setComment("txt file containing effective areas to be used for neutral PF-isolation pileup subtraction for photons");
307  desc.add<edm::FileInPath>("EAFile_PFIso_Pho")->setComment("txt file containing effective areas to be used for photon PF-isolation pileup subtraction for photons");
308  desc.add<edm::InputTag>("rho_PFIso")->setComment("rho to be used for effective-area based PF-isolation pileup subtraction for photons");
309  }
310  std::string modname;
311  if (typeid(T) == typeid(pat::Muon)) modname+="Muon";
312  else if (typeid(T) == typeid(pat::Electron)) modname+="Ele";
313  else if (typeid(T) == typeid(pat::Photon)) modname+="Pho";
314  else if (typeid(T) == typeid(pat::IsolatedTrack)) modname+="IsoTrack";
315  modname+="IsoValueMapProducer";
316  descriptions.add(modname,desc);
317 }
318 
319 
324 
325 //define this as a plug-in
330 
T getParameter(std::string const &) const
Analysis-level Photon class.
Definition: Photon.h:47
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
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:519
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
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:230
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::ValueMap< float > > mapIsoChg_
edm::EDGetTokenT< edm::View< T > > src_
edm::EDGetTokenT< edm::ValueMap< float > > mapIsoNeu_
edm::EDGetTokenT< edm::ValueMap< float > > mapIsoPho_
std::unique_ptr< EffectiveAreas > ea_pfiso_neu_
Analysis-level electron class.
Definition: Electron.h:52
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:50
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:40
def move(src, dest)
Definition: eostools.py:510