Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TA & TC Factory Method Abstraction #56

Merged
merged 11 commits into from
Jan 19, 2024
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ add_library(triggeralgs SHARED
src/TPWindow.cpp
src/dbscan/dbscan.cpp
src/dbscan/Hit.cpp
src/TriggerActivityFactory.cpp
)

target_include_directories(triggeralgs PUBLIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifndef TRIGGERALGS_ADCSIMPLEWINDOW_TRIGGERACTIVITYMAKERADCSIMPLEWINDOW_HPP_
#define TRIGGERALGS_ADCSIMPLEWINDOW_TRIGGERACTIVITYMAKERADCSIMPLEWINDOW_HPP_

#include "triggeralgs/TriggerActivityMaker.hpp"
#include "triggeralgs/TriggerActivityFactory.hpp"
#include "triggeralgs/Types.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef TRIGGERALGS_ADCSIMPLEWINDOW_TRIGGERCANDIDATEMAKERADCSIMPLEWINDOW_HPP_
#define TRIGGERALGS_ADCSIMPLEWINDOW_TRIGGERCANDIDATEMAKERADCSIMPLEWINDOW_HPP_

#include "triggeralgs/TriggerCandidateMaker.hpp"
#include "triggeralgs/TriggerCandidateFactory.hpp"

#include <vector>

Expand Down
50 changes: 50 additions & 0 deletions include/triggeralgs/AbstractFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* @file: AbstractFactory.hpp
*
* This is part of the DUNE DAQ Application Framework, copyright 2023.
* Licensing/copyright details are in the COPYING file that you should have
* received with this code.
*/

#ifndef TRIGGERALGS_ABSTRACT_FACTORY_HPP_
#define TRIGGERALGS_ABSTRACT_FACTORY_HPP_

#include "logging/Logging.hpp"

#include <functional>
#include <memory>
#include <string>
#include <unordered_map>

namespace triggeralgs {

template <typename T>
class AbstractFactory
{
using maker_creator = std::function<std::unique_ptr<T>()>;
using creation_map = std::unordered_map<std::string, maker_creator>;

public:
AbstractFactory() {}
AbstractFactory(const AbstractFactory&) = delete;
AbstractFactory& operator=(const AbstractFactory&) = delete;
virtual ~AbstractFactory() {}

std::unique_ptr<T> build_maker(const std::string& alg_name);

static void register_creator(const std::string alg_name, maker_creator creator);

static std::shared_ptr<AbstractFactory<T>> get_instance();

protected:
static std::shared_ptr<AbstractFactory<T>> s_single_factory;

private:
static creation_map& get_makers();
};

} /* namespace triggeralgs */

#include "triggeralgs/AbstractFactory.hxx"

// TODO: Define ers exceptions
#endif // TRIGGERALGS_ABSTRACT_FACTORY_HPP_
61 changes: 61 additions & 0 deletions include/triggeralgs/AbstractFactory.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* @file: AbstractFactory.hxx
*
* This is part of the DUNE DAQ Application Framework, copyright 2023.
* Licensing/copyright details are in the COPYING file that you should have
* received with this code.
*/

#ifndef TRIGGERALGS_ABSTRACT_FACTORY_HXX_
#define TRIGGERALGS_ABSTRACT_FACTORY_HXX_

namespace triggeralgs {

template <typename T>
std::shared_ptr<AbstractFactory<T>> AbstractFactory<T>::s_single_factory = nullptr;

template <typename T>
typename AbstractFactory<T>::creation_map& AbstractFactory<T>::get_makers(){
static creation_map s_makers;
return s_makers;
}

template <typename T>
void AbstractFactory<T>::register_creator(const std::string alg_name, maker_creator creator)
{
creation_map& makers = get_makers();
auto it = makers.find(alg_name);

if (it == makers.end()) {
makers[alg_name] = creator;
return;
}
return;
}

template <typename T>
std::unique_ptr<T> AbstractFactory<T>::build_maker(const std::string& alg_name)
{
creation_map& makers = get_makers();
auto it = makers.find(alg_name);

if (it != makers.end()) {
TLOG(0) << "Factory building " << alg_name << ".";
return it->second();
}

TLOG(0) << "Factory couldn't find " << alg_name << ".";
return nullptr;
}

template <typename T>
std::shared_ptr<AbstractFactory<T>> AbstractFactory<T>::get_instance()
{
if (s_single_factory == nullptr) {
s_single_factory = std::make_shared<AbstractFactory<T>>();
}
return s_single_factory;
}

} // namespace triggeralgs

#endif // TRIGGERALGS_ABSTRACT_FACTORY_HXX_
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifndef TRIGGERALGS_HORIZONTALMUON_TRIGGERACTIVITYMAKERHORIZONTALMUON_HPP_
#define TRIGGERALGS_HORIZONTALMUON_TRIGGERACTIVITYMAKERHORIZONTALMUON_HPP_

#include "triggeralgs/TriggerActivityMaker.hpp"
#include "triggeralgs/TPWindow.hpp"
#include "triggeralgs/TriggerActivityFactory.hpp"
#include <fstream>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef TRIGGERALGS_HORIZONTALMUON_TRIGGERCANDIDATEMAKERHORIZONTALMUON_HPP_
#define TRIGGERALGS_HORIZONTALMUON_TRIGGERCANDIDATEMAKERHORIZONTALMUON_HPP_

#include "triggeralgs/TriggerCandidateMaker.hpp"
#include "triggeralgs/TriggerCandidateFactory.hpp"
#include "triggeralgs/TAWindow.hpp"

#include <fstream>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifndef TRIGGERALGS_MICHELELECTRON_TRIGGERACTIVITYMAKERMICHELELECTRON_HPP_
#define TRIGGERALGS_MICHELELECTRON_TRIGGERACTIVITYMAKERMICHELELECTRON_HPP_

#include "triggeralgs/TriggerActivityMaker.hpp"
#include "triggeralgs/TriggerActivityFactory.hpp"
#include <fstream>
#include <vector>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef TRIGGERALGS_MICHELELECTRON_TRIGGERCANDIDATEMAKERMICHELELECTRON_HPP_
#define TRIGGERALGS_MICHELELECTRON_TRIGGERCANDIDATEMAKERMICHELELECTRON_HPP_

#include "triggeralgs/TriggerCandidateMaker.hpp"
#include "triggeralgs/TriggerCandidateFactory.hpp"

//#include "triggeralgs/triggercandidatemakerhorizontalmuon/Nljs.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define TRIGGERALGS_PLANECOINCIDENCE_TRIGGERACTIVITYMAKERPLANECOINCIDENCE_HPP_

#include "detchannelmaps/TPCChannelMap.hpp"
#include "triggeralgs/TriggerActivityMaker.hpp"
#include "triggeralgs/TriggerActivityFactory.hpp"
#include "triggeralgs/TPWindow.hpp"
#include <fstream>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef TRIGGERALGS_PLANECOINCIDENCE_TRIGGERCANDIDATEMAKERPLANECOINCIDENCE_HPP_
#define TRIGGERALGS_PLANECOINCIDENCE_TRIGGERCANDIDATEMAKERPLANECOINCIDENCE_HPP_

#include "triggeralgs/TriggerCandidateMaker.hpp"
#include "triggeralgs/TriggerCandidateFactory.hpp"
#include "triggeralgs/TAWindow.hpp"
#include <fstream>
#include <vector>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifndef TRIGGERALGS_PRESCALE_TRIGGERACTIVITYMAKERPRESCALE_HPP_
#define TRIGGERALGS_PRESCALE_TRIGGERACTIVITYMAKERPRESCALE_HPP_

#include "triggeralgs/TriggerActivityMaker.hpp"
#include "triggeralgs/TriggerActivityFactory.hpp"

#include <vector>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef TRIGGERALGS_PRESCALE_TRIGGERCANDIDATEMAKERPRESCALE_HPP_
#define TRIGGERALGS_PRESCALE_TRIGGERCANDIDATEMAKERPRESCALE_HPP_

#include "triggeralgs/TriggerCandidateMaker.hpp"
#include "triggeralgs/TriggerCandidateFactory.hpp"

#include <vector>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifndef TRIGGERALGS_SRC_TRIGGERALGS_SUPERNOVA_TRIGGERACTIVITYMAKERSUPERNOVA_HPP_
#define TRIGGERALGS_SRC_TRIGGERALGS_SUPERNOVA_TRIGGERACTIVITYMAKERSUPERNOVA_HPP_

#include "triggeralgs/TriggerActivityMaker.hpp"
#include "triggeralgs/TriggerActivityFactory.hpp"

#include <algorithm>
Expand Down Expand Up @@ -52,8 +51,6 @@ class TriggerActivityMakerSupernova : public TriggerActivityMaker

void flush(timestamp_t, std::vector<TriggerActivity>& tas) override { tas.push_back(MakeTriggerActivity()); }

static std::shared_ptr<TriggerActivityMaker> createMaker();

protected:
timestamp_diff_t m_time_tolerance =
250; /// Maximum tolerated time difference between two primitives to form an activity (in 50 MHz clock ticks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef TRIGGERALGS_SRC_TRIGGERALGS_SUPERNOVA_TRIGGERCANDIDATEMAKERSUPERNOVA_HPP_
#define TRIGGERALGS_SRC_TRIGGERALGS_SUPERNOVA_TRIGGERCANDIDATEMAKERSUPERNOVA_HPP_

#include "triggeralgs/TriggerCandidateMaker.hpp"
#include "triggeralgs/TriggerCandidateFactory.hpp"
#include "triggeralgs/Types.hpp"

#include <algorithm>
Expand Down
27 changes: 3 additions & 24 deletions include/triggeralgs/TriggerActivityFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,18 @@
#define TRIGGERALGS_TRIGGER_ACTIVITY_FACTORY_HPP_

#include "triggeralgs/TriggerActivityMaker.hpp"

#include <memory>
#include <string>
#include <unordered_map>
#include <functional>
#include "triggeralgs/AbstractFactory.hpp"

#define REGISTER_TRIGGER_ACTIVITY_MAKER(tam_name, tam_class) \
static struct tam_class##Registrar { \
tam_class##Registrar() { \
TriggerActivityFactory::registerCreator(tam_name, []() -> std::shared_ptr<TriggerActivityMaker> {return std::make_shared<tam_class>();}); \
TriggerActivityFactory::register_creator(tam_name, []() -> std::unique_ptr<TriggerActivityMaker> {return std::make_unique<tam_class>();}); \
} \
} tam_class##_registrar;

