Skip to content

Commit 37a1d46

Browse files
authored
Merge pull request #47549 from civanch/extended_sim_exception
[FULLSIM] Extended SIM exception
2 parents 3a36733 + e6ba53c commit 37a1d46

File tree

7 files changed

+77
-9
lines changed

7 files changed

+77
-9
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef SimG4Core_CMSG4TrackInterface_h
2+
#define SimG4Core_CMSG4TrackInterface_h 1
3+
4+
//
5+
// Package: Application
6+
// Class : SimTrackInterface
7+
//
8+
// 10.03.2025 V.Ivantchenko
9+
//
10+
// An interface between Geant4 and CMSSW
11+
12+
#include "G4ThreadLocalSingleton.hh"
13+
14+
class G4Track;
15+
16+
class CMSG4TrackInterface {
17+
friend class G4ThreadLocalSingleton<CMSG4TrackInterface>;
18+
19+
public:
20+
static CMSG4TrackInterface* instance();
21+
22+
~CMSG4TrackInterface();
23+
24+
const G4Track* getCurrentTrack() { return track_; }
25+
26+
void setCurrentTrack(const G4Track* p) { track_ = p; }
27+
28+
int getThreadID() { return threadID_; }
29+
30+
void setThreadID(int n) { threadID_ = n; }
31+
32+
CMSG4TrackInterface(CMSG4TrackInterface&) = delete;
33+
CMSG4TrackInterface& operator=(const CMSG4TrackInterface& right) = delete;
34+
35+
private:
36+
CMSG4TrackInterface();
37+
38+
static G4ThreadLocal CMSG4TrackInterface* interface_;
39+
40+
const G4Track* track_{nullptr};
41+
int threadID_{0};
42+
};
43+
44+
#endif

