Skip to content

Commit

Permalink
Added transform ability to Producer like modules
Browse files Browse the repository at this point in the history
The interface allows a module to read one of its own data products and create a new data product from it. This only happens if another module consumes the transformed data.
  • Loading branch information
Dr15Jones committed Jun 21, 2022
1 parent cb02e70 commit 7346305
Show file tree
Hide file tree
Showing 59 changed files with 1,143 additions and 14 deletions.
5 changes: 5 additions & 0 deletions DataFormats/Provenance/interface/BranchDescription.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ namespace edm {
std::string const& productInstanceName() const { return productInstanceName_; }
bool produced() const { return transient_.produced_; }
void setProduced(bool isProduced) { transient_.produced_ = isProduced; }
bool isTransform() const { return transient_.isTransform_; }
void setIsTransform(bool isTransform) { transient_.isTransform_ = isTransform; }
bool present() const { return !transient_.dropped_; }
bool dropped() const { return transient_.dropped_; }
void setDropped(bool isDropped) { transient_.dropped_ = isDropped; }
Expand Down Expand Up @@ -178,6 +180,9 @@ namespace edm {
// This item is set only in the framework, not by FWLite.
bool onDemand_;

// Was this branch produced in this current process via the transform ability
bool isTransform_;

// Has the branch been dropped from the product tree in this file
// (or if this is a merged product registry, in the first file).
// This item is set only in the framework, not by FWLite.
Expand Down
1 change: 1 addition & 0 deletions DataFormats/Provenance/src/BranchDescription.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace edm {
basketSize_(),
produced_(false),
onDemand_(false),
isTransform_(false),
dropped_(false),
transient_(false),
availableOnlyAtEndTransition_(false),
Expand Down
42 changes: 42 additions & 0 deletions FWCore/Framework/interface/EventForTransformer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef FWCore_Framework_EventForTransformer_h
#define FWCore_Framework_EventForTransformer_h

// -*- C++ -*-
//
// Package: Framework
// Class : EventForTransformer
//
/**\class edm::EventForTransformer
*/
/*----------------------------------------------------------------------
----------------------------------------------------------------------*/

#include "DataFormats/Common/interface/BasicHandle.h"
#include "DataFormats/Common/interface/WrapperBase.h"

#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Utilities/interface/TypeID.h"
#include "FWCore/Utilities/interface/ProductResolverIndex.h"

#include <memory>

namespace edm {

class EventPrincipal;
class ModuleCallingContext;

class EventForTransformer {
public:
EventForTransformer(EventPrincipal const&, ModuleCallingContext const*);

BasicHandle get(edm::TypeID const& iTypeID, ProductResolverIndex iIndex) const;

void put(ProductResolverIndex index, std::unique_ptr<WrapperBase> edp, BasicHandle const& iGetHandle);

private:
EventPrincipal const& eventPrincipal_;
ModuleCallingContext const* mcc_;
};
} // namespace edm
#endif
1 change: 1 addition & 0 deletions FWCore/Framework/interface/Principal.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ namespace edm {
void addDelayedReaderInputProduct(std::shared_ptr<BranchDescription const> bd);
void addPutOnReadInputProduct(std::shared_ptr<BranchDescription const> bd);
void addUnscheduledProduct(std::shared_ptr<BranchDescription const> bd);
void addTransformProduct(std::shared_ptr<BranchDescription const> bd);
void addAliasedProduct(std::shared_ptr<BranchDescription const> bd);
void addSwitchProducerProduct(std::shared_ptr<BranchDescription const> bd);
void addSwitchAliasProduct(std::shared_ptr<BranchDescription const> bd);
Expand Down
3 changes: 3 additions & 0 deletions FWCore/Framework/interface/ProducerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ namespace edm {
friend class limited::EDProducerBase;
friend class limited::EDFilterBase;
friend class PuttableSourceBase;
friend class TransformerBase;
template <typename T>
friend class stream::ProducingModuleAdaptorBase;

Expand All @@ -127,6 +128,8 @@ namespace edm {
iPrincipal.commit_(putIndicies_[producerbasehelper::PrincipalTraits<P>::kBranchType], iID);
}

using ProductRegistryHelper::transforms;

std::function<void(BranchDescription const&)> callWhenNewProductsRegistered_;
std::array<std::vector<edm::ProductResolverIndex>, edm::NumBranchTypes> putIndicies_;
std::vector<edm::ProductResolverIndex> putTokenToResolverIndex_;
Expand Down
14 changes: 12 additions & 2 deletions FWCore/Framework/interface/ProductRegistryHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@ namespace edm {
};

struct TypeLabelItem {
enum class AliasType { kBranchAlias, kSwitchAlias };
enum class AliasType : char { kBranchAlias, kSwitchAlias };

TypeLabelItem(Transition const& transition, TypeID const& tid, std::string pin)
: transition_(transition),
typeID_(tid),
productInstanceName_(std::move(pin)),
branchAlias_(),
aliasType_(AliasType::kBranchAlias) {}
aliasType_(AliasType::kBranchAlias),
isTransform_(false) {}
Transition transition_;
TypeID typeID_;
std::string productInstanceName_;
std::string branchAlias_;
AliasType aliasType_;
bool isTransform_;
};

struct BranchAliasSetter {
Expand Down Expand Up @@ -181,6 +183,14 @@ namespace edm {
return BranchAliasSetter{typeLabelList_.back(), EDPutToken{index}};
}

EDPutToken transforms(const TypeID& id, std::string instanceName) {
unsigned int index = typeLabelList_.size();
typeLabelList_.emplace_back(Transition::Event, id, std::move(instanceName));
typeLabelList_.back().isTransform_ = true;
recordProvenanceList_.push_back(true);
return EDPutToken{index};
}

virtual bool hasAbilityToProduceInBeginProcessBlocks() const { return false; }
virtual bool hasAbilityToProduceInEndProcessBlocks() const { return false; }

Expand Down
54 changes: 54 additions & 0 deletions FWCore/Framework/interface/TransformerBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// TransformerBase.h
// CMSSW
//
// Created by Chris Jones on 6/02/22.
//

#ifndef FWCore_Framework_TransformerBase_h
#define FWCore_Framework_TransformerBase_h

#include "FWCore/Utilities/interface/EDPutToken.h"
#include "FWCore/Utilities/interface/SoATuple.h"
#include "FWCore/Utilities/interface/TypeID.h"
#include "FWCore/Utilities/interface/ProductResolverIndex.h"

#include <string>
#include <functional>
#include <memory>

namespace edm {
class ProducerBase;
class TypeID;
class WrapperBase;
class EventForTransformer;
class BranchDescription;
class ProductResolverIndexHelper;
class ModuleDescription;

class TransformerBase {
public:
TransformerBase() = default;
virtual ~TransformerBase() noexcept(false) = default;

protected:
//The function takes the WrapperBase corresponding to the data product from the EDPutToken
// and returns the WrapperBase associated to the id and instanceName
using TransformFunction = std::function<std::unique_ptr<edm::WrapperBase>(edm::WrapperBase const&)>;

void registerTransformImp(ProducerBase&, EDPutToken, const TypeID& id, std::string instanceName, TransformFunction);

std::size_t findMatchingIndex(ProducerBase const& iBase, edm::BranchDescription const&) const;
ProductResolverIndex prefetchImp(std::size_t iIndex) const { return transformInfo_.get<0>(iIndex); }
void transformImp(std::size_t iIndex, ProducerBase const& iBase, edm::EventForTransformer&) const;

void extendUpdateLookup(ProducerBase const&,
ModuleDescription const& iModuleDesc,
ProductResolverIndexHelper const& iHelper);

private:
SoATuple<ProductResolverIndex, TypeID, EDPutToken, TransformFunction> transformInfo_;
};
} // namespace edm

#endif /* TransformerBase_h */
9 changes: 9 additions & 0 deletions FWCore/Framework/interface/global/EDFilterBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace edm {
class ActivityRegistry;
class ThinnedAssociationsHelper;
class WaitingTaskWithArenaHolder;
class EventForTransformer;

namespace maker {
template <typename T>
Expand Down Expand Up @@ -77,6 +78,10 @@ namespace edm {
ActivityRegistry*,
ModuleCallingContext const*,
WaitingTaskWithArenaHolder&);
void doTransform(size_t iTransformIndex,
EventPrincipal const& iEvent,
ActivityRegistry*,
ModuleCallingContext const*);
//For now this is a placeholder
/*virtual*/ void preActionBeforeRunEventAsync(WaitingTaskHolder iTask,
ModuleCallingContext const& iModuleCallingContext,
Expand Down Expand Up @@ -147,6 +152,10 @@ namespace edm {
virtual void doBeginLuminosityBlockProduce_(LuminosityBlock& lbp, EventSetup const& c);
virtual void doEndLuminosityBlockProduce_(LuminosityBlock& lbp, EventSetup const& c);

virtual size_t transformIndex(edm::BranchDescription const& iBranch) const;
virtual ProductResolverIndex transformPrefetch(std::size_t iIndex) const;
virtual void transform(std::size_t iIndex, edm::EventForTransformer& iEvent) const;

virtual void clearInputProcessBlockCaches();
virtual bool hasAcquire() const { return false; }
bool hasAccumulator() const { return false; }
Expand Down
9 changes: 9 additions & 0 deletions FWCore/Framework/interface/global/EDProducerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace edm {
class ActivityRegistry;
class ThinnedAssociationsHelper;
class WaitingTaskWithArenaHolder;
class EventForTransformer;

namespace maker {
template <typename T>
Expand Down Expand Up @@ -80,6 +81,10 @@ namespace edm {
ActivityRegistry*,
ModuleCallingContext const*,
WaitingTaskWithArenaHolder&);
void doTransform(size_t iTransformIndex,
EventPrincipal const& iEvent,
ActivityRegistry*,
ModuleCallingContext const*);
void doPreallocate(PreallocationConfiguration const&);
void doBeginJob();
void doEndJob();
Expand Down Expand Up @@ -150,6 +155,10 @@ namespace edm {
virtual void doBeginLuminosityBlockProduce_(LuminosityBlock& lbp, EventSetup const& c);
virtual void doEndLuminosityBlockProduce_(LuminosityBlock& lbp, EventSetup const& c);

virtual size_t transformIndex(edm::BranchDescription const& iBranch) const;
virtual ProductResolverIndex transformPrefetch(std::size_t iIndex) const;
virtual void transform(std::size_t iIndex, edm::EventForTransformer& iEvent) const;

virtual void clearInputProcessBlockCaches();
virtual bool hasAccumulator() const { return false; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ namespace edm {
using Type = edm::global::impl::ExternalWork<edm::global::EDFilterBase>;
};

template <>
struct AbilityToImplementor<edm::Transformer> {
using Type = edm::global::impl::Transformer<edm::global::EDFilterBase>;
};

template <bool, bool, typename T>
struct SpecializeAbilityToImplementor {
using Type = typename AbilityToImplementor<T>::Type;
Expand Down
50 changes: 50 additions & 0 deletions FWCore/Framework/interface/global/implementors.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/InputProcessBlockCacheImpl.h"
#include "FWCore/Framework/interface/LuminosityBlock.h"
#include "FWCore/Framework/interface/TransformerBase.h"
#include "FWCore/Framework/interface/ProductRegistryHelper.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/StreamID.h"
#include "FWCore/Utilities/interface/ProcessBlockIndex.h"
#include "FWCore/Utilities/interface/RunIndex.h"
#include "FWCore/Utilities/interface/LuminosityBlockIndex.h"
#include "FWCore/Utilities/interface/propagate_const.h"
#include "DataFormats/Common/interface/Wrapper.h"

// forward declarations
namespace edm {
Expand Down Expand Up @@ -439,6 +442,53 @@ namespace edm {

virtual void accumulate(StreamID streamID, Event const& ev, EventSetup const& es) const = 0;
};

template <typename T>
class Transformer : public virtual T, private TransformerBase {
public:
Transformer() = default;
Transformer(Transformer const&) = delete;
Transformer& operator=(Transformer const&) = delete;
~Transformer() noexcept(false) override{};

template <typename G, typename F>
void registerTransform(ProductRegistryHelper::BranchAliasSetterT<G> iSetter,
F&& iF,
std::string productInstance = std::string()) {
registerTransform(edm::EDPutTokenT<G>(iSetter), std::forward<F>(iF), std::move(productInstance));
}

template <typename G, typename F>
void registerTransform(edm::EDPutTokenT<G> iToken, F iF, std::string productInstance = std::string()) {
using ReturnTypeT = decltype(iF(G()));
TypeID returnType(typeid(ReturnTypeT));
TransformerBase::registerTransformImp(*this,
EDPutToken(iToken),
returnType,
std::move(productInstance),
[f = iF](edm::WrapperBase const& iGotProduct) {
return std::make_unique<edm::Wrapper<ReturnTypeT>>(
WrapperBase::Emplace{},
f(*static_cast<edm::Wrapper<G> const&>(iGotProduct).product()));
});
}

private:
size_t transformIndex(edm::BranchDescription const& iBranch) const final {
return TransformerBase::findMatchingIndex(*this, iBranch);
}
ProductResolverIndex transformPrefetch(std::size_t iIndex) const final {
return TransformerBase::prefetchImp(iIndex);
}
void transform(std::size_t iIndex, edm::EventForTransformer& iEvent) const final {
return TransformerBase::transformImp(iIndex, *this, iEvent);
}
void extendUpdateLookup(BranchType iBranchType, ProductResolverIndexHelper const& iHelper) override {
if (iBranchType == InEvent) {
TransformerBase::extendUpdateLookup(*this, this->moduleDescription(), iHelper);
}
}
};
} // namespace impl
} // namespace global
} // namespace edm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ namespace edm {
using Type = edm::global::impl::ExternalWork<edm::global::EDProducerBase>;
};

template <>
struct AbilityToImplementor<edm::Transformer> {
using Type = edm::global::impl::Transformer<edm::global::EDProducerBase>;
};

template <>
struct AbilityToImplementor<edm::Accumulator> {
using Type = edm::global::impl::Accumulator<edm::global::EDProducerBase>;
Expand Down
10 changes: 10 additions & 0 deletions FWCore/Framework/interface/limited/EDFilterBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace edm {
class StreamID;
class ActivityRegistry;
class ThinnedAssociationsHelper;
class EventForTransformer;

namespace maker {
template <typename T>
Expand Down Expand Up @@ -77,6 +78,11 @@ namespace edm {

private:
bool doEvent(EventTransitionInfo const&, ActivityRegistry*, ModuleCallingContext const*);
void doTransform(size_t iTransformIndex,
EventPrincipal const& iEvent,
ActivityRegistry*,
ModuleCallingContext const*);

//For now this is a placeholder
/*virtual*/ void preActionBeforeRunEventAsync(WaitingTaskHolder,
ModuleCallingContext const&,
Expand Down Expand Up @@ -147,6 +153,10 @@ namespace edm {
virtual void doBeginLuminosityBlockProduce_(LuminosityBlock& lbp, EventSetup const& c);
virtual void doEndLuminosityBlockProduce_(LuminosityBlock& lbp, EventSetup const& c);

virtual size_t transformIndex(edm::BranchDescription const& iBranch) const;
virtual ProductResolverIndex transformPrefetch(std::size_t iIndex) const;
virtual void transform(std::size_t iIndex, edm::EventForTransformer& iEvent) const;

virtual void clearInputProcessBlockCaches();

bool hasAcquire() const { return false; }
Expand Down
Loading

0 comments on commit 7346305

Please sign in to comment.