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 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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
 27 //
 28 // Class description:
 29 //
 30 // This is the class which represents an event. A G4Event is constructed and
 31 // deleted by G4RunManager (or its derived class). When a G4Event object is
 32 // passed to G4EventManager, G4Event must have one or more primary verteces
 33 // and primary particle(s) associated to the verteces as an input of
 34 // simulating an event.
 35 // G4Event has trajectories, hits collections, and/or digi collections. 
 36 
 37 // Author: M.Asai, SLAC
 38 // Adding sub-event : M.Asai, JLAB
 39 // --------------------------------------------------------------------
 40 #ifndef G4Event_hh
 41 #define G4Event_hh 1
 42 
 43 #include <set>
 44 #include <map>
 45 
 46 #include "globals.hh"
 47 #include "evtdefs.hh"
 48 #include "G4Allocator.hh"
 49 #include "G4PrimaryVertex.hh"
 50 #include "G4HCofThisEvent.hh"
 51 #include "G4DCofThisEvent.hh"
 52 #include "G4TrajectoryContainer.hh"
 53 #include "G4VUserEventInformation.hh"
 54 
 55 class G4VHitsCollection;
 56 class G4SubEvent;
 57 
 58 class G4Event 
 59 {
 60   public:
 61     G4Event() = default;
 62     explicit G4Event(G4int evID);
 63    ~G4Event();
 64 
 65     G4Event(const G4Event &) = delete;
 66     G4Event& operator=(const G4Event &) = delete;
 67 
 68     inline void *operator new(std::size_t);
 69     inline void operator delete(void* anEvent);
 70 
 71     G4bool operator==(const G4Event& right) const;
 72     G4bool operator!=(const G4Event& right) const;
 73 
 74     void Print() const;
 75       // Print the event ID (starts with zero and increments by one) to G4cout.
 76     void Draw() const;
 77       // Invoke Draw() methods of all stored trajectories, hits, and digits.
 78       // For hits and digits, Draw() methods of the concrete classes must be
 79       // implemented. Otherwise nothing will be drawn.
 80 
 81     inline void SetEventID(G4int i)
 82       { eventID =  i; }
 83     inline void SetHCofThisEvent(G4HCofThisEvent* value)
 84       { HC = value; }
 85     inline void SetDCofThisEvent(G4DCofThisEvent* value)
 86       { DC = value; }
 87     inline void SetTrajectoryContainer(G4TrajectoryContainer* value)
 88       { trajectoryContainer = value; }
 89     inline void SetEventAborted()
 90       { eventAborted = true; }
 91     inline void SetRandomNumberStatus(G4String& st)
 92       {
 93         randomNumberStatus = new G4String(st);
 94         validRandomNumberStatus = true;
 95       }
 96     inline void SetRandomNumberStatusForProcessing(G4String& st)
 97       {
 98         randomNumberStatusForProcessing = new G4String(st);
 99         validRandomNumberStatusForProcessing = true;
100       }
101     inline void KeepTheEvent(G4bool vl=true) const
102       { keepTheEvent = vl; }
103     inline G4bool KeepTheEventFlag() const
104       { return keepTheEvent; }
105     inline G4bool ToBeKept() const
106       {
107         /* TODO (PHASE-II): consider grips for subevents
108         { return keepTheEvent || grips>0 || GetNumberOfRemainingSubEvents()>0; }
109         */
110         return keepTheEvent || GetNumberOfRemainingSubEvents()>0;
111       }
112     inline void KeepForPostProcessing() const
113       { ++grips; }
114     inline void PostProcessingFinished() const
115       {
116         --grips;
117         if (grips<0)
118         {
119           G4Exception("G4Event::Release()", "EVENT91001", JustWarning,
120                       "Number of grips is negative. This cannot be correct.");
121         }
122       }
123     inline G4int GetNumberOfGrips() const
124       { return grips; }
125 
126     inline G4int GetEventID() const
127       { return eventID; }
128 
129     inline void AddPrimaryVertex(G4PrimaryVertex* aPrimaryVertex)
130       {
131         //  This method sets a new primary vertex. This method must be invoked 
132         // exclusively by G4VPrimaryGenerator concrete class.
133 
134         if( thePrimaryVertex == nullptr )
135         { thePrimaryVertex = aPrimaryVertex; }
136         else
137         { thePrimaryVertex->SetNext( aPrimaryVertex ); }
138         ++numberOfPrimaryVertex;
139       }
140 
141     inline G4int GetNumberOfPrimaryVertex() const
142       { return numberOfPrimaryVertex; }
143       // Returns number of primary verteces the G4Event object has.
144 
145     inline G4PrimaryVertex* GetPrimaryVertex(G4int i=0)  const
146       { 
147         if( i == 0 )
148         { return thePrimaryVertex; }
149         if( i > 0 && i < numberOfPrimaryVertex )
150         {
151           G4PrimaryVertex* primaryVertex = thePrimaryVertex;
152           for( G4int j=0; j<i; ++j )
153           {
154             if( primaryVertex == nullptr ) return nullptr; 
155             primaryVertex = primaryVertex->GetNext();
156           }
157           return primaryVertex;
158         }
159         
160         return nullptr;
161       }
162       // Returns i-th primary vertex of the event.
163 
164     inline G4HCofThisEvent* GetHCofThisEvent()  const
165       { return HC; }
166     inline G4DCofThisEvent* GetDCofThisEvent()  const
167       { return DC; }
168     inline G4TrajectoryContainer* GetTrajectoryContainer() const
169       { return trajectoryContainer; }
170       //  These three methods return the pointers to the G4HCofThisEvent
171       // (hits collections of this event), G4DCofThisEvent (digi collections
172       // of this event), and G4TrajectoryContainer (trajectory coonainer),
173       // respectively.
174 
175     inline G4bool IsAborted() const { return eventAborted; }
176       //  Return a boolean which indicates the event has been aborted and thus
177       // it should not be used for analysis.
178 
179     inline void SetUserInformation(G4VUserEventInformation* anInfo)
180       { userInfo = anInfo; }
181     inline G4VUserEventInformation* GetUserInformation() const
182       { return userInfo; }
183       //  Set and Get method of G4VUserEventInformation
184 
185     inline const G4String& GetRandomNumberStatus() const 
186       {
187         if(!validRandomNumberStatus)
188         { G4Exception(
189               "G4Event::GetRandomNumberStatus","Event0701",JustWarning,
190               "Random number status is not available for this event."); }
191         return *randomNumberStatus;
192       }
193     inline const G4String& GetRandomNumberStatusForProcessing() const 
194       {
195         if(!validRandomNumberStatusForProcessing)
196         { G4Exception(
197               "G4Event::GetRandomNumberStatusForProcessing","Event0702",
198               JustWarning,
199               "Random number status is not available for this event."); }
200         return *randomNumberStatusForProcessing;
201       }
202 
203   private:
204 
205     // event ID
206     G4int eventID = 0;      
207 
208     // PrimaryVertex
209     G4PrimaryVertex* thePrimaryVertex = nullptr;
210     G4int numberOfPrimaryVertex = 0;
211 
212     // HitsCollection
213     G4HCofThisEvent* HC = nullptr;
214 
215     // DigiCollection
216     G4DCofThisEvent* DC = nullptr;
217 
218     // TrajectoryContainer
219     G4TrajectoryContainer* trajectoryContainer = nullptr;
220 
221     // Boolean flag which shall be set to true if the event is aborted and 
222     // thus the containing information is not to be used.
223     G4bool eventAborted = false;
224 
225     // UserEventInformation (optional)
226     G4VUserEventInformation* userInfo = nullptr;
227 
228     // Initial random number engine status before primary particle generation
229     G4String* randomNumberStatus = nullptr;
230     G4bool validRandomNumberStatus = false;
231 
232     // Initial random number engine status before event processing
233     G4String* randomNumberStatusForProcessing = nullptr;
234     G4bool validRandomNumberStatusForProcessing = false;
235 
236     // Flag to keep the event until the end of run
237     mutable G4bool keepTheEvent = false;
238     mutable G4int grips = 0;
239 
240   //========================= for sub-event parallelism
241   // following methods should be used only within the master thread
242 
243   public:
244     G4SubEvent* PopSubEvent(G4int);
245       // This method is to be invoked from G4SubEvtRunManager. 
246       // SpawnSubEvent() is internally invoked.
247     G4int TerminateSubEvent(G4SubEvent*);
248       // This method is to be invoked from G4SubEvtRunManager once a sub-event is
249       // fully processed by a worker thread.
250 
251     G4int StoreSubEvent(G4int,G4SubEvent*);
252       // This method is used by G4EventManager to store all the remeining sub-event 
253       // object at the end of processing the event.
254 
255     G4int SpawnSubEvent(G4SubEvent*);
256       // Registering sub-event when it is sent to a worker thread.
257     G4int GetNumberOfRemainingSubEvents() const;
258       // Number of sub-events that are either still waiting to be processed by worker
259       // threads or sent to worker threads but not yet completed
260     inline G4int GetNumberOfCompletedSubEvent() const
261     { return (G4int)fSubEventGarbageBin.size(); }
262 
263     void MergeSubEventResults(const G4Event* se);
264 
265   private:
266   // These containers are for sub-event objects.
267   // - fSubEvtStackMap stores sub-events that are yet to be sent to the worker thread
268   //   it stores sub-events sorted separately to sub-event types
269   // - fSubEvtVector stores sub-events that are sent to worther thread for processing
270   // - fSubEventGarbageBin stores sub-events that have already been processed by worker thread
271   //   these sub-events are deleted at the time the G4Event of the master thread is deleted
272   //   - TODO (PHASE-II): we may need to consider better way to delete sub-events stored in this garbase bin
273   //     at earlier timing to reduce amount of memory used for storing these garbages
274     std::map<G4int,std::set<G4SubEvent*>*> fSubEvtStackMap;
275     std::set<G4SubEvent*> fSubEvtVector;
276     std::set<G4SubEvent*> fSubEventGarbageBin;
277 
278   // motherEvent pointer is non-null for an event created in the worker
279   // thread of sub-event parallelism.
280   // This object and all the contained objects must be deleted by the
281   // responsible worker thread.
282   private:
283     G4Event* motherEvent = nullptr;
284     G4int subEventType = -1;
285 
286   public:
287     void FlagAsSubEvent(G4Event* me, G4int ty)
288     {
289       motherEvent = me;
290       subEventType = ty;
291     }
292     inline G4Event* GetMotherEvent() const
293     { return motherEvent; }
294     inline G4int GetSubEventType() const
295     { return subEventType; }
296 
297   private:
298     mutable G4bool scoresRecorded = false;
299     mutable G4bool eventCompleted = false;
300   public:
301     void ScoresRecorded() const { scoresRecorded = true; }
302     G4bool ScoresAlreadyRecorded() const { return scoresRecorded; }
303     void EventCompleted() const { eventCompleted = true; }
304     G4bool IsEventCompleted() const { return eventCompleted; }
305 };
306 
307 extern G4EVENT_DLL G4Allocator<G4Event>*& anEventAllocator();
308 
309 inline void* G4Event::operator new(std::size_t)
310 { 
311   if (anEventAllocator() == nullptr) 
312   {
313     anEventAllocator() = new G4Allocator<G4Event>;
314   }
315   return (void*)anEventAllocator()->MallocSingle();
316 }
317 
318 inline void G4Event::operator delete(void* anEvent)
319 {
320   anEventAllocator()->FreeSingle((G4Event*)anEvent);
321 }
322 
323 #endif
324