SimG4Core/Application/interface/ExceptionHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class ExceptionHandler : public G4VExceptionHandler {
2020
public:
2121
explicit ExceptionHandler(double th, bool tr);
22-
~ExceptionHandler() override;
22+
~ExceptionHandler() override = default;
2323

2424
int operator==(const ExceptionHandler &right) const { return (this == &right); }
2525
int operator!=(const ExceptionHandler &right) const { return (this != &right); }

SimG4Core/Application/interface/TrackingAction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <vector>
1111

1212
class SimTrackManager;
13+
class CMSG4TrackInterface;
1314
class TrackWithHistory;
1415
class BeginOfTrack;
1516
class EndOfTrack;
@@ -31,8 +32,12 @@ class TrackingAction : public G4UserTrackingAction {
3132
SimActivityRegistry::BeginOfTrackSignal m_beginOfTrackSignal;
3233
SimActivityRegistry::EndOfTrackSignal m_endOfTrackSignal;
3334

35+
TrackingAction(TrackingAction&) = delete;
36+
TrackingAction& operator=(const TrackingAction& right) = delete;
37+
3438
private:
3539
SimTrackManager* trackManager_;
40+
CMSG4TrackInterface* interface_;
3641
CMSSteppingVerbose* steppingVerbose_;
3742
const G4Track* g4Track_ = nullptr;
3843
TrackInformation* trkInfo_ = nullptr;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "SimG4Core/Application/interface/CMSG4TrackInterface.h"
2+
3+
G4ThreadLocal CMSG4TrackInterface* CMSG4TrackInterface::interface_ = nullptr;
4+
5+
CMSG4TrackInterface* CMSG4TrackInterface::instance() {
6+
if (nullptr == interface_) {
7+
static G4ThreadLocalSingleton<CMSG4TrackInterface> inst;
8+
interface_ = inst.Instance();
9+
}
10+
return interface_;
11+
}
12+
13+
CMSG4TrackInterface::CMSG4TrackInterface() {};
14+
15+
CMSG4TrackInterface::~CMSG4TrackInterface() {};

SimG4Core/Application/src/ExceptionHandler.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "SimG4Core/Application/interface/ExceptionHandler.h"
2+
#include "SimG4Core/Application/interface/CMSG4TrackInterface.h"
23

34
#include "FWCore/MessageLogger/interface/MessageLogger.h"
45
#include "FWCore/Utilities/interface/Exception.h"
@@ -11,8 +12,6 @@
1112

1213
ExceptionHandler::ExceptionHandler(double th, bool tr) : m_eth(th), m_trace(tr) {}
1314

14-
ExceptionHandler::~ExceptionHandler() {}
15-
1615
bool ExceptionHandler::Notify(const char* exceptionOrigin,
1716
const char* exceptionCode,
1817
G4ExceptionSeverity severity,
@@ -22,12 +21,14 @@ bool ExceptionHandler::Notify(const char* exceptionOrigin,
2221
static const G4String ws_banner = "\n-------- WWWW ------- G4Exception-START -------- WWWW -------\n";
2322
static const G4String we_banner = "\n-------- WWWW -------- G4Exception-END --------- WWWW -------\n";
2423

25-
G4Track* track = G4EventManager::GetEventManager()->GetTrackingManager()->GetTrack();
24+
auto ig4 = CMSG4TrackInterface::instance();
25+
const G4Track* track = ig4->getCurrentTrack();
26+
int id = ig4->getThreadID();
2627
double ekin = m_eth;
2728

2829
std::stringstream message;
2930
message << "*** G4Exception : " << exceptionCode << "\n"
30-
<< " issued by : " << exceptionOrigin << "\n"
31+
<< " issued by : " << exceptionOrigin << " in threadID=" << id << "\n"
3132
<< description;
3233

3334
// part of exception happens outside tracking loop
@@ -61,10 +62,10 @@ bool ExceptionHandler::Notify(const char* exceptionOrigin,
6162
G4String code;
6263
mescode >> code;
6364

64-
// track is killed
65+
// track should be killed, const_cast is allowed in this place
6566
if (ekin < m_eth && (code == "GeomNav0003" || code == "GeomField0003")) {
6667
localSeverity = JustWarning;
67-
track->SetTrackStatus(fStopAndKill);
68+
const_cast<G4Track*>(track)->SetTrackStatus(fStopAndKill);
6869
++m_number;
6970
}
7071

SimG4Core/Application/src/RunManagerMTWorker.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "SimG4Core/Application/interface/CustomUIsessionToFile.h"
1313
#include "SimG4Core/Application/interface/ExceptionHandler.h"
1414
#include "SimG4Core/Application/interface/CMSGDMLWriteStructure.h"
15+
#include "SimG4Core/Application/interface/CMSG4TrackInterface.h"
1516

1617
#include "SimG4Core/Geometry/interface/CustomUIsession.h"
1718

@@ -178,6 +179,7 @@ RunManagerMTWorker::RunManagerMTWorker(const edm::ParameterSet& p, edm::Consumes
178179
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker for the thread " << id;
179180

180181
// Initialize per-thread output
182+
CMSG4TrackInterface::instance()->setThreadID(id);
181183
G4Threading::G4SetThreadId(id);
182184
G4UImanager::GetUIpointer()->SetUpForAThread(id);
183185
auto iPset = p.getUntrackedParameter<edm::ParameterSet>("CustomUIsession");
@@ -194,8 +196,6 @@ RunManagerMTWorker::RunManagerMTWorker(const edm::ParameterSet& p, edm::Consumes
194196
<< "', valid are MessageLogger, MessageLoggerThreadPrefix, FilePerThread";
195197
}
196198
G4UImanager::GetUIpointer()->SetCoutDestination(m_UIsession);
197-
//G4PhysListUtil::InitialiseParameters();
198-
//G4LossTableManager::Instance();
199199

200200
// sensitive detectors
201201
std::vector<std::string> onlySDs = p.getParameter<std::vector<std::string>>("OnlySDs");

SimG4Core/Application/src/TrackingAction.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "SimG4Core/Application/interface/TrackingAction.h"
2+
#include "SimG4Core/Application/interface/CMSG4TrackInterface.h"
23

34
#include "SimG4Core/Notification/interface/CurrentG4Track.h"
45
#include "SimG4Core/Notification/interface/BeginOfTrack.h"
@@ -25,6 +26,7 @@ TrackingAction::TrackingAction(SimTrackManager* stm, CMSSteppingVerbose* sv, con
2526
saveCaloBoundaryInformation_(p.getParameter<bool>("SaveCaloBoundaryInformation")),
2627
ekinMin_(p.getParameter<double>("PersistencyEmin") * CLHEP::GeV),
2728
ekinMinRegion_(p.getParameter<std::vector<double>>("RegionEmin")) {
29+
interface_ = CMSG4TrackInterface::instance();
2830
double eth = p.getParameter<double>("EminFineTrack") * CLHEP::MeV;
2931
if (doFineCalo_ && eth < ekinMin_) {
3032
ekinMin_ = eth;
@@ -39,6 +41,7 @@ TrackingAction::TrackingAction(SimTrackManager* stm, CMSSteppingVerbose* sv, con
3941
void TrackingAction::PreUserTrackingAction(const G4Track* aTrack) {
4042
g4Track_ = aTrack;
4143
currentTrack_ = new TrackWithHistory(aTrack, aTrack->GetParentID());
44+
interface_->setCurrentTrack(aTrack);
4245

4346
BeginOfTrack bt(aTrack);
4447
m_beginOfTrackSignal(&bt);

0 commit comments

Comments
 (0)