Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4Event class implementation << 27 // 26 // 28 // Author: M.Asai, SLAC/JLAB << 27 // $Id: G4Event.cc,v 1.14 2007/03/07 02:44:16 asaim Exp $ 29 // ------------------------------------------- << 28 // GEANT4 tag $Name: geant4-09-02 $ >> 29 // >> 30 >> 31 // G4Event 30 32 31 #include "G4Event.hh" 33 #include "G4Event.hh" 32 #include "G4VVisManager.hh" 34 #include "G4VVisManager.hh" >> 35 //#include "G4HCofThisEvent.hh" >> 36 //#include "G4DCofThisEvent.hh" 33 #include "G4VHitsCollection.hh" 37 #include "G4VHitsCollection.hh" 34 #include "G4VDigiCollection.hh" 38 #include "G4VDigiCollection.hh" 35 #include "G4ios.hh" 39 #include "G4ios.hh" 36 #include "G4SubEvent.hh" << 37 40 38 #include "G4Threading.hh" << 41 G4Allocator<G4Event> anEventAllocator; 39 #include "G4AutoLock.hh" << 40 namespace{ << 41 G4Mutex SubEventMutex = G4MUTEX_INITIALIZER; << 42 } << 43 42 44 G4Allocator<G4Event>*& anEventAllocator() << 43 G4Event::G4Event() 45 { << 44 :eventID(0), 46 G4ThreadLocalStatic G4Allocator<G4Event>* _i << 45 thePrimaryVertex(0),numberOfPrimaryVertex(0), 47 return _instance; << 46 HC(0),DC(0),trajectoryContainer(0),eventAborted(false),userInfo(0), 48 } << 47 randomNumberStatus(0),validRandomNumberStatus(false), >> 48 randomNumberStatusForProcessing(0),validRandomNumberStatusForProcessing(false), >> 49 keepTheEvent(false) >> 50 {;} 49 51 50 G4Event::G4Event(G4int evID) 52 G4Event::G4Event(G4int evID) 51 : eventID(evID) << 53 :eventID(evID), 52 { << 54 thePrimaryVertex(0),numberOfPrimaryVertex(0), 53 } << 55 HC(0),DC(0),trajectoryContainer(0),eventAborted(false),userInfo(0), >> 56 randomNumberStatus(0),validRandomNumberStatus(false), >> 57 randomNumberStatusForProcessing(0),validRandomNumberStatusForProcessing(false), >> 58 keepTheEvent(false) >> 59 {;} 54 60 55 G4Event::~G4Event() 61 G4Event::~G4Event() 56 { << 62 { 57 G4PrimaryVertex* nextVertex = thePrimaryVert << 63 if(thePrimaryVertex) delete thePrimaryVertex; 58 while(nextVertex != nullptr) << 64 if(HC) delete HC; 59 { << 65 if(DC) delete DC; 60 G4PrimaryVertex* thisVertex = nextVertex; << 66 if(trajectoryContainer) 61 nextVertex = thisVertex->GetNext(); << 62 thisVertex->ClearNext(); << 63 delete thisVertex; << 64 } << 65 thePrimaryVertex = nullptr; << 66 delete HC; << 67 delete DC; << 68 if(trajectoryContainer != nullptr) << 69 { 67 { 70 trajectoryContainer->clearAndDestroy(); 68 trajectoryContainer->clearAndDestroy(); 71 delete trajectoryContainer; 69 delete trajectoryContainer; 72 } 70 } 73 delete userInfo; << 71 if(userInfo) delete userInfo; 74 delete randomNumberStatus; << 72 if(validRandomNumberStatus) delete randomNumberStatus; 75 delete randomNumberStatusForProcessing; << 73 if(validRandomNumberStatusForProcessing) delete randomNumberStatusForProcessing; 76 << 77 // TODO (PHASE-II): count remaining subevent << 78 for(auto& sem : fSubEvtStackMap) << 79 { << 80 if((sem.second!=nullptr)&&!(sem.second->em << 81 { << 82 for(auto& se : *(sem.second)) << 83 { delete se; } << 84 sem.second->clear(); << 85 } << 86 } << 87 /* TODO (PHASE-II): Handle incorrect scoring << 88 if(!scoresRecorded) { << 89 G4ExceptionDescription ed; << 90 ed << "Deleting G4Event (id:" << eventID < << 91 << remainingSE << " sub-events un-proce << 92 G4Exception("G4Event::~G4Event()","SubEvt0 << 93 } else if(remainingSE>0) { << 94 G4ExceptionDescription ed; << 95 ed << "Deleting G4Event (id:" << eventID < << 96 << remainingSE << " sub-events un-proce << 97 G4Exception("G4Event::~G4Event()","SubEvt0 << 98 } << 99 */ << 100 if(!(fSubEvtVector.empty())) << 101 { << 102 for(auto& se : fSubEvtVector) << 103 { << 104 G4cout << "SubEvent " << se << " belongs << 105 << " (eventID=" << se->GetEvent() << 106 << se->GetNTrack() << " stacked t << 107 } << 108 G4ExceptionDescription ed; << 109 ed << "Deleting G4Event (id:" << eventID < << 110 << fSubEvtVector.size() << " sub-events << 111 G4Exception("G4Event::~G4Event()","SubEvt0 << 112 // TODO (PHASE-II): Handle deletion of sub << 113 //for(auto& se : fSubEvtVector) << 114 //{ delete se; } << 115 //fSubEvtVector.clear(); << 116 } << 117 << 118 if(!(fSubEventGarbageBin.empty())) << 119 { << 120 for(auto& se : fSubEventGarbageBin) << 121 { delete se; } << 122 fSubEventGarbageBin.clear(); << 123 } << 124 } 74 } 125 75 126 G4bool G4Event::operator==(const G4Event& righ << 76 G4int G4Event::operator==(const G4Event &right) const 127 { 77 { 128 return ( eventID == right.eventID ); 78 return ( eventID == right.eventID ); 129 } 79 } 130 80 131 G4bool G4Event::operator!=(const G4Event& righ << 81 G4int G4Event::operator!=(const G4Event &right) const 132 { 82 { 133 return ( eventID != right.eventID ); 83 return ( eventID != right.eventID ); 134 } 84 } 135 85 136 void G4Event::Print() const 86 void G4Event::Print() const 137 { 87 { 138 G4cout << "G4Event " << eventID << G4endl; 88 G4cout << "G4Event " << eventID << G4endl; 139 } 89 } 140 90 141 void G4Event::Draw() const 91 void G4Event::Draw() const 142 { 92 { 143 G4VVisManager* pVVisManager = G4VVisManager: 93 G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance(); 144 if(pVVisManager == nullptr) return; << 94 if(!pVVisManager) return; 145 95 146 if(trajectoryContainer != nullptr) << 96 if(trajectoryContainer) 147 { 97 { 148 std::size_t n_traj = trajectoryContainer-> << 98 G4int n_traj = trajectoryContainer->entries(); 149 for(std::size_t i=0; i<n_traj; ++i) << 99 for(G4int i=0;i<n_traj;i++) 150 { (*trajectoryContainer)[i]->DrawTrajector 100 { (*trajectoryContainer)[i]->DrawTrajectory(); } 151 } 101 } 152 102 153 if(HC != nullptr) << 103 if(HC) 154 { 104 { 155 std::size_t n_HC = HC->GetCapacity(); << 105 G4int n_HC = HC->GetCapacity(); 156 for(std::size_t j=0; j<n_HC; ++j) << 106 for(G4int j=0;j<n_HC;j++) 157 { 107 { 158 G4VHitsCollection* VHC = HC->GetHC((G4in << 108 G4VHitsCollection * VHC = HC->GetHC(j); 159 if(VHC != nullptr) VHC->DrawAllHits(); << 109 if(VHC) VHC->DrawAllHits(); 160 } 110 } 161 } 111 } 162 112 163 if(DC != nullptr) << 113 if(DC) 164 { 114 { 165 std::size_t n_DC = DC->GetCapacity(); << 115 G4int n_DC = DC->GetCapacity(); 166 for(std::size_t j=0; j<n_DC; ++j) << 116 for(G4int j=0;j<n_DC;j++) 167 { 117 { 168 G4VDigiCollection* VDC = DC->GetDC((G4in << 118 G4VDigiCollection * VDC = DC->GetDC(j); 169 if(VDC != nullptr) VDC->DrawAllDigi(); << 119 if(VDC) VDC->DrawAllDigi(); 170 } 120 } 171 } 121 } 172 } 122 } 173 123 174 G4int G4Event::StoreSubEvent(G4int ty,G4SubEve << 175 { << 176 G4AutoLock lock(&SubEventMutex); << 177 std::set<G4SubEvent*>* sev = nullptr; << 178 auto ses = fSubEvtStackMap.find(ty); << 179 if(ses==fSubEvtStackMap.end()) << 180 { << 181 sev = new std::set<G4SubEvent*>; << 182 fSubEvtStackMap[ty] = sev; << 183 } << 184 else << 185 { sev = ses->second; } << 186 sev->insert(se); << 187 return (G4int)sev->size(); << 188 } << 189 << 190 G4SubEvent* G4Event::PopSubEvent(G4int ty) << 191 { << 192 G4AutoLock lock(&SubEventMutex); << 193 G4SubEvent* se = nullptr; << 194 auto ses = fSubEvtStackMap.find(ty); << 195 if(ses!=fSubEvtStackMap.end()) << 196 { << 197 auto sev = ses->second; << 198 if(!(sev->empty())) << 199 { << 200 se = sev->extract(sev->begin()).value(); << 201 SpawnSubEvent(se); << 202 } << 203 } << 204 return se; << 205 } << 206 << 207 G4int G4Event::SpawnSubEvent(G4SubEvent* se) << 208 { << 209 // Can't use same mutex here as call by PopS << 210 // caller is PopSubEvent << 211 //G4AutoLock lock(&SubEventMutex); << 212 auto ss = fSubEvtVector.find(se); << 213 if(ss!=fSubEvtVector.end()) << 214 { << 215 G4ExceptionDescription ed; << 216 ed << "Sub-event " << se << " of type " << << 217 << " with " << se->GetNTrack() << " tra << 218 G4Exception("G4Event::SpawnSubEvent","SubE << 219 FatalException,ed); << 220 } << 221 fSubEvtVector.insert(se); << 222 return (G4int)fSubEvtVector.size(); << 223 } << 224 << 225 void G4Event::MergeSubEventResults(const G4Eve << 226 { << 227 // TODO (PHASE-II): Handle merging of subeve << 228 // Note: << 229 // - scores are merged directly to the s << 230 // - hits collections should be merged b << 231 /* << 232 #ifdef G4_STORE_TRAJECTORY << 233 if(se->trajectoryContainer!=nullptr && se->t << 234 { << 235 if(trajectoryContainer==nullptr) trajector << 236 for(auto& trj : *(se->trajectoryContainer- << 237 { trajectoryContainer->push_back(trj); } << 238 } << 239 #endif << 240 */ << 241 } << 242 << 243 G4int G4Event::TerminateSubEvent(G4SubEvent* s << 244 { << 245 G4AutoLock lock(&SubEventMutex); << 246 << 247 auto ss = fSubEvtVector.find(se); << 248 if(ss==fSubEvtVector.end()) << 249 { << 250 G4ExceptionDescription ed; << 251 ed << "Sub-event " << se << " of type " << << 252 << " with " << se->GetNTrack() << " tra << 253 << " in event " << se->GetEvent() << 254 << " has never been spawned."; << 255 G4Exception("G4Event::TerminateSubEvent"," << 256 FatalException,ed); << 257 } << 258 << 259 fSubEvtVector.erase(ss); << 260 << 261 ss = fSubEvtVector.find(se); << 262 if(ss!=fSubEvtVector.end()) << 263 { << 264 G4ExceptionDescription ed; << 265 ed << "Sub-event " << se << " of type " << << 266 << " with " << se->GetNTrack() << " app << 267 G4Exception("G4Event::TerminateSubEvent"," << 268 FatalException,ed); << 269 } << 270 << 271 //se->clearAndDestroy(); << 272 //delete se; << 273 fSubEventGarbageBin.insert(se); << 274 return (G4int)fSubEvtVector.size(); << 275 } << 276 << 277 G4int G4Event::GetNumberOfRemainingSubEvents() << 278 // Number of sub-events that are either still << 279 // threads or sent to worker threads but not y << 280 { << 281 G4AutoLock lock(&SubEventMutex); << 282 auto tot = (G4int)fSubEvtVector.size(); << 283 for(auto& sem : fSubEvtStackMap) << 284 { tot += (G4int)sem.second->size(); } << 285 return tot; << 286 } << 287 124