12 #define TRACE_NAME "genToArt"
14 #include "art/Framework/Art/artapp.h"
15 #include "artdaq-core/Core/SimpleMemoryReader.hh"
16 #include "artdaq-core/Data/Fragment.hh"
17 #include "artdaq-core/Data/detail/RawFragmentHeader.hh"
18 #include "artdaq-core/Generators/FragmentGenerator.hh"
19 #include "artdaq-core/Generators/makeFragmentGenerator.hh"
20 #include "artdaq-core/Utilities/SimpleLookupPolicy.hh"
21 #include "artdaq/DAQdata/GenericFragmentSimulator.hh"
22 #include "artdaq/DAQrate/SharedMemoryEventManager.hh"
23 #include "artdaq/Generators/CommandableFragmentGenerator.hh"
24 #include "canvas/Utilities/Exception.h"
25 #include "cetlib/container_algorithms.h"
26 #include "fhiclcpp/ParameterSet.h"
27 #include "fhiclcpp/make_ParameterSet.h"
29 #include <boost/program_options.hpp>
38 namespace bpo = boost::program_options;
49 bpo::variables_map& vm)
51 std::ostringstream descstr;
53 <<
" <-c <config-file>> <other-options> [<source-file>]+";
54 bpo::options_description desc(descstr.str());
55 desc.add_options()(
"config,c", bpo::value<std::string>(),
"Configuration file.")(
"help,h",
"produce help message");
58 bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
61 catch (bpo::error
const& e)
63 TLOG(TLVL_ERROR) <<
"Exception from command line processing in " << *argv
67 if (vm.count(
"help") != 0u)
69 std::cout << desc << std::endl;
72 if (vm.count(
"config") == 0u)
74 TLOG(TLVL_ERROR) <<
"Exception from command line processing in " << *argv
75 <<
": no configuration file given.\n"
76 <<
"For usage and an options list, please do '"
97 fhicl::ParameterSet
const& ps);
104 bool getNext(artdaq::FragmentPtrs& newFrags);
110 size_t numFragIDs()
const;
118 void start(
int run, uint64_t timeout, uint64_t timestamp)
const
121 if (gen_ptr !=
nullptr)
123 gen_ptr->
StartCmd(run, timeout, timestamp);
131 void stop(uint64_t timeout, uint64_t timestamp)
const
134 if (gen_ptr !=
nullptr)
136 gen_ptr->
StopCmd(timeout, timestamp);
141 bool generateFragments_();
143 std::unique_ptr<artdaq::FragmentGenerator> generator_;
144 size_t const numFragIDs_;
145 std::map<artdaq::Fragment::fragment_id_t,
146 std::deque<artdaq::FragmentPtr>>
151 ThrottledGenerator(std::string
const& generator,
152 fhicl::ParameterSet
const& ps)
153 : generator_(artdaq::makeFragmentGenerator(generator, ps))
154 , numFragIDs_(generator_->fragmentIDs().size())
163 if ((!frags_.empty()) && (!frags_.begin()->second.empty()))
165 for (
auto& fQp : frags_)
167 assert(fQp.second.size());
168 newFrags.emplace_back(std::move(fQp.second.front()));
169 fQp.second.pop_front();
174 return generateFragments_() &&
getNext(newFrags);
179 bool ThrottledGenerator::
182 artdaq::FragmentPtrs incomingFrags;
184 while ((result = generator_->getNext(incomingFrags)) &&
185 incomingFrags.empty())
188 for (
auto&& frag : incomingFrags)
190 frags_[frag->fragmentID()].emplace_back(std::move(frag));
228 auto const gta_pset = pset.get<fhicl::ParameterSet>(
"genToArt");
231 std::vector<ThrottledGenerator> generators;
233 auto const fr_pset = gta_pset.get<std::vector<fhicl::ParameterSet>>(
"fragment_receivers");
234 generators.reserve(fr_pset.size());
235 for (
auto const& gen_ps : fr_pset)
237 generators.emplace_back(gen_ps.get<std::string>(
"generator"),
241 artdaq::FragmentPtrs frags;
242 auto eb_pset = gta_pset.get<fhicl::ParameterSet>(
"event_builder", {});
243 size_t expected_frags_per_event = 0;
244 for (
auto& gen : generators)
246 gen.start(1000, 0, 0);
247 expected_frags_per_event += gen.numFragIDs();
249 eb_pset.put_or_replace<
size_t>(
"expected_fragments_per_event", expected_frags_per_event);
252 store.startRun(gta_pset.get<
int>(
"run_number", 1000));
254 auto const events_to_generate =
255 gta_pset.get<artdaq::Fragment::sequence_id_t>(
"events_to_generate", 0xFFFFFFFFFFFFFFFF);
256 auto const reset_sequenceID = pset.get<
bool>(
"reset_sequenceID",
true);
258 for (artdaq::Fragment::sequence_id_t event_count = 1;
259 (events_to_generate == 0xFFFFFFFFFFFFFFFF || event_count <= events_to_generate) && (!done);
262 for (
auto& gen : generators)
264 done |= !gen.getNext(frags);
266 TLOG(TLVL_TRACE) <<
"There are " << frags.size() <<
" Fragments in event " << event_count <<
".";
267 artdaq::Fragment::sequence_id_t current_sequence_id = -1;
268 for (
auto& val : frags)
270 if (reset_sequenceID)
272 TLOG(TLVL_DEBUG) <<
"Setting fragment sequence id to " << event_count;
273 val->setSequenceID(event_count);
275 if (current_sequence_id ==
276 static_cast<artdaq::Fragment::sequence_id_t>(-1))
278 current_sequence_id = val->sequenceID();
280 else if (val->sequenceID() != current_sequence_id)
282 throw art::Exception(art::errors::DataCorruption)
283 <<
"Data corruption: apparently related fragments have "
284 <<
" different sequence IDs: "
287 << current_sequence_id
291 auto start_time = std::chrono::steady_clock::now();
296 artdaq::FragmentPtr tempFrag;
297 sts = store.AddFragment(std::move(val), 1000000, tempFrag);
298 if (!sts && event_count <= 10 && loop_count > 100)
300 TLOG(TLVL_ERROR) <<
"Fragment was not added after " << artdaq::TimeUtils::GetElapsedTime(start_time) <<
" s. Check art thread status!";
304 val = std::move(tempFrag);
313 TLOG(TLVL_TRACE) <<
"Event " << event_count <<
" END";
315 for (
auto& gen : generators)
320 bool endSucceeded = store.endOfData();
330 int main(
int argc,
char* argv[])
333 artdaq::configureMessageFacility(
"genToArt");
335 bpo::variables_map vm;
342 fhicl::ParameterSet pset;
343 if (getenv(
"FHICL_FILE_PATH") ==
nullptr)
346 <<
"INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
347 setenv(
"FHICL_FILE_PATH",
".", 0);
349 artdaq::SimpleLookupPolicy lookup_policy(
"FHICL_FILE_PATH");
350 make_ParameterSet(vm[
"config"].as<std::string>(), lookup_policy, pset);
353 catch (std::exception& x)
355 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...