Module.cpp 3.19 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/** @file
 *  @brief Implementation of the base module class
 *  @copyright Copyright (c) 2017 CERN and the Corryvreckan 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.
 */

#include "Module.hpp"

using namespace corryvreckan;

13
Module::Module(Configuration config, std::shared_ptr<Detector> detector)
Simon Spannagel's avatar
Simon Spannagel committed
14
    : Module(std::move(config), std::vector<std::shared_ptr<Detector>>{detector}) {}
15

16
17
Module::~Module() {}

18
19
Module::Module(Configuration config, std::vector<std::shared_ptr<Detector>> detectors)
    : m_config(std::move(config)), m_detectors(std::move(detectors)) {}
20

Simon Spannagel's avatar
Simon Spannagel committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 * @throws InvalidModuleActionException If this method is called from the constructor
 *
 * This name is guaranteed to be unique for every single instantiation of all modules
 */
std::string Module::getUniqueName() const {
    std::string unique_name = get_identifier().getUniqueName();
    if(unique_name.empty()) {
        throw InvalidModuleActionException("Cannot uniquely identify module in constructor");
    }
    return unique_name;
}

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

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * @throws InvalidModuleActionException If this method is called from the constructor or destructor
 * @warning Cannot be used from the constructor, because the instantiation logic has not finished yet
 * @warning This method should not be accessed from the destructor (the file is then already closed)
 * @note It is not needed to change directory to this file explicitly in the module, this is done automatically.
 */
TDirectory* Module::getROOTDirectory() const {
    // The directory will only be a null pointer if this method is executed from the constructor or destructor
    if(directory_ == nullptr) {
        throw InvalidModuleActionException("Cannot access ROOT directory in constructor or destructor");
    }

    return directory_;
}
void Module::set_ROOT_directory(TDirectory* directory) {
    directory_ = directory;
}

59
60
61
std::shared_ptr<Detector> Module::get_detector(std::string name) {
    auto it = find_if(
        m_detectors.begin(), m_detectors.end(), [&name](std::shared_ptr<Detector> obj) { return obj->name() == name; });
62
63
64
65
66
67
68
    if(it == m_detectors.end()) {
        throw ModuleError("Device with detector ID " + name + " is not registered.");
    }

    return (*it);
}

69
70
std::shared_ptr<Detector> Module::get_reference() {
    return m_reference;
71
72
}

73
74
std::shared_ptr<Detector> Module::get_dut() {
    auto it = find_if(m_detectors.begin(), m_detectors.end(), [](std::shared_ptr<Detector> obj) { return obj->isDUT(); });
75
76
77
78
    if(it == m_detectors.end()) {
        return nullptr;
    }

79
80
81
    return (*it);
}

82
bool Module::has_detector(std::string name) {
83
84
    auto it = find_if(
        m_detectors.begin(), m_detectors.end(), [&name](std::shared_ptr<Detector> obj) { return obj->name() == name; });
85
86
87
88
89
    if(it == m_detectors.end()) {
        return false;
    }
    return true;
}