hpstr
The Heavy Photon Search Toolkit for Reconstruction (hpstr) provides an interface to physics data from the HPS experiment saved in the LCIO format and converts it into an ROOT based format.
Loading...
Searching...
No Matches
Bus::Passenger< BaggageType > Class Template Reference
Inheritance diagram for Bus::Passenger< BaggageType >:
Bus::Seat

Classes

struct  the_type
class  DeleteObjectStatus

Public Member Functions

 Passenger ()
virtual ~Passenger ()
virtual TBranch * attach (TTree *tree, const std::string &branch_name, bool can_create)
const BaggageType & get () const
void update (const BaggageType &updated_obj)
virtual void clear ()
virtual void stream (std::ostream &s) const
Public Member Functions inherited from Bus::Seat
virtual ~Seat ()

Private Member Functions

template<typename T>
TBranch * attach (the_type< T > t, TTree *tree, const std::string &branch_name, bool can_create)
TBranch * attachBasic (TTree *tree, const std::string &branch_name, bool can_create)
TBranch * attach (the_type< bool > t, TTree *tree, const std::string &branch_name, bool can_create)
TBranch * attach (the_type< short > t, TTree *tree, const std::string &branch_name, bool can_create)
TBranch * attach (the_type< int > t, TTree *tree, const std::string &branch_name, bool can_create)
TBranch * attach (the_type< long > t, TTree *tree, const std::string &branch_name, bool can_create)
TBranch * attach (the_type< float > t, TTree *tree, const std::string &branch_name, bool can_create)
TBranch * attach (the_type< double > t, TTree *tree, const std::string &branch_name, bool can_create)
void clear (the_type< bool > t)
void clear (the_type< short > t)
void clear (the_type< int > t)
void clear (the_type< long > t)
void clear (the_type< float > t)
void clear (the_type< double > t)
template<typename T>
void clear (the_type< T > t)
template<typename Content>
void clear (the_type< std::vector< Content > > t)
template<typename Key, typename Val>
void clear (the_type< std::map< Key, Val > > t)
template<typename T>
void post_update (the_type< T > t)
template<typename T>
void stream (the_type< T > t, std::ostream &s) const
template<typename Content>
void stream (the_type< std::vector< Content > > t, std::ostream &s) const
template<typename Key, typename Val>
void stream (the_type< std::map< Key, Val > > t, std::ostream &s) const

Private Attributes

BaggageType * baggage_
 A pointer to the baggage we own and created.

Friends

std::ostream & operator<< (std::ostream &s, const framework::Bus::Passenger< BaggageType > &p)

Detailed Description

template<typename BaggageType>
class framework::Bus::Passenger< BaggageType >

A bus passenger

We store the bus passenger's baggage as a dynamically created object. This is necessary so that we can interface with the ROOT TTree.

Here we do all the heavy lifting of carrying a certain type of object with specializations for clearing, sorting, and printing.

Note
Printing specialization is possible if we include the requirement that all event objects must define the 'operator<<' method.

Core Functions

  1. Define the type of "baggage" that is being carried on the event bus
  2. "Attach" the baggage to a output or input TTree for writing or reading
  3. "Update" the contents of the baggage
  4. "Clear" the baggage to a default or empty state at the end of each event
  5. "Print" the baggage to a ostream (via the operator<<)

Basic Structure

The method of specializing the clear, post_update, and attach methods to the different types of passenger's baggage is derived from a helpful blog post. Basically, the idea is to add a parameter input into the functions that "stores" the type that we are currently carrying. This allows the compiler to choose the best method from the overloaded functions by matching the types of parameters.

Possible Types

  • BSILFD - bool, short, int, long, float, double
  • class with Clear() method defined
  • std::vector of type with operator< defined
  • std::map with key type that has operator< defined
Template Parameters

in] BaggageType the type of object that this passenger carries

Definition at line 305 of file Bus.h.

Constructor & Destructor Documentation

◆ Passenger()

template<typename BaggageType>
Passenger ( )
inline

Constructor

Dynamically create a default constructed instance of our baggage.

Note
This requires that all of the BaggageTypes are default-constructible. This is a simple requirement because ROOT dictionary generation already requires a default constructor to be defined.

Definition at line 317 of file Bus.h.

◆ ~Passenger()

template<typename BaggageType>
virtual ~Passenger ( )
inlinevirtual

Destructor

Clean up after ourselves by deleting the object we created earlier in the constructor.

