31 #include "G4TrackStatus.hh"
32 #include "G4PrimaryVertex.hh"
33 #include "G4PrimaryParticle.hh"
34 #include "G4TrackingManager.hh"
35 #include "G4ParticleDefinition.hh"
36 #include "CLHEP/Units/SystemOfUnits.h"
45 using namespace DD4hep;
46 using namespace DD4hep::Simulation;
51 Geant4ParticleHandler::Geant4ParticleHandler(
Geant4Context* ctxt,
const string& nam)
97 except(
"Cannot add an invalid user particle handler object [Invalid-object-type].",
c_name());
99 except(
"Cannot add an user particle handler object [Object-exists].",
c_name());
101 except(
"Cannot add an invalid user particle handler object [NULL-object].",
c_name());
120 except(
"Cannot mark the G4Track if the pointer is invalid!",
c_name());
126 mark(step_value->GetTrack(),reason);
129 except(
"Cannot mark the G4Track if the step-pointer is invalid!",
c_name());
135 mark(step_value->GetTrack());
138 except(
"Cannot mark the G4Track if the step-pointer is invalid!",
c_name());
147 G4LogicalVolume* vol = track->GetVolume()->GetLogicalVolume();
151 if ( typ ==
"calorimeter" )
153 else if ( typ ==
"tracker" )
164 debug(
"+++ Event:%d Add EVENT extension of type Geant4ParticleHandler.....",event->GetEventID());
175 typedef vector<const G4Track*> _Sec;
183 const _Sec* sec=step_value->GetSecondaryInCurrentStep();
184 if ( not sec->empty() ) {
199 const G4ThreeVector&
v = h.
vertex();
201 const G4PrimaryParticle* prim = h.
primary();
207 except(
"+++ Tracking preaction: Primary particle without generator particle!");
286 G4ThreeVector
m = track->GetMomentum();
287 const G4ThreeVector& p = track->GetPosition();
300 const G4Step* theLastStep = track->GetStep();
301 G4StepPoint* theLastPostStepPoint = NULL;
302 if(theLastStep) theLastPostStepPoint = theLastStep->GetPostStepPoint();
303 if( theLastPostStepPoint &&
304 ( theLastPostStepPoint->GetStepStatus() == fWorldBoundary
311 if(track->GetKineticEnergy() <= 0.) {
336 else part = (*ip).second;
349 ParticleMap::iterator ip;
352 pid = (*iequiv).second;
355 (*ip).second->reason |= track_reason;
364 info(
"+++ Event %d Begin event action. Access event related information.",event->GetEventID());
415 ParticleMap::const_iterator ipar, iend, i;
423 for(count = 0, iend=pm.end(), i=pm.begin(); i!=iend; ++i) {
425 orgParticles[p->
id] = p->
id;
426 finalParticles[p->
id] = p;
427 if ( p->
id > count ) count = p->
id;
437 orgParticles[p->
id] = count;
438 finalParticles[count] = p;
445 int g4_equiv = (*ie).first;
448 if ( iequiv == ie_end ) {
451 g4_equiv = (*iequiv).second;
453 TrackEquivalents::mapped_type equiv = (*ie).second;
455 equivalents[(*ie).first] = (*ipar).second->id;
457 const G4ParticleDefinition* def = p.
definition();
458 int pdg = int(fabs(def->GetPDGEncoding())+0.1);
459 if ( pdg != 0 && pdg<36 && !(pdg > 10 && pdg < 17) && pdg != 22 ) {
460 error(
"+++ ERROR: Geant4 particle for track:%d last known is:%d -- is gluon or quark!",equiv,g4_equiv);
462 pdg = int(fabs(p->
pdgID)+0.1);
463 if ( pdg != 0 && pdg<36 && !(pdg > 10 && pdg < 17) && pdg != 22 ) {
464 error(
"+++ ERROR(2): Geant4 particle for track:%d last known is:%d -- is gluon or quark!",equiv,g4_equiv);
468 error(
"+++ No Equivalent particle for track:%d last known is:%d",equiv,g4_equiv);
478 int equiv_id = equivalents[p->
g4Parent];
479 if ( (ipar=finalParticles.find(equiv_id)) != finalParticles.end() ) {
485 error(
"+++ Inconsistency in particle record: Geant4 parent %d "
486 "of particle %d (equiv:%d) not in record!",
505 if ( mask.
isNull() || (secondaries && low_energy && !hits_produced) ) {
509 else if ( !hits_produced && low_energy ) {
513 else if ( !tracker_track && calo_track && low_energy ) {
563 Particle* parent_part = (*ip).second;
576 int g4_id = (*i).first;
578 remove.insert(g4_id);
581 Particle* parent_part = (*ip).second;
592 for(set<int>::const_iterator r=
remove.
begin(); r!=
remove.end();++r) {
595 (*ir).second->release();
599 return int(
remove.size());
613 for(set<int>::const_iterator
id=daughters.begin();
id!=daughters.end(); ++id) {
617 error(
"+++ Particle:%d Daughter %d is not in particle map!",p->
id,id_dau);
624 bool in_map =
false, in_parent_list =
false;
627 parent_id = (*eq_it).second;
631 if ( !in_map || !in_parent_list ) {
632 char parent_list[1024];
636 for(set<int>::const_iterator ip=p->
parents.begin(); ip!=p->
parents.end();++ip)
637 ::snprintf(parent_list+strlen(parent_list),
sizeof(parent_list)-strlen(parent_list),
"%d ",*ip);
638 error(
"+++ Particle:%d Parent %d (G4id:%d) In record:%s In parent list:%s [%s]",
644 if ( num_errors > 0 ) {
645 except(
"+++ Consistency check failed. Found %d problems.",num_errors);
652 ParticleMap::const_iterator iend, i;
653 for(iend=pm.end(), i=pm.begin(); i!=iend; ++i) {
661 const double X( parent->vex - p->
vsx );
662 const double Y( parent->vey - p->
vsy );
663 const double Z( parent->vez - p->
vsz );
PrintLevel outputLevel() const
Access the output level.
void clear()
Clear particle maps.
Geant4ParticleMap::ParticleMap ParticleMap
Geant4Event & event() const
Access the geant4 event – valid only between BeginEvent() and EndEvent()!
TrackEquivalents m_equivalentTracks
Map associating the G4Track identifiers with identifiers of existing MCParticles. ...
virtual ~Geant4ParticleHandler()
Default destructor.
Geant4Particle * addRef()
Increase reference count.
const G4VProcess * process
not persisten. ROOT cannot handle
int nextPID()
Access a new particle identifier within the interaction.
Processes m_processNames
Property: All the processes of which the decay products will be explicitly stored.
double m_kinEnergyCut
Property: Energy cut below which particles are not collected, but assigned to the parent...
Geant4TrackingActionSequence & trackingAction() const
Access to the main tracking action sequence from the kernel object.
Data structure to store the MC particle information.
void adopt(ParticleMap &pm, TrackEquivalents &equiv)
Adopt particle maps.
void dumpWithVertex(int level, const std::string &src, const char *tag) const
Output type 3:+++ "tag" ID: 0 e- status:00000014 type: 11 Vertex:(+0.00e+00,+0.00e+00,+0.00e+00) [mm] time: +0.00e+00 [ns] #Par: 0 #Dau: 4.
virtual void generate(G4Event *event, Geant4ParticleHandler *handler)
Event generation action callback.
ReferenceBitMask< int > PropertyMask
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Default Interface class to handle monte carlo truth records.
bool m_printStartTracking
Property: Steer printout at tracking action begin.
int parent() const
Track's parent identifier.
G4ThreeVector momentum() const
static void decrement(T *)
Decrement count according to type information.
const char * yes_no(bool value)
Helper function to print booleans in format YES/NO.
virtual void begin(const G4Event *event)
Pre-event action callback.
Interface class to access properties of the underlying Geant4 sensitive detector structure.
ParticleMap m_particleMap
Map with stored MC Particles.
void rebaseSimulatedTracks(int base)
Rebase the simulated tracks, so that they fit to the generator particles.
void callAtEnd(Q *p, void(T::*f)(const G4Event *))
Register end-of-event callback.
void dump4(int level, const std::string &src, const char *tag) const
double kineticEnergy() const
Track's kinetic energy.
Helper class to ease the extraction of information from a G4Track object.
void call(Q *p, void(T::*f)(const G4Step *, G4SteppingManager *))
Register stepping action callback. Types Q and T must be polymorph!
Class modelling a single interaction with multiple primary vertices and particles.
Geant4ParticleMap::TrackEquivalents TrackEquivalents
void dump2(int level, const std::string &src, const char *tag, int g4id, bool inrec) const
Output type 2:+++ "tag" 20 G4: 7 def:0xde4eaa8 [gamma , gamma] reason: 20 E:+3.304035e+01 in record:Y...
void dumpMap(const char *tag) const
Debugging: Dump Geant4 particle map.
virtual void beginEvent(const G4Event *event)
Pre-event action callback.
virtual const std::string & sensitiveType() const =0
Access to the sensitive type of the detector.
void checkConsistency() const
Check the record consistency.
Geant4ParticleHandler user extension action called by the particle handler.
void error(const char *fmt,...) const
Support of error messages.
Data structure to manipulate a bitmask held by reference and represented by an integer.
const char * c_name() const
Access name of the action.
Geant4Context * context() const
Access the context.
Data structure to map primaries to particles.
Geant4ParticleHandler()
No default constructor.
const std::string & name() const
Access name of the action.
void callAtBegin(Q *p, void(T::*f)(const G4Event *))
Register begin-of-event callback.
virtual void mark(const G4Track *track)
Mark a Geant4 track to be kept for later MC truth analysis. Default flag: CREATED_HIT.
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
void releasePtr(T &p)
Helper to delete objects from heap and reset the pointer. Saves many many lines of code...
void setVertexEndpointBit()
set the endpointIsNotVertexOfParentFlag at the end of the event
Geant4PrimaryMap * m_primaryMap
Primary map.
virtual void combine(Particle &to_be_deleted, Particle &remaining_parent)
Callback when parent should be combined.
virtual void step(const G4Step *step, G4SteppingManager *mgr, Particle &particle)
User stepping callback.
Particles parents
The list of daughters of this MC particle.
void * addExtension(void *ptr, const std::type_info &info, destruct_t dtor)
Add an extension object to the detector element.
double m_minDistToParentVertex
Property: Minimal distance after which the vertexIsNotEndpointOfParent flag is set.
virtual void end(const G4Track *track)
Post-track action callback.
bool anySet(const T &m) const
ParticleMap particles
The map of particles participating in this primary interaction.
Data structure to map particles produced during the generation and the simulation.
long addRef()
Increase reference count.
const G4ParticleDefinition * definition() const
Access the Geant4 particle definition object (expensive!)
Geant4Action to collect the MC particle information.
Geant4SteppingActionSequence & steppingAction() const
Access to the main stepping action sequence from the kernel object.
T * extension(bool alert=true)
Access to type safe extension object. Exception is thrown if the object is invalid.
static T::const_iterator find(const T &c, const string &s)
virtual void begin(const G4Track *track)
Pre-track action callback.
void callUpFront(Q *p, void(T::*f)(const G4Track *), CallbackSequence::Location where=CallbackSequence::END)
Register Pre-track action callback before anything else.
virtual void end(const G4Event *event)
Post-event action callback.
Particle m_currTrack
Local buffer about the 'current' G4Track.
Concrete implementation of the Geant4 generator action base class.
void releaseObjects(M &m)
Geant4Particle * get(const G4PrimaryParticle *particle)
Access DDG4 particle by G4 primary particle.
bool m_keepAll
Property: Flag to keep all particles generated.
int recombineParents()
Recombine particles and associate the to parents with cleanup.
double globalTime() const
Track global time.
virtual bool keepParticle(Particle &particle)
Callback to be answered if the particle MUST be kept during recombination step.
void callAtFinal(Q *p, void(T::*f)(const G4Track *), CallbackSequence::Location where=CallbackSequence::END)
Register Post-track action callback.
bool m_needsControl
Default property: Flag to create control instance.
G4ParticleDefinition * trackDef() const
Track's particle definition.
Data structure to access derived MC particle information.
Geant4ParticleHandler & operator=(const Geant4ParticleHandler &c)
No assignment operator.
static void increment(T *)
Increment count according to type information.
Geant4ParticleMap::Particle Particle
virtual void step(const G4Step *step, G4SteppingManager *mgr)
User stepping callback.
static bool defaultKeepParticle(Particle &particle)
Default callback to be answered if the particle should be kept if NO user handler is installed...
Generic context to extend user, run and event information.
bool m_printEndTracking
Property: Steer printout at tracking action end.
int m_globalParticleID
Global particle identifier. Obtained at the begin of the event.
const G4ThreeVector & vertex() const
Track's vertex position, where the track was created.
bool adopt(Geant4Action *action)
Adopt the user particle handler.
virtual void operator()(G4Event *event)
Event generation action callback.
Geant4UserParticleHandler * m_userHandler
User action pointer.
const G4VProcess * creatorProcess() const
Physical process of the track generation.
void info(const char *fmt,...) const
Support of info messages.
Geant4Particle & get_data(Geant4Particle &c)
Assignment operator.
Geant4EventActionSequence & eventAction() const
Access to the main event action sequence from the kernel object.
virtual void endEvent(const G4Event *event)
Post-event action callback.
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
bool isSet(const T &m) const
Default base class for all Geant 4 actions and derivates thereof.
int id() const
Track's identifier.
void debug(const char *fmt,...) const
Support of debug messages.
const G4PrimaryParticle * primary() const
Access the primary particle of the track object (if present)
void dumpWithMomentum(int level, const std::string &src, const char *tag) const
Output type 3:+++ <tag> ID: 0 e- status:00000014 type: 11 Vertex:(+0.00e+00,+0.00e+00,+0.00e+00) [mm] time: +0.00e+00 [ns] #Par: 0 #Dau: 4.