artdaq  v3_10_02
CompositeDriver_generator.cc
1 #define TRACE_NAME "CompositeDriver"
2 
3 #include "artdaq/Generators/CompositeDriver.hh"
4 
5 #include <boost/algorithm/string.hpp>
6 #include "artdaq/Generators/GeneratorMacros.hh"
7 #include "artdaq/Generators/makeCommandableFragmentGenerator.hh"
8 #include "canvas/Utilities/Exception.h"
9 #include "cetlib_except/exception.h"
10 
11 artdaq::CompositeDriver::CompositeDriver(const fhicl::ParameterSet& ps)
13 {
14  auto psetList =
15  ps.get<std::vector<fhicl::ParameterSet>>("generator_config_list");
16  for (const auto& pset : psetList)
17  {
18  if (!makeChildGenerator_(pset))
19  {
20  throw cet::exception("CompositeDriver") // NOLINT(cert-err60-cpp)
21  << "Unable to create child generator for PSet= \""
22  << pset.to_string() << "\"";
23  }
24  }
25 
26  std::string compositeMetricName;
27  for (auto& generator : generator_list_)
28  {
29  if (compositeMetricName.length() > 0) { compositeMetricName.append(", "); }
30  compositeMetricName.append(generator->metricsReportingInstanceName());
31  }
32  this->metricsReportingInstanceName(compositeMetricName);
33 }
34 
36 {
37  // 15-Feb-2014, KAB - explicitly destruct the generators so that
38  // we can control the order in which they are destructed
39  size_t listSize = generator_list_.size();
40  while (listSize > 0)
41  {
42  --listSize;
43  try
44  {
45  generator_list_.resize(listSize);
46  }
47  catch (...)
48  {
49  TLOG(TLVL_ERROR)
50  << "Unknown exception when destructing the generator at index "
51  << (listSize + 1);
52  }
53  }
54 }
55 
57 {
58  for (auto&& idx : generator_active_list_)
59  {
60  idx = true;
61  }
62  for (auto& generator : generator_list_)
63  {
64  generator->StartCmd(run_number(), timeout(), timestamp());
65  }
66 }
67 
69 {
70  std::vector<std::unique_ptr<CommandableFragmentGenerator>>::reverse_iterator riter;
71  for (riter = generator_list_.rbegin(); riter != generator_list_.rend(); ++riter)
72  {
73  (*riter)->StopCmd(timeout(), timestamp());
74  }
75 }
76 
78 {
79  // 02/17/16 ELF: This logic is now handled in stopNoMutex
80  /*
81  std::vector<std::unique_ptr<CommandableFragmentGenerator>>::reverse_iterator riter;
82  for (riter = generator_list_.rbegin(); riter != generator_list_.rend(); ++riter) {
83  (*riter)->StopCmd( timeout(), timestamp() );
84  }
85  */
86 }
87 
89 {
90  std::vector<std::unique_ptr<CommandableFragmentGenerator>>::reverse_iterator riter;
91  for (riter = generator_list_.rbegin(); riter != generator_list_.rend(); ++riter)
92  {
93  (*riter)->PauseCmd(timeout(), timestamp());
94  }
95 }
96 
98 {
99  for (auto&& idx : generator_active_list_)
100  {
101  idx = true;
102  }
103  for (auto& generator : generator_list_)
104  {
105  generator->ResumeCmd(timeout(), timestamp());
106  }
107 }
108 
109 std::vector<artdaq::Fragment::fragment_id_t> artdaq::CompositeDriver::fragmentIDs()
110 {
111  std::vector<artdaq::Fragment::fragment_id_t> workList;
112  for (auto& idx : generator_list_)
113  {
114  std::vector<artdaq::Fragment::fragment_id_t> tempList =
115  idx->fragmentIDs();
116  workList.insert(workList.end(), tempList.begin(), tempList.end());
117  }
118  return workList;
119 }
120 
121 bool artdaq::CompositeDriver::getNext_(artdaq::FragmentPtrs& frags)
122 {
123  bool anyGeneratorIsActive = false;
124  for (size_t idx = 0; idx < generator_list_.size(); ++idx)
125  {
126  if (generator_active_list_[idx])
127  {
128  bool status = generator_list_[idx]->getNext(frags);
129 
130  // 08-May-2015, KAB & JCF: if the generator getNext() method returns
131  // false (which indicates that the data flow has stopped) *and* the
132  // reason that it has stopped is because there was an exception that
133  // wasn't handled by the experiment-specific FragmentGenerator class,
134  // we throw an exception so that the process will move to the
135  // InRunError state. We do our best to try to reproduce the original
136  // exception message.
137  if (!status && generator_list_[idx]->exception())
138  {
139  std::string reportString =
140  generator_list_[idx]->ReportCmd("latest_exception");
141  if (std::string::npos !=
142  boost::algorithm::to_lower_copy(reportString).find("exception"))
143  {
144  throw cet::exception("CompositeDriver_generator") // NOLINT(cert-err60-cpp)
145  << "The FragmentGenerator for "
146  << generator_list_[idx]->metricsReportingInstanceName()
147  << " threw an exception: " << reportString;
148  }
149 
150  throw cet::exception("CompositeDriver_generator") // NOLINT(cert-err60-cpp)
151  << "The FragmentGenerator for "
152  << generator_list_[idx]->metricsReportingInstanceName()
153  << " threw an exception.";
154  }
155  generator_active_list_[idx] = status;
156  if (status) { anyGeneratorIsActive = true; }
157  }
158  }
159  return anyGeneratorIsActive;
160 }
161 
162 bool artdaq::CompositeDriver::makeChildGenerator_(fhicl::ParameterSet const& pset)
163 {
164  // pull out the relevant parts of the ParameterSet, if needed
165  auto daq_pset = pset.get<fhicl::ParameterSet>("daq", pset);
166  auto fr_pset = daq_pset.get<fhicl::ParameterSet>("fragment_receiver", daq_pset);
167 
168  // create the requested FragmentGenerator
169  auto frag_gen_name = fr_pset.get<std::string>("generator", "");
170  if (frag_gen_name.length() == 0)
171  {
172  TLOG(TLVL_ERROR)
173  << "No fragment generator (parameter name = \"generator\") was "
174  << "specified in the fragment_receiver ParameterSet. The "
175  << "DAQ initialization PSet was \"" << daq_pset.to_string() << "\".";
176  return false;
177  }
178 
179  std::unique_ptr<artdaq::CommandableFragmentGenerator> tmp_gen_ptr;
180  try
181  {
182  tmp_gen_ptr = artdaq::makeCommandableFragmentGenerator(frag_gen_name, fr_pset);
183  }
184  catch (art::Exception& excpt)
185  {
186  TLOG(TLVL_ERROR)
187  << "Exception creating a FragmentGenerator of type \""
188  << frag_gen_name << "\" with parameter set \"" << fr_pset.to_string()
189  << "\", exception = " << excpt;
190  return false;
191  }
192  catch (cet::exception& excpt)
193  {
194  TLOG(TLVL_ERROR)
195  << "Exception creating a FragmentGenerator of type \""
196  << frag_gen_name << "\" with parameter set \"" << fr_pset.to_string()
197  << "\", exception = " << excpt;
198  return false;
199  }
200  catch (...)
201  {
202  TLOG(TLVL_ERROR)
203  << "Unknown exception creating a FragmentGenerator of type \""
204  << frag_gen_name << "\" with parameter set \"" << fr_pset.to_string()
205  << "\".";
206  return false;
207  }
208 
209  std::unique_ptr<CommandableFragmentGenerator> generator_ptr;
210  generator_ptr.reset(nullptr);
211  try
212  {
213  auto tmp_cmdablegen_bareptr = tmp_gen_ptr.release();
214  if (tmp_cmdablegen_bareptr != nullptr)
215  {
216  generator_ptr.reset(tmp_cmdablegen_bareptr);
217  }
218  }
219  catch (...)
220  {}
221  if (!generator_ptr)
222  {
223  TLOG(TLVL_ERROR)
224  << "Error: The requested fragment generator type (" << frag_gen_name
225  << ") is not a CommandableFragmentGenerator, and only "
226  << "CommandableFragmentGenerators are currently supported.";
227  return false;
228  }
229 
230  generator_list_.push_back(std::move(generator_ptr));
231  generator_active_list_.push_back(true);
232  return true;
233 }
234 
235 DEFINE_ARTDAQ_COMMANDABLE_GENERATOR(artdaq::CompositeDriver)
void start() override
Start all configured CommandableFragmentGenerators.
virtual ~CompositeDriver() noexcept
Destructor. Calls the destructors for each configured CommandableFragmentGenerator.
CompositeDriver handles a set of lower-level generators.
void pause() override
Pause all configured CommandableFragmentGenerators.
std::unique_ptr< CommandableFragmentGenerator > makeCommandableFragmentGenerator(std::string const &generator_plugin_spec, fhicl::ParameterSet const &ps)
Load a CommandableFragmentGenerator plugin.
virtual std::string metricsReportingInstanceName() const
Get the name used when reporting metrics.
void stop() override
Call stop methods for all configured CommandableFragmentGenerators. Currently handled by stopNoMutex...
CommandableFragmentGenerator is a FragmentGenerator-derived abstract class that defines the interface...
CompositeDriver(fhicl::ParameterSet const &ps)
CompositeDriver Constructor.
void resume() override
Resume all configured CommandableFragmentGenerators.
void stopNoMutex() override
Call non-locked stop methods for all configured CommandableFragmentGenerators.