From 8360c6e25f527922abf8f288a1f749de9392fe98 Mon Sep 17 00:00:00 2001 From: lintao <lintao51@gmail.com> Date: Mon, 18 Jul 2022 09:34:06 +0800 Subject: [PATCH] copy the Geant4Hits from DD4hep v01-18. --- Simulation/DetSimSD/CMakeLists.txt | 4 + .../DetSimSD/include/DetSimSD/Geant4Hits.h | 201 ++++++++++++++++++ Simulation/DetSimSD/src/Geant4Hits.cpp | 135 ++++++++++++ 3 files changed, 340 insertions(+) create mode 100644 Simulation/DetSimSD/include/DetSimSD/Geant4Hits.h create mode 100644 Simulation/DetSimSD/src/Geant4Hits.cpp diff --git a/Simulation/DetSimSD/CMakeLists.txt b/Simulation/DetSimSD/CMakeLists.txt index 210e3639..1dbfc6f8 100644 --- a/Simulation/DetSimSD/CMakeLists.txt +++ b/Simulation/DetSimSD/CMakeLists.txt @@ -16,6 +16,10 @@ gaudi_add_module(DetSimSD src/GenericTrackerSensDetTool.cpp src/GenericTrackerSensitiveDetector.cpp src/TrackerCombineSensitiveDetector.cpp + + src/Geant4Hits.cpp + + LINK DetSimInterface DetInterface ${DD4hep_COMPONENT_LIBRARIES} diff --git a/Simulation/DetSimSD/include/DetSimSD/Geant4Hits.h b/Simulation/DetSimSD/include/DetSimSD/Geant4Hits.h new file mode 100644 index 00000000..60fc18d4 --- /dev/null +++ b/Simulation/DetSimSD/include/DetSimSD/Geant4Hits.h @@ -0,0 +1,201 @@ +//========================================================================== +// AIDA Detector description implementation +//-------------------------------------------------------------------------- +// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN) +// All rights reserved. +// +// For the licensing terms see $DD4hepINSTALL/LICENSE. +// For the list of contributors see $DD4hepINSTALL/doc/CREDITS. +// +// Author : M.Frank +// +//========================================================================== + +#ifndef DDG4_GEANT4HITS_H +#define DDG4_GEANT4HITS_H + +// Framework include files +#include "DD4hep/Objects.h" +#include "DDG4/Geant4StepHandler.h" + +// Geant4 include files +#include "G4VHit.hh" +#include "G4Step.hh" +#include "G4StepPoint.hh" + +/// Namespace for the AIDA detector description toolkit +namespace dd4hep { + + /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit + namespace sim { + + // Forward declarations; + class Geant4Hit; + class Geant4TrackerHit; + class Geant4CalorimeterHit; + + /// Deprecated: Base class for hit comparisons. + /** @class HitCompare Geant4Hits.h DDG4/Geant4Hits.h + * + * @author M.Frank + * @version 1.0 + */ + template <class HIT> class HitCompare { + public: + /// Default destructor + virtual ~HitCompare() {} + /// Comparison function + virtual bool operator()(const HIT* h) const = 0; + }; + + /// Deprecated: Seek the hits of an arbitrary collection for the same position. + /** @class HitPositionCompare Geant4Hits.h DDG4/Geant4Hits.h + * + * @author M.Frank + * @version 1.0 + */ + template <class HIT> struct HitPositionCompare: public HitCompare<HIT> { + /// Reference to the hit position + const Position& pos; + /// Constructor + HitPositionCompare(const Position& p) : pos(p) {} + /// Default destructor + virtual ~HitPositionCompare() {} + /// Comparison function + virtual bool operator()(const HIT* h) const { + return pos == h->position; + } + }; + + /// Deprecated: basic geant4 hit class for deprecated sensitive detectors + /** @class Geant4Hit Geant4Hits.h DDG4/Geant4Hits.h + * + * Geant4 hit base class. Here only the basic + * quantites are stored such as the energy deposit and + * the time stamp. + * + * @author M.Frank + * @version 1.0 + */ + class Geant4Hit: public G4VHit { + public: + + // cellID + unsigned long cellID = 0; + + /// Deprecated!!! + struct MonteCarloContrib { + /// Geant 4 Track identifier + int trackID = -1; + /// Particle ID from the PDG table + int pdgID = -1; + /// Total energy deposit in this hit + double deposit = 0.0; + /// Timestamp when this energy was deposited + double time = 0.0; + /// Default constructor + MonteCarloContrib() = default; + /// Copy constructor + MonteCarloContrib(const MonteCarloContrib& c) = default; + /// Initializing constructor + MonteCarloContrib(int track_id, double dep, double time_stamp) + : trackID(track_id), pdgID(-1), deposit(dep), time(time_stamp) {} + /// Initializing constructor + MonteCarloContrib(int track_id, int pdg, double dep, double time_stamp) + : trackID(track_id), pdgID(pdg), deposit(dep), time(time_stamp) {} + /// Assignment operator + MonteCarloContrib& operator=(const MonteCarloContrib& c) = default; + /// Clear data + void clear() { + time = deposit = 0.0; + pdgID = trackID = -1; + } + }; + typedef MonteCarloContrib Contribution; + typedef std::vector<MonteCarloContrib> Contributions; + + public: + /// Standard constructor + Geant4Hit() = default; + /// Default destructor + virtual ~Geant4Hit() { } + /// Check if the Geant4 track is a Geantino + static bool isGeantino(G4Track* track); + /// Extract the MC contribution for a given hit from the step information + static Contribution extractContribution(G4Step* step); + }; + + /// Deprecated: Geant4 tracker hit class for deprecated sensitive detectors + /** @class Geant4TrackerHit Geant4Hits.h DDG4/Geant4Hits.h + * + * Geant4 tracker hit class. Tracker hits contain the momentum + * direction as well as the hit position. + * + * @author M.Frank + * @version 1.0 + */ + class Geant4TrackerHit: public Geant4Hit { + public: + /// Hit position + Position position; + /// Hit direction + Direction momentum; + /// Length of the track segment contributing to this hit + double length; + /// Monte Carlo / Geant4 information + Contribution truth; + /// Energy deposit of the hit + double energyDeposit; + + public: + /// Default constructor + Geant4TrackerHit(); + /// Standard initializing constructor + Geant4TrackerHit(int track_id, int pdg_id, double deposit, double time_stamp); + /// Default destructor + virtual ~Geant4TrackerHit() {} + /// Clear hit content + Geant4TrackerHit& clear(); + /// Store Geant4 point and step information into tracker hit structure. + Geant4TrackerHit& storePoint(G4Step* step, G4StepPoint* point); + + /// Assignment operator + Geant4TrackerHit& operator=(const Geant4TrackerHit& c); + /// Geant4 required object allocator + void *operator new(size_t); + /// Geat4 required object destroyer + void operator delete(void *ptr); + }; + + /// Deprecated: Geant4 calorimeter hit class for deprecated sensitive detectors + /** @class Geant4CalorimeterHit Geant4Hits.h DDG4/Geant4Hits.h + * + * Geant4 calorimeter hit class. Calorimeter hits contain the momentum + * direction as well as the hit position. + * + * @author M.Frank + * @version 1.0 + */ + class Geant4CalorimeterHit: public Geant4Hit { + public: + /// Hit position + Position position; + /// Hit contributions by individual particles + Contributions truth; + /// Total energy deposit + double energyDeposit; + public: + /// Standard constructor + Geant4CalorimeterHit(const Position& cell_pos); + /// Default destructor + virtual ~Geant4CalorimeterHit() { } + /// Geant4 required object allocator + void *operator new(size_t); + /// Geat4 required object destroyer + void operator delete(void *ptr); + }; + + } // End namespace sim +} // End namespace dd4hep + +#endif // DDG4_GEANT4HITS_H diff --git a/Simulation/DetSimSD/src/Geant4Hits.cpp b/Simulation/DetSimSD/src/Geant4Hits.cpp new file mode 100644 index 00000000..0358bf01 --- /dev/null +++ b/Simulation/DetSimSD/src/Geant4Hits.cpp @@ -0,0 +1,135 @@ +//========================================================================== +// AIDA Detector description implementation +//-------------------------------------------------------------------------- +// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN) +// All rights reserved. +// +// For the licensing terms see $DD4hepINSTALL/LICENSE. +// For the list of contributors see $DD4hepINSTALL/doc/CREDITS. +// +// Author : M.Frank +// +//========================================================================== + +// Framework include files +#include "DetSimSD/Geant4Hits.h" + +// Geant4 include files +#include "G4Allocator.hh" +#include "G4ParticleDefinition.hh" +#include "G4ChargedGeantino.hh" +#include "G4OpticalPhoton.hh" +#include "G4Geantino.hh" + +// C/C++ include files +#include <iostream> + +using namespace std; +using namespace dd4hep::sim; + +G4ThreadLocal G4Allocator<Geant4TrackerHit>* TrackerHitAllocator = 0; +G4ThreadLocal G4Allocator<Geant4CalorimeterHit>* CalorimeterHitAllocator = 0; + + +/// Check if the Geant4 track is a Geantino +bool Geant4Hit::isGeantino(G4Track* track) { + if (track) { + G4ParticleDefinition* def = track->GetDefinition(); + if (def == G4ChargedGeantino::Definition()) + return true; + if (def == G4Geantino::Definition()) { + return true; + } + } + return false; +} + +Geant4Hit::Contribution Geant4Hit::extractContribution(G4Step* step) { + G4Track* trk = step->GetTrack(); + double energy_deposit = + (trk->GetDefinition() == G4OpticalPhoton::OpticalPhotonDefinition()) ? + trk->GetTotalEnergy() : step->GetTotalEnergyDeposit(); + Contribution contrib(trk->GetTrackID(), trk->GetDefinition()->GetPDGEncoding(), energy_deposit, trk->GetGlobalTime()); + return contrib; +} + +/// Default constructor +Geant4TrackerHit::Geant4TrackerHit() + : Geant4Hit(), position(), momentum(), length(0.0), truth(), energyDeposit(0.0) { +} + +/// Standard initializing constructor +Geant4TrackerHit::Geant4TrackerHit(int track_id, int pdg_id, double deposit, double time_stamp) + : Geant4Hit(), position(), momentum(), length(0.0), truth(track_id, pdg_id, deposit, time_stamp), energyDeposit(deposit) { +} + +/// Assignment operator +Geant4TrackerHit& Geant4TrackerHit::operator=(const Geant4TrackerHit& c) { + if ( this != &c ) { + position = c.position; + momentum = c.momentum; + length = c.length; + truth = c.truth; + energyDeposit = c.energyDeposit; + } + return *this; +} + +/// Clear hit content +Geant4TrackerHit& Geant4TrackerHit::clear() { + position.SetXYZ(0, 0, 0); + momentum.SetXYZ(0, 0, 0); + length = 0.0; + truth.clear(); + energyDeposit = 0.0; + return *this; +} + +/// Store Geant4 point and step information into tracker hit structure. +Geant4TrackerHit& Geant4TrackerHit::storePoint(G4Step* step, G4StepPoint* pnt) { + G4Track* trk = step->GetTrack(); + G4ThreeVector pos = pnt->GetPosition(); + G4ThreeVector mom = pnt->GetMomentum(); + + truth.trackID = trk->GetTrackID(); + truth.pdgID = trk->GetDefinition()->GetPDGEncoding(); + truth.deposit = step->GetTotalEnergyDeposit(); + truth.time = trk->GetGlobalTime(); + energyDeposit = step->GetTotalEnergyDeposit(); + position.SetXYZ(pos.x(), pos.y(), pos.z()); + momentum.SetXYZ(mom.x(), mom.y(), mom.z()); + length = 0; + return *this; +} + +/// Geant4 required object allocator +void* Geant4TrackerHit::operator new(size_t) { + if ( TrackerHitAllocator ) + return TrackerHitAllocator->MallocSingle(); + TrackerHitAllocator = new G4Allocator<Geant4TrackerHit>; + return TrackerHitAllocator->MallocSingle(); +} + +/// Geat4 required object destroyer +void Geant4TrackerHit::operator delete(void *p) { + TrackerHitAllocator->FreeSingle((Geant4TrackerHit*) p); +} + +/// Standard constructor +Geant4CalorimeterHit::Geant4CalorimeterHit(const Position& pos) + : Geant4Hit(), position(pos), truth(), energyDeposit(0) { +} + +/// Geant4 required object allocator +void* Geant4CalorimeterHit::operator new(size_t) { + if ( CalorimeterHitAllocator ) + return CalorimeterHitAllocator->MallocSingle(); + CalorimeterHitAllocator = new G4Allocator<Geant4CalorimeterHit>; + return CalorimeterHitAllocator->MallocSingle(); +} + +/// Geat4 required object destroyer +void Geant4CalorimeterHit::operator delete(void *p) { + CalorimeterHitAllocator->FreeSingle((Geant4CalorimeterHit*) p); +} + -- GitLab