CMS 3D CMS Logo

MuIsolatorResultProducer.h
Go to the documentation of this file.
1 #ifndef MuonIsolationProducers_MuIsolatorResultProducer_H
2 #define MuonIsolationProducers_MuIsolatorResultProducer_H
3 
6 
8 
10 
13 
16 
17 #include <string>
18 
19 namespace edm {
20  class EventSetup;
21 }
22 
23 struct muisorhelper {
27  typedef std::vector<Result> Results;
29 
30  template <typename BT>
31  class CandMap {
32  public:
33  typedef typename edm::RefToBase<BT> key_type;
36  typedef std::pair<key_type, value_type> pair_type;
37  typedef typename std::vector<pair_type> map_type;
38  typedef typename map_type::iterator iterator;
39 
40  map_type& get() { return cMap_; }
41  const map_type& get() const { return cMap_; }
42  const handle_type& handle() const { return handle_; }
43  void setHandle(const handle_type& rhs) { handle_ = rhs; }
44 
45  private:
48  };
49 };
50 
52 template <typename BT = reco::Candidate>
54 public:
56 
57  ~MuIsolatorResultProducer() override;
58 
59  void produce(edm::Event&, const edm::EventSetup&) override;
60 
61 private:
67 
69 
70  struct DepositConf {
72  double weight;
73  double threshold;
74  };
75 
76  struct VetoCuts {
77  bool selectAll;
78  double muAbsEtaMax;
79  double muPtMin;
80  double muAbsZMax;
81  double muD0Max;
82  };
83 
84  void callWhatProduces();
85 
86  unsigned int initAssociation(edm::Event& event, CandMap& candMapT) const;
87 
88  void initVetos(std::vector<reco::IsoDeposit::Vetos*>& vetos, CandMap& candMap) const;
89 
90  template <typename RT>
91  void writeOutImpl(edm::Event& event, const CandMap& candMapT, const Results& results) const;
92 
93  void writeOut(edm::Event& event, const CandMap& candMap, const Results& results) const;
94 
96  std::vector<DepositConf> theDepositConfs;
97 
101 
105 
110 };
111 
113 template <typename BT>
114 template <typename RT>
116  const CandMap& candMapT,
117  const Results& results) const {
119  std::vector<RT> resV(results.size());
120  for (unsigned int i = 0; i < resV.size(); ++i)
121  resV[i] = results[i].val<RT>();
122  auto outMap = std::make_unique<edm::ValueMap<RT>>();
123  typename edm::ValueMap<RT>::Filler filler(*outMap);
124 
126  if (!candMapT.get().empty()) {
127  filler.insert(candMapT.handle(), resV.begin(), resV.end());
128  filler.fill();
129  }
130 
131  event.put(std::move(outMap));
132 }
133 
135 template <typename BT>
137  const CandMap& candMapT,
138  const Results& results) const {
139  std::string metname = "RecoMuon|MuonIsolationProducers";
140  LogDebug(metname) << "Before calling writeOutMap with result type " << theIsolator->resultType();
141 
142  if (theResultType == Isolator::ISOL_INT_TYPE)
143  writeOutImpl<int>(event, candMapT, results);
144  if (theResultType == Isolator::ISOL_FLOAT_TYPE)
145  writeOutImpl<float>(event, candMapT, results);
146  if (theResultType == Isolator::ISOL_BOOL_TYPE)
147  writeOutImpl<bool>(event, candMapT, results);
148 }
149 
151 template <typename BT>
153  if (theResultType == Isolator::ISOL_FLOAT_TYPE)
154  produces<edm::ValueMap<float>>();
155  if (theResultType == Isolator::ISOL_INT_TYPE)
156  produces<edm::ValueMap<int>>();
157  if (theResultType == Isolator::ISOL_BOOL_TYPE)
158  produces<edm::ValueMap<bool>>();
159 }
160 
161 // Framework
165 
167 
174 
177 
181 
182 #include <string>
183 
185 template <typename BT>
187  : theConfig(par),
188  theRemoveOtherVetos(par.getParameter<bool>("RemoveOtherVetos")),
189  theIsolator(nullptr),
190  theBeam(0, 0, 0) {
191  LogDebug("RecoMuon|MuonIsolation") << " MuIsolatorResultProducer CTOR";
192 
194  std::vector<edm::ParameterSet> depositInputs = par.getParameter<std::vector<edm::ParameterSet>>("InputMuIsoDeposits");
195 
196  std::vector<double> dWeights(depositInputs.size());
197  std::vector<double> dThresholds(depositInputs.size());
198 
199  for (unsigned int iDep = 0; iDep < depositInputs.size(); ++iDep) {
200  DepositConf dConf;
201  dConf.tag = depositInputs[iDep].getParameter<edm::InputTag>("DepositTag");
202  dConf.weight = depositInputs[iDep].getParameter<double>("DepositWeight");
203  dConf.threshold = depositInputs[iDep].getParameter<double>("DepositThreshold");
204 
205  dWeights[iDep] = dConf.weight;
206  dThresholds[iDep] = dConf.threshold;
207 
208  theDepositConfs.push_back(dConf);
209  }
210 
211  edm::ParameterSet isoPset = par.getParameter<edm::ParameterSet>("IsolatorPSet");
213  std::string isolatorType = isoPset.getParameter<std::string>("ComponentName");
214  if (isolatorType == "IsolatorByDeposit") {
215  std::string coneSizeType = isoPset.getParameter<std::string>("ConeSizeType");
216  if (coneSizeType == "FixedConeSize") {
217  float coneSize(isoPset.getParameter<double>("coneSize"));
218 
219  theIsolator = new muonisolation::IsolatorByDeposit(coneSize, dWeights, dThresholds);
220 
221  // theIsolator = new IsolatorByDeposit(isoPset);
222  } else if (coneSizeType == "CutsConeSize") {
224  // Cuts cuts(isoPset.getParameter<edm::ParameterSet>("CutsPSet"));
225 
226  // theIsolator = new IsolatorByDeposit(coneSize, dWeights, dThresholds);
227  }
228  } else if (isolatorType == "IsolatorByNominalEfficiency") {
230  theIsolator =
231  new muonisolation::IsolatorByNominalEfficiency("noname", std::vector<std::string>(1, "8:0.97"), dWeights);
232  } else if (isolatorType == "IsolatorByDepositCount") {
233  std::string coneSizeType = isoPset.getParameter<std::string>("ConeSizeType");
234  if (coneSizeType == "FixedConeSize") {
235  float coneSize(isoPset.getParameter<double>("coneSize"));
236 
238 
239  // theIsolator = new IsolatorByDeposit(isoPset);
240  } else if (coneSizeType == "CutsConeSize") {
241  // Cuts cuts(isoPset.getParameter<edm::ParameterSet>("CutsPSet"));
242 
243  // theIsolator = new IsolatorByDeposit(coneSize, dWeights, dThresholds);
244  }
245  }
246 
247  if (theIsolator == nullptr) {
248  edm::LogError("MuonIsolationProducers") << "Failed to initialize an isolator";
249  }
251 
253 
254  if (theRemoveOtherVetos) {
255  edm::ParameterSet vetoPSet = par.getParameter<edm::ParameterSet>("VetoPSet");
256  theVetoCuts.selectAll = vetoPSet.getParameter<bool>("SelectAll");
257 
261  if (!theVetoCuts.selectAll) {
262  theVetoCuts.muAbsEtaMax = vetoPSet.getParameter<double>("MuAbsEtaMax");
263  theVetoCuts.muPtMin = vetoPSet.getParameter<double>("MuPtMin");
264  theVetoCuts.muAbsZMax = vetoPSet.getParameter<double>("MuAbsZMax");
265  theVetoCuts.muD0Max = vetoPSet.getParameter<double>("MuD0Max");
266  theBeamlineOption = par.getParameter<std::string>("BeamlineOption");
267  theBeamSpotLabel = par.getParameter<edm::InputTag>("BeamSpotLabel");
268  }
269  }
270 }
271 
273 template <typename BT>
275  if (theIsolator)
276  delete theIsolator;
277  LogDebug("RecoMuon|MuIsolatorResultProducer") << " MuIsolatorResultProducer DTOR";
278 }
279 
280 template <typename BT>
282  std::string metname = "RecoMuon|MuonIsolationProducers";
283  LogDebug(metname) << " Muon Deposit producing..."
284  << " BEGINING OF EVENT "
285  << "================================";
286 
287  theBeam = reco::TrackBase::Point(0, 0, 0);
288 
290  if (theRemoveOtherVetos && !theVetoCuts.selectAll) {
291  if (theBeamlineOption == "BeamSpotFromEvent") {
292  //pick beamSpot
294  edm::Handle<reco::BeamSpot> beamSpotH;
295 
296  event.getByLabel(theBeamSpotLabel, beamSpotH);
297 
298  if (beamSpotH.isValid()) {
299  theBeam = beamSpotH->position();
300  LogTrace(metname) << "Extracted beam point at " << theBeam << std::endl;
301  }
302  }
303  }
304 
310  CandMap candMapT;
311 
312  unsigned int colSize = initAssociation(event, candMapT);
313 
315  Results results(colSize);
316 
318  std::vector<reco::IsoDeposit::Vetos*> vetoDeps(theDepositConfs.size(), nullptr);
319 
320  if (colSize != 0) {
321  if (theRemoveOtherVetos) {
322  initVetos(vetoDeps, candMapT);
323  }
324 
326  for (unsigned int muI = 0; muI < colSize; ++muI) {
327  results[muI] = theIsolator->result(candMapT.get()[muI].second, *(candMapT.get()[muI].first));
328 
329  if (results[muI].typeF() != theIsolator->resultType()) {
330  edm::LogError("MuonIsolationProducers") << "Failed to get result from the isolator";
331  }
332  }
333  }
334 
335  LogDebug(metname) << "Ready to write out results of size " << results.size();
336  writeOut(event, candMapT, results);
337 
338  for (unsigned int iDep = 0; iDep < vetoDeps.size(); ++iDep) {
340  if (vetoDeps[iDep]) {
341  delete vetoDeps[iDep];
342  vetoDeps[iDep] = nullptr;
343  }
344  }
345 }
346 
347 template <typename BT>
349  std::string metname = "RecoMuon|MuonIsolationProducers";
350 
352 
353  for (unsigned int iMap = 0; iMap < theDepositConfs.size(); ++iMap) {
355  event.getByLabel(theDepositConfs[iMap].tag, depH);
356  LogDebug(metname) << "Got Deposits of size " << depH->size();
357  if (depH->empty())
358  continue;
359 
362  typename CandMap::handle_type keyH;
363  event.get(depH->begin().id(), keyH);
364  candMapT.setHandle(keyH);
365  typename CT::const_iterator depHCI = depH->begin().begin();
366  typename CT::const_iterator depEnd = depH->begin().end();
367  unsigned int keyI = 0;
368  for (; depHCI != depEnd; ++depHCI, ++keyI) {
369  typename CandMap::key_type muPtr(keyH->refAt(keyI));
371  if (iMap == 0)
372  candMapT.get().push_back(typename CandMap::pair_type(muPtr, DepositContainer(theDepositConfs.size())));
373  typename CandMap::iterator muI = candMapT.get().begin();
374  for (; muI != candMapT.get().end(); ++muI) {
375  if (muI->first == muPtr)
376  break;
377  }
378  if (muI->first != muPtr) {
379  edm::LogError("MuonIsolationProducers") << "Failed to align muon map";
380  }
381  muI->second[iMap].dep = &*depHCI;
382  }
383  }
384 
385  LogDebug(metname) << "Picked and aligned nDeps = " << candMapT.get().size();
386  return candMapT.get().size();
387 }
388 
389 template <typename BT>
390 void MuIsolatorResultProducer<BT>::initVetos(std::vector<reco::IsoDeposit::Vetos*>& vetos, CandMap& candMapT) const {
391  std::string metname = "RecoMuon|MuonIsolationProducers";
392 
393  if (theRemoveOtherVetos) {
394  LogDebug(metname) << "Start checking for vetos based on input/expected vetos.size of " << vetos.size()
395  << " passed at " << &vetos << " and an input map.size of " << candMapT.get().size();
396 
397  unsigned int muI = 0;
398  for (; muI < candMapT.get().size(); ++muI) {
399  typename CandMap::key_type mu = candMapT.get()[muI].first;
400  double d0 = ((mu->vx() - theBeam.x()) * mu->py() - (mu->vy() - theBeam.y()) * mu->px()) / mu->pt();
401  LogDebug(metname) << "Muon at index " << muI;
402  if (theVetoCuts.selectAll || (fabs(mu->eta()) < theVetoCuts.muAbsEtaMax && mu->pt() > theVetoCuts.muPtMin &&
403  fabs(mu->vz()) < theVetoCuts.muAbsZMax && fabs(d0) < theVetoCuts.muD0Max)) {
404  LogDebug(metname) << "muon passes the cuts";
405  for (unsigned int iDep = 0; iDep < candMapT.get()[muI].second.size(); ++iDep) {
406  if (vetos[iDep] == nullptr)
407  vetos[iDep] = new reco::IsoDeposit::Vetos();
408 
409  vetos[iDep]->push_back(candMapT.get()[muI].second[iDep].dep->veto());
410  }
411  }
412  }
413 
414  LogDebug(metname) << "Assigning vetos";
415  muI = 0;
416  for (; muI < candMapT.get().size(); ++muI) {
417  for (unsigned int iDep = 0; iDep < candMapT.get()[muI].second.size(); ++iDep) {
418  candMapT.get()[muI].second[iDep].vetos = vetos[iDep];
419  }
420  }
421  LogDebug(metname) << "Done with vetos";
422  }
423 }
424 
425 #endif
muisorhelper::ResultType ResultType
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::pair< key_type, value_type > pair_type
const Point & position() const
position
Definition: BeamSpot.h:59
MuIsolatorResultProducer(const edm::ParameterSet &)
constructor with config
std::vector< Result > Results
const std::string metname
std::vector< DepositConf > theDepositConfs
bool theRemoveOtherVetos
choose which muon vetos should be removed from all deposits
void callWhatProduces()
declare what&#39;s going to be produced
Log< level::Error, false > LogError
#define LogTrace(id)
std::vector< pair_type > map_type
muonisolation::MuIsoBaseIsolator Isolator
edm::RefToBase< BT > key_type
Isolator * theIsolator
the isolator
std::string theBeamlineOption
beam spot
Isolator::Result Result
void produce(edm::Event &, const edm::EventSetup &) override
void setHandle(const handle_type &rhs)
edm::Handle< edm::View< BT > > handle_type
ProductID id() const
Definition: ValueMap.h:173
math::XYZPoint Point
point in the space
Definition: TrackBase.h:80
muisorhelper::Isolator Isolator
virtual ResultType resultType() const =0
container::const_iterator end() const
Definition: ValueMap.h:175
const_iterator begin() const
Definition: ValueMap.h:229
container::const_iterator begin() const
Definition: ValueMap.h:174
muisorhelper::DepositContainer DepositContainer
static constexpr float d0
void writeOut(edm::Event &event, const CandMap &candMap, const Results &results) const
choose which result type to write here
std::vector< DepositAndVetos > DepositContainer
Isolator::ResultType ResultType
unsigned int initAssociation(edm::Event &event, CandMap &candMapT) const
Isolator::DepositContainer DepositContainer
bool isValid() const
Definition: HandleBase.h:70
void initVetos(std::vector< reco::IsoDeposit::Vetos *> &vetos, CandMap &candMap) const
std::vector< Veto > Vetos
Definition: IsoDeposit.h:65
const handle_type & handle() const
size_t size() const
Definition: ValueMap.h:156
HLT enums.
std::vector< value_type > container
Definition: ValueMap.h:110
void writeOutImpl(edm::Event &event, const CandMap &candMapT, const Results &results) const
actually do the writing here
~MuIsolatorResultProducer() override
destructor
bool empty() const
Definition: ValueMap.h:158
reco::TrackBase::Point theBeam
def move(src, dest)
Definition: eostools.py:511
muisorhelper::CandMap< BT > CandMap
Definition: event.py:1
#define LogDebug(id)