12 #define TRACE_NAME "genToArt"
14 #include "art/Framework/Art/artapp.h"
15 #include "canvas/Utilities/Exception.h"
16 #include "artdaq-core/Generators/FragmentGenerator.hh"
17 #include "artdaq-core/Data/Fragment.hh"
18 #include "artdaq/DAQdata/GenericFragmentSimulator.hh"
19 #include "artdaq/Application/CommandableFragmentGenerator.hh"
20 #include "artdaq/DAQrate/SharedMemoryEventManager.hh"
21 #include "artdaq-core/Generators/makeFragmentGenerator.hh"
22 #include "artdaq-core/Core/SimpleMemoryReader.hh"
23 #include "artdaq-core/Utilities/SimpleLookupPolicy.hh"
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;
49 bpo::variables_map& vm)
51 std::ostringstream descstr;
53 <<
" <-c <config-file>> <other-options> [<source-file>]+";
54 bpo::options_description desc(descstr.str());
56 (
"config,c", bpo::value<std::string>(),
"Configuration file.")
57 (
"help,h",
"produce help message");
60 bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
63 catch (bpo::error
const& e)
65 TLOG(TLVL_ERROR) <<
"Exception from command line processing in " << argv[0]
71 std::cout << desc << std::endl;
74 if (!vm.count(
"config"))
76 TLOG(TLVL_ERROR) <<
"Exception from command line processing in " << argv[0]
77 <<
": no configuration file given.\n"
78 <<
"For usage and an options list, please do '"
79 << argv[0] <<
" --help"
99 fhicl::ParameterSet
const& ps);
106 bool getNext(artdaq::FragmentPtrs& newFrags);
112 size_t numFragIDs()
const;
120 void start(
int run, uint64_t timeout, uint64_t timestamp)
const
123 if (gen_ptr !=
nullptr) gen_ptr->
StartCmd(run, timeout, timestamp);
130 void stop(uint64_t timeout, uint64_t timestamp)
const
133 if (gen_ptr !=
nullptr) gen_ptr->
StopCmd(timeout, timestamp);
137 bool generateFragments_();
139 std::unique_ptr<artdaq::FragmentGenerator> generator_;
140 size_t const numFragIDs_;
141 std::map<artdaq::Fragment::fragment_id_t,
142 std::deque<artdaq::FragmentPtr>> frags_;
146 ThrottledGenerator(std::string
const& generator,
147 fhicl::ParameterSet
const& ps)
149 generator_(artdaq::makeFragmentGenerator(generator, ps))
150 , numFragIDs_(generator_->fragmentIDs().size())
160 if (frags_.size() && frags_.begin()->second.size())
162 for (
auto& fQp : frags_)
164 assert(fQp.second.size());
165 newFrags.emplace_back(std::move(fQp.second.front()));
166 fQp.second.pop_front();
171 return generateFragments_() &&
getNext(newFrags);
180 artdaq::FragmentPtrs incomingFrags;
181 bool result{
false };
182 while ((result = generator_->getNext(incomingFrags)) &&
183 incomingFrags.empty()) {
185 for (
auto&& frag : incomingFrags)
187 frags_[frag->fragmentID()].emplace_back(std::move(frag));
225 auto const gta_pset = pset.get<fhicl::ParameterSet>(
"genToArt");
228 std::vector<ThrottledGenerator> generators;
230 auto const fr_pset = gta_pset.get<std::vector<fhicl::ParameterSet>>(
"fragment_receivers");
231 for (
auto const& gen_ps : fr_pset)
233 generators.emplace_back(gen_ps.get<std::string>(
"generator"),
237 artdaq::FragmentPtrs frags;
238 auto eb_pset = gta_pset.get<fhicl::ParameterSet>(
"event_builder", {});
239 size_t expected_frags_per_event = 0;
240 for (
auto& gen : generators)
242 gen.start(1000, 0, 0);
243 expected_frags_per_event += gen.numFragIDs();
245 eb_pset.put_or_replace<
size_t>(
"expected_fragments_per_event", expected_frags_per_event);
248 store.startRun(gta_pset.get<
int>(
"run_number", 1000));
250 auto const events_to_generate =
251 gta_pset.get<artdaq::Fragment::sequence_id_t>(
"events_to_generate", -1);
252 auto const reset_sequenceID = pset.get<
bool>(
"reset_sequenceID",
true);
254 for (artdaq::Fragment::sequence_id_t event_count = 1;
255 (events_to_generate ==
static_cast<decltype(events_to_generate)
>(-1)
256 || event_count <= events_to_generate) && (!done);
259 for (
auto& gen : generators)
261 done |= !gen.getNext(frags);
263 TLOG(TLVL_TRACE) <<
"There are " << std::to_string(frags.size()) <<
" Fragments in event " << std::to_string(event_count) <<
"." ;
264 artdaq::Fragment::sequence_id_t current_sequence_id = -1;
265 for (
auto& val : frags)
267 if (reset_sequenceID)
269 TLOG(TLVL_DEBUG) <<
"Setting fragment sequence id to " << std::to_string(event_count) ;
270 val->setSequenceID(event_count);
272 if (current_sequence_id ==
273 static_cast<artdaq::Fragment::sequence_id_t>(-1))
275 current_sequence_id = val->sequenceID();
277 else if (val->sequenceID() != current_sequence_id)
279 throw art::Exception(art::errors::DataCorruption)
280 <<
"Data corruption: apparently related fragments have "
281 <<
" different sequence IDs: "
284 << current_sequence_id
287 artdaq::FragmentPtr tempFrag;
288 auto sts = store.AddFragment(std::move(val), 1000000, tempFrag);
291 TLOG(TLVL_ERROR) <<
"Fragment was not added after 1s. Check art thread status!" ;
297 TLOG(TLVL_TRACE) <<
"Event " << std::to_string(event_count) <<
" END" ;
299 for (
auto& gen : generators)
304 bool endSucceeded = store.endOfData();
316 int main(
int argc,
char* argv[])
try
318 artdaq::configureMessageFacility(
"genToArt");
320 bpo::variables_map vm;
327 fhicl::ParameterSet pset;
328 if (getenv(
"FHICL_FILE_PATH") ==
nullptr)
331 <<
"INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
332 setenv(
"FHICL_FILE_PATH",
".", 0);
334 artdaq::SimpleLookupPolicy lookup_policy(
"FHICL_FILE_PATH");
335 make_ParameterSet(vm[
"config"].as<std::string>(), lookup_policy, pset);
338 catch (std::string& x)
340 TLOG(TLVL_ERROR) <<
"Exception (type string) caught in genToArt: " << x <<
'\n';
343 catch (
char const* m)
345 TLOG(TLVL_ERROR) <<
"Exception (type char const*) caught in genToArt: ";
348 TLOG(TLVL_ERROR) << m;
352 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...