All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ArtDataProductSelectors.h
Go to the documentation of this file.
1 /**
2  * @file icaruscode/Utilities/ArtDataProductSelectors.h
3  * @brief Helpers to pass to `art::Event::getMany()` and similar.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date August 30, 2022
6  */
7 
8 #ifndef ICARUSCODE_UTILITIES_ARTDATAPRODUCTSELECTORS_H
9 #define ICARUSCODE_UTILITIES_ARTDATAPRODUCTSELECTORS_H
10 
11 // framework libraries
12 #include "art/Framework/Principal/SelectorBase.h"
13 
14 // C/C++ standard libraries
15 #include <regex>
16 #include <vector>
17 #include <optional>
18 
19 
20 // -----------------------------------------------------------------------------
21 namespace art { class InputTag; class BranchDescription; }
22 
23 // -----------------------------------------------------------------------------
24 namespace util { class RegexDataProductSelector; }
25 /**
26  * @brief Matches products by regex on process, module label and instance name.
27  *
28  * A pattern is matched if all specified subpatterns (process, module, instance)
29  * match. Any component of the pattern may be unspecified.
30  * A product is selected if it matches any of the configured patterns.
31  *
32  * Patterns are matched with `std::regex_match()`, i.e. the whole string must
33  * match.
34  *
35  * The patterns may be created from one or a sequence of input-tag-like objects
36  * using `makePattern()` and `makePatterns()` static functions, respectively.
37  */
38 class util::RegexDataProductSelector: public art::SelectorBase {
39  public:
40 
41  /// A pattern on a input tag (empty matches everything).
42  struct ProductRegex {
43  std::optional<std::regex> process;
44  std::optional<std::regex> module;
45  std::optional<std::regex> instance;
46  };
47 
48  /// Initializes the selector with all the supported patterns.
49  RegexDataProductSelector(std::vector<ProductRegex> patterns);
50 
51 
52  /**
53  * @brief Parses a input tag to create a single data product pattern.
54  * @param spec pattern specification
55  * @return a pattern object
56  *
57  * A specification is in the form of an `art::InputTag`, but each element
58  * is a regex pattern rather than a standard name.
59  *
60  * If the input tag is created from a string, in the usual form
61  * `<processName>:<moduleLabel>:<instanceName>` (with the usual omissions),
62  * each element can't contain a `:` character (no escaping is supported).
63  *
64  * An empty `tag` matches everything.
65  */
66  static ProductRegex makePattern(art::InputTag const& spec);
67 
68 
69  /**
70  * @brief Parses a sequence of input tags to create data product patterns.
71  * @tparam SpecColl type of collection of specifications
72  * @param specs sequence of pattern specifications
73  * @return a sequence of pattern objects, one per input specification
74  *
75  * Each specification in `specs` is converted into a pattern via
76  * `makePattern()`. The specification is explicitly converted into an input
77  * tag.
78  */
79  template <typename SpecColl>
80  static std::vector<ProductRegex> makePatterns(SpecColl const& specs);
81 
82  private:
83 
84  std::vector<ProductRegex> fPatterns; ///< All the selection patterns.
85 
86 
87  /// Returns whether data product described by `brDescr` matches.
88  virtual bool doMatch(art::BranchDescription const& brDescr) const override;
89 
90  /// Part of the message used when no data product matches.
91  virtual std::string doPrint(std::string const& indent) const override;
92 
93 
94  /// Returns whether the input `tag` matches the pattern `ptn`.
95  bool matchPattern(art::InputTag const& tag, ProductRegex const& ptn) const;
96 
97 }; // class util::RegexDataProductSelector
98 
99 
100 
101 // -----------------------------------------------------------------------------
102 // --- Template implementation
103 // -----------------------------------------------------------------------------
104 template <typename SpecColl>
106  (SpecColl const& specs) -> std::vector<ProductRegex>
107 {
108  using std::size;
109  std::vector<ProductRegex> ptns;
110  ptns.reserve(size(specs));
111  for (auto const& spec: specs)
112  ptns.push_back(makePattern(art::InputTag{ spec }));
113  return ptns;
114 } // util::RegexDataProductSelector::makePatterns()
115 
116 
117 // -----------------------------------------------------------------------------
118 
119 
120 #endif // ICARUSCODE_UTILITIES_ARTDATAPRODUCTSELECTORS_H
Matches products by regex on process, module label and instance name.
virtual bool doMatch(art::BranchDescription const &brDescr) const override
Returns whether data product described by brDescr matches.
A pattern on a input tag (empty matches everything).
std::vector< ProductRegex > fPatterns
All the selection patterns.
std::size_t size(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:561
virtual std::string doPrint(std::string const &indent) const override
Part of the message used when no data product matches.
static std::vector< ProductRegex > makePatterns(SpecColl const &specs)
Parses a sequence of input tags to create data product patterns.
bool matchPattern(art::InputTag const &tag, ProductRegex const &ptn) const
Returns whether the input tag matches the pattern ptn.
RegexDataProductSelector(std::vector< ProductRegex > patterns)
Initializes the selector with all the supported patterns.
static ProductRegex makePattern(art::InputTag const &spec)
Parses a input tag to create a single data product pattern.