12 #define TRACE_NAME "genToArt"
14 #include "art/Framework/Art/artapp.h"
15 #include "artdaq-core/Data/Fragment.hh"
16 #include "artdaq-core/Data/detail/RawFragmentHeader.hh"
17 #include "artdaq-core/Generators/FragmentGenerator.hh"
18 #include "artdaq-core/Generators/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"
26 #include "fhiclcpp/make_ParameterSet.h"
28 #include <boost/program_options.hpp>
37 namespace bpo = boost::program_options;
48 bpo::variables_map& vm)
50 std::ostringstream descstr;
52 <<
" <-c <config-file>> <other-options> [<source-file>]+";
53 bpo::options_description desc(descstr.str());
54 desc.add_options()(
"config,c", bpo::value<std::string>(),
"Configuration file.")(
"help,h",
"produce help message");
57 bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
60 catch (bpo::error
const& e)
62 TLOG(TLVL_ERROR) <<
"Exception from command line processing in " << *argv
66 if (vm.count(
"help") != 0u)
68 std::cout << desc << std::endl;
71 if (vm.count(
"config") == 0u)
73 TLOG(TLVL_ERROR) <<
"Exception from command line processing in " << *argv
74 <<
": no configuration file given.\n"
75 <<
"For usage and an options list, please do '"
96 fhicl::ParameterSet
const& ps);
103 bool getNext(artdaq::FragmentPtrs& newFrags);
109 size_t numFragIDs()
const;
117 void start(
int run, uint64_t timeout, uint64_t timestamp)
const
120 if (gen_ptr !=
nullptr)
122 gen_ptr->
StartCmd(run, timeout, timestamp);
130 void stop(uint64_t timeout, uint64_t timestamp)
const
133 if (gen_ptr !=
nullptr)
135 gen_ptr->
StopCmd(timeout, timestamp);
140 bool generateFragments_();
142 std::unique_ptr<artdaq::FragmentGenerator> generator_;
143 size_t const numFragIDs_;
144 std::map<artdaq::Fragment::fragment_id_t,
145 std::deque<artdaq::FragmentPtr>>
150 ThrottledGenerator(std::string
const& generator,
151 fhicl::ParameterSet
const& ps)
152 : generator_(artdaq::makeFragmentGenerator(generator, ps))
153 , numFragIDs_(generator_->fragmentIDs().size())
162 if ((!frags_.empty()) && (!frags_.begin()->second.empty()))
164 for (
auto& fQp : frags_)
166 assert(fQp.second.size());
167 newFrags.emplace_back(std::move(fQp.second.front()));
168 fQp.second.pop_front();
173 return generateFragments_() &&
getNext(newFrags);
178 bool ThrottledGenerator::
181 artdaq::FragmentPtrs incomingFrags;
183 while ((result = generator_->getNext(incomingFrags)) &&
184 incomingFrags.empty())
187 for (
auto&& frag : incomingFrags)
189 frags_[frag->fragmentID()].emplace_back(std::move(frag));
227 auto const gta_pset = pset.get<fhicl::ParameterSet>(
"genToArt");
230 std::vector<ThrottledGenerator> generators;
232 auto const fr_pset = gta_pset.get<std::vector<fhicl::ParameterSet>>(
"fragment_receivers");
233 generators.reserve(fr_pset.size());
234 for (
auto const& gen_ps : fr_pset)
236 generators.emplace_back(gen_ps.get<std::string>(
"generator"),
240 artdaq::FragmentPtrs frags;
241 auto eb_pset = gta_pset.get<fhicl::ParameterSet>(
"event_builder", {});
242 size_t expected_frags_per_event = 0;
243 for (
auto& gen : generators)
245 gen.start(1000, 0, 0);
246 expected_frags_per_event += gen.numFragIDs();
248 eb_pset.put_or_replace<
size_t>(
"expected_fragments_per_event", expected_frags_per_event);
251 store.startRun(gta_pset.get<
int>(
"run_number", 1000));
253 auto const events_to_generate =
254 gta_pset.get<artdaq::Fragment::sequence_id_t>(
"events_to_generate", 0xFFFFFFFFFFFFFFFF);
255 auto const reset_sequenceID = pset.get<
bool>(
"reset_sequenceID",
true);
257 for (artdaq::Fragment::sequence_id_t event_count = 1;
258 (events_to_generate == 0xFFFFFFFFFFFFFFFF || event_count <= events_to_generate) && (!done);
261 for (
auto& gen : generators)
263 done |= !gen.getNext(frags);
265 TLOG(TLVL_TRACE) <<
"There are " << frags.size() <<
" Fragments in event " << event_count <<
".";
266 artdaq::Fragment::sequence_id_t current_sequence_id = -1;
267 for (
auto& val : frags)
269 if (reset_sequenceID)
271 TLOG(TLVL_DEBUG) <<
"Setting fragment sequence id to " << event_count;
272 val->setSequenceID(event_count);
274 if (current_sequence_id ==
275 static_cast<artdaq::Fragment::sequence_id_t>(-1))
277 current_sequence_id = val->sequenceID();
279 else if (val->sequenceID() != current_sequence_id)
281 throw art::Exception(art::errors::DataCorruption)
282 <<
"Data corruption: apparently related fragments have "
283 <<
" different sequence IDs: "
286 << current_sequence_id
290 auto start_time = std::chrono::steady_clock::now();
295 artdaq::FragmentPtr tempFrag;
296 sts = store.AddFragment(std::move(val), 1000000, tempFrag);
297 if (!sts && event_count <= 10 && loop_count > 100)
299 TLOG(TLVL_ERROR) <<
"Fragment was not added after " << artdaq::TimeUtils::GetElapsedTime(start_time) <<
" s. Check art thread status!";
303 val = std::move(tempFrag);
312 TLOG(TLVL_TRACE) <<
"Event " << event_count <<
" END";
314 for (
auto& gen : generators)
319 bool endSucceeded = store.endOfData();
329 int main(
int argc,
char* argv[])
try
331 artdaq::configureMessageFacility(
"genToArt");
333 bpo::variables_map vm;
340 fhicl::ParameterSet pset;
341 if (getenv(
"FHICL_FILE_PATH") ==
nullptr)
344 <<
"INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
345 setenv(
"FHICL_FILE_PATH",
".", 0);
347 artdaq::SimpleLookupPolicy lookup_policy(
"FHICL_FILE_PATH");
348 make_ParameterSet(vm[
"config"].as<std::string>(), lookup_policy, pset);
351 catch (std::exception& x)
353 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...