diff --git a/Simulation/DetSimAna/CMakeLists.txt b/Simulation/DetSimAna/CMakeLists.txt index 883dfd8215c9ccfd9e88b7d1713c442de1f5e151..b1bd935f32e1de1bb87d8c490f5967960deda351 100644 --- a/Simulation/DetSimAna/CMakeLists.txt +++ b/Simulation/DetSimAna/CMakeLists.txt @@ -5,12 +5,17 @@ include(${Geant4_USE_FILE}) gaudi_add_module(DetSimAna SOURCES src/Edm4hepWriterAnaElemTool.cpp LINK DetSimInterface + DetSimSDLib ${DD4hep_COMPONENT_LIBRARIES} Gaudi::GaudiKernel EDM4HEP::edm4hep EDM4HEP::edm4hepDict k4FWCore::k4FWCore ) +target_include_directories(DetSimAna PUBLIC + $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>/include + $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) + install(TARGETS DetSimAna EXPORT CEPCSWTargets RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT bin diff --git a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp index b05e7994b44cc54d2dbda14039a3700348fcc704..9de89e9447aec654c51a1567f9dd482cfcce00fe 100644 --- a/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp +++ b/Simulation/DetSimAna/src/Edm4hepWriterAnaElemTool.cpp @@ -12,7 +12,7 @@ #include "DDG4/Geant4Mapping.h" #include "DDG4/Geant4HitCollection.h" #include "DDG4/Geant4Data.h" -#include "DDG4/Geant4Hits.h" +#include "DetSimSD/Geant4Hits.h" DECLARE_COMPONENT(Edm4hepWriterAnaElemTool) diff --git a/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp b/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp index d912414d4108a8f45efdb394d26ddcfab5fc0d21..1e11da48b5c8872ff1553c95025597a2a7db251c 100644 --- a/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp +++ b/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp @@ -9,7 +9,7 @@ #include "DDG4/Geant4Mapping.h" #include "DDG4/Geant4HitCollection.h" #include "DDG4/Geant4Data.h" -#include "DDG4/Geant4Hits.h" +#include "DetSimSD/Geant4Hits.h" DECLARE_COMPONENT(ExampleAnaElemTool) diff --git a/Simulation/DetSimSD/CMakeLists.txt b/Simulation/DetSimSD/CMakeLists.txt index 210e3639bd4ea751e65e55ba8fc87955dc56fb96..f3a9618ac625fce03d1c277bcef6be28e355628e 100644 --- a/Simulation/DetSimSD/CMakeLists.txt +++ b/Simulation/DetSimSD/CMakeLists.txt @@ -2,21 +2,34 @@ find_package(Geant4 REQUIRED ui_all vis_all) include(${Geant4_USE_FILE}) +gaudi_add_library(DetSimSDLib + SOURCES src/Geant4Hits.cpp + src/DDG4SensitiveDetector.cpp + src/CaloSensitiveDetector.cpp + src/DriftChamberSensitiveDetector.cpp + src/TimeProjectionChamberSensitiveDetector.cpp + src/GenericTrackerSensitiveDetector.cpp + src/TrackerCombineSensitiveDetector.cpp + LINK DetSimInterface + DetInterface + ${DD4hep_COMPONENT_LIBRARIES} +) +target_include_directories(DetSimSDLib PUBLIC + $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>/include + $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) +install(TARGETS DetSimSDLib + EXPORT CEPCSWTargets + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT bin + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT shlib + COMPONENT dev) + gaudi_add_module(DetSimSD SOURCES src/CalorimeterSensDetTool.cpp - src/DDG4SensitiveDetector.cpp - src/CaloSensitiveDetector.cpp - src/DriftChamberSensDetTool.cpp - src/DriftChamberSensitiveDetector.cpp - src/TimeProjectionChamberSensDetTool.cpp - src/TimeProjectionChamberSensitiveDetector.cpp - src/GenericTrackerSensDetTool.cpp - src/GenericTrackerSensitiveDetector.cpp - src/TrackerCombineSensitiveDetector.cpp LINK DetSimInterface + DetSimSDLib DetInterface ${DD4hep_COMPONENT_LIBRARIES} Gaudi::GaudiKernel diff --git a/Simulation/DetSimSD/include/DetSimSD/DDG4SensitiveDetector.h b/Simulation/DetSimSD/include/DetSimSD/DDG4SensitiveDetector.h index 687771e344b9f12ae553e56888b1222288fefbaf..c4a29eb3983d0e1f240059eb1c767b8fc0773c15 100644 --- a/Simulation/DetSimSD/include/DetSimSD/DDG4SensitiveDetector.h +++ b/Simulation/DetSimSD/include/DetSimSD/DDG4SensitiveDetector.h @@ -15,7 +15,7 @@ #include "DD4hep/Detector.h" -#include "DDG4/Geant4Hits.h" +#include "DetSimSD/Geant4Hits.h" #include "G4Step.hh" #include "G4HCofThisEvent.hh" diff --git a/Simulation/DetSimSD/include/DetSimSD/Geant4Hits.h b/Simulation/DetSimSD/include/DetSimSD/Geant4Hits.h new file mode 100644 index 0000000000000000000000000000000000000000..60fc18d4792876eac5b8ebe6c558c74447a4f6c3 --- /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/DDG4SensitiveDetector.cpp b/Simulation/DetSimSD/src/DDG4SensitiveDetector.cpp index a1698ee3cd3cbe43b6ae856112a2edb2f9f20ed0..3d16e02be94bc3ecfe30e161e81b80bb683892f7 100644 --- a/Simulation/DetSimSD/src/DDG4SensitiveDetector.cpp +++ b/Simulation/DetSimSD/src/DDG4SensitiveDetector.cpp @@ -1,7 +1,6 @@ #include "DetSimSD/DDG4SensitiveDetector.h" #include "DDG4/Geant4Converter.h" -#include "DDG4/Geant4Hits.h" #include "DD4hep/Segmentations.h" #include "DD4hep/Printout.h" diff --git a/Simulation/DetSimSD/src/Geant4Hits.cpp b/Simulation/DetSimSD/src/Geant4Hits.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0358bf019d8222bc8a92ab02bc4fb2022e60b1cf --- /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); +} + diff --git a/build-k4.sh b/build-k4.sh index a483d8993ef03f377d1fdd74fecf76d4f1d6adbe..6e2e3bdb10466804663b8b424ae7487062b344ec 100755 --- a/build-k4.sh +++ b/build-k4.sh @@ -109,7 +109,7 @@ function run-make() { ############################################################################## # The current default platform -k4_platform=x86_64-linux-gcc9-opt +k4_platform=x86_64-linux-gcc11-opt k4_version=master bldtool=${CEPCSW_BLDTOOL} # make, ninja # set in env var