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 
13 #include "fhiclcpp/types/Atom.h"
14 #include "fhiclcpp/types/Name.h"
15 
16 #include <algorithm>
17 #include <fstream>
18 #include <string>
19 #include <vector>
20 
21 namespace art {
22  // DataFlow is the class we define to provide our specialization of
23  // the ProvenanceDumper class template, to create the DataFlowDumper
24  // class.
25  class DataFlow;
27 } // namespace art
28 
30 public:
31  struct Config {
32  fhicl::Atom<std::string> dotfile{fhicl::Name("dotfile"), "flow.dot"};
35  };
36 
37  explicit DataFlow(fhicl::TableFragment<Config> const& cfg);
38 
39  // Prepare to write out the graph for an event.
40  void preProcessEvent();
41 
42  // Finalize writing the graph for an event.
43  void postProcessEvent();
44 
45  // Write the graph description lines for the data product associated
46  // with the given provenance.
47  void processEventProvenance(art::Provenance const& prov);
48 
49 private:
50  std::ofstream out_;
51  int nEvents_;
53  int debug_;
54 };
55 
56 //-----------------------------------------------------------------
57 
59  : out_(cfg().dotfile())
60  , nEvents_(0)
61  , colorscheme_(cfg().colorscheme())
62  , debug_(cfg().debuglevel())
63 {
64  if (!out_) {
66  << "Failed to create output file: " << cfg().dotfile();
67  }
68 }
69 
70 void
72 {
73  out_ << "digraph d" << nEvents_ << " {\n";
74 }
75 
76 void
78 {
79  out_ << "}\n\n";
80  ++nEvents_;
81 }
82 
83 // Write the identifier for the node for the product to which this
84 // Provenance belongs.
85 void
86 write_id(art::ProductID const pid, std::ostream& os)
87 {
88  os << "\"b" << pid << '\"';
89 }
90 
91 void
92 write_id(art::Provenance const& p, std::ostream& os)
93 {
94  write_id(p.productID(), os);
95 }
96 
97 // format_product_node defines the format for the product nade.
98 void
100  std::string const& pin,
101  std::ostream& os)
102 {
103  os << " [label = \"" << fcn;
104  if (!pin.empty())
105  os << "/" << pin;
106  os << "\" shape = box];\n";
107 }
108 
109 // Write the line defining the node for the product to which this
110 // Provenance belongs.
111 void
112 write_product_node(art::Provenance const& p, std::ostream& os, int debug)
113 {
114  if (debug > 0) {
115  os << "# write_product_node for provenance: " << &p << '\n';
116  }
117  write_id(p, os);
119 }
120 
121 void
122 write_module_id(art::Provenance const& p, std::ostream& os)
123 {
124  os << '\"' << p.moduleLabel() << '/' << p.processName() << '\"';
125 }
126 
127 std::size_t
128 color(std::string const& procname)
129 {
130  static std::vector<std::string> names_seen;
131  auto it = std::find(begin(names_seen), end(names_seen), procname);
132  if (it == end(names_seen)) {
133  names_seen.push_back(procname);
134  return names_seen.size();
135  }
136  return std::distance(begin(names_seen), it) + 1;
137 }
138 
139 void
141  std::string const& colorscheme,
142  std::ostream& os)
143 {
144  os << " [ colorscheme=" << colorscheme << " color=" << color(p.processName())
145  << " style=filled ];\n";
146 }
147 
148 void
150  std::string const& colorscheme,
151  std::ostream& os,
152  int debug)
153 {
154  if (debug > 0) {
155  os << "# write_creator_line for provenance: " << &p << '\n';
156  }
157  write_module_id(p, os);
158  write_module_node(p, colorscheme, os);
159  write_module_id(p, os);
160  os << " -> ";
161  write_id(p, os);
162  os << ";\n";
163 }
164 
165 void
166 write_parent_id(art::ProductID const parent, std::ostream& os)
167 {
168  os << 'b' << parent;
169 }
170 
171 void
173  art::ProductID const parent,
174  std::ostream& os,
175  int debug)
176 {
177  if (debug > 0) {
178  os << "# write_parentage_line for provenance: " << &p << " parent "
179  << parent << '\n';
180  }
181  write_parent_id(parent, os);
182  os << " -> ";
183  write_module_id(p, os);
184  os << ";\n";
185 }
186 
187 void
189 {
192  for (art::ProductID const parent : p.parents()) {
194  }
195 }
196 
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
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 string
Definition: nybbler.cc:12
std::string const & productInstanceName() const noexcept
Definition: Provenance.cc:60
fhicl::Atom< std::string > colorscheme
ChannelGroupService::Name Name
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)
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:67
void write_parentage_line(art::Provenance const &p, art::ProductID const parent, std::ostream &os, int debug)
DataFlow(fhicl::TableFragment< Config > const &cfg)
p
Definition: test.py:223
fhicl::Atom< int > debuglevel
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
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)
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
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)
def parent(G, child, parent_type)
Definition: graph.py:67