Skip to content

Commit

Permalink
Merge pull request cms-sw#5515 from wddgit/thinnedCollections1000
Browse files Browse the repository at this point in the history
New feature. Thinned collections.
  • Loading branch information
ktf committed Sep 25, 2014
2 parents 2ee5872 + 87d13c0 commit 8e3e72f
Show file tree
Hide file tree
Showing 138 changed files with 6,300 additions and 373 deletions.
3 changes: 3 additions & 0 deletions DQM/SiStripMonitorHardware/src/SiStripSpyEventMatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "DataFormats/Provenance/interface/BranchIDListHelper.h"
#include "DataFormats/Provenance/interface/EventID.h"
#include "DataFormats/Provenance/interface/ModuleDescription.h"
#include "DataFormats/Provenance/interface/ThinnedAssociationsHelper.h"
#include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
#include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
#include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
Expand Down Expand Up @@ -65,6 +66,7 @@ namespace sistrip {

eventPrincipal_.reset(new edm::EventPrincipal(source_->productRegistry(),
source_->branchIDListHelper(),
source_->thinnedAssociationsHelper(),
*processConfiguration_,
nullptr));
}
Expand All @@ -75,6 +77,7 @@ namespace sistrip {
edm::InputSourceDescription description(edm::ModuleDescription(),
*productRegistry_,
std::make_shared<edm::BranchIDListHelper>(),
std::make_shared<edm::ThinnedAssociationsHelper>(),
std::make_shared<edm::ActivityRegistry>(),
-1, -1, -1,
edm::PreallocationConfiguration());
Expand Down
33 changes: 31 additions & 2 deletions DataFormats/Common/interface/EDProductGetter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@
//

// user include files
#include "DataFormats/Common/interface/WrapperBase.h"
#include "DataFormats/Provenance/interface/ProductID.h"

// system include files
#include <string>
#include <vector>

#include "boost/utility.hpp"

// forward declarations

namespace edm {

class ProductID;
class WrapperBase;

class EDProductGetter : private boost::noncopyable {

public:
Expand All @@ -36,6 +41,30 @@ namespace edm {
// ---------- const member functions ---------------------
virtual WrapperBase const* getIt(ProductID const&) const = 0;

// getThinnedProduct assumes getIt was already called and failed to find
// the product. The input key is the index of the desired element in the
// container identified by ProductID (which cannot be found).
// If the return value is not null, then the desired element was found
// in a thinned container and key is modified to be the index into
// that thinned container. If the desired element is not found, then
// nullptr is returned.
virtual WrapperBase const* getThinnedProduct(ProductID const&, unsigned int& key) const = 0;

// getThinnedProducts assumes getIt was already called and failed to find
// the product. The input keys are the indexes into the container identified
// by ProductID (which cannot be found). On input the WrapperBase pointers
// must all be set to nullptr (except when the function calls itself
// recursively where non-null pointers mark already found elements).
// Thinned containers derived from the product are searched to see
// if they contain the desired elements. For each that is
// found, the corresponding WrapperBase pointer is set and the key
// is modified to be the key into the container where the element
// was found. The WrapperBase pointers might or might not all point
// to the same thinned container.
virtual void getThinnedProducts(ProductID const& pid,
std::vector<WrapperBase const*>& foundContainers,
std::vector<unsigned int>& keys) const = 0;

unsigned int transitionIndex() const {
return transitionIndex_();
}
Expand Down
28 changes: 22 additions & 6 deletions DataFormats/Common/interface/Ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ namespace edm {

/// Checks if collection is in memory or available
/// in the event. No type checking is done.
bool isAvailable() const {return core_.isAvailable();}
bool isAvailable() const;

/// Checks if this Ptr is transient (i.e. not persistable).
bool isTransient() const {return core_.isTransient();}
Expand Down Expand Up @@ -191,14 +191,22 @@ namespace edm {
template<typename C>
T const* getItem_(C const* product, key_type iKey);

void getData_() const {
if(!hasProductCache() && 0 != productGetter()) {
void const* ad = 0;
void getData_(bool throwIfNotFound = true) const {
if(!hasProductCache() && productGetter() != nullptr) {
WrapperBase const* prod = productGetter()->getIt(core_.id());
unsigned int iKey = key_;
if(prod == nullptr) {
core_.productNotFoundException(typeid(T));
prod = productGetter()->getThinnedProduct(core_.id(), iKey);
if(prod == nullptr) {
if(throwIfNotFound) {
core_.productNotFoundException(typeid(T));
} else {
return;
}
}
}
prod->setPtr(typeid(T), key_, ad);
void const* ad = nullptr;
prod->setPtr(typeid(T), iKey, ad);
core_.setProductPtr(ad);
}
}
Expand Down Expand Up @@ -235,6 +243,14 @@ namespace edm {
return reinterpret_cast<T const*>(core_.productPtr());
}

template<typename T>
inline
bool
Ptr<T>::isAvailable() const {
getData_(false);
return hasProductCache();
}

template<typename T>
inline
bool
Expand Down
17 changes: 10 additions & 7 deletions DataFormats/Common/interface/PtrVectorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Package: Common
// Class : PtrVectorBase
//
/**\class PtrVectorBase PtrVectorBase.h DataFormats/Common/interface/PtrVectorBase.h
/**\class edm::PtrVectorBase
Description: Base class for PtrVector
Expand Down Expand Up @@ -60,11 +60,9 @@ namespace edm {

bool hasCache() const { return !cachedItems_.empty(); }

bool hasProductCache() const { return 0 == core_.productPtr(); }

/// True if the data is in memory or is available in the Event
/// No type checking is done.
bool isAvailable() const { return core_.isAvailable(); }
bool isAvailable() const;

/// Is the RefVector empty
bool empty() const {return indicies_.empty();}
Expand All @@ -90,7 +88,7 @@ namespace edm {
bool isTransient() const {return core_.isTransient();}

void const* product() const {
return 0;
return 0;
}

protected:
Expand All @@ -103,10 +101,12 @@ namespace edm {

std::vector<void const*>::const_iterator void_begin() const {
getProduct_();
checkCachedItems();
return cachedItems_.begin();
}
std::vector<void const*>::const_iterator void_end() const {
getProduct_();
checkCachedItems();
return cachedItems_.end();
}

Expand All @@ -116,7 +116,7 @@ namespace edm {
return TPtr(reinterpret_cast<typename TPtr::value_type const*>(cachedItems_[iIndex]),
indicies_[iIndex]);
}
if (hasCache()) {
if (hasCache() && (cachedItems_[iIndex] != nullptr || productGetter() == nullptr)) {
return TPtr(this->id(),
reinterpret_cast<typename TPtr::value_type const*>(cachedItems_[iIndex]),
indicies_[iIndex]);
Expand All @@ -130,7 +130,7 @@ namespace edm {
return TPtr(reinterpret_cast<typename TPtr::value_type const*>(*iIt),
indicies_[iIt - cachedItems_.begin()]);
}
if (hasCache()) {
if (hasCache() && (*iIt != nullptr || productGetter() == nullptr)) {
return TPtr(this->id(),
reinterpret_cast<typename TPtr::value_type const*>(*iIt),
indicies_[iIt - cachedItems_.begin()]);
Expand All @@ -145,6 +145,9 @@ namespace edm {
assert(false);
return *reinterpret_cast<const std::type_info*>(0);
}

void checkCachedItems() const;

// ---------- member data --------------------------------
RefCore core_;
std::vector<key_type> indicies_;
Expand Down
26 changes: 23 additions & 3 deletions DataFormats/Common/interface/Ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ namespace edm {

/// Checks if collection is in memory or available
/// in the Event. No type checking is done.
bool isAvailable() const {return product_.isAvailable();}
bool isAvailable() const;

/// Checks if this ref is transient (i.e. not persistable).
bool isTransient() const {return product_.isTransient();}
Expand Down Expand Up @@ -431,8 +431,8 @@ namespace edm {

/// Checks if collection is in memory or available
/// in the Event. No type checking is done.
bool isAvailable() const {return product_.isAvailable();}
bool isAvailable() const;

/// Checks if this ref is transient (i.e. not persistable).
bool isTransient() const {return product_.isTransient();}

Expand Down Expand Up @@ -608,6 +608,26 @@ namespace edm {
return isNull() ? 0 : edm::template getProduct<std::vector<E> >(product_.toRefCore());
}

template <typename C, typename T, typename F>
inline
bool
Ref<C, T, F>::isAvailable() const {
if(product_.isAvailable()) {
return true;
}
return isThinnedAvailable<C>(product_, index_);
}

template <typename E>
inline
bool
Ref<REF_FOR_VECTOR_ARGS>::isAvailable() const {
if(product_.isAvailable()) {
return true;
}
return isThinnedAvailable<std::vector<E> >(product_.toRefCore(), key());
}

/// Dereference operator
template <typename C, typename T, typename F>
inline
Expand Down
6 changes: 6 additions & 0 deletions DataFormats/Common/interface/RefCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ namespace edm {

WrapperBase const* getProductPtr(std::type_info const& type) const;

WrapperBase const* tryToGetProductPtr(std::type_info const& type) const;

WrapperBase const* getThinnedProductPtr(std::type_info const& type, unsigned int& thinnedKey) const;

bool isThinnedAvailable(unsigned int thinnedKey) const;

void productNotFoundException(std::type_info const& type) const;

void wrongTypeException(std::type_info const& expectedType, std::type_info const& actualType) const;
Expand Down
57 changes: 57 additions & 0 deletions DataFormats/Common/interface/RefCoreGet.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,62 @@ namespace edm {
}
return refcore::getProductPtr_<T>(ref);
}

namespace refcore {
template <typename T>
inline
T const*
tryToGetProductPtr_(RefCore const& ref) {
//if (isNull()) throwInvalidReference();
assert (!ref.isTransient());
WrapperBase const* product = ref.tryToGetProductPtr(typeid(T));
if(product == nullptr) {
return nullptr;
}
Wrapper<T> const* wrapper = static_cast<Wrapper<T> const*>(product);
ref.setProductPtr(wrapper->product());
return wrapper->product();
}
}

template <typename T>
inline
T const*
tryToGetProduct(RefCore const& ref) {
T const* p = static_cast<T const*>(ref.productPtr());
if (p != 0) return p;
if (ref.isTransient()) {
ref.nullPointerForTransientException(typeid(T));
}
return refcore::tryToGetProductPtr_<T>(ref);
}

namespace refcore {
template <typename T>
inline
T const*
getThinnedProductPtr_(RefCore const& ref, unsigned int& thinnedKey) {
assert (!ref.isTransient());
WrapperBase const* product = ref.getThinnedProductPtr(typeid(T), thinnedKey);
Wrapper<T> const* wrapper = static_cast<Wrapper<T> const*>(product);
// Do not cache the pointer to a thinned collection
// ref.setProductPtr(wrapper->product());
return wrapper->product();
}
}

template <typename T>
inline
T const*
getThinnedProduct(RefCore const& ref, unsigned int& thinnedKey) {
// The pointer to a thinned collection will never be cached
// T const* p = static_cast<T const*>(ref.productPtr());
// if (p != 0) return p;

if (ref.isTransient()) {
ref.nullPointerForTransientException(typeid(T));
}
return refcore::getThinnedProductPtr_<T>(ref, thinnedKey);
}
}
#endif
44 changes: 44 additions & 0 deletions DataFormats/Common/interface/RefItemGet.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ namespace edm {
return p;
}
};

template< typename C, typename T, typename F>
struct GetPtrImpl<C, T, F, unsigned int> {
static T const* getPtr_(RefCore const& product, unsigned int key) {
C const* prod = edm::template tryToGetProduct<C>(product);
if(prod != nullptr) {
F func;
T const* p = func(*prod, key);
return p;
}
unsigned int thinnedKey = key;
prod = edm::template getThinnedProduct<C>(product, thinnedKey);
F func;
T const* p = func(*prod, thinnedKey);
return p;
}
};
}

template <typename C, typename T, typename F, typename KEY>
Expand All @@ -42,6 +59,33 @@ namespace edm {
return p;
}

namespace refitem {
template< typename C, typename KEY>
struct IsThinnedAvailableImpl {
static bool isThinnedAvailable_(RefCore const& product, KEY const& key) {
return false;
}
};

template< typename C >
struct IsThinnedAvailableImpl<C, unsigned int> {
static bool isThinnedAvailable_(RefCore const& ref, unsigned int key) {
if(ref.productPtr() != nullptr) {
return true;
}
if (ref.isTransient()) {
return false;
}
return ref.isThinnedAvailable(key);
}
};
}

template <typename C, typename KEY>
inline
bool isThinnedAvailable(RefCore const& product, KEY const& iKey) {
return refitem::IsThinnedAvailableImpl<C, KEY>::isThinnedAvailable_(product, iKey);
}
}

#endif
Loading

0 comments on commit 8e3e72f

Please sign in to comment.