CMS 3D CMS Logo

TransformerBase.cc
Go to the documentation of this file.
9 
12 #include <optional>
13 
14 namespace {
15  class TransformSignalSentry {
16  public:
17  TransformSignalSentry(edm::ActivityRegistry* a, edm::StreamContext const& sc, edm::ModuleCallingContext const& mcc)
18  : a_(a), sc_(sc), mcc_(mcc) {
19  if (a_)
20  a_->preModuleTransformSignal_(sc_, mcc_);
21  }
22  ~TransformSignalSentry() {
23  if (a_)
24  a_->postModuleTransformSignal_(sc_, mcc_);
25  }
26 
27  private:
28  edm::ActivityRegistry* a_; // We do not use propagate_const because the registry itself is mutable.
29  edm::StreamContext const& sc_;
30  edm::ModuleCallingContext const& mcc_;
31  };
32 
33  class TransformAcquiringSignalSentry {
34  public:
35  TransformAcquiringSignalSentry(edm::ActivityRegistry* a,
36  edm::StreamContext const& sc,
37  edm::ModuleCallingContext const& mcc)
38  : a_(a), sc_(sc), mcc_(mcc) {
39  if (a_)
40  a_->preModuleTransformAcquiringSignal_(sc_, mcc_);
41  }
42  ~TransformAcquiringSignalSentry() {
43  if (a_)
44  a_->postModuleTransformAcquiringSignal_(sc_, mcc_);
45  }
46 
47  private:
48  edm::ActivityRegistry* a_; // We do not use propagate_const because the registry itself is mutable.
49  edm::StreamContext const& sc_;
50  edm::ModuleCallingContext const& mcc_;
51  };
52 
53 } // namespace
54 
55 namespace edm {
57  ProducerBase& iBase, EDPutToken iToken, const TypeID& id, std::string instanceName, TransformFunction iFunc) {
58  auto transformPut = iBase.transforms(id, std::move(instanceName));
60  transformInfo_.emplace_back(iToken.index(), id, transformPut, std::move(ptf), std::move(iFunc));
61  }
62 
64  EDPutToken iToken,
65  const TypeID& id,
66  std::string instanceName,
67  PreTransformFunction iPreFunc,
68  TransformFunction iFunc) {
69  auto transformPut = iBase.transforms(id, std::move(instanceName));
70  transformInfo_.emplace_back(iToken.index(), id, transformPut, std::move(iPreFunc), std::move(iFunc));
71  }
72 
74  edm::BranchDescription const& iBranch) const noexcept {
75  auto const& list = iBase.typeLabelList();
76 
77  std::size_t index = 0;
78  bool found = false;
79  for (auto const& element : list) {
80  if (not element.isTransform_) {
81  continue;
82  }
83  if (element.typeID_ == iBranch.unwrappedTypeID() &&
84  element.productInstanceName_ == iBranch.productInstanceName()) {
85  found = true;
86  break;
87  }
88  ++index;
89  }
90  assert(found);
91  return index;
92  }
93 
95  ModuleDescription const& iModuleDesc,
96  ProductResolverIndexHelper const& iHelper) {
97  auto const& list = iBase.typeLabelList();
98 
99  for (auto it = transformInfo_.begin<0>(); it != transformInfo_.end<0>(); ++it) {
100  auto const& putInfo = list[*it];
101  *it = iHelper.index(PRODUCT_TYPE,
102  putInfo.typeID_,
103  iModuleDesc.moduleLabel().c_str(),
104  putInfo.productInstanceName_.c_str(),
105  iModuleDesc.processName().c_str());
106  }
107  }
108 
110  std::size_t iIndex,
111  edm::ActivityRegistry* iAct,
112  ProducerBase const& iBase,
113  edm::EventForTransformer& iEvent) const noexcept {
114  auto const& mcc = iEvent.moduleCallingContext();
115  if (transformInfo_.get<kPreTransform>(iIndex)) {
116  std::optional<decltype(iEvent.get(transformInfo_.get<kType>(iIndex), transformInfo_.get<kResolverIndex>(iIndex)))>
117  handle;
118  //transform acquiring signal
119  TransformAcquiringSignalSentry sentry(iAct, *mcc.getStreamContext(), mcc);
120  CMS_SA_ALLOW try {
121  handle = iEvent.get(transformInfo_.get<kType>(iIndex), transformInfo_.get<kResolverIndex>(iIndex));
122  } catch (...) {
123  iHolder.doneWaiting(std::current_exception());
124  return;
125  }
126  if (handle->wrapper()) {
127  auto cache = std::make_shared<std::any>();
128  auto nextTask =
129  edm::make_waiting_task([holder = iHolder, cache, iIndex, this, &iBase, handle = *handle, iEvent, iAct](
130  std::exception_ptr const* iPtr) mutable {
131  if (iPtr) {
132  holder.doneWaiting(*iPtr);
133  } else {
134  //transform signal
135  auto mcc = iEvent.moduleCallingContext();
136  TransformSignalSentry sentry(iAct, *mcc.getStreamContext(), mcc);
137  iEvent.put(iBase.putTokenIndexToProductResolverIndex()[transformInfo_.get<kToken>(iIndex).index()],
138  transformInfo_.get<kTransform>(iIndex)(std::move(*cache)),
139  handle);
140  }
141  });
142  WaitingTaskWithArenaHolder wta(*iHolder.group(), nextTask);
143  CMS_SA_ALLOW try {
144  *cache = transformInfo_.get<kPreTransform>(iIndex)(*(handle->wrapper()), wta);
145  } catch (...) {
146  wta.doneWaiting(std::current_exception());
147  }
148  }
149  } else {
150  CMS_SA_ALLOW try {
151  auto handle = iEvent.get(transformInfo_.get<kType>(iIndex), transformInfo_.get<kResolverIndex>(iIndex));
152 
153  if (handle.wrapper()) {
154  std::any v = handle.wrapper();
155  //transform signal
156  TransformSignalSentry sentry(iAct, *mcc.getStreamContext(), mcc);
157  iEvent.put(iBase.putTokenIndexToProductResolverIndex()[transformInfo_.get<kToken>(iIndex).index()],
158  transformInfo_.get<kTransform>(iIndex)(std::move(v)),
159  handle);
160  }
161  } catch (...) {
162  iHolder.doneWaiting(std::current_exception());
163  }
164  }
165  }
166 
167 } // namespace edm
std::function< std::any(edm::WrapperBase const &, edm::WaitingTaskWithArenaHolder)> PreTransformFunction
#define CMS_SA_ALLOW
SoATuple< ProductResolverIndex, TypeID, EDPutToken, PreTransformFunction, TransformFunction > transformInfo_
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:37
EDPutToken transforms(const TypeID &id, std::string instanceName)
static const std::string kType("type")
assert(be >=bs)
int iEvent
Definition: GenABIO.cc:224
TypeLabelList const & typeLabelList() const
used by the fwk to register the list of products of this module
ProductResolverIndex index(KindOfType kindOfType, TypeID const &typeID, char const *moduleLabel, char const *instance, char const *process=nullptr) const
void extendUpdateLookup(ProducerBase const &, ModuleDescription const &iModuleDesc, ProductResolverIndexHelper const &iHelper)
std::function< std::unique_ptr< edm::WrapperBase >(std::any)> TransformFunction
FunctorWaitingTask< F > * make_waiting_task(F f)
Definition: WaitingTask.h:92
optional
Definition: Types.py:245
std::size_t findMatchingIndex(ProducerBase const &iBase, edm::BranchDescription const &) const noexcept
constexpr value_type index() const noexcept
Definition: EDPutToken.h:50
StreamContext const * getStreamContext() const noexcept(false)
void transformImpAsync(WaitingTaskHolder iTask, std::size_t iIndex, edm::ActivityRegistry *iAct, ProducerBase const &iBase, edm::EventForTransformer &) const noexcept
void registerTransformAsyncImp(ProducerBase &, EDPutToken, const TypeID &id, std::string instanceName, PreTransformFunction, TransformFunction)
std::string const & processName() const
HLT enums.
double a
Definition: hdecay.h:121
def cache(function)
Definition: utilities.py:3
void registerTransformImp(ProducerBase &, EDPutToken, const TypeID &id, std::string instanceName, TransformFunction)
std::string const & moduleLabel() const
def move(src, dest)
Definition: eostools.py:511