Commit 4601778f authored by Simon Spannagel's avatar Simon Spannagel
Browse files

DetectorModel: Move Instantiation to Class Factory Method

parent c4922dc9
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -11,8 +11,32 @@
#include "DetectorModel.hpp"
#include "core/module/exceptions.h"

#include "core/geometry/HybridPixelDetectorModel.hpp"
#include "core/geometry/MonolithicPixelDetectorModel.hpp"

using namespace allpix;

std::shared_ptr<DetectorModel> DetectorModel::factory(const std::string& name, const ConfigReader& reader) {
    Configuration config = reader.getHeaderConfiguration();

    if(!config.has("type")) {
        LOG(ERROR) << "Model file " << config.getFilePath() << " does not provide a type parameter";
    }
    auto type = config.get<std::string>("type");

    // Instantiate the correct detector model
    if(type == "hybrid") {
        return std::make_shared<HybridPixelDetectorModel>(name, reader);
    }
    if(type == "monolithic") {
        return std::make_shared<MonolithicPixelDetectorModel>(name, reader);
    }

    LOG(ERROR) << "Model file " << config.getFilePath() << " type parameter is not valid";
    // FIXME: The model can probably be silently ignored if we have more model readers later
    throw InvalidValueError(config, "type", "model type is not supported");
}

DetectorModel::DetectorModel(std::string type, ConfigReader reader) : type_(std::move(type)), reader_(std::move(reader)) {
    using namespace ROOT::Math;
    auto config = reader_.getHeaderConfiguration();
+8 −0
Original line number Diff line number Diff line
@@ -43,6 +43,14 @@ namespace allpix {
     */
    class DetectorModel {
    public:
        /**
         * @brief Factory to dynamically create detector model objects
         * @param name Name of the model
         * @param reader Reader with the configuration for this model
         * @return Detector model instantiated from the configuration
         */
        static std::shared_ptr<DetectorModel> factory(const std::string& name, const ConfigReader&);

        /**
         * @brief Helper class to hold support layers for a detector model
         */
+2 −26
Original line number Diff line number Diff line
@@ -30,9 +30,6 @@
#include "core/utils/unit.h"
#include "tools/ROOT.h"

#include "core/geometry/HybridPixelDetectorModel.hpp"
#include "core/geometry/MonolithicPixelDetectorModel.hpp"

using namespace allpix;
using namespace ROOT::Math;

@@ -425,29 +422,8 @@ void GeometryManager::load_models() {
        }

        // Parse configuration and add model to the config
        addModel(parse_config(name, reader));
    }
}

std::shared_ptr<DetectorModel> GeometryManager::parse_config(const std::string& name, const ConfigReader& reader) {
    Configuration config = reader.getHeaderConfiguration();

    if(!config.has("type")) {
        LOG(ERROR) << "Model file " << config.getFilePath() << " does not provide a type parameter";
        addModel(DetectorModel::factory(name, reader));
    }
    auto type = config.get<std::string>("type");

    // Instantiate the correct detector model
    if(type == "hybrid") {
        return std::make_shared<HybridPixelDetectorModel>(name, reader);
    }
    if(type == "monolithic") {
        return std::make_shared<MonolithicPixelDetectorModel>(name, reader);
    }

    LOG(ERROR) << "Model file " << config.getFilePath() << " type parameter is not valid";
    // FIXME: The model can probably be silently ignored if we have more model readers later
    throw InvalidValueError(config, "type", "model type is not supported");
}

/*
@@ -491,7 +467,7 @@ void GeometryManager::close_geometry() {
                    reader.addConfiguration(std::move(model_config));
                }

                model = parse_config(name, reader);
                model = DetectorModel::factory(name, reader);
            }

            detector->set_model(model);
+0 −8
Original line number Diff line number Diff line
@@ -248,14 +248,6 @@ namespace allpix {
         */
        void load_models();

        /**
         * @brief Parse a configuration object and instantiate the corresponding model
         * @param name Name of the model
         * @param reader Reader with the configuration for this model
         * @return Detector model instantiated from the configuration
         */
        std::shared_ptr<DetectorModel> parse_config(const std::string& name, const ConfigReader&);

        /**
         * @brief Get the orientation of an object
         * @param config Configuration that defines in the object
+5 −5
Original line number Diff line number Diff line
@@ -120,18 +120,18 @@ int main(int argc, char** argv) {
        // Parsing detector model to generate potential for:
        std::ifstream file(model_path);
        allpix::ConfigReader reader(file, model_path);
        auto model = allpix::DetectorModel(model_path, reader);
        auto implant = model.getImplantSize();
        auto model = allpix::DetectorModel::factory(model_path, reader);
        auto implant = model->getImplantSize();

        // Calculate thickness domain
        auto sensor_max_z = model.getSensorCenter().z() + model.getSensorSize().z() / 2.0;
        auto sensor_max_z = model->getSensorCenter().z() + model->getSensorSize().z() / 2.0;
        auto thickness_domain = std::make_pair(-sensor_max_z, sensor_max_z);

        // Calculate field size from induction matrix size and pixel pitch:
        auto pixel_pitch = model.getPixelSize();
        auto pixel_pitch = model->getPixelSize();
        auto fieldsize = ROOT::Math::XYZVector(pixel_pitch.x() * static_cast<double>(matrix.x()),
                                               pixel_pitch.y() * static_cast<double>(matrix.y()),
                                               model.getSensorSize().z());
                                               model->getSensorSize().z());

        // Binning: default to 1 bin per um:
        if(binning.mag2() == 0) {