Belle II Software development
MapMergeable< T > Class Template Reference

Wrap an STL map to make it mergeable. More...

#include <MapMergeable.h>

Inheritance diagram for MapMergeable< T >:
Mergeable

Public Member Functions

template<class ... Args>
 MapMergeable (Args &&... params)
 Constructor, forwards all arguments to T constructor.
 
T & get ()
 Get the wrapped standard object.
 
const T & get () const
 Get the wrapped standard object.
 
virtual void merge (const Mergeable *other)
 Merge object 'other' into this one.
 
virtual void clear ()
 Clear content of this object (e.g.
 
virtual void removeSideEffects ()
 An ugly little method that is called before event() for input and worker processes.
 
virtual Long64_t Merge (TCollection *hlist)
 Allow merging using TFileMerger if saved directly to a file.
 
virtual void Reset ()
 Root-like Reset function for "template compatibility" with ROOT objects.
 
virtual void SetDirectory (TDirectory *)
 Root-like SetDirectory function for "template compatibility" with ROOT objects.
 

Private Member Functions

 ClassDef (MapMergeable, 1)
 Wrap an STL map to make it mergeable.
 

Private Attributes

m_wrapped
 Wrapped object.
 

Detailed Description

template<class T>
class Belle2::MapMergeable< T >

Wrap an STL map to make it mergeable.

Should work out-of-the-box with std::map and 'similar' containers.

To use it to save data in your module:

setPropertyFlags(c_Parallelprocessing | c_terminateInAllProcesses);

create MapMergeable<X> in initalize (or in your constructor) of durability DataStore::c_Persistent, register it by calling registerInDataStore() and construct() the actual container.

Each template instance needs its own entry in framework/pcore/include/linkdef.h, please contact the framework librarian if your use case requires other classes than those in there.

See also
Mergeable

Definition at line 34 of file MapMergeable.h.

Constructor & Destructor Documentation

◆ MapMergeable() [1/2]

MapMergeable ( )
inline

Definition at line 36 of file MapMergeable.h.

36: m_wrapped() { }
T m_wrapped
Wrapped object.
Definition: MapMergeable.h:80

◆ ~MapMergeable()

virtual ~MapMergeable ( )
inlinevirtual

Definition at line 38 of file MapMergeable.h.

38{ }

◆ MapMergeable() [2/2]

MapMergeable ( Args &&...  params)
inlineexplicit

Constructor, forwards all arguments to T constructor.

Definition at line 40 of file MapMergeable.h.

40: m_wrapped(std::forward<Args>(params)...) { }

Member Function Documentation

◆ clear()

virtual void clear ( )
inlinevirtual

Clear content of this object (e.g.

set to zeroes).

Called after sending the objects to another process. If no clearing is performed, the same data (e.g. histogram entries) might be added again and again in each event.

Implements Mergeable.

Definition at line 73 of file MapMergeable.h.

74 {
75 m_wrapped.clear();
76 }

◆ get() [1/2]

T & get ( )
inline

Get the wrapped standard object.

Definition at line 43 of file MapMergeable.h.

43{ return m_wrapped; }

◆ get() [2/2]

const T & get ( ) const
inline

Get the wrapped standard object.

Definition at line 46 of file MapMergeable.h.

46{ return m_wrapped; }

◆ merge()

virtual void merge ( const Mergeable other)
inlinevirtual

Merge object 'other' into this one.

Your derived class should implement this function. You can static_cast 'other' to your own type (when called, this and other are guaranteed to point to objects of the same type).

Note that 'other' will be deleted after the merge, so make sure you copy all data from it that you will need.

Implements Mergeable.

Definition at line 55 of file MapMergeable.h.

56 {
57 auto* otherMergeable = static_cast<const MapMergeable*>(other);
58 for (const auto& element : otherMergeable->get()) {
59 auto it = m_wrapped.find(element.first);
60 if (it != m_wrapped.end()) {
61 it->second += element.second;
62 } else {
63 m_wrapped.insert(element);
64 }
65 }
66 }

◆ Merge()

Long64_t Merge ( TCollection *  hlist)
virtualinherited

Allow merging using TFileMerger if saved directly to a file.

Note
dictionaries containing your Mergeable class need to be loaded, so 'hadd' will not work currently.

Definition at line 14 of file Mergeable.cc.

15{
16 Long64_t nMerged = 0;
17 if (hlist) {
18 const Mergeable* xh = nullptr;
19 TIter nxh(hlist);
20 while ((xh = dynamic_cast<Mergeable*>(nxh()))) {
21 // Add xh to me
22 merge(xh);
23 ++nMerged;
24 }
25 }
26 return nMerged;
27}
Abstract base class for objects that can be merged.
Definition: Mergeable.h:31
virtual void merge(const Mergeable *other)=0
Merge object 'other' into this one.

◆ removeSideEffects()

virtual void removeSideEffects ( )
inlinevirtualinherited

An ugly little method that is called before event() for input and worker processes.

Main use case is to detach any attached TFile from this object. In the output process, it can stay attached (and grow as much as it likes).

Reimplemented in RootMergeable< T >.

Definition at line 58 of file Mergeable.h.

58{}

◆ Reset()

virtual void Reset ( )
inlinevirtualinherited

Root-like Reset function for "template compatibility" with ROOT objects.

Alias for clear().

Definition at line 66 of file Mergeable.h.

66{clear();}
virtual void clear()=0
Clear content of this object (e.g.

◆ SetDirectory()

virtual void SetDirectory ( TDirectory *  )
inlinevirtualinherited

Root-like SetDirectory function for "template compatibility" with ROOT objects.

Does nothing.

Definition at line 68 of file Mergeable.h.

68{}

Member Data Documentation

◆ m_wrapped

T m_wrapped
private

Wrapped object.

Definition at line 80 of file MapMergeable.h.


The documentation for this class was generated from the following file: