CMS 3D CMS Logo

SimHitTPAssociationProducer.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <map>
3 #include <vector>
4 
9 
16 
22 
24 
26  : _simHitSrc(),
27  _trackingParticleSrc(
28  consumes<TrackingParticleCollection>(cfg.getParameter<edm::InputTag>("trackingParticleSrc"))) {
29  produces<SimHitTPAssociationList>();
30  std::vector<edm::InputTag> tags = cfg.getParameter<std::vector<edm::InputTag>>("simHitSrc");
31  _simHitSrc.reserve(tags.size());
32  for (auto const &tag : tags) {
33  _simHitSrc.emplace_back(consumes<edm::PSimHitContainer>(tag));
34  }
35 }
36 
38 
40  std::unique_ptr<SimHitTPAssociationList> simHitTPList(new SimHitTPAssociationList);
41 
42  // TrackingParticle
44  iEvent.getByToken(_trackingParticleSrc, TPCollectionH);
45 
46  // prepare temporary map between SimTrackId and TrackingParticle index
47  std::unordered_map<UniqueSimTrackId, TrackingParticleRef, UniqueSimTrackIdHash> mapping;
48  auto const &tpColl = *TPCollectionH.product();
49  for (TrackingParticleCollection::size_type itp = 0, size = tpColl.size(); itp < size; ++itp) {
50  auto const &trackingParticle = tpColl[itp];
51  TrackingParticleRef trackingParticleRef(TPCollectionH, itp);
52  // SimTracks inside TrackingParticle
53  EncodedEventId eid(trackingParticle.eventId());
54  for (auto const &trk : trackingParticle.g4Tracks()) {
55  UniqueSimTrackId trkid(trk.trackId(), eid);
56  mapping.insert(std::make_pair(trkid, trackingParticleRef));
57  }
58  }
59 
60  // PSimHits
61  for (auto const &psit : _simHitSrc) {
62  edm::Handle<edm::PSimHitContainer> PSimHitCollectionH;
63  iEvent.getByToken(psit, PSimHitCollectionH);
64  auto const &pSimHitCollection = *PSimHitCollectionH;
65  for (unsigned int psimHitI = 0, size = pSimHitCollection.size(); psimHitI < size; ++psimHitI) {
66  TrackPSimHitRef pSimHitRef(PSimHitCollectionH, psimHitI);
67  auto const &pSimHit = pSimHitCollection[psimHitI];
68  UniqueSimTrackId simTkIds(pSimHit.trackId(), pSimHit.eventId());
69  auto ipos = mapping.find(simTkIds);
70  if (ipos != mapping.end()) {
71  simHitTPList->emplace_back(ipos->second, pSimHitRef);
72  }
73  }
74  }
75 
76  std::sort(simHitTPList->begin(), simHitTPList->end(), simHitTPAssociationListGreater);
77  iEvent.put(std::move(simHitTPList));
78 }
79 
82 
size
Write out results.
std::vector< edm::EDGetTokenT< edm::PSimHitContainer > > _simHitSrc
SimHitTPAssociationProducer(const edm::ParameterSet &)
std::pair< uint32_t, EncodedEventId > UniqueSimTrackId
static bool simHitTPAssociationListGreater(SimHitTPPair i, SimHitTPPair j)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
T const * product() const
Definition: Handle.h:70
uint16_t size_type
std::vector< SimHitTPPair > SimHitTPAssociationList
int iEvent
Definition: GenABIO.cc:224
edm::EDGetTokenT< TrackingParticleCollection > _trackingParticleSrc
HLT enums.
std::vector< TrackingParticle > TrackingParticleCollection
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
def move(src, dest)
Definition: eostools.py:511