11 #define TRACE_NAME "artdaqDriver"
13 #include "art/Framework/Art/artapp.h"
14 #include "artdaq-core/Generators/FragmentGenerator.hh"
15 #include "artdaq-core/Data/Fragment.hh"
16 #include "artdaq-core/Utilities/ExceptionHandler.hh"
17 #include "artdaq/DAQdata/GenericFragmentSimulator.hh"
19 #include "artdaq/DAQdata/Globals.hh"
20 #include "artdaq-core/Generators/makeFragmentGenerator.hh"
21 #include "artdaq/Generators/makeCommandableFragmentGenerator.hh"
22 #include "artdaq-utilities/Plugins/MetricManager.hh"
23 #include "artdaq-core/Core/SimpleMemoryReader.hh"
24 #include "cetlib/filepath_maker.h"
25 #include "fhiclcpp/ParameterSet.h"
26 #include "fhiclcpp/make_ParameterSet.h"
27 #include <boost/program_options.hpp>
33 #include "artdaq/DAQrate/SharedMemoryEventManager.hh"
34 #include "artdaq/Application/LoadParameterSet.hh"
36 namespace bpo = boost::program_options;
38 volatile int events_to_generate;
39 void sig_handler(
int) { events_to_generate = -1; }
41 template<
typename B,
typename D>
43 dynamic_unique_ptr_cast(std::unique_ptr<B>& p);
45 int main(
int argc,
char * argv[])
try
49 fhicl::Atom<int> run_number{ fhicl::Name{
"run_number"}, fhicl::Comment{
"Run number to use for output file"}, 1 };
50 fhicl::Atom<bool> debug_cout{ fhicl::Name{
"debug_cout"}, fhicl::Comment{
"Whether to print debug messages to console"},
false };
51 fhicl::Atom<uint64_t> transition_timeout{ fhicl::Name{
"transition_timeout"}, fhicl::Comment{
"Timeout to use (in seconds) for automatic transitions"}, 30 };
52 fhicl::Table<artdaq::CommandableFragmentGenerator::Config> generator{ fhicl::Name{
"fragment_receiver" } };
53 fhicl::Table<artdaq::MetricManager::Config> metrics{ fhicl::Name{
"metrics"} };
54 fhicl::Table<artdaq::SharedMemoryEventManager::Config> event_builder{ fhicl::Name{
"event_builder"} };
55 fhicl::Atom<int> events_to_generate{ fhicl::Name{
"events_to_generate"}, fhicl::Comment{
"Number of events to generate and process"}, 0 };
56 fhicl::TableFragment<art::Config> art_config;
58 auto pset = LoadParameterSet<Config>(argc, argv,
"driver",
"The artdaqDriver executable runs a Fragment Generator and an art process, acting as a \"unit integration\" test for a data source");
60 int run = pset.get<
int>(
"run_number", 1);
61 bool debug = pset.get<
bool>(
"debug_cout",
false);
62 uint64_t timeout = pset.get<uint64_t>(
"transition_timeout", 30);
63 uint64_t timestamp = 0;
65 artdaq::configureMessageFacility(
"artdaqDriver",
true, debug);
67 fhicl::ParameterSet fragment_receiver_pset = pset.get<fhicl::ParameterSet>(
"fragment_receiver");
69 std::unique_ptr<artdaq::FragmentGenerator>
70 gen(artdaq::makeFragmentGenerator(fragment_receiver_pset.get<std::string>(
"generator"),
71 fragment_receiver_pset));
73 std::unique_ptr<artdaq::CommandableFragmentGenerator> commandable_gen =
78 fhicl::ParameterSet metric_pset;
80 metric_pset = pset.get<fhicl::ParameterSet>(
"metrics");
84 if (metric_pset.is_empty()) {
85 TLOG(TLVL_INFO) <<
"No metric plugins appear to be defined";
88 metricMan->initialize(metric_pset,
"artdaqDriver");
89 metricMan->do_start();
93 artdaq::FragmentPtrs frags;
98 fhicl::ParameterSet event_builder_pset = pset.get<fhicl::ParameterSet>(
"event_builder");
99 fhicl::ParameterSet art_pset = pset.get<fhicl::ParameterSet>(
"art", pset);
104 int events_to_generate = pset.get<
int>(
"events_to_generate", 0);
106 artdaq::Fragment::sequence_id_t previous_sequence_id = -1;
108 if (commandable_gen) {
109 commandable_gen->
StartCmd(run, timeout, timestamp);
112 TLOG(50) <<
"driver main before event_manager.startRun";
113 event_manager.startRun(run);
119 while ((commandable_gen && commandable_gen->
getNext(frags)) ||
120 (gen && gen->getNext(frags))) {
121 TLOG(50) <<
"driver main: getNext returned frags.size()=" << frags.size() <<
" current event_count=" << event_count;
122 for (
auto & val : frags) {
123 if (val->sequenceID() != previous_sequence_id) {
125 previous_sequence_id = val->sequenceID();
127 if (events_to_generate != 0 && event_count > events_to_generate) {
128 if (commandable_gen) {
129 commandable_gen->
StopCmd(timeout, timestamp);
134 auto start_time = std::chrono::steady_clock::now();
139 artdaq::FragmentPtr tempFrag;
140 sts = event_manager.AddFragment(std::move(val), 1000000, tempFrag);
141 if (!sts && event_count <= 10 && loop_count > 100)
143 TLOG(TLVL_ERROR) <<
"Fragment was not added after " << artdaq::TimeUtils::GetElapsedTime(start_time) <<
" s. Check art thread status!";
144 event_manager.endOfData();
147 val = std::move(tempFrag);
157 if (events_to_generate != 0 && event_count >= events_to_generate) {
158 if (commandable_gen) {
159 commandable_gen->
StopCmd(timeout, timestamp);
165 if (commandable_gen) {
169 TLOG(TLVL_INFO) <<
"Fragments generated, waiting for art to process them.";
170 auto art_wait_start_time = std::chrono::steady_clock::now();
171 auto last_delta_time = std::chrono::steady_clock::now();
172 auto last_count = event_manager.size() - event_manager.WriteReadyCount(
false);
174 while (last_count > 0 && artdaq::TimeUtils::GetElapsedTime(last_delta_time) < 1.0)
176 auto this_count = event_manager.size() - event_manager.WriteReadyCount(
false);
177 if (this_count != last_count) {
178 last_delta_time = std::chrono::steady_clock::now();
179 last_count = this_count;
184 TLOG(TLVL_INFO) <<
"Ending Run, waited " << std::setprecision(2) << artdaq::TimeUtils::GetElapsedTime(art_wait_start_time) <<
" seconds for art to process events. (" << last_count <<
" buffers remain).";
185 event_manager.endRun();
186 usleep(artdaq::TimeUtils::GetElapsedTimeMicroseconds(art_wait_start_time));
188 TLOG(TLVL_INFO) <<
"Shutting down art";
189 bool endSucceeded =
false;
190 int attemptsToEnd = 1;
191 endSucceeded = event_manager.endOfData();
192 while (!endSucceeded && attemptsToEnd < 3) {
194 endSucceeded = event_manager.endOfData();
197 TLOG(TLVL_ERROR) <<
"Failed to shut down the reader and the SharedMemoryEventManager "
198 <<
"because the endOfData marker could not be pushed "
199 <<
"onto the queue.";
202 metricMan->do_stop();
207 catch (std::string & x)
209 std::cerr <<
"Exception (type string) caught in artdaqDriver: " << x <<
'\n';
212 catch (
char const * m)
214 std::cerr <<
"Exception (type char const*) caught in artdaqDriver: ";
221 std::cerr <<
"[the value was a null pointer, so no message is available]";
226 artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no,
227 "Exception caught in artdaqDriver");
231 template<
typename B,
typename D>
233 dynamic_unique_ptr_cast(std::unique_ptr<B>& p)
235 D* result =
dynamic_cast<D*
>(p.get());
239 return std::unique_ptr<D>(result);
The SharedMemoryEventManager is a SharedMemoryManger which tracks events as they are built...
static void CleanUpGlobals()
Clean up statically-allocated Manager class instances.
void StopCmd(uint64_t timeout, uint64_t timestamp)
Stop the CommandableFragmentGenerator.
void StartCmd(int run, uint64_t timeout, uint64_t timestamp)
Start the CommandableFragmentGenerator.
bool getNext(FragmentPtrs &output) overridefinal
getNext calls either applyRequests or getNext_ to get any data that is ready to be sent to the EventB...
CommandableFragmentGenerator is a FragmentGenerator-derived abstract class that defines the interface...
void joinThreads()
Join any data-taking threads. Should be called when destructing CommandableFragmentGenerator.