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(reco::TrackBase::Point const& theBeam,
89  std::vector<reco::IsoDeposit::Vetos*>& vetos,
90  CandMap& candMap) const;
91 
92  template <typename RT>
93  void writeOutImpl(edm::Event& event, const CandMap& candMapT, const Results& results) const;
94 
95  void writeOut(edm::Event& event, const CandMap& candMap, const Results& results) const;
96 
98  std::vector<DepositConf> theDepositConfs;
99 
103 
107 
111 };
112 
114 template <typename BT>
115 template <typename RT>
117  const CandMap& candMapT,
118  const Results& results) const {
120  std::vector<RT> resV(results.size());
121  for (unsigned int i = 0; i < resV.size(); ++i)
122  resV[i] = results[i].val<RT>();
123  auto outMap = std::make_unique<edm::ValueMap<RT>>();
124  typename edm::ValueMap<RT>::Filler filler(*outMap);
125 
127  if (!candMapT.get().empty()) {
128  filler.insert(candMapT.handle(), resV.begin(), resV.end());
129  filler.fill();
130  }
131 
132  event.put(std::move(outMap));
133 }
134 
136 template <typename BT>
138  const CandMap& candMapT,
139  const Results& results) const {
140  std::string metname = "RecoMuon|MuonIsolationProducers";
141  LogDebug(metname) << "Before calling writeOutMap with result type " << theIsolator->resultType();
142 
143  if (theResultType == Isolator::ISOL_INT_TYPE)
144  writeOutImpl<int>(event, candMapT, results);
145  if (theResultType == Isolator::ISOL_FLOAT_TYPE)
146  writeOutImpl<float>(event, candMapT, results);
147  if (theResultType == Isolator::ISOL_BOOL_TYPE)
148  writeOutImpl<bool>(event, candMapT, results);
149 }
150 
152 template <typename BT>
154  if (theResultType == Isolator::ISOL_FLOAT_TYPE)
155  produces<edm::ValueMap<float>>();
156  if (theResultType == Isolator::ISOL_INT_TYPE)
157  produces<edm::ValueMap<int>>();
158  if (theResultType == Isolator::ISOL_BOOL_TYPE)
159  produces<edm::ValueMap<bool>>();
160 }
161 
162 // Framework
165 
167 
174 
177 
181 
182 #include <string>
183 
185 template <typename BT>
187  : theConfig(par), theRemoveOtherVetos(par.getParameter<bool>("RemoveOtherVetos")), theIsolator(nullptr) {
188  LogDebug("RecoMuon|MuonIsolation") << " MuIsolatorResultProducer CTOR";
189 
191  std::vector<edm::ParameterSet> depositInputs = par.getParameter<std::vector<edm::ParameterSet>>("InputMuIsoDeposits");
192 
193  std::vector<double> dWeights(depositInputs.size());
194  std::vector<double> dThresholds(depositInputs.size());
195 
196  for (unsigned int iDep = 0; iDep < depositInputs.size(); ++iDep) {
197  DepositConf dConf;
198  dConf.tag = depositInputs[iDep].getParameter<edm::InputTag>("DepositTag");
199  dConf.weight = depositInputs[iDep].getParameter<double>("DepositWeight");
200  dConf.threshold = depositInputs[iDep].getParameter<double>("DepositThreshold");
201 
202  dWeights[iDep] = dConf.weight;
203  dThresholds[iDep] = dConf.threshold;
204 
205  theDepositConfs.push_back(dConf);
206  }
207 
208  edm::ParameterSet isoPset = par.getParameter<edm::ParameterSet>("IsolatorPSet");
210  std::string isolatorType = isoPset.getParameter<std::string>("ComponentName");
211  if (isolatorType == "IsolatorByDeposit") {
212  std::string coneSizeType = isoPset.getParameter<std::string>("ConeSizeType");
213  if (coneSizeType == "FixedConeSize") {
214  float coneSize(isoPset.getParameter<double>("coneSize"));
215 
216  theIsolator = new muonisolation::IsolatorByDeposit(coneSize, dWeights, dThresholds);
217 
218  // theIsolator = new IsolatorByDeposit(isoPset);
219  } else if (coneSizeType == "CutsConeSize") {
221  // Cuts cuts(isoPset.getParameter<edm::ParameterSet>("CutsPSet"));
222 
223  // theIsolator = new IsolatorByDeposit(coneSize, dWeights, dThresholds);
224  }
225  } else if (isolatorType == "IsolatorByNominalEfficiency") {
227  theIsolator =
228  new muonisolation::IsolatorByNominalEfficiency("noname", std::vector<std::string>(1, "8:0.97"), dWeights);
229  } else if (isolatorType == "IsolatorByDepositCount") {
230  std::string coneSizeType = isoPset.getParameter<std::string>("ConeSizeType");
231  if (coneSizeType == "FixedConeSize") {
232  float coneSize(isoPset.getParameter<double>("coneSize"));
233 
235 
236  // theIsolator = new IsolatorByDeposit(isoPset);
237  } else if (coneSizeType == "CutsConeSize") {
238  // Cuts cuts(isoPset.getParameter<edm::ParameterSet>("CutsPSet"));
239 
240  // theIsolator = new IsolatorByDeposit(coneSize, dWeights, dThresholds);
241  }
242  }
243 
244  if (theIsolator == nullptr) {
245  edm::LogError("MuonIsolationProducers") << "Failed to initialize an isolator";
246  }
248 
250 
251  if (theRemoveOtherVetos) {
252  edm::ParameterSet vetoPSet = par.getParameter<edm::ParameterSet>("VetoPSet");
253  theVetoCuts.selectAll = vetoPSet.getParameter<bool>("SelectAll");
254 
258  if (!theVetoCuts.selectAll) {
259  theVetoCuts.muAbsEtaMax = vetoPSet.getParameter<double>("MuAbsEtaMax");
260  theVetoCuts.muPtMin = vetoPSet.getParameter<double>("MuPtMin");
261  theVetoCuts.muAbsZMax = vetoPSet.getParameter<double>("MuAbsZMax");
262  theVetoCuts.muD0Max = vetoPSet.getParameter<double>("MuD0Max");
263  theBeamlineOption = par.getParameter<std::string>("BeamlineOption");
264  theBeamSpotLabel = par.getParameter<edm::InputTag>("BeamSpotLabel");
265  }
266  }
267 }
268 
270 template <typename BT>
272  if (theIsolator)
273  delete theIsolator;
274  LogDebug("RecoMuon|MuIsolatorResultProducer") << " MuIsolatorResultProducer DTOR";
275 }
276 
277 template <typename BT>
279  std::string metname = "RecoMuon|MuonIsolationProducers";
280  LogDebug(metname) << " Muon Deposit producing..."
281  << " BEGINING OF EVENT "
282  << "================================";
283 
285 
287  if (theRemoveOtherVetos && !theVetoCuts.selectAll) {
288  if (theBeamlineOption == "BeamSpotFromEvent") {
289  //pick beamSpot
291  edm::Handle<reco::BeamSpot> beamSpotH;
292 
293  event.getByLabel(theBeamSpotLabel, beamSpotH);
294 
295  if (beamSpotH.isValid()) {
296  theBeam = beamSpotH->position();
297  LogTrace(metname) << "Extracted beam point at " << theBeam << std::endl;
298  }
299  }
300  }
301 
307  CandMap candMapT;
308 
309  unsigned int colSize = initAssociation(event, candMapT);
310 
312  Results results(colSize);
313 
315  std::vector<reco::IsoDeposit::Vetos*> vetoDeps(theDepositConfs.size(), nullptr);
316 
317  if (colSize != 0) {
318  if (theRemoveOtherVetos) {
319  initVetos(theBeam, vetoDeps, candMapT);
320  }
321 
323  for (unsigned int muI = 0; muI < colSize; ++muI) {
324  results[muI] = theIsolator->result(candMapT.get()[muI].second, *(candMapT.get()[muI].first));
325 
326  if (results[muI].typeF() != theIsolator->resultType()) {
327  edm::LogError("MuonIsolationProducers") << "Failed to get result from the isolator";
328  }
329  }
330  }
331 
332  LogDebug(metname) << "Ready to write out results of size " << results.size();
333  writeOut(event, candMapT, results);
334 
335  for (unsigned int iDep = 0; iDep < vetoDeps.size(); ++iDep) {
337  if (vetoDeps[iDep]) {
338  delete vetoDeps[iDep];
339  vetoDeps[iDep] = nullptr;
340  }
341  }
342 }
343 
344 template <typename BT>
346  std::string metname = "RecoMuon|MuonIsolationProducers";
347 
349 
350  for (unsigned int iMap = 0; iMap < theDepositConfs.size(); ++iMap) {
352  event.getByLabel(theDepositConfs[iMap].tag, depH);
353  LogDebug(metname) << "Got Deposits of size " << depH->size();
354  if (depH->empty())
355  continue;
356 
359  typename CandMap::handle_type keyH;
360  event.get(depH->begin().id(), keyH);
361  candMapT.setHandle(keyH);
362  typename CT::const_iterator depHCI = depH->begin().begin();
363  typename CT::const_iterator depEnd = depH->begin().end();
364  unsigned int keyI = 0;
365  for (; depHCI != depEnd; ++depHCI, ++keyI) {
366  typename CandMap::key_type muPtr(keyH->refAt(keyI));
368  if (iMap == 0)
369  candMapT.get().push_back(typename CandMap::pair_type(muPtr, DepositContainer(theDepositConfs.size())));
370  typename CandMap::iterator muI = candMapT.get().begin();
371  for (; muI != candMapT.get().end(); ++muI) {
372  if (muI->first == muPtr)
373  break;
374  }
375  if (muI->first != muPtr) {
376  edm::LogError("MuonIsolationProducers") << "Failed to align muon map";
377  }
378  muI->second[iMap].dep = &*depHCI;
379  }
380  }
381 
382  LogDebug(metname) << "Picked and aligned nDeps = " << candMapT.get().size();
383  return candMapT.get().size();
384 }
385 
386 template <typename BT>
388  std::vector<reco::IsoDeposit::Vetos*>& vetos,
389  CandMap& candMapT) const {
390  std::string metname = "RecoMuon|MuonIsolationProducers";
391 
392  if (theRemoveOtherVetos) {
393  LogDebug(metname) << "Start checking for vetos based on input/expected vetos.size of " << vetos.size()
394  << " passed at " << &vetos << " and an input map.size of " << candMapT.get().size();
395 
396  unsigned int muI = 0;
397  for (; muI < candMapT.get().size(); ++muI) {
398  typename CandMap::key_type mu = candMapT.get()[muI].first;
399  double d0 = ((mu->vx() - theBeam.x()) * mu->py() - (mu->vy() - theBeam.y()) * mu->px()) / mu->pt();
400  LogDebug(metname) << "Muon at index " << muI;
401  if (theVetoCuts.selectAll || (fabs(mu->eta()) < theVetoCuts.muAbsEtaMax && mu->pt() > theVetoCuts.muPtMin &&
402  fabs(mu->vz()) < theVetoCuts.muAbsZMax && fabs(d0) < theVetoCuts.muD0Max)) {
403  LogDebug(metname) << "muon passes the cuts";
404  for (unsigned int iDep = 0; iDep < candMapT.get()[muI].second.size(); ++iDep) {
405  if (vetos[iDep] == nullptr)
406  vetos[iDep] = new reco::IsoDeposit::Vetos();
407 
408  vetos[iDep]->push_back(candMapT.get()[muI].second[iDep].dep->veto());
409  }
410  }
411  }
412 
413  LogDebug(metname) << "Assigning vetos";
414  muI = 0;
415  for (; muI < candMapT.get().size(); ++muI) {
416  for (unsigned int iDep = 0; iDep < candMapT.get()[muI].second.size(); ++iDep) {
417  candMapT.get()[muI].second[iDep].vetos = vetos[iDep];
418  }
419  }
420  LogDebug(metname) << "Done with vetos";
421  }
422 }
423 
424 #endif
muisorhelper::ResultType ResultType
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
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
void initVetos(reco::TrackBase::Point const &theBeam, std::vector< reco::IsoDeposit::Vetos *> &vetos, CandMap &candMap) const
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:167
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:169
const_iterator begin() const
Definition: ValueMap.h:223
container::const_iterator begin() const
Definition: ValueMap.h:168
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
std::vector< Veto > Vetos
Definition: IsoDeposit.h:65
const handle_type & handle() const
size_t size() const
Definition: ValueMap.h:150
HLT enums.
std::vector< value_type > container
Definition: ValueMap.h:110
results
Definition: mysort.py:8
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:152
def move(src, dest)
Definition: eostools.py:511
muisorhelper::CandMap< BT > CandMap
Definition: event.py:1
#define LogDebug(id)