namespace triggeralgs {

class TriggerActivityFactory
{
public:
using TAMakerCreator = std::function<std::shared_ptr<TriggerActivityMaker>()>;
using TAMakerMap = std::unordered_map<std::string, TAMakerCreator>;

public:
TriggerActivityFactory(const TriggerActivityFactory&) = delete;
TriggerActivityFactory& operator=(const TriggerActivityFactory&) = delete;
virtual ~TriggerActivityFactory() {}

static std::shared_ptr<TriggerActivityMaker> makeTAMaker(const std::string& algName);

static void registerCreator(const std::string algName, TAMakerCreator creator);

private:
static TAMakerMap& getTAMakers();
};
class TriggerActivityFactory : public AbstractFactory<TriggerActivityMaker> {};

} /* namespace triggeralgs */

Expand Down
28 changes: 28 additions & 0 deletions include/triggeralgs/TriggerCandidateFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* @file: TriggerCandidateFactory.hpp
*
* This is part of the DUNE DAQ Application Framework, copyright 2023.
* Licensing/copyright details are in the COPYING file that you should have
* received with this code.
*/

#ifndef TRIGGERALGS_TRIGGER_CANDIDATE_FACTORY_HPP_
#define TRIGGERALGS_TRIGGER_CANDIDATE_FACTORY_HPP_

