LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ConsumesInfo.h
Go to the documentation of this file.
1 #ifndef art_Framework_Principal_ConsumesInfo_h
2 #define art_Framework_Principal_ConsumesInfo_h
3 // vim: set sw=2 expandtab :
4 
5 // FIXME: THESE NOTES SHOULD GO SOMEWHERE ELSE
6 //
7 //============================================================================
8 // Consumer is the base class for all module types that retrieve
9 // products. See below for guidance as to which interface should be
10 // called for a given context.
11 //
12 // N.B. All 'consumes*' or 'mayConsume*' calls should be done in a
13 // module's constructor, before processing any beginJob
14 // callbacks.
15 //
16 // Unconditional retrieval of products
17 // -----------------------------------
18 //
19 // For products that are always intended to be retrieved for a given
20 // module execution, the 'consumes' family of calls should be invoked
21 // in a user's module constructor (e.g.):
22 //
23 // consumes<int>(input_tag); // => ProductToken<int>
24 // consumesMany<int>(); // => void
25 // consumesView<int>(input_tag); // => ViewToken<int>
26 //
27 // Making such calls tells the framework that the particular module in
28 // question will make the following product retrievals (e.g.):
29 //
30 // e.getValidHandle<int>(input_tag);
31 // e.getManyByType<int>();
32 // art::View<int> v;
33 // e.getView(input_tag, v);
34 //
35 // The returned type of the consumes calls are shown above. To
36 // eventually facilitate faster product lookup, the returned tokens
37 // can be supplied by the user (e.g.):
38 //
39 // ProductToken<int> intToken_; // data member of module class
40 // e.getValidHandle(intToken_); // => ValidHandle<int>
41 //
42 // ViewToken<int> viewToken_; // data member of module class
43 // std::vector<int const*> v;
44 // e.getView(viewToken_, v);
45 //
46 // The consumesMany function template does not return a token.
47 //
48 // Conditional retrieval of products
49 // ---------------------------------
50 //
51 // If there are situations in which a product *may* be retrieved
52 // (e.g. within an 'if' block), then the *mayConsume* interface should
53 // be considered instead of the consumes interface:
54 //
55 // mayConsume<int>(input_tag); // => ProductToken<int>
56 // mayConsumeMany<int>(); // => void
57 // mayConsumeView<int>(); // => ViewToken<int>
58 //
59 // The return types of the functions are the same as their 'consumes'
60 // partners for unconditional product retrieving. However, how the
61 // tokens are used by the framework may be different than how they are
62 // used in the 'consumes' case.
63 //
64 // Retrieving products in non-module contexts
65 // ------------------------------------------
66 //
67 // There are cases where products are retrieved in non-module
68 // contexts. Although such product retrieval is not forbidden, it is
69 // not a generally recommended usage pattern. The 'consumes'
70 // interface is, therefore, not supported in non-module contexts.
71 //============================================================================
72 
74 
75 #include <array>
76 #include <atomic>
77 #include <map>
78 #include <mutex>
79 #include <set>
80 #include <string>
81 #include <vector>
82 
83 namespace art {
84 
85  class ModuleDescription;
86  class ProductInfo;
87 
88  class ConsumesInfo {
89  public: // MEMBER FUNCTIONS -- Special Member Functions
90  ~ConsumesInfo();
91  ConsumesInfo(ConsumesInfo const&) = delete;
92  ConsumesInfo(ConsumesInfo&&) = delete;
93  ConsumesInfo& operator=(ConsumesInfo const&) = delete;
94  ConsumesInfo& operator=(ConsumesInfo&&) = delete;
95 
96  public:
97  static ConsumesInfo* instance();
98  static std::string assemble_consumes_statement(BranchType const,
99  ProductInfo const&);
100  static std::string module_context(ModuleDescription const&);
101 
102  void setRequireConsumes(bool const);
103 
104  // Maps module label to run, per-branch consumes info.
105  using consumables_t =
106  std::map<std::string const,
107  std::array<std::vector<ProductInfo>, NumBranchTypes>>;
108 
109  consumables_t::mapped_type const&
110  consumables(std::string const& module_label) const
111  {
112  return consumables_.at(module_label);
113  }
114 
115  void collectConsumes(std::string const& module_label,
116  consumables_t::mapped_type const& consumables);
117 
118  // This is used by get*() in ProductRetriever.
120  ModuleDescription const&,
121  ProductInfo const& productInfo);
122 
123  void showMissingConsumes() const;
124 
125  private:
126  ConsumesInfo();
127 
128  // Protects access to consumables_ and missingConsumes_.
129  mutable std::recursive_mutex mutex_{};
130 
131  std::atomic<bool> requireConsumes_;
132 
133  // Maps module label to run, per-branch consumes info. Note that
134  // there is only one entry per module label. This is intentional
135  // so that we do not need to store consumes information for each
136  // replicated module object.
138 
139  // Maps module label to run, per-branch missing product consumes info.
140  std::map<std::string const,
141  std::array<std::set<ProductInfo>, NumBranchTypes>>
143  };
144 } // namespace art
145 
146 #endif /* art_Framework_Principal_ConsumesInfo_h */
147 
148 // Local Variables:
149 // mode: c++
150 // End:
consumables_t::mapped_type const & consumables(std::string const &module_label) const
Definition: ConsumesInfo.h:110
void collectConsumes(std::string const &module_label, consumables_t::mapped_type const &consumables)
static ConsumesInfo * instance()
Definition: ConsumesInfo.cc:27
std::map< std::string const, std::array< std::set< ProductInfo >, NumBranchTypes > > missingConsumes_
Definition: ConsumesInfo.h:142
consumables_t consumables_
Definition: ConsumesInfo.h:137
std::map< std::string const, std::array< std::vector< ProductInfo >, NumBranchTypes >> consumables_t
Definition: ConsumesInfo.h:107
std::recursive_mutex mutex_
Definition: ConsumesInfo.h:129
ConsumesInfo & operator=(ConsumesInfo const &)=delete
void validateConsumedProduct(BranchType const, ModuleDescription const &, ProductInfo const &productInfo)
void showMissingConsumes() const
static std::string assemble_consumes_statement(BranchType const, ProductInfo const &)
Definition: ConsumesInfo.cc:34
static std::string module_context(ModuleDescription const &)
Definition: ConsumesInfo.cc:85
BranchType
Definition: BranchType.h:20
void setRequireConsumes(bool const)
Definition: ConsumesInfo.cc:96
Definition: MVAAlg.h:12
std::atomic< bool > requireConsumes_
Definition: ConsumesInfo.h:131