Note
Because the baggage_ member variable may be attached to a TTree, this object needs to be destructed carefully. The TTree assumes that the object we attach to it is valid until we either destruct the TTree or reset its addresses. As such, if we want to destruct a Passenger, we need to know that its baggage is either not attached to a TTree or the TTree it was attached to has stopped looking at it (either by deletion or reset).

Definition at line 336 of file Bus.h.

Member Function Documentation

◆ attach() [1/8]

template<typename BaggageType>
TBranch * attach ( the_type< bool > t,
TTree * tree,
const std::string & branch_name,
bool can_create )
inlineprivate

Specialization for bools

See also
attachBasic for the actual implementation
Parameters
tUnused, only helping compiler choose the correct method
[in]treepointer to TTree to attach to
[in]branch_namename of branch we should attach to
[in]can_createallow us to create a branch on tree if needed
Returns
pointer to branch that we attached to (maybe be null)

Definition at line 566 of file Bus.h.

◆ attach() [2/8]

template<typename BaggageType>
TBranch * attach ( the_type< double > t,
TTree * tree,
const std::string & branch_name,
bool can_create )
inlineprivate

Specialization for doubles

See also
attachBasic for the actual implementation
Parameters
tUnused, only helping compiler choose the correct method
[in]treepointer to TTree to attach to
[in]branch_namename of branch we should attach to
[in]can_createallow us to create a branch on tree if needed
Returns
pointer to branch that we attached to (maybe be null)

Definition at line 646 of file Bus.h.

◆ attach() [3/8]

template<typename BaggageType>
TBranch * attach ( the_type< float > t,
TTree * tree,
const std::string & branch_name,
bool can_create )
inlineprivate

Specialization for floats

See also
attachBasic for the actual implementation
Parameters
tUnused, only helping compiler choose the correct method
[in]treepointer to TTree to attach to
[in]branch_namename of branch we should attach to
[in]can_createallow us to create a branch on tree if needed
Returns
pointer to branch that we attached to (maybe be null)

Definition at line 630 of file Bus.h.

◆ attach() [4/8]

template<typename BaggageType>
TBranch * attach ( the_type< int > t,
TTree * tree,
const std::string & branch_name,
bool can_create )
inlineprivate

Specialization for ints

See also
attachBasic for the actual implementation
Parameters
tUnused, only helping compiler choose the correct method
[in]treepointer to TTree to attach to
[in]branch_namename of branch we should attach to
[in]can_createallow us to create a branch on tree if needed
Returns
pointer to branch that we attached to (maybe be null)

Definition at line 598 of file Bus.h.

◆ attach() [5/8]

template<typename BaggageType>
TBranch * attach ( the_type< long > t,
TTree * tree,
const std::string & branch_name,
bool can_create )
inlineprivate

Specialization for longs

See also
attachBasic for the actual implementation
Parameters
tUnused, only helping compiler choose the correct method
[in]treepointer to TTree to attach to
[in]branch_namename of branch we should attach to
[in]can_createallow us to create a branch on tree if needed
Returns
pointer to branch that we attached to (maybe be null)

Definition at line 614 of file Bus.h.

◆ attach() [6/8]

template<typename BaggageType>
TBranch * attach ( the_type< short > t,
TTree * tree,
const std::string & branch_name,
bool can_create )
inlineprivate

Specialization for shorts

See also
attachBasic for the actual implementation
Parameters
tUnused, only helping compiler choose the correct method
[in]treepointer to TTree to attach to
[in]branch_namename of branch we should attach to
[in]can_createallow us to create a branch on tree if needed
Returns
pointer to branch that we attached to (maybe be null)

Definition at line 582 of file Bus.h.

◆ attach() [7/8]

template<typename BaggageType>
template<typename T>
TBranch * attach ( the_type< T > t,
TTree * tree,
const std::string & branch_name,
bool can_create )
inlineprivate

Attach to a (potentially) new branch on the input tree.

This is the attachment mechanism used for all non-basic types. The specializations for the basic types (BSILFD - bool, short, int, long, float, double) use attachBasic which is slightly different in its branching.

See also
TBranchElement::SetObject for attaching an object to a branch that already exists
TTree::Branch for creating a new branch

With complicated objects like most of our event bus baggage, ROOT is able to deduce the type as long as we load a ROOT dictionary with all of the classes defined inside of it.

Parameters
tUnused, only helping compiler choose the correct method
[in]treepointer to TTree to attach to
[in]branch_namename of branch we should attach to
[in]can_createallow us to create a branch on tree if needed
Returns
pointer to branch that we attached to (maybe be null)