#include "triggeralgs/TriggerCandidateMaker.hpp"
#include "triggeralgs/AbstractFactory.hpp"

#define REGISTER_TRIGGER_CANDIDATE_MAKER(tcm_name, tcm_class) \
static struct tcm_class##Registrar { \
tcm_class##Registrar() { \
TriggerCandidateFactory::register_creator(tcm_name, []() -> std::unique_ptr<TriggerCandidateMaker> {return std::make_unique<tcm_class>();}); \
} \
} tcm_class##_registrar;

namespace triggeralgs {

class TriggerCandidateFactory : public AbstractFactory<TriggerCandidateMaker> {};

} /* namespace triggeralgs */

// TODO: Define ers exceptions
#endif // TRIGGERALGS_TRIGGER_CANDIDATE_FACTORY_HPP_
1 change: 0 additions & 1 deletion include/triggeralgs/dbscan/TriggerActivityMakerDBSCAN.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifndef TRIGGERALGS_DBSCAN_TRIGGERACTIVITYMAKERDBSCAN_HPP_
#define TRIGGERALGS_DBSCAN_TRIGGERACTIVITYMAKERDBSCAN_HPP_

#include "triggeralgs/TriggerActivityMaker.hpp"
#include "triggeralgs/TriggerActivityFactory.hpp"
#include "triggeralgs/dbscan/dbscan.hpp"

