CMS 3D CMS Logo

MuonToTrackingParticleAssociatorEDProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: SimMuon/MCTruth
4 // Class: MuonToTrackingParticleAssociatorEDProducer
5 //
15 //
16 // Original Author: Christopher Jones
17 // Created: Wed, 07 Jan 2015 21:30:14 GMT
18 //
19 //
20 
21 // system include files
22 #include <memory>
23 
24 // user include files
27 
30 
32 
38 
39 //
40 // class declaration
41 //
42 namespace {
44 
45  class InputDumper {
46  public:
47  InputDumper(const edm::ParameterSet &conf, edm::ConsumesCollector &&iC)
48  : simtracksTag(conf.getParameter<edm::InputTag>("simtracksTag")),
49  simtracksXFTag(conf.getParameter<edm::InputTag>("simtracksXFTag")),
50  crossingframe(conf.getParameter<bool>("crossingframe")) {
51  if (crossingframe) {
52  simtracksXFToken_ = iC.consumes<CrossingFrame<SimTrack>>(simtracksXFTag);
53  simvertsXFToken_ = iC.consumes<CrossingFrame<SimVertex>>(simtracksXFTag);
54  } else {
55  simtracksToken_ = iC.consumes<edm::SimTrackContainer>(simtracksTag);
56  simvertsToken_ = iC.consumes<edm::SimVertexContainer>(simtracksTag);
57  }
58  }
59 
60  void read(const edm::Event &);
61  void dump(const TrackHitsCollection &, const TrackingParticleCollection &) const;
62 
63  private:
70 
75  bool const crossingframe;
76  };
77 
78  void InputDumper::read(const edm::Event &iEvent) {
79  if (crossingframe) {
80  iEvent.getByToken(simtracksXFToken_, simtracksXF_);
81  iEvent.getByToken(simvertsXFToken_, simvertsXF_);
82  } else {
83  iEvent.getByToken(simtracksToken_, simtracks_);
84  iEvent.getByToken(simvertsToken_, simverts_);
85  }
86  }
87 
88  void InputDumper::dump(const TrackHitsCollection &tC, const TrackingParticleCollection &tPC) const {
89  using namespace std;
90  // reco::Track collection
91  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer") << "\n"
92  << "reco::Track collection --- size = " << tC.size();
93 
94  // TrackingParticle collection
95  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
96  << "\n"
97  << "TrackingParticle collection --- size = " << tPC.size();
98  int j = 0;
99  for (TrackingParticleCollection::const_iterator ITER = tPC.begin(); ITER != tPC.end(); ITER++, j++) {
100  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
101  << "TrackingParticle " << j << ", q = " << ITER->charge() << ", p = " << ITER->p() << ", pT = " << ITER->pt()
102  << ", eta = " << ITER->eta() << ", phi = " << ITER->phi();
103 
104  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
105  << "\t pdg code = " << ITER->pdgId() << ", made of " << ITER->numberOfHits() << " PSimHit"
106  << " (in " << ITER->numberOfTrackerLayers() << " layers)"
107  << " from " << ITER->g4Tracks().size() << " SimTrack:";
108  for (TrackingParticle::g4t_iterator g4T = ITER->g4Track_begin(); g4T != ITER->g4Track_end(); g4T++) {
109  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
110  << "\t\t Id:" << g4T->trackId() << "/Evt:(" << g4T->eventId().event() << ","
111  << g4T->eventId().bunchCrossing() << ")";
112  }
113  }
114 
115  if (crossingframe) {
116  std::unique_ptr<MixCollection<SimTrack>> SimTk(new MixCollection<SimTrack>(simtracksXF_.product()));
117  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
118  << "\n"
119  << "CrossingFrame<SimTrack> collection with InputTag = " << simtracksXFTag << " has size = " << SimTk->size();
120  int k = 0;
121  for (MixCollection<SimTrack>::MixItr ITER = SimTk->begin(); ITER != SimTk->end(); ITER++, k++) {
122  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
123  << "SimTrack " << k << " - Id:" << ITER->trackId() << "/Evt:(" << ITER->eventId().event() << ","
124  << ITER->eventId().bunchCrossing() << ")"
125  << ", q = " << ITER->charge() << ", p = " << ITER->momentum().P() << ", pT = " << ITER->momentum().Pt()
126  << ", eta = " << ITER->momentum().Eta() << ", phi = " << ITER->momentum().Phi()
127  << "\n\t pdgId = " << ITER->type() << ", Vertex index = " << ITER->vertIndex()
128  << ", Gen Particle index = " << (ITER->genpartIndex() > 0 ? ITER->genpartIndex() - 1 : ITER->genpartIndex())
129  << endl;
130  }
131 
132  std::unique_ptr<MixCollection<SimVertex>> SimVtx(new MixCollection<SimVertex>(simvertsXF_.product()));
133  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
134  << "\n"
135  << "CrossingFrame<SimVertex> collection with InputTag = " << simtracksXFTag
136  << " has size = " << SimVtx->size();
137  int kv = 0;
138  for (MixCollection<SimVertex>::MixItr VITER = SimVtx->begin(); VITER != SimVtx->end(); VITER++, kv++) {
139  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
140  << "SimVertex " << kv << " - Id:" << VITER->vertexId() << ", position = " << VITER->position()
141  << ", parent SimTrack Id = " << VITER->parentIndex() << ", processType = " << VITER->processType();
142  }
143  } else {
144  const edm::SimTrackContainer simTC = *(simtracks_.product());
145  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
146  << "\n"
147  << "SimTrack collection with InputTag = " << simtracksTag << " has size = " << simTC.size() << endl;
148  int k = 0;
149  for (edm::SimTrackContainer::const_iterator ITER = simTC.begin(); ITER != simTC.end(); ITER++, k++) {
150  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
151  << "SimTrack " << k << " - Id:" << ITER->trackId() << "/Evt:(" << ITER->eventId().event() << ","
152  << ITER->eventId().bunchCrossing() << ")"
153  << ", q = " << ITER->charge() << ", p = " << ITER->momentum().P() << ", pT = " << ITER->momentum().Pt()
154  << ", eta = " << ITER->momentum().Eta() << ", phi = " << ITER->momentum().Phi()
155  << "\n\t pdgId = " << ITER->type() << ", Vertex index = " << ITER->vertIndex()
156  << ", Gen Particle index = " << (ITER->genpartIndex() > 0 ? ITER->genpartIndex() - 1 : ITER->genpartIndex())
157  << endl;
158  }
159  const edm::SimVertexContainer simVC = *(simverts_.product());
160  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer") << "\n"
161  << "SimVertex collection with InputTag = "
162  << "g4SimHits"
163  << " has size = " << simVC.size() << endl;
164  int kv = 0;
165  for (edm::SimVertexContainer::const_iterator VITER = simVC.begin(); VITER != simVC.end(); VITER++, kv++) {
166  edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
167  << "SimVertex " << kv << " - Id:" << VITER->vertexId() << ", position = " << VITER->position()
168  << ", parent SimTrack Id = " << VITER->parentIndex() << ", processType = " << VITER->processType();
169  }
170  }
171  }
172 
173 } // namespace
174 
176 public:
179 
180  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
181 
182 private:
183  void produce(edm::Event &, const edm::EventSetup &) override;
184 
185  // ----------member data ---------------------------
190 
191  std::unique_ptr<RPCHitAssociator> rpctruth_;
192  std::unique_ptr<GEMHitAssociator> gemtruth_;
193  std::unique_ptr<DTHitAssociator> dttruth_;
194  std::unique_ptr<CSCHitAssociator> csctruth_;
195  std::unique_ptr<TrackerHitAssociator> trackertruth_;
196  std::unique_ptr<InputDumper> diagnostics_;
197 };
198 
199 //
200 // constants, enums and typedefs
201 //
202 
203 //
204 // static data member definitions
205 //
206 
207 //
208 // constructors and destructor
209 //
211  : config_(iConfig),
212  helper_(iConfig),
213  trackerHitAssociatorConfig_(iConfig, consumesCollector()),
214  hitExtractor_(iConfig, consumesCollector()) {
215  // register your products
216  produces<reco::MuonToTrackingParticleAssociator>();
217 
218  // hack for consumes
219  RPCHitAssociator rpctruth(iConfig, consumesCollector());
220  GEMHitAssociator gemtruth(iConfig, consumesCollector());
221  DTHitAssociator dttruth(iConfig, consumesCollector());
222  CSCHitAssociator cscruth(iConfig, consumesCollector());
223 
224  if (iConfig.getUntrackedParameter<bool>("dumpInputCollections")) {
225  diagnostics_.reset(new InputDumper(iConfig, consumesCollector()));
226  }
227 }
228 
230  // do anything here that needs to be done at desctruction time
231  // (e.g. close files, deallocate resources etc.)
232 }
233 
234 //
235 // member functions
236 //
237 
238 // ------------ method called to produce the data ------------
240  using namespace edm;
241 
242  hitExtractor_.init(iEvent, iSetup);
243 
244  // Retrieve tracker topology from geometry
246  iSetup.get<TrackerTopologyRcd>().get(tTopoHand);
247  const TrackerTopology *tTopo = tTopoHand.product();
248 
249  bool printRtS = true;
250 
251  // NOTE: This assumes that produce will not be called until the edm::Event
252  // used in the previous call
253  // has been deleted. This is true for now. In the future, we may have to have
254  // the resources own the memory.
255 
256  // Tracker hit association
258  // CSC hit association
259  csctruth_.reset(new CSCHitAssociator(iEvent, iSetup, config_));
260  // DT hit association
261  printRtS = false;
262  dttruth_.reset(new DTHitAssociator(iEvent, iSetup, config_, printRtS));
263  // RPC hit association
264  rpctruth_.reset(new RPCHitAssociator(iEvent, iSetup, config_));
265  // GEM hit association
266  gemtruth_.reset(new GEMHitAssociator(iEvent, iSetup, config_));
267 
269  tTopo, trackertruth_.get(), csctruth_.get(), dttruth_.get(), rpctruth_.get(), gemtruth_.get(), {}};
270 
271  if (diagnostics_) {
272  diagnostics_->read(iEvent);
273  resources.diagnostics_ = [this](const TrackHitsCollection &hC, const TrackingParticleCollection &pC) {
274  diagnostics_->dump(hC, pC);
275  };
276  }
277 
278  std::unique_ptr<reco::MuonToTrackingParticleAssociatorBaseImpl> impl{
280  std::unique_ptr<reco::MuonToTrackingParticleAssociator> toPut(
282  iEvent.put(std::move(toPut));
283 }
284 
285 // ------------ method fills 'descriptions' with the allowed parameters for the
286 // module ------------
288  // The following says we do not know what parameters are allowed so do no
289  // validation
290  // Please change this to state exactly what you do use, even if it is no
291  // parameters
293  desc.setUnknown();
294  descriptions.addDefault(desc);
295 }
296 
297 // define this as a plug-in
T getUntrackedParameter(std::string const &, T const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
std::vector< TrackingParticle > TrackingParticleCollection
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
std::function< void(const TrackHitsCollection &, const TrackingParticleCollection &)> diagnostics_
std::vector< std::pair< trackingRecHit_iterator, trackingRecHit_iterator > > TrackHitsCollection
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void addDefault(ParameterSetDescription const &psetDescription)
MuonAssociatorByHitsHelper::TrackHitsCollection TrackHitsCollection
void init(const edm::Event &, const edm::EventSetup &)
std::vector< SimTrack >::const_iterator g4t_iterator
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< SimVertex > SimVertexContainer
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
HLT enums.
T get() const
Definition: EventSetup.h:73
void produce(edm::Event &, const edm::EventSetup &) override
T const * product() const
Definition: ESHandle.h:86
std::vector< SimTrack > SimTrackContainer
def move(src, dest)
Definition: eostools.py:511