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[0]
69 std::cout << desc << std::endl;
72 if (!vm.count(
"config"))
74 TLOG(TLVL_ERROR) <<
"Exception from command line processing in " << argv[0]
75 <<
": no configuration file given.\n"
76 <<
"For usage and an options list, please do '"
77 << argv[0] <<
" --help"
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) gen_ptr->
StartCmd(run, timeout, timestamp);
128 void stop(uint64_t timeout, uint64_t timestamp)
const
131 if (gen_ptr !=
nullptr) gen_ptr->
StopCmd(timeout, timestamp);
135 bool generateFragments_();
137 std::unique_ptr<artdaq::FragmentGenerator> generator_;
138 size_t const numFragIDs_;
139 std::map<artdaq::Fragment::fragment_id_t,
140 std::deque<artdaq::FragmentPtr>>
145 ThrottledGenerator(std::string
const& generator,
146 fhicl::ParameterSet
const& ps)
147 : generator_(artdaq::makeFragmentGenerator(generator, ps))
148 , numFragIDs_(generator_->fragmentIDs().size())
157 if (frags_.size() && frags_.begin()->second.size())
159 for (
auto& fQp : frags_)
161 assert(fQp.second.size());
162 newFrags.emplace_back(std::move(fQp.second.front()));
163 fQp.second.pop_front();
168 return generateFragments_() &&
getNext(newFrags);
173 bool ThrottledGenerator::
176 artdaq::FragmentPtrs incomingFrags;
178 while ((result = generator_->getNext(incomingFrags)) &&
179 incomingFrags.empty())
182 for (
auto&& frag : incomingFrags)
184 frags_[frag->fragmentID()].emplace_back(std::move(frag));
222 auto const gta_pset = pset.get<fhicl::ParameterSet>(
"genToArt");
225 std::vector<ThrottledGenerator> generators;
227 auto const fr_pset = gta_pset.get<std::vector<fhicl::ParameterSet>>(
"fragment_receivers");
228 for (
auto const& gen_ps : fr_pset)
230 generators.emplace_back(gen_ps.get<std::string>(
"generator"),
234 artdaq::FragmentPtrs frags;
235 auto eb_pset = gta_pset.get<fhicl::ParameterSet>(
"event_builder", {});
236 size_t expected_frags_per_event = 0;
237 for (
auto& gen : generators)
239 gen.start(1000, 0, 0);
240 expected_frags_per_event += gen.numFragIDs();
242 eb_pset.put_or_replace<
size_t>(
"expected_fragments_per_event", expected_frags_per_event);
245 store.startRun(gta_pset.get<
int>(
"run_number", 1000));
247 auto const events_to_generate =
248 gta_pset.get<artdaq::Fragment::sequence_id_t>(
"events_to_generate", 0xFFFFFFFFFFFFFFFF);
249 auto const reset_sequenceID = pset.get<
bool>(
"reset_sequenceID",
true);
251 for (artdaq::Fragment::sequence_id_t event_count = 1;
252 (events_to_generate == 0xFFFFFFFFFFFFFFFF || event_count <= events_to_generate) && (!done);
255 for (
auto& gen : generators)
257 done |= !gen.getNext(frags);
259 TLOG(TLVL_TRACE) <<
"There are " << frags.size() <<
" Fragments in event " << event_count <<
".";
260 artdaq::Fragment::sequence_id_t current_sequence_id = -1;
261 for (
auto& val : frags)
263 if (reset_sequenceID)
265 TLOG(TLVL_DEBUG) <<
"Setting fragment sequence id to " << event_count;
266 val->setSequenceID(event_count);
268 if (current_sequence_id ==
269 static_cast<artdaq::Fragment::sequence_id_t>(-1))
271 current_sequence_id = val->sequenceID();
273 else if (val->sequenceID() != current_sequence_id)
275 throw art::Exception(art::errors::DataCorruption)
276 <<
"Data corruption: apparently related fragments have "
277 <<
" different sequence IDs: "
280 << current_sequence_id
284 auto start_time = std::chrono::steady_clock::now();
289 artdaq::FragmentPtr tempFrag;
290 sts = store.AddFragment(std::move(val), 1000000, tempFrag);
291 if (!sts && event_count <= 10 && loop_count > 100)
293 TLOG(TLVL_ERROR) <<
"Fragment was not added after " << artdaq::TimeUtils::GetElapsedTime(start_time) <<
" s. Check art thread status!";
297 val = std::move(tempFrag);
306 TLOG(TLVL_TRACE) <<
"Event " << event_count <<
" END";
308 for (
auto& gen : generators)
313 bool endSucceeded = store.endOfData();
325 int main(
int argc,
char* argv[])
try
327 artdaq::configureMessageFacility(
"genToArt");
329 bpo::variables_map vm;
336 fhicl::ParameterSet pset;
337 if (getenv(
"FHICL_FILE_PATH") ==
nullptr)
340 <<
"INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
341 setenv(
"FHICL_FILE_PATH",
".", 0);
343 artdaq::SimpleLookupPolicy lookup_policy(
"FHICL_FILE_PATH");
344 make_ParameterSet(vm[
"config"].as<std::string>(), lookup_policy, pset);
347 catch (std::string& x)
349 TLOG(TLVL_ERROR) <<
"Exception (type string) caught in genToArt: " << x <<
'\n';
352 catch (
char const* m)
354 TLOG(TLVL_ERROR) <<
"Exception (type char const*) caught in genToArt: ";
357 TLOG(TLVL_ERROR) << m;
361 TLOG(TLVL_ERROR) <<
"[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...
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...