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 
17 #include "cetlib/lpad.h"
18 #include "cetlib/rpad.h"
20 
21 #include <algorithm>
22 #include <iostream>
23 #include <string>
24 #include <vector>
25 
26 namespace art::detail {
27  struct ProductInfo {
34  };
35 } // namespace art::detail
36 
37 namespace {
38 
40  product_size(art::EDProduct const* product, bool const isPresent)
41  {
42  return isPresent ? product->productSize() : "?";
43  }
44 
46  dummyProcess()
47  {
48  return "PROCESS NAME";
49  }
50 
51  auto
52  dummyInfo()
53  {
54  return art::detail::ProductInfo{"MODULE LABEL",
55  "PRODUCT INSTANCE NAME",
56  "DATA PRODUCT TYPE",
57  "PRODUCT FRIENDLY TYPE",
58  "PRODUCT ID",
59  "SIZE"};
60  }
61 
62  using ProductInfos = std::vector<art::detail::ProductInfo>;
63  std::size_t
64  columnWidthFirst(std::map<std::string, ProductInfos> const& m,
65  std::string const& title)
66  {
67  std::size_t i{title.size()};
69  m, [&i](auto const& entry) { i = std::max(i, entry.first.size()); });
70  return i;
71  }
72 
73  std::size_t
74  columnWidth(std::map<std::string, ProductInfos> const& m,
76  std::string const& title)
77  {
78  std::size_t i{title.size()};
79  for (auto const& entry : m) {
80  for (auto const& pi : entry.second) {
81  i = std::max(i, (pi.*pim).size());
82  }
83  }
84  return i;
85  }
86 
87 } // namespace
88 
89 namespace art {
90  class FileDumperOutput;
91 }
92 
94 public:
95  struct Config {
97  fhicl::Atom<bool> wantProductFullClassName{
98  fhicl::Name("wantProductFullClassName"),
99  true};
100  fhicl::Atom<bool> wantProductFriendlyClassName{
101  fhicl::Name("wantProductFriendlyClassName"),
102  wantProductFullClassName()};
103  fhicl::Atom<bool> wantProductID{fhicl::Name{"wantProductID"}, false};
104  fhicl::Atom<bool> resolveProducts{fhicl::Name("resolveProducts"), true};
105  fhicl::Atom<bool> onlyIfPresent{fhicl::Name("onlyIfPresent"), false};
106  };
107 
108  using Parameters =
110 
111  explicit FileDumperOutput(Parameters const&);
112 
113 private:
114  void write(EventPrincipal& e) override;
115  void writeRun(RunPrincipal& r) override;
116  void writeSubRun(SubRunPrincipal& sr) override;
117  void readResults(ResultsPrincipal const& resp) override;
118 
119  template <typename P>
120  void printPrincipal(P const& p);
121 
122  void printProductInfo(std::vector<std::size_t> const& columnWidths,
123  std::string const& processName,
124  detail::ProductInfo const& pi) const;
125 
128  bool const wantProductID_;
130  bool const wantPresentOnly_;
131 }; // FileDumperOutput
132 
134  : OutputModule{ps().omConfig, ps.get_PSet()}
135  , wantProductFullClassName_{ps().wantProductFullClassName()}
136  , wantProductFriendlyClassName_{ps().wantProductFriendlyClassName()}
137  , wantProductID_{ps().wantProductID()}
138  , wantResolveProducts_{ps().resolveProducts()}
139  , wantPresentOnly_{ps().onlyIfPresent()}
140 {}
141 
142 void
144 {
145  printPrincipal(e);
146 }
147 
148 void
150 {
151  printPrincipal(r);
152 }
153 
154 void
156 {
157  printPrincipal(sr);
158 }
159 
160 void
162 {
163  printPrincipal(resp);
164 }
165 
166 template <typename P>
167 void
169 {
170  if (!p.size())
171  return;
172 
173  size_t present{0};
174  size_t not_present{0};
175  std::map<std::string, std::vector<detail::ProductInfo>> products;
176 
177  auto const& dinfo = dummyInfo();
178 
179  products[dummyProcess()].emplace_back(dinfo);
180 
181  for (auto const& pr : p) {
182  auto const& g = *pr.second;
183  auto const& pd = g.productDescription();
184  auto const& oh = p.getForOutput(pd.productID(), wantResolveProducts_);
185 
186  EDProduct const* product = oh.isValid() ? oh.wrapper() : nullptr;
187  bool const productPresent = product != nullptr && product->isPresent();
188 
189  if (productPresent) {
190  ++present;
191  } else {
192  ++not_present;
193  }
194 
195  if (!wantPresentOnly_ || productPresent) {
196  auto pi = detail::ProductInfo{pd.moduleLabel(),
197  pd.productInstanceName(),
198  pd.producedClassName(),
199  pd.friendlyClassName(),
200  std::to_string(pd.productID().value()),
201  product_size(product, productPresent)};
202  products[pd.processName()].emplace_back(std::move(pi));
203  }
204  }
205 
206  std::cout << "PRINCIPAL TYPE: " << BranchTypeToString(p.branchType())
207  << std::endl;
208 
209  std::vector<std::size_t> const widths{
210  columnWidthFirst(products, dummyProcess()),
211  columnWidth(
212  products, &detail::ProductInfo::module_label, dinfo.module_label),
213  columnWidth(
214  products, &detail::ProductInfo::instance_name, dinfo.instance_name),
215  columnWidth(
216  products, &detail::ProductInfo::product_type, dinfo.product_type),
217  columnWidth(
218  products, &detail::ProductInfo::friendly_type, dinfo.friendly_type),
219  columnWidth(products, &detail::ProductInfo::product_id, dinfo.product_id),
220  columnWidth(products, &detail::ProductInfo::str_size, dinfo.str_size)};
221 
222  // Print banner
223  printProductInfo(widths, dummyProcess(), dummyInfo());
224  for (auto const& processConfig : p.processHistory()) {
225  auto const& processName = processConfig.processName();
226  for (auto const& pi : products[processName]) {
227  printProductInfo(widths, processName, pi);
228  }
229  }
230 
231  std::cout << "\nTotal products (present, not present): "
232  << present + not_present << " (" << present << ", " << not_present
233  << ").\n\n";
234 }
235 
236 void
237 art::FileDumperOutput::printProductInfo(std::vector<std::size_t> const& widths,
238  std::string const& processName,
239  detail::ProductInfo const& pi) const
240 {
241  std::ostringstream oss;
242  oss << cet::rpad(processName, widths[0], '.') << " | "
243  << cet::rpad(pi.module_label, widths[1], '.') << " | "
244  << cet::rpad(pi.instance_name, widths[2], '.') << " | ";
246  oss << cet::rpad(pi.product_type, widths[3], '.') << " | ";
247  }
249  oss << cet::rpad(pi.friendly_type, widths[4], '.') << " | ";
250  }
251  if (wantProductID_) {
252  oss << cet::rpad(pi.product_id, widths[5], '.') << " | ";
253  }
254  oss << cet::lpad(pi.str_size, widths[6], '.');
255  std::cout << oss.str() << '\n';
256 }
257 
std::string const & processName() const
Definition: Observer.cc:68
QList< Entry > entry
static constexpr double g
Definition: Units.h:144
void write(EventPrincipal &e) override
std::string string
Definition: nybbler.cc:12
auto const & get_PSet() const
std::size_t columnWidth(T const &coll, std::string const Elem::*cp, std::string const &header)
ChannelGroupService::Name Name
std::string rpad(std::string const &pad_me, std::string::size_type wanted_size, char char_to_pad_with= ' ')
Definition: rpad.cc:10
std::string lpad(std::string const &pad_me, std::string::size_type wanted_size, char char_to_pad_with= ' ')
Definition: lpad.cc:10
FileDumperOutput(Parameters const &)
std::pair< float, std::string > P
size_t write(int, const char *, size_t)
Writes count bytes from buf to the filedescriptor fd.
QCollection::Item first()
Definition: qglist.cpp:807
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
void printProductInfo(std::vector< std::size_t > const &columnWidths, std::string const &processName, detail::ProductInfo const &pi) const
def move(depos, offset)
Definition: depos.py:107
p
Definition: test.py:223
static int max(int a, int b)
static constexpr double ps
Definition: Units.h:99
std::string const & BranchTypeToString(BranchType const bt)
Definition: BranchType.cc:65
void writeRun(RunPrincipal &r) override
void readResults(ResultsPrincipal const &resp) override
fhicl::TableFragment< OutputModule::Config > omConfig
virtual std::string productSize() const
Definition: EDProduct.h:58
auto for_all(FwdCont &, Func)
bool isPresent() const
Definition: EDProduct.h:31
void writeSubRun(SubRunPrincipal &sr) override
float pi
Definition: units.py:11
static constexpr double sr
Definition: Units.h:166
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
constexpr ProductStatus present() noexcept
Definition: ProductStatus.h:10
QTextStream & endl(QTextStream &s)