CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CandIsoDepositProducer.cc
Go to the documentation of this file.
21 
22 #include <string>
23 
25 public:
27 
28  ~CandIsoDepositProducer() override;
29 
30  void produce(edm::Event &, const edm::EventSetup &) override;
31 
32 private:
33  inline const reco::Track *extractTrack(const reco::Candidate &cand, reco::Track *dummyStorage) const;
38  std::vector<std::string> theDepositNames;
40  std::unique_ptr<reco::isodeposit::IsoDepositExtractor> theExtractor;
41 };
42 
43 using namespace edm;
44 using namespace reco;
45 
48  : theConfig(par),
49  theCandCollectionToken(consumes<edm::View<reco::Candidate> >(par.getParameter<edm::InputTag>("src"))),
50  theDepositNames(std::vector<std::string>(1, "")),
51  theMultipleDepositsFlag(par.getParameter<bool>("MultipleDepositsFlag")) {
52  LogDebug("PhysicsTools|MuonIsolation") << " CandIsoDepositProducer CTOR";
53 
54  edm::ParameterSet extractorPSet = theConfig.getParameter<edm::ParameterSet>("ExtractorPSet");
55  std::string extractorName = extractorPSet.getParameter<std::string>("ComponentName");
56  theExtractor = IsoDepositExtractorFactory::get()->create(extractorName, extractorPSet, consumesCollector());
57 
59  produces<reco::IsoDepositMap>();
60  else {
61  theDepositNames = extractorPSet.getParameter<std::vector<std::string> >("DepositInstanceLabels");
62  if (theDepositNames.size() > 10)
63  throw cms::Exception("Configuration Error") << "This module supports only up to 10 deposits";
64  for (unsigned int iDep = 0; iDep < theDepositNames.size(); ++iDep) {
65  produces<reco::IsoDepositMap>(theDepositNames[iDep]);
66  }
67  }
68 
69  std::string trackType = par.getParameter<std::string>("trackType");
70  if (trackType == "fake")
72  else if (trackType == "best")
74  else if (trackType == "standAloneMuon")
76  else if (trackType == "combinedMuon")
78  else if (trackType == "trackerMuon")
80  else if (trackType == "track")
82  else if (trackType == "gsf")
84  else if (trackType == "candidate")
86  else
87  throw cms::Exception("Error") << "Track type " << trackType << " not valid.";
88 }
89 
92  LogDebug("PhysicsTools/CandIsoDepositProducer") << " CandIsoDepositProducer DTOR";
93 }
94 
96  if (theTrackType == CandidateT) {
97  return nullptr;
98  } else if (theTrackType == FakeT) {
99  *dummy = Track(10, 10, c.vertex(), c.momentum(), c.charge(), reco::Track::CovarianceMatrix());
100  return dummy;
101  } else {
102  const RecoCandidate *rc = dynamic_cast<const RecoCandidate *>(&c);
103  if (rc == nullptr)
104  throw cms::Exception("Error") << " Candidate is not RecoCandidate: can't get a real track from it!";
105  switch (theTrackType) {
106  case FakeT:
107  break; // to avoid warning
108  case CandidateT:
109  break; // to avoid warning
110  case BestT:
111  return rc->bestTrack();
112  break;
113  case StandAloneMuonT:
114  return &*rc->standAloneMuon();
115  break;
116  case CombinedMuonT:
117  return &*rc->combinedMuon();
118  break;
119  case TrackT:
120  return &*rc->track();
121  break;
122  case GsfT:
123  return static_cast<const Track *>(rc->gsfTrack().get());
124  break;
125  }
126  return nullptr;
127  }
128 }
129 
132  static const std::string metname = "CandIsoDepositProducer";
133 
135  event.getByToken(theCandCollectionToken, hCands);
136 
137  unsigned int nDeps = theMultipleDepositsFlag ? theDepositNames.size() : 1;
138 
139  static const unsigned int MAX_DEPS = 10;
140 
141  if (nDeps > MAX_DEPS)
142  LogError(metname) << "Unable to handle more than 10 input deposits";
143 
147  size_t nMuons = hCands->size();
148  std::vector<std::vector<IsoDeposit> > deps2D(nDeps, std::vector<IsoDeposit>(nMuons));
149 
150  if (nMuons > 0) {
151  theExtractor->initEvent(event, eventSetup);
152 
153  Track dummy;
154  for (size_t i = 0; i < nMuons; ++i) {
155  const Candidate &c = (*hCands)[i];
156  const Track *track = extractTrack(c, &dummy);
157  if ((theTrackType != CandidateT) && (!track)) {
158  edm::LogWarning("CandIsoDepositProducer")
159  << "Candidate #" << i << " has no bestTrack(), it will produce no deposit";
160  reco::IsoDeposit emptyDep;
161  for (size_t iDep = 0; iDep < nDeps; ++iDep) {
162  deps2D[iDep][i] = emptyDep;
163  }
164  continue;
165  }
167  deps2D[0][i] = ((theTrackType == CandidateT) ? theExtractor->deposit(event, eventSetup, c)
168  : theExtractor->deposit(event, eventSetup, *track));
169  } else {
170  std::vector<IsoDeposit> deps =
171  ((theTrackType == CandidateT) ? theExtractor->deposits(event, eventSetup, c)
172  : theExtractor->deposits(event, eventSetup, *track));
173  for (unsigned int iDep = 0; iDep < nDeps; ++iDep) {
174  deps2D[iDep][i] = deps[iDep];
175  }
176  }
177  }
178  } //if (nMuons>0)
179 
181  for (unsigned int iDep = 0; iDep < nDeps; ++iDep) {
183  for (unsigned int iMu = 0; iMu < nMuons; ++iMu) {
184  LogTrace(metname) << "Contents of " << theDepositNames[iDep] << " for a muon at index " << iMu;
185  LogTrace(metname) << deps2D[iDep][iMu].print();
186  }
187 
189  auto depMap = std::make_unique<reco::IsoDepositMap>();
190  reco::IsoDepositMap::Filler filler(*depMap);
191  filler.insert(hCands, deps2D[iDep].begin(), deps2D[iDep].end());
192  deps2D[iDep].clear();
193  filler.fill();
194  event.put(std::move(depMap), theDepositNames[iDep]);
195  }
196 }
197 
virtual reco::GsfTrackRef gsfTrack() const
reference to a GsfTrack
const edm::EventSetup & c
const std::string metname
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< edm::View< reco::Candidate > > theCandCollectionToken
std::vector< std::string > theDepositNames
const reco::Track * extractTrack(const reco::Candidate &cand, reco::Track *dummyStorage) const
Log< level::Error, false > LogError
virtual reco::TrackRef standAloneMuon() const
reference to a stand-alone muon Track
void produce(edm::Event &, const edm::EventSetup &) override
build deposits
#define LogTrace(id)
virtual reco::TrackRef track() const
reference to a Track
virtual Vector momentum() const =0
spatial momentum vector
def move
Definition: eostools.py:511
virtual const Point & vertex() const =0
vertex position
virtual int charge() const =0
electric charge
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:232
std::unique_ptr< reco::isodeposit::IsoDepositExtractor > theExtractor
const Track * bestTrack() const override
best track pointer
CandIsoDepositProducer(const edm::ParameterSet &)
constructor with config
tuple deps
Definition: symbols.py:61
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
string end
Definition: dataset.py:937
~CandIsoDepositProducer() override
destructor
#define get
Log< level::Warning, false > LogWarning
math::Error< dimension >::type CovarianceMatrix
5 parameter covariance matrix
Definition: TrackBase.h:74
#define LogDebug(id)
virtual reco::TrackRef combinedMuon() const
reference to a stand-alone muon Track