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