Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/implementation/geant4-interface/low-level.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ Utility interfaces

.. doxygenclass:: celeritas::ScopedGeantLogger
.. doxygenclass:: celeritas::ScopedGeantExceptionHandler

.. _g4_views:

Views
^^^^^

.. doxygenclass:: celeritas::GeantParticleView
.. doxygenclass:: celeritas::GeantStepPointView
.. doxygenclass:: celeritas::GeantTrackView
.. doxygenclass:: celeritas::GeantStepView
49 changes: 22 additions & 27 deletions src/accel/LocalTransporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "corecel/cont/Span.hh"
#include "corecel/io/BuildOutput.hh"
#include "corecel/io/Logger.hh"
#include "corecel/math/QuantityIO.hh"
#include "corecel/sys/Device.hh"
#include "corecel/sys/ScopedProfiling.hh"
#include "corecel/sys/ScopedSignalHandler.hh"
Expand All @@ -35,6 +36,7 @@
#include "celeritas/Quantities.hh"
#include "celeritas/Types.hh"
#include "celeritas/ext/GeantSd.hh"
#include "celeritas/ext/GeantTrackView.hh"
#include "celeritas/ext/GeantUnits.hh"
#include "celeritas/ext/detail/HitProcessor.hh"
#include "celeritas/global/ActionSequence.hh"
Expand Down Expand Up @@ -245,56 +247,49 @@ void LocalTransporter::Push(G4Track& g4track)

ScopedProfiling profile_this{"push"};

if (Real3 pos = convert_from_geant(g4track.GetPosition(), 1);
!is_inside(bbox_, pos))
GeantTrackView gtv{g4track};

if (!is_inside(bbox_, gtv.pos()))
{
// Primary may have been created by a particle generator outside the
// geometry
double energy
= convert_from_geant(g4track.GetKineticEnergy(), CLHEP::MeV);
CELER_LOG_LOCAL(error)
<< "Discarding track outside world bounds: " << energy
<< " MeV from " << g4track.GetDefinition()->GetParticleName()
<< " at " << pos << " along "
<< convert_from_geant(g4track.GetMomentumDirection(), 1);
<< "Discarding track outside world bounds: " << gtv.energy()
<< " from " << gtv.particle().name() << " at " << gtv.pos()
<< " along " << gtv.dir();

buffer_accum_.lost_energy += energy;
buffer_accum_.lost_energy += gtv.energy().value();
++buffer_accum_.lost_primaries;
return;
}

Primary track;

PDGNumber const pdg{g4track.GetDefinition()->GetPDGEncoding()};
track.particle_id = particles_->find(pdg);
Primary offloaded;

// Generate Celeritas-specific PrimaryID
if (hit_processor_)
{
track.primary_id
offloaded.primary_id
= hit_processor_->track_processor().register_primary(g4track);
}

track.energy = units::MevEnergy(
convert_from_geant(g4track.GetKineticEnergy(), CLHEP::MeV));
offloaded.energy = gtv.energy();
offloaded.particle_id = particles_->find(gtv.particle().pdg());
offloaded.position = gtv.pos();
offloaded.direction = gtv.dir();
offloaded.time = gtv.time();
offloaded.weight = gtv.weight();

