diff --git a/include/DDML/CaloCloudsTwoAngleModel.h b/include/DDML/CaloCloudsTwoAngleModel.h index da4cb54..4cb5545 100644 --- a/include/DDML/CaloCloudsTwoAngleModel.h +++ b/include/DDML/CaloCloudsTwoAngleModel.h @@ -20,9 +20,9 @@ namespace ddml { class CaloCloudsTwoAngleModel : public ModelInterface { public: - CaloCloudsTwoAngleModel(){}; + CaloCloudsTwoAngleModel() {}; - virtual ~CaloCloudsTwoAngleModel(){}; + virtual ~CaloCloudsTwoAngleModel() {}; /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin) { diff --git a/include/DDML/EndcapGeometry.h b/include/DDML/EndcapGeometry.h index 716e4c3..7c4f99e 100644 --- a/include/DDML/EndcapGeometry.h +++ b/include/DDML/EndcapGeometry.h @@ -23,7 +23,7 @@ class EndcapGeometry : public GeometryInterface { /// initialize the plugin - after properties have been set void initialize(); - virtual ~EndcapGeometry(){}; + virtual ~EndcapGeometry() {}; /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin) { diff --git a/include/DDML/EndcapTriggerTwoAngleBIBAE.h b/include/DDML/EndcapTriggerTwoAngleBIBAE.h index fc741c0..bc45a74 100644 --- a/include/DDML/EndcapTriggerTwoAngleBIBAE.h +++ b/include/DDML/EndcapTriggerTwoAngleBIBAE.h @@ -18,9 +18,9 @@ namespace ddml { class EndcapTriggerTwoAngleBIBAE : public TriggerInterface { public: - EndcapTriggerTwoAngleBIBAE(){}; + EndcapTriggerTwoAngleBIBAE() {}; - virtual ~EndcapTriggerTwoAngleBIBAE(){}; + virtual ~EndcapTriggerTwoAngleBIBAE() {}; // check trigger diff --git a/include/DDML/GeometryInterface.h b/include/DDML/GeometryInterface.h index 7aa423f..e6e672f 100644 --- a/include/DDML/GeometryInterface.h +++ b/include/DDML/GeometryInterface.h @@ -22,7 +22,7 @@ namespace ddml { class GeometryInterface { public: - virtual ~GeometryInterface(){}; + virtual ~GeometryInterface() {}; /** compute local direction in coordinate system that has the z-axis pointing * into the calorimeter, normal to the layers diff --git a/include/DDML/InferenceInterface.h b/include/DDML/InferenceInterface.h index c5b4c07..028b908 100644 --- a/include/DDML/InferenceInterface.h +++ b/include/DDML/InferenceInterface.h @@ -16,7 +16,7 @@ namespace ddml { class InferenceInterface { public: - virtual ~InferenceInterface(){}; + virtual ~InferenceInterface() {}; /// run the inference model - based on input vector and resized outputvector virtual void runInference(const InputVecs& inputs, const TensorDimVecs& tensDims, std::vector& output) = 0; diff --git a/include/DDML/L2LFlowsModel.h b/include/DDML/L2LFlowsModel.h index ff738db..dc53785 100644 --- a/include/DDML/L2LFlowsModel.h +++ b/include/DDML/L2LFlowsModel.h @@ -18,7 +18,7 @@ namespace ddml { */ class L2LFlowsModel : public ModelInterface { public: - L2LFlowsModel(){}; + L2LFlowsModel() {}; virtual ~L2LFlowsModel() = default; diff --git a/include/DDML/LoadHdf5.h b/include/DDML/LoadHdf5.h index 74bdf02..e104b7a 100644 --- a/include/DDML/LoadHdf5.h +++ b/include/DDML/LoadHdf5.h @@ -51,6 +51,9 @@ class LoadHdf5 : public InferenceInterface { // shower library dimensions std::vector m_dimsOut{}; + // dimensions of shower library + int m_rank; + // properties for plugin std::string m_filePath = {}; diff --git a/include/DDML/ModelInterface.h b/include/DDML/ModelInterface.h index 12e1f11..b2c8557 100644 --- a/include/DDML/ModelInterface.h +++ b/include/DDML/ModelInterface.h @@ -19,7 +19,7 @@ namespace ddml { class ModelInterface { public: - virtual ~ModelInterface(){}; + virtual ~ModelInterface() {}; /** prepare the input vector and resize the output vector for this model * based on the current FastTrack (e.g. extract kinetic energy and incident diff --git a/include/DDML/ONNXInference.h b/include/DDML/ONNXInference.h index cb9452d..1d340e3 100644 --- a/include/DDML/ONNXInference.h +++ b/include/DDML/ONNXInference.h @@ -21,7 +21,7 @@ class ONNXInference : public InferenceInterface { public: ONNXInference(); - virtual ~ONNXInference(){}; + virtual ~ONNXInference() {}; /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin); diff --git a/include/DDML/OctogonalBarrelTrigger.h b/include/DDML/OctogonalBarrelTrigger.h index 6d9231c..c4ec4d4 100644 --- a/include/DDML/OctogonalBarrelTrigger.h +++ b/include/DDML/OctogonalBarrelTrigger.h @@ -18,9 +18,9 @@ namespace ddml { class OctogonalBarrelTrigger : public TriggerInterface { public: - OctogonalBarrelTrigger(){}; + OctogonalBarrelTrigger() {}; - virtual ~OctogonalBarrelTrigger(){}; + virtual ~OctogonalBarrelTrigger() {}; // check trigger diff --git a/include/DDML/Par04ExampleVAE.h b/include/DDML/Par04ExampleVAE.h index 24834eb..891ac11 100644 --- a/include/DDML/Par04ExampleVAE.h +++ b/include/DDML/Par04ExampleVAE.h @@ -16,9 +16,9 @@ namespace ddml { class Par04ExampleVAE : public ModelInterface { public: - Par04ExampleVAE(){}; + Par04ExampleVAE() {}; - virtual ~Par04ExampleVAE(){}; + virtual ~Par04ExampleVAE() {}; /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin) { diff --git a/include/DDML/PionCloudsModel.h b/include/DDML/PionCloudsModel.h new file mode 100644 index 0000000..1689549 --- /dev/null +++ b/include/DDML/PionCloudsModel.h @@ -0,0 +1,57 @@ +#ifndef PionClouds_H +#define PionClouds_H + +#include "DDML/FastMLShower.h" +#include "DDML/ModelInterface.h" + +namespace ddml { + +/** Class for running a point cloud based ML model for fast shower simulation. + * Assumes a cartesian (x,y) coordinates defining the calorimeter planes (layers) and z the depth + * of the calorimeter. + * + * Based on BiBAETwoAngleModel. + * + * Implemented here for the PionClouds model intended for hadron shower simulation (ECAL+HCAL). + * + * @author A.Korol, DESY + * @author P. McKeown, CERN + * @date Feb. 2025 + */ + +class PionCloudsModel : public ModelInterface { +public: + PionCloudsModel() {}; + + virtual ~PionCloudsModel() {}; + + /// declare the proerties needed for the plugin + void declareProperties(dd4hep::sim::Geant4Action* plugin) { + plugin->declareProperty("LatentVectorSize", this->m_latentSize); + } + + /** 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.) + */ + virtual 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); + +private: + /// model properties for plugin + int m_numPoints = 2600; // 4148; //2600; //number of points in the shower + size_t m_nDims = 4; // Number of dimensions + int m_latentSize = 3; // number of input features (energy, theta, phi) + int m_maxNumElements = m_numPoints * 4; // number of space points in the output multiplied by 4 (x,y,z,energy) + // number of layers for ILD is 78: int m_nLayer = 78; + + TensorDimVecs m_tensDims = {{1, 1}, {1, 1}, {1, 1}, {1, 3}}; +}; + +} // namespace ddml +#endif diff --git a/include/DDML/PolyhedraBarrelGeometry.h b/include/DDML/PolyhedraBarrelGeometry.h index ee08de2..8ccb791 100644 --- a/include/DDML/PolyhedraBarrelGeometry.h +++ b/include/DDML/PolyhedraBarrelGeometry.h @@ -12,6 +12,11 @@ namespace ddml { * * @author F.Gaede, DESY * @date Mar 2023 + * + * Addiional option included to support Hadronic shower simulation in ECAL + HCAL + * @author P. McKeown, CERN + * @date Feb 2025 + * */ class PolyhedraBarrelGeometry : public GeometryInterface { @@ -20,7 +25,7 @@ class PolyhedraBarrelGeometry : public GeometryInterface { initialize(); }; - virtual ~PolyhedraBarrelGeometry(){}; + virtual ~PolyhedraBarrelGeometry() {}; /// initialize the plugin - after properties have been set void initialize(); @@ -28,7 +33,10 @@ class PolyhedraBarrelGeometry : public GeometryInterface { /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin) { plugin->declareProperty("Detector", this->m_detector); + plugin->declareProperty("isHadShower", this->m_isHadShower); + plugin->declareProperty("HadDetector", this->m_hadDetector); plugin->declareProperty("Symmetry", this->m_nSymmetry); + plugin->declareProperty("HadSymmetry", this->m_nHadSymmetry); plugin->declareProperty("CorrectForAngles", this->m_correctForAngles); } @@ -52,6 +60,9 @@ class PolyhedraBarrelGeometry : public GeometryInterface { std::string m_detector = {"EcalBarrel"}; int m_nSymmetry = 8; bool m_correctForAngles = false; + bool m_isHadShower; //= true; + std::string m_hadDetector = {"HcalBarrel"}; + int m_nHadSymmetry = m_nSymmetry; }; } // namespace ddml diff --git a/include/DDML/RegularGridBIBAEModel.h b/include/DDML/RegularGridBIBAEModel.h index 491a6c1..87a5e54 100644 --- a/include/DDML/RegularGridBIBAEModel.h +++ b/include/DDML/RegularGridBIBAEModel.h @@ -21,9 +21,9 @@ namespace ddml { class RegularGridBIBAEModel : public ModelInterface { public: - RegularGridBIBAEModel(){}; + RegularGridBIBAEModel() {}; - virtual ~RegularGridBIBAEModel(){}; + virtual ~RegularGridBIBAEModel() {}; /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin) { diff --git a/include/DDML/RegularGridGANModel.h b/include/DDML/RegularGridGANModel.h index 8e8cb34..6275850 100644 --- a/include/DDML/RegularGridGANModel.h +++ b/include/DDML/RegularGridGANModel.h @@ -16,9 +16,9 @@ namespace ddml { class RegularGridGANModel : public ModelInterface { public: - RegularGridGANModel(){}; + RegularGridGANModel() {}; - virtual ~RegularGridGANModel(){}; + virtual ~RegularGridGANModel() {}; /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin) { diff --git a/include/DDML/RegularGridTwoAngleBIBAEModel.h b/include/DDML/RegularGridTwoAngleBIBAEModel.h index 76540a3..6348b33 100644 --- a/include/DDML/RegularGridTwoAngleBIBAEModel.h +++ b/include/DDML/RegularGridTwoAngleBIBAEModel.h @@ -23,9 +23,9 @@ namespace ddml { class RegularGridTwoAngleBIBAEModel : public ModelInterface { public: - RegularGridTwoAngleBIBAEModel(){}; + RegularGridTwoAngleBIBAEModel() {}; - virtual ~RegularGridTwoAngleBIBAEModel(){}; + virtual ~RegularGridTwoAngleBIBAEModel() {}; /// declare the proerties needed for the plugin void declareProperties(dd4hep::sim::Geant4Action* plugin) { diff --git a/include/DDML/TriggerInterface.h b/include/DDML/TriggerInterface.h index 195f786..07fd43e 100644 --- a/include/DDML/TriggerInterface.h +++ b/include/DDML/TriggerInterface.h @@ -15,7 +15,7 @@ namespace ddml { class TriggerInterface { public: - virtual ~TriggerInterface(){}; + virtual ~TriggerInterface() {}; virtual bool check_trigger(const G4FastTrack&) = 0; }; diff --git a/scripts/ddsim_steer.py b/scripts/ddsim_steer.py index 94dec80..785d160 100644 --- a/scripts/ddsim_steer.py +++ b/scripts/ddsim_steer.py @@ -394,16 +394,27 @@ def aiDanceTorch(kernel): CaloClouds = True L2LFlows = False old_DD4hep = False ## use for DD4hep versions/commits before ~ Apr 21st 2023 + hadrons = False if ild == True: ml_barrel_name = "EcalBarrel" ml_barrel_symmetry = 8 ml_endcap_name = "EcalEndcap" + + ## For hadron shower fast simulation + ml_had_barrel_name = "HcalBarrel" + ml_had_barrel_symmetry = 8 + ml_had_endcap_name = "HcalEndcap" else: ml_barrel_name = "ECalBarrel" ml_barrel_symmetry = 12 ml_endcap_name = "ECalEndcap" + ## For hadron shower fast simulation is needed + ml_had_barrel_name = "HCalBarrel" + ml_had_barrel_symmetry = 12 + ml_had_endcap_name = "HCalEndcap" + if BIBAE == True and Two_Angle == False: ml_file = "../models/BIBAE_Full_PP_cut.pt" ml_model = "RegularGridBIBAEPolyhedraBarrelTorchModel/BarrelModelTorch" @@ -459,6 +470,7 @@ def aiDanceTorch(kernel): model = DetectorConstruction(kernel, str(ml_model)) ## # Mandatory model parameters + model.isHadShower = False model.RegionName = "EcalBarrelRegion" model.Detector = ml_barrel_name model.Symmetry = ml_barrel_symmetry @@ -515,16 +527,28 @@ def LoadHdf5(kernel): BIBAE = True Two_Angle = True old_DD4hep = False ## use for DD4hep versions/commits before ~ Apr 21st 2023 + hadrons = True if ild == True: ml_barrel_name = "EcalBarrel" ml_barrel_symmetry = 8 ml_endcap_name = "EcalEndcap" + + ## For hadron shower fast simulation + ml_had_barrel_name = "HcalBarrel" + ml_had_barrel_symmetry = 8 + ml_had_endcap_name = "HcalEndcap" + else: ml_barrel_name = "ECalBarrel" ml_barrel_symmetry = 12 ml_endcap_name = "ECalEndcap" + ## For hadron shower fast simulation is needed + ml_had_barrel_name = "HCalBarrel" + ml_had_barrel_symmetry = 12 + ml_had_endcap_name = "HCalEndcap" + if BIBAE == True and Two_Angle == True: ml_file = "../models/photons-E5050A-theta9090A-phi9090-p1.hdf5" ml_model = ( @@ -533,6 +557,11 @@ def LoadHdf5(kernel): ml_model_1 = "LoadHDF5RegularGridTwoAngleBIBAEModelEndcap/EndcapModelTorch" ml_correct_angles = False + if hadrons == True: + ml_model_had = "LoadHDF5PionCloudsPCHadronModelPolyhedraBarrel/BarrelModelTorch" + ml_had_file = "../models/PionClouds_50GeV_sp_scaled.h5" + ml_correct_angles = False + from g4units import GeV, MeV # DO NOT REMOVE OR MOVE!!!!! (EXCLAMATION MARK) from DDG4 import DetectorConstruction, Geant4, PhysicsList @@ -557,6 +586,8 @@ def LoadHdf5(kernel): seq.adopt(sensitives) # ----------------- + """ + ## EM in Barrel model = DetectorConstruction(kernel, str(ml_model)) ## # Mandatory model parameters @@ -578,7 +609,9 @@ def LoadHdf5(kernel): model.enableUI() seq.adopt(model) + """ # ------------------- + ## EM in Endcap model1 = DetectorConstruction(kernel, str(ml_model_1)) ## # Mandatory model parameters @@ -599,12 +632,38 @@ def LoadHdf5(kernel): model1.enableUI() seq.adopt(model1) + + # ------------------- + ## Hadrons in Barrel + modelHad1 = DetectorConstruction(kernel, str(ml_model_had)) + + ## # Mandatory model parameters + modelHad1.isHadShower = True + modelHad1.RegionName = ( + "EcalBarrelRegion" # or "HcalBarrelRegion" ## hadron model triggers in ecal + ) + modelHad1.Detector = ml_barrel_name + modelHad1.HadDetector = ml_had_barrel_name + modelHad1.Symmetry = ml_barrel_symmetry + modelHad1.HadSymmetry = ml_had_barrel_symmetry + modelHad1.Enable = True + modelHad1.CorrectForAngles = ml_correct_angles + # Energy boundaries are optional: Units are GeV + modelHad1.ApplicableParticles = {"pi+"} + modelHad1.Etrigger = {"pi+": 10.0 * GeV} # trigger on lower training threshold + modelHad1.FilePath = ml_had_file + # model.OptimizeFlag = 1 + # model.IntraOpNumThreads = 1 + + modelHad1.enableUI() + seq.adopt(modelHad1) + # ------------------- # Now build the physics list: phys = kernel.physicsList() ph = PhysicsList(kernel, str("Geant4FastPhysics/FastPhysicsList")) - ph.EnabledParticles = ["e+", "e-", "gamma"] + ph.EnabledParticles = ["e+", "e-", "gamma", "pi+"] ph.BeVerbose = True ph.enableUI() phys.adopt(ph) diff --git a/scripts/test_onnx.mac b/scripts/test_onnx.mac index b7a22b4..c1f4695 100644 --- a/scripts/test_onnx.mac +++ b/scripts/test_onnx.mac @@ -3,13 +3,24 @@ # example script to run inference on a GAN w/ ONNX runtime # --- the particle gun -/gps/pos/centre 0 0 0 -/gps/particle gamma -#/gps/particle pi- -/gps/energy 50 GeV + +# At right side face (perpendicular to x=0) +#/gps/pos/centre 18.47 0 0 #0 180.47 150 #### this is ILD calo Face #120 180.48 120 #30 180.48 0 #30 0 0 +#30 0 0 + +#/gps/pos/centre 0 0 0 #0 180.47 150 #### this is ILD calo Face #120 180.48 120 #30 180.48 0 #30 0 0 +#30 0 0 +#/gps/particle gamma # for SimpleCalo -/gps/direction -0.4152 1 0 #(pi/8 + pi/4) #1 0.414213562 0 #(pi/8) #-1 2.414213562 0 #(pi/8 + 1 deg) # 0.25 0.25 0.5 #0 0.5 0.655 # ( 37 deg in theta- most you can get in endcap from IP ) +#/gps/direction 1 0 0 #0 1 0 #-0.4152 1 0 #(pi/8 + pi/4) #1 0.414213562 0 #(pi/8) #-1 2.414213562 0 #(pi/8 + 1 deg) # 0.25 0.25 0.5 #0 0.5 0.655 # ( 37 deg in theta- most you can get in endcap from IP ) + +/gps/particle pi+ +/gps/energy 50 GeV + +/gps/pos/centre -5 0 -15 +/gps/direction -0.037864591009775746 0.9992828792427412 0 # 90 deg imact for 50 GeV p+ + # phi barrel #1 0.414213562 0 #(pi/8) @@ -64,5 +75,5 @@ #/gps/direction 0.1 -0.8 .1 -/run/beamOn 10 #100 +/run/beamOn 2000 #100 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b56fb29..2882c79 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ set(sources Par04ExampleVAE.cc RegularGridBIBAEModel.cc RegularGridTwoAngleBIBAEModel.cc + PionCloudsModel.cc OctogonalBarrelTrigger.cc EndcapTriggerTwoAngleBIBAE.cc CaloCloudsTwoAngleModel.cc @@ -82,6 +83,7 @@ set(headers ${PROJECT_SOURCE_DIR}/include/DDML/Par04ExampleVAE.h ${PROJECT_SOURCE_DIR}/include/DDML/RegularGridBIBAEModel.h ${PROJECT_SOURCE_DIR}/include/DDML/RegularGridTwoAngleBIBAEModel.h + ${PROJECT_SOURCE_DIR}/include/DDML/PionCloudsModel.h ${PROJECT_SOURCE_DIR}/include/DDML/PolyhedraBarrelGeometry.h ${PROJECT_SOURCE_DIR}/include/DDML/ModelInterface.h ${PROJECT_SOURCE_DIR}/include/DDML/InferenceInterface.h diff --git a/src/CaloCloudsTwoAngleModel.cc b/src/CaloCloudsTwoAngleModel.cc index 4e18ae6..a29110f 100644 --- a/src/CaloCloudsTwoAngleModel.cc +++ b/src/CaloCloudsTwoAngleModel.cc @@ -28,8 +28,6 @@ void CaloCloudsTwoAngleModel::prepareInput(G4FastTrack const& aFastTrack, G4Thre dd4hep::printout(dd4hep::DEBUG, "CaloCloudsTwoAngleModel::prepareInput", "DDML::localDir: (%f, %f, %f)", localDir_.x(), localDir_.y(), localDir_.z()); - // std::cout << "CaloClouds::localDir:" << "(" << localDir_.x() << "," << localDir_.y() << "," << localDir_.z() << ")" - // << std::endl; // compute local incident angles double r = sqrt(localDir_.x() * localDir_.x() + localDir_.y() * localDir_.y() + localDir_.z() * localDir_.z()); @@ -37,7 +35,6 @@ void CaloCloudsTwoAngleModel::prepareInput(G4FastTrack const& aFastTrack, G4Thre double phi = atan2(localDir_.y(), localDir_.x()) / M_PI * 180.; dd4hep::printout(dd4hep::DEBUG, "CaloCloudsTwoAngleModel::prepareInput", "DDML::localDir: (%f, %f)", theta, phi); - // std::cout << "CaloClouds::localDir:" << " theta = " << theta << " phi = " << phi << std::endl; // the input for the CaloCLouds is one energy and two angles (local Theta and Phi) inputs.resize(m_latentSize); @@ -51,12 +48,6 @@ void CaloCloudsTwoAngleModel::prepareInput(G4FastTrack const& aFastTrack, G4Thre inputs[1][0] = theta; // 89.*(M_PI/180.) ; //Theta_vec[0]/(90.*(M_PI/180.)); inputs[2][0] = phi; - // if (DEBUGPRINT) { - // std::cout << " Input_energy_tensor : " << inputs[0][0] << std::endl; - // std::cout << " Input_theta_tensor : " << inputs[1][0] << std::endl; - // std::cout << " Input_phi_tensor : " << inputs[2][0] << std::endl; - // } - dd4hep::printout(dd4hep::DEBUG, "CaloCloudsTwoAngleModel::prepareInput", "Input_energy_tensor : %f", inputs[0][0]); dd4hep::printout(dd4hep::DEBUG, "CaloCloudsTwoAngleModel::prepareInput", "Input_theta_tensor : %f", inputs[1][0]); dd4hep::printout(dd4hep::DEBUG, "CaloCloudsTwoAngleModel::prepareInput", "Input_phi_tensor : %f", inputs[2][0]); diff --git a/src/EndcapGeometry.cc b/src/EndcapGeometry.cc index ccf5d3a..470f91d 100644 --- a/src/EndcapGeometry.cc +++ b/src/EndcapGeometry.cc @@ -22,7 +22,7 @@ void EndcapGeometry::initialize() { } } else { - std::cout << " ###### error: detector " << m_detector << " not found !" << std::endl; + dd4hep::printout(dd4hep::ERROR, "EndcapGeometry::initialize", "Detector %s not found!", m_detector.c_str()); } } @@ -41,13 +41,9 @@ G4ThreeVector EndcapGeometry::localDirection(G4FastTrack const& aFastTrack) cons localDir = {-direction.x(), direction.y(), -direction.z()}; } - if (DEBUGPRINT) { - G4double energy = aFastTrack.GetPrimaryTrack()->GetKineticEnergy(); - - std::cout << " EndcapGeometry::localDirection " - << " pos0 = " << position << " - dir = " << direction << " - E = " - << " - localDir = " << localDir << energy << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "EndcapGeometry::localDirection", "pos0 = (%f, %f, %f) - dir = (%f, %f, %f) - E = %f", + position.x(), position.y(), position.z(), direction.x(), direction.y(), direction.z(), + aFastTrack.GetPrimaryTrack()->GetKineticEnergy()); return localDir; } @@ -58,10 +54,8 @@ void EndcapGeometry::localToGlobal(G4FastTrack const& aFastTrack, std::vectorGetPosition(); G4ThreeVector direction = aFastTrack.GetPrimaryTrack()->GetMomentumDirection(); - if (DEBUGPRINT) { - std::cout << " EndcapGeometry::localToGlobal pos0 = " << position << " - dir = " << direction - << " - E = " << energy << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "EndcapGeometry::localToGlobal", "pos0 = (%f, %f, %f) - dir = (%f, %f, %f) - E = %f", + position.x(), position.y(), position.z(), direction.x(), direction.y(), direction.z(), energy); float signZ = (position.z() > 0. ? 1.0 : -1.0); diff --git a/src/Geant4FastHitMakerGlobal.cc b/src/Geant4FastHitMakerGlobal.cc index 9740761..e4cbe94 100644 --- a/src/Geant4FastHitMakerGlobal.cc +++ b/src/Geant4FastHitMakerGlobal.cc @@ -32,6 +32,8 @@ #include "G4VFastSimSensitiveDetector.hh" +#include + Geant4FastHitMakerGlobal::Geant4FastHitMakerGlobal() { m_touchableHandle = new G4TouchableHistory(); m_navigator = new G4Navigator(); @@ -64,25 +66,28 @@ void Geant4FastHitMakerGlobal::make(const G4FastHit& aHit, const G4FastTrack& aT } m_navigator->SetWorldVolume(worldWithSD); // use track global position - m_navigator->LocateGlobalPointAndUpdateTouchable( - // aTrack.GetPrimaryTrack()->GetPosition(), - aHit.GetPosition(), m_touchableHandle(), false); + m_navigator->LocateGlobalPointAndUpdateTouchable(aHit.GetPosition(), m_touchableHandle(), false); m_naviSetup = true; } else { // for further deposits use hit (local) position and local->global // transformation - m_navigator->LocateGlobalPointAndUpdateTouchable( - // aTrack.GetInverseAffineTransformation()->TransformPoint( - // aHit.GetPosition()), - aHit.GetPosition(), m_touchableHandle()); + m_navigator->LocateGlobalPointAndUpdateTouchable(aHit.GetPosition(), m_touchableHandle()); } G4VPhysicalVolume* currentVolume = m_touchableHandle()->GetVolume(); G4VSensitiveDetector* sensitive; if (currentVolume != 0) { + // In full sim sensitive detector + dd4hep::printout(dd4hep::DEBUG, "Geant4FastHitMakerGlobal::make", + "In full sim sensitive: aHit.GetPosition() x: %f, y: %f, z: %f ", aHit.GetPosition().x(), + aHit.GetPosition().y(), aHit.GetPosition().z()); sensitive = currentVolume->GetLogicalVolume()->GetSensitiveDetector(); G4VFastSimSensitiveDetector* fastSimSensitive = dynamic_cast(sensitive); if (fastSimSensitive) { + // In fast sim sensitive detector + dd4hep::printout(dd4hep::DEBUG, "Geant4FastHitMakerGlobal::make", + "In fast sim sensitive: aHit.GetPosition() x: %f, y: %f, z: %f ", aHit.GetPosition().x(), + aHit.GetPosition().y(), aHit.GetPosition().z()); fastSimSensitive->Hit(&aHit, &aTrack, &m_touchableHandle); } else if (sensitive && currentVolume->GetLogicalVolume()->GetFastSimulationManager()) { G4cerr << "ERROR - Geant4FastHitMakerGlobal::make()" << G4endl << " It is required to derive from the " diff --git a/src/LoadHdf5.cc b/src/LoadHdf5.cc index 7bfebe1..1e27139 100644 --- a/src/LoadHdf5.cc +++ b/src/LoadHdf5.cc @@ -19,7 +19,7 @@ void LoadHdf5::initialize() { // inputs and TensorDimVecs unused // Open dataset + dataspace - std::string datasetName = "layers"; + std::string datasetName = "spase_points"; //"events"; //"spase_points"; //"layers"; H5::DataSet dataset = m_file.openDataSet(datasetName); dd4hep::printout(dd4hep::DEBUG, "LoadHdf5::initialize", "Accessed HDF5 dataset"); H5::DataSpace dataspace = dataset.getSpace(); @@ -28,8 +28,8 @@ void LoadHdf5::initialize() { // bool H5::DataSpace::isSimple() const; // Get dimensions - int rank = dataspace.getSimpleExtentNdims(); - std::vector dims_out(rank); + m_rank = dataspace.getSimpleExtentNdims(); + std::vector dims_out(m_rank); dataspace.getSimpleExtentDims(dims_out.data(), nullptr); // Calculate the total number of elements @@ -39,13 +39,19 @@ void LoadHdf5::initialize() { m_dimsOut = dims_out; - assert(rank == 4); // assuming 4 dimensional input + assert(m_rank == 3 || m_rank == 4); // ensure either 3 dimensional PionCloud input or 4 dimensional input + // if m_rank == 4 // index 0: shower number // index 1, 2, 3: x, y, z cell number - dd4hep::printout(dd4hep::DEBUG, "LoadHdf5::initialize", "Rank %i", rank); + // else if m_rank == 3 + // index 0: shower number + // index 1: number of points + // index 2: 4 point dimensions (x, y, z, E) - currently in ILD coordinates + + dd4hep::printout(dd4hep::DEBUG, "LoadHdf5::initialize", "Rank %i", m_rank); - for (int i = 0, N = rank; i < N; ++i) { + for (int i = 0, N = m_rank; i < N; ++i) { dd4hep::printout(dd4hep::DEBUG, "LoadHdf5::initialize", "dimension %i: %lu", i, (unsigned long)(m_dimsOut[i])); } @@ -71,23 +77,29 @@ void LoadHdf5::runInference(const InputVecs&, const TensorDimVecs&, std::vector< m_isInitialized = true; } - // keep track of shower index in file - // LoadHdf5::incrementCounter(); - // If counter exceeds number of showers in file, reset if (m_count > m_totalSize) { m_count = 0; } // select shower from library - std::vector shower(m_library.begin() + m_count * m_dimsOut[1] * m_dimsOut[2] * m_dimsOut[3], - m_library.begin() + (m_count + 1) * m_dimsOut[1] * m_dimsOut[2] * m_dimsOut[3]); - - // enforce length of shower - assert(shower.size() == m_dimsOut[1] * m_dimsOut[2] * m_dimsOut[3]); - - // write output - output = std::move(shower); + if (m_rank == 4) { + std::vector shower(m_library.begin() + m_count * m_dimsOut[1] * m_dimsOut[2] * m_dimsOut[3], + m_library.begin() + (m_count + 1) * m_dimsOut[1] * m_dimsOut[2] * m_dimsOut[3]); + assert(shower.size() == m_dimsOut[1] * m_dimsOut[2] * m_dimsOut[3]); + + // write output + output = std::move(shower); + } else if (m_rank == 3) { + std::vector shower(m_library.begin() + m_count * m_dimsOut[1] * m_dimsOut[2], + m_library.begin() + (m_count + 1) * m_dimsOut[1] * m_dimsOut[2]); + assert(shower.size() == m_dimsOut[1] * m_dimsOut[2]); + + // write output + output = std::move(shower); + } else { + throw std::runtime_error("Shower library does not have the correct dimensions!"); + } ++m_count; } diff --git a/src/MLModelActions.cc b/src/MLModelActions.cc index ab56dd1..c0ea2bf 100644 --- a/src/MLModelActions.cc +++ b/src/MLModelActions.cc @@ -21,6 +21,7 @@ #include "DDML/L2LFlowsModel.h" #include "DDML/OctogonalBarrelTrigger.h" #include "DDML/Par04ExampleVAE.h" +#include "DDML/PionCloudsModel.h" #include "DDML/PolyhedraBarrelGeometry.h" #include "DDML/RegularGridBIBAEModel.h" #include "DDML/RegularGridGANModel.h" @@ -127,6 +128,17 @@ typedef FastMLShower< FastMLModel> // add ML trigger LoadHDF5RegularGridTwoAngleBIBAEModelEndcap; + +/// Load from HDF5 file- as an example for Hadron showers from PionClouds +// Barrel +typedef FastMLShower< + FastMLModel> // add ML trigger + LoadHDF5PionCloudsPCHadronModelPolyhedraBarrel; +// Endcap // ENDCAP IS CURRENTLY NOT IMPLEMENTED!!!! +typedef FastMLShower> // add ML trigger + LoadHDF5PionCloudsPCHadronModelEndcap; #endif } // namespace ddml @@ -156,4 +168,6 @@ DECLARE_GEANT4ACTION_NS(ddml, L2LFlowsModelEndcapTorchModel) #ifdef DDML_USE_LOAD_HDF5 DECLARE_GEANT4ACTION_NS(ddml, LoadHDF5RegularGridTwoAngleBIBAEModelPolyhedraBarrel) DECLARE_GEANT4ACTION_NS(ddml, LoadHDF5RegularGridTwoAngleBIBAEModelEndcap) +DECLARE_GEANT4ACTION_NS(ddml, LoadHDF5PionCloudsPCHadronModelPolyhedraBarrel) +DECLARE_GEANT4ACTION_NS(ddml, LoadHDF5PionCloudsPCHadronModelEndcap) #endif diff --git a/src/ONNXInference.cc b/src/ONNXInference.cc index 09e9c4a..992b4ba 100644 --- a/src/ONNXInference.cc +++ b/src/ONNXInference.cc @@ -56,29 +56,24 @@ void ONNXInference::initialize() { std::vector input_node_names(num_input_nodes); for (std::size_t i = 0; i < num_input_nodes; i++) { #if ORT_API_VERSION < 13 - const auto input_name = AllocatedStringPtr(fSession->GetInputName(i, allocator), allocDeleter).release(); + const auto input_name = AllocatedStringPtr(m_session->GetInputName(i, allocator), allocDeleter).release(); #else const auto input_name = m_session->GetInputNameAllocated(i, allocator).release(); #endif - - if (DEBUGPRINT) { - std::cout << " *** input_name : " << i << " = " << input_name << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "ONNXInference::initialize", "input_name : %i = %c", i, input_name); m_inNames.push_back(input_name); input_node_names[i] = input_name; Ort::TypeInfo type_info = m_session->GetInputTypeInfo(i); auto tensor_info = type_info.GetTensorTypeAndShapeInfo(); input_node_dims = tensor_info.GetShape(); - if (DEBUGPRINT) { - std::cout << " *** input_node_dims : " << i << " = " << input_node_dims.size() << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "ONNXInference::initialize", "input_node_dims : %i = %i", i, + input_node_dims.size()); + for (std::size_t j = 0; j < input_node_dims.size(); j++) { if (input_node_dims[j] < 0) { input_node_dims[j] = 1; } - if (DEBUGPRINT) { - std::cout << " - dim " << j << " : " << input_node_dims[j] << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "ONNXInference::initialize", "dim %i : %i", j, input_node_dims[j]); } } // output nodes @@ -87,28 +82,25 @@ void ONNXInference::initialize() { std::vector output_node_names(num_output_nodes); for (std::size_t i = 0; i < num_output_nodes; i++) { #if ORT_API_VERSION < 12 - const auto output_name = AllocatedStringPtr(fSession->GetOutputName(i, allocator), allocDeleter).release(); + const auto output_name = AllocatedStringPtr(m_session->GetOutputName(i, allocator), allocDeleter).release(); #else const auto output_name = m_session->GetOutputNameAllocated(i, allocator).release(); #endif m_outNames.push_back(output_name); output_node_names[i] = output_name; - if (DEBUGPRINT) { - std::cout << " *** output_name : " << i << " = " << output_name << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "ONNXInference::initialize", "output_name: %i = %c", i, output_name); + Ort::TypeInfo type_info = m_session->GetOutputTypeInfo(i); auto tensor_info = type_info.GetTensorTypeAndShapeInfo(); output_node_dims = tensor_info.GetShape(); - if (DEBUGPRINT) { - std::cout << " *** output_node_dims : " << i << " = " << output_node_dims.size() << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "ONNXInference::initialize", "output_node_dims : %i = %i", i, + output_node_dims.size()); + for (std::size_t j = 0; j < output_node_dims.size(); j++) { if (output_node_dims[j] < 0) { output_node_dims[j] = 1; } - if (DEBUGPRINT) { - std::cout << " - dim " << j << " : " << output_node_dims[j] << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "ONNXInference::initialize", "dim %i : %i", j, output_node_dims[j]); } } } diff --git a/src/Par04ExampleVAE.cc b/src/Par04ExampleVAE.cc index 5a6134c..3adcf6f 100644 --- a/src/Par04ExampleVAE.cc +++ b/src/Par04ExampleVAE.cc @@ -21,10 +21,9 @@ void Par04ExampleVAE::prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector // compute local incident angle double theta = acos(localDir.z()); - if (DEBUGPRINT) { - std::cout << " Par04ExampleVAE::prepareInput pos0 = " << position << " - dir = " << direction - << " - E = " << energy / CLHEP::GeV << " theta = " << theta * 180. / M_PI << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "Par04ExampleVAE::prepareInput", + "pos0 = (%f, %f, %f) - dir = (%f, %f, %f) - E = %f - theta = %f", position.x(), position.y(), + position.z(), direction.x(), direction.y(), direction.z(), energy / CLHEP::GeV, theta * 180. / M_PI); // the input for this model is the latent space and the energy conditioning diff --git a/src/PionCloudsModel.cc b/src/PionCloudsModel.cc new file mode 100644 index 0000000..3f1c050 --- /dev/null +++ b/src/PionCloudsModel.cc @@ -0,0 +1,79 @@ +#include "DDML/PionCloudsModel.h" + +#include // for G4FastTrack +#include + +#define DEBUGPRINT 0 + +namespace ddml { + +void PionCloudsModel::prepareInput(G4FastTrack const& aFastTrack, G4ThreeVector const& localDir, InputVecs& inputs, + TensorDimVecs& tensDims, std::vector& output) { + tensDims = m_tensDims; + + G4double energy = aFastTrack.GetPrimaryTrack()->GetKineticEnergy(); + + G4ThreeVector direction = aFastTrack.GetPrimaryTrack()->GetMomentumDirection(); + G4RotationMatrix rotZ; + rotZ.rotateZ(M_PI / 2.); + G4RotationMatrix rotX; + rotX.rotateX(M_PI / 2.); + // this convention is used for the local coordinates in the dataset (model was trained in this convention) + + G4ThreeVector localDir_ = localDir; + localDir_.setX(-1. * localDir_.x()); // *(-1) to align local to global convention in ddml + localDir_.setY(-1. * localDir_.y()); // *(-1) to align local to global convention in ddml + + dd4hep::printout(dd4hep::DEBUG, "PionCloudsModel::prepareInput", "DDML::localDir: (%f, %f, %f)", localDir_.x(), + localDir_.y(), localDir_.z()); + + // compute local incident angles + double r = sqrt(localDir_.x() * localDir_.x() + localDir_.y() * localDir_.y() + localDir_.z() * localDir_.z()); + double theta = acos(localDir_.z() / r) / M_PI * 180.; + double phi = atan2(localDir_.y(), localDir_.x()) / M_PI * 180.; + + dd4hep::printout(dd4hep::DEBUG, "PionCloudsModel::prepareInput", "DDML::localDir: (%f, %f)", theta, phi); + + // the input for the PionClouds is one energy and two angles (local Theta and Phi) + inputs.resize(m_latentSize); + + inputs[0].resize(1); // Energy + inputs[1].resize(1); // Theta + inputs[2].resize(1); // Phi + + // For now, assume batch size one, and just assign values + inputs[0][0] = energy / CLHEP::GeV; // E_vec[0]/100.; + inputs[1][0] = theta; // 89.*(M_PI/180.) ; //Theta_vec[0]/(90.*(M_PI/180.)); + inputs[2][0] = phi; + + dd4hep::printout(dd4hep::DEBUG, "PionCloudsModel::prepareInput", "Input_energy_tensor : %f", inputs[0][0]); + dd4hep::printout(dd4hep::DEBUG, "PionCloudsModel::prepareInput", "Input_theta_tensor : %f", inputs[1][0]); + dd4hep::printout(dd4hep::DEBUG, "PionCloudsModel::prepareInput", "Input_phi_tensor : %f", inputs[2][0]); + + // ---- resize output vector + + output.assign(m_maxNumElements, 0); +} + +// For array structure: (No. showers, No. points, dimensions(4)) +void PionCloudsModel::convertOutput(G4FastTrack const&, G4ThreeVector const&, const std::vector& output, + std::vector& spacepoints) { + const int nPoints = output.size() / m_nDims; + int layerNum = 0; + // Reshape into intermediate representation + auto reshaped = std::views::iota(0, nPoints) | + std::views::transform([&output](int i) { return std::span{output.data() + i * 4, 4}; }); + + spacepoints.resize(nPoints); + for (const auto& values : reshaped) { + ddml::SpacePoint sp(values[0], // x // *(-1) to align local to global convention in ddml + values[2], // y // *(-1) to align local to global convention in ddml + 0., // z + values[3], // energy + 0. // time + ); + layerNum = int(values[1]); + spacepoints[layerNum].emplace_back(sp); + } +} +} // namespace ddml \ No newline at end of file diff --git a/src/PolyhedraBarrelGeometry.cc b/src/PolyhedraBarrelGeometry.cc index 9b2ef65..6e3ac25 100644 --- a/src/PolyhedraBarrelGeometry.cc +++ b/src/PolyhedraBarrelGeometry.cc @@ -19,9 +19,28 @@ void PolyhedraBarrelGeometry::initialize() { if (cal) { for (auto l : cal->layers) { m_caloLayerDistances.push_back((l.distance + l.inner_thickness) / dd4hep::mm); + dd4hep::printout(dd4hep::INFO, "PolyhedraBarrelGeometry::initialize", "ECAL Layer distances %f", + l.distance + l.inner_thickness); } } else { - std::cout << " ###### error: detector " << m_detector << " not found !" << std::endl; + dd4hep::printout(dd4hep::ERROR, "PolyhedraBarrelGeometry::initialize", "Detector %s not found!", + m_detector.c_str()); + } + + // For hadronic shower simulation + if (m_isHadShower == true) { + auto det_had = theDetector.detector(m_hadDetector); + auto* cal_had = det_had.extension(); + if (cal_had) { + for (auto l_had : cal_had->layers) { + m_caloLayerDistances.push_back((l_had.distance + l_had.inner_thickness) / dd4hep::mm); + dd4hep::printout(dd4hep::INFO, "PolyhedraBarrelGeometry::initialize", "HCAL Layer distances %f", + l_had.distance + l_had.inner_thickness); + } + } else { + dd4hep::printout(dd4hep::ERROR, "PolyhedraBarrelGeometry::initialize", "Detector %s not found!", + m_hadDetector.c_str()); + } } } @@ -70,16 +89,12 @@ G4ThreeVector PolyhedraBarrelGeometry::localDirection(G4FastTrack const& aFastTr // the z-axis pointing into the calo G4ThreeVector localDir(-dirR.z(), dirR.y(), dirR.x()); - if (DEBUGPRINT) { - G4double energy = aFastTrack.GetPrimaryTrack()->GetKineticEnergy(); - - std::cout << " PolyhedraBarrelGeometry::localDirection - symmetry = " << m_nSymmetry << " pos0 = " << position - << " - dir = " << direction << " - E = " - << " - localDir = " << localDir << energy << std::endl; - std::cout << " PolyhedraBarrelGeometry::localDirection - phi = " - << atan2(localDir.y(), localDir.x()) / M_PI * 180. << " theta : " << acos(localDir.z()) / M_PI * 180. - << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "PolyhedraBarrelGeometry::localDirection", + "symmetry = %i - pos0 = (%f, %f, %f) - dir = (%f, %f, %f) - E = %f - localDir = (%f, %f, %f)", + m_nSymmetry, position.x(), position.y(), position.z(), direction.x(), direction.y(), direction.z(), + aFastTrack.GetPrimaryTrack()->GetKineticEnergy(), localDir.x(), localDir.y(), localDir.z()); + dd4hep::printout(dd4hep::DEBUG, "PolyhedraBarrelGeometry::localDirection", "phi = %f - theta = %f", + atan2(localDir.y(), localDir.x()) / M_PI * 180., acos(localDir.z()) / M_PI * 180.); return localDir; } @@ -91,12 +106,10 @@ void PolyhedraBarrelGeometry::localToGlobal(G4FastTrack const& aFastTrack, int phiSec = phiSector(position); - if (DEBUGPRINT) { - G4double energy = aFastTrack.GetPrimaryTrack()->GetKineticEnergy(); - - std::cout << " PolyhedraBarrelGeometry::localToGlobal - symmetry = " << m_nSymmetry << " pos0 = " << position - << " - dir = " << direction << " - E = " << energy << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "PolyhedraBarrelGeometry::localToGlobal", + "symmetry = %i - pos0= (%f, %f, %f) - dir = (%f, %f, %f) - E = %f", m_nSymmetry, position.x(), + position.y(), position.z(), direction.x(), direction.y(), direction.z(), + aFastTrack.GetPrimaryTrack()->GetKineticEnergy()); // --- rotate position and direction to phi sector 0 (calo plane parallel to // y-axis at positive x ) @@ -114,16 +127,18 @@ void PolyhedraBarrelGeometry::localToGlobal(G4FastTrack const& aFastTrack, dirR = {1., 0., 0.}; // position layers w/ impact normal to the plane } - if (DEBUGPRINT) { - std::cout << " PolyhedraBarrelGeometry::localToGlobal - position " << position << " - direction " << direction - << " phiSec: " << phiSec << " posR " << posR << " dirR " << dirR << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "PolyhedraBarrelGeometry::localToGlobal", + "pos0= (%f, %f, %f) - dir = (%f, %f, %f) - phiSec = %i - posR = (%f, %f, %f) - dirR = (%f, %f, %f)", + position.x(), position.y(), position.z(), direction.x(), direction.y(), direction.z(), phiSec, + posR.x(), posR.y(), posR.z(), dirR.x(), dirR.y(), dirR.z()); // find the first layer that will have signals as sometimes particles are // create in the calorimeter ! int firstLayer = 0; int nLayer = m_caloLayerDistances.size(); + dd4hep::printout(dd4hep::INFO, "PolyhedraBarrelGeometry::localToGlobal", "nLayer = %i", nLayer); + for (int l = 0; l < nLayer; ++l) { double r = m_caloLayerDistances[l]; firstLayer = l; @@ -167,6 +182,9 @@ void PolyhedraBarrelGeometry::localToGlobal(G4FastTrack const& aFastTrack, sp.X = global.x(); sp.Y = global.y(); sp.Z = global.z(); + + dd4hep::printout(dd4hep::DEBUG, "PolyhedraBarrelGeometry::localToGlobal", + "global.x() = %f, global.y() = %f, global.z() = %f", global.x(), global.y(), global.z()); } } } diff --git a/src/RegularGridBIBAEModel.cc b/src/RegularGridBIBAEModel.cc index 13afc13..22607f0 100644 --- a/src/RegularGridBIBAEModel.cc +++ b/src/RegularGridBIBAEModel.cc @@ -20,10 +20,9 @@ void RegularGridBIBAEModel::prepareInput(G4FastTrack const& aFastTrack, G4ThreeV // compute local incident angle double theta = acos(localDir.z()); - if (DEBUGPRINT) { - std::cout << " RegularGridBIBAEModel::prepareInput pos0 = " << position << " - dir = " << direction - << " - E = " << energy / CLHEP::GeV << " theta = " << theta << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "RegularGridBIBAEModel::prepareInput", + "pos0 = (%f, %f, %f) - dir = (%f, %f, %f) - E = %f - theta = %f", position.x(), position.y(), + position.z(), direction.x(), direction.y(), direction.z(), energy / CLHEP::GeV, theta * 180. / M_PI); // the input for the BIB-AE is one energy and an angle (plus cond tensor) inputs.resize(m_latentSize); @@ -38,9 +37,8 @@ void RegularGridBIBAEModel::prepareInput(G4FastTrack const& aFastTrack, G4ThreeV inputs[2][0] = (inputs[0][0]) / 100.; inputs[2][1] = (inputs[1][0]) / (90. * (M_PI / 180.)); - if (DEBUGPRINT) { - std::cout << " Input_energy_tensor : " << inputs[0][0] * 100. << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "RegularGridBIBAEModel::prepareInput", "Input_energy_tensor : %f", + inputs[0][0] * 100.); // ---- resize output vector diff --git a/src/RegularGridGANModel.cc b/src/RegularGridGANModel.cc index 5682868..9ce469c 100644 --- a/src/RegularGridGANModel.cc +++ b/src/RegularGridGANModel.cc @@ -22,10 +22,9 @@ void RegularGridGANModel::prepareInput(G4FastTrack const& aFastTrack, G4ThreeVec // conditioning variables, such as incident angles ... // for now assume simple GAN with 90 deg incident - if (DEBUGPRINT) { - std::cout << " RegularGridGANModel::prepareInput pos0 = " << position << " - dir = " << direction - << " - E = " << energy / CLHEP::GeV << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "RegularGridGANModel::prepareInput", + "pos0 = (%f, %f, %f) - dir = (%f, %f, %f) - E = %f", position.x(), position.y(), position.z(), + direction.x(), direction.y(), direction.z(), energy / CLHEP::GeV); // the input for this model is the latent space and the energy conditioning diff --git a/src/RegularGridTwoAngleBIBAEModel.cc b/src/RegularGridTwoAngleBIBAEModel.cc index dcba618..dbe8e02 100644 --- a/src/RegularGridTwoAngleBIBAEModel.cc +++ b/src/RegularGridTwoAngleBIBAEModel.cc @@ -65,19 +65,17 @@ void RegularGridTwoAngleBIBAEModel::prepareInput(G4FastTrack const& aFastTrack, const auto [theta_cond, phi_cond] = getConditionAngles(localDir); - if (DEBUGPRINT) { - // compute local incident angles - double theta = acos(localDir.x()); - double phi = atan2(localDir.y(), localDir.x()); - - std::cout << " RegularGridTwoAngleBIBAEModel::prepareInput pos0 = " << position << " - dir = " << direction - << " - E = " << energy / CLHEP::GeV << " - local dir = " << localDir << " theta = " << theta - << " phi= " << phi << std::endl; - std::cout << " RegularGridTwoAngleBIBAEModel::prepareInput Cond, theta_cond = " << theta_cond << " / " - << theta_cond / M_PI * 180. << " phi_cond = " << phi_cond << " / " << phi_cond / M_PI * 180. - << " / Global phi " << direction.phi() << " / " << direction.phi() / M_PI * 180. << " / Global theta " - << direction.theta() << " / " << direction.theta() / M_PI * 180 << std::endl; - } + // compute local incident angles + double theta = acos(localDir.x()); + double phi = atan2(localDir.y(), localDir.x()); + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::prepareInput", + "pos0 = (%f, %f, %f) - dir = (%f, %f, %f) - E = %f - local dir =(%f, %f, %f) - theta = %f - phi=%f", + position.x(), position.y(), position.z(), direction.x(), direction.y(), direction.z(), + energy / CLHEP::GeV, localDir.x(), localDir.y(), localDir.z(), theta, phi); + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::prepareInput", + "theta_cond = %f / %f - phi_cond = %f / %f - Global phi = %f / %f - Global theta = %f / %f", + theta_cond, theta_cond / M_PI * 180., phi_cond, phi_cond / M_PI * 180., direction.phi(), + direction.phi() / M_PI * 180., direction.theta(), direction.theta() / M_PI * 180); // the input for the BIB-AE is one energy and two angles (plus cond tensor) inputs.resize(m_latentSize); @@ -95,14 +93,15 @@ void RegularGridTwoAngleBIBAEModel::prepareInput(G4FastTrack const& aFastTrack, inputs[3][1] = (inputs[1][0]) / (95. * (M_PI / 180.)); inputs[3][2] = (inputs[2][0]) / (95. * (M_PI / 180.)); - if (DEBUGPRINT) { - std::cout << " Input_energy_tensor : " << inputs[0][0] << std::endl; - std::cout << " Input_theta_tensor : " << inputs[1][0] << std::endl; - std::cout << " Input_phi_tensor : " << inputs[2][0] << std::endl; - std::cout << " Input_Flow_energy : " << inputs[3][0] << std::endl; - std::cout << " Input_Flow_theta : " << inputs[3][1] << std::endl; - std::cout << " Input_Flow_phi : " << inputs[3][2] << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::prepareInput", "Input_energy_tensor : %f", + inputs[0][0]); + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::prepareInput", "Input_theta_tensor : %f", + inputs[1][0]); + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::prepareInput", "Input_phi_tensor : %f", inputs[2][0]); + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::prepareInput", "Input_Flow_energy : %f", + inputs[3][0]); + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::prepareInput", "Input_Flow_theta : %f", inputs[3][1]); + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::prepareInput", "Input_Flow_phi : %f", inputs[3][2]); // ---- resize output vector @@ -232,11 +231,10 @@ std::vector RegularGridTwoAngleBIBAEModel::getIncidentCell(const double& double pos_X = intersect[0]; double pos_Y = intersect[2]; - if (DEBUGPRINT) { - std::cout << "X position of intersection: " << pos_X << std::endl; - - std::cout << "Y position of intersection: " << pos_Y << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::getIncidentCell", "X position of intersection: %f", + pos_X); + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::getIncidentCell", "Y position of intersection: %f", + pos_Y); // equivalent of np.arange(-77, 78, 5.088333) std::vector binX; @@ -258,41 +256,27 @@ std::vector RegularGridTwoAngleBIBAEModel::getIncidentCell(const double& // binX.size() < binX.size()) { - if (DEBUGPRINT) { - std::cout << "RegularGridTwoAngleBIBAEModel::getIncidentCell incident " - "position not found in grid X!" - << std::endl; - } + dd4hep::printout(dd4hep::PrintLevel::WARNING, "RegularGridTwoAngleBIBAEModel::getIncidentCell", + "Incident position not found in grid X!"); } else if (binX[i] <= pos_X && pos_X < binX[i + 1]) { double frac = (pos_X - binX[i]) / 5.088333; if (frac < 0) { - std::cout << "RegularGridTwoAngleBIBAEModel::getIncidentCell incident " - "cell fraction wrong X!" - << std::endl; // has to be positive by construction! + dd4hep::printout(dd4hep::PrintLevel::WARNING, "RegularGridTwoAngleBIBAEModel::getIncidentCell", + "Incident cell fraction wrong X!"); } - // std::cout << "RegularGridTwoAngleBIBAEModel::getIncidentCell frac " << - // frac << std::endl; - gridX = static_cast(i) + frac; - if (DEBUGPRINT) { - std::cout << "RegularGridTwoAngleBIBAEModel::getIncidentCell gridX " << gridX << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::getIncidentCell", "gridX = %f", gridX); i = binX.size(); // break; - - // std::cout << "RegularGridTwoAngleBIBAEModel::getIncidentCell - // break"< RegularGridTwoAngleBIBAEModel::getIncidentCell(const double& // Y for (size_t j = 0; j < binY.size(); ++j) { - if (DEBUGPRINT) { - std::cout << "RegularGridTwoAngleBIBAEModel::getIncidentCell binY[j]: " << binY[j] << " binY[j+1]: " - << " j: " << j << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::getIncidentCell", + "binY[j] = %f - binY[j+1] = %f - j = %i", binY[j], binY[j + 1], j); if (j + 1 > binY.size()) { - if (DEBUGPRINT) { - std::cout << "RegularGridTwoAngleBIBAEModel::getIncidentCell incident " - "position not found in grid Y!" - << std::endl; - } - + dd4hep::printout(dd4hep::PrintLevel::WARNING, "RegularGridTwoAngleBIBAEModel::getIncidentCell", + "Incident position not found in grid Y!"); } else if (binY[j] <= pos_Y && pos_Y < binY[j + 1]) { double frac = (pos_Y - binY[j]) / 5.088333; if (frac < 0) { - std::cout << "RegularGridTwoAngleBIBAEModel::getIncidentCell incident " - "cell fraction wrong Y!" - << std::endl; // has to be positive by construction! + dd4hep::printout(dd4hep::PrintLevel::WARNING, "RegularGridTwoAngleBIBAEModel::getIncidentCell", + "Incident cell fraction wrong Y!"); } gridY = static_cast(j) + frac; - if (DEBUGPRINT) { - std::cout << "RegularGridTwoAngleBIBAEModel::getIncidentCell gridY " << gridY << std::endl; - } + dd4hep::printout(dd4hep::DEBUG, "RegularGridTwoAngleBIBAEModel::getIncidentCell", "gridY: %f", gridY); j = binY.size(); // break; } @@ -338,7 +313,7 @@ std::vector RegularGridTwoAngleBIBAEModel::getIncidentCell(const double& grid_incident_point.push_back(gridX); grid_incident_point.push_back(gridY); - std::cout << "Done incident pos func " << std::endl; + dd4hep::printout(dd4hep::INFO, "RegularGridTwoAngleBIBAEModel::getIncidentCell", "Done incident pos func"); return grid_incident_point; } diff --git a/src/TorchInference.cc b/src/TorchInference.cc index ef0ea94..1871172 100644 --- a/src/TorchInference.cc +++ b/src/TorchInference.cc @@ -45,23 +45,19 @@ void TorchInference::runInference(const InputVecs& inputs, const TensorDimVecs& m_isInitialized = true; } - if (DEBUGPRINT) { - std::cout << " ----- TorchInference::runInference \n" - << " # inputs = " << inputs.size() << " : "; + if (dd4hep::printLevel() <= dd4hep::DEBUG) { + dd4hep::printout(dd4hep::DEBUG, "TorchInference::runInference", " ----- TorchInference::runInference -----"); + dd4hep::printout(dd4hep::DEBUG, "TorchInference::runInference", "# inputs = %f : ", inputs.size()); for (auto iv : inputs) { - std::cout << " " << iv.size() << ", "; + dd4hep::printout(dd4hep::DEBUG, "TorchInference::runInference", "%f, ", iv.size()); } - std::cout << std::endl; - - std::cout << " # dims = " << tensDims.size() << " : "; + dd4hep::printout(dd4hep::DEBUG, "TorchInference::runInference", "# dims = %f :", tensDims.size()); for (auto iv : tensDims) { - std::cout << " " << iv.size() << ", "; + dd4hep::printout(dd4hep::DEBUG, "TorchInference::runInference", "%f, ", iv.size()); } - - std::cout << std::endl; } assert(inputs.size() == tensDims.size()); @@ -72,15 +68,20 @@ void TorchInference::runInference(const InputVecs& inputs, const TensorDimVecs& torch::Tensor inTens = torch::tensor(inputs[i], m_options).view(tensDims[i]); tensors.emplace_back(inTens); - if (DEBUGPRINT) { - std::cout << " inTensor " << i << " : " << inTens << std::endl; + if (dd4hep::printLevel() <= dd4hep::DEBUG) { + torch::IntArrayRef sizes = inTens.sizes(); + for (unsigned iv = 0; iv < sizes.size(); ++iv) { + dd4hep::printout(dd4hep::DEBUG, "TorchInference::runInference", "inTens.size()[%i]: %i", iv, sizes[iv]); + } } } at::Tensor outTensor = m_jitModule.forward(tensors).toTensor(); //.contiguous(); - - if (DEBUGPRINT) { - std::cout << " outTensor : " << outTensor << std::endl; + if (dd4hep::printLevel() <= dd4hep::DEBUG) { + torch::IntArrayRef sizes_out = outTensor.sizes(); + for (unsigned iv = 0; iv < sizes_out.size(); ++iv) { + dd4hep::printout(dd4hep::DEBUG, "TorchInference::runInference", "outTensor.size()[%i]: %i", iv, sizes_out[iv]); + } } // torch.flatten(outTensor);