Expand Down
40 changes: 0 additions & 40 deletions src/TriggerActivityFactory.cpp

This file was deleted.

3 changes: 2 additions & 1 deletion src/TriggerCandidateMakerADCSimpleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "triggeralgs/ADCSimpleWindow/TriggerCandidateMakerADCSimpleWindow.hpp"

#include "TRACE/trace.h"
#define TRACE_NAME "TriggerCandidateMakerADCSimpleWindow"
#define TRACE_NAME "TriggerCandidateMakerADCSimpleWindowPlugin"

#include <vector>

Expand Down Expand Up @@ -44,3 +44,4 @@ TriggerCandidateMakerADCSimpleWindow::configure(const nlohmann::json &config)
{
}

REGISTER_TRIGGER_CANDIDATE_MAKER(TRACE_NAME, TriggerCandidateMakerADCSimpleWindow)
4 changes: 3 additions & 1 deletion src/TriggerCandidateMakerHorizontalMuon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "triggeralgs/HorizontalMuon/TriggerCandidateMakerHorizontalMuon.hpp"

#include "TRACE/trace.h"
#define TRACE_NAME "TriggerCandidateMakerHorizontalMuon"
#define TRACE_NAME "TriggerCandidateMakerHorizontalMuonPlugin"

#include <vector>
#include <math.h>
Expand Down Expand Up @@ -227,3 +227,5 @@ reset."; m_current_window.clear();

return;
}*/

REGISTER_TRIGGER_CANDIDATE_MAKER(TRACE_NAME, TriggerCandidateMakerHorizontalMuon)
4 changes: 3 additions & 1 deletion src/TriggerCandidateMakerMichelElectron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "triggeralgs/MichelElectron/TriggerCandidateMakerMichelElectron.hpp"

#include "TRACE/trace.h"
#define TRACE_NAME "TriggerCandidateMakerMichelElectron"
#define TRACE_NAME "TriggerCandidateMakerMichelElectronPlugin"

#include <vector>

Expand Down Expand Up @@ -219,3 +219,5 @@ reset."; m_current_window.clear();

return;
}*/

REGISTER_TRIGGER_CANDIDATE_MAKER(TRACE_NAME, TriggerCandidateMakerMichelElectron)
Loading