Skip to content

Commit

Permalink
Merge pull request #55 from DUNE-DAQ/aeoranday/refactoring
Browse files Browse the repository at this point in the history
Trigger Activity Factory Refactorization
  • Loading branch information
aeoranday authored Dec 12, 2023
2 parents e393a80 + 419dbc7 commit f90d1d8
Show file tree
Hide file tree
Showing 21 changed files with 180 additions and 81 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ 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 @@ -10,6 +10,7 @@
#define TRIGGERALGS_ADCSIMPLEWINDOW_TRIGGERACTIVITYMAKERADCSIMPLEWINDOW_HPP_

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

#include <vector>
Expand Down Expand Up @@ -94,7 +95,6 @@ class TriggerActivityMakerADCSimpleWindow : public TriggerActivityMaker
// Configurable parameters.
uint32_t m_adc_threshold = 1200000;
timestamp_t m_window_length = 100000;

};
} // namespace triggeralgs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "triggeralgs/TriggerActivityMaker.hpp"
#include "triggeralgs/TPWindow.hpp"
#include "triggeralgs/TriggerActivityFactory.hpp"
#include <fstream>
#include <vector>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#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 @@ -11,6 +11,7 @@

#include "detchannelmaps/TPCChannelMap.hpp"
#include "triggeralgs/TriggerActivityMaker.hpp"
#include "triggeralgs/TriggerActivityFactory.hpp"
#include "triggeralgs/TPWindow.hpp"
#include <fstream>
#include <vector>
Expand Down Expand Up @@ -67,7 +68,6 @@ class TriggerActivityMakerPlaneCoincidence : public TriggerActivityMaker
void dump_window_record();
void dump_tp(TriggerPrimitive const& input_tp);
std::vector<TPWindow> m_window_record;

};
} // namespace triggeralgs
#endif // TRIGGERALGS_PLANECOINCIDENCE_TRIGGERACTIVITYMAKERPLANECOINCIDENCE_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define TRIGGERALGS_PRESCALE_TRIGGERACTIVITYMAKERPRESCALE_HPP_

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

#include <vector>

Expand All @@ -25,7 +26,6 @@ class TriggerActivityMakerPrescale : public TriggerActivityMaker
private:
uint64_t m_primitive_count = 0; // NOLINT(build/unsigned)
uint64_t m_prescale = 1; // NOLINT(build/unsigned)

};
} // namespace triggeralgs

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

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

#include <algorithm>
#include <limits>
Expand Down Expand Up @@ -51,6 +52,8 @@ 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
49 changes: 49 additions & 0 deletions include/triggeralgs/TriggerActivityFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* @file: TriggerActivityFactory.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_ACTIVITY_FACTORY_HPP_
#define TRIGGERALGS_TRIGGER_ACTIVITY_FACTORY_HPP_

#include "triggeralgs/TriggerActivityMaker.hpp"

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

#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>();}); \
} \
} 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();
};

} /* namespace triggeralgs */

// TODO: Define ers exceptions
#endif // TRIGGERALGS_TRIGGER_ACTIVITY_FACTORY_HPP_
2 changes: 1 addition & 1 deletion include/triggeralgs/dbscan/TriggerActivityMakerDBSCAN.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define TRIGGERALGS_DBSCAN_TRIGGERACTIVITYMAKERDBSCAN_HPP_

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

#include <memory>
Expand All @@ -30,7 +31,6 @@ class TriggerActivityMakerDBSCAN : public TriggerActivityMaker
timestamp_t m_prev_timestamp{0};
std::vector<dbscan::Cluster> m_dbscan_clusters;
std::unique_ptr<dbscan::IncrementalDBSCAN> m_dbscan;

};
} // namespace triggeralgs

Expand Down
40 changes: 40 additions & 0 deletions src/TriggerActivityFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* @file: TriggerActivityFactory.cpp
*
* 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.
*/
#include <iostream>

#include "triggeralgs/TriggerActivityFactory.hpp"
#include "TRACE/trace.h"
#define TRACE_NAME "TriggerActivityFactory"

using namespace triggeralgs;

TriggerActivityFactory::TAMakerMap& TriggerActivityFactory::getTAMakers() {
static TAMakerMap s_makers;
return s_makers;
}

