Skip to content
Open
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
17 changes: 10 additions & 7 deletions include/DDML/CaloCloudsTwoAngleModel.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef CaloCloudsTwoAngleModel_H
#define CaloCloudsTwoAngleModel_H

#include "DDML/FastMLShower.h"
#include "DDML/ModelInterface.h"

#include <DDG4/Geant4Action.h>

namespace ddml {

/** Class for running a point cloud based ML model for fast shower simulation.
Expand All @@ -18,11 +19,11 @@ namespace ddml {
* @date May. 2024
*/

class CaloCloudsTwoAngleModel : public ModelInterface {
class CaloCloudsTwoAngleModel {
public:
CaloCloudsTwoAngleModel() = default;

virtual ~CaloCloudsTwoAngleModel() = default;
~CaloCloudsTwoAngleModel() = default;

/// declare the proerties needed for the plugin
void declareProperties(dd4hep::sim::Geant4Action* plugin) {
Expand All @@ -33,13 +34,13 @@ class CaloCloudsTwoAngleModel : public ModelInterface {
* based on the current FastTrack (e.g. extract kinetic energy and incident
* angles.)
*/
virtual void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs,
TensorDimVecs& tensDims, std::vector<float>& output);
void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs,
TensorDimVecs& tensDims, std::vector<float>& output);

/** create a vector of spacepoints per layer interpreting the model output
*/
virtual void convertOutput(G4FastTrack const&, G4ThreeVector const&, const std::vector<float>& output,
std::vector<SpacePointVec>& spacepoints);
void convertOutput(G4FastTrack const&, G4ThreeVector const&, const std::vector<float>& output,
std::vector<SpacePointVec>& spacepoints);

private:
/// model properties for plugin
Expand All @@ -61,5 +62,7 @@ class CaloCloudsTwoAngleModel : public ModelInterface {
TensorDimVecs m_tensDims = {{1, 1}, {1, 1}, {1, 1}, {1, 3}};
};

static_assert(ModelInterface<CaloCloudsTwoAngleModel>, "CaloCloudsTwoAngleModel must fulfill the ModelInterface concept");

} // namespace ddml
#endif
7 changes: 5 additions & 2 deletions include/DDML/EndcapGeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ddml {
* @date Mar 2023
*/

class EndcapGeometry : public GeometryInterface {
class EndcapGeometry {
public:
EndcapGeometry() { initialize(); };

Expand All @@ -36,7 +36,7 @@ class EndcapGeometry : public GeometryInterface {

/** convert the local spacepoints to global spacepoints
*/
virtual void localToGlobal(G4FastTrack const& aFastTrack, std::vector<SpacePointVec>& spacepoints) const;
void localToGlobal(G4FastTrack const& aFastTrack, std::vector<SpacePointVec>& spacepoints) const;

private:
std::vector<float> m_caloLayerDistances = {};
Expand All @@ -46,5 +46,8 @@ class EndcapGeometry : public GeometryInterface {
bool m_correctForAngles = false;
};

// Static assert to ensure EndcapGeometry models the GeometryInterface concept
static_assert(GeometryInterface<EndcapGeometry>, "EndcapGeometry should model the GeometryInterface concept");

} // namespace ddml
#endif
11 changes: 7 additions & 4 deletions include/DDML/EndcapTriggerTwoAngleBIBAE.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ namespace ddml {
*
*/

class EndcapTriggerTwoAngleBIBAE : public TriggerInterface {
class EndcapTriggerTwoAngleBIBAE {
public:
EndcapTriggerTwoAngleBIBAE() = default;

virtual ~EndcapTriggerTwoAngleBIBAE() = default;
~EndcapTriggerTwoAngleBIBAE() = default;

// check trigger

virtual bool check_trigger(const G4FastTrack& aFastTrack);
bool check_trigger(const G4FastTrack& aFastTrack) const;
};

// Ensure this class models the TriggerInterface concept
static_assert(TriggerInterface<EndcapTriggerTwoAngleBIBAE>,
"EndcapTriggerTwoAngleBIBAE should model the TriggerInterface concept");

} // namespace ddml
#endif
39 changes: 14 additions & 25 deletions include/DDML/FastMLShower.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#include <G4Track.hh>

#include "DDML/DDML.h"
#include "DDML/Geant4FastHitMakerGlobal.h"
#include "DDML/GeometryInterface.h"
#include "DDML/InferenceInterface.h"
#include "DDML/ModelInterface.h"
#include "DDML/TriggerInterface.h"

#ifndef DDML_INSTRUMENT_MODEL_SHOWER
#define DDML_INSTRUMENT_MODEL_SHOWER 0
Expand All @@ -21,6 +26,7 @@

#include <chrono>
#include <functional>
#include <memory>
#include <numeric>

using ClockT = std::chrono::high_resolution_clock;
Expand Down Expand Up @@ -81,24 +87,18 @@ class FastMLShower : public dd4hep::sim::Geant4FastSimShowerModel {

/// Geometry construction callback. Called at "Construct()"
virtual void constructGeo(dd4hep::sim::Geant4DetectorConstructionContext* ctxt) override {
// if( fastsimML.has_constructGeo ) fastsimML.constructGeo( ctxt ) ;
// else
this->Geant4FastSimShowerModel::constructGeo(ctxt);
}

/// Electromagnetic field construction callback. Called at
/// "ConstructSDandField()"
virtual void constructField(dd4hep::sim::Geant4DetectorConstructionContext* ctxt) override {
// if( fastsimML.has_constructField ) fastsimML.constructField( ctxt )
// ; else
this->Geant4FastSimShowerModel::constructField(ctxt);
}

/// Sensitive detector construction callback. Called at
/// "ConstructSDandField()"
virtual void constructSensitives(dd4hep::sim::Geant4DetectorConstructionContext* ctxt) override {
// if( fastsimML.has_constructSensitives ) fastsimML.constructSensitives(
// ctxt ) ; else
this->Geant4FastSimShowerModel::constructSensitives(ctxt);
}

Expand All @@ -108,8 +108,6 @@ class FastMLShower : public dd4hep::sim::Geant4FastSimShowerModel {
* 'ApplicableParticles'
*/
virtual bool check_applicability(const G4ParticleDefinition& particle) override {
// if( fastsimML.has_check_applicability ) return
// fastsimML.check_applicability(particle) ; else
return this->Geant4FastSimShowerModel::check_applicability(particle);
}

Expand All @@ -118,8 +116,6 @@ class FastMLShower : public dd4hep::sim::Geant4FastSimShowerModel {
* 'Etrigger' the kinetic energy is bigger than the value.
*/
virtual bool check_trigger(const G4FastTrack& track) override {
// if( fastsimML.has_check_trigger ) return fastsimML.check_trigger(track )
// ; else
if (this->Geant4FastSimShowerModel::check_trigger(track)) {
return m_fastsimML.trigger.check_trigger(track);
}
Expand All @@ -129,11 +125,9 @@ class FastMLShower : public dd4hep::sim::Geant4FastSimShowerModel {
/// User callback to model the particle/energy shower - details defined in
/// ML_MODEL
virtual void modelShower(const G4FastTrack& track, G4FastStep& step) override {
// remove particle from further processing by G4
step.KillPrimaryTrack();
step.ProposePrimaryTrackPathLength(0.0);
G4double energy = track.GetPrimaryTrack()->GetKineticEnergy();
step.ProposeTotalEnergyDeposited(energy);
// remove particle from further processing by G4
killParticle(step, energy);

#if DDML_INSTRUMENT_MODEL_SHOWER
podio::UserDataCollection<double> prepareInputTime;
Expand Down Expand Up @@ -204,35 +198,30 @@ class FastMLShower : public dd4hep::sim::Geant4FastSimShowerModel {
};

struct AlwaysTrueTrigger {
bool check_trigger(const G4FastTrack&) { return true; }
bool check_trigger(const G4FastTrack&) const { return true; }
};
static_assert(TriggerInterface<AlwaysTrueTrigger>, "AlwaysTrueTrigger should model the TriggerInterface concept");

/** Template class to put together a complete ML model by specifying
* implementations for the Inference, the Model, the Geometry and a HitMaker.
*
* @author F.Gaede, DESY
* @date Mar 2023
*/
template <class Inference, class MLModel, class Geometry, class HitMaker, class Trigger = AlwaysTrueTrigger>
template <InferenceInterface Inference, ModelInterface MLModel, GeometryInterface Geometry,
TriggerInterface Trigger = AlwaysTrueTrigger>
struct FastMLModel {
using InferenceT = Inference;
using MLModelT = MLModel;
using GeometryT = Geometry;
using HitMakerT = HitMaker;

Inference inference = {};
MLModel model = {};
Geometry geometry = {};
HitMaker* hitMaker = {};
std::unique_ptr<Geant4FastHitMakerGlobal> hitMaker = {};
Trigger trigger{};

FastMLModel() : hitMaker(new HitMaker) {}

const bool has_constructGeo = false;
const bool has_constructField = false;
const bool has_constructSensitives = false;
const bool has_check_applicability = false;
const bool has_check_trigger = false;
FastMLModel() : hitMaker(std::make_unique<Geant4FastHitMakerGlobal>()) {}

void declareProperties(dd4hep::sim::Geant4Action* plugin) {
model.declareProperties(plugin);
Expand Down
30 changes: 15 additions & 15 deletions include/DDML/GeometryInterface.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef GeometryInterface_H
#define GeometryInterface_H

#include <concepts>
#include <vector>

#include <G4ThreeVector.hh>
Expand All @@ -11,7 +12,7 @@ class G4FastTrack;

namespace ddml {

/** The basic interface for the detector geometry - converting between global
/** The basic concept for the detector geometry - converting between global
* (envelope) and local coordinates. The convention for the local coordinate
* system is a right-handed coordinate system that has the z-axis pointing into
* the calorimeter, normal to the calorimeter planes.
Expand All @@ -20,20 +21,19 @@ namespace ddml {
* @date Mar 2023
*/

class GeometryInterface {
public:
virtual ~GeometryInterface() = default;

/** compute local direction in coordinate system that has the z-axis pointing
* into the calorimeter, normal to the layers
*/
virtual G4ThreeVector localDirection(G4FastTrack const& aFastTrack) const = 0;

/** convert the local spacepoints to global spacepoints inside sensitive
* volumes
*/
virtual void localToGlobal(G4FastTrack const& aFastTrack, std::vector<SpacePointVec>& spacepoints) const = 0;
};
template <typename T>
concept GeometryInterface =
requires(const T t, const G4FastTrack& aFastTrack, std::vector<SpacePointVec>& spacepoints) {
/** compute local direction in coordinate system that has the z-axis pointing
* into the calorimeter, normal to the layers
*/
{ t.localDirection(aFastTrack) } -> std::same_as<G4ThreeVector>;

/** convert the local spacepoints to global spacepoints inside sensitive
* volumes
*/
{ t.localToGlobal(aFastTrack, spacepoints) } -> std::same_as<void>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ones that are void should be harmonized. Some use same_as<void> others do not specify anything.

};

} // namespace ddml

Expand Down
11 changes: 5 additions & 6 deletions include/DDML/InferenceInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
#include "DDML/DDML.h"

#include <vector>
#include <concepts>

namespace ddml {

/** The basic interface for running inference with one input vector and one
/** The basic concept for running inference with one input vector and one
* output vector.
*
* @author F.Gaede, DESY
* @date Mar 2023
*/

class InferenceInterface {
public:
virtual ~InferenceInterface() = default;

template<typename T>
concept InferenceInterface = requires(T t, const InputVecs& inputs, const TensorDimVecs& tensDims, std::vector<float>& output) {
/// run the inference model - based on input vector and resized outputvector
virtual void runInference(const InputVecs& inputs, const TensorDimVecs& tensDims, std::vector<float>& output) = 0;
t.runInference(inputs, tensDims, output);
};

} // namespace ddml
Expand Down
13 changes: 7 additions & 6 deletions include/DDML/L2LFlowsModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ namespace ddml {
* @author Th. Buss, Uni Hamburg
* @date Aug. 2024
*/
class L2LFlowsModel : public ModelInterface {
class L2LFlowsModel {
public:
L2LFlowsModel() = default;

virtual ~L2LFlowsModel() = default;
~L2LFlowsModel() = default;

/// declare the proerties needed for the plugin
void declareProperties(dd4hep::sim::Geant4Action* plugin) {
Expand All @@ -41,13 +41,13 @@ class L2LFlowsModel : public ModelInterface {
* based on the current FastTrack (e.g. extract kinetic energy and incident
* angles.)
*/
virtual void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs,
TensorDimVecs& tensDims, std::vector<float>& output);
void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs,
TensorDimVecs& tensDims, std::vector<float>& output);

/** create a vector of spacepoints per layer interpreting the model output
*/
virtual void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir,
const std::vector<float>& output, std::vector<SpacePointVec>& spacepoints);
void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, const std::vector<float>& output,
std::vector<SpacePointVec>& spacepoints);

private:
/// model properties for plugin
Expand All @@ -63,5 +63,6 @@ class L2LFlowsModel : public ModelInterface {
TensorDimVecs m_tensDims = {{1, 4}};
};

static_assert(ModelInterface<L2LFlowsModel>, "L2LFlowsModel must fulfill the ModelInterface concept");
} // namespace ddml
#endif
6 changes: 4 additions & 2 deletions include/DDML/LoadHdf5.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace ddml {
* @date Sep. 2024
*/

class LoadHdf5 : public InferenceInterface {
class LoadHdf5 {
public:
LoadHdf5() = default;

Expand All @@ -35,7 +35,7 @@ class LoadHdf5 : public InferenceInterface {
void initialize();

/// run the inference model - based on input vector and resized outputvector
void runInference(const InputVecs&, const TensorDimVecs&, std::vector<float>& output) override;
void runInference(const InputVecs&, const TensorDimVecs&, std::vector<float>& output);

private:
H5::H5File m_file = 0;
Expand All @@ -61,5 +61,7 @@ class LoadHdf5 : public InferenceInterface {
uint32_t m_count{0};
};

static_assert(InferenceInterface<LoadHdf5>, "LoadHdf5 must satisfy InferenceInterface concept");

} // namespace ddml
#endif
Loading
Loading