Skip to content

Commit

Permalink
Merge pull request #39960 from Dr15Jones/consumesInEventBase
Browse files Browse the repository at this point in the history
Added getByToken to edm::EventBase
  • Loading branch information
cmsbuild committed Nov 3, 2022
2 parents e01b0cd + d5f2f9a commit 1393895
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 5 deletions.
7 changes: 6 additions & 1 deletion DataFormats/FWLite/interface/ChainEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ namespace fwlite {

// ---------- const member functions ---------------------
std::string const getBranchNameFor(std::type_info const&, char const*, char const*, char const*) const override;

template <typename T>
edm::EDGetTokenT<T> consumes(edm::InputTag const& iTag) const {
return event_->consumes<T>(iTag);
}
using fwlite::EventBase::getByLabel;

// This function should only be called by fwlite::Handle<>
Expand Down Expand Up @@ -124,6 +127,8 @@ namespace fwlite {
fwlite::Run const& getRun();

private:
bool getByTokenImp(edm::EDGetToken, edm::WrapperBase const*&) const override;

friend class MultiChainEvent;

ChainEvent(Event const&); // stop default
Expand Down
9 changes: 5 additions & 4 deletions DataFormats/FWLite/interface/DataGetterHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ namespace fwlite {
std::shared_ptr<edm::EDProductGetter> getter = std::shared_ptr<edm::EDProductGetter>(),
bool useCache = false,
std::function<void(TBranch const&)> baFunc = [](TBranch const&) {});
virtual ~DataGetterHelper();
~DataGetterHelper();

// ---------- const member functions ---------------------
virtual std::string const getBranchNameFor(std::type_info const&, char const*, char const*, char const*) const;
std::string const getBranchNameFor(std::type_info const&, char const*, char const*, char const*) const;
std::optional<edm::BranchID> getBranchIDFor(std::type_info const&, char const*, char const*, char const*) const;

// This function should only be called by fwlite::Handle<>
virtual bool getByLabel(std::type_info const&, char const*, char const*, char const*, void*, Long_t) const;
bool getByLabel(std::type_info const&, char const*, char const*, char const*, void*, Long_t) const;

edm::WrapperBase const* getByProductID(edm::ProductID const& pid, Long_t eventEntry) const;
edm::WrapperBase const* getByBranchID(edm::BranchID const& bid, Long_t eventEntry) const;
std::optional<std::tuple<edm::WrapperBase const*, unsigned int>> getThinnedProduct(edm::ProductID const& pid,
unsigned int key,
Long_t eventEntry) const;
Expand Down Expand Up @@ -98,7 +100,6 @@ namespace fwlite {
internal::Data& getBranchDataFor(std::type_info const&, char const*, char const*, char const*) const;
void getBranchData(edm::EDProductGetter const*, Long64_t, internal::Data&) const;
bool getByBranchDescription(edm::BranchDescription const&, Long_t eventEntry, KeyToDataMap::iterator&) const;
edm::WrapperBase const* getByBranchID(edm::BranchID const& bid, Long_t eventEntry) const;
edm::WrapperBase const* wrapperBasePtr(edm::ObjectWithDict const&) const;
edm::ThinnedAssociation const* getThinnedAssociation(edm::BranchID const& branchID, Long_t eventEntry) const;

Expand Down
16 changes: 16 additions & 0 deletions DataFormats/FWLite/interface/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ namespace fwlite {
class HistoryGetterBase;
class DataGetterHelper;
class RunFactory;
class ChainEvent;
class MultiChainEvent;

class Event : public EventBase {
public:
friend class ChainEvent;
friend class MultiChainEvent;

// NOTE: Does NOT take ownership so iFile must remain around
// at least as long as Event.
// useCache and baFunc (branch-access-function) are passed to
Expand Down Expand Up @@ -127,6 +133,15 @@ namespace fwlite {
char const* iProductInstanceLabel,
char const* iProcessName) const override;

template <typename T>
edm::EDGetTokenT<T> consumes(edm::InputTag const& iTag) const {
auto bid =
dataHelper_.getBranchIDFor(typeid(T), iTag.label().c_str(), iTag.instance().c_str(), iTag.process().c_str());
if (bid) {
return this->makeTokenUsing<T>(bid.value().id());
}
return {};
}
using fwlite::EventBase::getByLabel;
/// This function should only be called by fwlite::Handle<>
bool getByLabel(std::type_info const&, char const*, char const*, char const*, void*) const override;
Expand Down Expand Up @@ -190,6 +205,7 @@ namespace fwlite {
static void throwProductNotFoundException(std::type_info const&, char const*, char const*, char const*);

private:
bool getByTokenImp(edm::EDGetToken, edm::WrapperBase const*&) const override;
friend class internal::ProductGetter;
friend class ChainEvent;
friend class EventHistoryGetter;
Expand Down
9 changes: 9 additions & 0 deletions DataFormats/FWLite/interface/EventBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,17 @@ namespace fwlite {
virtual Long64_t fileIndex() const { return -1; }
virtual Long64_t secondaryFileIndex() const { return -1; }

protected:
template <typename T>
static edm::EDGetTokenT<T> makeTokenUsing(unsigned int iIndex) {
return edm::EDGetTokenT<T>(iIndex);
}

private:
virtual bool getByTokenImp(edm::EDGetToken, edm::WrapperBase const*&) const = 0;
edm::BasicHandle getByLabelImpl(std::type_info const&, std::type_info const&, const edm::InputTag&) const override;
edm::BasicHandle getByTokenImpl(std::type_info const&, edm::EDGetToken) const override;

edm::BasicHandle getImpl(std::type_info const&, edm::ProductID const&) const override;
};
} // namespace fwlite
Expand Down
10 changes: 10 additions & 0 deletions DataFormats/FWLite/interface/MultiChainEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ namespace fwlite {

// ---------- const member functions ---------------------
std::string const getBranchNameFor(std::type_info const&, char const*, char const*, char const*) const override;
template <typename T>
edm::EDGetTokenT<T> consumes(edm::InputTag const& iTag) const {
auto t = event1_->consumes<T>(iTag);
if (t) {
return t;
}
return event2_->consumes<T>(iTag);
}

using fwlite::EventBase::getByLabel;

Expand Down Expand Up @@ -133,6 +141,8 @@ namespace fwlite {
edm::ProductID const& thinned) const;

private:
bool getByTokenImp(edm::EDGetToken, edm::WrapperBase const*&) const override;

MultiChainEvent(Event const&); // stop default

const MultiChainEvent& operator=(Event const&); // stop default
Expand Down
4 changes: 4 additions & 0 deletions DataFormats/FWLite/src/ChainEvent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ namespace fwlite {
return event_->getByLabel(iType, iModule, iInstance, iProcess, iValue);
}

bool ChainEvent::getByTokenImp(edm::EDGetToken iToken, edm::WrapperBase const*& iValue) const {
return event_->getByTokenImp(iToken, iValue);
}

edm::WrapperBase const* ChainEvent::getByProductID(edm::ProductID const& iID) const {
return event_->getByProductID(iID);
}
Expand Down
31 changes: 31 additions & 0 deletions DataFormats/FWLite/src/DataGetterHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,37 @@ namespace fwlite {
iData.lastProduct_ = eventEntry;
}

std::optional<edm::BranchID> DataGetterHelper::getBranchIDFor(std::type_info const& iInfo,
char const* iModuleLabel,
char const* iProductInstanceLabel,
char const* iProcessLabel) const {
auto branchFor = [this](edm::TypeID const& iInfo,
char const* iModuleLabel,
char const* iProductInstanceLabel,
char const* iProcessLabel) -> std::optional<edm::BranchID> {
for (auto const& bd : branchMap_->getBranchDescriptions()) {
if (bd.unwrappedTypeID() == iInfo and bd.moduleLabel() == iModuleLabel and
bd.productInstanceName() == iProductInstanceLabel and bd.processName() == iProcessLabel) {
return bd.branchID();
}
}
return std::nullopt;
};
if (nullptr == iProcessLabel || strlen(iProcessLabel) == 0) {
//have to search in reverse order since newest are on the bottom
const edm::ProcessHistory& h = DataGetterHelper::history();
edm::TypeID typeID(iInfo);
for (edm::ProcessHistory::const_reverse_iterator iproc = h.rbegin(), eproc = h.rend(); iproc != eproc; ++iproc) {
auto v = branchFor(typeID, iModuleLabel, iProductInstanceLabel, iproc->processName().c_str());
if (v) {
return v;
}
}
return std::nullopt;
}
return branchFor(edm::TypeID(iInfo), iModuleLabel, iProductInstanceLabel, iProcessLabel);
}

internal::Data& DataGetterHelper::getBranchDataFor(std::type_info const& iInfo,
char const* iModuleLabel,
char const* iProductInstanceLabel,
Expand Down
9 changes: 9 additions & 0 deletions DataFormats/FWLite/src/Event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,15 @@ namespace fwlite {
return dataHelper_.getByLabel(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel, oData, eventIndex);
}

bool Event::getByTokenImp(edm::EDGetToken iToken, edm::WrapperBase const*& oData) const {
if (atEnd()) {
throw cms::Exception("OffEnd") << "You have requested data past the last event";
}
Long_t eventIndex = branchMap_.getEventEntry();
oData = dataHelper_.getByBranchID(edm::BranchID(iToken.index()), eventIndex);
return oData != nullptr;
}

edm::EventAuxiliary const& Event::eventAuxiliary() const {
Long_t eventIndex = branchMap_.getEventEntry();
updateAux(eventIndex);
Expand Down
20 changes: 20 additions & 0 deletions DataFormats/FWLite/src/EventBase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ namespace fwlite {
return value;
}

edm::BasicHandle EventBase::getByTokenImpl(std::type_info const& iProdInfo, edm::EDGetToken iToken) const {
edm::WrapperBase const* prod = nullptr;
getByTokenImp(iToken, prod);
if (prod == nullptr || !prod->isPresent()) {
edm::TypeID productType(iProdInfo);

edm::BasicHandle failed(edm::makeHandleExceptionFactory([=]() -> std::shared_ptr<cms::Exception> {
std::shared_ptr<cms::Exception> whyFailed(std::make_shared<edm::Exception>(edm::errors::ProductNotFound));
*whyFailed << "getByToken: Found zero products matching all criteria\n"
<< "Looking for type: " << productType << "\n"
<< "The data is registered in the file but is not available for this event\n";
return whyFailed;
}));
return failed;
}

edm::BasicHandle value(prod, &s_prov);
return value;
}

edm::BasicHandle EventBase::getImpl(std::type_info const& iProductInfo, const edm::ProductID& pid) const {
edm::WrapperBase const* prod = getByProductID(pid);
if (prod == nullptr || !prod->isPresent()) {
Expand Down
11 changes: 11 additions & 0 deletions DataFormats/FWLite/src/MultiChainEvent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,17 @@ namespace fwlite {
return true;
}

bool MultiChainEvent::getByTokenImp(edm::EDGetToken iToken, edm::WrapperBase const*& iValue) const {
bool ret1 = event1_->getByTokenImp(iToken, iValue);
if (!ret1) {
(const_cast<MultiChainEvent*>(this))->toSec(event1_->id());
bool ret2 = event2_->getByTokenImp(iToken, iValue);
if (!ret2)
return false;
}
return true;
}

edm::WrapperBase const* MultiChainEvent::getByProductID(edm::ProductID const& iID) const {
// First try the first file
edm::WrapperBase const* edp = event1_->getByProductID(iID);
Expand Down
47 changes: 47 additions & 0 deletions DataFormats/FWLite/test/event_looping_consumes_cint.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <vector>
#include <TFile.h>
using namespace std;

#if defined(__CINT__) && !defined(__MAKECINT__)
class loadFWLite {
public:
loadFWLite() {
gSystem->Load("libFWCoreFWLite");
FWLiteEnabler::enable();
}
};

static loadFWLite lfw;
#endif

#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/FWLite/interface/Event.h"
#include "FWCore/Utilities/interface/InputTag.h"

#if !defined(__CINT__) && !defined(__MAKECINT__)
#include "DataFormats/TestObjects/interface/ThingCollection.h"
#endif

void event_looping_consumes_cint() {
TFile f("good_a.root");
fwlite::Event e(&f);

auto token = e.consumes<std::vector<edmtest::Thing>>(edm::InputTag("Thing"));

int i = 0;
int returnValue = 0;
for (; e.isValid(); ++e, ++i) {
edm::Handle<vector<edmtest::Thing>> pThing;
e.getByToken(token, pThing);

for (int i = 0; i != pThing->size(); ++i) {
cout << pThing->at(i).a << " ";
}
cout << endl;
}
if (i == 0) {
cout << "First loop failed!" << endl;
returnValue = 1;
}
exit(returnValue);
}
1 change: 1 addition & 0 deletions DataFormats/FWLite/test/run_all_t.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function die { echo $1: status $2 ; exit $2; }

${LOCAL_TEST_DIR}/RefTest_a.sh || die 'Failed to create file' $?
root -b -n -q ${LOCAL_TEST_DIR}/event_looping_cint.C || die 'Failed in event_looping_cint.C' $?
root -b -n -q ${LOCAL_TEST_DIR}/event_looping_consumes_cint.C || die 'Failed in event_looping_cint.C' $?
root -b -n -q ${LOCAL_TEST_DIR}/chainevent_looping_cint.C || die 'Failed in chainevent_looping_cint.C' $?
python3 ${LOCAL_TEST_DIR}/chainEvent_python.py || die 'Failed in chainEvent_python.py' $?
#root -b -n -q ${LOCAL_TEST_DIR}/autoload_with_std.C || die 'Failed in autoload_with_std.C' $?
Expand Down
16 changes: 16 additions & 0 deletions FWCore/Common/interface/EventBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/Common/interface/TriggerResultsByName.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Utilities/interface/EDGetToken.h"

// system include files
#include <typeinfo>
Expand All @@ -52,6 +53,9 @@ namespace edm {
template <typename T>
bool getByLabel(InputTag const&, Handle<T>&) const;

template <typename T>
bool getByToken(edm::EDGetTokenT<T> const& token, edm::Handle<T>& result) const;

template <typename T>
bool get(ProductID const&, Handle<T>&) const;

Expand Down Expand Up @@ -84,6 +88,7 @@ namespace edm {
virtual BasicHandle getByLabelImpl(std::type_info const& iWrapperType,
std::type_info const& iProductType,
InputTag const& iTag) const = 0;
virtual BasicHandle getByTokenImpl(std::type_info const& iProductType, EDGetToken) const = 0;
virtual BasicHandle getImpl(std::type_info const& iProductType, ProductID const& iTag) const = 0;
// ---------- member data --------------------------------
};
Expand All @@ -99,6 +104,17 @@ namespace edm {
return true;
}

template <typename T>
bool EventBase::getByToken(edm::EDGetTokenT<T> const& token, edm::Handle<T>& result) const {
result.clear();
edm::BasicHandle bh = this->getByTokenImpl(typeid(T), token);
result = edm::convert_handle<T>(std::move(bh));
if (result.failedToGet()) {
return false;
}
return true;
}

template <typename T>
bool EventBase::get(ProductID const& pid, Handle<T>& result) const {
result.clear();
Expand Down
1 change: 1 addition & 0 deletions FWCore/Framework/interface/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ namespace edm {
BasicHandle getByLabelImpl(std::type_info const& iWrapperType,
std::type_info const& iProductType,
InputTag const& iTag) const override;
BasicHandle getByTokenImpl(std::type_info const& iProductType, EDGetToken iToken) const override;

//override used by EventBase class
BasicHandle getImpl(std::type_info const& iProductType, ProductID const& pid) const override;
Expand Down
8 changes: 8 additions & 0 deletions FWCore/Framework/src/Event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ namespace edm {
return h;
}

BasicHandle Event::getByTokenImpl(std::type_info const& iProductType, EDGetToken iToken) const {
BasicHandle h = provRecorder_.getByToken_(TypeID(iProductType), PRODUCT_TYPE, iToken, moduleCallingContext_);
if (h.isValid()) {
addToGotBranchIDs(*(h.provenance()));
}
return h;
}

BasicHandle Event::getImpl(std::type_info const&, ProductID const& pid) const {
BasicHandle h = this->getByProductID_(pid);
if (h.isValid()) {
Expand Down

0 comments on commit 1393895

Please sign in to comment.