Skip to content

Commit

Permalink
segmentations: Add cellsSpanVolumes function and refactor handleCalor…
Browse files Browse the repository at this point in the history
…imeterHit

Co-authored-by: sss <sss@karma>
Co-authored-by: Andre Sailer <[email protected]>
  • Loading branch information
3 people authored Jan 16, 2025
1 parent 3622a79 commit 9b9d43a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 71 deletions.
5 changes: 5 additions & 0 deletions DDCore/include/DD4hep/Segmentations.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ namespace dd4hep {
* \return vector<double> in natural order of dimensions, e.g., dx/dy/dz, or dr/r*dPhi
*/
std::vector<double> cellDimensions(const CellID& cellID) const;
/// Return true if this segmentation can have cells that span multiple
/// volumes. That is, points from multiple distinct volumes may
/// be assigned to the same cell.
/// In that case, a working volumeID() implementation is required.
bool cellsSpanVolumes() const;

/// Access to the base DDSegmentation object. WARNING: Deprecated call!
DDSegmentation::Segmentation* segmentation() const;
Expand Down
8 changes: 8 additions & 0 deletions DDCore/include/DDSegmentation/Segmentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ namespace dd4hep {
\return vector<double> in natural order of dimensions, e.g., dx/dy/dz, or dr/r*dPhi
*/
virtual std::vector<double> cellDimensions(const CellID& cellID) const;
/// Return true if this segmentation can have cells that span multiple
/// volumes. That is, points from multiple distinct volumes may
/// be assigned to the same cell.
/// In that case, a working volumeID() implementation is required.
virtual bool cellsSpanVolumes() const
{
return false;
}

protected:
/// Default constructor used by derived classes passing the encoding string
Expand Down
8 changes: 8 additions & 0 deletions DDCore/src/Segmentations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ std::vector<double> Segmentation::cellDimensions(const CellID& cell) const {
return access()->segmentation->cellDimensions(cell);
}

/// Return true if this segmentation can have cells that span multiple
/// volumes. That is, points from multiple distinct volumes may
/// be assigned to the same cell.
bool Segmentation::cellsSpanVolumes() const
{
return access()->segmentation->cellsSpanVolumes();
}

/// Access to the base DDSegmentation object. WARNING: Deprecated call!
DDSegmentation::Segmentation* Segmentation::segmentation() const {
return access()->segmentation;
Expand Down
126 changes: 55 additions & 71 deletions DDG4/plugins/Geant4SDActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,58 @@ namespace dd4hep {

namespace {
struct Geant4VoidSensitive {};

/// Common code to handle the creation of a calorimeter hit.
template <class HANDLER>
void handleCalorimeterHit (VolumeID cell,
const HitContribution& contrib,
Geant4HitCollection& coll,
const HANDLER& h,
const Geant4Sensitive& sd,
const Segmentation& segmentation)
{
typedef Geant4Calorimeter::Hit Hit;
Hit* hit = coll.findByKey<Hit>(cell);
if ( !hit ) {
DDSegmentation::Vector3D pos = segmentation.position(cell);
Position global;

// Convert the position relative to the local readout volume
// to a global position.
if (!segmentation.cellsSpanVolumes()) {
global = h.localToGlobal(pos);
}
else {
// The segmentation can gang together multiple volumes.
// In this case, we can't use the transformation we get from
// the step --- the volume that actually contains the hit
// may not be the same volume that the segmentation uses
// for the local coordinate system. We need to get the
// actual volID used from the segmentation and then look
// it up the volume manager to get the proper transformation.
VolumeID volID = segmentation.volumeID(cell);
VolumeManager vman = VolumeManager::getVolumeManager(sd.detectorDescription());
VolumeManagerContext* vc = vman.lookupContext(volID);
// explicit unit conversion; h.localToGlobal does it internally already
global = vc->localToWorld(Position(pos)) / dd4hep::mm;
}
hit = new Hit(global);
hit->cellID = cell;
coll.add(cell, hit);
Geant4TouchableHandler handler(h.touchable());
sd.printM2("%s> CREATE hit with deposit:%e MeV Pos:%8.2f %8.2f %8.2f %s [%s]",
sd.c_name(),contrib.deposit,pos.X,pos.Y,pos.Z,handler.path().c_str(),
coll.GetName().c_str());
if ( 0 == hit->cellID ) { // for debugging only!
hit->cellID = cell;
sd.except("+++ Invalid CELL ID for hit!");
}
}
hit->truth.emplace_back(contrib);
hit->energyDeposit += contrib.deposit;
}
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Geant4SensitiveAction<Geant4VoidSensitive>
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Expand Down Expand Up @@ -250,25 +301,7 @@ namespace dd4hep {
return true;
}

//Hit* hit = coll->find<Hit>(CellIDCompare<Hit>(cell));
Hit* hit = coll->findByKey<Hit>(cell);
if ( !hit ) {
Geant4TouchableHandler handler(step);
DDSegmentation::Vector3D pos = m_segmentation.position(cell);
Position global = h.localToGlobal(pos);
hit = new Hit(global);
hit->cellID = cell;
coll->add(cell, hit);
printM2("%s> CREATE hit with deposit:%e MeV Pos:%8.2f %8.2f %8.2f %s [%s]",
c_name(),contrib.deposit,pos.X,pos.Y,pos.Z,handler.path().c_str(),
coll->GetName().c_str());
if ( 0 == hit->cellID ) { // for debugging only!
hit->cellID = cellID(step);
except("+++ Invalid CELL ID for hit!");
}
}
hit->truth.emplace_back(contrib);
hit->energyDeposit += contrib.deposit;
handleCalorimeterHit(cell, contrib, *coll, h, *this, m_segmentation);
mark(h.track);
return true;
}
Expand All @@ -294,24 +327,7 @@ namespace dd4hep {
std::cout << out.str();
return true;
}
Hit* hit = coll->findByKey<Hit>(cell);
if ( !hit ) {
Geant4TouchableHandler handler(h.touchable());
DDSegmentation::Vector3D pos = m_segmentation.position(cell);
Position global = h.localToGlobal(pos);
hit = new Hit(global);
hit->cellID = cell;
coll->add(cell, hit);
printM2("%s> CREATE hit with deposit:%e MeV Pos:%8.2f %8.2f %8.2f %s [%s]",
c_name(),contrib.deposit,pos.X,pos.Y,pos.Z,handler.path().c_str(),
coll->GetName().c_str());
if ( 0 == hit->cellID ) { // for debugging only!
hit->cellID = cellID(h.touchable(), h.avgPositionG4());
except("+++ Invalid CELL ID for hit!");
}
}
hit->truth.emplace_back(contrib);
hit->energyDeposit += contrib.deposit;
handleCalorimeterHit(cell, contrib, *coll, h, *this, m_segmentation);
mark(h.track);
return true;
}
Expand Down Expand Up @@ -462,23 +478,7 @@ namespace dd4hep {
std::cout << out.str();
return true;
}
Hit* hit = coll->findByKey<Hit>(cell);
if ( !hit ) {
Geant4TouchableHandler handler(step);
DDSegmentation::Vector3D pos = m_segmentation.position(cell);
Position global = h.localToGlobal(pos);
hit = new Hit(global);
hit->cellID = cell;
coll->add(cell, hit);
printM2("CREATE hit with deposit:%e MeV Pos:%8.2f %8.2f %8.2f %s",
contrib.deposit,pos.X,pos.Y,pos.Z,handler.path().c_str());
if ( 0 == hit->cellID ) { // for debugging only!
hit->cellID = cellID(step);
except("+++ Invalid CELL ID for hit!");
}
}
hit->truth.emplace_back(contrib);
hit->energyDeposit += contrib.deposit;
handleCalorimeterHit(cell, contrib, *coll, h, *this, m_segmentation);
mark(h.track);
return true;
}
Expand All @@ -505,23 +505,7 @@ namespace dd4hep {
std::cout << out.str();
return true;
}
Hit* hit = coll->findByKey<Hit>(cell);
if ( !hit ) {
Geant4TouchableHandler handler(h.touchable());
DDSegmentation::Vector3D pos = m_segmentation.position(cell);
Position global = h.localToGlobal(pos);
hit = new Hit(global);
hit->cellID = cell;
coll->add(cell, hit);
printM2("CREATE hit with deposit:%e MeV Pos:%8.2f %8.2f %8.2f %s",
contrib.deposit,pos.X,pos.Y,pos.Z,handler.path().c_str());
if ( 0 == hit->cellID ) { // for debugging only!
hit->cellID = cellID(h.touchable(), h.avgPositionG4());
except("+++ Invalid CELL ID for hit!");
}
}
hit->truth.emplace_back(contrib);
hit->energyDeposit += contrib.deposit;
handleCalorimeterHit(cell, contrib, *coll, h, *this, m_segmentation);
mark(h.track);
return true;
}
Expand Down

0 comments on commit 9b9d43a

Please sign in to comment.