CMS 3D CMS Logo

MuIsoDepositProducer.cc
Go to the documentation of this file.
2 
3 // Framework
8 
15 
18 
21 
23 #include <string>
24 
25 using namespace edm;
26 using namespace std;
27 using namespace reco;
28 using namespace muonisolation;
29 
32  : theDepositNames(std::vector<std::string>(1, std::string())) {
33  static const std::string metname = "RecoMuon|MuonIsolationProducers|MuIsoDepositProducer";
34  LogDebug(metname) << " MuIsoDepositProducer CTOR";
35 
36  edm::ParameterSet ioPSet = par.getParameter<edm::ParameterSet>("IOPSet");
37 
38  theInputType = ioPSet.getParameter<std::string>("InputType");
39  theExtractForCandidate = ioPSet.getParameter<bool>("ExtractForCandidate");
40  theMuonTrackRefType = ioPSet.getParameter<std::string>("MuonTrackRefType");
41 
42  bool readFromRecoTrack = theInputType == "TrackCollection";
43  bool readFromRecoMuon = theInputType == "MuonCollection";
44  bool readFromCandidateView = theInputType == "CandidateView";
45  if (readFromRecoTrack) {
46  theMuonCollectionTag = consumes<View<Track>>(ioPSet.getParameter<edm::InputTag>("inputMuonCollection"));
47  } else if (readFromRecoMuon) {
48  theMuonCollectionTag = consumes<View<RecoCandidate>>(ioPSet.getParameter<edm::InputTag>("inputMuonCollection"));
49  } else if (readFromCandidateView) {
50  theMuonCollectionTag = consumes<View<Candidate>>(ioPSet.getParameter<edm::InputTag>("inputMuonCollection"));
51  } else {
52  throw cms::Exception("Configuration") << "Inconsistent configuration or failure to read Candidate-muon view";
53  }
54 
55  theMultipleDepositsFlag = ioPSet.getParameter<bool>("MultipleDepositsFlag");
56 
58  theDepositNames = par.getParameter<edm::ParameterSet>("ExtractorPSet")
59  .getParameter<std::vector<std::string>>("DepositInstanceLabels");
60  }
61 
62  for (unsigned int i = 0; i < theDepositNames.size(); ++i) {
63  std::string alias = par.getParameter<std::string>("@module_label");
64  if (!theDepositNames[i].empty())
65  alias += "_" + theDepositNames[i];
66  produces<reco::IsoDepositMap>(theDepositNames[i]).setBranchAlias(alias);
67  }
68 
69  edm::ParameterSet extractorPSet = par.getParameter<edm::ParameterSet>("ExtractorPSet");
70  std::string extractorName = extractorPSet.getParameter<std::string>("ComponentName");
71  theExtractor = IsoDepositExtractorFactory::get()->create(extractorName, extractorPSet, consumesCollector());
72  LogDebug(metname) << " Load extractor..." << extractorName;
73 }
74 
77  LogDebug("RecoMuon/MuIsoDepositProducer") << " MuIsoDepositProducer DTOR";
78 }
79 
82  static const std::string metname = "RecoMuon|MuonIsolationProducers|MuIsoDepositProducer";
83 
84  LogDebug(metname) << " Muon Deposit producing..."
85  << " BEGINING OF EVENT "
86  << "================================";
87 
88  unsigned int nDeps = theMultipleDepositsFlag ? theDepositNames.size() : 1;
89 
90  // Take the muon container
91  LogTrace(metname) << " Taking the muons: "
92  << theMuonCollectionTag.index(); //a more friendly print would use "inputMuonCollection"
97 
98  unsigned int nMuons = 0;
99 
100  bool readFromRecoTrack = theInputType == "TrackCollection";
101  bool readFromRecoMuon = theInputType == "MuonCollection";
102  bool readFromCandidateView = theInputType == "CandidateView";
103 
104  if (readFromRecoMuon) {
105  event.getByToken(theMuonCollectionTag, muons);
106  nMuons = muons->size();
107  LogDebug(metname) << "Got Muons of size " << nMuons;
108  }
109  if (readFromRecoTrack) {
110  event.getByToken(theMuonCollectionTag, tracks);
111  nMuons = tracks->size();
112  LogDebug(metname) << "Got MuonTracks of size " << nMuons;
113  }
114  if (readFromCandidateView || theExtractForCandidate) {
115  event.getByToken(theMuonCollectionTag, cands);
116  unsigned int nCands = cands->size();
117  if (readFromRecoMuon && theExtractForCandidate) {
119  if (nMuons != nCands)
120  edm::LogError(metname) << "Inconsistent configuration or failure to read Candidate-muon view";
121  }
122  nMuons = nCands;
123  LogDebug(metname) << "Got candidate view with size " << nMuons;
124  }
125 
126  static const unsigned int MAX_DEPS = 10;
127  std::unique_ptr<reco::IsoDepositMap> depMaps[MAX_DEPS];
128 
129  if (nDeps > 10)
130  LogError(metname) << "Unable to handle more than 10 input deposits";
131  for (unsigned int i = 0; i < nDeps; ++i) {
132  depMaps[i] = std::make_unique<reco::IsoDepositMap>();
133  }
134 
138  if (nMuons > 0) {
139  std::vector<std::vector<IsoDeposit>> deps2D(nDeps, std::vector<IsoDeposit>(nMuons));
140 
141  for (unsigned int i = 0; i < nMuons; ++i) {
142  TrackBaseRef muRef;
143  if (readFromRecoMuon) {
144  if (theMuonTrackRefType == "track") {
145  muRef = TrackBaseRef((*muons)[i].track());
146  } else if (theMuonTrackRefType == "standAloneMuon") {
147  muRef = TrackBaseRef((*muons)[i].standAloneMuon());
148  } else if (theMuonTrackRefType == "combinedMuon") {
149  muRef = TrackBaseRef((*muons)[i].combinedMuon());
150  } else if (theMuonTrackRefType == "bestGlbTrkSta") {
151  if (!(*muons)[i].combinedMuon().isNull()) {
152  muRef = TrackBaseRef((*muons)[i].combinedMuon());
153  } else if (!(*muons)[i].track().isNull()) {
154  muRef = TrackBaseRef((*muons)[i].track());
155  } else {
156  muRef = TrackBaseRef((*muons)[i].standAloneMuon());
157  }
158  } else if (theMuonTrackRefType == "bestTrkSta") {
159  if (!(*muons)[i].track().isNull()) {
160  muRef = TrackBaseRef((*muons)[i].track());
161  } else {
162  muRef = TrackBaseRef((*muons)[i].standAloneMuon());
163  }
164  } else {
165  edm::LogWarning(metname) << "Wrong track type is supplied: breaking";
166  break;
167  }
168  } else if (readFromRecoTrack) {
169  muRef = TrackBaseRef(tracks, i);
170  }
171 
173  if (readFromCandidateView || theExtractForCandidate)
174  deps2D[0][i] = theExtractor->deposit(event, eventSetup, (*cands)[i]);
175  else
176  deps2D[0][i] = theExtractor->deposit(event, eventSetup, muRef);
177 
178  } else {
179  std::vector<IsoDeposit> deps(nDeps);
180  if (readFromCandidateView || theExtractForCandidate)
181  deps = theExtractor->deposits(event, eventSetup, (*cands)[i]);
182  else
183  deps = theExtractor->deposits(event, eventSetup, muRef);
184  for (unsigned int iDep = 0; iDep < nDeps; ++iDep) {
185  deps2D[iDep][i] = deps[iDep];
186  }
187  }
188  }
189 
191  for (unsigned int iDep = 0; iDep < nDeps; ++iDep) {
193  for (unsigned int iMu = 0; iMu < nMuons; ++iMu) {
194  LogTrace(metname) << "Contents of " << theDepositNames[iDep] << " for a muon at index " << iMu;
195  LogTrace(metname) << deps2D[iDep][iMu].print();
196  }
197 
199  reco::IsoDepositMap::Filler filler(*depMaps[iDep]);
200 
202  if (readFromRecoMuon) {
203  filler.insert(muons, deps2D[iDep].begin(), deps2D[iDep].end());
204  } else if (readFromRecoTrack) {
205  filler.insert(tracks, deps2D[iDep].begin(), deps2D[iDep].end());
206  } else if (readFromCandidateView) {
207  filler.insert(cands, deps2D[iDep].begin(), deps2D[iDep].end());
208  } else {
209  edm::LogError(metname) << "Inconsistent configuration: unknown type requested";
210  }
211 
213  filler.fill();
214  }
215  }
216 
217  for (unsigned int iMap = 0; iMap < nDeps; ++iMap) {
218  LogTrace(metname) << "About to put a deposit named " << theDepositNames[iMap] << " of size "
219  << depMaps[iMap]->size() << " into edm::Event";
220  event.put(std::move(depMaps[iMap]), theDepositNames[iMap]);
221  }
222 
223  LogTrace(metname) << " END OF EVENT "
224  << "================================";
225 }
226 
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
constexpr unsigned int index() const noexcept
Definition: EDGetToken.h:54
std::vector< std::string > theDepositNames
MuIsoDepositProducer(const edm::ParameterSet &)
constructor
const std::string metname
std::unique_ptr< reco::isodeposit::IsoDepositExtractor > theExtractor
Log< level::Error, false > LogError
~MuIsoDepositProducer() override
destructor
#define LogTrace(id)
edm::RefToBase< reco::Track > TrackBaseRef
persistent reference to a Track, using views
Definition: TrackFwd.h:35
edm::EDGetToken theMuonCollectionTag
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
fixed size matrix
HLT enums.
#define get
Log< level::Warning, false > LogWarning
void produce(edm::Event &, const edm::EventSetup &) override
data making method
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1
#define LogDebug(id)