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