From 7c3287f15b88223c0fa3d7244af47e47c9772657 Mon Sep 17 00:00:00 2001 From: Fabio Cossutti Date: Tue, 8 Aug 2023 11:15:04 +0200 Subject: [PATCH 1/2] Define PSimHit offset and propagate to MTD hit categories, add exception for track Ids exceeding it --- SimDataFormats/TrackingHit/interface/PSimHit.h | 11 +++++++++++ SimG4CMS/Forward/BuildFile.xml | 1 + SimG4CMS/Forward/interface/MtdSD.h | 6 +++--- SimG4CMS/Forward/src/MtdSD.cc | 7 ++++--- SimG4Core/Application/test/SimHitCaloHitDumper.cc | 3 ++- SimG4Core/Notification/BuildFile.xml | 1 + SimG4Core/Notification/src/SimTrackManager.cc | 5 +++++ 7 files changed, 27 insertions(+), 7 deletions(-) diff --git a/SimDataFormats/TrackingHit/interface/PSimHit.h b/SimDataFormats/TrackingHit/interface/PSimHit.h index a5994e8ceb05f..df5441a7a3581 100644 --- a/SimDataFormats/TrackingHit/interface/PSimHit.h +++ b/SimDataFormats/TrackingHit/interface/PSimHit.h @@ -14,6 +14,8 @@ class TrackingSlaveSD; // for friend declaration only class PSimHit { public: + static constexpr unsigned int k_tidOffset = 200000000; + PSimHit() : theDetUnitId(0) {} PSimHit(const Local3DPoint& entry, @@ -105,6 +107,15 @@ class PSimHit { */ unsigned int trackId() const { return theTrackId; } + /** In case te SimTrack ID is incremented by the k_tidOffset for hit category definition, this + * methods returns the original theTrackId value directly. + */ + unsigned int originalTrackId() const { return (theTrackId > k_tidOffset) ? theTrackId % k_tidOffset : theTrackId; } + + unsigned int offsetTrackId() const { return (theTrackId > k_tidOffset) ? theTrackId / k_tidOffset : theTrackId; } + + static unsigned int addTrackIdOffset(unsigned int tId, unsigned int offset) { return offset * k_tidOffset + tId; } + EncodedEventId eventId() const { return theEventId; } void setEventId(EncodedEventId e) { theEventId = e; } diff --git a/SimG4CMS/Forward/BuildFile.xml b/SimG4CMS/Forward/BuildFile.xml index d7e856506a070..96bf0c7119f3c 100644 --- a/SimG4CMS/Forward/BuildFile.xml +++ b/SimG4CMS/Forward/BuildFile.xml @@ -8,6 +8,7 @@ + diff --git a/SimG4CMS/Forward/interface/MtdSD.h b/SimG4CMS/Forward/interface/MtdSD.h index 46830841d2653..c035479bcaa8b 100644 --- a/SimG4CMS/Forward/interface/MtdSD.h +++ b/SimG4CMS/Forward/interface/MtdSD.h @@ -28,9 +28,9 @@ class MtdSD : public TimingSD { int getTrackID(const G4Track *) override; private: - static constexpr unsigned int k_idsecOffset = 100000000; - static constexpr unsigned int k_idloopOffset = 200000000; - static constexpr unsigned int k_idFromCaloOffset = 300000000; + static constexpr unsigned int k_idsecOffset = 1; + static constexpr unsigned int k_idloopOffset = 2; + static constexpr unsigned int k_idFromCaloOffset = 3; double energyCut; double energyHistoryCut; diff --git a/SimG4CMS/Forward/src/MtdSD.cc b/SimG4CMS/Forward/src/MtdSD.cc index 1c42dee56aefc..c0908795d803c 100644 --- a/SimG4CMS/Forward/src/MtdSD.cc +++ b/SimG4CMS/Forward/src/MtdSD.cc @@ -8,6 +8,7 @@ #include "Geometry/MTDCommonData/interface/BTLNumberingScheme.h" #include "Geometry/MTDCommonData/interface/ETLNumberingScheme.h" #include "DataFormats/ForwardDetId/interface/MTDDetId.h" +#include "SimDataFormats/TrackingHit/interface/PSimHit.h" #include "G4Track.hh" #include "G4Step.hh" @@ -108,11 +109,11 @@ int MtdSD::getTrackID(const G4Track* aTrack) { if (rname == "FastTimerRegionSensBTL") { theID = trkInfo->mcTruthID(); if (trkInfo->isExtSecondary() && !trkInfo->isInTrkFromBackscattering()) { - theID += k_idsecOffset; + theID = PSimHit::addTrackIdOffset(theID, k_idsecOffset); } else if (trkInfo->isInTrkFromBackscattering()) { - theID += k_idFromCaloOffset; + theID = PSimHit::addTrackIdOffset(theID, k_idFromCaloOffset); } else if (trkInfo->isBTLlooper()) { - theID += k_idloopOffset; + theID = PSimHit::addTrackIdOffset(theID, k_idloopOffset); } #ifdef EDM_ML_DEBUG edm::LogVerbatim("MtdSim") << "MtdSD: Track ID: " << aTrack->GetTrackID() diff --git a/SimG4Core/Application/test/SimHitCaloHitDumper.cc b/SimG4Core/Application/test/SimHitCaloHitDumper.cc index b628bbf144efb..56b7d828e730d 100644 --- a/SimG4Core/Application/test/SimHitCaloHitDumper.cc +++ b/SimG4Core/Application/test/SimHitCaloHitDumper.cc @@ -371,7 +371,8 @@ void SimHitCaloHitDumper::analyze(const edm::Event& iEvent, const edm::EventSetu edm::LogPrint("SimHitCaloHitDumper") << (*icoll).second << " hits in the event = " << (*icoll).first; edm::LogPrint("SimHitCaloHitDumper") << "\n"; for (int ihit = 0; ihit < (*icoll).first; ++ihit) { - edm::LogPrint("SimHitCaloHitDumper") << theMTDHits[nhit] << " Track Id = " << theMTDHits[nhit].trackId(); + edm::LogPrint("SimHitCaloHitDumper") << theMTDHits[nhit] << " Energy = " << theMTDHits[nhit].energyLoss() + << " Track Id = " << theMTDHits[nhit].trackId(); nhit++; } } diff --git a/SimG4Core/Notification/BuildFile.xml b/SimG4Core/Notification/BuildFile.xml index da854057df8fe..ee41dc0ac9c68 100644 --- a/SimG4Core/Notification/BuildFile.xml +++ b/SimG4Core/Notification/BuildFile.xml @@ -2,6 +2,7 @@ + diff --git a/SimG4Core/Notification/src/SimTrackManager.cc b/SimG4Core/Notification/src/SimTrackManager.cc index ed5a6818f5eea..c9c191e82d0a7 100644 --- a/SimG4Core/Notification/src/SimTrackManager.cc +++ b/SimG4Core/Notification/src/SimTrackManager.cc @@ -19,6 +19,7 @@ #include "SimG4Core/Notification/interface/TmpSimVertex.h" #include "SimG4Core/Notification/interface/TmpSimEvent.h" #include "SimG4Core/Notification/interface/TrackInformation.h" +#include "SimDataFormats/TrackingHit/interface/PSimHit.h" #include "FWCore/MessageLogger/interface/MessageLogger.h" #include "FWCore/Utilities/interface/Exception.h" @@ -162,6 +163,10 @@ void SimTrackManager::reallyStoreTracks() { } } + if (id >= static_cast(PSimHit::k_tidOffset)) { + throw cms::Exception("SimTrackManager::reallyStoreTracks") + << " SimTrack ID " << id << " exceeds maximum allowed by PSimHit identifier" << PSimHit::k_tidOffset; + } TmpSimTrack* g4simtrack = new TmpSimTrack(id, trkH->particleID(), trkH->momentum(), trkH->totalEnergy(), ivertex, ig, pm, spos, smom); g4simtrack->copyCrossedBoundaryVars(trkH); From 49cd3e2a39b5ed0c748d7c0d21d0e22408a063bb Mon Sep 17 00:00:00 2001 From: Fabio Cossutti Date: Wed, 9 Aug 2023 11:37:37 +0200 Subject: [PATCH 2/2] Fix and simplify logic for offset/original tid retrival, add test for new methods in SimHitCaloHitDumper --- SimDataFormats/TrackingHit/interface/PSimHit.h | 4 ++-- SimG4Core/Application/test/SimHitCaloHitDumper.cc | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/SimDataFormats/TrackingHit/interface/PSimHit.h b/SimDataFormats/TrackingHit/interface/PSimHit.h index df5441a7a3581..375c7e0419cd3 100644 --- a/SimDataFormats/TrackingHit/interface/PSimHit.h +++ b/SimDataFormats/TrackingHit/interface/PSimHit.h @@ -110,9 +110,9 @@ class PSimHit { /** In case te SimTrack ID is incremented by the k_tidOffset for hit category definition, this * methods returns the original theTrackId value directly. */ - unsigned int originalTrackId() const { return (theTrackId > k_tidOffset) ? theTrackId % k_tidOffset : theTrackId; } + unsigned int originalTrackId() const { return theTrackId % k_tidOffset; } - unsigned int offsetTrackId() const { return (theTrackId > k_tidOffset) ? theTrackId / k_tidOffset : theTrackId; } + unsigned int offsetTrackId() const { return theTrackId / k_tidOffset; } static unsigned int addTrackIdOffset(unsigned int tId, unsigned int offset) { return offset * k_tidOffset + tId; } diff --git a/SimG4Core/Application/test/SimHitCaloHitDumper.cc b/SimG4Core/Application/test/SimHitCaloHitDumper.cc index 56b7d828e730d..556931cc47854 100644 --- a/SimG4Core/Application/test/SimHitCaloHitDumper.cc +++ b/SimG4Core/Application/test/SimHitCaloHitDumper.cc @@ -371,8 +371,10 @@ void SimHitCaloHitDumper::analyze(const edm::Event& iEvent, const edm::EventSetu edm::LogPrint("SimHitCaloHitDumper") << (*icoll).second << " hits in the event = " << (*icoll).first; edm::LogPrint("SimHitCaloHitDumper") << "\n"; for (int ihit = 0; ihit < (*icoll).first; ++ihit) { - edm::LogPrint("SimHitCaloHitDumper") << theMTDHits[nhit] << " Energy = " << theMTDHits[nhit].energyLoss() - << " Track Id = " << theMTDHits[nhit].trackId(); + edm::LogPrint("SimHitCaloHitDumper") + << theMTDHits[nhit] << " Energy = " << theMTDHits[nhit].energyLoss() + << " tid orig/offset= " << theMTDHits[nhit].originalTrackId() << " " << theMTDHits[nhit].offsetTrackId() + << " Track Id = " << theMTDHits[nhit].trackId(); nhit++; } }