LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
DataFlowDumper_module.cc
Go to the documentation of this file.
1 // DataFlowDumper
2 //
3 // DataFlowDumper is an output module that creates a printout of the
4 // "data flow" that produced an Event. This means it writes
5 // information naming each data product in the event, what module that
6 // product was created by, and what data products were read by that
7 // module (the 'parents' of the original data product).
8 
12 #include "fhiclcpp/types/Atom.h"
13 #include "fhiclcpp/types/Name.h"
14 
15 #include <algorithm>
16 #include <fstream>
17 #include <string>
18 #include <vector>
19 
20 namespace art {
21  // DataFlow is the class we define to provide our specialization of
22  // the ProvenanceDumper class template, to create the DataFlowDumper
23  // class.
24  class DataFlow;
26 } // namespace art
27 
29 public:
30  struct Config {
31  fhicl::Atom<std::string> dotfile{fhicl::Name("dotfile"), "flow.dot"};
34  };
35 
36  explicit DataFlow(fhicl::TableFragment<Config> const& cfg);
37 
38  // Prepare to write out the graph for an event.
39  void preProcessEvent();
40 
41  // Finalize writing the graph for an event.
42  void postProcessEvent();
43 
44  // Write the graph description lines for the data product associated
45  // with the given provenance.
46  void processEventProvenance(art::Provenance const& prov);
47 
48 private:
49  std::ofstream out_;
50  int nEvents_;
51  std::string colorscheme_;
52  int debug_;
53 };
54 
55 //-----------------------------------------------------------------
56 
58  : out_(cfg().dotfile())
59  , nEvents_(0)
60  , colorscheme_(cfg().colorscheme())
61  , debug_(cfg().debuglevel())
62 {
63  if (!out_) {
65  << "Failed to create output file: " << cfg().dotfile();
66  }
67 }
68 
69 void
71 {
72  out_ << "digraph d" << nEvents_ << " {\n";
73 }
74 
75 void
77 {
78  out_ << "}\n\n";
79  ++nEvents_;
80 }
81 
82 // Write the identifier for the node for the product to which this
83 // Provenance belongs.
84 void
85 write_id(art::ProductID const pid, std::ostream& os)
86 {
87  os << "\"b" << pid << '\"';
88 }
89 
90 void
91 write_id(art::Provenance const& p, std::ostream& os)
92 {
93  write_id(p.productID(), os);
94 }
95 
96 // format_product_node defines the format for the product nade.
97 void
98 format_product_node(std::string const& fcn,
99  std::string const& pin,
100  std::ostream& os)
101 {
102  os << " [label = \"" << fcn;
103  if (!pin.empty())
104  os << "/" << pin;
105  os << "\" shape = box];\n";
106 }
107 
108 // Write the line defining the node for the product to which this
109 // Provenance belongs.
110 void
111 write_product_node(art::Provenance const& p, std::ostream& os, int debug)
112 {
113  if (debug > 0) {
114  os << "# write_product_node for provenance: " << &p << '\n';
115  }
116  write_id(p, os);
118 }
119 
120 void
121 write_module_id(art::Provenance const& p, std::ostream& os)
122 {
123  os << '\"' << p.moduleLabel() << '/' << p.processName() << '\"';
124 }
125 
126 std::size_t
127 color(std::string const& procname)
128 {
129  static std::vector<std::string> names_seen;
130  auto it = std::find(begin(names_seen), end(names_seen), procname);
131  if (it == end(names_seen)) {
132  names_seen.push_back(procname);
133  return names_seen.size();
134  }
135  return std::distance(begin(names_seen), it) + 1;
136 }
137 
138 void
140  std::string const& colorscheme,
141  std::ostream& os)
142 {
143  os << " [ colorscheme=" << colorscheme << " color=" << color(p.processName())
144  << " style=filled ];\n";
145 }
146 
147 void
149  std::string const& colorscheme,
150  std::ostream& os,
151  int debug)
152 {
153  if (debug > 0) {
154  os << "# write_creator_line for provenance: " << &p << '\n';
155  }
156  write_module_id(p, os);
157  write_module_node(p, colorscheme, os);
158  write_module_id(p, os);
159  os << " -> ";
160  write_id(p, os);
161  os << ";\n";
162 }
163 
164 void
165 write_parent_id(art::ProductID const parent, std::ostream& os)
166 {
167  os << 'b' << parent;
168 }
169 
170 void
172  art::ProductID const parent,
173  std::ostream& os,
174  int debug)
175 {
176  if (debug > 0) {
177  os << "# write_parentage_line for provenance: " << &p << " parent "
178  << parent << '\n';
179  }
180  write_parent_id(parent, os);
181  os << " -> ";
182  write_module_id(p, os);
183  os << ";\n";
184 }
185 
186 void
188 {
191  for (art::ProductID const parent : p.parents()) {
192  write_parentage_line(p, parent, out_, debug_);
193  }
194 }
195 
fhicl::Atom< std::string > dotfile
void write_module_node(art::Provenance const &p, std::string const &colorscheme, std::ostream &os)
std::vector< ProductID > const & parents() const
Definition: Provenance.cc:109
ProductID productID() const noexcept
Definition: Provenance.cc:72
std::string const & productInstanceName() const noexcept
Definition: Provenance.cc:60
fhicl::Atom< std::string > colorscheme
std::string colorscheme_
void write_module_id(art::Provenance const &p, std::ostream &os)
std::string const & moduleLabel() const noexcept
Definition: Provenance.cc:54
void processEventProvenance(art::Provenance const &prov)
void write_product_node(art::Provenance const &p, std::ostream &os, int debug)
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
void write_creator_line(art::Provenance const &p, std::string const &colorscheme, std::ostream &os, int debug)
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
DebugStuff debug
Definition: DebugStruct.cxx:4
void write_parentage_line(art::Provenance const &p, art::ProductID const parent, std::ostream &os, int debug)
DataFlow(fhicl::TableFragment< Config > const &cfg)
fhicl::Atom< int > debuglevel
std::string const & processName() const noexcept
Definition: Provenance.cc:66
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::size_t color(std::string const &procname)
std::string const & friendlyClassName() const noexcept
Definition: Provenance.cc:48
void write_id(art::ProductID const pid, std::ostream &os)
Definition: MVAAlg.h:12
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
void write_parent_id(art::ProductID const parent, std::ostream &os)
void format_product_node(std::string const &fcn, std::string const &pin, std::ostream &os)