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
47 auto pset = LoadParameterSet(argc, argv);
49 int run = pset.get<
int>(
"run_number", 1);
50 bool debug = pset.get<
bool>(
"debug_cout",
false);
51 uint64_t timeout = pset.get<uint64_t>(
"transition_timeout", 30);
52 uint64_t timestamp = 0;
54 artdaq::configureMessageFacility(
"artdaqDriver",
true, debug);
56 fhicl::ParameterSet fragment_receiver_pset = pset.get<fhicl::ParameterSet>(
"fragment_receiver");
58 std::unique_ptr<artdaq::FragmentGenerator>
59 gen(artdaq::makeFragmentGenerator(fragment_receiver_pset.get<std::string>(
"generator"),
60 fragment_receiver_pset));
62 std::unique_ptr<artdaq::CommandableFragmentGenerator> commandable_gen =
65 artdaq::MetricManager metricMan_;
66 metricMan = &metricMan_;
69 fhicl::ParameterSet metric_pset;
71 metric_pset = pset.get<fhicl::ParameterSet>(
"metrics");
75 if (metric_pset.is_empty()) {
76 TLOG(TLVL_INFO) <<
"No metric plugins appear to be defined";
79 metricMan_.initialize(metric_pset,
"artdaqDriver");
80 metricMan_.do_start();
84 artdaq::FragmentPtrs frags;
89 fhicl::ParameterSet event_builder_pset = pset.get<fhicl::ParameterSet>(
"event_builder");
94 int events_to_generate = pset.get<
int>(
"events_to_generate", 0);
96 artdaq::Fragment::sequence_id_t previous_sequence_id = -1;
98 if (commandable_gen) {
99 commandable_gen->
StartCmd(run, timeout, timestamp);
102 TLOG(50) <<
"driver main before event_manager.startRun";
103 event_manager.startRun(run);
109 while ((commandable_gen && commandable_gen->
getNext(frags)) ||
110 (gen && gen->getNext(frags))) {
111 TLOG(50) <<
"driver main: getNext returned frags.size()=" << std::to_string(frags.size()) <<
" current event_count=" << event_count;
112 for (
auto & val : frags) {
113 if (val->sequenceID() != previous_sequence_id) {
115 previous_sequence_id = val->sequenceID();
117 if (events_to_generate != 0 && event_count > events_to_generate) {
118 if (commandable_gen) {
119 commandable_gen->
StopCmd(timeout, timestamp);
123 artdaq::FragmentPtr tempFrag;
124 auto sts = event_manager.AddFragment(std::move(val), 1000000, tempFrag);
127 TLOG(TLVL_ERROR) <<
"Fragment was not added after 1s. Check art process status!";
133 if (events_to_generate != 0 && event_count >= events_to_generate) {
134 if (commandable_gen) {
135 commandable_gen->
StopCmd(timeout, timestamp);
141 if (commandable_gen) {
145 TLOG(TLVL_INFO) <<
"Fragments generated, waiting for art to process them.";
146 auto art_wait_start_time = std::chrono::steady_clock::now();
147 auto last_delta_time = std::chrono::steady_clock::now();
148 auto last_count = event_manager.size() - event_manager.WriteReadyCount(
false);
150 while (last_count > 0 && artdaq::TimeUtils::GetElapsedTime(last_delta_time) < 1.0)
152 auto this_count = event_manager.size() - event_manager.WriteReadyCount(
false);
153 if (this_count != last_count) {
154 last_delta_time = std::chrono::steady_clock::now();
155 last_count = this_count;
160 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).";
161 event_manager.endRun();
162 usleep(artdaq::TimeUtils::GetElapsedTimeMicroseconds(art_wait_start_time));
164 TLOG(TLVL_INFO) <<
"Shutting down art";
165 bool endSucceeded =
false;
166 int attemptsToEnd = 1;
167 endSucceeded = event_manager.endOfData();
168 while (!endSucceeded && attemptsToEnd < 3) {
170 endSucceeded = event_manager.endOfData();
173 TLOG(TLVL_ERROR) <<
"Failed to shut down the reader and the SharedMemoryEventManager "
174 <<
"because the endOfData marker could not be pushed "
175 <<
"onto the queue.";
178 metricMan_.do_stop();
181 catch (std::string & x)
183 std::cerr <<
"Exception (type string) caught in artdaqDriver: " << x <<
'\n';
186 catch (
char const * m)
188 std::cerr <<
"Exception (type char const*) caught in artdaqDriver: ";
195 std::cerr <<
"[the value was a null pointer, so no message is available]";
200 artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no,
201 "Exception caught in artdaqDriver");
205 template<
typename B,
typename D>
207 dynamic_unique_ptr_cast(std::unique_ptr<B>& p)
209 D* result =
dynamic_cast<D*
>(p.get());
213 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.