If the branch already exists, we need to explicitly remind ROOT that we own the object we are going to pass and it shouldn't try to delete it. Then we set the branch object to our baggage_.

Idk why this is necessary. The docs say that you can tell ROOT you own an object by using a non-null pointer passed to SetObject (or its descendent SetAddress); however, in the implementation of TBranchElement::SetAddressImpl, the kDeleteObject status bit is merely tested and the validity of the passed reference is never checked. This means the bits value is leftover from however the branch was initialized which when reading is ROOT-owned objects.

If the branch doesn't already exist and we are allowed to make one, we make a new one passing our baggage.

The extra . character informs ROOT to fully-specify the final sub-branch names after branch splitting. This is helpful for columnar analyses like with uproot, dask, and awkward.

Definition at line 476 of file Bus.h.

◆ attach() [8/8]

template<typename BaggageType>
virtual TBranch * attach ( TTree * tree,
const std::string & branch_name,
bool can_create )
inlinevirtual

Attach this passenger to the input tree.

Now we know what type of object this passenger is carrying, so we can attach it to the tree.

If the 'can_create' parameter is true and a branch of the input name doesn't exist on the tree, we create a new branch of the input branch name. If this parameter is false, we simply return the nullptr signifying that this branch doesn't exist.

See also
attach(the_type<T>,TTree*,const std::string&,bool) for how we attach to higher-level classes
attachBasic(TTree*,const std::string&,bool) for how we attach to basic types
Parameters
[in]treepointer to TTree to attach to
[in]branch_namename of branch we should attach to
[in]can_createallow us to create a branch on tree if needed
Returns
pointer to branch that we attached to (maybe be null)

Implements Bus::Seat.

Definition at line 361 of file Bus.h.

◆ attachBasic()

template<typename BaggageType>
TBranch * attachBasic ( TTree * tree,
const std::string & branch_name,
bool can_create )
inlineprivate

Attach a basic type

We assume that the current baggage is one of BSILFD bool, short, int, long, float, double

We need this attaching mechanism to be different from the higher-level objects because TTree requires different inputs and returns different outputs for the basic types.

See also
TBranch::SetAddress for attaching a basic type to a branch that already exists
TTree::Branch for creating a new branch
typeid for identifying the type of an object
Parameters
[in]treepointer to TTree to attach to
[in]branch_namename of branch we should attach to
[in]can_createallow us to create a branch on tree if needed
Returns
pointer to branch that we attached to (maybe be null)

Definition at line 535 of file Bus.h.

◆ clear() [1/10]

template<typename BaggageType>
virtual void clear ( )
inlinevirtual

Reset the object we are carrying to an undefined state.

We call the overloaded, templated clear method that allows the compiler to deduce which implementation to use depending on the type of baggage we are carrying.

Implements Bus::Seat.

Definition at line 395 of file Bus.h.

◆ clear() [2/10]

template<typename BaggageType>
void clear ( the_type< bool > t)
inlineprivate

Clear bool by setting it to false.

Parameters
tUnused, only helping compiler choose the correct method

Definition at line 656 of file Bus.h.

◆ clear() [3/10]

template<typename BaggageType>
void clear ( the_type< double > t)
inlineprivate

Clear double by setting it to the minimum defined by the compiler.

Parameters
tUnused, only helping compiler choose the correct method

Definition at line 694 of file Bus.h.

◆ clear() [4/10]

template<typename BaggageType>
void clear ( the_type< float > t)
inlineprivate

Clear float by setting it to the minimum defined by the compiler.

Parameters
tUnused, only helping compiler choose the correct method

Definition at line 686 of file Bus.h.

◆ clear() [5/10]

template<typename BaggageType>
void clear ( the_type< int > t)
inlineprivate

Clear int by setting it to the minimum defined by the compiler.

Parameters
tUnused, only helping compiler choose the correct method

Definition at line 670 of file Bus.h.

◆ clear() [6/10]

template<typename BaggageType>
void clear ( the_type< long > t)
inlineprivate

Clear long by setting it to the minimum defined by the compiler.

Parameters
tUnused, only helping compiler choose the correct method

Definition at line 678 of file Bus.h.

◆ clear() [7/10]

template<typename BaggageType>
void clear ( the_type< short > t)
inlineprivate

Clear short by setting it to the minimum defined by the compiler.

Parameters
tUnused, only helping compiler choose the correct method

