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