5
5
6
6
#include < numeric>
7
7
8
+ G4Mutex OMSimHitManager::mMutex = G4Mutex();
9
+ OMSimHitManager *OMSimHitManager::mInstance = nullptr ;
10
+ G4ThreadLocal OMSimHitManager::ThreadLocalData *OMSimHitManager::mThreadData = nullptr ;
11
+
12
+ /* *
13
+ * @brief Returns the singleton instance of the OMSimHitManager.
14
+ * @return A reference to the singleton instance.
15
+ */
16
+ OMSimHitManager &OMSimHitManager::getInstance ()
17
+ {
18
+ if (!mInstance )
19
+ {
20
+ G4AutoLock lock (&mMutex );
21
+ if (!mInstance )
22
+ {
23
+ mInstance = new OMSimHitManager ();
24
+ }
25
+ }
26
+ return *mInstance ;
27
+ }
28
+
8
29
/* *
9
30
* @brief Applies a given permutation to a container.
10
31
*
@@ -21,7 +42,7 @@ void applyPermutation(std::vector<T> &pVec, const std::vector<std::size_t> &pPer
21
42
for (std::size_t i = 0 ; i < pVec.size (); ++i)
22
43
{
23
44
if (lDone[i])
24
- continue ; // Skip the item if it's already in place.
45
+ continue ; // Skip the item if it's already in place.
25
46
lDone[i] = true ; // Mark the item as placed.
26
47
std::size_t prev_j = i;
27
48
std::size_t j = pPermutation[i];
@@ -66,24 +87,29 @@ void OMSimHitManager::appendHitInfo(
66
87
OMSimPMTResponse::PMTPulse pResponse,
67
88
G4int pModuleNumber)
68
89
{
69
- // Check if the module exists in the map
70
- if (mModuleHits .find (pModuleNumber) == mModuleHits .end ())
90
+ if (!mThreadData )
91
+ {
92
+ mThreadData = new ThreadLocalData ();
93
+ }
94
+
95
+ // Check if the module exists in the thread-local map
96
+ if (mThreadData ->moduleHits .find (pModuleNumber) == mThreadData ->moduleHits .end ())
71
97
{
72
98
// Create a new HitStats for this module
73
- mModuleHits [pModuleNumber] = HitStats ();
99
+ mThreadData -> moduleHits [pModuleNumber] = HitStats ();
74
100
}
75
- G4int lEventID = EventInfoManager::getInstance (). getCurrentEventID () ;
76
- mModuleHits [pModuleNumber] .eventId .push_back (lEventID );
77
- mModuleHits [pModuleNumber] .hitTime .push_back (pGlobalTime);
78
- mModuleHits [pModuleNumber] .flightTime .push_back (pLocalTime);
79
- mModuleHits [pModuleNumber] .pathLenght .push_back (pTrackLength);
80
- mModuleHits [pModuleNumber] .energy .push_back (pEnergy);
81
- mModuleHits [pModuleNumber] .PMTnr .push_back (pPMTHitNumber);
82
- mModuleHits [pModuleNumber] .direction .push_back (pMomentumDirection);
83
- mModuleHits [pModuleNumber] .globalPosition .push_back (pGlobalPos);
84
- mModuleHits [pModuleNumber] .localPosition .push_back (pLocalPos);
85
- mModuleHits [pModuleNumber] .generationDetectionDistance .push_back (pDistance);
86
- mModuleHits [pModuleNumber] .PMTresponse .push_back (pResponse);
101
+ auto &moduleHits = mThreadData -> moduleHits [pModuleNumber] ;
102
+ moduleHits .eventId .push_back (EventInfoManager::getInstance (). getCurrentEventID () );
103
+ moduleHits .hitTime .push_back (pGlobalTime);
104
+ moduleHits .flightTime .push_back (pLocalTime);
105
+ moduleHits .pathLenght .push_back (pTrackLength);
106
+ moduleHits .energy .push_back (pEnergy);
107
+ moduleHits .PMTnr .push_back (pPMTHitNumber);
108
+ moduleHits .direction .push_back (pMomentumDirection);
109
+ moduleHits .globalPosition .push_back (pGlobalPos);
110
+ moduleHits .localPosition .push_back (pLocalPos);
111
+ moduleHits .generationDetectionDistance .push_back (pDistance);
112
+ moduleHits .PMTresponse .push_back (pResponse);
87
113
}
88
114
89
115
/* *
@@ -113,6 +139,11 @@ HitStats OMSimHitManager::getHitsOfModule(int pModuleIndex)
113
139
void OMSimHitManager::reset ()
114
140
{
115
141
mModuleHits .clear ();
142
+
143
+ if (mThreadData )
144
+ {
145
+ mThreadData ->moduleHits .clear ();
146
+ }
116
147
}
117
148
118
149
/* *
@@ -123,6 +154,7 @@ void OMSimHitManager::reset()
123
154
std::vector<double > OMSimHitManager::countHits (int pModuleIndex)
124
155
{
125
156
log_debug (" Counting number of detected photons in module with index {}" , pModuleIndex);
157
+ if (!mDataWasMerged ){mergeThreadData ();};
126
158
HitStats lHitsOfModule = mModuleHits [pModuleIndex];
127
159
G4int lNumberPMTs = mNumPMTs [pModuleIndex];
128
160
@@ -187,6 +219,8 @@ void OMSimHitManager::sortHitStatsByTime(HitStats &lHits)
187
219
std::vector<int > OMSimHitManager::calculateMultiplicity (const G4double pTimeWindow, int pModuleIndex)
188
220
{
189
221
log_debug (" Calculating multiplicity in time window {} for module with index" , pTimeWindow, pModuleIndex);
222
+ if (!mDataWasMerged ){mergeThreadData ();};
223
+
190
224
HitStats lHitsOfModule = mModuleHits [pModuleIndex];
191
225
G4int lNumberPMTs = mNumPMTs [pModuleIndex];
192
226
@@ -230,4 +264,30 @@ std::vector<int> OMSimHitManager::calculateMultiplicity(const G4double pTimeWind
230
264
lMultiplicity[lCurrentSum - 1 ] += 1 ;
231
265
}
232
266
return lMultiplicity;
267
+ }
268
+
269
+ void OMSimHitManager::mergeThreadData ()
270
+ {
271
+ G4AutoLock lock (&mMutex );
272
+ if (mThreadData )
273
+ {
274
+ for (const auto &[moduleIndex, hits] : mThreadData ->moduleHits )
275
+ {
276
+ auto &globalHits = mModuleHits [moduleIndex];
277
+ globalHits.eventId .insert (globalHits.eventId .end (), hits.eventId .begin (), hits.eventId .end ());
278
+ globalHits.hitTime .insert (globalHits.hitTime .end (), hits.hitTime .begin (), hits.hitTime .end ());
279
+ globalHits.flightTime .insert (globalHits.flightTime .end (), hits.flightTime .begin (), hits.flightTime .end ());
280
+ globalHits.pathLenght .insert (globalHits.pathLenght .end (), hits.pathLenght .begin (), hits.pathLenght .end ());
281
+ globalHits.energy .insert (globalHits.energy .end (), hits.energy .begin (), hits.energy .end ());
282
+ globalHits.PMTnr .insert (globalHits.PMTnr .end (), hits.PMTnr .begin (), hits.PMTnr .end ());
283
+ globalHits.direction .insert (globalHits.direction .end (), hits.direction .begin (), hits.direction .end ());
284
+ globalHits.localPosition .insert (globalHits.localPosition .end (), hits.localPosition .begin (), hits.localPosition .end ());
285
+ globalHits.globalPosition .insert (globalHits.globalPosition .end (), hits.globalPosition .begin (), hits.globalPosition .end ());
286
+ globalHits.generationDetectionDistance .insert (globalHits.generationDetectionDistance .end (), hits.generationDetectionDistance .begin (), hits.generationDetectionDistance .end ());
287
+ globalHits.PMTresponse .insert (globalHits.PMTresponse .end (), hits.PMTresponse .begin (), hits.PMTresponse .end ());
288
+ }
289
+ delete mThreadData ;
290
+ mThreadData = nullptr ;
291
+ }
292
+ mDataWasMerged = true ;
233
293
}
0 commit comments