Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:02:23

0001 #include "FWCore/Framework/interface/TransformerBase.h"
0002 #include "FWCore/Framework/interface/ProducerBase.h"
0003 #include "FWCore/Framework/interface/EventForTransformer.h"
0004 #include "FWCore/Concurrency/interface/WaitingTaskHolder.h"
0005 #include "FWCore/Concurrency/interface/WaitingTaskWithArenaHolder.h"
0006 #include "DataFormats/Provenance/interface/ProductResolverIndexHelper.h"
0007 #include "DataFormats/Provenance/interface/BranchDescription.h"
0008 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0009 
0010 #include <optional>
0011 
0012 namespace edm {
0013   void TransformerBase::registerTransformImp(
0014       ProducerBase& iBase, EDPutToken iToken, const TypeID& id, std::string instanceName, TransformFunction iFunc) {
0015     auto transformPut = iBase.transforms(id, std::move(instanceName));
0016     PreTransformFunction ptf;
0017     transformInfo_.emplace_back(iToken.index(), id, transformPut, std::move(ptf), std::move(iFunc));
0018   }
0019 
0020   void TransformerBase::registerTransformAsyncImp(ProducerBase& iBase,
0021                                                   EDPutToken iToken,
0022                                                   const TypeID& id,
0023                                                   std::string instanceName,
0024                                                   PreTransformFunction iPreFunc,
0025                                                   TransformFunction iFunc) {
0026     auto transformPut = iBase.transforms(id, std::move(instanceName));
0027     transformInfo_.emplace_back(iToken.index(), id, transformPut, std::move(iPreFunc), std::move(iFunc));
0028   }
0029 
0030   std::size_t TransformerBase::findMatchingIndex(ProducerBase const& iBase,
0031                                                  edm::BranchDescription const& iBranch) const {
0032     auto const& list = iBase.typeLabelList();
0033 
0034     std::size_t index = 0;
0035     bool found = false;
0036     for (auto const& element : list) {
0037       if (not element.isTransform_) {
0038         continue;
0039       }
0040       if (element.typeID_ == iBranch.unwrappedTypeID() &&
0041           element.productInstanceName_ == iBranch.productInstanceName()) {
0042         found = true;
0043         break;
0044       }
0045       ++index;
0046     }
0047     assert(found);
0048     return index;
0049   }
0050 
0051   void TransformerBase::extendUpdateLookup(ProducerBase const& iBase,
0052                                            ModuleDescription const& iModuleDesc,
0053                                            ProductResolverIndexHelper const& iHelper) {
0054     auto const& list = iBase.typeLabelList();
0055 
0056     for (auto it = transformInfo_.begin<0>(); it != transformInfo_.end<0>(); ++it) {
0057       auto const& putInfo = list[*it];
0058       *it = iHelper.index(PRODUCT_TYPE,
0059                           putInfo.typeID_,
0060                           iModuleDesc.moduleLabel().c_str(),
0061                           putInfo.productInstanceName_.c_str(),
0062                           iModuleDesc.processName().c_str());
0063     }
0064   }
0065 
0066   void TransformerBase::transformImpAsync(edm::WaitingTaskHolder iHolder,
0067                                           std::size_t iIndex,
0068                                           ProducerBase const& iBase,
0069                                           edm::EventForTransformer& iEvent) const {
0070     if (transformInfo_.get<kPreTransform>(iIndex)) {
0071       std::optional<decltype(iEvent.get(transformInfo_.get<kType>(iIndex), transformInfo_.get<kResolverIndex>(iIndex)))>
0072           handle;
0073       CMS_SA_ALLOW try {
0074         handle = iEvent.get(transformInfo_.get<kType>(iIndex), transformInfo_.get<kResolverIndex>(iIndex));
0075       } catch (...) {
0076         iHolder.doneWaiting(std::current_exception());
0077         return;
0078       }
0079       if (handle->wrapper()) {
0080         auto cache = std::make_shared<std::any>();
0081         auto nextTask =
0082             edm::make_waiting_task([holder = iHolder, cache, iIndex, this, &iBase, handle = *handle, iEvent](
0083                                        std::exception_ptr const* iPtr) mutable {
0084               if (iPtr) {
0085                 holder.doneWaiting(*iPtr);
0086               } else {
0087                 iEvent.put(iBase.putTokenIndexToProductResolverIndex()[transformInfo_.get<kToken>(iIndex).index()],
0088                            transformInfo_.get<kTransform>(iIndex)(std::move(*cache)),
0089                            handle);
0090               }
0091             });
0092         WaitingTaskWithArenaHolder wta(*iHolder.group(), nextTask);
0093         CMS_SA_ALLOW try {
0094           *cache = transformInfo_.get<kPreTransform>(iIndex)(*(handle->wrapper()), wta);
0095         } catch (...) {
0096           wta.doneWaiting(std::current_exception());
0097         }
0098       }
0099     } else {
0100       CMS_SA_ALLOW try {
0101         auto handle = iEvent.get(transformInfo_.get<kType>(iIndex), transformInfo_.get<kResolverIndex>(iIndex));
0102 
0103         if (handle.wrapper()) {
0104           std::any v = handle.wrapper();
0105           iEvent.put(iBase.putTokenIndexToProductResolverIndex()[transformInfo_.get<kToken>(iIndex).index()],
0106                      transformInfo_.get<kTransform>(iIndex)(std::move(v)),
0107                      handle);
0108         }
0109       } catch (...) {
0110         iHolder.doneWaiting(std::current_exception());
0111       }
0112     }
0113   }
0114 
0115 }  // namespace edm