All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ArtDataProductSelectors.cxx
Go to the documentation of this file.
1 /**
2  * @file icaruscode/Utilities/ArtDataProductSelectors.cxx
3  * @brief Helpers to pass to `art::Event::getMany()` and similar (implement.).
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date August 30, 2022
6  * @see icaruscode/Utilities/ArtDataProductSelectors.h
7  */
8 
9 // library header
11 
12 // framework libraries
13 #include "canvas/Persistency/Provenance/BranchDescription.h"
14 #include "canvas/Utilities/InputTag.h"
15 
16 // C/C++ standard libraries
17 #include <regex>
18 #include <string>
19 #include <utility> // std::move()
20 
21 
22 // -----------------------------------------------------------------------------
24  (std::vector<ProductRegex> patterns)
25  : fPatterns(std::move(patterns))
26 {}
27 
28 
29 // -----------------------------------------------------------------------------
31  (art::InputTag const& spec) -> ProductRegex
32 {
33 
34  auto const regexIfNotEmpty = [](std::string const& ptn)
35  { return ptn.empty()? std::nullopt: std::optional{ std::regex{ ptn }}; };
36 
37  return {
38  regexIfNotEmpty(spec.process()) // process
39  , regexIfNotEmpty(spec.label()) // module
40  , regexIfNotEmpty(spec.instance()) // instance
41  };
42 
43 } // util::RegexDataProductSelector::makePattern()
44 
45 
46 // -----------------------------------------------------------------------------
48  (art::BranchDescription const& brDescr) const
49 {
50  art::InputTag const& tag = brDescr.inputTag();
51  for (ProductRegex const& pattern: fPatterns)
52  if (matchPattern(tag, pattern)) return true;
53  return false;
54 } // util::RegexDataProductSelector::doMatch()
55 
56 
57 // -----------------------------------------------------------------------------
59  (std::string const& indent) const
60 {
61  return indent
62  + "any of " + std::to_string(fPatterns.size()) + " patterns";
63 } // util::RegexDataProductSelector::doPrint()
64 
65 
66 // -----------------------------------------------------------------------------
68  (art::InputTag const& tag, ProductRegex const& ptn) const
69 {
70  if (ptn.process && !std::regex_match(tag.process(), *(ptn.process) ))
71  return false;
72  if (ptn.module && !std::regex_match(tag.label(), *(ptn.module) ))
73  return false;
74  if (ptn.instance && !std::regex_match(tag.instance(), *(ptn.instance)))
75  return false;
76  return true;
77 } // util::RegexDataProductSelector::matchPattern()
78 
79 
80 // -----------------------------------------------------------------------------
81 
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).
virtual std::string doPrint(std::string const &indent) const override
Part of the message used when no data product matches.
bool matchPattern(art::InputTag const &tag, ProductRegex const &ptn) const
Returns whether the input tag matches the pattern ptn.
Helpers to pass to art::Event::getMany() and similar.
std::string to_string(WindowPattern const &pattern)
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.