CELER_VALIDATE(track.particle_id,
<< "cannot offload '"
<< g4track.GetDefinition()->GetParticleName()
CELER_VALIDATE(offloaded.particle_id,
<< "cannot offload '" << gtv.particle().name()
<< "' particles");

track.position = convert_from_geant(g4track.GetPosition(), clhep_length);
track.direction = convert_from_geant(g4track.GetMomentumDirection(), 1);
track.time = convert_from_geant(g4track.GetGlobalTime(), clhep_time);
track.weight = g4track.GetWeight();

/*!
* \todo Eliminate event ID from primary.
*/
track.event_id = EventId{0};
offloaded.event_id = EventId{0};

buffer_.push_back(track);
buffer_accum_.energy += track.energy.value();
buffer_.push_back(offloaded);
buffer_accum_.energy += offloaded.energy.value();
if (buffer_.size() >= auto_flush_)
{
this->Flush();
Expand Down
2 changes: 2 additions & 0 deletions src/celeritas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ if(CELERITAS_USE_Geant4)
ext/GeantSd.cc
ext/GeantSdOutput.cc
ext/GeantSetup.cc
ext/GeantStepPointView.cc
ext/GeantStepView.cc
ext/detail/EmStandardPhysics.cc
ext/detail/GeantMicroXsCalculator.cc
ext/detail/GeantModelImporter.cc
Expand Down
12 changes: 7 additions & 5 deletions src/celeritas/ext/GeantParticleView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
//---------------------------------------------------------------------------//
#pragma once

#include <CLHEP/Units/SystemOfUnits.h>
#include <G4ParticleDefinition.hh>
#include <G4Version.hh>

#include "corecel/math/Quantity.hh"
#include "geocel/g4/Convert.hh"
#include "celeritas/UnitTypes.hh"
#include "celeritas/phys/PDGNumber.hh"

#include "GeantUnits.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -72,11 +76,9 @@ auto GeantParticleView::decay_constant() const -> real_type
return 0;
}

// CLHEP time unit system
using Time = Quantity<units::ClhepTraits::Time, double>;

// Decay constant is 1/lifetime
return 1 / native_value_from(Time{pd_.GetPDGLifeTime()});
// Decay constant is 1/lifetime (lifetime is in CLHEP time units)
real_type lifetime = convert_from_geant(pd_.GetPDGLifeTime(), clhep_time);
return 1 / lifetime;
}

//---------------------------------------------------------------------------//
Expand Down
86 changes: 86 additions & 0 deletions src/celeritas/ext/GeantStepPointView.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/ext/GeantStepPointView.cc
//---------------------------------------------------------------------------//
#include "GeantStepPointView.hh"

#include <G4LogicalVolume.hh>

namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Update attributes from logical volume.
*/
void GeantStepPointView::update_from_volume(G4LogicalVolume const& lv)
{
sp_.SetMaterial(lv.GetMaterial());
sp_.SetMaterialCutsCouple(lv.GetMaterialCutsCouple());
sp_.SetSensitiveDetector(lv.GetSensitiveDetector());
}

//---------------------------------------------------------------------------//
/*!
* Update attributes from the touchable's logical volume if possible.
*
* If the step point has an associated touchable, and that touchable is inside
* the geometry, it updates. Otherwise, it clears the corresponding attributes.
*/
void GeantStepPointView::update_from_volume()
{
G4LogicalVolume const* lv = nullptr;
if (auto* touch = sp_.GetTouchable())
{
// The physical volume could be null if post-step is outside
if (auto* pv = touch->GetVolume())
{
lv = pv->GetLogicalVolume();
}
}
if (lv)
{
this->update_from_volume(*lv);
}
else
{
sp_.SetMaterial(nullptr);
sp_.SetMaterialCutsCouple(nullptr);
sp_.SetSensitiveDetector(nullptr);
}
}

//---------------------------------------------------------------------------//
/*!
* Update mass and charge from particle definition.
*/
void GeantStepPointView::update_from_particle(GeantParticleView const& particle)
{
sp_.SetMass(particle.mass().value() * CLHEP::MeV);
sp_.SetCharge(particle.charge().value() * CLHEP::eplus);
}

//---------------------------------------------------------------------------//
/*!
* Clear unsupported attributes to invalid sentinel values.
*
* This sets attributes that Celeritas does not currently track to sentinel
* values to indicate they are unavailable to sensitive detectors.
*/
void GeantStepPointView::clear_unsupported()
{
// Time since track was created
sp_.SetLocalTime(std::numeric_limits<double>::infinity());
// Time in rest frame since track was created
sp_.SetProperTime(std::numeric_limits<double>::infinity());
// Speed (TODO: use ParticleView)
sp_.SetVelocity(std::numeric_limits<double>::infinity());
// Safety distance
sp_.SetSafety(std::numeric_limits<double>::infinity());
// Polarization (default to zero)
sp_.SetPolarization(G4ThreeVector());
}

//---------------------------------------------------------------------------//
} // namespace celeritas
Loading
Loading