12 #include "TRACE/tracemf.h"
13 #define TRACE_NAME "genToArt"
15 #include "artdaq-core/Data/Fragment.hh"
16 #include "artdaq-core/Data/detail/RawFragmentHeader.hh"
17 #include "artdaq-core/Plugins/FragmentGenerator.hh"
18 #include "artdaq-core/Plugins/makeFragmentGenerator.hh"
19 #include "artdaq-core/Utilities/SimpleLookupPolicy.hh"
20 #include "artdaq/DAQdata/GenericFragmentSimulator.hh"
21 #include "artdaq/DAQrate/SharedMemoryEventManager.hh"
22 #include "artdaq/Generators/CommandableFragmentGenerator.hh"
23 #include "canvas/Utilities/Exception.h"
24 #include "cetlib/container_algorithms.h"
25 #include "fhiclcpp/ParameterSet.h"
27 #include <boost/program_options.hpp>
36 namespace bpo = boost::program_options;
47 bpo::variables_map& vm)
49 std::ostringstream descstr;
51 <<
" <-c <config-file>> <other-options> [<source-file>]+";
52 bpo::options_description desc(descstr.str());
53 desc.add_options()(
"config,c", bpo::value<std::string>(),
"Configuration file.")(
"help,h",
"produce help message");
56 bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
59 catch (bpo::error
const& e)
61 TLOG(TLVL_ERROR) <<
"Exception from command line processing in " << *argv
65 if (vm.count(
"help") != 0u)
67 std::cout << desc << std::endl;
70 if (vm.count(
"config") == 0u)
72 TLOG(TLVL_ERROR) <<
"Exception from command line processing in " << *argv
73 <<
": no configuration file given.\n"
74 <<
"For usage and an options list, please do '"
95 fhicl::ParameterSet
const& ps);
102 bool getNext(artdaq::FragmentPtrs& newFrags);
108 size_t numFragIDs()
const;
116 void start(
int run, uint64_t timeout, uint64_t timestamp)
const
119 if (gen_ptr !=
nullptr)
121 gen_ptr->
StartCmd(run, timeout, timestamp);
129 void stop(uint64_t timeout, uint64_t timestamp)
const
132 if (gen_ptr !=
nullptr)
134 gen_ptr->
StopCmd(timeout, timestamp);
139 bool generateFragments_();
141 std::unique_ptr<artdaq::FragmentGenerator> generator_;
142 size_t const numFragIDs_;
143 std::map<artdaq::Fragment::fragment_id_t,
144 std::deque<artdaq::FragmentPtr>>
149 ThrottledGenerator(std::string
const& generator,
150 fhicl::ParameterSet
const& ps)
151 : generator_(artdaq::makeFragmentGenerator(generator, ps))
152 , numFragIDs_(generator_->fragmentIDs().size())
161 if ((!frags_.empty()) && (!frags_.begin()->second.empty()))
163 for (
auto& fQp : frags_)
165 assert(fQp.second.size());
166 newFrags.emplace_back(std::move(fQp.second.front()));
167 fQp.second.pop_front();
172 return generateFragments_() &&
getNext(newFrags);
177 bool ThrottledGenerator::
180 artdaq::FragmentPtrs incomingFrags;
182 while ((result = generator_->getNext(incomingFrags)) &&
183 incomingFrags.empty())
186 for (
auto&& frag : incomingFrags)
188 frags_[frag->fragmentID()].emplace_back(std::move(frag));
226 auto const gta_pset = pset.get<fhicl::ParameterSet>(
"genToArt");
229 std::vector<ThrottledGenerator> generators;
231 auto const fr_pset = gta_pset.get<std::vector<fhicl::ParameterSet>>(
"fragment_receivers");
232 generators.reserve(fr_pset.size());
233 for (
auto const& gen_ps : fr_pset)
235 generators.emplace_back(gen_ps.get<std::string>(
"generator"),
239 artdaq::FragmentPtrs frags;
240 auto eb_pset = gta_pset.get<fhicl::ParameterSet>(
"event_builder", {});
241 size_t expected_frags_per_event = 0;
242 for (
auto& gen : generators)
244 gen.start(1000, 0, 0);
245 expected_frags_per_event += gen.numFragIDs();
247 eb_pset.put_or_replace<
size_t>(
"expected_fragments_per_event", expected_frags_per_event);
250 store.startRun(gta_pset.get<
int>(
"run_number", 1000));
252 auto const events_to_generate =
253 gta_pset.get<artdaq::Fragment::sequence_id_t>(
"events_to_generate", 0xFFFFFFFFFFFFFFFF);
254 auto const reset_sequenceID = pset.get<
bool>(
"reset_sequenceID",
true);
256 for (artdaq::Fragment::sequence_id_t event_count = 1;
257 (events_to_generate == 0xFFFFFFFFFFFFFFFF || event_count <= events_to_generate) && (!done);
260 for (
auto& gen : generators)
262 done |= !gen.getNext(frags);
264 TLOG(TLVL_DEBUG + 33) <<
"There are " << frags.size() <<
" Fragments in event " << event_count <<
".";
265 artdaq::Fragment::sequence_id_t current_sequence_id = -1;
266 for (
auto& val : frags)
268 if (reset_sequenceID)
270 TLOG(TLVL_DEBUG + 32) <<
"Setting fragment sequence id to " << event_count;
271 val->setSequenceID(event_count);
273 if (current_sequence_id ==
274 static_cast<artdaq::Fragment::sequence_id_t>(-1))
276 current_sequence_id = val->sequenceID();
278 else if (val->sequenceID() != current_sequence_id)
280 throw art::Exception(art::errors::DataCorruption)
281 <<
"Data corruption: apparently related fragments have "
282 <<
" different sequence IDs: "
285 << current_sequence_id
289 auto start_time = std::chrono::steady_clock::now();
294 artdaq::FragmentPtr tempFrag;
295 sts = store.AddFragment(std::move(val), 1000000, tempFrag);
296 if (!sts && event_count <= 10 && loop_count > 100)
298 TLOG(TLVL_ERROR) <<
"Fragment was not added after " << artdaq::TimeUtils::GetElapsedTime(start_time) <<
" s. Check art thread status!";
302 val = std::move(tempFrag);
311 TLOG(TLVL_DEBUG + 33) <<
"Event " << event_count <<
" END";
313 for (
auto& gen : generators)
318 bool endSucceeded = store.endOfData();
328 int main(
int argc,
char* argv[])
331 artdaq::configureMessageFacility(
"genToArt");
333 bpo::variables_map vm;
340 if (getenv(
"FHICL_FILE_PATH") ==
nullptr)
343 <<
"INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
344 setenv(
"FHICL_FILE_PATH",
".", 0);
346 artdaq::SimpleLookupPolicy lookup_policy(
"FHICL_FILE_PATH");
347 auto pset = fhicl::ParameterSet::make(vm[
"config"].as<std::string>(), lookup_policy);
350 catch (std::exception& x)
352 TLOG(TLVL_ERROR) <<
"Exception (type std::exception) caught in genToArt: " << x.what() <<
'\n';
void start(int run, uint64_t timeout, uint64_t timestamp) const
Send start signal to FragmentGenerator, if it's a CommandableFragmentGenerator.
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.
int process_cmd_line(int argc, char **argv, bpo::variables_map &vm)
Process the command line.
bool getNext(artdaq::FragmentPtrs &newFrags)
Get the next fragment from the generator.
CommandableFragmentGenerator is a FragmentGenerator-derived abstract class that defines the interface...
int process_data(fhicl::ParameterSet const &pset)
Run the test, instantiating configured generators and an EventStore.
void stop(uint64_t timeout, uint64_t timestamp) const
Send stop signal to FragmentGenerator, if it's a CommandableFragmentGenerator.
size_t numFragIDs() const
Get the number of Fragment IDs handled by this generator.
ThrottledGenerator: ensure that we only get one fragment per type at a time from the generator...