Skip to content

Commit

Permalink
Merge pull request #41022 from civanch/cleanup_and_preparation_for_si…
Browse files Browse the repository at this point in the history
…m_extentions

Cleanup and preparation for sim extentions
  • Loading branch information
cmsbuild authored Mar 13, 2023
2 parents 33b1318 + 425d713 commit df27e39
Show file tree
Hide file tree
Showing 22 changed files with 142 additions and 198 deletions.
5 changes: 2 additions & 3 deletions SimG4CMS/Calo/src/CaloSD.cc
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ unsigned int CaloSD::findBoundaryCrossingParent(const G4Track* track, bool markA
edm::LogVerbatim("DoFineCalo") << "Track " << id << " crosses boundary itself";
#endif
boundaryCrossingParentMap_[id] = id;
trkInfo->storeTrack(true);
trkInfo->setStoreTrack();
return id;
}
// Else, traverse the history of the track
Expand Down Expand Up @@ -613,8 +613,7 @@ CaloG4Hit* CaloSD::createNewHit(const G4Step* aStep, const G4Track* theTrack) {
<< " save: " << (etrack >= energyCut || forceSave);
#endif
if (etrack >= energyCut || forceSave) {
trkInfo->storeTrack(true);
trkInfo->putInHistory();
trkInfo->setStoreTrack();
}
} else {
TrackWithHistory* trkh = tkMap[currentID.trackID()];
Expand Down
4 changes: 2 additions & 2 deletions SimG4CMS/Forward/src/TimingSD.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ void TimingSD::getStepInfo(const G4Step* aStep) {
TrackInformation* info = nullptr;
if (incidentEnergy > energyCut) {
info = cmsTrackInformation(theTrack);
info->storeTrack(true);
info->setStoreTrack();
}
if (incidentEnergy > energyHistoryCut) {
if (!info) {
if (nullptr == info) {
info = cmsTrackInformation(theTrack);
}
info->putInHistory();
Expand Down
2 changes: 1 addition & 1 deletion SimG4CMS/Muon/src/MuonSensitiveDetector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ void MuonSensitiveDetector::createHit(const G4Step* aStep) {
//---VI - in parameters cut in energy is declared but applied to momentum
if (thePabs > ePersistentCutGeV_ || (thePID == 13 && allMuonsPersistent_)) {
TrackInformation* info = cmsTrackInformation(theTrack);
info->storeTrack(true);
info->setStoreTrack();
}

#ifdef EDM_ML_DEBUG
Expand Down
4 changes: 2 additions & 2 deletions SimG4CMS/Tracker/src/TkAccumulatingSensitiveDetector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ void TkAccumulatingSensitiveDetector::update(const BeginOfTrack* bot) {
TrackInformation* info = nullptr;
if (gTrack->GetKineticEnergy() > energyCut) {
info = cmsTrackInformation(gTrack);
info->storeTrack(true);
info->setStoreTrack();
}
//
// Save History?
//
if (gTrack->GetKineticEnergy() > energyHistoryCut) {
if (!info) {
if (nullptr == info) {
info = cmsTrackInformation(gTrack);
}
info->putInHistory();
Expand Down
11 changes: 1 addition & 10 deletions SimG4Core/Application/interface/EventAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CMSSteppingVerbose;
class EventAction : public G4UserEventAction {
public:
explicit EventAction(const edm::ParameterSet& ps, SimRunInterface*, SimTrackManager*, CMSSteppingVerbose*);
~EventAction() override;
~EventAction() override = default;

void BeginOfEventAction(const G4Event* evt) override;
void EndOfEventAction(const G4Event* evt) override;
Expand All @@ -32,17 +32,8 @@ class EventAction : public G4UserEventAction {

inline const TrackContainer* trackContainer() const { return m_trackManager->trackContainer(); }

inline void addTrack(TrackWithHistory* iTrack, bool inHistory, bool withAncestor) {
m_trackManager->addTrack(iTrack, inHistory, withAncestor);
}

bool trackExists(unsigned int id) const { return m_trackManager->trackExists(id); }
TrackWithHistory* getTrackByID(unsigned int id) const { return m_trackManager->getTrackByID(id); }

void addTkCaloStateInfo(uint32_t t, const std::pair<math::XYZVectorD, math::XYZTLorentzVectorD>& p);

inline void prepareForNewPrimary() { m_trackManager->cleanTracksWithHistory(); }

SimActivityRegistry::BeginOfEventSignal m_beginOfEventSignal;
SimActivityRegistry::EndOfEventSignal m_endOfEventSignal;

Expand Down
17 changes: 9 additions & 8 deletions SimG4Core/Application/interface/SteppingAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <string>
#include <vector>

class EventAction;
class SimTrackManager;
class CMSSteppingVerbose;

enum TrackStatus {
Expand All @@ -31,8 +31,8 @@ enum TrackStatus {

class SteppingAction : public G4UserSteppingAction {
public:
explicit SteppingAction(EventAction* ea, const edm::ParameterSet& ps, const CMSSteppingVerbose*, bool hasW);
~SteppingAction() override;
explicit SteppingAction(SimTrackManager*, const CMSSteppingVerbose*, const edm::ParameterSet&, bool hasW);
~SteppingAction() override = default;

void UserSteppingAction(const G4Step* aStep) final;

Expand All @@ -47,8 +47,9 @@ class SteppingAction : public G4UserSteppingAction {
bool isLowEnergy(const G4LogicalVolume*, const G4Track*) const;
void PrintKilledTrack(const G4Track*, const TrackStatus&) const;

EventAction* eventAction_;
const G4VPhysicalVolume *tracker, *calo;
SimTrackManager* trackManager_;
const G4VPhysicalVolume* tracker{nullptr};
const G4VPhysicalVolume* calo{nullptr};
const CMSSteppingVerbose* steppingVerbose;
double theCriticalEnergyForVacuum;
double theCriticalDensity;
Expand All @@ -60,11 +61,11 @@ class SteppingAction : public G4UserSteppingAction {
unsigned int numberEkins;
unsigned int numberPart;
unsigned int ndeadRegions;
unsigned int nWarnings;
unsigned int nWarnings{0};
G4int maxNumberOfSteps;

bool initialized;
bool killBeamPipe;
bool initialized{false};
bool killBeamPipe{false};
bool hasWatcher;

std::vector<double> maxTrackTimes, ekinMins;
Expand Down
9 changes: 5 additions & 4 deletions SimG4Core/Application/interface/TrackingAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "G4UserTrackingAction.hh"

class EventAction;
class SimTrackManager;
class TrackWithHistory;
class BeginOfTrack;
class EndOfTrack;
Expand All @@ -15,8 +15,8 @@ class TrackInformation;

class TrackingAction : public G4UserTrackingAction {
public:
explicit TrackingAction(EventAction* ea, const edm::ParameterSet& ps, CMSSteppingVerbose*);
~TrackingAction() override;
explicit TrackingAction(SimTrackManager*, CMSSteppingVerbose*, const edm::ParameterSet& ps);
~TrackingAction() override = default;

void PreUserTrackingAction(const G4Track* aTrack) override;
void PostUserTrackingAction(const G4Track* aTrack) override;
Expand All @@ -29,11 +29,12 @@ class TrackingAction : public G4UserTrackingAction {
SimActivityRegistry::EndOfTrackSignal m_endOfTrackSignal;

private:
EventAction* eventAction_;
SimTrackManager* trackManager_;
CMSSteppingVerbose* steppingVerbose_;
const G4Track* g4Track_ = nullptr;
TrackInformation* trkInfo_ = nullptr;
TrackWithHistory* currentTrack_ = nullptr;
int endPrintTrackID_;
bool checkTrack_;
bool doFineCalo_;
bool saveCaloBoundaryInformation_;
Expand Down
1 change: 1 addition & 0 deletions SimG4Core/Application/python/g4SimHits_cfi.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@
common_MCtruth,
DetailedTiming = cms.untracked.bool(False),
CheckTrack = cms.untracked.bool(False),
EndPrintTrackID = cms.int32(0)
),
SteppingAction = cms.PSet(
common_maximum_time,
Expand Down
11 changes: 3 additions & 8 deletions SimG4Core/Application/src/EventAction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ EventAction::EventAction(const edm::ParameterSet& p,
m_trackManager->setCollapsePrimaryVertices(p.getParameter<bool>("CollapsePrimaryVertices"));
}

EventAction::~EventAction() {}

void EventAction::BeginOfEventAction(const G4Event* anEvent) {
m_trackManager->reset();

Expand All @@ -37,7 +35,7 @@ void EventAction::BeginOfEventAction(const G4Event* anEvent) {
}

if (nullptr != m_SteppingVerbose) {
m_SteppingVerbose->BeginOfEvent(anEvent);
m_SteppingVerbose->beginOfEvent(anEvent);
}
}

Expand All @@ -60,16 +58,13 @@ void EventAction::EndOfEventAction(const G4Event* anEvent) {

m_trackManager->storeTracks(m_runInterface->simEvent());

// dispatch now end of event, and only then delete tracks...
// dispatch now end of event
EndOfEvent e(anEvent);
m_endOfEventSignal(&e);

// delete transient objects
m_trackManager->deleteTracks();
m_trackManager->cleanTkCaloStateInfoMap();
}

void EventAction::addTkCaloStateInfo(uint32_t t, const std::pair<math::XYZVectorD, math::XYZTLorentzVectorD>& p) {
m_trackManager->addTkCaloStateInfo(t, p);
}

void EventAction::abortEvent() { m_runInterface->abortEvent(); }
1 change: 0 additions & 1 deletion SimG4Core/Application/src/RunManagerMT.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "G4MTRunManagerKernel.hh"
#include "G4UImanager.hh"

#include "G4EventManager.hh"
#include "G4Run.hh"
#include "G4Event.hh"
#include "G4TransportationManager.hh"
Expand Down
14 changes: 8 additions & 6 deletions SimG4Core/Application/src/RunManagerMTWorker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "SimG4Core/Notification/interface/SimActivityRegistry.h"
#include "SimG4Core/Notification/interface/BeginOfJob.h"
#include "SimG4Core/Notification/interface/CMSSteppingVerbose.h"
#include "SimG4Core/Notification/interface/SimTrackManager.h"
#include "SimG4Core/Watcher/interface/SimWatcherFactory.h"

#include "SimG4Core/Geometry/interface/DDDWorld.h"
Expand Down Expand Up @@ -120,7 +121,7 @@ struct RunManagerMTWorker::TLSData {
std::vector<SensitiveCaloDetector*> sensCaloDets;
std::vector<std::shared_ptr<SimWatcher>> watchers;
std::vector<std::shared_ptr<SimProducer>> producers;
//G4Run can only be deleted if there is a G4RunManager
// G4Run can only be deleted if there is a G4RunManager
// on the thread where the G4Run is being deleted,
// else it causes a segmentation fault
G4Run* currentRun = nullptr;
Expand All @@ -131,15 +132,15 @@ struct RunManagerMTWorker::TLSData {

TLSData() {}

~TLSData() {}
~TLSData() = default;
};

//This can not be a smart pointer since we must delete some of the members
// This can not be a smart pointer since we must delete some of the members
// before leaving main() else we get a segmentation fault caused by accessing
// other 'singletons' after those singletons have been deleted. Instead we
// atempt to delete all TLS at RunManagerMTWorker destructor. If that fails for
// some reason, it is better to leak than cause a crash.
//thread_local RunManagerMTWorker::TLSData* RunManagerMTWorker::m_tls{nullptr};
// thread_local RunManagerMTWorker::TLSData* RunManagerMTWorker::m_tls{nullptr};

RunManagerMTWorker::RunManagerMTWorker(const edm::ParameterSet& iConfig, edm::ConsumesCollector&& iC)
: m_generator(iConfig.getParameter<edm::ParameterSet>("Generator")),
Expand Down Expand Up @@ -397,12 +398,13 @@ void RunManagerMTWorker::initializeUserActions() {
Connect(userEventAction);
eventManager->SetUserAction(userEventAction);

TrackingAction* userTrackingAction = new TrackingAction(userEventAction, m_pTrackingAction, m_sVerbose.get());
TrackingAction* userTrackingAction =
new TrackingAction(m_tls->trackManager.get(), m_sVerbose.get(), m_pTrackingAction);
Connect(userTrackingAction);
eventManager->SetUserAction(userTrackingAction);

SteppingAction* userSteppingAction =
new SteppingAction(userEventAction, m_pSteppingAction, m_sVerbose.get(), m_hasWatchers);
new SteppingAction(m_tls->trackManager.get(), m_sVerbose.get(), m_pSteppingAction, m_hasWatchers);
Connect(userSteppingAction);
eventManager->SetUserAction(userSteppingAction);

Expand Down
2 changes: 1 addition & 1 deletion SimG4Core/Application/src/StackingAction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ G4ClassificationOfNewTrack StackingAction::ClassifyNewTrack(const G4Track* aTrac
}
}
if (nullptr != steppingVerbose) {
steppingVerbose->StackFilled(aTrack, (classification == fKill));
steppingVerbose->stackFilled(aTrack, (classification == fKill));
}
return classification;
}
Expand Down
44 changes: 11 additions & 33 deletions SimG4Core/Application/src/SteppingAction.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "SimG4Core/Application/interface/SteppingAction.h"
#include "SimG4Core/Application/interface/EventAction.h"

#include "SimG4Core/Notification/interface/SimTrackManager.h"
#include "SimG4Core/Notification/interface/CMSSteppingVerbose.h"

#include "G4LogicalVolumeStore.hh"
Expand All @@ -14,15 +15,8 @@

//#define DebugLog

SteppingAction::SteppingAction(EventAction* e, const edm::ParameterSet& p, const CMSSteppingVerbose* sv, bool hasW)
: eventAction_(e),
tracker(nullptr),
calo(nullptr),
steppingVerbose(sv),
nWarnings(0),
initialized(false),
killBeamPipe(false),
hasWatcher(hasW) {
SteppingAction::SteppingAction(SimTrackManager* stm, const CMSSteppingVerbose* sv, const edm::ParameterSet& p, bool hasW)
: trackManager_(stm), steppingVerbose(sv), hasWatcher(hasW) {
theCriticalEnergyForVacuum = (p.getParameter<double>("CriticalEnergyForVacuum") * CLHEP::MeV);
if (0.0 < theCriticalEnergyForVacuum) {
killBeamPipe = true;
Expand Down Expand Up @@ -85,14 +79,11 @@ SteppingAction::SteppingAction(EventAction* e, const edm::ParameterSet& p, const
}
}

SteppingAction::~SteppingAction() {}

void SteppingAction::UserSteppingAction(const G4Step* aStep) {
if (!initialized) {
initialized = initPointer();
}

//if(hasWatcher) { m_g4StepSignal(aStep); }
m_g4StepSignal(aStep);

G4Track* theTrack = aStep->GetTrack();
Expand All @@ -111,23 +102,10 @@ void SteppingAction::UserSteppingAction(const G4Step* aStep) {
const G4StepPoint* preStep = aStep->GetPreStepPoint();
const G4StepPoint* postStep = aStep->GetPostStepPoint();

// NaN energy deposit
if (edm::isNotFinite(aStep->GetTotalEnergyDeposit())) {
tstat = sEnergyDepNaN;
if (nWarnings < 5) {
++nWarnings;
edm::LogWarning("SimG4CoreApplication")
<< "Track #" << theTrack->GetTrackID() << " " << theTrack->GetDefinition()->GetParticleName()
<< " E(MeV)= " << preStep->GetKineticEnergy() / MeV << " Nstep= " << theTrack->GetCurrentStepNumber()
<< " is killed due to edep=NaN inside PV: " << preStep->GetPhysicalVolume()->GetName() << " at "
<< theTrack->GetPosition() << " StepLen(mm)= " << aStep->GetStepLength();
}
}

// the track is killed by the process
if (tstat == sKilledByProcess) {
if (nullptr != steppingVerbose) {
steppingVerbose->NextStep(aStep, fpSteppingManager, false);
steppingVerbose->nextStep(aStep, fpSteppingManager, false);
}
return;
}
Expand Down Expand Up @@ -191,10 +169,10 @@ void SteppingAction::UserSteppingAction(const G4Step* aStep) {
(postStep->GetMomentum()).z(),
postStep->GetTotalEnergy());

// record intersection
uint32_t id = theTrack->GetTrackID();

std::pair<math::XYZVectorD, math::XYZTLorentzVectorD> p(pos, mom);
eventAction_->addTkCaloStateInfo(id, p);
trackManager_->addTkCaloStateInfo(id, p);
}
} else {
theTrack->SetTrackStatus(fStopAndKill);
Expand All @@ -204,7 +182,7 @@ void SteppingAction::UserSteppingAction(const G4Step* aStep) {
#endif
}
if (nullptr != steppingVerbose) {
steppingVerbose->NextStep(aStep, fpSteppingManager, isKilled);
steppingVerbose->nextStep(aStep, fpSteppingManager, isKilled);
}
}

Expand All @@ -229,11 +207,11 @@ bool SteppingAction::initPointer() {
const G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
for (auto const& pvcite : *pvs) {
const G4String& pvname = pvcite->GetName();
if (pvname == "Tracker" || pvname == "tracker:Tracker_1")
if (pvname == "Tracker" || pvname == "tracker:Tracker_1") {
tracker = pvcite;
else if (pvname == "CALO" || pvname == "caloBase:CALO_1")
} else if (pvname == "CALO" || pvname == "caloBase:CALO_1") {
calo = pvcite;

}
if (tracker && calo)
break;
}
Expand Down
Loading

0 comments on commit df27e39

Please sign in to comment.