src/core/module/Module.hpp

Base for the module implementation. More…

Namespaces

Name
allpix
Helper class to hold support layers for a detector model.

Classes

Name
class allpix::Module
Base class for all modules.
class allpix::SequentialModule
A Module that always ensure to execute events in the order of event numbers. It implements buffering out of the box so interested modules can directly use it.

Detailed Description

Base for the module implementation.

Copyright: Copyright (c) 2017-2024 CERN and the Allpix Squared authors. This software is distributed under the terms of the MIT License, copied verbatim in the file “LICENSE.md”. In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an Intergovernmental Organization or submit itself to any jurisdiction. SPDX-License-Identifier: MIT

Source code


#ifndef ALLPIX_MODULE_H
#define ALLPIX_MODULE_H

#include <atomic>
#include <condition_variable>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>

#include <TDirectory.h>

#include "ModuleIdentifier.hpp"
#include "core/config/ConfigManager.hpp"
#include "core/config/Configuration.hpp"
#include "core/geometry/Detector.hpp"
#include "core/messenger/delegates.h"
#include "core/module/exceptions.h"
#include "core/utils/prng.h"

namespace allpix {
    class Messenger;
    class Event;
    class Module {
        friend class Event;
        friend class ModuleManager;
        friend class Messenger;
        friend class LocalMessenger;

    public:
        explicit Module(Configuration& config);
        explicit Module(Configuration& config, std::shared_ptr<Detector> detector);
        virtual ~Module();


        Module(const Module&) = delete;
        Module& operator=(const Module&) = delete;


        Module(Module&&) noexcept = delete;
        Module& operator=(Module&&) noexcept = delete;

        std::shared_ptr<Detector> getDetector() const { return detector_; }

        std::string getUniqueName() const;

        std::string createOutputFile(const std::string& pathname,
                                     const std::string& extension = "",
                                     bool global = false,
                                     bool delete_file = false);

        TDirectory* getROOTDirectory() const;

        ConfigManager* getConfigManager() const;

        bool multithreadingEnabled() const { return multithreading_; }

        virtual void initializeThread() {}

        virtual void initialize() {}

        virtual void run(Event* event) { (void)event; }

        virtual void finalizeThread() {}

        virtual void finalize() {}

    protected:
        void allow_multithreading() { set_multithreading(true); }

        Configuration& get_configuration() { return config_; }
        Configuration& config_;

    private:
        void set_identifier(ModuleIdentifier identifier) { identifier_ = std::move(identifier); }
        ModuleIdentifier get_identifier() const { return identifier_; }
        ModuleIdentifier identifier_;

        void set_ROOT_directory(TDirectory* directory);
        TDirectory* directory_{nullptr};

        void set_config_manager(ConfigManager* config);
        ConfigManager* conf_manager_{nullptr};

        void add_delegate(Messenger* messenger, BaseDelegate* delegate);

        bool check_delegates(Messenger* messenger, Event* event);

        virtual void skip_event(uint64_t) {}

        std::vector<std::pair<Messenger*, BaseDelegate*>> delegates_;

        std::shared_ptr<Detector> detector_;

        void set_multithreading(bool multithreading) { multithreading_ = multithreading; }
        bool multithreading_{false};

        virtual bool require_sequence() const { return false; }
    };

    class SequentialModule : public Module {
        friend class Event;
        friend class ModuleManager;
        friend class Messenger;

    public:
        explicit SequentialModule(Configuration& config) : Module(config) {}
        explicit SequentialModule(Configuration& config, std::shared_ptr<Detector> detector)
            : Module(config, std::move(detector)) {}

    protected:
        void waive_sequence_requirement(bool waive = true);

    private:
        bool require_sequence() const override { return sequence_required_; }
        bool sequence_required_{true};
    };

} // namespace allpix

#endif /* ALLPIX_MODULE_H */

Updated on 2024-12-13 at 08:31:37 +0000