diff --git a/SimDataFormats/Track/interface/SimTrack.h b/SimDataFormats/Track/interface/SimTrack.h index 19f73ea6b385a..27f2db19d2c16 100644 --- a/SimDataFormats/Track/interface/SimTrack.h +++ b/SimDataFormats/Track/interface/SimTrack.h @@ -68,7 +68,7 @@ class SimTrack : public CoreSimTrack { void setIsPrimary() { trackInfo_ |= (1 << 1); } void setGenParticleID(const int idx) { igenpart = idx; } - int getPrimaryID() const { return igenpart; } + int getPrimaryOrLastStoredID() const { return igenpart; } uint8_t getTrackInfo() const { return trackInfo_; } private: diff --git a/SimG4CMS/Calo/src/CaloSD.cc b/SimG4CMS/Calo/src/CaloSD.cc index 05cdba80f02e1..0312e8f0ba7b7 100644 --- a/SimG4CMS/Calo/src/CaloSD.cc +++ b/SimG4CMS/Calo/src/CaloSD.cc @@ -574,7 +574,8 @@ unsigned int CaloSD::findBoundaryCrossingParent(const G4Track* track, bool markA #endif boundaryCrossingParentMap_[id] = id; trkInfo->setStoreTrack(); - trkInfo->setIdLastStoredAncestor(id); + if (trkInfo->idLastStoredAncestor() == track->GetParentID()) + trkInfo->setIdLastStoredAncestor(id); return id; } // Else, traverse the history of the track @@ -683,7 +684,8 @@ CaloG4Hit* CaloSD::createNewHit(const G4Step* aStep, const G4Track* theTrack, in #endif if (etrack >= energyCut || forceSave) { trkInfo->setStoreTrack(); - trkInfo->setIdLastStoredAncestor(theTrack->GetTrackID()); + if (trkInfo->idLastStoredAncestor() == theTrack->GetParentID()) + trkInfo->setIdLastStoredAncestor(theTrack->GetTrackID()); } } else { TrackWithHistory* trkh = tkMap[currentID[k].trackID()]; diff --git a/SimG4CMS/Forward/src/TimingSD.cc b/SimG4CMS/Forward/src/TimingSD.cc index a5ee228530145..89001a42db26f 100644 --- a/SimG4CMS/Forward/src/TimingSD.cc +++ b/SimG4CMS/Forward/src/TimingSD.cc @@ -163,7 +163,8 @@ void TimingSD::getStepInfo(const G4Step* aStep) { if (incidentEnergy > energyCut) { info = cmsTrackInformation(theTrack); info->setStoreTrack(); - info->setIdLastStoredAncestor(theTrack->GetTrackID()); + if (info->idLastStoredAncestor() == theTrack->GetParentID()) + info->setIdLastStoredAncestor(theTrack->GetTrackID()); } if (incidentEnergy > energyHistoryCut) { if (nullptr == info) { diff --git a/SimG4CMS/Muon/src/MuonSensitiveDetector.cc b/SimG4CMS/Muon/src/MuonSensitiveDetector.cc index 6a9866ecccf18..7dee80eccac1a 100644 --- a/SimG4CMS/Muon/src/MuonSensitiveDetector.cc +++ b/SimG4CMS/Muon/src/MuonSensitiveDetector.cc @@ -249,7 +249,8 @@ void MuonSensitiveDetector::createHit(const G4Step* aStep) { if (thePabs > ePersistentCutGeV_ || (thePID == 13 && allMuonsPersistent_)) { TrackInformation* info = cmsTrackInformation(theTrack); info->setStoreTrack(); - info->setIdLastStoredAncestor(theTrack->GetTrackID()); + if (info->idLastStoredAncestor() == theTrack->GetParentID()) + info->setIdLastStoredAncestor(theTrack->GetTrackID()); } #ifdef EDM_ML_DEBUG diff --git a/SimG4CMS/Tracker/src/TkAccumulatingSensitiveDetector.cc b/SimG4CMS/Tracker/src/TkAccumulatingSensitiveDetector.cc index d97ba804a0b3b..483e2309a400f 100644 --- a/SimG4CMS/Tracker/src/TkAccumulatingSensitiveDetector.cc +++ b/SimG4CMS/Tracker/src/TkAccumulatingSensitiveDetector.cc @@ -153,7 +153,8 @@ void TkAccumulatingSensitiveDetector::update(const BeginOfTrack* bot) { if (gTrack->GetKineticEnergy() > energyCut) { info = cmsTrackInformation(gTrack); info->setStoreTrack(); - info->setIdLastStoredAncestor(gTrack->GetTrackID()); + if (info->idLastStoredAncestor() == gTrack->GetParentID()) + info->setIdLastStoredAncestor(gTrack->GetTrackID()); } // // Save History? diff --git a/SimG4Core/Application/plugins/OscarMTProducer.cc b/SimG4Core/Application/plugins/OscarMTProducer.cc index 25d985174d794..0035387b9d16d 100644 --- a/SimG4Core/Application/plugins/OscarMTProducer.cc +++ b/SimG4Core/Application/plugins/OscarMTProducer.cc @@ -268,7 +268,7 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) { edm::LogVerbatim("Track") << " " << i << ". " << (*p1)[i].trackId() << ", " << (*p1)[i] << ", " << (*p1)[i].crossedBoundary() << "-> " << (*p1)[i].getIDAtBoundary() << ", " << (*p1)[i].isFromBackScattering() << ", " << (*p1)[i].isPrimary() << "-> " - << (*p1)[i].getPrimaryID(); + << (*p1)[i].getPrimaryOrLastStoredID(); } } } diff --git a/SimG4Core/CustomPhysics/plugins/DBremWatcher.cc b/SimG4Core/CustomPhysics/plugins/DBremWatcher.cc index 04b4131a6edde..6c9580852e18d 100644 --- a/SimG4Core/CustomPhysics/plugins/DBremWatcher.cc +++ b/SimG4Core/CustomPhysics/plugins/DBremWatcher.cc @@ -105,7 +105,8 @@ void DBremWatcher::update(const BeginOfTrack* trk) { if (std::find(pdgs_.begin(), pdgs_.end(), pdg) != pdgs_.end()) { //Found an A' trkInfo->setStoreTrack(); - trkInfo->setIdLastStoredAncestor(theTrack->GetTrackID()); + if (trkInfo->idLastStoredAncestor() == theTrack->GetParentID()) + trkInfo->setIdLastStoredAncestor(theTrack->GetTrackID()); VertexPos = Vpos; aPrimeTraj = theTrack->GetMomentum(); LogDebug("DBremWatcher") << "Save SimTrack the Track " << theTrack->GetTrackID() << " Type " @@ -137,7 +138,8 @@ void DBremWatcher::update(const EndOfTrack* trk) { if (std::find(pdgs_.begin(), pdgs_.end(), pdg) == pdgs_.end() && (theTrack->GetCreatorProcess()->GetProcessName()) == "muDBrem") { trkInfo->setStoreTrack(); - trkInfo->setIdLastStoredAncestor(theTrack->GetTrackID()); + if (trkInfo->idLastStoredAncestor() == theTrack->GetParentID()) + trkInfo->setIdLastStoredAncestor(theTrack->GetTrackID()); } } } diff --git a/SimG4Core/Notification/src/SimTrackManager.cc b/SimG4Core/Notification/src/SimTrackManager.cc index db4ef4006ca23..357384fae7241 100644 --- a/SimG4Core/Notification/src/SimTrackManager.cc +++ b/SimG4Core/Notification/src/SimTrackManager.cc @@ -76,12 +76,12 @@ void SimTrackManager::addTrack(TrackWithHistory* iTrack, const G4Track* track, b auto info = static_cast(track->GetUserInformation()); if (info->isInTrkFromBackscattering()) iTrack->setFromBackScattering(); - // set there for the *non-primary* tracks the genParticle ID associated with the G4Track - // for the primaries this is done in the TrackWithHistory constructor. + // set there for the *non-primary* tracks the G4Track ID of the last stored ancestor + // for the primaries the genparticle id is saved in the TrackWithHistory constructor. // In the constructor of TrackWithHistory the info isPrimary is saved and used // to give -1 if the track is not a primary. if (not iTrack->isPrimary()) - iTrack->setGenParticleID(info->mcTruthID()); + iTrack->setGenParticleID(info->idLastStoredAncestor()); m_trackContainer.push_back(iTrack); const auto& v = track->GetStep()->GetPostStepPoint()->GetPosition(); std::pair p(iTrack->trackID(), diff --git a/SimGeneral/Debugging/plugins/CaloParticleDebugger.cc b/SimGeneral/Debugging/plugins/CaloParticleDebugger.cc index 4de3d03e99d9a..5074be7412528 100644 --- a/SimGeneral/Debugging/plugins/CaloParticleDebugger.cc +++ b/SimGeneral/Debugging/plugins/CaloParticleDebugger.cc @@ -164,7 +164,7 @@ void CaloParticleDebugger::analyze(const edm::Event& iEvent, const edm::EventSet LogVerbatim("CaloParticleDebuggerSimTracks") << i << "\t" << t.trackId() << "\t" << t << " Crossed Boundary: " << t.crossedBoundary() << " Vtx: " << t.vertIndex() << " isFromBackScattering: " << t.isFromBackScattering() - << " isPrimary: " << t.isPrimary() << " ParentID: " << t.getPrimaryID() + << " isPrimary: " << t.isPrimary() << " ParentID: " << t.getPrimaryOrLastStoredID() << " Position Boundary: " << t.getPositionAtBoundary() << " Momentum Boundary: " << t.getMomentumAtBoundary() << " Momemtum Origin: " << t.momentum(); trackid_to_track_index[t.trackId()] = i; @@ -232,10 +232,11 @@ void CaloParticleDebugger::analyze(const edm::Event& iEvent, const edm::EventSet << "\n\n" << i << "\tType: " << simcl.pdgId() << "\tEnergy: " << simcl.energy() << "\tKey: " << i; // << simcl ; auto const& simtrack = simcl.g4Tracks()[0]; - LogVerbatim("CaloParticleDebuggerSimClusters") << "\n Primary GenParticleID: " << simtrack.getPrimaryID() - << "\n Crossed Boundary: " << simtrack.crossedBoundary() - << "\n Position Boundary: " << simtrack.getPositionAtBoundary() - << "\n Momentum Boundary: " << simtrack.getMomentumAtBoundary(); + LogVerbatim("CaloParticleDebuggerSimClusters") + << "\n GenParticleID/ancestor: " << simtrack.getPrimaryOrLastStoredID() + << "\n Crossed Boundary: " << simtrack.crossedBoundary() + << "\n Position Boundary: " << simtrack.getPositionAtBoundary() + << "\n Momentum Boundary: " << simtrack.getMomentumAtBoundary(); if (simClusters_in_CaloParticles.find(simcl.g4Tracks()[0].trackId()) == simClusters_in_CaloParticles.end()) { LogVerbatim("CaloParticleDebuggerSimClusters") << " Orphan SimCluster: " << simtrack.trackId(); }