-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement edmtest::GenericCloner (alt version)
- Loading branch information
Showing
3 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
/* | ||
* This EDProducer will clone all the event products declared by its configuration, using their ROOT dictionaries. | ||
* | ||
* The products can be specified either as module labels (e.g. "<module label>") or as branch names (e.g. | ||
* "<product type>_<module label>_<instance name>_<process name>"). | ||
* | ||
* If a module label is used, no underscore ("_") must be present; this module will clone all the products produced by | ||
* that module, including those produced by the Transformer functionality (such as the implicitly copied-to-host | ||
* products in case of Alpaka-based modules). | ||
* If a branch name is used, all four fields must be present, separated by underscores; this module will clone only on | ||
* the matching product(s). | ||
* | ||
* Glob expressions ("?" and "*") are supported in module labels and within the individual fields of branch names, | ||
* similar to an OutputModule's "keep" statements. | ||
* Use "*" to clone all products. | ||
* | ||
* For example, in the case of Alpaka-based modules running on a device, using | ||
* | ||
* eventProducts = cms.untracked.vstring( "module" ) | ||
* | ||
* will cause "module" to run, along with automatic copy of its device products to the host, and will attempt to clone | ||
* all device and host products. | ||
* To clone only the host product, the branch can be specified explicitly with | ||
* | ||
* eventProducts = cms.untracked.vstring( "HostProductType_module_*_*" ) | ||
* | ||
* . | ||
*/ | ||
|
||
#include <cstring> | ||
#include <iostream> | ||
#include <memory> | ||
#include <string> | ||
#include <string_view> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include <TBufferFile.h> | ||
|
||
#include "DataFormats/Provenance/interface/ProductDescription.h" | ||
#include "DataFormats/Provenance/interface/ProductNamePattern.h" | ||
#include "FWCore/Framework/interface/Event.h" | ||
#include "FWCore/Framework/interface/WrapperBaseHandle.h" | ||
#include "FWCore/Framework/interface/WrapperBaseOrphanHandle.h" | ||
#include "FWCore/Framework/interface/global/EDProducer.h" | ||
#include "FWCore/MessageLogger/interface/MessageLogger.h" | ||
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" | ||
#include "FWCore/ParameterSet/interface/ParameterDescriptionNode.h" | ||
#include "FWCore/ParameterSet/interface/ParameterSet.h" | ||
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h" | ||
#include "FWCore/Reflection/interface/ObjectWithDict.h" | ||
#include "FWCore/Utilities/interface/EDMException.h" | ||
|
||
namespace edmtest { | ||
|
||
class GenericClonerAlt : public edm::global::EDProducer<> { | ||
public: | ||
explicit GenericClonerAlt(edm::ParameterSet const&); | ||
~GenericClonerAlt() override = default; | ||
|
||
void produce(edm::StreamID, edm::Event&, edm::EventSetup const&) const override; | ||
|
||
static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); | ||
|
||
private: | ||
struct Entry { | ||
edm::TypeWithDict objectType_; | ||
edm::TypeWithDict wrappedType_; | ||
edm::EDGetToken getToken_; | ||
edm::EDPutToken putToken_; | ||
}; | ||
|
||
std::vector<edm::ProductNamePattern> eventPatterns_; | ||
std::vector<Entry> eventProducts_; | ||
std::string label_; | ||
bool verbose_; | ||
}; | ||
|
||
GenericClonerAlt::GenericClonerAlt(edm::ParameterSet const& config) | ||
: eventPatterns_(edm::productPatterns(config.getParameter<std::vector<std::string>>("eventProducts"))), | ||
label_(config.getParameter<std::string>("@module_label")), | ||
verbose_(config.getUntrackedParameter<bool>("verbose")) { | ||
eventProducts_.reserve(eventPatterns_.size()); | ||
|
||
callWhenNewProductsRegistered([this](edm::ProductDescription const& product) { | ||
static const std::string_view kPathStatus("edm::PathStatus"); | ||
static const std::string_view kEndPathStatus("edm::EndPathStatus"); | ||
|
||
switch (product.branchType()) { | ||
case edm::InEvent: | ||
if (product.className() == kPathStatus or product.className() == kEndPathStatus) { | ||
return; | ||
} | ||
for (auto& pattern : eventPatterns_) { | ||
if (pattern.match(product)) { | ||
// check that the product is not transient | ||
if (product.transient()) { | ||
edm::LogWarning("GenericCloner") << "Event product " << product.branchName() << " of type " | ||
<< product.unwrappedType() << " is transient, will not be cloned."; | ||
break; | ||
} | ||
if (verbose_) { | ||
edm::LogInfo("GenericCloner") | ||
<< "will clone Event product " << product.branchName() << " of type " << product.unwrappedType(); | ||
} | ||
Entry entry; | ||
entry.objectType_ = product.unwrappedType(); | ||
entry.wrappedType_ = product.wrappedType(); | ||
// TODO move this to EDConsumerBase::consumes() ? | ||
entry.getToken_ = this->consumes( | ||
edm::TypeToGet{product.unwrappedTypeID(), edm::PRODUCT_TYPE}, | ||
edm::InputTag{product.moduleLabel(), product.productInstanceName(), product.processName()}); | ||
entry.putToken_ = this->produces(product.unwrappedTypeID(), product.productInstanceName()); | ||
eventProducts_.emplace_back(std::move(entry)); | ||
break; | ||
} | ||
} | ||
break; | ||
|
||
case edm::InLumi: | ||
case edm::InRun: | ||
case edm::InProcess: | ||
// lumi, run and process products are not supported | ||
break; | ||
|
||
default: | ||
throw edm::Exception(edm::errors::LogicError) | ||
<< "Unexpected product type " << product.branchType() << "\nPlease contact a Framework developer."; | ||
} | ||
}); | ||
} | ||
|
||
void GenericClonerAlt::produce(edm::StreamID /*unused*/, edm::Event& event, edm::EventSetup const& /*unused*/) const { | ||
for (auto& product : eventProducts_) { | ||
edm::Handle<edm::WrapperBase> handle(product.objectType_.typeInfo()); | ||
event.getByToken(product.getToken_, handle); | ||
edm::WrapperBase const* wrapper = handle.product(); | ||
|
||
// write the wrapper into a TBuffer | ||
TBufferFile buffer(TBuffer::kWrite); | ||
product.wrappedType_.getClass()->Streamer(const_cast<edm::WrapperBase*>(wrapper), buffer); | ||
|
||
// read back a copy of the product form the TBuffer | ||
buffer.SetReadMode(); | ||
buffer.SetBufferOffset(0); | ||
std::unique_ptr<edm::WrapperBase> clone( | ||
reinterpret_cast<edm::WrapperBase*>(product.wrappedType_.getClass()->New())); | ||
product.wrappedType_.getClass()->Streamer(clone.get(), buffer); | ||
|
||
// move the wrapper into the Event | ||
event.put(product.putToken_, std::move(clone)); | ||
} | ||
} | ||
|
||
void GenericClonerAlt::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { | ||
descriptions.setComment( | ||
R"(This EDProducer will clone all the event products declared by its configuration, using their ROOT dictionaries. | ||
The products can be specified either as module labels (e.g. "<module label>") or as branch names (e.g. "<product type>_<module label>_<instance name>_<process name>"). | ||
If a module label is used, no underscore ("_") must be present; this module will clone all the products produced by that module, including those produced by the Transformer functionality (such as the implicitly copied-to-host products in case of Alpaka-based modules). | ||
If a branch name is used, all four fields must be present, separated by underscores; this module will clone only on the matching product(s). | ||
Glob expressions ("?" and "*") are supported in module labels and within the individual fields of branch names, similar to an OutputModule's "keep" statements. | ||
Use "*" to clone all products. | ||
For example, in the case of Alpaka-based modules running on a device, using | ||
eventProducts = cms.untracked.vstring( "module" ) | ||
will cause "module" to run, along with automatic copy of its device products to the host, and will attempt to clone all device and host products. | ||
To clone only the host product, the branch can be specified explicitly with | ||
eventProducts = cms.untracked.vstring( "HostProductType_module_*_*" ) | ||
.)"); | ||
|
||
edm::ParameterSetDescription desc; | ||
desc.add<std::vector<std::string>>("eventProducts", {}) | ||
->setComment("List of modules or branches whose event products will be cloned."); | ||
desc.addUntracked<bool>("verbose", false) | ||
->setComment("Print the branch names of the products that will be cloned."); | ||
descriptions.addWithDefaultLabel(desc); | ||
} | ||
|
||
} // namespace edmtest | ||
|
||
#include "FWCore/Framework/interface/MakerMacros.h" | ||
DEFINE_FWK_MODULE(edmtest::GenericClonerAlt); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
<test name="TestFWCoreModulesEventIDValidator" command="cmsRun ${LOCALTOP}/src/FWCore/TestModules/test/testEventIDValidator_cfg.py"/> | ||
|
||
<test name="TestFWCoreModulesGenericCloner" command="cmsRun ${LOCALTOP}/src/FWCore/TestModules/test/testGenericCloner_cfg.py"/> | ||
|
||
<test name="TestFWCoreModulesGenericClonerAlt" command="cmsRun ${LOCALTOP}/src/FWCore/TestModules/test/testGenericClonerAlt_cfg.py"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import FWCore.ParameterSet.Config as cms | ||
|
||
process = cms.Process("TEST") | ||
|
||
process.load("FWCore.MessageService.MessageLogger_cfi") | ||
process.MessageLogger.cerr.INFO.limit = 10000000 | ||
|
||
process.options.numberOfThreads = 1 | ||
process.options.numberOfStreams = 1 | ||
|
||
process.source = cms.Source("EmptySource") | ||
process.maxEvents.input = 10 | ||
|
||
# produce, clone and validate products of type int | ||
process.produceInt = cms.EDProducer("edmtest::GlobalIntProducer", | ||
value = cms.int32(42) | ||
) | ||
|
||
process.cloneInt = cms.EDProducer("edmtest::GenericClonerAlt", | ||
eventProducts = cms.vstring("produceInt"), | ||
verbose = cms.untracked.bool(True) | ||
) | ||
|
||
process.validateInt = cms.EDAnalyzer("edmtest::GlobalIntAnalyzer", | ||
source = cms.InputTag("cloneInt"), | ||
expected = cms.int32(42) | ||
) | ||
|
||
process.taskInt = cms.Task(process.produceInt, process.cloneInt) | ||
|
||
process.pathInt = cms.Path(process.validateInt, process.taskInt) | ||
|
||
# produce, clone and validate products of type std::string | ||
process.produceString = cms.EDProducer("edmtest::GlobalStringProducer", | ||
value = cms.string("Hello world") | ||
) | ||
|
||
process.cloneString = cms.EDProducer("edmtest::GenericClonerAlt", | ||
eventProducts = cms.vstring("produceString"), | ||
verbose = cms.untracked.bool(True) | ||
) | ||
|
||
process.validateString = cms.EDAnalyzer("edmtest::GlobalStringAnalyzer", | ||
source = cms.InputTag("cloneString"), | ||
expected = cms.string("Hello world") | ||
) | ||
|
||
process.taskString = cms.Task(process.produceString, process.cloneString) | ||
|
||
process.pathString = cms.Path(process.validateString, process.taskString) | ||
|
||
# produce, clone and validate products of type edm::EventID | ||
process.eventIds = cms.EDProducer("edmtest::EventIDProducer") | ||
|
||
process.cloneIdsByLabel = cms.EDProducer("edmtest::GenericClonerAlt", | ||
eventProducts = cms.vstring("eventIds"), | ||
verbose = cms.untracked.bool(True) | ||
) | ||
|
||
process.cloneIdsByBranch = cms.EDProducer("edmtest::GenericClonerAlt", | ||
eventProducts = cms.vstring("*_eventIds__TEST"), | ||
verbose = cms.untracked.bool(True) | ||
) | ||
|
||
process.validateIdsByLabel = cms.EDAnalyzer("edmtest::EventIDValidator", | ||
source = cms.untracked.InputTag('cloneIdsByLabel') | ||
) | ||
|
||
process.validateIdsByBranch = cms.EDAnalyzer("edmtest::EventIDValidator", | ||
source = cms.untracked.InputTag('cloneIdsByBranch') | ||
) | ||
|
||
process.taskIds = cms.Task(process.eventIds, process.cloneIdsByLabel, process.cloneIdsByBranch) | ||
|
||
process.pathIds = cms.Path(process.validateIdsByLabel + process.validateIdsByBranch, process.taskIds) | ||
|
||
# will not clone a transient product | ||
process.produceTransient = cms.EDProducer("TransientIntProducer", | ||
ivalue = cms.int32(22) | ||
) | ||
|
||
process.cloneTransient = cms.EDProducer("edmtest::GenericClonerAlt", | ||
eventProducts = cms.vstring("produceTransient"), | ||
verbose = cms.untracked.bool(True) | ||
) | ||
|
||
process.pathTransient = cms.Path(process.produceTransient + process.cloneTransient) |