FileDumperOutput_module.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // FileDumperOutput.cc: "dump contents of a file"
4 //
5 // Proposed output format (Feature #941):
6 // Process Name | Module Label | Process Label | Data Product type | (ProductID
7 // |) size
8 //
9 // ======================================================================
10 
18 #include "cetlib/column_width.h"
19 #include "cetlib/lpad.h"
20 #include "cetlib/rpad.h"
21 #include "fhiclcpp/ParameterSet.h"
23 
24 #include <algorithm>
25 #include <iomanip>
26 #include <iostream>
27 #include <string>
28 #include <vector>
29 
30 namespace art {
31  namespace detail {
32  struct ProductInfo {
39  };
40  } // namespace detail
41 } // namespace art
42 
43 namespace {
44 
46  product_size(art::EDProduct const* product, bool const isPresent)
47  {
48  return isPresent ? product->productSize() : "?";
49  }
50 
52  dummyProcess()
53  {
54  return "PROCESS NAME";
55  }
56 
57  auto
58  dummyInfo()
59  {
60  return art::detail::ProductInfo{"MODULE LABEL",
61  "PRODUCT INSTANCE NAME",
62  "DATA PRODUCT TYPE",
63  "PRODUCT FRIENDLY TYPE",
64  "PRODUCT ID",
65  "SIZE"};
66  }
67 
68  using ProductInfos = std::vector<art::detail::ProductInfo>;
69  std::size_t
70  columnWidthFirst(std::map<std::string, ProductInfos> const& m,
71  std::string const& title)
72  {
73  std::size_t i{title.size()};
75  m, [&i](auto const& entry) { i = std::max(i, entry.first.size()); });
76  return i;
77  }
78 
79  std::size_t
80  columnWidth(std::map<std::string, ProductInfos> const& m,
82  std::string const& title)
83  {
84  std::size_t i{title.size()};
85  for (auto const& entry : m) {
86  for (auto const& pi : entry.second) {
87  i = std::max(i, (pi.*pim).size());
88  }
89  }
90  return i;
91  }
92 
93 } // namespace
94 
95 namespace art {
96  class FileDumperOutput;
97 }
98 
100 
102 public:
103  struct Config {
105  fhicl::Atom<bool> wantProductFullClassName{
106  fhicl::Name("wantProductFullClassName"),
107  true};
108  fhicl::Atom<bool> wantProductFriendlyClassName{
109  fhicl::Name("wantProductFriendlyClassName"),
110  wantProductFullClassName()};
111  fhicl::Atom<bool> wantProductID{fhicl::Name{"wantProductID"}, false};
112  fhicl::Atom<bool> resolveProducts{fhicl::Name("resolveProducts"), true};
113  fhicl::Atom<bool> onlyIfPresent{fhicl::Name("onlyIfPresent"), false};
114  };
115 
116  using Parameters =
118 
119  explicit FileDumperOutput(Parameters const&);
120 
121 private:
122  void write(EventPrincipal& e) override;
123  void writeRun(RunPrincipal& r) override;
124  void writeSubRun(SubRunPrincipal& sr) override;
125  void readResults(ResultsPrincipal const& resp) override;
126 
127  template <typename P>
128  void printPrincipal(P const& p);
129 
130  void printProductInfo(std::vector<std::size_t> const& columnWidths,
131  std::string const& processName,
132  detail::ProductInfo const& pi) const;
133 
136  bool const wantProductID_;
138  bool const wantPresentOnly_;
139 }; // FileDumperOutput
140 
142  : OutputModule{ps().omConfig, ps.get_PSet()}
143  , wantProductFullClassName_{ps().wantProductFullClassName()}
144  , wantProductFriendlyClassName_{ps().wantProductFriendlyClassName()}
145  , wantProductID_{ps().wantProductID()}
146  , wantResolveProducts_{ps().resolveProducts()}
147  , wantPresentOnly_{ps().onlyIfPresent()}
148 {}
149 
150 void
152 {
153  printPrincipal(e);
154 }
155 
156 void
158 {
159  printPrincipal(r);
160 }
161 
162 void
164 {
165  printPrincipal(sr);
166 }
167 
168 void
170 {
171  printPrincipal(resp);
172 }
173 
174 template <typename P>
175 void
177 {
178  if (!p.size())
179  return;
180 
181  size_t present{0};
182  size_t not_present{0};
183  std::map<std::string, std::vector<detail::ProductInfo>> products;
184 
185  auto const& dinfo = dummyInfo();
186 
187  products[dummyProcess()].emplace_back(dinfo);
188 
189  for (auto const& pr : p) {
190  auto const& g = *pr.second;
191  auto const& pd = g.productDescription();
192  auto const& oh = p.getForOutput(pd.productID(), wantResolveProducts_);
193 
194  EDProduct const* product = oh.isValid() ? oh.wrapper() : nullptr;
195  bool const productPresent = product != nullptr && product->isPresent();
196 
197  if (productPresent) {
198  ++present;
199  } else {
200  ++not_present;
201  }
202 
203  if (!wantPresentOnly_ || productPresent) {
204  auto pi = detail::ProductInfo{pd.moduleLabel(),
205  pd.productInstanceName(),
206  pd.producedClassName(),
207  pd.friendlyClassName(),
208  std::to_string(pd.productID().value()),
209  product_size(product, productPresent)};
210  products[pd.processName()].emplace_back(std::move(pi));
211  }
212  }
213 
214  std::cout << "PRINCIPAL TYPE: " << BranchTypeToString(p.branchType())
215  << std::endl;
216 
217  std::vector<std::size_t> const widths{
218  columnWidthFirst(products, dummyProcess()),
219  columnWidth(
220  products, &detail::ProductInfo::module_label, dinfo.module_label),
221  columnWidth(
222  products, &detail::ProductInfo::instance_name, dinfo.instance_name),
223  columnWidth(
224  products, &detail::ProductInfo::product_type, dinfo.product_type),
225  columnWidth(
226  products, &detail::ProductInfo::friendly_type, dinfo.friendly_type),
227  columnWidth(products, &detail::ProductInfo::product_id, dinfo.product_id),
228  columnWidth(products, &detail::ProductInfo::str_size, dinfo.str_size)};
229 
230  // Print banner
231  printProductInfo(widths, dummyProcess(), dummyInfo());
232  for (auto const& processConfig : p.processHistory()) {
233  auto const& processName = processConfig.processName();
234  for (auto const& pi : products[processName]) {
235  printProductInfo(widths, processName, pi);
236  }
237  }
238 
239  std::cout << "\nTotal products (present, not present): "
240  << present + not_present << " (" << present << ", " << not_present
241  << ").\n\n";
242 }
243 
244 void
245 art::FileDumperOutput::printProductInfo(std::vector<std::size_t> const& widths,
246  std::string const& processName,
247  detail::ProductInfo const& pi) const
248 {
249  std::ostringstream oss;
250  oss << cet::rpad(processName, widths[0], '.') << " | "
251  << cet::rpad(pi.module_label, widths[1], '.') << " | "
252  << cet::rpad(pi.instance_name, widths[2], '.') << " | ";
254  oss << cet::rpad(pi.product_type, widths[3], '.') << " | ";
255  }
257  oss << cet::rpad(pi.friendly_type, widths[4], '.') << " | ";
258  }
259  if (wantProductID_) {
260  oss << cet::rpad(pi.product_id, widths[5], '.') << " | ";
261  }
262  oss << cet::lpad(pi.str_size, widths[6], '.');
263  std::cout << oss.str() << '\n';
264 }
265 
T max(const caf::Proxy< T > &a, T b)
void write(EventPrincipal &e) override
auto const & get_PSet() const
std::size_t columnWidth(T const &coll, std::string const Elem::*cp, std::string const &header)
const char * p
Definition: xmltok.h:285
std::string rpad(std::string const &pad_me, std::string::size_type wanted_size, char char_to_pad_with= ' ')
std::string lpad(std::string const &pad_me, std::string::size_type wanted_size, char char_to_pad_with= ' ')
FileDumperOutput(Parameters const &)
DEFINE_ART_MODULE(TestTMapFile)
write
Run ND cosmics.
#define P(a, b, c, d, e, x)
void printProductInfo(std::vector< std::size_t > const &columnWidths, std::string const &processName, detail::ProductInfo const &pi) const
caf::StandardRecord * sr
OStream cout
Definition: OStream.cxx:6
void writeRun(RunPrincipal &r) override
std::string const & processName() const
void readResults(ResultsPrincipal const &resp) override
fhicl::TableFragment< OutputModule::Config > omConfig
std::string const & BranchTypeToString(BranchType)
virtual std::string productSize() const
Definition: EDProduct.h:57
auto for_all(FwdCont &, Func)
TRandom3 r(0)
Service to store calibration data products (CDP) in the SQLite3 metadatabase of a file...
Definition: FillParentInfo.h:8
bool isPresent() const
Definition: EDProduct.h:30
void writeSubRun(SubRunPrincipal &sr) override
Float_t e
Definition: plot.C:35
def entry(str)
Definition: HTMLTools.py:26
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
constexpr ProductStatus present() noexcept
Definition: ProductStatus.h:10
enum BeamMode string