Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4Event.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4Event class implementation
27//
28// Author: M.Asai, SLAC/JLAB
29// --------------------------------------------------------------------
30
31#include "G4Event.hh"
32#include "G4VVisManager.hh"
33#include "G4VHitsCollection.hh"
34#include "G4VDigiCollection.hh"
35#include "G4ios.hh"
36#include "G4SubEvent.hh"
37
39{
40 G4ThreadLocalStatic G4Allocator<G4Event>* _instance = nullptr;
41 return _instance;
42}
43
45 : eventID(evID)
46{
47}
48
50{
51 G4PrimaryVertex* nextVertex = thePrimaryVertex;
52 while(nextVertex != nullptr)
53 {
54 G4PrimaryVertex* thisVertex = nextVertex;
55 nextVertex = thisVertex->GetNext();
56 thisVertex->ClearNext();
57 delete thisVertex;
58 }
59 thePrimaryVertex = nullptr;
60 delete HC;
61 delete DC;
62 if(trajectoryContainer != nullptr)
63 {
64 trajectoryContainer->clearAndDestroy();
65 delete trajectoryContainer;
66 }
67 delete userInfo;
68 delete randomNumberStatus;
69 delete randomNumberStatusForProcessing;
70
71 // Following G4Exception are temporally issuing JustWarning to delete
72 // unprocessed tracks in sub-events. Once sub-event mechanism is completely
73 // implemented, G4Exception should cause FatalException.
74
75 G4int remainingSE = 0;
76 for(auto& sem : fSubEvtStackMap)
77 {
78 if((sem.second!=nullptr)&&!(sem.second->empty()))
79 {
80 remainingSE += sem.second->size();
81 for(auto& se : *(sem.second))
82 {
83 se->clearAndDestroy();
84 }
85 sem.second->clear();
86 }
87 }
88 if(remainingSE>0)
89 {
91 ed << "Deleting G4Event (id:" << eventID << ") that still has "
92 << remainingSE << " sub-events un-processed.";
93 G4Exception("G4Event::~G4Event()","SubEvt0001",JustWarning,ed);
94 }
95
96 if(!(fSubEvtVector.empty()))
97 {
99 ed << "Deleting G4Event (id:" << eventID << ") that has "
100 << fSubEvtVector.size() << " sub-events still processing.";
101 G4Exception("G4Event::~G4Event()","SubEvt0001",JustWarning,ed);
102 for(auto& se : fSubEvtVector)
103 {
104 se->clearAndDestroy();
105 delete se;
106 }
107 }
108}
109
111{
112 return ( eventID == right.eventID );
113}
114
116{
117 return ( eventID != right.eventID );
118}
119
120void G4Event::Print() const
121{
122 G4cout << "G4Event " << eventID << G4endl;
123}
124
125void G4Event::Draw() const
126{
128 if(pVVisManager == nullptr) return;
129
130 if(trajectoryContainer != nullptr)
131 {
132 std::size_t n_traj = trajectoryContainer->entries();
133 for(std::size_t i=0; i<n_traj; ++i)
134 { (*trajectoryContainer)[i]->DrawTrajectory(); }
135 }
136
137 if(HC != nullptr)
138 {
139 std::size_t n_HC = HC->GetCapacity();
140 for(std::size_t j=0; j<n_HC; ++j)
141 {
142 G4VHitsCollection* VHC = HC->GetHC((G4int)j);
143 if(VHC != nullptr) VHC->DrawAllHits();
144 }
145 }
146
147 if(DC != nullptr)
148 {
149 std::size_t n_DC = DC->GetCapacity();
150 for(std::size_t j=0; j<n_DC; ++j)
151 {
152 G4VDigiCollection* VDC = DC->GetDC((G4int)j);
153 if(VDC != nullptr) VDC->DrawAllDigi();
154 }
155 }
156}
157
159{
160 std::set<G4SubEvent*>* sev = nullptr;
161 auto ses = fSubEvtStackMap.find(ty);
162 if(ses==fSubEvtStackMap.end())
163 {
164 sev = new std::set<G4SubEvent*>;
165 fSubEvtStackMap[ty] = sev;
166 }
167 else
168 { sev = ses->second; }
169 sev->insert(se);
170 return (G4int)sev->size();
171}
172
174{
175 G4SubEvent* se = nullptr;
176 auto ses = fSubEvtStackMap.find(ty);
177 if(ses!=fSubEvtStackMap.end())
178 {
179 auto sev = ses->second;
180 if(!(sev->empty()))
181 {
182 se = sev->extract(sev->begin()).value();
183 SpawnSubEvent(se);
184 }
185 }
186 return se;
187}
188
190{
191 auto ss = fSubEvtVector.find(se);
192 if(ss!=fSubEvtVector.end())
193 {
195 ed << "Sub-event " << se << " of type " << se->GetSubEventType()
196 << " with " << se->GetNTrack() << " tracks has already spawned.";
197 G4Exception("G4Event::SpawnSubEvent","SubEvent9001",
198 FatalException,ed);
199 }
200 fSubEvtVector.insert(se);
201 return (G4int)fSubEvtVector.size();
202}
203
205{
206#ifdef G4_STORE_TRAJECTORY
207 if(se->trajectoryContainer!=nullptr && se->trajectoryContainer->size()>0)
208 {
209 if(trajectoryContainer==nullptr) trajectoryContainer = new G4TrajectoryContainer;
210 for(auto& trj : *(se->trajectoryContainer->GetVector()))
211 { trajectoryContainer->push_back(trj); }
212 }
213#endif
214 // Note:
215 // - scores are merged directly to the scoring manager
216 // - hits collections should be merged by the user event action
217}
218
220{
221 auto ss = fSubEvtVector.find(se);
222 if(ss==fSubEvtVector.end())
223 {
225 ed << "Sub-event " << se << " of type " << se->GetSubEventType()
226 << " with " << se->GetNTrack() << " tracks has never been spawned.";
227 G4Exception("G4Event::TerminateSubEvent","SubEvent9002",
228 FatalException,ed);
229 }
230
231 fSubEvtVector.erase(ss);
232
233 ss = fSubEvtVector.find(se);
234 if(ss!=fSubEvtVector.end())
235 {
237 ed << "Sub-event " << se << " of type " << se->GetSubEventType()
238 << " with " << se->GetNTrack() << " appears more than once. PANIC!";
239 G4Exception("G4Event::TerminateSubEvent","SubEvent9003",
240 FatalException,ed);
241 }
242
243 se->clearAndDestroy();
244 delete se;
245 return (G4int)fSubEvtVector.size();
246}
247
G4Allocator< G4Event > *& anEventAllocator()
Definition G4Event.cc:38
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
G4VDigiCollection * GetDC(G4int i) const
size_t GetCapacity() const
G4int StoreSubEvent(G4int, G4SubEvent *)
Definition G4Event.cc:158
void Print() const
Definition G4Event.cc:120
~G4Event()
Definition G4Event.cc:49
void MergeSubEventResults(const G4Event *se)
Definition G4Event.cc:204
G4Event()=default
G4bool operator!=(const G4Event &right) const
Definition G4Event.cc:115
G4SubEvent * PopSubEvent(G4int)
Definition G4Event.cc:173
G4bool operator==(const G4Event &right) const
Definition G4Event.cc:110
G4int TerminateSubEvent(G4SubEvent *)
Definition G4Event.cc:219
G4int SpawnSubEvent(G4SubEvent *)
Definition G4Event.cc:189
void Draw() const
Definition G4Event.cc:125
G4VHitsCollection * GetHC(G4int i)
G4PrimaryVertex * GetNext() const
G4int GetSubEventType() const
Definition G4SubEvent.hh:61
void clearAndDestroy()
Definition G4SubEvent.cc:40
std::size_t GetNTrack() const
Definition G4SubEvent.hh:62
TrajectoryVector * GetVector() const
void push_back(G4VTrajectory *p)
std::size_t entries() const
virtual void DrawAllDigi()
virtual void DrawAllHits()
static G4VVisManager * GetConcreteInstance()
#define G4ThreadLocalStatic
Definition tls.hh:76