Definition at line 662 of file Bus.h.

◆ clear() [8/10]

template<typename BaggageType>
template<typename Key, typename Val>
void clear ( the_type< std::map< Key, Val > > t)
inlineprivate

Clear a map by calling the std::map::clear method.

Parameters
tUnused, only helping compiler choose the correct method

Definition at line 727 of file Bus.h.

◆ clear() [9/10]

template<typename BaggageType>
template<typename Content>
void clear ( the_type< std::vector< Content > > t)
inlineprivate

Clear a vector by calling the std::vector::clear method.

Parameters
tUnused, only helping compiler choose the correct method

Definition at line 718 of file Bus.h.

◆ clear() [10/10]

template<typename BaggageType>
template<typename T>
void clear ( the_type< T > t)
inlineprivate

Clear a general class by calling its 'Clear' method.

Note
The capitalization of 'Clear' is a relic of when the event bus objects needed to inherit from TObject. We could simplify this code by renaming the 'Clear' method to 'clear' which would match std::vector and std::map.
Parameters
tUnused, only helping compiler choose the correct method

Definition at line 709 of file Bus.h.

◆ get()

template<typename BaggageType>
const BaggageType & get ( ) const
inline

Get the object this passenger is carrying

Returns
const reference to the object

Definition at line 370 of file Bus.h.

◆ post_update()

template<typename BaggageType>
template<typename T>
void post_update ( the_type< T > t)
inlineprivate

In general, don't do anything after an object has been updated.

Parameters
tUnused, only helping compiler choose the correct method

Definition at line 737 of file Bus.h.

◆ stream() [1/4]

template<typename BaggageType>
virtual void stream ( std::ostream & s) const
inlinevirtual

Stream the passenger's object to the input ostream

We call the overloaded, templated stream method that allows the compiler to deduce which implementation to use depending on the type of baggage we are carrying.

Implements Bus::Seat.

Definition at line 404 of file Bus.h.

◆ stream() [2/4]

template<typename BaggageType>
template<typename Key, typename Val>
void stream ( the_type< std::map< Key, Val > > t,
std::ostream & s ) const
inlineprivate

Stream a map of objects by looping through them

Note
Here is where we require that all event objects used as a key or value in a map need the 'operator<<' defined.
Parameters
tUnused, only helping compiler choose the correct method
sostream to write to

Definition at line 799 of file Bus.h.

◆ stream() [3/4]

template<typename BaggageType>
template<typename Content>
void stream ( the_type< std::vector< Content > > t,
std::ostream & s ) const
inlineprivate

Stream a vector of objects by looping through them.

Note
Here is where we require that all event objects inside a vector need the 'operator<<' defined.
Parameters
tUnused, only helping compiler choose the correct method
sostream to write to

Definition at line 779 of file Bus.h.

◆ stream() [4/4]

template<typename BaggageType>
template<typename T>
void stream ( the_type< T > t,
std::ostream & s ) const
inlineprivate

For std::vector, use the sort method after the contents are updated.

Note
This is where we require that any contents of vectors have the operator< defined.
Parameters
tUnused, only helping compiler choose the correct method template <typename Content> void post_update(the_type<std::vector<Content>> t) { std::sort(baggage_->begin(), baggage_->end()); } Stream a basic type that has its own definition of 'operator<<'.
Note
Here is where we require that all event bus objects (not inside a vector or map) need the 'operatore<<' defined.
Parameters
tUnused, only helping compiler choose the correct method
sostream to write to

Definition at line 765 of file Bus.h.

◆ update()

template<typename BaggageType>
void update ( const BaggageType & updated_obj)
inline

Update this passenger's baggage.

See also
post_update to allow the different types to take actions after the baggage is updated. For example, the vector specialization of post_update uses this opportunity to sort the list of objects.
Parameters
[in]updated_objBaggageType to copy into our object

Definition at line 383 of file Bus.h.

◆ operator<<

template<typename BaggageType>
std::ostream & operator<< ( std::ostream & s,
const framework::Bus::Passenger< BaggageType > & p )
friend

Stream this object to the output stream

May contain newlines if large object.

See also
stream for detailed implementation
Parameters
[in]sostream to write to
[in]pPassenger to write out
Returns
modified ostream

Definition at line 418 of file Bus.h.

Member Data Documentation

◆ baggage_

template<typename BaggageType>
BaggageType* baggage_
private

A pointer to the baggage we own and created.

Definition at line 812 of file Bus.h.


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