Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/Geant4/G4Event.hh was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // G4Event
0027 //
0028 // Class description:
0029 //
0030 // This is the class which represents an event. A G4Event is constructed and
0031 // deleted by G4RunManager (or its derived class). When a G4Event object is
0032 // passed to G4EventManager, G4Event must have one or more primary verteces
0033 // and primary particle(s) associated to the verteces as an input of
0034 // simulating an event.
0035 // G4Event has trajectories, hits collections, and/or digi collections. 
0036 
0037 // Author: M.Asai, SLAC
0038 // Adding sub-event : M.Asai, JLAB
0039 // --------------------------------------------------------------------
0040 #ifndef G4Event_hh
0041 #define G4Event_hh 1
0042 
0043 #include <set>
0044 #include <map>
0045 
0046 #include "globals.hh"
0047 #include "evtdefs.hh"
0048 #include "G4Allocator.hh"
0049 #include "G4PrimaryVertex.hh"
0050 #include "G4HCofThisEvent.hh"
0051 #include "G4DCofThisEvent.hh"
0052 #include "G4TrajectoryContainer.hh"
0053 #include "G4VUserEventInformation.hh"
0054 
0055 class G4VHitsCollection;
0056 class G4SubEvent;
0057 
0058 class G4Event 
0059 {
0060   public:
0061     G4Event() = default;
0062     explicit G4Event(G4int evID);
0063    ~G4Event();
0064 
0065     G4Event(const G4Event &) = delete;
0066     G4Event& operator=(const G4Event &) = delete;
0067 
0068     inline void *operator new(std::size_t);
0069     inline void operator delete(void* anEvent);
0070 
0071     G4bool operator==(const G4Event& right) const;
0072     G4bool operator!=(const G4Event& right) const;
0073 
0074     void Print() const;
0075       // Print the event ID (starts with zero and increments by one) to G4cout.
0076     void Draw() const;
0077       // Invoke Draw() methods of all stored trajectories, hits, and digits.
0078       // For hits and digits, Draw() methods of the concrete classes must be
0079       // implemented. Otherwise nothing will be drawn.
0080 
0081     inline void SetEventID(G4int i)
0082       { eventID =  i; }
0083     inline void SetHCofThisEvent(G4HCofThisEvent* value)
0084       { HC = value; }
0085     inline void SetDCofThisEvent(G4DCofThisEvent* value)
0086       { DC = value; }
0087     inline void SetTrajectoryContainer(G4TrajectoryContainer* value)
0088       { trajectoryContainer = value; }
0089     inline void SetEventAborted()
0090       { eventAborted = true; }
0091     inline void SetRandomNumberStatus(G4String& st)
0092       {
0093         randomNumberStatus = new G4String(st);
0094         validRandomNumberStatus = true;
0095       }
0096     inline void SetRandomNumberStatusForProcessing(G4String& st)
0097       {
0098         randomNumberStatusForProcessing = new G4String(st);
0099         validRandomNumberStatusForProcessing = true;
0100       }
0101     inline void KeepTheEvent(G4bool vl=true) const
0102       { keepTheEvent = vl; }
0103     inline G4bool KeepTheEventFlag() const
0104       { return keepTheEvent; }
0105     inline G4bool ToBeKept() const
0106       {
0107         /* TODO (PHASE-II): consider grips for subevents
0108         { return keepTheEvent || grips>0 || GetNumberOfRemainingSubEvents()>0; }
0109         */
0110         return keepTheEvent || GetNumberOfRemainingSubEvents()>0;
0111       }
0112     inline void KeepForPostProcessing() const
0113       { ++grips; }
0114     inline void PostProcessingFinished() const
0115       {
0116         --grips;
0117         if (grips<0)
0118         {
0119           G4Exception("G4Event::Release()", "EVENT91001", JustWarning,
0120                       "Number of grips is negative. This cannot be correct.");
0121         }
0122       }
0123     inline G4int GetNumberOfGrips() const
0124       { return grips; }
0125 
0126     inline G4int GetEventID() const
0127       { return eventID; }
0128 
0129     inline void AddPrimaryVertex(G4PrimaryVertex* aPrimaryVertex)
0130       {
0131         //  This method sets a new primary vertex. This method must be invoked 
0132         // exclusively by G4VPrimaryGenerator concrete class.
0133 
0134         if( thePrimaryVertex == nullptr )
0135         { thePrimaryVertex = aPrimaryVertex; }
0136         else
0137         { thePrimaryVertex->SetNext( aPrimaryVertex ); }
0138         ++numberOfPrimaryVertex;
0139       }
0140 
0141     inline G4int GetNumberOfPrimaryVertex() const
0142       { return numberOfPrimaryVertex; }
0143       // Returns number of primary verteces the G4Event object has.
0144 
0145     inline G4PrimaryVertex* GetPrimaryVertex(G4int i=0)  const
0146       { 
0147         if( i == 0 )
0148         { return thePrimaryVertex; }
0149         if( i > 0 && i < numberOfPrimaryVertex )
0150         {
0151           G4PrimaryVertex* primaryVertex = thePrimaryVertex;
0152           for( G4int j=0; j<i; ++j )
0153           {
0154             if( primaryVertex == nullptr ) return nullptr; 
0155             primaryVertex = primaryVertex->GetNext();
0156           }
0157           return primaryVertex;
0158         }
0159         
0160         return nullptr;
0161       }
0162       // Returns i-th primary vertex of the event.
0163 
0164     inline G4HCofThisEvent* GetHCofThisEvent()  const
0165       { return HC; }
0166     inline G4DCofThisEvent* GetDCofThisEvent()  const
0167       { return DC; }
0168     inline G4TrajectoryContainer* GetTrajectoryContainer() const
0169       { return trajectoryContainer; }
0170       //  These three methods return the pointers to the G4HCofThisEvent
0171       // (hits collections of this event), G4DCofThisEvent (digi collections
0172       // of this event), and G4TrajectoryContainer (trajectory coonainer),
0173       // respectively.
0174 
0175     inline G4bool IsAborted() const { return eventAborted; }
0176       //  Return a boolean which indicates the event has been aborted and thus
0177       // it should not be used for analysis.
0178 
0179     inline void SetUserInformation(G4VUserEventInformation* anInfo)
0180       { userInfo = anInfo; }
0181     inline G4VUserEventInformation* GetUserInformation() const
0182       { return userInfo; }
0183       //  Set and Get method of G4VUserEventInformation
0184 
0185     inline const G4String& GetRandomNumberStatus() const 
0186       {
0187         if(!validRandomNumberStatus)
0188         { G4Exception(
0189               "G4Event::GetRandomNumberStatus","Event0701",JustWarning,
0190               "Random number status is not available for this event."); }
0191         return *randomNumberStatus;
0192       }
0193     inline const G4String& GetRandomNumberStatusForProcessing() const 
0194       {
0195         if(!validRandomNumberStatusForProcessing)
0196         { G4Exception(
0197               "G4Event::GetRandomNumberStatusForProcessing","Event0702",
0198               JustWarning,
0199               "Random number status is not available for this event."); }
0200         return *randomNumberStatusForProcessing;
0201       }
0202 
0203   private:
0204 
0205     // event ID
0206     G4int eventID = 0;      
0207 
0208     // PrimaryVertex
0209     G4PrimaryVertex* thePrimaryVertex = nullptr;
0210     G4int numberOfPrimaryVertex = 0;
0211 
0212     // HitsCollection
0213     G4HCofThisEvent* HC = nullptr;
0214 
0215     // DigiCollection
0216     G4DCofThisEvent* DC = nullptr;
0217 
0218     // TrajectoryContainer
0219     G4TrajectoryContainer* trajectoryContainer = nullptr;
0220 
0221     // Boolean flag which shall be set to true if the event is aborted and 
0222     // thus the containing information is not to be used.
0223     G4bool eventAborted = false;
0224 
0225     // UserEventInformation (optional)
0226     G4VUserEventInformation* userInfo = nullptr;
0227 
0228     // Initial random number engine status before primary particle generation
0229     G4String* randomNumberStatus = nullptr;
0230     G4bool validRandomNumberStatus = false;
0231 
0232     // Initial random number engine status before event processing
0233     G4String* randomNumberStatusForProcessing = nullptr;
0234     G4bool validRandomNumberStatusForProcessing = false;
0235 
0236     // Flag to keep the event until the end of run
0237     mutable G4bool keepTheEvent = false;
0238     mutable G4int grips = 0;
0239 
0240   //========================= for sub-event parallelism
0241   // following methods should be used only within the master thread
0242 
0243   public:
0244     G4SubEvent* PopSubEvent(G4int);
0245       // This method is to be invoked from G4SubEvtRunManager. 
0246       // SpawnSubEvent() is internally invoked.
0247     G4int TerminateSubEvent(G4SubEvent*);
0248       // This method is to be invoked from G4SubEvtRunManager once a sub-event is
0249       // fully processed by a worker thread.
0250 
0251     G4int StoreSubEvent(G4int,G4SubEvent*);
0252       // This method is used by G4EventManager to store all the remeining sub-event 
0253       // object at the end of processing the event.
0254 
0255     G4int SpawnSubEvent(G4SubEvent*);
0256       // Registering sub-event when it is sent to a worker thread.
0257     G4int GetNumberOfRemainingSubEvents() const;
0258       // Number of sub-events that are either still waiting to be processed by worker
0259       // threads or sent to worker threads but not yet completed
0260     inline G4int GetNumberOfCompletedSubEvent() const
0261     { return (G4int)fSubEventGarbageBin.size(); }
0262 
0263     void MergeSubEventResults(const G4Event* se);
0264 
0265   private:
0266   // These containers are for sub-event objects.
0267   // - fSubEvtStackMap stores sub-events that are yet to be sent to the worker thread
0268   //   it stores sub-events sorted separately to sub-event types
0269   // - fSubEvtVector stores sub-events that are sent to worther thread for processing
0270   // - fSubEventGarbageBin stores sub-events that have already been processed by worker thread
0271   //   these sub-events are deleted at the time the G4Event of the master thread is deleted
0272   //   - TODO (PHASE-II): we may need to consider better way to delete sub-events stored in this garbase bin
0273   //     at earlier timing to reduce amount of memory used for storing these garbages
0274     std::map<G4int,std::set<G4SubEvent*>*> fSubEvtStackMap;
0275     std::set<G4SubEvent*> fSubEvtVector;
0276     std::set<G4SubEvent*> fSubEventGarbageBin;
0277 
0278   // motherEvent pointer is non-null for an event created in the worker
0279   // thread of sub-event parallelism.
0280   // This object and all the contained objects must be deleted by the
0281   // responsible worker thread.
0282   private:
0283     G4Event* motherEvent = nullptr;
0284     G4int subEventType = -1;
0285 
0286   public:
0287     void FlagAsSubEvent(G4Event* me, G4int ty)
0288     {
0289       motherEvent = me;
0290       subEventType = ty;
0291     }
0292     inline G4Event* GetMotherEvent() const
0293     { return motherEvent; }
0294     inline G4int GetSubEventType() const
0295     { return subEventType; }
0296 
0297   private:
0298     mutable G4bool scoresRecorded = false;
0299     mutable G4bool eventCompleted = false;
0300   public:
0301     void ScoresRecorded() const { scoresRecorded = true; }
0302     G4bool ScoresAlreadyRecorded() const { return scoresRecorded; }
0303     void EventCompleted() const { eventCompleted = true; }
0304     G4bool IsEventCompleted() const { return eventCompleted; }
0305 };
0306 
0307 extern G4EVENT_DLL G4Allocator<G4Event>*& anEventAllocator();
0308 
0309 inline void* G4Event::operator new(std::size_t)
0310 { 
0311   if (anEventAllocator() == nullptr) 
0312   {
0313     anEventAllocator() = new G4Allocator<G4Event>;
0314   }
0315   return (void*)anEventAllocator()->MallocSingle();
0316 }
0317 
0318 inline void G4Event::operator delete(void* anEvent)
0319 {
0320   anEventAllocator()->FreeSingle((G4Event*)anEvent);
0321 }
0322 
0323 #endif