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/Application/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");
103 int events_to_generate = pset.get<
int>(
"events_to_generate", 0);
105 artdaq::Fragment::sequence_id_t previous_sequence_id = -1;
107 if (commandable_gen) {
108 commandable_gen->
StartCmd(run, timeout, timestamp);
111 TLOG(50) <<
"driver main before event_manager.startRun";
112 event_manager.startRun(run);
118 while ((commandable_gen && commandable_gen->
getNext(frags)) ||
119 (gen && gen->getNext(frags))) {
120 TLOG(50) <<
"driver main: getNext returned frags.size()=" << frags.size() <<
" current event_count=" << event_count;
121 for (
auto & val : frags) {
122 if (val->sequenceID() != previous_sequence_id) {
124 previous_sequence_id = val->sequenceID();
126 if (events_to_generate != 0 && event_count > events_to_generate) {
127 if (commandable_gen) {
128 commandable_gen->
StopCmd(timeout, timestamp);
133 auto start_time = std::chrono::steady_clock::now();
138 artdaq::FragmentPtr tempFrag;
139 sts = event_manager.AddFragment(std::move(val), 1000000, tempFrag);
140 if (!sts && event_count <= 10 && loop_count > 100)
142 TLOG(TLVL_ERROR) <<
"Fragment was not added after " << artdaq::TimeUtils::GetElapsedTime(start_time) <<
" s. Check art thread status!";
143 event_manager.endOfData();
146 val = std::move(tempFrag);
156 if (events_to_generate != 0 && event_count >= events_to_generate) {
157 if (commandable_gen) {
158 commandable_gen->
StopCmd(timeout, timestamp);
164 if (commandable_gen) {
168 TLOG(TLVL_INFO) <<
"Fragments generated, waiting for art to process them.";
169 auto art_wait_start_time = std::chrono::steady_clock::now();
170 auto last_delta_time = std::chrono::steady_clock::now();
171 auto last_count = event_manager.size() - event_manager.WriteReadyCount(
false);
173 while (last_count > 0 && artdaq::TimeUtils::GetElapsedTime(last_delta_time) < 1.0)
175 auto this_count = event_manager.size() - event_manager.WriteReadyCount(
false);
176 if (this_count != last_count) {
177 last_delta_time = std::chrono::steady_clock::now();
178 last_count = this_count;
183 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).";
184 event_manager.endRun();
185 usleep(artdaq::TimeUtils::GetElapsedTimeMicroseconds(art_wait_start_time));
187 TLOG(TLVL_INFO) <<
"Shutting down art";
188 bool endSucceeded =
false;
189 int attemptsToEnd = 1;
190 endSucceeded = event_manager.endOfData();
191 while (!endSucceeded && attemptsToEnd < 3) {
193 endSucceeded = event_manager.endOfData();
196 TLOG(TLVL_ERROR) <<
"Failed to shut down the reader and the SharedMemoryEventManager "
197 <<
"because the endOfData marker could not be pushed "
198 <<
"onto the queue.";
201 metricMan->do_stop();
204 catch (std::string & x)
206 std::cerr <<
"Exception (type string) caught in artdaqDriver: " << x <<
'\n';
209 catch (
char const * m)
211 std::cerr <<
"Exception (type char const*) caught in artdaqDriver: ";
218 std::cerr <<
"[the value was a null pointer, so no message is available]";
223 artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no,
224 "Exception caught in artdaqDriver");
228 template<
typename B,
typename D>
230 dynamic_unique_ptr_cast(std::unique_ptr<B>& p)
232 D* result =
dynamic_cast<D*
>(p.get());
236 return std::unique_ptr<D>(result);
The SharedMemoryEventManager is a SharedMemoryManger which tracks events as they are built...
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.