00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #define TRACE_NAME "genToArt"
00013
00014 #include "art/Framework/Art/artapp.h"
00015 #include "canvas/Utilities/Exception.h"
00016 #include "artdaq-core/Generators/FragmentGenerator.hh"
00017 #include "artdaq-core/Data/Fragment.hh"
00018 #include "artdaq/DAQdata/GenericFragmentSimulator.hh"
00019 #include "artdaq/Application/CommandableFragmentGenerator.hh"
00020 #include "artdaq/DAQrate/SharedMemoryEventManager.hh"
00021 #include "artdaq-core/Generators/makeFragmentGenerator.hh"
00022 #include "artdaq-core/Core/SimpleMemoryReader.hh"
00023 #include "artdaq-core/Utilities/SimpleLookupPolicy.hh"
00024 #include "cetlib/container_algorithms.h"
00025 #include "fhiclcpp/ParameterSet.h"
00026 #include "fhiclcpp/make_ParameterSet.h"
00027
00028 #include <boost/program_options.hpp>
00029
00030 #include <deque>
00031 #include <iostream>
00032 #include <map>
00033 #include <memory>
00034 #include <string>
00035 #include <utility>
00036
00037 namespace bpo = boost::program_options;
00038
00039 namespace
00040 {
00048 int process_cmd_line(int argc, char** argv,
00049 bpo::variables_map& vm)
00050 {
00051 std::ostringstream descstr;
00052 descstr << argv[0]
00053 << " <-c <config-file>> <other-options> [<source-file>]+";
00054 bpo::options_description desc(descstr.str());
00055 desc.add_options()
00056 ("config,c", bpo::value<std::string>(), "Configuration file.")
00057 ("help,h", "produce help message");
00058 try
00059 {
00060 bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
00061 bpo::notify(vm);
00062 }
00063 catch (bpo::error const& e)
00064 {
00065 TLOG(TLVL_ERROR) << "Exception from command line processing in " << argv[0]
00066 << ": " << e.what();
00067 return -1;
00068 }
00069 if (vm.count("help"))
00070 {
00071 std::cout << desc << std::endl;
00072 return 1;
00073 }
00074 if (!vm.count("config"))
00075 {
00076 TLOG(TLVL_ERROR) << "Exception from command line processing in " << argv[0]
00077 << ": no configuration file given.\n"
00078 << "For usage and an options list, please do '"
00079 << argv[0] << " --help"
00080 << "'.";
00081 return 2;
00082 }
00083 return 0;
00084 }
00085
00090 class ThrottledGenerator
00091 {
00092 public:
00098 ThrottledGenerator(std::string const& generator,
00099 fhicl::ParameterSet const& ps);
00100
00106 bool getNext(artdaq::FragmentPtrs& newFrags);
00107
00112 size_t numFragIDs() const;
00113
00120 void start(int run, uint64_t timeout, uint64_t timestamp) const
00121 {
00122 auto gen_ptr = dynamic_cast<artdaq::CommandableFragmentGenerator*>(generator_.get());
00123 if (gen_ptr != nullptr) gen_ptr->StartCmd(run, timeout, timestamp);
00124 }
00130 void stop(uint64_t timeout, uint64_t timestamp) const
00131 {
00132 auto gen_ptr = dynamic_cast<artdaq::CommandableFragmentGenerator*>(generator_.get());
00133 if (gen_ptr != nullptr) gen_ptr->StopCmd(timeout, timestamp);
00134 }
00135
00136 private:
00137 bool generateFragments_();
00138
00139 std::unique_ptr<artdaq::FragmentGenerator> generator_;
00140 size_t const numFragIDs_;
00141 std::map<artdaq::Fragment::fragment_id_t,
00142 std::deque<artdaq::FragmentPtr>> frags_;
00143 };
00144
00145 ThrottledGenerator::
00146 ThrottledGenerator(std::string const& generator,
00147 fhicl::ParameterSet const& ps)
00148 :
00149 generator_(artdaq::makeFragmentGenerator(generator, ps))
00150 , numFragIDs_(generator_->fragmentIDs().size())
00151 , frags_()
00152 {
00153 assert(generator_);
00154 }
00155
00156 bool
00157 ThrottledGenerator::
00158 getNext(artdaq::FragmentPtrs& newFrags)
00159 {
00160 if (frags_.size() && frags_.begin()->second.size())
00161 {
00162 for (auto& fQp : frags_)
00163 {
00164 assert(fQp.second.size());
00165 newFrags.emplace_back(std::move(fQp.second.front()));
00166 fQp.second.pop_front();
00167 }
00168 }
00169 else
00170 {
00171 return generateFragments_() && getNext(newFrags);
00172 }
00173 return true;
00174 }
00175
00176 bool
00177 ThrottledGenerator::
00178 generateFragments_()
00179 {
00180 artdaq::FragmentPtrs incomingFrags;
00181 bool result{ false };
00182 while ((result = generator_->getNext(incomingFrags)) &&
00183 incomingFrags.empty()) {
00184 }
00185 for (auto&& frag : incomingFrags)
00186 {
00187 frags_[frag->fragmentID()].emplace_back(std::move(frag));
00188 }
00189 return result;
00190 }
00191
00192 size_t
00193 ThrottledGenerator::
00194 numFragIDs() const
00195 {
00196 return numFragIDs_;
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00223 int process_data(fhicl::ParameterSet const& pset)
00224 {
00225 auto const gta_pset = pset.get<fhicl::ParameterSet>("genToArt");
00226
00227
00228 std::vector<ThrottledGenerator> generators;
00229
00230 auto const fr_pset = gta_pset.get<std::vector<fhicl::ParameterSet>>("fragment_receivers");
00231 for (auto const& gen_ps : fr_pset)
00232 {
00233 generators.emplace_back(gen_ps.get<std::string>("generator"),
00234 gen_ps);
00235 }
00236
00237 artdaq::FragmentPtrs frags;
00238 auto eb_pset = gta_pset.get<fhicl::ParameterSet>("event_builder", {});
00239 size_t expected_frags_per_event = 0;
00240 for (auto& gen : generators)
00241 {
00242 gen.start(1000, 0, 0);
00243 expected_frags_per_event += gen.numFragIDs();
00244 }
00245 eb_pset.put_or_replace<size_t>("expected_fragments_per_event", expected_frags_per_event);
00246
00247 artdaq::SharedMemoryEventManager store(eb_pset, pset);
00248 store.startRun(gta_pset.get<int>("run_number", 1000));
00249
00250 auto const events_to_generate =
00251 gta_pset.get<artdaq::Fragment::sequence_id_t>("events_to_generate", -1);
00252 auto const reset_sequenceID = pset.get<bool>("reset_sequenceID", true);
00253 bool done = false;
00254 for (artdaq::Fragment::sequence_id_t event_count = 1;
00255 (events_to_generate == static_cast<decltype(events_to_generate)>(-1)
00256 || event_count <= events_to_generate) && (!done);
00257 ++event_count)
00258 {
00259 for (auto& gen : generators)
00260 {
00261 done |= !gen.getNext(frags);
00262 }
00263 TLOG(TLVL_TRACE) << "There are " << std::to_string(frags.size()) << " Fragments in event " << std::to_string(event_count) << "." ;
00264 artdaq::Fragment::sequence_id_t current_sequence_id = -1;
00265 for (auto& val : frags)
00266 {
00267 if (reset_sequenceID)
00268 {
00269 TLOG(TLVL_DEBUG) << "Setting fragment sequence id to " << std::to_string(event_count) ;
00270 val->setSequenceID(event_count);
00271 }
00272 if (current_sequence_id ==
00273 static_cast<artdaq::Fragment::sequence_id_t>(-1))
00274 {
00275 current_sequence_id = val->sequenceID();
00276 }
00277 else if (val->sequenceID() != current_sequence_id)
00278 {
00279 throw art::Exception(art::errors::DataCorruption)
00280 << "Data corruption: apparently related fragments have "
00281 << " different sequence IDs: "
00282 << val->sequenceID()
00283 << " and "
00284 << current_sequence_id
00285 << ".\n";
00286 }
00287 artdaq::FragmentPtr tempFrag;
00288 auto sts = store.AddFragment(std::move(val), 1000000, tempFrag);
00289 if (!sts)
00290 {
00291 TLOG(TLVL_ERROR) << "Fragment was not added after 1s. Check art thread status!" ;
00292 store.endOfData();
00293 exit(1);
00294 }
00295 }
00296 frags.clear();
00297 TLOG(TLVL_TRACE) << "Event " << std::to_string(event_count) << " END" ;
00298 }
00299 for (auto& gen : generators)
00300 {
00301 gen.stop(0, 0);
00302 }
00303
00304 bool endSucceeded = store.endOfData();
00305 if (endSucceeded)
00306 {
00307 return 0;
00308 }
00309 else
00310 {
00311 return 15;
00312 }
00313 }
00314 }
00315
00316 int main(int argc, char* argv[]) try
00317 {
00318 artdaq::configureMessageFacility("genToArt");
00319
00320 bpo::variables_map vm;
00321 auto result = process_cmd_line(argc, argv, vm);
00322 if (result != 0)
00323 {
00324 return (result);
00325 }
00326
00327 fhicl::ParameterSet pset;
00328 if (getenv("FHICL_FILE_PATH") == nullptr)
00329 {
00330 TLOG(TLVL_ERROR)
00331 << "INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
00332 setenv("FHICL_FILE_PATH", ".", 0);
00333 }
00334 artdaq::SimpleLookupPolicy lookup_policy("FHICL_FILE_PATH");
00335 make_ParameterSet(vm["config"].as<std::string>(), lookup_policy, pset);
00336 return process_data(pset);
00337 }
00338 catch (std::string& x)
00339 {
00340 TLOG(TLVL_ERROR) << "Exception (type string) caught in genToArt: " << x << '\n';
00341 return 1;
00342 }
00343 catch (char const* m)
00344 {
00345 TLOG(TLVL_ERROR) << "Exception (type char const*) caught in genToArt: ";
00346 if (m)
00347 {
00348 TLOG(TLVL_ERROR) << m;
00349 }
00350 else
00351 {
00352 TLOG(TLVL_ERROR) << "[the value was a null pointer, so no message is available]";
00353 }
00354 }