CMS 3D CMS Logo

CandIsoDepositProducer.cc
Go to the documentation of this file.
2 
3 // Framework
9 
11 
15 
16 
20 
23 
25 #include <string>
26 
27 using namespace edm;
28 using namespace reco;
29 using namespace muonisolation;
30 
31 
34  theConfig(par),
35  theCandCollectionToken(consumes< edm::View<reco::Candidate> >(par.getParameter<edm::InputTag>("src"))),
36  theDepositNames(std::vector<std::string>(1,"")),
37  theMultipleDepositsFlag(par.getParameter<bool>("MultipleDepositsFlag"))
38  {
39  LogDebug("PhysicsTools|MuonIsolation")<<" CandIsoDepositProducer CTOR";
40 
41  edm::ParameterSet extractorPSet = theConfig.getParameter<edm::ParameterSet>("ExtractorPSet");
42  std::string extractorName = extractorPSet.getParameter<std::string>("ComponentName");
43  theExtractor = std::unique_ptr<reco::isodeposit::IsoDepositExtractor>{IsoDepositExtractorFactory::get()->create( extractorName, extractorPSet, consumesCollector())};
44 
45  if (! theMultipleDepositsFlag) produces<reco::IsoDepositMap>();
46  else {
47  theDepositNames = extractorPSet.getParameter<std::vector<std::string> >("DepositInstanceLabels");
48  if (theDepositNames.size() > 10) throw cms::Exception("Configuration Error") << "This module supports only up to 10 deposits";
49  for (unsigned int iDep=0; iDep<theDepositNames.size(); ++iDep){
50  produces<reco::IsoDepositMap>(theDepositNames[iDep]);
51  }
52  }
53 
54  std::string trackType = par.getParameter<std::string>("trackType");
55  if (trackType == "fake") theTrackType = FakeT;
56  else if (trackType == "best") theTrackType = BestT;
57  else if (trackType == "standAloneMuon") theTrackType = StandAloneMuonT;
58  else if (trackType == "combinedMuon") theTrackType = CombinedMuonT;
59  else if (trackType == "trackerMuon") theTrackType = TrackT;
60  else if (trackType == "track") theTrackType = TrackT;
61  else if (trackType == "gsf") theTrackType = GsfT;
62  else if (trackType == "candidate") theTrackType = CandidateT;
63  else throw cms::Exception("Error") << "Track type " << trackType << " not valid.";
64 }
65 
68  LogDebug("PhysicsTools/CandIsoDepositProducer")<<" CandIsoDepositProducer DTOR";
69 }
70 
72  if (theTrackType == CandidateT) {
73  return nullptr;
74  } else if (theTrackType == FakeT) {
75  *dummy = Track(10,10,c.vertex(),c.momentum(),c.charge(), reco::Track::CovarianceMatrix());
76  return dummy;
77  } else {
78  const RecoCandidate *rc = dynamic_cast<const RecoCandidate *>(&c);
79  if (rc == nullptr) throw cms::Exception("Error") << " Candidate is not RecoCandidate: can't get a real track from it!";
80  switch (theTrackType) {
81  case FakeT: break; // to avoid warning
82  case CandidateT: break; // to avoid warning
83  case BestT: return rc->bestTrack(); break;
84  case StandAloneMuonT: return &*rc->standAloneMuon(); break;
85  case CombinedMuonT: return &*rc->combinedMuon(); break;
86  case TrackT: return &*rc->track(); break;
87  case GsfT: return static_cast<const Track*>(rc->gsfTrack().get()); break;
88  }
89  return nullptr;
90  }
91 }
92 
95  static const std::string metname = "CandIsoDepositProducer";
96 
98  event.getByToken(theCandCollectionToken, hCands);
99 
100  unsigned int nDeps = theMultipleDepositsFlag ? theDepositNames.size() : 1;
101 
102  static const unsigned int MAX_DEPS=10;
103 
104  if (nDeps >MAX_DEPS ) LogError(metname)<<"Unable to handle more than 10 input deposits";
105 
109  size_t nMuons = hCands->size();
110  std::vector<std::vector<IsoDeposit> > deps2D(nDeps, std::vector<IsoDeposit>(nMuons));
111 
112  if (nMuons > 0){
113  theExtractor->initEvent(event, eventSetup);
114 
115  Track dummy;
116  for (size_t i=0; i< nMuons; ++i) {
117  const Candidate &c = (*hCands)[i];
118  const Track *track = extractTrack(c, &dummy);
119  if ((theTrackType != CandidateT) && (!track)) {
120  edm::LogWarning("CandIsoDepositProducer") << "Candidate #"<<i<<" has no bestTrack(), it will produce no deposit";
121  reco::IsoDeposit emptyDep;
122  for (size_t iDep=0;iDep<nDeps;++iDep) {
123  deps2D[iDep][i] = emptyDep;
124  }
125  continue;
126  }
128  deps2D[0][i] = ( ( theTrackType == CandidateT )
129  ? theExtractor->deposit(event, eventSetup, c)
130  : theExtractor->deposit(event, eventSetup, *track) );
131  } else {
132  std::vector<IsoDeposit> deps = ( ( theTrackType == CandidateT )
133  ? theExtractor->deposits(event, eventSetup, c)
134  : theExtractor->deposits(event, eventSetup, *track) );
135  for (unsigned int iDep=0; iDep < nDeps; ++iDep){ deps2D[iDep][i] = deps[iDep]; }
136  }
137  }
138  }//if (nMuons>0)
139 
141  for (unsigned int iDep=0; iDep < nDeps; ++iDep){
143  for (unsigned int iMu = 0; iMu< nMuons; ++iMu){
144  LogTrace(metname)<<"Contents of "<<theDepositNames[iDep]
145  <<" for a muon at index "<<iMu;
146  LogTrace(metname)<<deps2D[iDep][iMu].print();
147  }
148 
150  auto depMap = std::make_unique<reco::IsoDepositMap>();
152  filler.insert(hCands, deps2D[iDep].begin(), deps2D[iDep].end());
153  deps2D[iDep].clear();
154  filler.fill();
155  event.put(std::move(depMap), theDepositNames[iDep]);
156  }
157 
158 }
159 
#define LogDebug(id)
virtual reco::GsfTrackRef gsfTrack() const
reference to a GsfTrack
T getParameter(std::string const &) const
const std::string metname
std::vector< std::string > theDepositNames
const reco::Track * extractTrack(const reco::Candidate &cand, reco::Track *dummyStorage) const
virtual reco::TrackRef standAloneMuon() const
reference to a stand-alone muon Track
void produce(edm::Event &, const edm::EventSetup &) override
build deposits
virtual reco::TrackRef track() const
reference to a Track
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
#define end
Definition: vmac.h:39
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:243
std::unique_ptr< reco::isodeposit::IsoDepositExtractor > theExtractor
#define LogTrace(id)
CandIsoDepositProducer(const edm::ParameterSet &)
constructor with config
edm::EDGetTokenT< edm::View< reco::Candidate > > theCandCollectionToken
virtual Vector momentum() const =0
spatial momentum vector
virtual int charge() const =0
electric charge
fixed size matrix
#define begin
Definition: vmac.h:32
HLT enums.
const Track * bestTrack() const override
best track pointer
virtual const Point & vertex() const =0
vertex position
~CandIsoDepositProducer() override
destructor
def move(src, dest)
Definition: eostools.py:511
T get(const Candidate &c)
Definition: component.h:55
Definition: event.py:1
math::Error< dimension >::type CovarianceMatrix
5 parameter covariance matrix
Definition: TrackBase.h:77
virtual reco::TrackRef combinedMuon() const
reference to a stand-alone muon Track