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 
42 #include "cetlib/exempt_ptr.h"
43 
44 #include <atomic>
45 #include <memory>
46 #include <mutex>
47 #include <string>
48 #include <vector>
49 
50 namespace art {
51 
52  class EDProduct;
53 
54  class Principal : public PrincipalBase {
55  public:
56  using GroupCollection = std::map<ProductID, std::unique_ptr<Group>>;
59 
60  // The destructor is defined in the header so that overrides of
61  // DelayedReader's readFromSecondaryFile_ virtual function can
62  // return an std::unique_ptr<Principal> object (std::unique_ptr
63  // instantiations require a well-formed deleter).
64  virtual ~Principal() noexcept
65  {
66  presentProducts_ = nullptr;
67  producedProducts_ = nullptr;
68  delete eventAux_.load();
69  }
70 
72  ProcessConfiguration const&,
74  ProcessHistoryID const&,
75  std::unique_ptr<DelayedReader>&&);
76 
77  // Run
78  Principal(RunAuxiliary const&,
79  ProcessConfiguration const&,
81  std::unique_ptr<DelayedReader>&& reader =
82  std::make_unique<NoDelayedReader>());
83 
84  // SubRun
86  ProcessConfiguration const&,
88  std::unique_ptr<DelayedReader>&& reader =
89  std::make_unique<NoDelayedReader>());
90 
91  // Event
93  ProcessConfiguration const&,
95  std::unique_ptr<History>&& history = std::make_unique<History>(),
96  std::unique_ptr<DelayedReader>&& reader =
97  std::make_unique<NoDelayedReader>(),
98  bool lastInSubRun = false);
99 
100  // Results
102  ProcessConfiguration const&,
103  cet::exempt_ptr<ProductTable const> presentProducts,
104  std::unique_ptr<DelayedReader>&& reader =
105  std::make_unique<NoDelayedReader>());
106 
107  // Disable copying and moving.
108  Principal(Principal const&) = delete;
109  Principal& operator=(Principal const&) = delete;
110 
111  // Interface for DataViewImpl<T>
112  //
113  // - Used by art::DataViewImpl<T>::get(ProductID const pid,
114  // Handle<T>& result) const. (easy user-facing api) Used by
115  // Principal::productGetter(ProductID const pid) const
116  //
117  // - Used by (Run,SubRun,Event,Results)::productGetter (advanced
118  // user-facing api)
120 
122  WrappedTypeID const& wrapped,
123  SelectorBase const&,
124  ProcessTag const&) const;
126  WrappedTypeID const& wrapped,
127  std::string const& label,
128  std::string const& productInstanceName,
129  ProcessTag const& processTag) const;
130  std::vector<GroupQueryResult> getMany(ModuleContext const& mc,
131  WrappedTypeID const& wrapped,
132  SelectorBase const&,
133  ProcessTag const&) const;
134 
135  std::vector<InputTag> getInputTags(ModuleContext const& mc,
136  WrappedTypeID const& wrapped,
137  SelectorBase const&,
138  ProcessTag const&) const;
139 
140  // Used only by DataViewImpl<T> to implement getView.
141  // FIXME: Return a vector of GroupQueryResult to products which
142  // are sequences, have a nested type named 'value_type',
143  // and where elementType the same as, or a public base of,
144  // this value_type, and which match the given selector.
145  std::vector<cet::exempt_ptr<Group>> getMatchingSequence(
146  ModuleContext const&,
147  SelectorBase const&,
148  ProcessTag const&) const;
149 
150  // Note: LArSoft uses this extensively to create a Ptr by hand.
151  EDProductGetter const* productGetter(ProductID const& pid) const;
152 
153  ProcessHistory const& processHistory() const;
154 
155  // This is intended to be used by a module that fetches a very
156  // large data product, makes a copy, and would like to release
157  // the memory held by the original immediately.
158  void removeCachedProduct(ProductID) const;
159 
160  // Interface for other parts of art
161 
162  // Note: We invoke the delay reader if no user module has fetched
163  // them for this principal if resolvedProd is true. This
164  // attempts to resolved the product and converts the
165  // resulting group into an OutputHandle.
166  OutputHandle getForOutput(ProductID const&, bool resolveProd) const;
167 
168  // Used to provide access to the product descriptions
170  ProductID const pid,
171  bool const alwaysEnableLookupOfProducedProducts = false) const;
172 
173  // The product tables data member for produced products is set by
174  // the EventProcessor after the Principal is provided by the input
175  // source.
176  void createGroupsForProducedProducts(ProductTables const& producedProducts);
177  void enableLookupOfProducedProducts(ProductTables const& producedProducts);
178 
179  // FIXME: This breaks the purpose of the
180  // Principal::addToProcessHistory() compare_exchange_strong
181  // because of the temporal hole between when the history is
182  // changed and when the flag is set, this must be fixed!
184 
185  // Read all data products and provenance immediately, if available.
186  void readImmediate() const;
187 
189 
191  ProductID const&) const;
192 
193  size_t size() const;
194 
195  const_iterator begin() const;
196  const_iterator cbegin() const;
197 
198  const_iterator end() const;
199  const_iterator cend() const;
200 
201  // Flag that we have been updated in the current process.
202  void addToProcessHistory();
203 
204  BranchType branchType() const;
205 
206  RangeSet seenRanges() const;
207 
208  void put(BranchDescription const&,
209  std::unique_ptr<ProductProvenance const>&&,
210  std::unique_ptr<EDProduct>&&,
211  std::unique_ptr<RangeSet>&&);
212 
213  // Used to be in subclasses
214  RunAuxiliary const& runAux() const;
215  SubRunAuxiliary const& subRunAux() const;
216  EventAuxiliary const& eventAux() const;
217  ResultsAuxiliary const& resultsAux() const;
218 
219  RunID const& runID() const;
220  SubRunID subRunID() const;
221  EventID const& eventID() const;
222 
223  RunNumber_t run() const;
224  SubRunNumber_t subRun() const;
225  EventNumber_t event() const;
226 
227  Timestamp const& beginTime() const;
228  Timestamp const& endTime() const;
229 
230  void endTime(Timestamp const& time);
231  Timestamp const& time() const;
232 
233  // Used by EndPathExecutor
234  void updateSeenRanges(RangeSet const& rs);
235 
236  RunPrincipal const& runPrincipal() const;
237  SubRunPrincipal const& subRunPrincipal() const;
239  SubRunPrincipal const* subRunPrincipalPtr() const;
243  bool isReal() const;
245  History const& history() const;
246  bool isLastInSubRun() const;
247 
248  private:
249  // Used by our ctors.
251  void ctor_read_provenance();
253 
255 
256  std::vector<cet::exempt_ptr<Group>> matchingSequenceFromInputFile(
257  ModuleContext const&,
258  SelectorBase const&) const;
260  ModuleContext const&,
261  WrappedTypeID const& wrapped,
262  SelectorBase const&,
263  std::vector<cet::exempt_ptr<Group>>& results) const;
264  size_t findGroups(ProcessLookup const&,
265  ModuleContext const&,
266  SelectorBase const&,
267  std::vector<cet::exempt_ptr<Group>>& groups) const;
268  size_t findGroupsForProcess(
269  std::vector<ProductID> const& vpid,
270  ModuleContext const& mc,
271  SelectorBase const& selector,
272  std::vector<cet::exempt_ptr<Group>>& groups) const;
273  bool producedInProcess(ProductID) const;
274  bool presentFromSource(ProductID) const;
275  auto tryNextSecondaryFile() const;
276 
277  // Implementation of the DataViewImpl API.
278  std::vector<cet::exempt_ptr<Group>> findGroupsForProduct(
279  ModuleContext const& mc,
280  WrappedTypeID const& wrapped,
281  SelectorBase const&,
282  ProcessTag const&) const;
283 
284  EDProductGetter const* getEDProductGetter_(ProductID const&) const override;
285 
287 
288  protected:
289  // Used to deal with TriggerResults.
290  void fillGroup(BranchDescription const&);
291 
292  // Used by addToProcessHistory()
294 
295  private:
298  std::atomic<bool> processHistoryModified_;
300 
301  // Product-lookup tables
302  std::atomic<ProductTable const*> presentProducts_;
303  std::atomic<ProductTable const*> producedProducts_;
305 
306  // Protects access to groups_.
307  mutable std::recursive_mutex groupMutex_{};
308 
309  // All of the currently known data products.
310  // tbb::concurrent_unordered_map<ProductID, std::unique_ptr<Group>>
311  // groups_{};
313 
314  // Pointer to the reader that will be used to obtain
315  // EDProducts from the persistent store.
316  std::unique_ptr<DelayedReader> delayedReader_{nullptr};
317 
318  // Secondary principals. Note that the lifetimes of Results, Run,
319  // and SubRun principals do not exceed the lifetime of the input
320  // file.
321  //
322  // Note: To make secondary file-reading thread-safe, we will need to
323  // ensure that any adjustments to the secondaryPrincipals_
324  // vector is done atomically with respect to any reading of
325  // the vector.
326  mutable std::vector<std::unique_ptr<Principal>> secondaryPrincipals_{};
327 
328  // Index into the secondary file names vector of the next
329  // file that a secondary principal should be created from.
330  mutable int nextSecondaryFileIdx_{};
331 
335 
336  // We own.
337  std::atomic<EventAuxiliary*> eventAux_;
338 
341  std::atomic<SubRunPrincipal const*> subRunPrincipal_;
342  std::unique_ptr<History> history_{nullptr};
343  bool lastInSubRun_{false};
344  };
345 
346 } // namespace art
347 
348 // Local Variables:
349 // mode: c++
350 // End:
351 #endif /* art_Framework_Principal_Principal_h */
Principal & operator=(Principal const &)=delete
const_iterator cend() const
Definition: Principal.cc:407
std::atomic< bool > processHistoryModified_
Definition: Principal.h:298
cet::exempt_ptr< RunPrincipal const > runPrincipalExemptPtr() const
Definition: Principal.cc:854
size_t findGroups(ProcessLookup const &, ModuleContext const &, SelectorBase const &, std::vector< cet::exempt_ptr< Group >> &groups) const
Definition: Principal.cc:607
RunNumber_t run() const
Definition: Principal.cc:1070
EventID const & eventID() const
Definition: Principal.cc:1064
EDProductGetter const * productGetter(ProductID const &pid) const
Definition: Principal.cc:297
std::map< std::string, std::vector< ProductID >> ProcessLookup
Definition: type_aliases.h:17
std::atomic< bool > enableLookupOfProducedProducts_
Definition: Principal.h:304
Principal(BranchType, ProcessConfiguration const &, cet::exempt_ptr< ProductTable const > presentProducts, ProcessHistoryID const &, std::unique_ptr< DelayedReader > &&)
Definition: Principal.cc:124
RunID const & runID() const
Definition: Principal.cc:1052
auto tryNextSecondaryFile() const
Definition: Principal.cc:690
GroupCollection groups_
Definition: Principal.h:312
const_iterator end() const
Definition: Principal.cc:400
std::vector< InputTag > getInputTags(ModuleContext const &mc, WrappedTypeID const &wrapped, SelectorBase const &, ProcessTag const &) const
Definition: Principal.cc:666
Timestamp const & time() const
Definition: Principal.cc:1125
std::string string
Definition: nybbler.cc:12
ProcessHistory processHistory_
Definition: Principal.h:297
Timestamp const & beginTime() const
Definition: Principal.cc:1097
SubRunPrincipal const * subRunPrincipalPtr() const
Definition: Principal.cc:860
EventAuxiliary::ExperimentType ExperimentType() const
Definition: Principal.cc:896
struct vector vector
History const & history() const
Definition: Principal.cc:902
void addToProcessHistory()
Definition: Principal.cc:563
intermediate_table::const_iterator const_iterator
size_t size() const
Definition: Principal.cc:379
std::vector< cet::exempt_ptr< Group > > matchingSequenceFromInputFile(ModuleContext const &, SelectorBase const &) const
Definition: Principal.cc:741
SubRunAuxiliary const & subRunAux() const
Definition: Principal.cc:1034
std::atomic< SubRunPrincipal const * > subRunPrincipal_
Definition: Principal.h:341
size_t findGroupsFromInputFile(ModuleContext const &, WrappedTypeID const &wrapped, SelectorBase const &, std::vector< cet::exempt_ptr< Group >> &results) const
Definition: Principal.cc:753
void updateSeenRanges(RangeSet const &rs)
Definition: Principal.cc:884
std::recursive_mutex groupMutex_
Definition: Principal.h:307
void readImmediate() const
Definition: Principal.cc:341
BranchType branchType_
Definition: Principal.h:296
bool isLastInSubRun() const
Definition: Principal.cc:924
std::vector< EventSelectionID > EventSelectionIDVector
EventNumber_t event() const
Definition: Principal.cc:1091
GroupQueryResult getBySelector(ModuleContext const &mc, WrappedTypeID const &wrapped, SelectorBase const &, ProcessTag const &) const
Definition: Principal.cc:634
void createGroupsForProducedProducts(ProductTables const &producedProducts)
Definition: Principal.cc:314
RangeSet rangeSet_
Definition: Principal.h:332
bool presentFromSource(ProductID) const
Definition: Principal.cc:1141
RangeSet seenRanges() const
Definition: Principal.cc:878
void removeCachedProduct(ProductID) const
Definition: Principal.cc:417
std::atomic< ProductTable const * > producedProducts_
Definition: Principal.h:303
std::unique_ptr< History > history_
Definition: Principal.h:342
OutputHandle getForOutput(ProductID const &, bool resolveProd) const
Definition: Principal.cc:965
cet::exempt_ptr< BranchDescription const > getProductDescription(ProductID const pid, bool const alwaysEnableLookupOfProducedProducts=false) const
Definition: Principal.cc:994
EDProductGetter const * getEDProductGetter_(ProductID const &) const override
Definition: Principal.cc:308
void setProcessHistoryIDcombined(ProcessHistoryID const &)
Definition: Principal.cc:272
SubRunID subRunID() const
Definition: Principal.cc:1058
void put(BranchDescription const &, std::unique_ptr< ProductProvenance const > &&, std::unique_ptr< EDProduct > &&, std::unique_ptr< RangeSet > &&)
Definition: Principal.cc:930
IDNumber_t< Level::SubRun > SubRunNumber_t
Definition: IDNumber.h:119
bool lastInSubRun_
Definition: Principal.h:343
virtual ~Principal() noexcept
Definition: Principal.h:64
void setRunPrincipal(cet::exempt_ptr< RunPrincipal const > rp)
Definition: Principal.cc:866
void enableLookupOfProducedProducts(ProductTables const &producedProducts)
Definition: Principal.cc:334
std::vector< cet::exempt_ptr< Group > > getMatchingSequence(ModuleContext const &, SelectorBase const &, ProcessTag const &) const
Definition: Principal.cc:696
std::atomic< ProductTable const * > presentProducts_
Definition: Principal.h:302
void setSubRunPrincipal(cet::exempt_ptr< SubRunPrincipal const > srp)
Definition: Principal.cc:872
void ctor_fetch_process_history(ProcessHistoryID const &)
Definition: Principal.cc:114
std::vector< cet::exempt_ptr< Group > > findGroupsForProduct(ModuleContext const &mc, WrappedTypeID const &wrapped, SelectorBase const &, ProcessTag const &) const
Definition: Principal.cc:771
RunAuxiliary const & runAux() const
Definition: Principal.cc:1028
void ctor_create_groups(cet::exempt_ptr< ProductTable const >)
Definition: Principal.cc:72
ProcessHistory const & processHistory() const
Definition: Principal.cc:363
cet::exempt_ptr< ProductProvenance const > branchToProductProvenance(ProductID const &) const
Definition: Principal.cc:438
ProcessConfiguration const & processConfiguration() const
Definition: Principal.cc:373
RunPrincipal const & runPrincipal() const
Definition: Principal.cc:914
int nextSecondaryFileIdx_
Definition: Principal.h:330
ResultsAuxiliary const & resultsAux() const
Definition: Principal.cc:1046
bool isReal() const
Definition: Principal.cc:890
ProcessConfiguration const & processConfiguration_
Definition: Principal.h:299
std::atomic< EventAuxiliary * > eventAux_
Definition: Principal.h:337
static RangeSet invalid()
Definition: RangeSet.cc:45
void ctor_read_provenance()
Definition: Principal.cc:93
EventAuxiliary const & eventAux() const
Definition: Principal.cc:1040
cet::exempt_ptr< Group > getGroupLocal(ProductID const) const
Definition: Principal.cc:1164
IDNumber_t< Level::Event > EventNumber_t
Definition: IDNumber.h:118
BranchType
Definition: BranchType.h:20
std::vector< GroupQueryResult > getMany(ModuleContext const &mc, WrappedTypeID const &wrapped, SelectorBase const &, ProcessTag const &) const
Definition: Principal.cc:680
std::map< ProductID, std::unique_ptr< Group >> GroupCollection
Definition: Principal.h:56
void fillGroup(BranchDescription const &)
Definition: Principal.cc:242
RunAuxiliary runAux_
Definition: Principal.h:333
SubRunPrincipal const & subRunPrincipal() const
Definition: Principal.cc:844
GroupQueryResult getByLabel(ModuleContext const &mc, WrappedTypeID const &wrapped, std::string const &label, std::string const &productInstanceName, ProcessTag const &processTag) const
Definition: Principal.cc:652
std::unique_ptr< DelayedReader > delayedReader_
Definition: Principal.h:316
void markProcessHistoryAsModified()
Definition: Principal.cc:290
ResultsAuxiliary resultsAux_
Definition: Principal.h:339
GroupQueryResult getByProductID(ProductID const pid) const
Definition: Principal.cc:1151
std::vector< std::unique_ptr< Principal > > secondaryPrincipals_
Definition: Principal.h:326
GroupCollection::const_iterator const_iterator
Definition: Principal.h:57
SubRunAuxiliary subRunAux_
Definition: Principal.h:334
Timestamp const & endTime() const
Definition: Principal.cc:1106
bool producedInProcess(ProductID) const
Definition: Principal.cc:1131
cet::exempt_ptr< Group const > getGroupTryAllFiles(ProductID const) const
Definition: Principal.cc:1172
const_iterator cbegin() const
Definition: Principal.cc:393
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:813
SubRunNumber_t subRun() const
Definition: Principal.cc:1082
const_iterator begin() const
Definition: Principal.cc:386
BranchType branchType() const
Definition: Principal.cc:1022
cet::exempt_ptr< RunPrincipal const > runPrincipal_
Definition: Principal.h:340
EventSelectionIDVector const & eventSelectionIDs() const
Definition: Principal.cc:908
IDNumber_t< Level::Run > RunNumber_t
Definition: IDNumber.h:120