LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
MixOp.h
Go to the documentation of this file.
1 #ifndef art_Framework_IO_ProductMix_MixOp_h
2 #define art_Framework_IO_ProductMix_MixOp_h
3 // vim: set sw=2 expandtab :
4 
5 // Template encapsulating all the attributes and functionality of a
6 // product mixing operation.
7 
12 #include "art/Utilities/Globals.h"
19 #include "cetlib/exempt_ptr.h"
20 
21 #include <algorithm>
22 #include <cstdlib>
23 #include <functional>
24 #include <iostream>
25 #include <memory>
26 
27 namespace art {
28 
29  template <typename Prod, typename OProd>
30  class MixOp : public MixOpBase {
31  public:
32  template <typename FUNC>
33  MixOp(std::string const& moduleLabel,
34  InputTag const& inputTag,
35  std::string const& outputInstanceLabel,
36  FUNC mixFunc,
37  bool outputProduct,
38  bool compactMissingProducts,
39  BranchType bt);
40 
41  InputTag const&
42  inputTag() const override
43  {
44  return inputTag_;
45  }
46  TypeID
47  inputType() const override
48  {
49  return inputType_;
50  }
51  EDProduct const*
52  newIncomingWrappedProduct() const override
53  {
54  return new Wrapper<Prod>{};
55  }
56  ProductID incomingProductID() const override;
57  ProductID outgoingProductID() const override;
58  BranchType branchType() const override;
59 
60  private:
61  void mixAndPut(Event& e,
62  SpecProdList const& inProducts,
63  PtrRemapper const& remap) const override;
64  void setIncomingProductID(ProductID) override;
65 
67  std::string const outputInstanceLabel_;
70  std::string const processName_;
71  std::string const moduleLabel_;
72  bool const outputProduct_;
76  };
77 
78  template <typename Prod, typename OProd>
79  template <typename FUNC>
80  MixOp<Prod, OProd>::MixOp(std::string const& moduleLabel,
81  InputTag const& inputTag,
82  std::string const& outputInstanceLabel,
83  FUNC mixFunc,
84  bool const outputProduct,
85  bool const compactMissingProducts,
86  BranchType const bt)
87  : inputTag_{inputTag}
88  , outputInstanceLabel_{outputInstanceLabel}
89  , inputType_{typeid(Prod)}
90  , mixFunc_{mixFunc}
92  , moduleLabel_{moduleLabel}
93  , outputProduct_{outputProduct}
94  , compactMissingProducts_{compactMissingProducts}
95  , branchType_{bt}
96  {}
97 
98  template <typename Prod, typename OProd>
99  void
101  SpecProdList const& inProducts,
102  PtrRemapper const& remap) const
103  {
104  std::vector<Prod const*> inConverted;
105  inConverted.reserve(inProducts.size());
106  try {
107  for (auto const& ep : inProducts) {
108  auto const prod =
109  std::dynamic_pointer_cast<Wrapper<Prod> const>(ep)->product();
110  if (prod || !compactMissingProducts_) {
111  inConverted.emplace_back(prod);
112  }
113  }
114  }
115  catch (std::bad_cast const&) {
117  << "Unable to obtain correctly-typed product from wrapper.\n";
118  }
119 
120  auto rProd = std::make_unique<OProd>();
121  // False means don't want this in the event.
122  if (mixFunc_(inConverted, *rProd, remap)) {
123  if (!outputProduct_) {
125  << "Returned true (output product to be put in event) from a mix "
126  "function\n"
127  << "declared with outputProduct=false.\n";
128  }
129  e.put(std::move(rProd), outputInstanceLabel_);
130  }
131  }
132 
133  template <typename Prod, typename OProd>
134  void
136  {
137  incomingProductID_ = prodID;
138  }
139 
140  template <typename Prod, typename OProd>
141  ProductID
143  {
144  return incomingProductID_;
145  }
146 
147  template <typename Prod, typename OProd>
148  ProductID
150  {
151  ProductID result;
152  if (outputProduct_) {
153  TypeID const outputType{typeid(OProd)};
154  // Note: Outgoing product must be InEvent.
155  auto const productName =
156  canonicalProductName(outputType.friendlyClassName(),
157  moduleLabel_,
159  processName_);
160  result = ProductID{productName};
161  }
162  return result;
163  }
164 
165  template <typename Prod, typename OProd>
166  inline BranchType
168  {
169  return branchType_;
170  }
171 
172 } // namespace art
173 
174 #endif /* art_Framework_IO_ProductMix_MixOp_h */
175 
176 // Local Variables:
177 // mode: c++
178 // End:
BranchType branchType() const override
Definition: MixOp.h:167
std::string const moduleLabel_
Definition: MixOp.h:71
ProductID incomingProductID_
Definition: MixOp.h:75
BranchType const branchType_
Definition: MixOp.h:74
ProductID incomingProductID() const override
Definition: MixOp.h:142
InputTag const & inputTag() const override
Definition: MixOp.h:42
std::vector< std::shared_ptr< EDProduct const >> SpecProdList
Definition: MixTypes.h:21
bool const outputProduct_
Definition: MixOp.h:72
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
EDProduct const * newIncomingWrappedProduct() const override
Definition: MixOp.h:52
std::string const outputInstanceLabel_
Definition: MixOp.h:67
MixOp(std::string const &moduleLabel, InputTag const &inputTag, std::string const &outputInstanceLabel, FUNC mixFunc, bool outputProduct, bool compactMissingProducts, BranchType bt)
Definition: MixOp.h:80
InputTag const inputTag_
Definition: MixOp.h:66
std::function< bool(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &)> MixFunc
Definition: MixTypes.h:19
std::string canonicalProductName(std::string const &friendlyClassName, std::string const &moduleLabel, std::string const &productInstanceName, std::string const &processName)
bool const compactMissingProducts_
Definition: MixOp.h:73
std::string const processName_
Definition: MixOp.h:70
MixFunc< Prod, OProd > const mixFunc_
Definition: MixOp.h:69
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
TypeID inputType() const override
Definition: MixOp.h:47
std::string const & processName() const
Definition: Globals.cc:48
TypeID const inputType_
Definition: MixOp.h:68
static constexpr ProductID invalid() noexcept
Definition: ProductID.h:26
void mixAndPut(Event &e, SpecProdList const &inProducts, PtrRemapper const &remap) const override
Definition: MixOp.h:100
BranchType
Definition: BranchType.h:20
Definition: MVAAlg.h:12
static Globals * instance()
Definition: Globals.cc:17
Float_t e
Definition: plot.C:35
ProductID outgoingProductID() const override
Definition: MixOp.h:149
void setIncomingProductID(ProductID) override
Definition: MixOp.h:135