CMS 3D CMS Logo

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