void TriggerActivityFactory::registerCreator(const std::string algName, TAMakerCreator creator)
{
TAMakerMap& makers = getTAMakers();

auto it = makers.find(algName);
if (it == makers.end()) {
makers[algName] = creator;
}
}

std::shared_ptr<TriggerActivityMaker> TriggerActivityFactory::makeTAMaker(const std::string& algName)
{
TAMakerMap& makers = getTAMakers();

auto it = makers.find(algName);
if (it != makers.end()) {
return it->second();
}

return nullptr;
}
5 changes: 4 additions & 1 deletion src/TriggerActivityMakerADCSimpleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "triggeralgs/ADCSimpleWindow/TriggerActivityMakerADCSimpleWindow.hpp"

#include "TRACE/trace.h"
#define TRACE_NAME "TriggerActivityMakerADCSimpleWindow"
#define TRACE_NAME "TriggerActivityMakerADCSimpleWindowPlugin"

#include <vector>

Expand Down Expand Up @@ -96,3 +96,6 @@ TriggerActivityMakerADCSimpleWindow::construct_ta() const
ta.inputs = m_current_window.tp_list;
return ta;
}

// Register algo in TA Factory
REGISTER_TRIGGER_ACTIVITY_MAKER(TRACE_NAME, TriggerActivityMakerADCSimpleWindow)
5 changes: 4 additions & 1 deletion src/TriggerActivityMakerDBSCAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "triggeralgs/Types.hpp"
#include <chrono>
#include <limits>
#define TRACE_NAME "TriggerActivityMakerDBSCAN"
#define TRACE_NAME "TriggerActivityMakerDBSCANPlugin"

#include <vector>

Expand Down Expand Up @@ -86,3 +86,6 @@ TriggerActivityMakerDBSCAN::configure(const nlohmann::json &config)

m_dbscan=std::make_unique<dbscan::IncrementalDBSCAN>(10, m_min_pts, 10000);
}

// Register algo in TA Factory
REGISTER_TRIGGER_ACTIVITY_MAKER(TRACE_NAME, TriggerActivityMakerDBSCAN)
5 changes: 4 additions & 1 deletion src/TriggerActivityMakerHorizontalMuon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "triggeralgs/HorizontalMuon/TriggerActivityMakerHorizontalMuon.hpp"
#include "TRACE/trace.h"
#define TRACE_NAME "TriggerActivityMakerHorizontalMuon"
#define TRACE_NAME "TriggerActivityMakerHorizontalMuonPlugin"
#include <vector>
#include <math.h>

Expand Down Expand Up @@ -342,3 +342,6 @@ TriggerActivityMakerHorizontalMuon::check_tot() const

return window_tot;
}

// Register algo in TA Factory
REGISTER_TRIGGER_ACTIVITY_MAKER(TRACE_NAME, TriggerActivityMakerHorizontalMuon)
5 changes: 4 additions & 1 deletion src/TriggerActivityMakerMichelElectron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "triggeralgs/MichelElectron/TriggerActivityMakerMichelElectron.hpp"
#include "TRACE/trace.h"
#define TRACE_NAME "TriggerActivityMakerMichelElectron"
#define TRACE_NAME "TriggerActivityMakerMichelElectronPlugin"
#include <vector>
#include <algorithm>

Expand Down Expand Up @@ -384,3 +384,6 @@ reset."; m_current_window.clear();
return;
}*/

// Register algo in TA Factory
REGISTER_TRIGGER_ACTIVITY_MAKER(TRACE_NAME, TriggerActivityMakerMichelElectron)
4 changes: 3 additions & 1 deletion src/TriggerActivityMakerPlaneCoincidence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "triggeralgs/PlaneCoincidence/TriggerActivityMakerPlaneCoincidence.hpp"
#include "TRACE/trace.h"
#define TRACE_NAME "TriggerActivityMakerPlaneCoincidence"
#define TRACE_NAME "TriggerActivityMakerPlaneCoincidencePlugin"
#include <vector>

using namespace triggeralgs;
Expand Down Expand Up @@ -273,4 +273,6 @@ TriggerActivityMakerPlaneCoincidence::check_tot(TPWindow m_current_window) const
return window_tot;
}

