LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
POTaccumulator_module.cc
Go to the documentation of this file.
1 
8 // LArSoft libraries
10 
11 // framework libraries
18 #include "fhiclcpp/types/Atom.h"
20 #include "range/v3/view.hpp"
21 
22 // C/C++ standard libraries
23 #include <map>
24 #include <string>
25 
26 // -----------------------------------------------------------------------------
27 namespace sim {
28  class POTaccumulator;
29 }
30 
72 public:
74  struct Config {
75  using Name = fhicl::Name;
77 
79  Name("SummaryTag"),
80  Comment("data product (subrun level) with the summary information"),
81  "generator"};
82 
84  Name("SummaryCategory"),
85  Comment("name of the output category the summary is sent to"),
86  "POTaccumulator" // default value
87  };
88 
90  Name("RunSummaryCategory"),
91  Comment("name of the output category the summary is sent to"),
92  "" // default value
93  };
94 
95  }; // struct Config
96 
99 
101  explicit POTaccumulator(Parameters const& config);
102 
103  // Plugins should not be copied or assigned.
104  POTaccumulator(POTaccumulator const&) = delete;
105  POTaccumulator(POTaccumulator&&) = delete;
106  POTaccumulator& operator=(POTaccumulator const&) = delete;
108 
109  // Nothing to be done at event level.
110  virtual void analyze(art::Event const& event) override {}
111 
113  virtual void endSubRun(art::SubRun const& subRun) override;
114 
116  virtual void endJob() override;
117 
118 private:
119  // -- BEGIN -- Configuration variables ---------------------------------------
120 
123  std::string fRunOutputCategory;
124 
125  // -- END -- Configuration variables -----------------------------------------
126 
127  // -- BEGIN -- Internal cache variables --------------------------------------
128 
130  std::map<art::SubRunID, unsigned int> fPresentSubrunFragments;
131 
133  std::map<art::SubRunID, unsigned int> fMissingSubrunFragments;
134 
136  std::map<art::RunID, art::SummedValue<sumdata::POTSummary>> fRunPOT;
137 
138  // -- END -- Internal cache variables ----------------------------------------
139 
141  void printMissingSubrunList() const;
142 
144  void printRunSummary() const;
145 
147  void printSummary(sumdata::POTSummary const& totalPOT) const;
148 
150  static std::string to_string(sumdata::POTSummary const& POT);
151 
152 }; // class sim::POTaccumulator
153 
154 //------------------------------------------------------------------------------
155 //--- module implementation
156 //---
157 //------------------------------------------------------------------------------
159  : EDAnalyzer(config)
160  , fPOTtag(config().SummaryTag())
163 {}
164 
165 //------------------------------------------------------------------------------
167 {
168 
169  auto const& ID = subRun.id();
170 
171  //
172  // get the information from the subrun and update the subrun counts
173  //
174  art::Handle<sumdata::POTSummary> summaryHandle;
175  if (!subRun.getByLabel(fPOTtag, summaryHandle)) {
178  << "Fragment of subrun " << ID << " has no '" << fPOTtag.encode() << "' POT summary.";
179  return;
180  }
181 
183 
184  //
185  // accumulate the information by run
186  //
187  sumdata::POTSummary const& subRunPOT = *summaryHandle;
188 
189  fRunPOT[ID.runID()].update(summaryHandle);
191  << "Fragment #" << fPresentSubrunFragments[ID] << " of subrun " << ID << ": "
192  << sim::POTaccumulator::to_string(subRunPOT);
193 
194 } // sim::POTaccumulator::endSubRun()
195 
196 //------------------------------------------------------------------------------
198 {
199 
200  //
201  // print the run summary
202  //
203 
204  if (!fRunOutputCategory.empty()) {
205 
207 
208  printRunSummary();
209 
210  } // if
211 
212  //
213  // print the total summary
214  //
215 
216  // here we skip _art_ aggregation mechanism
217  // because it can't handle multiple runs
218  sumdata::POTSummary totalPOT;
219  for (auto const& POT : fRunPOT | ranges::views::values)
220  totalPOT.aggregate(POT.value());
221 
222  printSummary(totalPOT);
223 
224 } // sim::POTaccumulator::endJob()
225 
226 //------------------------------------------------------------------------------
228 {
229 
230  //
231  // missing fragments information
232  //
234  log << size(fMissingSubrunFragments) << " subruns lack POT information:";
235 
236  auto const fend = fPresentSubrunFragments.cend();
237 
238  for (auto const& [id, nMissing] : fMissingSubrunFragments) {
239 
240  // add to the count of fragments the ones which we have actually found
241  unsigned int nFragments = nMissing;
242  auto const iFound = fPresentSubrunFragments.find(id);
243  if (iFound != fend) nFragments += iFound->second;
244 
245  log << "\n" << id << ": " << nMissing << " / " << nFragments << " \"fragments\"";
246 
247  } // for
248 
249 } // sim::POTaccumulator::printMissingSubrunList()
250 
251 //------------------------------------------------------------------------------
253 {
254 
255  // count subruns in run
256  std::map<art::RunID, unsigned int> subrunCount;
257  for (art::SubRunID const& ID : fPresentSubrunFragments | ranges::views::keys)
258  ++subrunCount[ID.runID()];
259 
261  log << "POT from " << size(fRunPOT) << " runs:";
262  for (auto const& [id, POT] : fRunPOT) {
263  log << "\n " << id << " (" << subrunCount[id]
264  << " subruns): " << sim::POTaccumulator::to_string(POT.value());
265  } // for
266 
267 } // sim::POTaccumulator::printRunSummary()
268 
269 //------------------------------------------------------------------------------
271 {
272 
273  // aggregate all run summaries
275  << "Aggregated POT from " << fRunPOT.size() << " runs (" << fPresentSubrunFragments.size()
276  << " subruns): " << sim::POTaccumulator::to_string(totalPOT);
277 
278 } // sim::POTaccumulator::printSummary()
279 
280 //------------------------------------------------------------------------------
282 {
283  using namespace std::string_literals;
284  return std::to_string(POT.totgoodpot) + " good POT ( "s + std::to_string(POT.goodspills) +
285  " spills); total: " + std::to_string(POT.totpot) + " POT ( "s +
286  std::to_string(POT.totspills) + " spills)";
287 } // sim::POTaccumulator::to_string(sumdata::POTSummary)
288 
289 //------------------------------------------------------------------------------
291 
292 //------------------------------------------------------------------------------
fhicl::Atom< art::InputTag > SummaryTag
POTaccumulator(Parameters const &config)
Configuration-checking constructor.
std::string fSummaryOutputCategory
Name of the main stream for output.
std::map< art::RunID, art::SummedValue< sumdata::POTSummary > > fRunPOT
Partial count of POT in the run, per run.
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.cc:6
std::string encode() const
Definition: InputTag.cc:97
fhicl::Atom< std::string > SummaryCategory
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
POTaccumulator & operator=(POTaccumulator const &)=delete
static std::string to_string(sumdata::POTSummary const &POT)
Converts the information from POT in a compact string.
virtual void endJob() override
Prints the general summary.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
fhicl::Atom< std::string > RunSummaryCategory
decltype(auto) constexpr to_string(T &&obj)
ADL-aware version of std::to_string.
Collection of configuration parameters for the module.
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
void printRunSummary() const
Prints the list of POT per run.
std::string fRunOutputCategory
Name of the run stream for output.
#define MF_LOG_TRACE(id)
void printMissingSubrunList() const
Prints the list of subruns with partial or missing POT information.
Monte Carlo Simulation.
Prints on console the total Protons On Target from the input subruns.
void aggregate(POTSummary const &other)
Definition: POTSummary.cxx:14
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
MaybeLogger_< ELseverityLevel::ELsev_success, false > LogDebug
virtual void analyze(art::Event const &event) override
SubRunID id() const
Definition: SubRun.cc:25
virtual void endSubRun(art::SubRun const &subRun) override
Collects information from each subrun.
std::map< art::SubRunID, unsigned int > fPresentSubrunFragments
Count of subrun fragments with POT information.
art::InputTag fPOTtag
Name of sumdata::POTSummary data product.
void printSummary(sumdata::POTSummary const &totalPOT) const
Prints the total POT summary totalPOT.
std::map< art::SubRunID, unsigned int > fMissingSubrunFragments
Count of subrun fragments without POT information.
Event finding and building.