12 #include "art/Framework/Art/artapp.h"
13 #include "canvas/Utilities/Exception.h"
14 #include "artdaq/Application/MPI2/MPISentry.hh"
15 #include "artdaq-core/Generators/FragmentGenerator.hh"
16 #include "artdaq-core/Data/Fragment.hh"
17 #include "artdaq/DAQdata/GenericFragmentSimulator.hh"
18 #include "artdaq/Application/CommandableFragmentGenerator.hh"
19 #include "artdaq/DAQrate/SharedMemoryEventManager.hh"
20 #include "artdaq-core/Generators/makeFragmentGenerator.hh"
21 #include "artdaq-core/Core/SimpleMemoryReader.hh"
22 #include "artdaq-core/Utilities/SimpleLookupPolicy.hh"
23 #include "cetlib/container_algorithms.h"
24 #include "fhiclcpp/ParameterSet.h"
25 #include "fhiclcpp/make_ParameterSet.h"
27 #include <boost/program_options.hpp>
36 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());
55 (
"config,c", bpo::value<std::string>(),
"Configuration file.")
56 (
"help,h",
"produce help message");
59 bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
62 catch (bpo::error
const& e)
64 std::cerr <<
"Exception from command line processing in " << argv[0]
65 <<
": " << e.what() <<
"\n";
70 std::cout << desc << std::endl;
73 if (!vm.count(
"config"))
75 std::cerr <<
"Exception from command line processing in " << argv[0]
76 <<
": no configuration file given.\n"
77 <<
"For usage and an options list, please do '"
78 << argv[0] <<
" --help"
98 fhicl::ParameterSet
const& ps);
105 bool getNext(artdaq::FragmentPtrs& newFrags);
111 size_t numFragIDs()
const;
119 void start(
int run, uint64_t timeout, uint64_t timestamp)
const
122 if (gen_ptr !=
nullptr) gen_ptr->
StartCmd(run, timeout, timestamp);
129 void stop(uint64_t timeout, uint64_t timestamp)
const
132 if (gen_ptr !=
nullptr) gen_ptr->
StopCmd(timeout, timestamp);
136 bool generateFragments_();
138 std::unique_ptr<artdaq::FragmentGenerator> generator_;
139 size_t const numFragIDs_;
140 std::map<artdaq::Fragment::fragment_id_t,
141 std::deque<artdaq::FragmentPtr>> frags_;
145 ThrottledGenerator(std::string
const& generator,
146 fhicl::ParameterSet
const& ps)
148 generator_(artdaq::makeFragmentGenerator(generator, ps))
149 , numFragIDs_(generator_->fragmentIDs().size())
159 if (frags_.size() && frags_.begin()->second.size())
161 for (
auto& fQp : frags_)
163 assert(fQp.second.size());
164 newFrags.emplace_back(std::move(fQp.second.front()));
165 fQp.second.pop_front();
170 return generateFragments_() &&
getNext(newFrags);
179 artdaq::FragmentPtrs incomingFrags;
180 bool result{
false };
181 while ((result = generator_->getNext(incomingFrags)) &&
182 incomingFrags.empty()) {
184 for (
auto&& frag : incomingFrags)
186 frags_[frag->fragmentID()].emplace_back(std::move(frag));
224 auto const gta_pset = pset.get<fhicl::ParameterSet>(
"genToArt");
227 std::vector<ThrottledGenerator> generators;
229 auto const fr_pset = gta_pset.get<std::vector<fhicl::ParameterSet>>(
"fragment_receivers");
230 for (
auto const& gen_ps : fr_pset)
232 generators.emplace_back(gen_ps.get<std::string>(
"generator"),
236 artdaq::FragmentPtrs frags;
237 auto eb_pset = gta_pset.get<fhicl::ParameterSet>(
"event_builder", {});
238 size_t expected_frags_per_event = 0;
239 for (
auto& gen : generators)
241 gen.start(1000, 0, 0);
242 expected_frags_per_event += gen.numFragIDs();
244 eb_pset.put_or_replace<
size_t>(
"expected_fragments_per_event", expected_frags_per_event);
247 store.startRun(gta_pset.get<
int>(
"run_number", 1000));
249 auto const events_to_generate =
250 gta_pset.get<artdaq::Fragment::sequence_id_t>(
"events_to_generate", -1);
251 auto const reset_sequenceID = pset.get<
bool>(
"reset_sequenceID",
true);
253 for (artdaq::Fragment::sequence_id_t event_count = 1;
254 (events_to_generate ==
static_cast<decltype(events_to_generate)
>(-1)
255 || event_count <= events_to_generate) && (!done);
258 for (
auto& gen : generators)
260 done |= !gen.getNext(frags);
262 TLOG_TRACE(
"genToArt") <<
"There are " << std::to_string(frags.size()) <<
" Fragments in event " << std::to_string(event_count) <<
"." << TLOG_ENDL;
263 artdaq::Fragment::sequence_id_t current_sequence_id = -1;
264 for (
auto& val : frags)
266 if (reset_sequenceID)
268 TLOG_DEBUG(
"genToArt") <<
"Setting fragment sequence id to " << std::to_string(event_count) << TLOG_ENDL;
269 val->setSequenceID(event_count);
271 if (current_sequence_id ==
272 static_cast<artdaq::Fragment::sequence_id_t>(-1))
274 current_sequence_id = val->sequenceID();
276 else if (val->sequenceID() != current_sequence_id)
278 throw art::Exception(art::errors::DataCorruption)
279 <<
"Data corruption: apparently related fragments have "
280 <<
" different sequence IDs: "
283 << current_sequence_id
286 artdaq::FragmentPtr tempFrag;
287 auto sts = store.AddFragment(std::move(val), 1000000, tempFrag);
290 TLOG_ERROR(
"genToArt") <<
"Fragment was not added after 1s. Check art thread status!" << TLOG_ENDL;
296 TLOG_TRACE(
"genToArt") <<
"Event " << std::to_string(event_count) <<
" END" << TLOG_ENDL;
298 for (
auto& gen : generators)
303 bool endSucceeded = store.endOfData();
315 int main(
int argc,
char* argv[])
try
317 artdaq::configureMessageFacility(
"genToArt");
321 bpo::variables_map vm;
328 fhicl::ParameterSet pset;
329 if (getenv(
"FHICL_FILE_PATH") ==
nullptr)
332 <<
"INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
333 setenv(
"FHICL_FILE_PATH",
".", 0);
335 artdaq::SimpleLookupPolicy lookup_policy(
"FHICL_FILE_PATH");
336 make_ParameterSet(vm[
"config"].as<std::string>(), lookup_policy, pset);
339 catch (std::string& x)
341 std::cerr <<
"Exception (type string) caught in genToArt: " << x <<
'\n';
344 catch (
char const* m)
346 std::cerr <<
"Exception (type char const*) caught in genToArt: ";
353 std::cerr <<
"[the value was a null pointer, so no message is available]";
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...
The MPISentry class initializes and finalizes the MPI context that the artdaq applciations run in...
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...