// Regiser algo in TA Factory
REGISTER_TRIGGER_ACTIVITY_MAKER(TRACE_NAME, TriggerActivityMakerPlaneCoincidence)
// END OF TA MAKER - LOW ENERGY EVENTS
5 changes: 4 additions & 1 deletion src/TriggerActivityMakerPrescale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "triggeralgs/Prescale/TriggerActivityMakerPrescale.hpp"

#include "TRACE/trace.h"
#define TRACE_NAME "TriggerActivityMakerPrescale"
#define TRACE_NAME "TriggerActivityMakerPrescalePlugin"

#include <vector>

Expand Down Expand Up @@ -62,3 +62,6 @@ TriggerActivityMakerPrescale::configure(const nlohmann::json &config)
}
TLOG_DEBUG(TRACE_NAME) << "Using activity prescale " << m_prescale;
}

// Register algo in TA Factory
REGISTER_TRIGGER_ACTIVITY_MAKER(TRACE_NAME, TriggerActivityMakerPrescale)
6 changes: 6 additions & 0 deletions src/TriggerActivityMakerSupernova.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#include "triggeralgs/Supernova/TriggerActivityMakerSupernova.hpp"

#include "TRACE/trace.h"
#define TRACE_NAME "TriggerActivityMakerSupernovaPlugin"

#include <chrono>
#include <vector>

Expand Down Expand Up @@ -79,3 +82,6 @@ TriggerActivityMakerSupernova::operator()(const TriggerPrimitive& input_tp, std:
m_adc_integral += input_tp.adc_integral;
m_detid |= input_tp.detid;
}

// Register algo in TA Factory
REGISTER_TRIGGER_ACTIVITY_MAKER(TRACE_NAME, TriggerActivityMakerSupernova)
12 changes: 4 additions & 8 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@

add_executable(test_trivial test_trivial.cxx)
target_include_directories(test_trivial PRIVATE ${BOOST_INCLUDE_DIRS})
add_test(NAME trivial COMMAND test_trivial)

add_executable(test_supernova test_supernova.cxx)
target_include_directories(test_supernova PRIVATE ${BOOST_INCLUDE_DIRS})
add_test(NAME supernova COMMAND test_supernova)
add_executable(test_factory test_factory.cxx)
target_link_libraries(test_factory PRIVATE triggeralgs trgdataformats::trgdataformats)
target_include_directories(test_factory PRIVATE ${BOOST_INCLUDE_DIRS})
add_test(NAME factory COMMAND test_factory)
48 changes: 48 additions & 0 deletions test/test_factory.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @file test_factory.cxx
*
* 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.
*/

// NOLINTNEXTLINE(build/define_used)
#define BOOST_TEST_MODULE boost_test_macro_overview

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

#include <boost/test/included/unit_test.hpp>

#include <iostream>
#include <vector>

using namespace dunedaq;

namespace triggeralgs {

BOOST_AUTO_TEST_CASE(test_macro_overview)
{
std::shared_ptr<TriggerActivityMaker> prescale_maker = TriggerActivityFactory::makeTAMaker("TriggerActivityMakerPrescalePlugin");

std::vector<TriggerActivity> prescale_ta;
TriggerPrimitive some_tp;
for (int idx = 0; idx < 10; idx++) {
some_tp.type = TriggerPrimitive::Type::kTPC;
some_tp.algorithm = TriggerPrimitive::Algorithm::kTPCDefault;
some_tp.time_start = idx;
some_tp.time_peak = 1+idx;
some_tp.time_over_threshold = 2;
some_tp.adc_integral = 1000+idx;
some_tp.adc_peak = 1000+idx;
some_tp.channel = 0+idx;
some_tp.detid = 0;
(*prescale_maker)(some_tp, prescale_ta);
}

bool same_alg = (prescale_ta[0].algorithm == TriggerActivity::Algorithm::kPrescale);

BOOST_TEST(same_alg);
}

} /* namespace triggeralgs */
18 changes: 0 additions & 18 deletions test/test_supernova.cxx

This file was deleted.

Loading

0 comments on commit f90d1d8

Please sign in to comment.