From ddc630895091a54447d7a469b538e71ef616d037 Mon Sep 17 00:00:00 2001 From: Artur Sztuc Date: Tue, 15 Oct 2024 17:05:58 +0200 Subject: [PATCH 1/3] Adding extra configurability to RTCM --- plugins/RandomTCMakerModule.cpp | 77 ++++++++++++++++++++------------- plugins/RandomTCMakerModule.hpp | 19 ++++++++ 2 files changed, 65 insertions(+), 31 deletions(-) diff --git a/plugins/RandomTCMakerModule.cpp b/plugins/RandomTCMakerModule.cpp index a97ad29..26e3960 100644 --- a/plugins/RandomTCMakerModule.cpp +++ b/plugins/RandomTCMakerModule.cpp @@ -44,6 +44,7 @@ RandomTCMakerModule::RandomTCMakerModule(const std::string& name) register_command("start", &RandomTCMakerModule::do_start); register_command("stop_trigger_sources", &RandomTCMakerModule::do_stop); register_command("scrap", &RandomTCMakerModule::do_scrap); + register_command("change_rate", &RandomTCMakerModule::do_change_trigger_rate); } void @@ -61,7 +62,17 @@ RandomTCMakerModule::init(std::shared_ptr mcfg) m_time_sync_source = get_iom_receiver(con->UID()); } m_conf = mtrg->get_configuration(); + + // Get the TC out configuration + m_tc_readout = m_conf->get_tc_readout(); + m_latency_monitoring.store( m_conf->get_latency_monitoring() ); + + // Get the clock speed from detector configuration + m_clock_speed_hz = mcfg->configuration_manager()->session()->get_detector_configuration()->get_clock_speed_hz(); + m_trigger_rate_hz = m_conf->get_trigger_rate_hz(); + TLOG() << "Clock speed is: " << m_clock_speed_hz; + TLOG() << "Output trigger rate is: " << m_trigger_rate_hz; } void @@ -105,31 +116,26 @@ RandomTCMakerModule::do_start(const nlohmann::json& obj) std::string timestamp_method = m_conf->get_timestamp_method(); if (timestamp_method == "kTimeSync") { TLOG_DEBUG(0) << "Creating TimestampEstimator"; - m_timestamp_estimator.reset(new utilities::TimestampEstimator(m_run_number, m_conf->get_clock_frequency_hz())); + m_timestamp_estimator.reset(new utilities::TimestampEstimator(m_run_number, m_clock_speed_hz)); m_time_sync_source->add_callback(std::bind(&utilities::TimestampEstimator::timesync_callback, reinterpret_cast(m_timestamp_estimator.get()), std::placeholders::_1)); } else if(timestamp_method == "kSystemClock"){ TLOG_DEBUG(0) << "Creating TimestampEstimatorSystem"; - m_timestamp_estimator.reset(new utilities::TimestampEstimatorSystem(m_conf->get_clock_frequency_hz())); + m_timestamp_estimator.reset(new utilities::TimestampEstimatorSystem(m_clock_speed_hz)); } else{ // TODO: write some error message } - //switch (m_conf->get_timestamp_method()) { - // case randomtriggercandidatemaker::timestamp_estimation::kTimeSync: - // TLOG_DEBUG(0) << "Creating TimestampEstimator"; - // m_timestamp_estimator.reset(new utilities::TimestampEstimator(m_run_number, m_conf->get_clock_frequency_hz())); - // m_time_sync_source->add_callback(std::bind(&utilities::TimestampEstimator::timesync_callback, - // reinterpret_cast(m_timestamp_estimator.get()), - // std::placeholders::_1)); - // break; - // case randomtriggercandidatemaker::timestamp_estimation::kSystemClock: - // TLOG_DEBUG(0) << "Creating TimestampEstimatorSystem"; - // m_timestamp_estimator.reset(new utilities::TimestampEstimatorSystem(m_conf->get_clock_frequency_hz())); - // break; - //} + + // Check if the trigger rate was changed in the starting parameters, and set it if so + auto start_params = obj.get(); + if (start_params.trigger_rate > 0) { + TLOG() << " Changing rate: trigger_rate " + << start_params.trigger_rate; + m_trigger_rate_hz = start_params.trigger_rate; + } m_send_trigger_candidates_thread = std::thread(&RandomTCMakerModule::send_trigger_candidates, this); pthread_setname_np(m_send_trigger_candidates_thread.native_handle(), "random-tc-maker"); @@ -154,15 +160,32 @@ RandomTCMakerModule::do_scrap(const nlohmann::json& /*obj*/) m_configured_flag.store(false); } + +void +RandomTCMakerModule::do_change_trigger_rate(const nlohmann::json& obj) +{ + auto change_rate_params = obj.get(); + + TLOG() << "Changing trigger rate from " << m_trigger_rate_hz << " to " << change_rate_params.trigger_rate; + + m_trigger_rate_hz = change_rate_params.trigger_rate; +} + triggeralgs::TriggerCandidate RandomTCMakerModule::create_candidate(dfmessages::timestamp_t timestamp) { triggeralgs::TriggerCandidate candidate; - candidate.time_start = (timestamp - 1000); - candidate.time_end = (timestamp + 1000); + candidate.time_start = (timestamp - m_tc_readout->get_time_before()); + candidate.time_end = (timestamp + m_tc_readout->get_time_after()); candidate.time_candidate = timestamp; candidate.detid = { 0 }; - candidate.type = triggeralgs::TriggerCandidate::Type::kRandom; + candidate.type = static_cast(m_tc_readout->get_candidate_type()); + + // Throw error if unknown TC type + if (candidate.type == triggeralgs::TriggerCandidate::Type::kUnknown) { + throw InvalidConfiguration(ERS_HERE); + } + // TODO: Originally kHSIEventToTriggerCandidate candidate.algorithm = triggeralgs::TriggerCandidate::Algorithm::kCustom; @@ -174,27 +197,19 @@ RandomTCMakerModule::get_interval(std::mt19937& gen) { std::string time_distribution = m_conf->get_time_distribution(); + int interval = m_trigger_rate_hz * m_clock_speed_hz; + if( time_distribution == "kUniform"){ - return m_conf->get_trigger_interval_ticks(); + return interval; } else if(time_distribution == "kPoisson"){ - std::exponential_distribution d(1.0 / m_conf->get_trigger_interval_ticks()); + std::exponential_distribution d(1.0 / interval); return static_cast(0.5 + d(gen)); } else{ TLOG_DEBUG(1) << get_name() << " unknown distribution! Using kUniform."; } - return m_conf->get_trigger_interval_ticks(); - //switch (m_conf->get_time_distribution()) { - // default: // Treat an unknown distribution as kUniform, but warn - // TLOG_DEBUG(1) << get_name() << " unknown distribution! Using kUniform."; - // // fall through - // case randomtriggercandidatemaker::distribution_type::kUniform: - // return m_conf->get_trigger_interval_ticks(); - // case randomtriggercandidatemaker::distribution_type::kPoisson: - // std::exponential_distribution d(1.0 / m_conf->get_trigger_interval_ticks()); - // return static_cast(0.5 + d(gen)); - //} + return interval; } void diff --git a/plugins/RandomTCMakerModule.hpp b/plugins/RandomTCMakerModule.hpp index 5dc7698..f50fca7 100644 --- a/plugins/RandomTCMakerModule.hpp +++ b/plugins/RandomTCMakerModule.hpp @@ -12,9 +12,12 @@ #include "appfwk/DAQModule.hpp" #include "appfwk/ModuleConfiguration.hpp" #include "confmodel/Connection.hpp" +#include "confmodel/Session.hpp" +#include "confmodel/DetectorConfig.hpp" #include "appmodel/RandomTCMakerConf.hpp" #include "appmodel/RandomTCMakerModule.hpp" +#include "appmodel/TCReadoutMap.hpp" #include "daqdataformats/SourceID.hpp" #include "dfmessages/TimeSync.hpp" @@ -30,6 +33,8 @@ #include "trigger/opmon/randomtcmaker_info.pb.h" #include "trigger/opmon/latency_info.pb.h" +#include "rcif/cmd/Nljs.hpp" + #include #include #include @@ -73,6 +78,13 @@ class RandomTCMakerModule : public dunedaq::appfwk::DAQModule void do_stop(const nlohmann::json& obj); void do_scrap(const nlohmann::json& obj); + /** + * @brief Command function to change the output trigger rate + * + * @param obj descriprice json object with the ChangeRateParams + */ + void do_change_trigger_rate(const nlohmann::json& obj); + void send_trigger_candidates(); std::thread m_send_trigger_candidates_thread; @@ -87,6 +99,13 @@ class RandomTCMakerModule : public dunedaq::appfwk::DAQModule //randomtriggercandidatemaker::Conf m_conf; const appmodel::RandomTCMakerConf* m_conf; + const appmodel::TCReadoutMap* m_tc_readout; + + /// @brief Clock speed in hz, taken from detector configuration + uint64_t m_clock_speed_hz; + + /// @brief Output trigger rate in hz + std::atomic m_trigger_rate_hz{ 0 }; int get_interval(std::mt19937& gen); From f17e7684790986f7630813afe700071c7f1ba367 Mon Sep 17 00:00:00 2001 From: Artur Sztuc Date: Wed, 16 Oct 2024 16:59:27 +0200 Subject: [PATCH 2/3] Fixing a bug + accessing atomic properly --- plugins/RandomTCMakerModule.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/plugins/RandomTCMakerModule.cpp b/plugins/RandomTCMakerModule.cpp index 26e3960..a670575 100644 --- a/plugins/RandomTCMakerModule.cpp +++ b/plugins/RandomTCMakerModule.cpp @@ -70,9 +70,9 @@ RandomTCMakerModule::init(std::shared_ptr mcfg) // Get the clock speed from detector configuration m_clock_speed_hz = mcfg->configuration_manager()->session()->get_detector_configuration()->get_clock_speed_hz(); - m_trigger_rate_hz = m_conf->get_trigger_rate_hz(); + m_trigger_rate_hz.store(m_conf->get_trigger_rate_hz()); TLOG() << "Clock speed is: " << m_clock_speed_hz; - TLOG() << "Output trigger rate is: " << m_trigger_rate_hz; + TLOG() << "Output trigger rate is: " << m_trigger_rate_hz.load(); } void @@ -132,9 +132,8 @@ RandomTCMakerModule::do_start(const nlohmann::json& obj) // Check if the trigger rate was changed in the starting parameters, and set it if so auto start_params = obj.get(); if (start_params.trigger_rate > 0) { - TLOG() << " Changing rate: trigger_rate " - << start_params.trigger_rate; - m_trigger_rate_hz = start_params.trigger_rate; + TLOG() << "Changing trigger rate from " << m_trigger_rate_hz.load() << " to " << start_params.trigger_rate; + m_trigger_rate_hz.store(start_params.trigger_rate); } m_send_trigger_candidates_thread = std::thread(&RandomTCMakerModule::send_trigger_candidates, this); @@ -166,9 +165,9 @@ RandomTCMakerModule::do_change_trigger_rate(const nlohmann::json& obj) { auto change_rate_params = obj.get(); - TLOG() << "Changing trigger rate from " << m_trigger_rate_hz << " to " << change_rate_params.trigger_rate; + TLOG() << "Changing trigger rate from " << m_trigger_rate_hz.load() << " to " << change_rate_params.trigger_rate; - m_trigger_rate_hz = change_rate_params.trigger_rate; + m_trigger_rate_hz.store(change_rate_params.trigger_rate); } triggeralgs::TriggerCandidate @@ -197,7 +196,7 @@ RandomTCMakerModule::get_interval(std::mt19937& gen) { std::string time_distribution = m_conf->get_time_distribution(); - int interval = m_trigger_rate_hz * m_clock_speed_hz; + int interval = m_clock_speed_hz / m_trigger_rate_hz.load(); if( time_distribution == "kUniform"){ return interval; From 010c949c0893fd04a9b0960358d03228eef58020 Mon Sep 17 00:00:00 2001 From: Artur Sztuc Date: Wed, 16 Oct 2024 21:36:48 +0200 Subject: [PATCH 3/3] Accomodating for trigger-rate=0 case in RTCM --- plugins/RandomTCMakerModule.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/plugins/RandomTCMakerModule.cpp b/plugins/RandomTCMakerModule.cpp index a670575..a65c28f 100644 --- a/plugins/RandomTCMakerModule.cpp +++ b/plugins/RandomTCMakerModule.cpp @@ -229,6 +229,14 @@ RandomTCMakerModule::send_trigger_candidates() TLOG_DEBUG(1) << get_name() << " initial timestamp estimate is " << initial_timestamp; while (m_running_flag.load()) { + // If trigger rate is 0, just sleep. Need to use small number here because + // of floating point precision... + constexpr float epsilon = 1e-9; + if ( m_trigger_rate_hz.load() <= epsilon) { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + continue; + } + if (m_timestamp_estimator->wait_for_timestamp(next_trigger_timestamp, m_running_flag) == utilities::TimestampEstimatorBase::kInterrupted) { break;