Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:58:15

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 #include "G4Profiler.hh"
0055 
0056 class G4VHitsCollection;
0057 class G4SubEvent;
0058 
0059 class G4Event 
0060 {
0061   public:
0062     using ProfilerConfig = G4ProfilerConfig<G4ProfileType::Event>;
0063 
0064   public:
0065     G4Event() = default;
0066     explicit G4Event(G4int evID);
0067    ~G4Event();
0068 
0069     G4Event(const G4Event &) = delete;
0070     G4Event& operator=(const G4Event &) = delete;
0071 
0072     inline void *operator new(std::size_t);
0073     inline void operator delete(void* anEvent);
0074 
0075     G4bool operator==(const G4Event& right) const;
0076     G4bool operator!=(const G4Event& right) const;
0077 
0078     void Print() const;
0079       // Print the event ID (starts with zero and increments by one) to G4cout.
0080     void Draw() const;
0081       // Invoke Draw() methods of all stored trajectories, hits, and digits.
0082       // For hits and digits, Draw() methods of the concrete classes must be
0083       // implemented. Otherwise nothing will be drawn.
0084 
0085     inline void SetEventID(G4int i)
0086       { eventID =  i; }
0087     inline void SetHCofThisEvent(G4HCofThisEvent* value)
0088       { HC = value; }
0089     inline void SetDCofThisEvent(G4DCofThisEvent* value)
0090       { DC = value; }
0091     inline void SetTrajectoryContainer(G4TrajectoryContainer* value)
0092       { trajectoryContainer = value; }
0093     inline void SetEventAborted()
0094       { eventAborted = true; }
0095     inline void SetRandomNumberStatus(G4String& st)
0096       {
0097         randomNumberStatus = new G4String(st);
0098         validRandomNumberStatus = true;
0099       }
0100     inline void SetRandomNumberStatusForProcessing(G4String& st)
0101       {
0102         randomNumberStatusForProcessing = new G4String(st);
0103         validRandomNumberStatusForProcessing = true;
0104       }
0105     inline void KeepTheEvent(G4bool vl=true)
0106       { keepTheEvent = vl; }
0107     inline G4bool ToBeKept() const
0108       { return keepTheEvent || GetNumberOfRemainingSubEvents()>0; }
0109     inline void KeepForPostProcessing() const
0110       { ++grips; }
0111     inline void PostProcessingFinished() const
0112       {
0113         --grips;
0114         if (grips<0)
0115         {
0116           G4Exception("G4Event::Release()", "EVENT91001", FatalException,
0117                       "Number of grips is negative. This cannot be correct.");
0118         }
0119       }
0120     inline G4int GetNumberOfGrips() const
0121       { return grips; }
0122 
0123     inline G4int GetEventID() const
0124       { return eventID; }
0125 
0126     inline void AddPrimaryVertex(G4PrimaryVertex* aPrimaryVertex)
0127       {
0128         //  This method sets a new primary vertex. This method must be invoked 
0129         // exclusively by G4VPrimaryGenerator concrete class.
0130 
0131         if( thePrimaryVertex == nullptr )
0132         { thePrimaryVertex = aPrimaryVertex; }
0133         else
0134         { thePrimaryVertex->SetNext( aPrimaryVertex ); }
0135         ++numberOfPrimaryVertex;
0136       }
0137 
0138     inline G4int GetNumberOfPrimaryVertex() const
0139       { return numberOfPrimaryVertex; }
0140       // Returns number of primary verteces the G4Event object has.
0141 
0142       inline G4PrimaryVertex* GetPrimaryVertex(G4int i=0)  const
0143       { 
0144         if( i == 0 )
0145         { return thePrimaryVertex; }
0146         if( i > 0 && i < numberOfPrimaryVertex )
0147         {
0148           G4PrimaryVertex* primaryVertex = thePrimaryVertex;
0149           for( G4int j=0; j<i; ++j )
0150           {
0151             if( primaryVertex == nullptr ) return nullptr; 
0152             primaryVertex = primaryVertex->GetNext();
0153           }
0154           return primaryVertex;
0155         }
0156         
0157         return nullptr;
0158       }
0159       // Returns i-th primary vertex of the event.
0160 
0161     inline G4HCofThisEvent* GetHCofThisEvent()  const
0162       { return HC; }
0163     inline G4DCofThisEvent* GetDCofThisEvent()  const
0164       { return DC; }
0165     inline G4TrajectoryContainer* GetTrajectoryContainer() const
0166       { return trajectoryContainer; }
0167       //  These three methods return the pointers to the G4HCofThisEvent
0168       // (hits collections of this event), G4DCofThisEvent (digi collections
0169       // of this event), and G4TrajectoryContainer (trajectory coonainer),
0170       // respectively.
0171 
0172     inline G4bool IsAborted() const { return eventAborted; }
0173       //  Return a boolean which indicates the event has been aborted and thus
0174       // it should not be used for analysis.
0175 
0176     inline void SetUserInformation(G4VUserEventInformation* anInfo)
0177       { userInfo = anInfo; }
0178     inline G4VUserEventInformation* GetUserInformation() const
0179       { return userInfo; }
0180       //  Set and Get method of G4VUserEventInformation
0181 
0182     inline const G4String& GetRandomNumberStatus() const 
0183       {
0184         if(!validRandomNumberStatus)
0185         { G4Exception(
0186               "G4Event::GetRandomNumberStatus","Event0701",JustWarning,
0187               "Random number status is not available for this event."); }
0188         return *randomNumberStatus;
0189       }
0190     inline const G4String& GetRandomNumberStatusForProcessing() const 
0191       {
0192         if(!validRandomNumberStatusForProcessing)
0193         { G4Exception(
0194               "G4Event::GetRandomNumberStatusForProcessing","Event0702",
0195               JustWarning,
0196               "Random number status is not available for this event."); }
0197         return *randomNumberStatusForProcessing;
0198       }
0199 
0200   private:
0201 
0202     // event ID
0203     G4int eventID = 0;      
0204 
0205     // PrimaryVertex
0206     G4PrimaryVertex* thePrimaryVertex = nullptr;
0207     G4int numberOfPrimaryVertex = 0;
0208 
0209     // HitsCollection
0210     G4HCofThisEvent* HC = nullptr;
0211 
0212     // DigiCollection
0213     G4DCofThisEvent* DC = nullptr;
0214 
0215     // TrajectoryContainer
0216     G4TrajectoryContainer* trajectoryContainer = nullptr;
0217 
0218     // Boolean flag which shall be set to true if the event is aborted and 
0219     // thus the containing information is not to be used.
0220     G4bool eventAborted = false;
0221 
0222     // UserEventInformation (optional)
0223     G4VUserEventInformation* userInfo = nullptr;
0224 
0225     // Initial random number engine status before primary particle generation
0226     G4String* randomNumberStatus = nullptr;
0227     G4bool validRandomNumberStatus = false;
0228 
0229     // Initial random number engine status before event processing
0230     G4String* randomNumberStatusForProcessing = nullptr;
0231     G4bool validRandomNumberStatusForProcessing = false;
0232 
0233     // Flag to keep the event until the end of run
0234     G4bool keepTheEvent = false;
0235     mutable G4int grips = 0;
0236 
0237   //========================= for sub-event parallelism
0238   // following methods should be used only within the master thread
0239 
0240   public:
0241     G4SubEvent* PopSubEvent(G4int);
0242       // This method is to be invoked from G4RunManager. 
0243       // SpawnSubEvent() is internally invoked.
0244     G4int TerminateSubEvent(G4SubEvent*);
0245       // This method is to be invoked from G4RunManager once a sub-event is
0246       // fully processed by a worker thread.
0247 
0248     G4int StoreSubEvent(G4int,G4SubEvent*);
0249       // This method is used by G4EventManager to store all the remeining sub-event 
0250       // object at the end of processing the event.
0251 
0252     G4int SpawnSubEvent(G4SubEvent*);
0253       // Registering sub-event when it is sent to a worker thread.
0254     inline G4int GetNumberOfRemainingSubEvents() const
0255       // Number of sub-events that have been sent to worker threads but not yet
0256       // completed.
0257     {
0258       auto  tot = (G4int)fSubEvtVector.size(); 
0259       for(auto& sem : fSubEvtStackMap)
0260       { tot += (G4int)sem.second->size(); }
0261       return tot;
0262     }
0263 
0264     void MergeSubEventResults(const G4Event* se);
0265 
0266   private:
0267     std::map<G4int,std::set<G4SubEvent*>*> fSubEvtStackMap;
0268     std::set<G4SubEvent*> fSubEvtVector;
0269 
0270   // motherEvent pointer is non-null for an event created in the worker
0271   // thread of sub-event parallelism.
0272   // This object and all the contained objects must be deleted by the
0273   // responsible worker thread.
0274   private:
0275     G4Event* motherEvent = nullptr;
0276     G4int subEventType = -1;
0277 
0278   public:
0279     void FlagAsSubEvent(G4Event* me, G4int ty)
0280     {
0281       motherEvent = me;
0282       subEventType = ty;
0283     }
0284     G4Event* GetMotherEvent() const
0285     { return motherEvent; }
0286     G4int GetSubEventType() const
0287     { return subEventType; }
0288 };
0289 
0290 extern G4EVENT_DLL G4Allocator<G4Event>*& anEventAllocator();
0291 
0292 inline void* G4Event::operator new(std::size_t)
0293 { 
0294   if (anEventAllocator() == nullptr) 
0295   {
0296     anEventAllocator() = new G4Allocator<G4Event>;
0297   }
0298   return (void*)anEventAllocator()->MallocSingle();
0299 }
0300 
0301 inline void G4Event::operator delete(void* anEvent)
0302 {
0303   anEventAllocator()->FreeSingle((G4Event*)anEvent);
0304 }
0305 
0306 #endif