LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
Principal.h
Go to the documentation of this file.
1 #ifndef art_Framework_Principal_Principal_h
2 #define art_Framework_Principal_Principal_h
3 // vim: set sw=2 expandtab :
4 
5 // =================================================================
6 // Principal
7 //
8 // Pure abstract base class for Run-, SubRun-, and EventPrincipal,
9 // the classes which manage data products.
10 //
11 // The major internal component is the Group, which contains an
12 // EDProduct and its associated Provenance, along with ancillary
13 // transient information regarding the two. Groups are handled
14 // through shared pointers.
15 //
16 // The Principal returns GroupQueryResult, rather than a shared
17 // pointer to a Group, when queried.
18 // =================================================================
19 
38 #include "cetlib/exempt_ptr.h"
39 
40 #include <atomic>
41 #include <memory>
42 #include <mutex>
43 #include <optional>
44 #include <string>
45 #include <vector>
46 
47 namespace art {
48 
49  class Principal : public PrincipalBase {
50  public:
51  using GroupCollection = std::map<ProductID, std::unique_ptr<Group>>;
54 
55  // The destructor is defined in the header so that overrides of
56  // DelayedReader's readFromSecondaryFile_ virtual function can
57  // return an std::unique_ptr<Principal> object (std::unique_ptr
58  // instantiations require a well-formed deleter).
59  virtual ~Principal() noexcept = default;
60 
62  ProcessConfiguration const&,
63  cet::exempt_ptr<ProductTable const> presentProducts,
64  ProcessHistoryID const& id,
65  std::unique_ptr<DelayedReader>&& reader =
66  std::make_unique<NoDelayedReader>());
67 
68  // Disable copying and moving.
69  Principal(Principal const&) = delete;
70  Principal& operator=(Principal const&) = delete;
71 
72  // Interface for ProductRetriever<T>
73  //
74  // - Used by art::ProductRetriever<T>::get(ProductID const pid,
75  // Handle<T>& result) const. (easy user-facing api) Used by
76  // Principal::productGetter(ProductID const pid) const
77  //
78  // - Used by (Run,SubRun,Event,Results)::productGetter (advanced
79  // user-facing api)
81 
83  WrappedTypeID const& wrapped,
84  SelectorBase const&,
85  ProcessTag const&) const;
87  WrappedTypeID const& wrapped,
88  std::string const& label,
89  std::string const& productInstanceName,
90  ProcessTag const& processTag) const;
91  std::vector<GroupQueryResult> getMany(ModuleContext const& mc,
92  WrappedTypeID const& wrapped,
93  SelectorBase const&,
94  ProcessTag const&) const;
95 
96  std::vector<InputTag> getInputTags(ModuleContext const& mc,
97  WrappedTypeID const& wrapped,
98  SelectorBase const&,
99  ProcessTag const&) const;
100 
101  // Used only by ProductRetriever<T> to implement getView.
102  // FIXME: Return a vector of GroupQueryResult to products which
103  // are sequences, have a nested type named 'value_type',
104  // and where elementType the same as, or a public base of,
105  // this value_type, and which match the given selector.
106  std::vector<cet::exempt_ptr<Group>> getMatchingSequence(
107  ModuleContext const&,
108  SelectorBase const&,
109  ProcessTag const&) const;
110 
111  // Note: LArSoft uses this extensively to create a Ptr by hand.
112  EDProductGetter const* productGetter(ProductID id) const;
113  Provenance provenance(ProductID id) const;
114 
115  ProcessHistory const& processHistory() const;
116 
117  // Interface for other parts of art
118 
119  // Note: We invoke the delay reader if no user module has fetched
120  // them for this principal if resolvedProd is true. This
121  // attempts to resolved the product and converts the
122  // resulting group into an OutputHandle.
123  OutputHandle getForOutput(ProductID const&, bool resolveProd) const;
124 
125  // Used to provide access to the product descriptions
126  cet::exempt_ptr<BranchDescription const> getProductDescription(
127  ProductID const pid,
128  bool const alwaysEnableLookupOfProducedProducts = false) const;
129 
130  // The product tables data member for produced products is set by
131  // the EventProcessor after the Principal is provided by the input
132  // source.
133  void createGroupsForProducedProducts(ProductTables const& producedProducts);
135 
136  // FIXME: This breaks the purpose of the
137  // Principal::addToProcessHistory() compare_exchange_strong
138  // because of the temporal hole between when the history is
139  // changed and when the flag is set, this must be fixed!
141 
142  // Read all data products and provenance immediately, if available.
143  void readImmediate() const;
144 
146 
147  ProcessHistoryID const&
149  {
150  return processHistory_.id();
151  }
152 
153  cet::exempt_ptr<ProductProvenance const> branchToProductProvenance(
154  ProductID const&) const;
155 
156  size_t size() const;
157 
158  const_iterator begin() const;
159  const_iterator cbegin() const;
160 
161  const_iterator end() const;
162  const_iterator cend() const;
163 
164  // Flag that we have been updated in the current process.
165  void addToProcessHistory();
166 
167  BranchType branchType() const;
168 
169  RangeSet seenRanges() const;
170 
171  void put(BranchDescription const&,
172  std::unique_ptr<ProductProvenance const>&&,
173  std::unique_ptr<EDProduct>&&,
174  std::unique_ptr<RangeSet>&&);
175 
176  protected:
177  std::optional<ProductInserter> makeInserter(ModuleContext const& mc);
178 
179  private:
180  // Used by our ctors.
181  void ctor_create_groups(cet::exempt_ptr<ProductTable const>);
182  void ctor_read_provenance();
184 
185  cet::exempt_ptr<Group> getGroupLocal(ProductID const) const;
186 
187  std::vector<cet::exempt_ptr<Group>> matchingSequenceFromInputFile(
188  ModuleContext const&,
189  SelectorBase const&) const;
191  ModuleContext const&,
192  WrappedTypeID const& wrapped,
193  SelectorBase const&,
194  std::vector<cet::exempt_ptr<Group>>& results) const;
195  size_t findGroups(ProcessLookup const&,
196  ModuleContext const&,
197  SelectorBase const&,
198  std::vector<cet::exempt_ptr<Group>>& groups) const;
199  size_t findGroupsForProcess(
200  std::vector<ProductID> const& vpid,
201  ModuleContext const& mc,
202  SelectorBase const& selector,
203  std::vector<cet::exempt_ptr<Group>>& groups) const;
204  bool producedInProcess(ProductID) const;
205  bool presentFromSource(ProductID) const;
206  auto tryNextSecondaryFile() const;
207 
208  // Implementation of the ProductRetriever API.
209  std::vector<cet::exempt_ptr<Group>> findGroupsForProduct(
210  ModuleContext const& mc,
211  WrappedTypeID const& wrapped,
212  SelectorBase const&,
213  ProcessTag const&) const;
214 
215  EDProductGetter const* getEDProductGetter_(ProductID const&) const override;
216 
217  cet::exempt_ptr<Group> getGroupTryAllFiles(ProductID const) const;
218 
219  protected:
220  // Used to deal with TriggerResults.
221  void fillGroup(BranchDescription const&);
222 
223  // Used by addToProcessHistory()
225 
226  // Used by EndPathExecutor
227  void updateSeenRanges(RangeSet const& rs);
228 
229  private:
232  std::atomic<bool> processHistoryModified_{false};
234 
235  // Product-lookup tables
236  std::atomic<ProductTable const*> presentProducts_;
237  std::atomic<ProductTable const*> producedProducts_{nullptr};
238  std::atomic<bool> enableLookupOfProducedProducts_{false};
239 
240  // Protects access to groups_.
241  mutable std::recursive_mutex groupMutex_{};
242 
243  // All of the currently known data products.
244  // tbb::concurrent_unordered_map<ProductID, std::unique_ptr<Group>>
245  // groups_{};
247 
248  // Pointer to the reader that will be used to obtain
249  // EDProducts from the persistent store.
250  std::unique_ptr<DelayedReader> delayedReader_{nullptr};
251 
252  // Secondary principals. Note that the lifetimes of Results, Run,
253  // and SubRun principals do not exceed the lifetime of the input
254  // file.
255  //
256  // Note: To make secondary file-reading thread-safe, we will need to
257  // ensure that any adjustments to the secondaryPrincipals_
258  // vector is done atomically with respect to any reading of
259  // the vector.
260  mutable std::vector<std::unique_ptr<Principal>> secondaryPrincipals_{};
261 
262  // Index into the secondary file names vector of the next
263  // file that a secondary principal should be created from.
264  mutable int nextSecondaryFileIdx_{};
265 
267  };
268 
269 } // namespace art
270 
271 // Local Variables:
272 // mode: c++
273 // End:
274 #endif /* art_Framework_Principal_Principal_h */
Principal & operator=(Principal const &)=delete
std::optional< ProductInserter > makeInserter(ModuleContext const &mc)
Definition: Principal.cc:813
const_iterator cend() const
Definition: Principal.cc:291
std::atomic< bool > processHistoryModified_
Definition: Principal.h:232
size_t findGroups(ProcessLookup const &, ModuleContext const &, SelectorBase const &, std::vector< cet::exempt_ptr< Group >> &groups) const
Definition: Principal.cc:461
virtual ~Principal() noexcept=default
std::map< std::string, std::vector< ProductID >> ProcessLookup
Definition: type_aliases.h:17
std::atomic< bool > enableLookupOfProducedProducts_
Definition: Principal.h:238
auto tryNextSecondaryFile() const
Definition: Principal.cc:544
GroupCollection groups_
Definition: Principal.h:246
const_iterator end() const
Definition: Principal.cc:284
Principal(BranchType, ProcessConfiguration const &, cet::exempt_ptr< ProductTable const > presentProducts, ProcessHistoryID const &id, std::unique_ptr< DelayedReader > &&reader=std::make_unique< NoDelayedReader >())
Definition: Principal.cc:119
std::vector< InputTag > getInputTags(ModuleContext const &mc, WrappedTypeID const &wrapped, SelectorBase const &, ProcessTag const &) const
Definition: Principal.cc:520
ProcessHistory processHistory_
Definition: Principal.h:231
void addToProcessHistory()
Definition: Principal.cc:423
intermediate_table::const_iterator const_iterator
size_t size() const
Definition: Principal.cc:263
std::vector< cet::exempt_ptr< Group > > matchingSequenceFromInputFile(ModuleContext const &, SelectorBase const &) const
Definition: Principal.cc:595
size_t findGroupsFromInputFile(ModuleContext const &, WrappedTypeID const &wrapped, SelectorBase const &, std::vector< cet::exempt_ptr< Group >> &results) const
Definition: Principal.cc:607
void updateSeenRanges(RangeSet const &rs)
Definition: Principal.cc:707
std::recursive_mutex groupMutex_
Definition: Principal.h:241
void readImmediate() const
Definition: Principal.cc:226
BranchType branchType_
Definition: Principal.h:230
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
GroupQueryResult getBySelector(ModuleContext const &mc, WrappedTypeID const &wrapped, SelectorBase const &, ProcessTag const &) const
Definition: Principal.cc:488
void createGroupsForProducedProducts(ProductTables const &producedProducts)
Definition: Principal.cc:201
RangeSet rangeSet_
Definition: Principal.h:266
bool presentFromSource(ProductID) const
Definition: Principal.cc:829
RangeSet seenRanges() const
Definition: Principal.cc:701
std::atomic< ProductTable const * > producedProducts_
Definition: Principal.h:237
OutputHandle getForOutput(ProductID const &, bool resolveProd) const
Definition: Principal.cc:750
cet::exempt_ptr< BranchDescription const > getProductDescription(ProductID const pid, bool const alwaysEnableLookupOfProducedProducts=false) const
Definition: Principal.cc:779
EDProductGetter const * productGetter(ProductID id) const
Definition: Principal.cc:177
ProcessHistoryID const & processHistoryID() const
Definition: Principal.h:148
EDProductGetter const * getEDProductGetter_(ProductID const &) const override
Definition: Principal.cc:195
void setProcessHistoryIDcombined(ProcessHistoryID const &)
void put(BranchDescription const &, std::unique_ptr< ProductProvenance const > &&, std::unique_ptr< EDProduct > &&, std::unique_ptr< RangeSet > &&)
Definition: Principal.cc:713
std::vector< cet::exempt_ptr< Group > > getMatchingSequence(ModuleContext const &, SelectorBase const &, ProcessTag const &) const
Definition: Principal.cc:550
std::atomic< ProductTable const * > presentProducts_
Definition: Principal.h:236
void ctor_fetch_process_history(ProcessHistoryID const &)
Definition: Principal.cc:109
std::vector< cet::exempt_ptr< Group > > findGroupsForProduct(ModuleContext const &mc, WrappedTypeID const &wrapped, SelectorBase const &, ProcessTag const &) const
Definition: Principal.cc:625
void ctor_create_groups(cet::exempt_ptr< ProductTable const >)
Definition: Principal.cc:68
ProcessHistory const & processHistory() const
Definition: Principal.cc:247
cet::exempt_ptr< ProductProvenance const > branchToProductProvenance(ProductID const &) const
Definition: Principal.cc:298
void enableLookupOfProducedProducts()
Definition: Principal.cc:220
ProcessConfiguration const & processConfiguration() const
Definition: Principal.cc:257
cet::exempt_ptr< Group > getGroupTryAllFiles(ProductID const) const
Definition: Principal.cc:860
int nextSecondaryFileIdx_
Definition: Principal.h:264
ProcessConfiguration const & processConfiguration_
Definition: Principal.h:233
static RangeSet invalid()
Definition: RangeSet.cc:45
void ctor_read_provenance()
Definition: Principal.cc:89
cet::exempt_ptr< Group > getGroupLocal(ProductID const) const
Definition: Principal.cc:852
BranchType
Definition: BranchType.h:20
std::vector< GroupQueryResult > getMany(ModuleContext const &mc, WrappedTypeID const &wrapped, SelectorBase const &, ProcessTag const &) const
Definition: Principal.cc:534
std::map< ProductID, std::unique_ptr< Group >> GroupCollection
Definition: Principal.h:51
Definition: MVAAlg.h:12
void fillGroup(BranchDescription const &)
Definition: Principal.cc:137
GroupQueryResult getByLabel(ModuleContext const &mc, WrappedTypeID const &wrapped, std::string const &label, std::string const &productInstanceName, ProcessTag const &processTag) const
Definition: Principal.cc:506
std::unique_ptr< DelayedReader > delayedReader_
Definition: Principal.h:250
void markProcessHistoryAsModified()
Definition: Principal.cc:170
GroupQueryResult getByProductID(ProductID const pid) const
Definition: Principal.cc:839
std::vector< std::unique_ptr< Principal > > secondaryPrincipals_
Definition: Principal.h:260
GroupCollection::const_iterator const_iterator
Definition: Principal.h:52
ProcessHistoryID const & id() const
bool producedInProcess(ProductID) const
Definition: Principal.cc:819
const_iterator cbegin() const
Definition: Principal.cc:277
size_t findGroupsForProcess(std::vector< ProductID > const &vpid, ModuleContext const &mc, SelectorBase const &selector, std::vector< cet::exempt_ptr< Group >> &groups) const
Definition: Principal.cc:667
const_iterator begin() const
Definition: Principal.cc:270
BranchType branchType() const
Definition: Principal.cc:807
Provenance provenance(ProductID id) const
Definition: Principal.cc:189