diff --git a/include/DDML/CaloCloudsTwoAngleModel.h b/include/DDML/CaloCloudsTwoAngleModel.h index de492d1..e6278ed 100644 --- a/include/DDML/CaloCloudsTwoAngleModel.h +++ b/include/DDML/CaloCloudsTwoAngleModel.h @@ -1,9 +1,10 @@ #ifndef CaloCloudsTwoAngleModel_H #define CaloCloudsTwoAngleModel_H -#include "DDML/FastMLShower.h" #include "DDML/ModelInterface.h" +#include + namespace ddml { /** Class for running a point cloud based ML model for fast shower simulation. @@ -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) { @@ -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& output); + void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs, + TensorDimVecs& tensDims, std::vector& output); /** create a vector of spacepoints per layer interpreting the model output */ - virtual void convertOutput(G4FastTrack const&, G4ThreeVector const&, const std::vector& output, - std::vector& spacepoints); + void convertOutput(G4FastTrack const&, G4ThreeVector const&, const std::vector& output, + std::vector& spacepoints); private: /// model properties for plugin @@ -61,5 +62,7 @@ class CaloCloudsTwoAngleModel : public ModelInterface { TensorDimVecs m_tensDims = {{1, 1}, {1, 1}, {1, 1}, {1, 3}}; }; +static_assert(ModelInterface, "CaloCloudsTwoAngleModel must fulfill the ModelInterface concept"); + } // namespace ddml #endif diff --git a/include/DDML/EndcapGeometry.h b/include/DDML/EndcapGeometry.h index 6666140..1e087f3 100644 --- a/include/DDML/EndcapGeometry.h +++ b/include/DDML/EndcapGeometry.h @@ -14,7 +14,7 @@ namespace ddml { * @date Mar 2023 */ -class EndcapGeometry : public GeometryInterface { +class EndcapGeometry { public: EndcapGeometry() { initialize(); }; @@ -36,7 +36,7 @@ class EndcapGeometry : public GeometryInterface { /** convert the local spacepoints to global spacepoints */ - virtual void localToGlobal(G4FastTrack const& aFastTrack, std::vector& spacepoints) const; + void localToGlobal(G4FastTrack const& aFastTrack, std::vector& spacepoints) const; private: std::vector m_caloLayerDistances = {}; @@ -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 should model the GeometryInterface concept"); + } // namespace ddml #endif diff --git a/include/DDML/EndcapTriggerTwoAngleBIBAE.h b/include/DDML/EndcapTriggerTwoAngleBIBAE.h index 8dfc3ec..3f462ec 100644 --- a/include/DDML/EndcapTriggerTwoAngleBIBAE.h +++ b/include/DDML/EndcapTriggerTwoAngleBIBAE.h @@ -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 should model the TriggerInterface concept"); + } // namespace ddml #endif diff --git a/include/DDML/FastMLShower.h b/include/DDML/FastMLShower.h index 8741c15..33f00cb 100644 --- a/include/DDML/FastMLShower.h +++ b/include/DDML/FastMLShower.h @@ -9,6 +9,11 @@ #include #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 @@ -21,6 +26,7 @@ #include #include +#include #include using ClockT = std::chrono::high_resolution_clock; @@ -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); } @@ -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); } @@ -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); } @@ -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 prepareInputTime; @@ -204,8 +198,9 @@ 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 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. @@ -213,26 +208,20 @@ struct AlwaysTrueTrigger { * @author F.Gaede, DESY * @date Mar 2023 */ -template +template 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 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()) {} void declareProperties(dd4hep::sim::Geant4Action* plugin) { model.declareProperties(plugin); diff --git a/include/DDML/GeometryInterface.h b/include/DDML/GeometryInterface.h index 9f5e853..503ed0b 100644 --- a/include/DDML/GeometryInterface.h +++ b/include/DDML/GeometryInterface.h @@ -1,6 +1,7 @@ #ifndef GeometryInterface_H #define GeometryInterface_H +#include #include #include @@ -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. @@ -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& spacepoints) const = 0; -}; +template +concept GeometryInterface = + requires(const T t, const G4FastTrack& aFastTrack, std::vector& 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; + + /** convert the local spacepoints to global spacepoints inside sensitive + * volumes + */ + { t.localToGlobal(aFastTrack, spacepoints) } -> std::same_as; + }; } // namespace ddml diff --git a/include/DDML/InferenceInterface.h b/include/DDML/InferenceInterface.h index 6bcac06..3176644 100644 --- a/include/DDML/InferenceInterface.h +++ b/include/DDML/InferenceInterface.h @@ -4,22 +4,21 @@ #include "DDML/DDML.h" #include +#include 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 +concept InferenceInterface = requires(T t, const InputVecs& inputs, const TensorDimVecs& tensDims, std::vector& output) { /// run the inference model - based on input vector and resized outputvector - virtual void runInference(const InputVecs& inputs, const TensorDimVecs& tensDims, std::vector& output) = 0; + t.runInference(inputs, tensDims, output); }; } // namespace ddml diff --git a/include/DDML/L2LFlowsModel.h b/include/DDML/L2LFlowsModel.h index 86b7453..ca810ff 100644 --- a/include/DDML/L2LFlowsModel.h +++ b/include/DDML/L2LFlowsModel.h @@ -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) { @@ -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& output); + void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs, + TensorDimVecs& tensDims, std::vector& output); /** create a vector of spacepoints per layer interpreting the model output */ - virtual void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, - const std::vector& output, std::vector& spacepoints); + void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, const std::vector& output, + std::vector& spacepoints); private: /// model properties for plugin @@ -63,5 +63,6 @@ class L2LFlowsModel : public ModelInterface { TensorDimVecs m_tensDims = {{1, 4}}; }; +static_assert(ModelInterface, "L2LFlowsModel must fulfill the ModelInterface concept"); } // namespace ddml #endif diff --git a/include/DDML/LoadHdf5.h b/include/DDML/LoadHdf5.h index e104b7a..02c7313 100644 --- a/include/DDML/LoadHdf5.h +++ b/include/DDML/LoadHdf5.h @@ -22,7 +22,7 @@ namespace ddml { * @date Sep. 2024 */ -class LoadHdf5 : public InferenceInterface { +class LoadHdf5 { public: LoadHdf5() = default; @@ -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& output) override; + void runInference(const InputVecs&, const TensorDimVecs&, std::vector& output); private: H5::H5File m_file = 0; @@ -61,5 +61,7 @@ class LoadHdf5 : public InferenceInterface { uint32_t m_count{0}; }; +static_assert(InferenceInterface, "LoadHdf5 must satisfy InferenceInterface concept"); + } // namespace ddml #endif diff --git a/include/DDML/ModelInterface.h b/include/DDML/ModelInterface.h index 6048544..2896252 100644 --- a/include/DDML/ModelInterface.h +++ b/include/DDML/ModelInterface.h @@ -2,6 +2,7 @@ #define ModelInterface_H #include +#include #include "DDML/DDML.h" @@ -16,26 +17,22 @@ namespace ddml { * @author F.Gaede, DESY * @date Mar 2023 */ - -class ModelInterface { -public: - virtual ~ModelInterface() = default; - +template +concept ModelInterface = requires(T t, G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, + InputVecs& inputs, TensorDimVecs& tensDims, std::vector& output, + const std::vector& constOutput, std::vector& spacepoints) { /** prepare the input vector and resize the output vector for this model * based on the current FastTrack (e.g. extract kinetic energy and incident * angles.) and the direction in the local coordinate system (see * @GeometryInterface) */ - - virtual void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs, - TensorDimVecs& tensDims, std::vector& output) = 0; + { t.prepareInput(aFastTrack, localDir, inputs, tensDims, output) } -> std::same_as; /** interpreting the model output and create a vector of spacepoints per layer * in local coordinates - with the origin at the entry point into the * calorimeter. */ - virtual void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, - const std::vector& output, std::vector& spacepoints) = 0; + { t.convertOutput(aFastTrack, localDir, constOutput, spacepoints) } -> std::same_as; }; } // namespace ddml diff --git a/include/DDML/ONNXInference.h b/include/DDML/ONNXInference.h index 1907b53..5061eef 100644 --- a/include/DDML/ONNXInference.h +++ b/include/DDML/ONNXInference.h @@ -17,7 +17,7 @@ namespace ddml { * @date Mar 2023 */ -class ONNXInference : public InferenceInterface { +class ONNXInference { public: ONNXInference(); @@ -30,7 +30,7 @@ class ONNXInference : public InferenceInterface { void initialize(); /// run the inference model - based on input vector and resized outputvector - virtual void runInference(const InputVecs& inputs, const TensorDimVecs& tensDims, std::vector& output); + void runInference(const InputVecs& inputs, const TensorDimVecs& tensDims, std::vector& output); private: /// Pointer to the ONNX enviroment @@ -57,5 +57,7 @@ class ONNXInference : public InferenceInterface { int m_intraOpNumThreads = 0; }; +static_assert(InferenceInterface, "ONNXInference must satisfy InferenceInterface concept"); + } // namespace ddml #endif diff --git a/include/DDML/OctogonalBarrelTrigger.h b/include/DDML/OctogonalBarrelTrigger.h index 5e66849..2c814a9 100644 --- a/include/DDML/OctogonalBarrelTrigger.h +++ b/include/DDML/OctogonalBarrelTrigger.h @@ -16,16 +16,19 @@ namespace ddml { * */ -class OctogonalBarrelTrigger : public TriggerInterface { +class OctogonalBarrelTrigger { public: OctogonalBarrelTrigger() = default; - virtual ~OctogonalBarrelTrigger() = default; + ~OctogonalBarrelTrigger() = 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, + "OctogonalBarrelTrigger should model the TriggerInterface concept"); + } // namespace ddml #endif diff --git a/include/DDML/Par04ExampleVAE.h b/include/DDML/Par04ExampleVAE.h index fa14eae..17c662e 100644 --- a/include/DDML/Par04ExampleVAE.h +++ b/include/DDML/Par04ExampleVAE.h @@ -14,11 +14,11 @@ namespace ddml { * @date May 2023 */ -class Par04ExampleVAE : public ModelInterface { +class Par04ExampleVAE { public: Par04ExampleVAE() = default; - virtual ~Par04ExampleVAE() = default; + ~Par04ExampleVAE() = default; /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin) { @@ -37,13 +37,13 @@ class Par04ExampleVAE : public ModelInterface { * angles.) */ - virtual void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs, - TensorDimVecs& tensDims, std::vector& output); + void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs, + TensorDimVecs& tensDims, std::vector& output); /** create a vector of spacepoints per layer interpreting the model output */ - virtual void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, - const std::vector& output, std::vector& spacepoints); + void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, const std::vector& output, + std::vector& spacepoints); private: float m_initialEnergy = 0.; @@ -58,5 +58,7 @@ class Par04ExampleVAE : public ModelInterface { TensorDimVecs m_tensDims = {{1, m_latentSize + 4}}; }; +static_assert(ModelInterface, "Par04ExampleVAE must fulfill the ModelInterface concept"); + } // namespace ddml #endif diff --git a/include/DDML/PionCloudsModel.h b/include/DDML/PionCloudsModel.h index 3830e59..936c42d 100644 --- a/include/DDML/PionCloudsModel.h +++ b/include/DDML/PionCloudsModel.h @@ -19,11 +19,11 @@ namespace ddml { * @date Feb. 2025 */ -class PionCloudsModel : public ModelInterface { +class PionCloudsModel { public: PionCloudsModel() = default; - virtual ~PionCloudsModel() = default; + ~PionCloudsModel() = default; /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin) { @@ -34,13 +34,13 @@ class PionCloudsModel : 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& output); + void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs, + TensorDimVecs& tensDims, std::vector& output); /** create a vector of spacepoints per layer interpreting the model output */ - virtual void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, - const std::vector& output, std::vector& spacepoints); + void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, const std::vector& output, + std::vector& spacepoints); private: /// model properties for plugin @@ -53,5 +53,7 @@ class PionCloudsModel : public ModelInterface { TensorDimVecs m_tensDims = {{1, 1}, {1, 1}, {1, 1}, {1, 3}}; }; +static_assert(ModelInterface); + } // namespace ddml #endif diff --git a/include/DDML/PolyhedraBarrelGeometry.h b/include/DDML/PolyhedraBarrelGeometry.h index 520d2ec..85c0aae 100644 --- a/include/DDML/PolyhedraBarrelGeometry.h +++ b/include/DDML/PolyhedraBarrelGeometry.h @@ -19,11 +19,11 @@ namespace ddml { * */ -class PolyhedraBarrelGeometry : public GeometryInterface { +class PolyhedraBarrelGeometry { public: PolyhedraBarrelGeometry() { initialize(); }; - virtual ~PolyhedraBarrelGeometry() = default; + ~PolyhedraBarrelGeometry() = default; /// initialize the plugin - after properties have been set void initialize(); @@ -45,7 +45,7 @@ class PolyhedraBarrelGeometry : public GeometryInterface { /** convert the local spacepoints to global spacepoints */ - virtual void localToGlobal(G4FastTrack const& aFastTrack, std::vector& spacepoints) const; + void localToGlobal(G4FastTrack const& aFastTrack, std::vector& spacepoints) const; protected: /// local helper @@ -63,5 +63,9 @@ class PolyhedraBarrelGeometry : public GeometryInterface { int m_nHadSymmetry = m_nSymmetry; }; +// Static assert to ensure PolyhedraBarrelGeometry models the GeometryInterface concept +static_assert(GeometryInterface, + "PolyhedraBarrelGeomtry should model the GeometryInterface concept"); + } // namespace ddml #endif diff --git a/include/DDML/RegularGridBIBAEModel.h b/include/DDML/RegularGridBIBAEModel.h index 10585a7..49248c0 100644 --- a/include/DDML/RegularGridBIBAEModel.h +++ b/include/DDML/RegularGridBIBAEModel.h @@ -19,11 +19,11 @@ namespace ddml { * @date Apr. 2023 */ -class RegularGridBIBAEModel : public ModelInterface { +class RegularGridBIBAEModel { public: RegularGridBIBAEModel() = default; - virtual ~RegularGridBIBAEModel() = default; + ~RegularGridBIBAEModel() = default; /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin) { @@ -41,13 +41,13 @@ class RegularGridBIBAEModel : 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& output); + void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs, + TensorDimVecs& tensDims, std::vector& output); /** create a vector of spacepoints per layer interpreting the model output */ - virtual void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, - const std::vector& output, std::vector& spacepoints); + void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, const std::vector& output, + std::vector& spacepoints); private: /// model properties for plugin @@ -66,5 +66,7 @@ class RegularGridBIBAEModel : public ModelInterface { TensorDimVecs m_tensDims = {{1, 1}, {1, 1}, {1, 2}}; }; +static_assert(ModelInterface, "RegularGridBIBAEModel must fulfill the ModelInterface concept"); + } // namespace ddml #endif diff --git a/include/DDML/RegularGridGANModel.h b/include/DDML/RegularGridGANModel.h index 0bf7d8e..cb4ed7f 100644 --- a/include/DDML/RegularGridGANModel.h +++ b/include/DDML/RegularGridGANModel.h @@ -14,11 +14,11 @@ namespace ddml { * @date Mar 2023 */ -class RegularGridGANModel : public ModelInterface { +class RegularGridGANModel { public: RegularGridGANModel() = default; - virtual ~RegularGridGANModel() = default; + ~RegularGridGANModel() = default; /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin) { @@ -37,13 +37,13 @@ class RegularGridGANModel : public ModelInterface { * angles.) */ - virtual void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs, - TensorDimVecs& tensDims, std::vector& output); + void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs, + TensorDimVecs& tensDims, std::vector& output); /** create a vector of spacepoints per layer interpreting the model output */ - virtual void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, - const std::vector& output, std::vector& spacepoints); + void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, const std::vector& output, + std::vector& spacepoints); private: /// model properties for plugin @@ -56,5 +56,7 @@ class RegularGridGANModel : public ModelInterface { TensorDimVecs m_tensDims = {{1, 100, 1, 1, 1}, {1, 1, 1, 1, 1}}; }; +static_assert(ModelInterface, "RegularGridGANModel must fulfill the ModelInterface concept"); + } // namespace ddml #endif diff --git a/include/DDML/RegularGridTwoAngleBIBAEModel.h b/include/DDML/RegularGridTwoAngleBIBAEModel.h index 45587c5..513dc24 100644 --- a/include/DDML/RegularGridTwoAngleBIBAEModel.h +++ b/include/DDML/RegularGridTwoAngleBIBAEModel.h @@ -21,11 +21,11 @@ namespace ddml { * @date Aug. 2023 */ -class RegularGridTwoAngleBIBAEModel : public ModelInterface { +class RegularGridTwoAngleBIBAEModel { public: RegularGridTwoAngleBIBAEModel() = default; - virtual ~RegularGridTwoAngleBIBAEModel() = default; + ~RegularGridTwoAngleBIBAEModel() = default; /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin) { @@ -43,13 +43,13 @@ class RegularGridTwoAngleBIBAEModel : 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& output); + void prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs, + TensorDimVecs& tensDims, std::vector& output); /** create a vector of spacepoints per layer interpreting the model output */ - virtual void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, - const std::vector& output, std::vector& spacepoints); + void convertOutput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, const std::vector& output, + std::vector& spacepoints); private: /// model properties for plugin @@ -83,5 +83,7 @@ class RegularGridTwoAngleBIBAEModel : public ModelInterface { TensorDimVecs m_tensDims = {{1, 1}, {1, 1}, {1, 1}, {1, 3}}; }; +static_assert(ModelInterface, "RegularGridTwoAngleBIBAEModel must fulfill the ModelInterface concept"); + } // namespace ddml #endif diff --git a/include/DDML/TorchInference.h b/include/DDML/TorchInference.h index 7f9dcf4..aba6ba8 100644 --- a/include/DDML/TorchInference.h +++ b/include/DDML/TorchInference.h @@ -17,7 +17,7 @@ namespace ddml { * @date Mar 2023 */ -class TorchInference : public InferenceInterface { +class TorchInference { public: TorchInference(); @@ -30,7 +30,7 @@ class TorchInference : public InferenceInterface { void initialize(); /// run the inference model - based on input vector and resized outputvector - virtual void runInference(const InputVecs& inputs, const TensorDimVecs& tensDims, std::vector& output); + void runInference(const InputVecs& inputs, const TensorDimVecs& tensDims, std::vector& output); private: torch::jit::script::Module m_jitModule; @@ -45,5 +45,7 @@ class TorchInference : public InferenceInterface { int m_intraOpNumThreads = 0; }; +static_assert(InferenceInterface, "TorchInference must satisfy InferenceInterface concept"); + } // namespace ddml #endif diff --git a/include/DDML/TriggerInterface.h b/include/DDML/TriggerInterface.h index 801e780..ae0a4ff 100644 --- a/include/DDML/TriggerInterface.h +++ b/include/DDML/TriggerInterface.h @@ -2,22 +2,21 @@ #define TriggerInterface_H #include "DDML/DDML.h" +#include class G4FastTrack; namespace ddml { -/** The basic interface for ML model trigger +/** The basic concept for ML model trigger * * @author P.McKeown, DESY * @date Sep. 2023 */ -class TriggerInterface { -public: - virtual ~TriggerInterface() = default; - - virtual bool check_trigger(const G4FastTrack&) = 0; +template +concept TriggerInterface = requires(const T t, const G4FastTrack& track) { + { t.check_trigger(track) } -> std::convertible_to; }; } // namespace ddml diff --git a/src/EndcapTriggerTwoAngleBIBAE.cc b/src/EndcapTriggerTwoAngleBIBAE.cc index 57f8009..3308c18 100644 --- a/src/EndcapTriggerTwoAngleBIBAE.cc +++ b/src/EndcapTriggerTwoAngleBIBAE.cc @@ -4,7 +4,7 @@ namespace ddml { -bool EndcapTriggerTwoAngleBIBAE::check_trigger(const G4FastTrack& aFastTrack) { +bool EndcapTriggerTwoAngleBIBAE::check_trigger(const G4FastTrack& aFastTrack) const { G4ThreeVector direction = aFastTrack.GetPrimaryTrack()->GetMomentumDirection(); G4double global_theta = direction.theta(); diff --git a/src/MLModelActions.cc b/src/MLModelActions.cc index 64d601d..01ed964 100644 --- a/src/MLModelActions.cc +++ b/src/MLModelActions.cc @@ -33,86 +33,73 @@ namespace ddml { #ifdef DDML_USE_ONNX_INFERENCE /// a concrete model for regular grid GANs applied to the barrel calorimeter /// with ONNX -typedef FastMLShower> +typedef FastMLShower> RegularGridGANPolyhedraBarrelONNXModel; /// a concrete model for regular grid GANs applied to the endcap calorimeter /// with ONNX -typedef FastMLShower< - FastMLModel> +typedef FastMLShower> RegularGridGANEndcapONNXModel; /// Par04 example -typedef FastMLShower< - FastMLModel> +typedef FastMLShower> Par04ExampleVAEPolyhedraBarrelONNXModel; /// a concrete model for regular grid GANs applied to the endcap calorimeter /// with ONNX -typedef FastMLShower< - FastMLModel> +typedef FastMLShower> Par04ExampleVAEEndcapONNXModel; #endif #ifdef DDML_USE_TORCH_INFERENCE /// a concrete model for regular grid GANs applied to the barrel calorimeter /// with Torch -typedef FastMLShower> +typedef FastMLShower> RegularGridGANPolyhedraBarrelTorchModel; /// a concrete model for regular grid GANs applied to the endcap calorimeter /// with Torch -typedef FastMLShower< - FastMLModel> +typedef FastMLShower> RegularGridGANEndcapTorchModel; /// Model for BIBAE regular grid inference in the barrel calorimeter with Torch -typedef FastMLShower> +typedef FastMLShower> RegularGridBIBAEPolyhedraBarrelTorchModel; /// Model for BIBAE regular grid inference in the endcap calorimeter with Torch -typedef FastMLShower< - FastMLModel> +typedef FastMLShower> RegularGridBIBAEEndcapTorchModel; /// Model for two angle BIBAE regular grid inference in the barrel calorimeter /// with Torch -typedef FastMLShower> // add ML trigger +typedef FastMLShower< + FastMLModel> // add ML trigger RegularGridTwoAngleBIBAEModelPolyhedraBarrelTorchModel; /// Model for two angle BIBAE regular grid inference in the endcap calorimter /// with Torch typedef FastMLShower> // add ML trigger RegularGridTwoAngleBIBAEModelEndcapTorchModel; /// CaloClouds Model for the barrel calorimeter with Torch typedef FastMLShower> // add ML trigger CaloCloudsTwoAngleModelPolyhedraBarrelTorchModel; /// CaloClouds Model for the endcap calorimeter with Torch -typedef FastMLShower< - FastMLModel> // add ML trigger +typedef FastMLShower> // add ML trigger CaloCloudsTwoAngleModelEndcapTorchModel; /// L2L Flows Model -typedef FastMLShower< - FastMLModel> // add ML trigger +typedef FastMLShower> // add ML trigger L2LFlowsModelPolyhedraBarrelTorchModel; /// L2L Flows Model -typedef FastMLShower< - FastMLModel> // add ML trigger +typedef FastMLShower> // add ML trigger L2LFlowsModelEndcapTorchModel; #endif @@ -120,23 +107,20 @@ typedef FastMLShower< /// Load from HDF5 file- as an example for the two angle BIBAE regular grid // Barrel typedef FastMLShower> // add ML trigger LoadHDF5RegularGridTwoAngleBIBAEModelPolyhedraBarrel; // Endcap -typedef FastMLShower< - FastMLModel> // add ML trigger +typedef FastMLShower> // add ML trigger LoadHDF5RegularGridTwoAngleBIBAEModelEndcap; /// Load from HDF5 file- as an example for Hadron showers from PionClouds // Barrel -typedef FastMLShower< - FastMLModel> // add ML trigger +typedef FastMLShower> // add ML trigger LoadHDF5PionCloudsPCHadronModelPolyhedraBarrel; // Endcap // ENDCAP IS CURRENTLY NOT IMPLEMENTED!!!! -typedef FastMLShower> // add ML trigger LoadHDF5PionCloudsPCHadronModelEndcap; #endif diff --git a/src/OctogonalBarrelTrigger.cc b/src/OctogonalBarrelTrigger.cc index ef3b147..2a6d873 100644 --- a/src/OctogonalBarrelTrigger.cc +++ b/src/OctogonalBarrelTrigger.cc @@ -4,7 +4,7 @@ namespace ddml { -bool OctogonalBarrelTrigger::check_trigger(const G4FastTrack& aFastTrack) { +bool OctogonalBarrelTrigger::check_trigger(const G4FastTrack& aFastTrack) const { G4ThreeVector direction = aFastTrack.GetPrimaryTrack()->GetMomentumDirection(); G4double global_phi = direction.phi();