|
| 1 | +// Copyright 2019-2026 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +#ifndef O2_FASTSIM_MODEL_H_ |
| 13 | +#define O2_FASTSIM_MODEL_H_ |
| 14 | + |
| 15 | +/// Base class for fast simulation models. |
| 16 | +/// |
| 17 | +/// A fast simulation model replaces the detailed transport through a region of |
| 18 | +/// the geometry by a function from the particle that enters it to the particles |
| 19 | +/// that leave it. `DoIt()` is Geant4's own entry point |
| 20 | +/// (`G4VFastSimulationModel::DoIt`); the implementation here wraps the logic |
| 21 | +/// common to every model and delegates the physics to `sample()`. A model that |
| 22 | +/// needs a different shape can still override `DoIt()`. |
| 23 | + |
| 24 | +#include "G4VFastSimulationModel.hh" |
| 25 | + |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +class G4FastStep; |
| 29 | +class G4FastTrack; |
| 30 | +class G4ParticleDefinition; |
| 31 | + |
| 32 | +namespace o2::fastsim |
| 33 | +{ |
| 34 | + |
| 35 | +/// The particle entering the envelope, plus the geometric context a model needs. |
| 36 | +/// Units are the O2/VMC ones: cm, GeV, ns. |
| 37 | +struct FastSimInput { |
| 38 | + int pdg = 0; |
| 39 | + double position[3] = {}; ///< global, on the envelope surface |
| 40 | + double direction[3] = {}; ///< unit vector |
| 41 | + double kineticEnergy = 0.; ///< GeV |
| 42 | + double mass = 0.; ///< GeV |
| 43 | + double time = 0.; ///< ns |
| 44 | + double exitDistance = 0.; ///< cm from `position` to the ENVELOPE surface along `direction` |
| 45 | +}; |
| 46 | + |
| 47 | +/// One particle leaving the envelope. |
| 48 | +struct FastSimOutput { |
| 49 | + int pdg = 0; |
| 50 | + double position[3] = {}; ///< global; put it outside the envelope surface |
| 51 | + double momentum[3] = {}; ///< GeV/c |
| 52 | + double time = 0.; ///< ns |
| 53 | +}; |
| 54 | + |
| 55 | +/// A secondary created exactly on the envelope surface is located by the |
| 56 | +/// navigator in whichever daughter owns that point, which costs two extra |
| 57 | +/// zero-length steps before it gets out. Models should emit just beyond it. |
| 58 | +constexpr double kSurfaceEpsilonCm = 1e-5; |
| 59 | + |
| 60 | +/// Base class for fast simulation models. |
| 61 | +/// |
| 62 | +/// The model is attached to regions (see G4FastSimulation.h) purely so that |
| 63 | +/// Geant4 consults it; what it encloses is the ENVELOPE VOLUME named below, |
| 64 | +/// which is normally the mother volume of a whole module. The two are |
| 65 | +/// deliberately separate, because a Geant4 region in O2 can only ever be "every |
| 66 | +/// volume of a given material" -- the VMC special cuts make every logical volume |
| 67 | +/// a root of its own material's region, and Geant4 stops propagating a region at |
| 68 | +/// any such daughter. So `G4FastTrack::GetEnvelopeSolid()` would hand back one |
| 69 | +/// absorber piece rather than the absorber, and this class does not use it. |
| 70 | +/// |
| 71 | +/// Containment and the exit distance are taken from the track's own touchable, |
| 72 | +/// which already carries the full ancestry and the transform of every level. |
| 73 | +class FastSimModel : public G4VFastSimulationModel |
| 74 | +{ |
| 75 | + public: |
| 76 | + FastSimModel(const G4String& name, const G4String& envelopeVolume, double minEnergyGeV); |
| 77 | + |
| 78 | + G4bool IsApplicable(const G4ParticleDefinition& particle) override; |
| 79 | + G4bool ModelTrigger(const G4FastTrack& fastTrack) override; |
| 80 | + |
| 81 | + /// Wraps the common logic of a fast simulation action and delegates the |
| 82 | + /// physics to `sample()`: it measures the distance to the envelope surface, |
| 83 | + /// kills the incident particle, stacks what `sample()` returned and books the |
| 84 | + /// energy difference as a deposit. Override it for a model that does not fit |
| 85 | + /// that shape. |
| 86 | + void DoIt(const G4FastTrack& fastTrack, G4FastStep& fastStep) override; |
| 87 | + |
| 88 | + protected: |
| 89 | + /// Given the particle that entered, return everything that leaves. This is |
| 90 | + /// the function a trained model implements. |
| 91 | + virtual std::vector<FastSimOutput> sample(const FastSimInput& input) const = 0; |
| 92 | + |
| 93 | + private: |
| 94 | + /// The track's ancestry level at which the envelope volume sits, or -1 when |
| 95 | + /// the track is not inside it at all. |
| 96 | + int envelopeDepth(const G4Track* track) const; |
| 97 | + |
| 98 | + G4String mEnvelope; ///< logical volume the model encloses |
| 99 | + double mMinEnergy = 0.; ///< internal Geant4 units; below this the detailed transport runs |
| 100 | + mutable bool mWarned = false; |
| 101 | +}; |
| 102 | + |
| 103 | +} // namespace o2::fastsim |
| 104 | + |
| 105 | +#endif // O2_FASTSIM_MODEL_H_ |
0 commit comments