00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef BLMANAGER_HH
00020 #define BLMANAGER_HH
00021
00022 #include <vector>
00023 #include <map>
00024 #include <set>
00025 #include "G4VUserDetectorConstruction.hh"
00026 #include "G4SteppingManager.hh"
00027 #include "G4Step.hh"
00028 #include "G4Track.hh"
00029 #include "G4Run.hh"
00030 #include "G4Event.hh"
00031 #include "G4VPhysicalVolume.hh"
00032 #include "G4VExceptionHandler.hh"
00033
00034 #include "BLCallback.hh"
00035 #include "BLRunManager.hh"
00036 #include "BLPhysics.hh"
00037 #include "BLZStep.hh"
00038 #include "BLUserCode.hh"
00039
00040 class BLBeam;
00041
00042
00043
00044
00045 enum BLManagerState { IDLE, VISUAL, TUNE, REFERENCE, BEAM, SPECIAL };
00046 enum VerboseFormat { TAG,NSTEP,GLOBAL,CL,CLX,KE,STEP,VOL,PROCESS,B,E,MAT,
00047 P, ID, PART, SEG, WT, NEWLINE, EXT };
00048 enum PRNGSeedMethod { EVENT_NUMBER, NO_SEED, TIME_US };
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 class BLManager : public G4VUserDetectorConstruction,
00066 G4VExceptionHandler {
00067 public:
00068 class RunAction {
00069 public: virtual void BeginOfRunAction(const G4Run *run) = 0;
00070 public: virtual void EndOfRunAction(const G4Run *run) = 0;
00071 };
00072 class EventAction {
00073 public: virtual void BeginOfEventAction(const G4Event* event) = 0;
00074 public: virtual void EndOfEventAction(const G4Event* event) = 0;
00075 };
00076 class TrackingAction {
00077 protected: G4TrackingManager *fpTrackingManager;
00078 public: void SetTrackingManagerPointer(G4TrackingManager *p)
00079 {fpTrackingManager=p;}
00080 public: virtual void PreUserTrackingAction(const G4Track *track) = 0;
00081 public: virtual void PostUserTrackingAction(const G4Track *track) = 0;
00082 };
00083 class SteppingAction {
00084 public: virtual void UserSteppingAction(const G4Step *step) = 0;
00085 };
00086 class ZSteppingAction {
00087 public: virtual void UserZSteppingAction(const G4Track *track) = 0;
00088 };
00089 class PrimaryGeneratorAction {
00090 public: virtual void GeneratePrimaries(G4Event *event) = 0;
00091 };
00092 class StackingAction {
00093
00094 public: virtual G4ClassificationOfNewTrack
00095 ClassifyNewTrack(const G4Track*) = 0;
00096 public: virtual void NewStage() { }
00097 public: virtual void PrepareNewEvent() { }
00098 };
00099 private:
00100 struct ZStep {
00101 G4double z;
00102 ZSteppingAction *action;
00103 ZStep(G4double _z, ZSteppingAction *a) { z=_z; action=a; }
00104 };
00105 static BLManager *blManager;
00106 static bool initialized;
00107 BLRunManager *runManager;
00108 BLManagerState state;
00109 G4int steppingVerbose;
00110 unsigned int beamIndex;
00111 G4int histoUpdate;
00112 time_t startRun;
00113 time_t startEvent;
00114 G4int eventTimeLimit;
00115 BLPhysics *physics;
00116 G4VPhysicalVolume *worldPhysicalVolume;
00117 G4int eventID;
00118 G4int trackID;
00119 G4ParticleDefinition *trackParticleDef;
00120 G4int fatalExceptions;
00121 G4int eventsAborted;
00122 G4int tracksAborted;
00123 G4int warnings;
00124 G4int prevEventID;
00125 G4int eventsProcessed;
00126 bool endRun;
00127 PRNGSeedMethod seedMethod;
00128 std::set<int> keepEventList;
00129 std::set<int> skipEventList;
00130 G4SteppingManager *fpSteppingManager;
00131 G4TrackingManager *fpTrackingManager;
00132 std::vector<BLBeam*> beamVector;
00133 std::vector<BLBeam*> referenceVector;
00134 std::vector<RunAction*> runActionVector;
00135 std::vector<RunAction*> beamRunActionVector;
00136 std::vector<EventAction*> eventActionVector;
00137 std::vector<EventAction*> beamEventActionVector;
00138 std::vector<TrackingAction*> trackingActionVector;
00139 std::vector<BLCallback*> preReferenceCallbackVector;
00140 std::vector<BLCallback*> postReferenceCallbackVector;
00141 std::vector<BLCallback*> postTrackingCallbackVector;
00142 std::vector<BLCallback*> replaceMainLoopCallbackVector;
00143 std::vector<BLCallback*> visualizationCallbackVector;
00144 std::vector<SteppingAction*> allStepVector;
00145 std::map<G4VPhysicalVolume*,SteppingAction*> allStepMap;
00146 std::map<G4VPhysicalVolume*,SteppingAction*> tpStepMap;
00147 std::map<G4VPhysicalVolume*,SteppingAction*> rpStepMap;
00148 std::vector<SteppingAction*> tpStepVector;
00149 std::vector<SteppingAction*> rpStepVector;
00150 std::map<G4VPhysicalVolume*,SteppingAction*> beamStepMap;
00151 std::vector<SteppingAction*> beamStepVector;
00152 std::vector<int> verboseFormat;
00153 G4double zTolerance;
00154 std::vector<ZStep> tuneZStep;
00155 std::vector<ZStep> referenceZStep;
00156 std::vector<ZStep> beamZStep;
00157 std::vector<ZStep> *currentZStep;
00158 unsigned indexZStep;
00159 G4double prevZ;
00160 G4int nStuck;
00161 std::vector<StackingAction*> stackingActionVector;
00162 int nextSecondaryTrackID;
00163 std::map<G4int,G4int> trackIDMap;
00164 int primaryTrackID;
00165 int primaryParentID;
00166 std::vector<BLUserCode*> userCodeVector;
00167 std::map<G4String,int> exceptionCount;
00168
00169 BLManager();
00170 void insertZStep(std::vector<ZStep>& vector, G4double z, ZSteppingAction *action);
00171 public:
00172
00173
00174
00175
00176 static BLManager *getObject();
00177
00178
00179
00180 void delayedConstruction();
00181
00182
00183 ~BLManager();
00184
00185
00186 BLManagerState getState() { return state; }
00187
00188
00189 void setState(BLManagerState _state) { state = _state; }
00190
00191
00192
00193 int getSteppingVerbose() { return steppingVerbose; }
00194
00195
00196
00197 void setSteppingVerbose(int v) { steppingVerbose = v; }
00198
00199
00200
00201 int getEventTimeLimit() { return eventTimeLimit; }
00202
00203
00204
00205 void setEventTimeLimit(int sec) { eventTimeLimit = sec; }
00206
00207
00208 G4int getEventID() const { return eventID; }
00209
00210
00211 void setEventID(int evId) { eventID=evId; prevEventID=evId-1; }
00212
00213
00214
00215 bool skipEvent(int EventID) {
00216 if(skipEventList.count(EventID) > 0) return true;
00217 return keepEventList.size() > 0 && keepEventList.count(EventID) == 0;
00218 }
00219
00220
00221
00222 void setKeepEvent(int EventID) { keepEventList.insert(EventID); }
00223
00224
00225 void setSkipEvent(int EventID) { skipEventList.insert(EventID); }
00226
00227
00228
00229 void incrEventsProcessed(int eventID);
00230
00231
00232
00233 void registerSteppingAction(SteppingAction *sa)
00234 { allStepVector.push_back(sa); }
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 void registerSteppingAction(G4VPhysicalVolume *physicalVol,
00245 SteppingAction *sa)
00246 { if(physicalVol != 0) allStepMap[physicalVol] = sa;
00247 else allStepVector.push_back(sa); }
00248
00249
00250
00251
00252
00253
00254
00255
00256 void registerTuneParticleStep(G4VPhysicalVolume *physicalVol,
00257 SteppingAction *sa)
00258 { if(physicalVol != 0) tpStepMap[physicalVol] = sa;
00259 else tpStepVector.push_back(sa); }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 void registerReferenceParticleStep(G4VPhysicalVolume *physicalVol,
00270 SteppingAction *sa)
00271 { if(physicalVol != 0) rpStepMap[physicalVol] = sa;
00272 else rpStepVector.push_back(sa); }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 void registerBeamStep(G4VPhysicalVolume *physicalVol,
00283 SteppingAction *sa)
00284 { if(physicalVol != 0) beamStepMap[physicalVol] = sa;
00285 else beamStepVector.push_back(sa); }
00286
00287
00288
00289
00290
00291 void registerZStep(G4double z, ZSteppingAction *sa, G4int when=7);
00292
00293
00294
00295 void registerStackingAction(StackingAction *sa)
00296 { stackingActionVector.push_back(sa); }
00297
00298
00299
00300 void setSteppingFormat();
00301
00302
00303
00304 G4String getFormatHelp();
00305
00306
00307
00308 void appendVerboseFormat(G4String fmt);
00309
00310
00311
00312
00313 void steppingVerbosePrint(const G4Step *step, const G4Track *track, int
00314 header=0);
00315
00316
00317
00318 void setSteppingManager(G4SteppingManager *p) { fpSteppingManager = p; }
00319
00320
00321
00322 G4SteppingManager *getSteppingManager() { return fpSteppingManager; }
00323
00324
00325
00326 void setTrackingManager(G4TrackingManager *p) { fpTrackingManager = p; }
00327
00328
00329
00330
00331
00332
00333 void initialize();
00334
00335
00336 static bool isInitialized() { return initialized; }
00337
00338
00339
00340 void trackTuneAndReferenceParticles();
00341
00342
00343 void trackBeam();
00344
00345
00346 void displayVisual();
00347
00348
00349
00350
00351 void displayGeometry(G4VPhysicalVolume *phys=0, int level=0);
00352
00353
00354
00355
00356 void registerPhysics(BLPhysics *_physics)
00357 { physics = _physics;
00358 runManager->SetUserInitialization(physics->getPhysicsList());
00359 }
00360
00361
00362 BLPhysics *getPhysics() { return physics; }
00363
00364
00365
00366 void registerBeam(BLBeam *_beam) { beamVector.push_back(_beam); }
00367
00368
00369
00370
00371 void registerReference(BLBeam *_beam)
00372 { referenceVector.push_back(_beam); }
00373
00374
00375 int nReference() { return referenceVector.size(); }
00376
00377
00378
00379
00380 void registerRunAction(RunAction *a, G4bool beamOnly=true)
00381 { if(beamOnly)
00382 beamRunActionVector.push_back(a);
00383 else
00384 runActionVector.push_back(a);
00385 }
00386
00387
00388
00389
00390 void registerEventAction(EventAction *a, G4bool beamOnly=true)
00391 { if(beamOnly)
00392 beamEventActionVector.push_back(a);
00393 else
00394 eventActionVector.push_back(a);
00395 }
00396
00397
00398
00399
00400 void registerTrackingAction(TrackingAction *a, bool front=false)
00401 { if(front)
00402 trackingActionVector.insert(trackingActionVector.begin(),a);
00403 else
00404 trackingActionVector.push_back(a);
00405 }
00406
00407
00408 void registerUserCode(BLUserCode *instance)
00409 { userCodeVector.push_back(instance); }
00410
00411
00412
00413 std::vector<BLUserCode*> getUserCodeInstances(G4String type)
00414 { std::vector<BLUserCode*> ret;
00415 for(unsigned i=0; i<userCodeVector.size(); ++i) {
00416 if(type == userCodeVector[i]->getType())
00417 ret.push_back(userCodeVector[i]);
00418 }
00419 return ret;
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434 void registerCallback(BLCallback *cb, int type) {
00435 if(type==0) preReferenceCallbackVector.push_back(cb);
00436 else if(type==1) postReferenceCallbackVector.push_back(cb);
00437 else if(type==2) postTrackingCallbackVector.push_back(cb);
00438 else if(type==3) replaceMainLoopCallbackVector.push_back(cb);
00439 else if(type==4) visualizationCallbackVector.push_back(cb);
00440 }
00441
00442
00443
00444
00445
00446
00447
00448 void handleCallbacks(int type);
00449
00450
00451
00452
00453 G4VPhysicalVolume *getWorldPhysicalVolume()
00454 { return worldPhysicalVolume; }
00455
00456
00457
00458 void setPRNGSeedMethod(PRNGSeedMethod method)
00459 { seedMethod = method; }
00460
00461
00462
00463 PRNGSeedMethod getPRNGSeedMethod() { return seedMethod; }
00464
00465
00466
00467
00468
00469
00470
00471
00472 void UserSteppingAction(const G4Step *step);
00473
00474
00475 G4VPhysicalVolume *Construct();
00476
00477
00478 void PreUserTrackingAction(const G4Track *track);
00479
00480
00481 void PostUserTrackingAction(const G4Track *track);
00482
00483
00484 void BeginOfRunAction(const G4Run *run);
00485
00486
00487 void EndOfRunAction(const G4Run *run);
00488
00489
00490 void BeginOfEventAction(const G4Event* event);
00491
00492
00493 void EndOfEventAction(const G4Event* event);
00494
00495
00496 void GeneratePrimaries(G4Event *event);
00497
00498
00499 G4ClassificationOfNewTrack ClassifyNewTrack(const G4Track*);
00500
00501
00502 void NewStage();
00503
00504
00505 void PrepareNewEvent();
00506
00507
00508 void clearTrackIDMap() { trackIDMap.clear(); }
00509
00510
00511
00512
00513 void setNextSecondaryTrackID(int next) { nextSecondaryTrackID = next; }
00514 int getNextSecondaryTrackID() { return nextSecondaryTrackID; }
00515
00516
00517
00518
00519 int getExternalTrackID(const G4Track *track);
00520
00521
00522
00523
00524 int getExternalParentID(const G4Track *track);
00525
00526
00527
00528 void setExternalTrackID(G4Track *track, int trackID, int parentID);
00529
00530
00531 int getPrimaryTrackID() { return primaryTrackID; }
00532
00533
00534 int getPrimaryParentID() { return primaryParentID; }
00535
00536
00537 void setPrimaryTrackID(int t, int p)
00538 { primaryTrackID = t; primaryParentID = p; }
00539
00540
00541 G4bool Notify(const char* originOfException, const char* exceptionCode,
00542 G4ExceptionSeverity severity, const char* description);
00543
00544
00545 void exceptionSummary();
00546
00547
00548 static void gsl_error_handler(const char *reason, const char *file,
00549 int lineno, int gsl_errno);
00550 };
00551
00552 #endif // BLMANAGER_HH