Commit fe021a89 authored by Simon Spannagel's avatar Simon Spannagel
Browse files

Move to using std::shared_ptr<Detector> instead of raw pointers

parent 76bff776
......@@ -123,14 +123,15 @@ void Analysis::load_detectors() {
std::string name = detector.getName();
// Check if we have a duplicate:
if(std::find_if(detectors.begin(), detectors.end(), [&name](Detector* obj) { return obj->name() == name; }) !=
detectors.end()) {
if(std::find_if(detectors.begin(), detectors.end(), [&name](std::shared_ptr<Detector> obj) {
return obj->name() == name;
}) != detectors.end()) {
throw InvalidValueError(
global_config, "detectors_file", "Detector " + detector.getName() + " defined twice");
}
LOG_PROGRESS(STATUS, "DET_LOAD_LOOP") << "Loading detector " << name;
Detector* det_parm = new Detector(detector);
auto det_parm = std::make_shared<Detector>(detector);
// Check if we already found a reference plane:
if(found_reference && det_parm->role() == DetectorRole::REFERENCE) {
......@@ -138,7 +139,10 @@ void Analysis::load_detectors() {
}
// Switch flag if we found the reference plane:
found_reference |= (det_parm->role() == DetectorRole::REFERENCE);
if(det_parm->role() == DetectorRole::REFERENCE) {
found_reference = true;
m_reference = det_parm;
}
// Add the new detector to the global list:
detectors.push_back(det_parm);
......@@ -153,7 +157,7 @@ void Analysis::load_detectors() {
LOG_PROGRESS(STATUS, "DET_LOAD_LOOP") << "Loaded " << detectors.size() << " detectors";
// Finally, sort the list of detectors by z position (from lowest to highest)
std::sort(detectors.begin(), detectors.end(), [](Detector* det1, Detector* det2) {
std::sort(detectors.begin(), detectors.end(), [](std::shared_ptr<Detector> det1, std::shared_ptr<Detector> det2) {
return det1->displacement().Z() < det2->displacement().Z();
});
}
......@@ -315,6 +319,11 @@ void Analysis::load_modules() {
}
}
}
// Set the reference detector:
for(auto& module : m_modules) {
module->setReference(m_reference);
}
LOG_PROGRESS(STATUS, "MOD_LOAD_LOOP") << "Loaded " << configs.size() << " modules";
}
......@@ -332,10 +341,11 @@ Module* Analysis::create_unique_module(void* library, Configuration config) {
}
// Convert to correct generator function
auto module_generator = reinterpret_cast<Module* (*)(Configuration, std::vector<Detector*>)>(generator); // NOLINT
auto module_generator =
reinterpret_cast<Module* (*)(Configuration, std::vector<std::shared_ptr<Detector>>)>(generator); // NOLINT
// Figure out which detectors should run on this module:
std::vector<Detector*> module_det;
std::vector<std::shared_ptr<Detector>> module_det;
if(!config.has("detectors")) {
module_det = detectors;
} else {
......@@ -393,10 +403,10 @@ std::vector<Module*> Analysis::create_detector_modules(void* library, Configurat
}
// Convert to correct generator function
auto module_generator = reinterpret_cast<Module* (*)(Configuration, Detector*)>(generator); // NOLINT
auto module_generator = reinterpret_cast<Module* (*)(Configuration, std::shared_ptr<Detector>)>(generator); // NOLINT
// Figure out which detectors should run on this module:
std::vector<Detector*> module_det;
std::vector<std::shared_ptr<Detector>> module_det;
if(!config.has("detectors")) {
module_det = detectors;
} else {
......
......@@ -56,13 +56,15 @@ namespace corryvreckan {
// Member variables
Clipboard* m_clipboard;
Configuration global_config;
std::vector<Detector*> detectors;
std::vector<std::shared_ptr<Detector>> detectors;
private:
void load_detectors();
void load_modules();
void add_units();
std::shared_ptr<Detector> m_reference;
// Log file if specified
std::ofstream log_file_;
......
......@@ -10,9 +10,12 @@
using namespace corryvreckan;
Module::Module(Configuration config, Detector* detector) : Module(config, std::vector<Detector*>{detector}) {}
Module::Module(Configuration config, std::shared_ptr<Detector> detector)
: Module(config, std::vector<std::shared_ptr<Detector>>{detector}) {}
Module::Module(Configuration config, std::vector<Detector*> detectors) {
Module::~Module() {}
Module::Module(Configuration config, std::vector<std::shared_ptr<Detector>> detectors) {
m_name = config.getName();
m_config = config;
m_detectors = detectors;
......@@ -25,10 +28,9 @@ Module::Module(Configuration config, std::vector<Detector*> detectors) {
}
}
Module::~Module() {}
Detector* Module::get_detector(std::string name) {
auto it = find_if(m_detectors.begin(), m_detectors.end(), [&name](Detector* obj) { return obj->name() == name; });
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; });
if(it == m_detectors.end()) {
throw ModuleError("Device with detector ID " + name + " is not registered.");
}
......@@ -36,13 +38,12 @@ Detector* Module::get_detector(std::string name) {
return (*it);
}
Detector* Module::get_reference() {
auto it = find_if(m_detectors.begin(), m_detectors.end(), [](Detector* obj) { return obj->isReference(); });
return (*it);
std::shared_ptr<Detector> Module::get_reference() {
return m_reference;
}
Detector* Module::get_dut() {
auto it = find_if(m_detectors.begin(), m_detectors.end(), [](Detector* obj) { return obj->isDUT(); });
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(); });
if(it == m_detectors.end()) {
return nullptr;
}
......@@ -51,7 +52,8 @@ Detector* Module::get_dut() {
}
bool Module::has_detector(std::string name) {
auto it = find_if(m_detectors.begin(), m_detectors.end(), [&name](Detector* obj) { return obj->name() == name; });
auto it = find_if(
m_detectors.begin(), m_detectors.end(), [&name](std::shared_ptr<Detector> obj) { return obj->name() == name; });
if(it == m_detectors.end()) {
return false;
}
......
......@@ -49,6 +49,7 @@ namespace corryvreckan {
* - Module::finalise(): for finalising the module at the end
*/
class Module {
friend class Analysis;
public:
/**
......@@ -61,14 +62,14 @@ namespace corryvreckan {
* @param config Configuration for this module
* @param detector Pointer to the detector this module is bound to
*/
Module(Configuration config, Detector* detector);
Module(Configuration config, std::shared_ptr<Detector> detector);
/**
* @brief Base constructor for unique modules
* @param config Configuration for this module
* @param detectors List of detectors this module should be aware of
*/
Module(Configuration config, std::vector<Detector*> detectors);
Module(Configuration config, std::vector<std::shared_ptr<Detector>> detectors);
/**
* @brief Essential virtual destructor.
......@@ -125,7 +126,7 @@ namespace corryvreckan {
* @brief Get list of all detectors this module acts on
* @return List of all detectors for this module
*/
std::vector<Detector*> get_detectors() { return m_detectors; };
std::vector<std::shared_ptr<Detector>> get_detectors() { return m_detectors; };
/**
* @brief Get the number of detectors this module takes care of
......@@ -139,20 +140,20 @@ namespace corryvreckan {
* @return Pointer to the requested detector
* @throws ModuleError if detector with given name is not found for this module
*/
Detector* get_detector(std::string name);
std::shared_ptr<Detector> get_detector(std::string name);
/**
* @brief Get the reference detector for this setup
* @return Pointer to the reference detector
*/
Detector* get_reference();
std::shared_ptr<Detector> get_reference();
/**
* @brief Get the device under test
* @return Pointer to the DUT detector. A nullptr is returned if no DUT is found.
* FIXME This should allow retrieval of a vector of DUTs
*/
Detector* get_dut();
std::shared_ptr<Detector> get_dut();
/**
* @brief Check if this module should act on a given detector
......@@ -162,11 +163,15 @@ namespace corryvreckan {
bool has_detector(std::string name);
private:
// Configure the reference detector:
void setReference(std::shared_ptr<Detector> reference) { m_reference = reference; };
std::shared_ptr<Detector> m_reference;
// Name of the module
std::string m_name;
// List of detectors to act on
std::vector<Detector*> m_detectors;
std::vector<std::shared_ptr<Detector>> m_detectors;
};
} // namespace corryvreckan
......
......@@ -53,8 +53,8 @@ namespace corryvreckan {
* Internal method for the dynamic loading in the central Analysis class. Forwards the supplied arguments to the
* constructor and returns an instantiation.
*/
Module* corryvreckan_module_generator(Configuration config, std::vector<Detector*> detectors);
Module* corryvreckan_module_generator(Configuration config, std::vector<Detector*> detectors) {
Module* corryvreckan_module_generator(Configuration config, std::vector<std::shared_ptr<Detector>> detectors);
Module* corryvreckan_module_generator(Configuration config, std::vector<std::shared_ptr<Detector>> detectors) {
auto module = new CORRYVRECKAN_MODULE_NAME(std::move(config), std::move(detectors)); // NOLINT
return static_cast<Module*>(module);
}
......@@ -75,8 +75,8 @@ namespace corryvreckan {
* Internal method for the dynamic loading in the central Analysis class. Forwards the supplied arguments to the
* constructor and returns an instantiation
*/
Module* corryvreckan_module_generator(Configuration config, Detector* detector);
Module* corryvreckan_module_generator(Configuration config, Detector* detector) {
Module* corryvreckan_module_generator(Configuration config, std::shared_ptr<Detector> detector);
Module* corryvreckan_module_generator(Configuration config, std::shared_ptr<Detector> detector) {
auto module = new CORRYVRECKAN_MODULE_NAME(std::move(config), std::move(detector)); // NOLINT
return static_cast<Module*>(module);
}
......@@ -97,8 +97,8 @@ namespace corryvreckan {
* Internal method for the dynamic loading in the central Analysis class. Forwards the supplied arguments to the
* constructor and returns an instantiation
*/
Module* corryvreckan_module_generator(Configuration config, Detector* detector);
Module* corryvreckan_module_generator(Configuration config, Detector* detector) {
Module* corryvreckan_module_generator(Configuration config, std::shared_ptr<Detector> detector);
Module* corryvreckan_module_generator(Configuration config, std::shared_ptr<Detector> detector) {
auto module = new CORRYVRECKAN_MODULE_NAME(std::move(config), std::move(detector)); // NOLINT
return static_cast<Module*>(module);
}
......
......@@ -4,7 +4,7 @@
using namespace corryvreckan;
using namespace std;
ATLASpixEventLoader::ATLASpixEventLoader(Configuration config, std::vector<Detector*> detectors)
ATLASpixEventLoader::ATLASpixEventLoader(Configuration config, std::vector<std::shared_ptr<Detector>> detectors)
: Module(std::move(config), std::move(detectors)) {
m_timewalkCorrectionFactors = m_config.getArray<double>("timewalkCorrectionFactors", std::vector<double>());
......
......@@ -22,7 +22,7 @@ namespace corryvreckan {
public:
// Constructors and destructors
ATLASpixEventLoader(Configuration config, std::vector<Detector*> detectors);
ATLASpixEventLoader(Configuration config, std::vector<std::shared_ptr<Detector>> detectors);
~ATLASpixEventLoader() {}
// Functions
......
......@@ -9,10 +9,10 @@ using namespace std;
// Global container declarations
Tracks globalTracks;
std::string detectorToAlign;
Detector* globalDetector;
std::shared_ptr<Detector> globalDetector;
int detNum;
Alignment::Alignment(Configuration config, std::vector<Detector*> detectors)
Alignment::Alignment(Configuration config, std::vector<std::shared_ptr<Detector>> detectors)
: Module(std::move(config), std::move(detectors)) {
m_numberOfTracksForAlignment = m_config.get<size_t>("number_of_tracks", 20000);
nIterations = m_config.get<size_t>("iterations", 3);
......
......@@ -21,7 +21,7 @@ namespace corryvreckan {
public:
// Constructors and destructors
Alignment(Configuration config, std::vector<Detector*> detectors);
Alignment(Configuration config, std::vector<std::shared_ptr<Detector>> detectors);
~Alignment() {}
// Functions
......
......@@ -5,7 +5,7 @@
using namespace corryvreckan;
using namespace std;
BasicTracking::BasicTracking(Configuration config, std::vector<Detector*> detectors)
BasicTracking::BasicTracking(Configuration config, std::vector<std::shared_ptr<Detector>> detectors)
: Module(std::move(config), std::move(detectors)) {
// Default values for cuts
......
......@@ -17,7 +17,7 @@ namespace corryvreckan {
public:
// Constructors and destructors
BasicTracking(Configuration config, std::vector<Detector*> detectors);
BasicTracking(Configuration config, std::vector<std::shared_ptr<Detector>> detectors);
~BasicTracking() {}
// Functions
......
......@@ -6,7 +6,7 @@
using namespace corryvreckan;
CLICpix2Analysis::CLICpix2Analysis(Configuration config, std::vector<Detector*> detectors)
CLICpix2Analysis::CLICpix2Analysis(Configuration config, std::vector<std::shared_ptr<Detector>> detectors)
: Module(std::move(config), std::move(detectors)) {
m_timeCutFrameEdge = m_config.get<double>("timeCutFrameEdge", static_cast<double>(Units::convert(20, "ns")));
......
......@@ -15,7 +15,7 @@ namespace corryvreckan {
public:
// Constructors and destructors
CLICpix2Analysis(Configuration config, std::vector<Detector*> detectors);
CLICpix2Analysis(Configuration config, std::vector<std::shared_ptr<Detector>> detectors);
~CLICpix2Analysis() {}
// Functions
......
......@@ -3,7 +3,7 @@
using namespace corryvreckan;
using namespace std;
CLICpixEventLoader::CLICpixEventLoader(Configuration config, std::vector<Detector*> detectors)
CLICpixEventLoader::CLICpixEventLoader(Configuration config, std::vector<std::shared_ptr<Detector>> detectors)
: Module(std::move(config), std::move(detectors)) {
m_filename = "";
}
......
......@@ -21,7 +21,7 @@ namespace corryvreckan {
public:
// Constructors and destructors
CLICpixEventLoader(Configuration config, std::vector<Detector*> detectors);
CLICpixEventLoader(Configuration config, std::vector<std::shared_ptr<Detector>> detectors);
~CLICpixEventLoader() {}
// Functions
......
......@@ -9,7 +9,7 @@ using namespace std;
using namespace caribou;
using namespace clicpix2_utils;
Clicpix2EventLoader::Clicpix2EventLoader(Configuration config, std::vector<Detector*> detectors)
Clicpix2EventLoader::Clicpix2EventLoader(Configuration config, std::vector<std::shared_ptr<Detector>> detectors)
: Module(std::move(config), std::move(detectors)) {
discardZeroToT = m_config.get<bool>("discardZeroToT", false);
......
......@@ -27,7 +27,7 @@ namespace corryvreckan {
public:
// Constructors and destructors
Clicpix2EventLoader(Configuration config, std::vector<Detector*> detectors);
Clicpix2EventLoader(Configuration config, std::vector<std::shared_ptr<Detector>> detectors);
~Clicpix2EventLoader() {}
// Functions
......
......@@ -6,7 +6,7 @@
using namespace corryvreckan;
using namespace std;
ClicpixAnalysis::ClicpixAnalysis(Configuration config, std::vector<Detector*> detectors)
ClicpixAnalysis::ClicpixAnalysis(Configuration config, std::vector<std::shared_ptr<Detector>> detectors)
: Module(std::move(config), std::move(detectors)) {
m_associationCut = m_config.get<double>("associationCut", static_cast<double>(Units::convert(100, "um")));
......
......@@ -16,7 +16,7 @@ namespace corryvreckan {
public:
// Constructors and destructors
ClicpixAnalysis(Configuration config, std::vector<Detector*> detectors);
ClicpixAnalysis(Configuration config, std::vector<std::shared_ptr<Detector>> detectors);
~ClicpixAnalysis() {}
// Functions
......
......@@ -7,7 +7,8 @@
using namespace corryvreckan;
DUTAnalysis::DUTAnalysis(Configuration config, Detector* detector) : Module(std::move(config), std::move(detector)) {
DUTAnalysis::DUTAnalysis(Configuration config, std::shared_ptr<Detector> detector)
: Module(std::move(config), std::move(detector)) {
m_digitalPowerPulsing = m_config.get<bool>("digitalPowerPulsing", false);
m_useMCtruth = m_config.get<bool>("useMCtruth", false);
chi2ndofCut = m_config.get<double>("chi2ndofCut", 3.);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment