 |
Belle II Software
release-05-02-19
|
13 #include <framework/datastore/DataStore.h>
14 #include <framework/datastore/StoreAccessorBase.h>
15 #include <framework/utilities/ArrayIterator.h>
16 #include <framework/dataobjects/RelationElement.h>
17 #include <framework/dataobjects/RelationContainer.h>
18 #include <framework/logging/Logger.h>
25 template <
class T>
class StoreArray;
64 class RelationArray:
public StoreAccessorBase {
67 typedef ObjArrayIterator<const TClonesArray, const RelationElement>
const_iterator;
106 template <
class MapType = std::map<index_type, consol
idation_type> >
class ReplaceMap {
115 typename MapType::const_iterator iter =
m_replace.find(old);
119 return std::make_pair(old,
false);
134 template <
class VecType = std::vector<consol
idation_type> >
class ReplaceVec {
155 bool create(
bool replace =
false)
180 template <
class FROM,
class TO>
RelationArray(
const StoreArray<FROM>& from,
const StoreArray<TO>& to,
const std::string& name =
"",
183 RelationContainer::Class(), false),
189 B2FATAL(
"Tried to create RelationArray '" <<
m_name <<
"' with a durability larger than the StoreArrays it relates");
202 StoreAccessorBase(DataStore::relationName(fromAccessor.first, toAccessor.first), durability, RelationContainer::Class(), false),
208 B2FATAL(
"Tried to create RelationArray '" <<
m_name <<
"' with a durability larger than the StoreArrays it relates");
225 B2FATAL(
"Cannot guess relation name, please supply correct name");
238 inline operator bool()
const {
return isValid(); }
251 B2FATAL(
"trying to get accessor params from non-existing relation (this is likely a framework bug)");
260 B2FATAL(
"trying to get accessor params from non-existing relation (this is likely a framework bug)");
271 void clear()
override
274 (*m_relations)->elements().Delete();
298 std::vector<weight_type> weights(to.size(), weight);
308 void add(
index_type from,
const std::vector<index_type>& to,
const std::vector<weight_type>& weights)
311 new(
next()) RelationElement(from, to, weights);
322 template <
class InputIterator>
void add(
index_type from,
const InputIterator&
begin,
const InputIterator&
end)
333 void consolidate() { consolidate<Identity, Identity>(); }
365 template<
class FunctionFrom,
class FunctionTo>
void consolidate(
const
366 FunctionFrom& replaceFrom = FunctionFrom(),
const FunctionTo&
384 StoreAccessorBase(params.first, params.second, RelationContainer::Class(), false),
387 if (params.first.empty()) {
388 B2FATAL(
"Cannot guess relation name, please supply correct name");
396 int index = (*m_relations)->elements().GetLast() + 1;
397 return static_cast<RelationElement*
>((*m_relations)->elements().AddrAt(index));
403 if (array.second == 0 && array.first.empty())
407 B2FATAL(
"Relation '" <<
m_name <<
"' exists but points " << direction <<
" wrong array:"
408 <<
" requested " << array.first <<
"(" << array.second <<
")"
409 <<
", got " << rel.first <<
"(" << rel.second <<
")"
438 void assertValid()
const {
if (!
isValid()) B2FATAL(
"RelationArray does not point to valid StoreObject"); }
448 B2FATAL(
"Couldn't create relation " <<
m_name <<
"!");
467 template<
class FunctionFrom,
class FunctionTo>
471 B2ERROR(
"Cannot consolidate an invalid relation (" <<
m_name <<
")");
474 typedef std::map<index_type, weight_type> element_t;
475 typedef std::map<index_type, element_t > buffer_t;
481 buffer_t::iterator lastFromIter = buffer.end();
482 unsigned int nElements = (*m_relations)->getEntries();
483 TClonesArray& elements = (*m_relations)->elements();
484 for (
unsigned int i = 0; i < nElements; ++i) {
494 if (from.first != lastFromIndex || lastFromIter == buffer.end()) {
495 lastFromIter = buffer.insert(make_pair(from.first, element_t())).first;
496 lastFromIndex = from.first;
499 size_t size = element.getSize();
500 for (
size_t j = 0; j < size; ++j) {
505 double weight = element.getWeight(j);
511 if (from.second || to.second) {
519 lastFromIter->second[to.first] += weight;
525 for (buffer_t::iterator iter = buffer.begin(); iter != buffer.end(); ++iter) {
526 add(iter->first, iter->second.begin(), iter->second.end());
void clear() override
Clear all elements from the relation.
Low-level class to create/modify relations between StoreArrays.
void setModified(bool modified)
Set modified flag of underlying container.
bool isDefaultConstructed() const
Returns true if no information was set yet or Clear() was called.
@ c_negativeWeight
Flip the sign of the weight to become negative if the original element got re-attributed.
void consolidate()
Consolidate Relation Elements.
static DataStore & Instance()
Instance of singleton Store.
Struct for identity transformation on indices.
MapType & m_replace
Reference of the used replacement map.
@ c_doNothing
Do nothing, just treat it as reordering.
consolidation_type operator()(index_type old) const
Take old index and return the new index.
Class to store relations between StoreArrays in the DataStore.
AccessorParams getAccessorParams() const
Return pair of name and durability under which stored object is saved.
Provides access to fast ( O(log n) ) bi-directional lookups on a specified relation.
std::pair< index_type, bool > consolidation_type
Typedef declaring the return value of any consolidation mapping.
const AccessorParams & getFromAccessorParams() const
Return the AccessorParams the attached relation points from.
const std::string & getName() const
Return name under which the object is saved in the DataStore.
EConsolidationAction
Modification actions for the consolidate member.
const_iterator end() const
Return const_iterator to last entry +1.
@ c_zeroWeight
Set the weight of the relation to 0 if the original element got re-attributed.
bool create(bool replace=false)
Create an empty relation array in the data store.
TObject ** getObject(const StoreAccessorBase &accessor)
Get a pointer to a pointer of an object in the DataStore.
RelationElement::index_type index_type
Typedef to simplify use of correct index_type.
VecType & m_replace
Reference of the used replacement vector.
consolidation_type operator()(index_type old) const
Take old index and return the new index.
AccessorParams m_accessorTo
Accessor params for to array.
RelationElement * next()
Return address where the next RelationElement should be created.
std::string m_name
Store name under which this object/array is saved.
RelationContainer ** m_relations
Pointer that actually holds the relations.
Class to store a single element of a relation.
std::pair< std::string, DataStore::EDurability > AccessorParams
Pair of parameters needed to find an object in the DataStore.
RelationElement::weight_type weight_type
Typedef to simplify use of correct weight_type.
DataStore::EDurability m_durability
Store durability under which the object/array is saved.
ObjArrayIterator< const TClonesArray, const RelationElement > const_iterator
STL-like const_iterator over the T objects (not T* ).
consolidation_type operator()(index_type old) const
Take old index and return the new index.
void setFromName(const std::string &name)
Set name of the StoreArray we relate from.
Abstract base class for different kinds of events.
@ c_deleteElement
Delete the whole relation element if the original element got re-attributed.
const_iterator begin() const
Return const_iterator to first entry.
bool isValid() const
Check whether the object was created.
const AccessorParams & getToAccessorParams() const
Return the AccessorParams the attached relation points to.
void ensureAttached() const
Attach to relation, if necessary.
void assertValid() const
check that pointer exits, otherwise bail out.
unsigned int index_type
type used for indices.
void add(index_type from, index_type to, weight_type weight=1.0)
Add a new element to the relation.
bool getModified() const
Get modified flag of underlying container.
Struct to replace indices based on a sequential container.
void checkRelation(const std::string &direction, const AccessorParams &array, const AccessorParams &rel) const
Check that the AccessorParams stored in the relation and the one given to the constructor are the sam...
Struct to replace indices based on a map-like container.
RelationArray(const StoreArray< FROM > &from, const StoreArray< TO > &to, const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
Constructor which takes both store arrays and performs some sanity checks on the relation.
Class to store a bidirectional index between two StoreArrays.
int getEntries() const
Get the number of elements.
~RelationArray()
Empty destructor.
void assertCreated()
Create relation, if necessary.
ReplaceMap(MapType &replace)
Set reference to used replacement map.
AccessorParams m_accessorFrom
Accessor params for from array.
bool createObject(TObject *object, bool replace, const StoreAccessorBase &accessor)
Create a new object/array in the DataStore or add an existing one.
float weight_type
type used for weights.
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
StoreAccessorBase(const std::string &name, DataStore::EDurability durability, TClass *objClass, bool isArray)
Constructor to access an object or array in the DataStore.
ReplaceVec(VecType &replace)
Set reference to used replacement vector.
const RelationElement & operator[](int i) const
Imitate array functionality.
EDurability
Durability types.