CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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  ea_pfiso_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso")).fullPath()));
61  rho_pfiso_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho_PFIso"));
62  }
63  else if ((typeid(T) == typeid(pat::Photon))) {
64  produces<edm::ValueMap<float>>("PFIsoChg");
65  produces<edm::ValueMap<float>>("PFIsoAll");
66  mapIsoChg_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("mapIsoChg"));
67  mapIsoNeu_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("mapIsoNeu"));
68  mapIsoPho_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("mapIsoPho"));
69  ea_pfiso_chg_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso_Chg")).fullPath()));
70  ea_pfiso_neu_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso_Neu")).fullPath()));
71  ea_pfiso_pho_.reset(new EffectiveAreas((iConfig.getParameter<edm::FileInPath>("EAFile_PFIso_Pho")).fullPath()));
72  rho_pfiso_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho_PFIso"));
73  }
74  }
75  ~IsoValueMapProducer() override {}
76 
77  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
78 
79  private:
80 
81  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
82 
83  // ----------member data ---------------------------
84 
86  bool relative_;
92  std::unique_ptr<EffectiveAreas> ea_miniiso_;
93  std::unique_ptr<EffectiveAreas> ea_pfiso_;
94  std::unique_ptr<EffectiveAreas> ea_pfiso_chg_;
95  std::unique_ptr<EffectiveAreas> ea_pfiso_neu_;
96  std::unique_ptr<EffectiveAreas> ea_pfiso_pho_;
97  float getEtaForEA(const T*) const;
98  void doMiniIso(edm::Event&) const;
99  void doPFIsoEle(edm::Event&) const;
100  void doPFIsoPho(edm::Event&) const;
101 
102 };
103 
104 //
105 // constants, enums and typedefs
106 //
107 
108 
109 //
110 // static data member definitions
111 //
112 
113 template<typename T> float IsoValueMapProducer<T>::getEtaForEA(const T *obj) const{
114  return obj->eta();
115 }
117  return el->superCluster()->eta();
118 }
120  return ph->superCluster()->eta();
121 }
122 
123 template <typename T>
124 void
126 {
127 
128  if ((typeid(T) == typeid(pat::Muon)) || (typeid(T) == typeid(pat::Electron)) || typeid(T) == typeid(pat::IsolatedTrack)) { doMiniIso(iEvent); };
129  if ((typeid(T) == typeid(pat::Electron))) { doPFIsoEle(iEvent); }
130  if ((typeid(T) == typeid(pat::Photon))) { doPFIsoPho(iEvent); }
131 
132 }
133 
134 template<typename T>
135 void
137 
139  iEvent.getByToken(src_, src);
141  iEvent.getByToken(rho_miniiso_,rho);
142 
143  unsigned int nInput = src->size();
144 
145  std::vector<float> miniIsoChg, miniIsoAll;
146  miniIsoChg.reserve(nInput);
147  miniIsoAll.reserve(nInput);
148 
149  for (const auto & obj : *src) {
150  auto iso = obj.miniPFIsolation();
151  auto chg = iso.chargedHadronIso();
152  auto neu = iso.neutralHadronIso();
153  auto pho = iso.photonIso();
154  auto ea = ea_miniiso_->getEffectiveArea(fabs(getEtaForEA(&obj)));
155  float R = 10.0/std::min(std::max(obj.pt(), 50.0),200.0);
156  ea *= std::pow(R/0.3,2);
157  float scale = relative_ ? 1.0/obj.pt() : 1;
158  miniIsoChg.push_back(scale*chg);
159  miniIsoAll.push_back(scale*(chg+std::max(0.0,neu+pho-(*rho)*ea)));
160  }
161 
162  std::unique_ptr<edm::ValueMap<float>> miniIsoChgV(new edm::ValueMap<float>());
163  edm::ValueMap<float>::Filler fillerChg(*miniIsoChgV);
164  fillerChg.insert(src,miniIsoChg.begin(),miniIsoChg.end());
165  fillerChg.fill();
166  std::unique_ptr<edm::ValueMap<float>> miniIsoAllV(new edm::ValueMap<float>());
167  edm::ValueMap<float>::Filler fillerAll(*miniIsoAllV);
168  fillerAll.insert(src,miniIsoAll.begin(),miniIsoAll.end());
169  fillerAll.fill();
170 
171  iEvent.put(std::move(miniIsoChgV),"miniIsoChg");
172  iEvent.put(std::move(miniIsoAllV),"miniIsoAll");
173 }
174 
175 template<>
176 void
178 
179 
180 template<typename T>
181 void
183 
184 template<>
185 void
187 
189  iEvent.getByToken(src_, src);
191  iEvent.getByToken(rho_pfiso_,rho);
192 
193  unsigned int nInput = src->size();
194 
195  std::vector<float> PFIsoChg, PFIsoAll;
196  PFIsoChg.reserve(nInput);
197  PFIsoAll.reserve(nInput);
198 
199  for (const auto & obj : *src) {
200  auto iso = obj.pfIsolationVariables();
201  auto chg = iso.sumChargedHadronPt;
202  auto neu = iso.sumNeutralHadronEt;
203  auto pho = iso.sumPhotonEt;
204  auto ea = ea_pfiso_->getEffectiveArea(fabs(getEtaForEA(&obj)));
205  float scale = relative_ ? 1.0/obj.pt() : 1;
206  PFIsoChg.push_back(scale*chg);
207  PFIsoAll.push_back(scale*(chg+std::max(0.0,neu+pho-(*rho)*ea)));
208  }
209 
210  std::unique_ptr<edm::ValueMap<float>> PFIsoChgV(new edm::ValueMap<float>());
211  edm::ValueMap<float>::Filler fillerChg(*PFIsoChgV);
212  fillerChg.insert(src,PFIsoChg.begin(),PFIsoChg.end());
213  fillerChg.fill();
214  std::unique_ptr<edm::ValueMap<float>> PFIsoAllV(new edm::ValueMap<float>());
215  edm::ValueMap<float>::Filler fillerAll(*PFIsoAllV);
216  fillerAll.insert(src,PFIsoAll.begin(),PFIsoAll.end());
217  fillerAll.fill();
218 
219  iEvent.put(std::move(PFIsoChgV),"PFIsoChg");
220  iEvent.put(std::move(PFIsoAllV),"PFIsoAll");
221 
222 }
223 
224 template<typename T>
225 void
227 
228 template<>
229 void
231 
233  iEvent.getByToken(src_, src);
235  iEvent.getByToken(rho_pfiso_,rho);
237  iEvent.getByToken(mapIsoChg_, mapIsoChg);
239  iEvent.getByToken(mapIsoNeu_, mapIsoNeu);
241  iEvent.getByToken(mapIsoPho_, mapIsoPho);
242 
243  unsigned int nInput = src->size();
244 
245  std::vector<float> PFIsoChg, PFIsoAll;
246  PFIsoChg.reserve(nInput);
247  PFIsoAll.reserve(nInput);
248 
249  for (unsigned int i=0; i<nInput; i++){
250  auto obj = src->ptrAt(i);
251  auto chg = (*mapIsoChg)[obj];
252  auto neu = (*mapIsoNeu)[obj];
253  auto pho = (*mapIsoPho)[obj];
254  auto ea_chg = ea_pfiso_chg_->getEffectiveArea(fabs(getEtaForEA(obj.get())));
255  auto ea_neu = ea_pfiso_neu_->getEffectiveArea(fabs(getEtaForEA(obj.get())));
256  auto ea_pho = ea_pfiso_pho_->getEffectiveArea(fabs(getEtaForEA(obj.get())));
257  float scale = relative_ ? 1.0/obj->pt() : 1;
258  PFIsoChg.push_back(scale*std::max(0.0,chg-(*rho)*ea_chg));
259  PFIsoAll.push_back(PFIsoChg.back()+scale*(std::max(0.0,neu-(*rho)*ea_neu)+std::max(0.0,pho-(*rho)*ea_pho)));
260  }
261 
262  std::unique_ptr<edm::ValueMap<float>> PFIsoChgV(new edm::ValueMap<float>());
263  edm::ValueMap<float>::Filler fillerChg(*PFIsoChgV);
264  fillerChg.insert(src,PFIsoChg.begin(),PFIsoChg.end());
265  fillerChg.fill();
266  std::unique_ptr<edm::ValueMap<float>> PFIsoAllV(new edm::ValueMap<float>());
267  edm::ValueMap<float>::Filler fillerAll(*PFIsoAllV);
268  fillerAll.insert(src,PFIsoAll.begin(),PFIsoAll.end());
269  fillerAll.fill();
270 
271  iEvent.put(std::move(PFIsoChgV),"PFIsoChg");
272  iEvent.put(std::move(PFIsoAllV),"PFIsoAll");
273 
274 }
275 
276 
277 
278 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
279 template <typename T>
280 void
283  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
284  desc.add<bool>("relative")->setComment("compute relative isolation instead of absolute one");
285  if ((typeid(T) == typeid(pat::Muon)) || (typeid(T) == typeid(pat::Electron)) || typeid(T) == typeid(pat::IsolatedTrack)) {
286  desc.add<edm::FileInPath>("EAFile_MiniIso")->setComment("txt file containing effective areas to be used for mini-isolation pileup subtraction");
287  desc.add<edm::InputTag>("rho_MiniIso")->setComment("rho to be used for effective-area based mini-isolation pileup subtraction");
288  }
289  if ((typeid(T) == typeid(pat::Electron))) {
290  desc.add<edm::FileInPath>("EAFile_PFIso")->setComment("txt file containing effective areas to be used for PF-isolation pileup subtraction for electrons");
291  desc.add<edm::InputTag>("rho_PFIso")->setComment("rho to be used for effective-area based PF-isolation pileup subtraction for electrons");
292  }
293  if ((typeid(T) == typeid(pat::Photon))) {
294  desc.add<edm::InputTag>("mapIsoChg")->setComment("input charged PF isolation calculated in VID for photons");
295  desc.add<edm::InputTag>("mapIsoNeu")->setComment("input neutral PF isolation calculated in VID for photons");
296  desc.add<edm::InputTag>("mapIsoPho")->setComment("input photon PF isolation calculated in VID for photons");
297  desc.add<edm::FileInPath>("EAFile_PFIso_Chg")->setComment("txt file containing effective areas to be used for charged PF-isolation pileup subtraction for photons");
298  desc.add<edm::FileInPath>("EAFile_PFIso_Neu")->setComment("txt file containing effective areas to be used for neutral PF-isolation pileup subtraction for photons");
299  desc.add<edm::FileInPath>("EAFile_PFIso_Pho")->setComment("txt file containing effective areas to be used for photon PF-isolation pileup subtraction for photons");
300  desc.add<edm::InputTag>("rho_PFIso")->setComment("rho to be used for effective-area based PF-isolation pileup subtraction for photons");
301  }
302  std::string modname;
303  if (typeid(T) == typeid(pat::Muon)) modname+="Muon";
304  else if (typeid(T) == typeid(pat::Electron)) modname+="Ele";
305  else if (typeid(T) == typeid(pat::Photon)) modname+="Pho";
306  else if (typeid(T) == typeid(pat::IsolatedTrack)) modname+="IsoTrack";
307  modname+="IsoValueMapProducer";
308  descriptions.add(modname,desc);
309 }
310 
311 
316 
317 //define this as a plug-in
322 
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:127
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:508
#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