LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
FileCatalogMetadata.h
Go to the documentation of this file.
1 #ifndef art_Framework_Services_System_FileCatalogMetadata_h
2 #define art_Framework_Services_System_FileCatalogMetadata_h
3 // vim: set sw=2 expandtab :
4 
9 #include "cetlib/container_algorithms.h"
10 #include "fhiclcpp/types/Atom.h"
13 
14 #include <mutex>
15 #include <string>
16 #include <unordered_map>
17 #include <vector>
18 
19 namespace art {
20 
22  public:
23  using collection_type = std::vector<std::pair<std::string, std::string>>;
24  using value_type = collection_type::value_type;
25 
26  struct Config {
27  fhicl::Atom<bool> checkSyntax{fhicl::Name("checkSyntax"), false};
29  fhicl::Name("applicationFamily")};
31  fhicl::Name("applicationVersion")};
34 
36  fhicl::Name("metadataFromInput"),
38  "This list specifies the metadata that is inherited\n"
39  "from the input file. Currently only the run type and\n"
40  "file type metadata can be inherited. The default list is empty."),
41  std::vector<std::string>{}};
42 
43  bool
44  inMetadataList(std::string const& name) const
45  {
46  return cet::search_all(metadataFromInput(), name);
47  }
48 
50  fhicl::Name("fileType"),
51  fhicl::Comment("Can specify 'fileType' only if it is not specified\n"
52  "in the 'metadataFromInput' list."),
53  [this] { return !inMetadataList("fileType"); },
54  "unknown"};
55 
57  fhicl::Name("runType"),
58  fhicl::Comment("Can specify 'runType' only if it is not specified\n"
59  "in the 'metadataFromInput' list."),
60  [this] { return !inMetadataList("runType"); }};
61  };
62 
64 
65  explicit FileCatalogMetadata(Parameters const& config);
66 
67  // Add a new value to the metadata store.
68  void addMetadata(std::string const& key, std::string const& value);
69 
70  // Ensure the value is a canonical string representation.
71  void addMetadataString(std::string const& key, std::string const& value);
72 
73  // Dump stored metadata into the provided container.
74  void getMetadata(collection_type& coll) const;
75 
76  // RootInput can set the run-type and file-type parameters
77  void setMetadataFromInput(collection_type const& coll);
78 
79  // Ascertain whether JSON syntax checking is desired.
80  bool wantCheckSyntax() const noexcept;
81 
82  // Types
83  private:
85  public:
86  InheritedMetadata(std::vector<std::string> const& sortedMdToInherit,
87  collection_type const& coll)
88  {
89  detail::OldToNew const translator;
90  for (auto const& pr : coll) {
91  if (cet::search_all(sortedMdToInherit, translator(pr.first))) {
92  inputmd_.insert(pr);
93  orderedmd_.emplace_back(pr);
94  }
95  }
96  }
97 
98  auto const&
99  entries() const
100  {
101  return orderedmd_;
102  }
103 
104  void
105  check_values(collection_type const& fromInput) const
106  {
107  for (auto const& [key, value] : fromInput) {
108  auto it = inputmd_.find(key);
109  if (it == cend(inputmd_)) {
111  << "Metadata key " << key
112  << " missing from list of metadata to inherit from input "
113  "files.\n";
114  } else if (it->second != value) {
116  << "The value for '" << key
117  << "' for the current file is: " << value
118  << ", which conflicts with the value from the first input file "
119  "(\""
120  << it->second << "\").\n";
121  }
122  }
123  }
124 
125  private:
127  std::unordered_map<std::string, std::string> inputmd_;
128  };
129 
130  private:
131  // Protects all data members.
132  mutable std::recursive_mutex mutex_{};
133 
134  // Whether or not the user wishes metadata to be checked for syntax by
135  // parsing with a JSON parser.
136  bool const checkSyntax_;
137 
138  // Metadata the user wishes to inherit from the input file, const after the
139  // ctor.
140  std::vector<std::string> const mdToInherit_;
141 
142  // The collected metadata.
144 
145  // Metadata we have already inherited.
146  std::unique_ptr<InheritedMetadata> imd_{};
147  };
148 
149 } // namespace art
150 
152 
153 #endif /* art_Framework_Services_System_FileCatalogMetadata_h */
154 
155 // Local Variables:
156 // mode: c++
157 // End:
FileCatalogMetadata(Parameters const &config)
std::vector< std::pair< std::string, std::string >> collection_type
void check_values(collection_type const &fromInput) const
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:93
fhicl::OptionalAtom< std::string > applicationVersion
void setMetadataFromInput(collection_type const &coll)
fhicl::Sequence< std::string > metadataFromInput
bool inMetadataList(std::string const &name) const
fhicl::Atom< std::string > fileType
std::unordered_map< std::string, std::string > inputmd_
void addMetadataString(std::string const &key, std::string const &value)
#define DECLARE_ART_SERVICE(svc, scope)
std::recursive_mutex mutex_
fhicl::OptionalAtom< std::string > applicationFamily
fhicl::OptionalAtom< std::string > group
InheritedMetadata(std::vector< std::string > const &sortedMdToInherit, collection_type const &coll)
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
fhicl::OptionalAtom< std::string > processID
fhicl::OptionalAtom< std::string > runType
collection_type::value_type value_type