LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
FileCatalogMetadata.cc
Go to the documentation of this file.
2 
3 #include "boost/json.hpp"
5 #include "cetlib/canonical_string.h"
6 #include "cetlib/container_algorithms.h"
7 #include "fhiclcpp/types/Atom.h"
10 
11 #include <string>
12 #include <vector>
13 
14 using namespace std;
15 
16 namespace art {
17 
18  namespace {
19  detail::OldToNew const oldToNew{};
20  vector<string>
21  maybeTranslate(vector<string> names)
22  {
23  cet::transform_all(names, names.begin(), oldToNew);
24  cet::sort_all(names);
25  return names;
26  }
27 
28  bool
29  search_translated_all(vector<string> const& s, string const& md)
30  {
31  return cet::search_all(s, oldToNew(md));
32  }
33  } // unnamed namespace
34 
35  FileCatalogMetadata::FileCatalogMetadata(
36  FileCatalogMetadata::Parameters const& config)
37  : checkSyntax_{config().checkSyntax()}
38  , mdToInherit_{maybeTranslate(config().metadataFromInput())}
39  {
40  if (auto appFam = config().applicationFamily()) {
41  addMetadataString("application.family", *appFam);
42  }
43  if (auto appVer = config().applicationVersion()) {
44  addMetadataString("application.version", *appVer);
45  }
46 
47  // Always write out fileType -- either by inheriting from the input
48  // file or by overriding via the FHiCL parameter "fileType".
49  if (!search_translated_all(mdToInherit_, "file_type")) {
50  addMetadataString("file_type", config().fileType());
51  }
52  string rt;
53  if (!search_translated_all(mdToInherit_, "run_type") &&
54  config().runType(rt)) {
55  addMetadataString("art.run_type", rt);
56  }
57  if (auto grp = config().group()) {
58  addMetadataString("group", *grp);
59  }
60  if (auto pid = config().processID()) {
61  addMetadataString("process_id", *pid);
62  }
63  }
64 
65  bool
67  {
68  return checkSyntax_;
69  }
70 
71  void
72  FileCatalogMetadata::addMetadataString(string const& key, string const& value)
73  {
74  addMetadata(key, cet::canonical_string(value));
75  }
76 
77  void
78  FileCatalogMetadata::addMetadata(string const& key, string const& value)
79  {
80  if (checkSyntax_) {
81  string checkString("{ ");
82  checkString += cet::canonical_string(key) + " : " + value + " }";
83  boost::json::error_code ec;
85  auto const n_parsed_chars = p.write_some(checkString, ec);
86  if (ec) {
88  << "FileCatalogMetadata::addMetadata() JSON " << ec.message() << ":\n"
89  << "Faulty key/value clause:\n"
90  << checkString << '\n'
91  << (n_parsed_chars ? string(n_parsed_chars, '-') : "") << "^\n";
92  }
93  }
94  std::lock_guard sentry{mutex_};
95  md_.emplace_back(key, value);
96  }
97 
98  void
100  {
101  if (mdToInherit_.empty()) {
102  return;
103  }
104 
105  std::lock_guard sentry{mutex_};
106  if (!imd_) {
107  imd_ = make_unique<InheritedMetadata>(mdToInherit_, mdFromInput);
108  } else {
109  imd_->check_values(mdFromInput);
110  }
111  for (auto const& [key, value] : imd_->entries()) {
112  addMetadataString(oldToNew(key), value);
113  }
114  }
115 
116  void
118  {
119  std::lock_guard sentry{mutex_};
120  cet::copy_all(md_, back_inserter(coll));
121  }
122 
123 } // namespace art
std::vector< std::pair< std::string, std::string >> collection_type
void setMetadataFromInput(collection_type const &coll)
STL namespace.
void addMetadataString(std::string const &key, std::string const &value)
std::recursive_mutex mutex_
CommandLineParser * parser(0)
double value
Definition: spectrum.C:18
std::unique_ptr< InheritedMetadata > imd_
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::vector< std::string > const mdToInherit_
void addMetadata(std::string const &key, std::string const &value)
Definition: MVAAlg.h:12
bool wantCheckSyntax() const noexcept
void getMetadata(collection_type &coll) const