|
| 1 | +#include "TrackParticleRelationAlg.h" |
| 2 | + |
| 3 | +DECLARE_COMPONENT(TrackParticleRelationAlg) |
| 4 | + |
| 5 | +TrackParticleRelationAlg::TrackParticleRelationAlg(const std::string& name, ISvcLocator* svcLoc) |
| 6 | +: GaudiAlgorithm(name, svcLoc){ |
| 7 | + declareProperty("MCParticleCollection", m_inMCParticleColHdl, "Handle of the Input MCParticle collection"); |
| 8 | +} |
| 9 | + |
| 10 | +StatusCode TrackParticleRelationAlg::initialize() { |
| 11 | + for(auto name : m_inTrackCollectionNames) { |
| 12 | + m_inTrackColHdls.push_back(new DataHandle<edm4hep::TrackCollection> (name, Gaudi::DataHandle::Reader, this)); |
| 13 | + m_outColHdls.push_back(new DataHandle<edm4hep::MCRecoTrackParticleAssociationCollection> (name+"ParticleAssociation", Gaudi::DataHandle::Writer, this)); |
| 14 | + } |
| 15 | + |
| 16 | + for(unsigned i=0; i<m_inAssociationCollectionNames.size(); i++) { |
| 17 | + m_inAssociationColHdls.push_back(new DataHandle<edm4hep::MCRecoTrackerAssociationCollection> (m_inAssociationCollectionNames[i], Gaudi::DataHandle::Reader, this)); |
| 18 | + } |
| 19 | + |
| 20 | + if(m_inAssociationColHdls.size()==0) { |
| 21 | + warning() << "no Association Collection input" << endmsg; |
| 22 | + return StatusCode::FAILURE; |
| 23 | + } |
| 24 | + |
| 25 | + m_nEvt = 0; |
| 26 | + return GaudiAlgorithm::initialize(); |
| 27 | +} |
| 28 | + |
| 29 | +StatusCode TrackParticleRelationAlg::execute() { |
| 30 | + info() << "start Event " << m_nEvt << endmsg; |
| 31 | + |
| 32 | + // MCParticle |
| 33 | + const edm4hep::MCParticleCollection* mcpCol = nullptr; |
| 34 | + try { |
| 35 | + mcpCol = m_inMCParticleColHdl.get(); |
| 36 | + for (auto mcp : *mcpCol) { |
| 37 | + debug() << "id=" << mcp.id() << " pdgid=" << mcp.getPDG() << " charge=" << mcp.getCharge() << " genstat=" << mcp.getGeneratorStatus() << " simstat=" << mcp.getSimulatorStatus() |
| 38 | + << " p=" << mcp.getMomentum() << endmsg; |
| 39 | + int nparents = mcp.parents_size(); |
| 40 | + int ndaughters = mcp.daughters_size(); |
| 41 | + for (int i=0; i<nparents; i++) { |
| 42 | + debug() << "<<<<<<" << mcp.getParents(i).id() << endmsg; |
| 43 | + } |
| 44 | + for (int i=0; i<ndaughters; i++) { |
| 45 | + debug() << "<<<<<<" << mcp.getDaughters(i).id() << endmsg; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + catch ( GaudiException &e ) { |
| 50 | + debug() << "Collection " << m_inMCParticleColHdl.fullKey() << " is unavailable in event " << m_nEvt << endmsg; |
| 51 | + } |
| 52 | + if(!mcpCol) { |
| 53 | + warning() << "pass this event because MCParticle collection can not read" << endmsg; |
| 54 | + return StatusCode::SUCCESS; |
| 55 | + } |
| 56 | + |
| 57 | + // Prepare map from hit to MCParticle |
| 58 | + std::map<edm4hep::TrackerHit, edm4hep::MCParticle> mapHitParticle; |
| 59 | + debug() << "reading Association" << endmsg; |
| 60 | + for (auto hdl : m_inAssociationColHdls) { |
| 61 | + const edm4hep::MCRecoTrackerAssociationCollection* assCol = nullptr; |
| 62 | + try { |
| 63 | + assCol = hdl->get(); |
| 64 | + } |
| 65 | + catch ( GaudiException &e ) { |
| 66 | + debug() << "Collection " << hdl->fullKey() << " is unavailable in event " << m_nEvt << endmsg; |
| 67 | + return StatusCode::FAILURE; |
| 68 | + } |
| 69 | + for (auto ass: *assCol) { |
| 70 | + auto hit = ass.getRec(); |
| 71 | + auto particle = ass.getSim().getMCParticle(); |
| 72 | + mapHitParticle[hit] = particle; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Handle all input TrackCollection |
| 77 | + for (unsigned icol=0; icol<m_inTrackColHdls.size(); icol++) { |
| 78 | + auto hdl = m_inTrackColHdls[icol]; |
| 79 | + |
| 80 | + const edm4hep::TrackCollection* trkCol = nullptr; |
| 81 | + try { |
| 82 | + trkCol = hdl->get(); |
| 83 | + } |
| 84 | + catch ( GaudiException &e ) { |
| 85 | + debug() << "Collection " << hdl->fullKey() << " is unavailable in event " << m_nEvt << endmsg; |
| 86 | + } |
| 87 | + |
| 88 | + auto outCol = m_outColHdls[icol]->createAndPut(); |
| 89 | + if(!outCol) { |
| 90 | + error() << "Collection " << m_outColHdls[icol]->fullKey() << " cannot be created" << endmsg; |
| 91 | + return StatusCode::FAILURE; |
| 92 | + } |
| 93 | + |
| 94 | + if(trkCol) { |
| 95 | + std::map<edm4hep::MCParticle, std::vector<edm4hep::TrackerHit> > mapParticleHits; |
| 96 | + |
| 97 | + for (auto track: *trkCol) { |
| 98 | + std::map<edm4hep::MCParticle, int> mapParticleNHits; |
| 99 | + |
| 100 | + // Count hits related to MCParticle |
| 101 | + int nhits = track.trackerHits_size(); |
| 102 | + for (int ihit=0; ihit<nhits; ihit++) { |
| 103 | + auto hit = track.getTrackerHits(ihit); |
| 104 | + auto itHP = mapHitParticle.find(hit); |
| 105 | + if (itHP==mapHitParticle.end()) { |
| 106 | + warning() << "track " << track.id() << "'s hit " << hit.id() << "not in association list, will be ignored" << endmsg; |
| 107 | + } |
| 108 | + else { |
| 109 | + auto particle = itHP->second; |
| 110 | + if(!particle.isAvailable()) continue; |
| 111 | + debug() << "track " << track.id() << "'s hit " << hit.id() << " link to MCParticle " << particle.id() << endmsg; |
| 112 | + auto itPN = mapParticleNHits.find(particle); |
| 113 | + if (itPN!=mapParticleNHits.end()) itPN->second++; |
| 114 | + else mapParticleNHits[particle] = 1; |
| 115 | + |
| 116 | + if (msgLevel(MSG::DEBUG)) mapParticleHits[particle].push_back(hit); |
| 117 | + } |
| 118 | + } |
| 119 | + debug() << "Total " << mapParticleNHits.size() << " particles link to the track " << track.id() << endmsg; |
| 120 | + |
| 121 | + // Save to collection |
| 122 | + for (std::map<edm4hep::MCParticle, int>::iterator it=mapParticleNHits.begin(); it!=mapParticleNHits.end(); it++) { |
| 123 | + auto ass = outCol->create(); |
| 124 | + ass.setWeight(it->second); |
| 125 | + ass.setRec(track); |
| 126 | + ass.setSim(it->first); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (msgLevel(MSG::DEBUG)) { |
| 131 | + for (std::map<edm4hep::MCParticle, std::vector<edm4hep::TrackerHit> >::iterator it=mapParticleHits.begin(); it!=mapParticleHits.end(); it++) { |
| 132 | + auto particle = it->first; |
| 133 | + auto hits = it->second; |
| 134 | + debug() << "=== MCPaticle ===" << particle << endmsg; |
| 135 | + for (auto hit : hits) { |
| 136 | + debug() << hit << endmsg; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + m_nEvt++; |
| 144 | + return StatusCode::SUCCESS; |
| 145 | +} |
| 146 | + |
| 147 | +StatusCode TrackParticleRelationAlg::finalize() { |
| 148 | + |
| 149 | + return StatusCode::SUCCESS; |
| 150 | +} |
0 commit comments