LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ProductInserter.h
Go to the documentation of this file.
1 #ifndef art_Framework_Principal_ProductInserter_h
2 #define art_Framework_Principal_ProductInserter_h
3 // vim: set sw=2 expandtab :
4 
21 #include "cetlib/HorizontalRule.h"
22 #include "cetlib/exempt_ptr.h"
23 #include "cetlib_except/exception.h"
24 
25 #include <cassert>
26 #include <cstddef>
27 #include <map>
28 #include <memory>
29 #include <mutex>
30 #include <set>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace art {
36 
38  public:
40  explicit ProductInserter(BranchType bt,
41  Principal& p,
42  ModuleContext const& mc);
43 
44  ProductInserter(ProductInserter const&) = delete;
45  ProductInserter& operator=(ProductInserter const&) = delete;
46  ProductInserter(ProductInserter&&) = default;
48 
49  // Product insertion - all processing levels
50  template <typename PROD>
51  PutHandle<PROD> put(std::unique_ptr<PROD>&& edp,
52  std::string const& instance = {});
53 
54  // Product insertion -- run/subrun
55  template <typename PROD>
56  PutHandle<PROD> put(std::unique_ptr<PROD>&& edp,
57  std::string const& instance,
58  RangeSet const& rs);
59 
60  void commitProducts();
61  void commitProducts(
62  bool checkProducts,
63  std::map<TypeLabel, BranchDescription> const* expectedProducts,
64  std::vector<ProductID> retrievedPIDs);
65 
66  private:
67  struct PMValue {
68  std::unique_ptr<EDProduct> product;
71  };
72 
74  TypeID const& type,
75  std::string const& instance,
76  bool const alwaysEnableLookupOfProducedProducts = false) const;
77 
78  EDProductGetter const* productGetter_(ProductID id) const;
80 
81  // Protects use of retrievedProducts_ and putProducts_.
82  mutable std::unique_ptr<std::recursive_mutex> mutex_{
83  std::make_unique<std::recursive_mutex>()};
84 
85  // Is this an Event, a Run, a SubRun, or a Results.
87 
88  // The principal we are operating on.
90 
91  // The module we were created for.
93 
94  // The set of products which have been put by the user.
95  std::map<TypeLabel, PMValue> putProducts_{};
96  };
97 
98  // =======================================================================
99  // Product-insertion implementation
100  template <typename PROD>
102  ProductInserter::put(std::unique_ptr<PROD>&& edp, std::string const& instance)
103  {
104  return put(std::move(edp), instance, RangeSet::invalid());
105  }
106 
107  template <typename PROD>
109  ProductInserter::put(std::unique_ptr<PROD>&& edp,
110  std::string const& instance,
111  RangeSet const& rs)
112  {
113  TypeID const tid{typeid(PROD)};
114  if (!edp) {
116  << "A null unique_ptr was passed to 'put'.\n"
117  << "The pointer is of type " << tid << ".\n"
118  << "The specified productInstanceName was '" << instance << "'.\n";
119  }
120 
121  std::lock_guard lock{*mutex_};
122  auto const& bd = getProductDescription_(tid, instance, true);
123  assert(bd.productID() != ProductID::invalid());
124  auto const typeLabel =
126  auto wp = std::make_unique<Wrapper<PROD>>(std::move(edp));
127 
128  // Mind the product ownership! The wrapper is the final resting
129  // place of the product before it is taken out of memory.
130  cet::exempt_ptr<PROD const> product{wp->product()};
131  auto result =
132  putProducts_.try_emplace(typeLabel, PMValue{std::move(wp), bd, rs})
133  .second;
134  if (!result) {
135  constexpr cet::HorizontalRule rule{30};
137  << "Attempt to put multiple products with the following descriptions.\n"
138  << "Each product must be unique.\n"
139  << rule('=') << '\n'
140  << bd << rule('=') << '\n';
141  }
142  return PutHandle{
143  product.get(), productGetter_(bd.productID()), bd.productID()};
144  }
145 
146 } // namespace art
147 
148 #endif /* art_Framework_Principal_ProductInserter_h */
149 
150 // Local Variables:
151 // mode: c++
152 // End:
ProductInserter(BranchType bt, Principal &p, ModuleContext const &mc)
EDProductGetter const * productGetter_(ProductID id) const
BranchDescription const & description
TypeLabel type_label_for(TypeID const typeID, std::string const &instance, bool const supportsView, ModuleDescription const &md)
const std::string instance
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
ProductInserter & operator=(ProductInserter const &)=delete
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
BranchDescription const & getProductDescription_(TypeID const &type, std::string const &instance, bool const alwaysEnableLookupOfProducedProducts=false) const
std::unique_ptr< EDProduct > product
std::map< TypeLabel, PMValue > putProducts_
std::unique_ptr< std::recursive_mutex > mutex_
static constexpr ProductID invalid() noexcept
Definition: ProductID.h:26
static RangeSet invalid()
Definition: RangeSet.cc:45
BranchType
Definition: BranchType.h:20
Definition: MVAAlg.h:12
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:82
Provenance provenance_(ProductID id) const
ModuleDescription const * md_