"Detector/git@code.ihep.ac.cn:dhb112358/CEPCSW.git" did not exist on "087ab6fdbdae75a2c8a45e9fbe15a3fd8f59ac7c"
Newer
Older
Markus Frank
committed
//==========================================================================
// AIDA Detector description implementation for LCD
//--------------------------------------------------------------------------
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
Markus Frank
committed
// All rights reserved.
Markus Frank
committed
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
Markus Frank
committed
// Author : M.Frank
//
//==========================================================================
Markus Frank
committed
// Framework include files
#include "DD4hep/Printout.h"
#include "DD4hep/Primitives.h"
#include "DD4hep/InstanceCount.h"
#include "DD4hep/Handle.h"
#include "DDG4/Geant4RunAction.h"
#include "DDG4/Geant4EventAction.h"
#include "DDG4/Geant4SteppingAction.h"
#include "DDG4/Geant4TrackingAction.h"
#include "DDG4/Geant4StackingAction.h"
#include "DDG4/Geant4GeneratorAction.h"
Markus Frank
committed
#include "DDG4/Geant4UserInitialization.h"
#include "DDG4/Geant4DetectorConstruction.h"
#include "DDG4/Geant4Kernel.h"
#include "DDG4/Geant4Random.h"
Markus Frank
committed
// Geant4 include files
Markus Frank
committed
#include "G4Version.hh"
Markus Frank
committed
#include "G4UserRunAction.hh"
#include "G4UserEventAction.hh"
#include "G4UserTrackingAction.hh"
#include "G4UserStackingAction.hh"
#include "G4UserSteppingAction.hh"
#include "G4VUserPhysicsList.hh"
#include "G4VModularPhysicsList.hh"
#include "G4VUserPrimaryGeneratorAction.hh"
Markus Frank
committed
#include "G4VUserActionInitialization.hh"
#include "G4VUserDetectorConstruction.hh"
Markus Frank
committed
// C/C++ include files
Markus Frank
committed
namespace {
G4Mutex action_mutex=G4MUTEX_INITIALIZER;
}
/// Namespace for the AIDA detector description toolkit
/// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
/// Sequence handler implementing common actions to all sequences.
Markus Frank
committed
template <typename T> class SequenceHdl {
public:
Markus Frank
committed
mutable Geant4Context* m_activeContext;
Markus Frank
committed
: m_sequence(0), m_activeContext(0) {
Markus Frank
committed
SequenceHdl(Geant4Context* ctxt, T* seq) : m_sequence(0), m_activeContext(ctxt) {
Markus Frank
committed
_aquire(seq);
Markus Frank
committed
_release();
Markus Frank
committed
InstanceCount::increment(this);
m_sequence = s;
Markus Frank
committed
void updateContext(Geant4Context* ctxt) {
m_activeContext = ctxt;
if ( m_sequence ) {
m_sequence->updateContext(ctxt);
}
}
Markus Frank
committed
releasePtr(m_sequence);
InstanceCount::decrement(this);
Markus Frank
committed
Geant4Context* context() const {
Markus Frank
committed
}
Geant4Kernel& kernel() const {
Markus Frank
committed
}
Markus Frank
committed
void configureFiber(Geant4Context* ctxt) {
if ( m_sequence ) {
m_sequence->configureFiber(ctxt);
}
}
void createClientContext(const G4Run* run) {
Markus Frank
committed
Geant4Run* r = new Geant4Run(run);
m_activeContext->setRun(r);
Markus Frank
committed
Geant4Run* r = m_activeContext->runPtr();
if ( r ) {
m_activeContext->setRun(0);
deletePtr(r);
}
}
void createClientContext(const G4Event* evt) {
Geant4Event* e = new Geant4Event(evt,Geant4Random::instance());
Markus Frank
committed
m_activeContext->setEvent(e);
}
void destroyClientContext(const G4Event*) {
Markus Frank
committed
Geant4Event* e = m_activeContext->eventPtr();
if ( e ) {
m_activeContext->setEvent(0);
deletePtr(e);
}
class Geant4UserRunAction;
class Geant4UserEventAction;
/// Concrete implementation of the Geant4 run action
/** @class Geant4UserRunAction
*
* @author M.Frank
* @version 1.0
*/
Markus Frank
committed
class Geant4UserRunAction :
public G4UserRunAction,
public SequenceHdl<Geant4RunActionSequence>
{
Markus Frank
committed
public:
Markus Frank
committed
Geant4UserRunAction(Geant4Context* ctxt, Geant4RunActionSequence* seq)
: Base(ctxt, seq), eventAction(0) {
Markus Frank
committed
updateContext(ctxt);
configureFiber(ctxt);
virtual ~Geant4UserRunAction() {
}
/// Begin-of-run callback
virtual void BeginOfRunAction(const G4Run* run);
virtual void EndOfRunAction(const G4Run* run);
/// Concrete implementation of the Geant4 event action
/** @class Geant4UserEventAction
*
* @author M.Frank
* @version 1.0
*/
Markus Frank
committed
class Geant4UserEventAction :
public G4UserEventAction,
public SequenceHdl<Geant4EventActionSequence>
{
Markus Frank
committed
public:
Markus Frank
committed
Geant4UserEventAction(Geant4Context* ctxt, Geant4EventActionSequence* seq)
: Base(ctxt, seq), runAction(0) {
Markus Frank
committed
updateContext(ctxt);
configureFiber(ctxt);
virtual ~Geant4UserEventAction() {
}
/// Begin-of-event callback
virtual void BeginOfEventAction(const G4Event* evt);
virtual void EndOfEventAction(const G4Event* evt);
/// Concrete implementation of the Geant4 tracking action
/** @class Geant4UserTrackingAction
*
* @author M.Frank
* @version 1.0
*/
Markus Frank
committed
class Geant4UserTrackingAction :
public G4UserTrackingAction,
public SequenceHdl<Geant4TrackingActionSequence>
{
Markus Frank
committed
public:
Markus Frank
committed
Geant4UserTrackingAction(Geant4Context* ctxt, Geant4TrackingActionSequence* seq)
: Base(ctxt, seq) {
Markus Frank
committed
updateContext(ctxt);
configureFiber(ctxt);
virtual ~Geant4UserTrackingAction() {
}
virtual void PreUserTrackingAction(const G4Track* trk) {
Markus Frank
committed
m_sequence->context()->kernel().setTrackMgr(fpTrackingManager);
virtual void PostUserTrackingAction(const G4Track* trk) {
m_sequence->end(trk);
Markus Frank
committed
m_sequence->context()->kernel().setTrackMgr(0);
/// Concrete implementation of the Geant4 stacking action sequence
/** @class Geant4UserStackingAction
*
* @author M.Frank
* @version 1.0
*/
Markus Frank
committed
class Geant4UserStackingAction :
public G4UserStackingAction,
public SequenceHdl<Geant4StackingActionSequence>
{
Markus Frank
committed
public:
Markus Frank
committed
Geant4UserStackingAction(Geant4Context* ctxt, Geant4StackingActionSequence* seq)
: Base(ctxt, seq) {
Markus Frank
committed
updateContext(ctxt);
configureFiber(ctxt);
virtual ~Geant4UserStackingAction() {
}
virtual void NewStage() {
m_sequence->newStage();
}
virtual void PrepareNewEvent() {
m_sequence->prepare();
}
Markus Frank
committed
/// Concrete implementation of the Geant4 generator action
/** @class Geant4UserGeneratorAction
*
* @author M.Frank
* @version 1.0
*/
Markus Frank
committed
class Geant4UserGeneratorAction :
public G4VUserPrimaryGeneratorAction,
public SequenceHdl<Geant4GeneratorActionSequence>
{
Markus Frank
committed
public:
Markus Frank
committed
Geant4UserGeneratorAction(Geant4Context* ctxt, Geant4GeneratorActionSequence* seq)
: G4VUserPrimaryGeneratorAction(), Base(ctxt, seq) {
Markus Frank
committed
updateContext(ctxt);
configureFiber(ctxt);
virtual ~Geant4UserGeneratorAction() {
}
Markus Frank
committed
virtual void GeneratePrimaries(G4Event* event);
/// Concrete implementation of the Geant4 stepping action
/** @class Geant4UserSteppingAction
*
* @author M.Frank
* @version 1.0
*/
Markus Frank
committed
class Geant4UserSteppingAction :
public G4UserSteppingAction,
public SequenceHdl<Geant4SteppingActionSequence>
{
Markus Frank
committed
public:
Markus Frank
committed
Geant4UserSteppingAction(Geant4Context* ctxt, Geant4SteppingActionSequence* seq)
: Base(ctxt, seq) {
Markus Frank
committed
updateContext(ctxt);
configureFiber(ctxt);
virtual ~Geant4UserSteppingAction() {
/// User stepping callback
virtual void UserSteppingAction(const G4Step* s) {
(*m_sequence)(s, fpSteppingManager);
Markus Frank
committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/// Concrete implementation of the Geant4 user detector construction action sequence
/** @class Geant4UserDetectorConstruction
*
* @author M.Frank
* @version 1.0
*/
class Geant4UserDetectorConstruction :
public G4VUserDetectorConstruction,
public SequenceHdl<Geant4DetectorConstructionSequence>
{
protected:
Geant4DetectorConstructionContext m_ctxt;
public:
/// Standard constructor
Geant4UserDetectorConstruction(Geant4Context* ctxt, Geant4DetectorConstructionSequence* seq)
: G4VUserDetectorConstruction(), Base(ctxt, seq),
m_ctxt(ctxt->kernel().lcdd(), this)
{
}
/// Default destructor
virtual ~Geant4UserDetectorConstruction() {
}
/// Call the actions for the construction of the sensitive detectors and the field
virtual void ConstructSDandField();
/// Call the actions to construct the detector geometry
virtual G4VPhysicalVolume* Construct();
};
/// Concrete implementation of the Geant4 user initialization action sequence
/** @class Geant4UserActionInitialization
*
* @author M.Frank
* @version 1.0
*/
class Geant4UserActionInitialization :
public G4VUserActionInitialization,
public SequenceHdl<Geant4UserInitializationSequence>
{
public:
/// Standard constructor
Geant4UserActionInitialization(Geant4Context* ctxt, Geant4UserInitializationSequence* seq)
: G4VUserActionInitialization(), Base(ctxt, seq) {
}
/// Default destructor
virtual ~Geant4UserActionInitialization() {
}
/// Build the actions for the worker thread
virtual void Build() const;
/// Build the action sequences for the master thread
virtual void BuildForMaster() const;
};
/// Begin-of-run callback
void Geant4UserRunAction::BeginOfRunAction(const G4Run* run) {
createClientContext(run);
Markus Frank
committed
kernel().executePhase("begin-run",(const void**)&run);
Markus Frank
committed
if ( m_sequence ) m_sequence->begin(run); // Action not mandatory
}
/// End-of-run callback
void Geant4UserRunAction::EndOfRunAction(const G4Run* run) {
Markus Frank
committed
if ( m_sequence ) m_sequence->end(run); // Action not mandatory
Markus Frank
committed
kernel().executePhase("end-run",(const void**)&run);
destroyClientContext(run);
}
/// Begin-of-event callback
void Geant4UserEventAction::BeginOfEventAction(const G4Event* evt) {
Markus Frank
committed
kernel().executePhase("begin-event",(const void**)&evt);
Markus Frank
committed
if ( m_sequence ) m_sequence->begin(evt); // Action not mandatory
}
/// End-of-event callback
void Geant4UserEventAction::EndOfEventAction(const G4Event* evt) {
Markus Frank
committed
if ( m_sequence ) m_sequence->end(evt); // Action not mandatory
Markus Frank
committed
kernel().executePhase("end-event",(const void**)&evt);
Markus Frank
committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/// Generate primary particles
void Geant4UserGeneratorAction::GeneratePrimaries(G4Event* event) {
createClientContext(event);
if ( m_sequence ) {
(*m_sequence)(event);
return;
}
throw std::runtime_error("GeneratePrimaries: Panic! No action sequencer defined. "
"No primary particles can be produced.");
}
/// Instantiate Geant4 sensitive detectors and electro-magnetic field
void Geant4UserDetectorConstruction::ConstructSDandField() {
G4AutoLock protection_lock(&action_mutex);
Geant4Context* ctx = m_sequence->context();
Geant4Kernel* krnl = 0;
try {
krnl = &kernel().worker(::pthread_self());
}
catch(...) {
}
if ( 0 == krnl ) {
krnl = &kernel().createWorker();
}
updateContext(krnl->workerContext());
m_sequence->constructField(&m_ctxt);
m_sequence->constructSensitives(&m_ctxt);
updateContext(ctx);
}
/// Construct electro magnetic field entity from the DD4hep field
G4VPhysicalVolume* Geant4UserDetectorConstruction::Construct() {
// The G4TransportationManager is thread-local.
// Thus, regardless of whether the field class object is global or local
// to a certain volume, a field object must be assigned to G4FieldManager.
G4AutoLock protection_lock(&action_mutex);
updateContext(m_sequence->context());
m_sequence->constructGeo(&m_ctxt);
if ( 0 == m_ctxt.world ) {
m_sequence->except("+++ Executing G4 detector construction did not result in a valid world volume!");
}
return m_ctxt.world;
}
/// Build the actions for the worker thread
void Geant4UserActionInitialization::Build() const {
G4AutoLock protection_lock(&action_mutex);
Geant4Kernel* krnl = 0;
try {
krnl = &kernel().worker(::pthread_self());
}
catch(...) {
}
if ( 0 == krnl ) {
krnl = &kernel().createWorker();
}
Geant4Context* ctx = krnl->workerContext();
if ( m_sequence ) {
Geant4Context* old = m_sequence->context();
m_sequence->info("+++ Executing Geant4UserActionInitialization::Build. "
"Context:%p Kernel:%p [%ld]", (void*)ctx, (void*)krnl, krnl->id());
m_sequence->updateContext(ctx);
m_sequence->build();
m_sequence->updateContext(old);
}
// Set user generator action sequence. Not optional, since event context is defined inside
Geant4UserGeneratorAction* gen_action = new Geant4UserGeneratorAction(ctx,krnl->generatorAction(false));
SetUserAction(gen_action);
// Set the run action sequence. Not optional, since run context is defined/destroyed inside
Geant4UserRunAction* run_action = new Geant4UserRunAction(ctx,krnl->runAction(false));
SetUserAction(run_action);
// Set the event action sequence. Not optional, since event context is destroyed inside
Geant4UserEventAction* evt_action = new Geant4UserEventAction(ctx,krnl->eventAction(false));
run_action->eventAction = evt_action;
evt_action->runAction = run_action;
SetUserAction(evt_action);
// Set the tracking action sequence
Geant4TrackingActionSequence* trk_action = krnl->trackingAction(false);
if ( trk_action ) {
Geant4UserTrackingAction* action = new Geant4UserTrackingAction(ctx, trk_action);
SetUserAction(action);
}
// Set the stepping action sequence
Geant4SteppingActionSequence* stp_action = krnl->steppingAction(false);
if ( stp_action ) {
Geant4UserSteppingAction* action = new Geant4UserSteppingAction(ctx, stp_action);
SetUserAction(action);
}
// Set the stacking action sequence
Geant4StackingActionSequence* stk_action = krnl->stackingAction(false);
if ( stk_action ) {
Geant4UserStackingAction* action = new Geant4UserStackingAction(ctx, stk_action);
SetUserAction(action);
}
}
/// Build the action sequences for the master thread
void Geant4UserActionInitialization::BuildForMaster() const {
if ( m_sequence ) {
m_sequence->info("+++ Executing Geant4UserActionInitialization::BuildForMaster....");
m_sequence->buildMaster();
}
}
#include "DDG4/Geant4DetectorConstruction.h"
#include "DDG4/Geant4UserPhysicsList.h"
#include "DDG4/Geant4Kernel.h"
using namespace std;
using namespace DD4hep;
using namespace DD4hep::Simulation;
// Geant4 include files
#include "G4RunManager.hh"
Markus Frank
committed
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
class Geant4Compatibility {
public:
Geant4Compatibility() {}
Geant4DetectorConstructionSequence* buildDefaultDetectorConstruction(Geant4Kernel& kernel);
};
Geant4DetectorConstructionSequence* Geant4Compatibility::buildDefaultDetectorConstruction(Geant4Kernel& kernel) {
Geant4Action* cr;
Geant4DetectorConstruction* det_cr;
Geant4Context* ctx = kernel.workerContext();
Geant4DetectorConstructionSequence* seq = kernel.detectorConstruction(true);
printout(WARNING, "Geant4Exec", "+++ Building default Geant4DetectorConstruction for single threaded compatibility.");
// Attach first the geometry converter from DD4hep to Geant4
cr = PluginService::Create<Geant4Action*>("Geant4DetectorGeometryConstruction",ctx,string("ConstructGeometry"));
det_cr = dynamic_cast<Geant4DetectorConstruction*>(cr);
if ( det_cr )
seq->adopt(det_cr);
else
throw runtime_error("Panic! Failed to build Geant4DetectorGeometryConstruction.");
// Attach the sensitive detector manipulator:
cr = PluginService::Create<Geant4Action*>("Geant4DetectorSensitivesConstruction",ctx,string("ConstructSensitives"));
det_cr = dynamic_cast<Geant4DetectorConstruction*>(cr);
if ( det_cr )
seq->adopt(det_cr);
else
throw runtime_error("Panic! Failed to build Geant4DetectorSensitivesConstruction.");
return seq;
}
int Geant4Exec::configure(Geant4Kernel& kernel) {
CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine);
Geometry::LCDD& lcdd = kernel.lcdd();
Markus Frank
committed
Geant4Context* ctx = kernel.workerContext();
Geant4Random* rndm = Geant4Random::instance(false);
if ( !rndm ) {
rndm = new Geant4Random(ctx, "Geant4Random");
/// Initialize the engine etc.
rndm->initialize();
}
Markus Frank
committed
kernel.executePhase("configure",0);
// Construct the default run manager
G4RunManager& runManager = kernel.runManager();
// Check if the geometry was loaded
printout(WARNING, "Geant4Exec", "+++ Only %d subdetectors present. "
Markus Frank
committed
"You sure you loaded the geometry properly?",
int(lcdd.sensitiveDetectors().size()));
Markus Frank
committed
// Get the detector constructed
Geant4DetectorConstructionSequence* user_det = kernel.detectorConstruction(false);
if ( 0 == user_det && kernel.isMultiThreaded() ) {
throw runtime_error("Panic! No valid detector construction sequencer present. [Mandatory MT]");
}
if ( 0 == user_det && !kernel.isMultiThreaded() ) {
user_det = Geant4Compatibility().buildDefaultDetectorConstruction(kernel);
Markus Frank
committed
Geant4UserDetectorConstruction* det_seq = new Geant4UserDetectorConstruction(ctx,user_det);
runManager.SetUserInitialization(det_seq);
// Get the physics list constructed
Geant4PhysicsListActionSequence* phys_seq = kernel.physicsList(false);
if ( 0 == phys_seq ) {
string phys_model = "QGSP_BERT";
phys_seq = kernel.physicsList(true);
phys_seq->property("extends").set<string>(phys_model);
}
G4VUserPhysicsList* physics = phys_seq->extensionList();
if (0 == physics) {
throw runtime_error("Panic! No valid user physics list present!");
}
runManager.SetUserInitialization(physics);
Markus Frank
committed
// Construct the remaining user initialization in multi-threaded mode
Geant4UserInitializationSequence* user_init = kernel.userInitialization(false);
if ( 0 == user_init && kernel.isMultiThreaded() ) {
throw runtime_error("Panic! No valid user initialization sequencer present. [Mandatory MT]");
Markus Frank
committed
else if ( 0 == user_init && !kernel.isMultiThreaded() ) {
// Use default actions registered to the default kernel. Will do the right thing...
user_init = kernel.userInitialization(true);
Markus Frank
committed
Geant4UserActionInitialization* init = new Geant4UserActionInitialization(ctx,user_init);
runManager.SetUserInitialization(init);
return 1;
}
/// Initialize the simulation
int Geant4Exec::initialize(Geant4Kernel& kernel) {
// Construct the default run manager
G4RunManager& runManager = kernel.runManager();
//
// Initialize G4 engine
//
Markus Frank
committed
kernel.executePhase("initialize",0);
runManager.Initialize();
return 1;
}
/// Run the simulation
int Geant4Exec::run(Geant4Kernel& kernel) {
Property& p = kernel.property("UI");
string value = p.value<string>();
Markus Frank
committed
kernel.executePhase("start",0);
if ( !value.empty() ) {
Geant4Action* ui = kernel.globalAction(value);
if ( ui ) {
Geant4Call* c = dynamic_cast<Geant4Call*>(ui);
if ( c ) {
Markus Frank
committed
(*c)(0);
Markus Frank
committed
return 1;
ui->except("++ Geant4Exec: Failed to start UI interface.");
throw runtime_error(format("Geant4Exec","++ Failed to locate UI interface %s.",value.c_str()));
long nevt = kernel.property("NumEvents").value<long>();
Markus Frank
committed
kernel.executePhase("stop",0);
return 1;
}
/// Run the simulation
Markus Frank
committed
int Geant4Exec::terminate(Geant4Kernel& kernel) {
kernel.executePhase("terminate",0);