LDMX Software
Classes | Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
framework::Bus Class Reference

A map of bus passengers. More...

#include <Bus.h>

Classes

class  Passenger
 A bus passenger. More...
 
class  Seat
 The handle of a bus passenger. More...
 

Public Member Functions

template<typename BaggageType >
const BaggageType & get (const std::string &name)
 Get the baggage carried by the passenger with the passed name.
 
template<typename BaggageType >
void board (const std::string &name)
 Board a new passenger onto the bus.
 
template<typename BaggageType >
void update (const std::string &name, const BaggageType &obj)
 Update the object a passenger is carrying.
 
TBranch * attach (TTree *tree, const std::string &name, bool can_create)
 Attach the input tree to the object a passenger is carrying.
 
bool isOnBoard (const std::string &name)
 Check if a passenger is on the bus.
 
void clear ()
 Reset the objects carried by the passengers.
 
void everybodyOff ()
 Kicks all of the passengers off the bus and therefore destroys any objects they are carrying.
 
void stream (std::ostream &s) const
 Write the bus to the input ostream.
 

Private Member Functions

template<typename BaggageType >
Passenger< BaggageType > & getRef (const std::string &name)
 Get a reference to a passenger on the bus.
 

Private Attributes

std::unordered_map< std::string, std::unique_ptr< Seat > > passengers_
 Map of passenger names to the seats filled by passengers.
 

Friends

std::ostream & operator<< (std::ostream &s, const framework::Bus &b)
 Allow for the bus to be streamed to an ostream.
 

Detailed Description

A map of bus passengers.

This is the actual bus that the passengers ride on during event processing. At its core, it is simply a specialization of a map between branch names and handles to the bus passengers (Seats) with some special accessors for connecting TTrees and updating contents.

See also
Bus::Seat for how we keep a handle on the Passengers
Bus::Passenger for what actually carries the event objects

Definition at line 29 of file Bus.h.

Member Function Documentation

◆ attach()

TBranch * framework::Bus::attach ( TTree *  tree,
const std::string &  name,
bool  can_create 
)
inline

Attach the input tree to the object a passenger is carrying.

Note
Does not check if passenger exists in the map of passengers. This means it would get pretty messy if you try to attach a TTree to a passenger that is the wrong type or is not already on the bus.
Does not check if branch on the tree is expecting the type that is carried by the passenger. Don't know how this will affect things, but it tends to produce a seg fault because serializing objects between different types gets very messy.
See also
Passenger::attach for how we attach a passenger
Parameters
[in]treepointer to TTree to attach to
[in]namename of passenger (and branch of tree)
[in]can_createtrue if we are allowed to create new branches on the tree
Returns
pointer to branch that we attached to (may be null)

Definition at line 107 of file Bus.h.

107 {
108 return passengers_[name]->attach(tree, name, can_create);
109 }
std::unordered_map< std::string, std::unique_ptr< Seat > > passengers_
Map of passenger names to the seats filled by passengers.
Definition Bus.h:821

References passengers_.

Referenced by framework::Event::add(), framework::NtupleManager::addVar(), and framework::Event::getObject().

◆ board()

template<typename BaggageType >
void framework::Bus::board ( const std::string &  name)
inline

Board a new passenger onto the bus.

Note
Does not check if we are overwriting any other passenger!

Creates a new passenger that carries an object of type BaggageType and puts this passenger into the map of passengers.

Note
If you are seeing some funky "Clear not defined" compiling error while attempting to use a newly created event bus object, you should check that the class you have written matches the requirements for any of the bus types in the documentation for Bus::Passenger.
Template Parameters

in] BaggageType type of object new passenger is carrying

Parameters
[in]nameName of new passenger (corresponds to branch name)

Definition at line 65 of file Bus.h.

65 {
66 passengers_[name] = std::make_unique<Passenger<BaggageType>>();
67 passengers_[name]->clear(); // make sure 'default' state is well defined
68 }

References passengers_.

Referenced by framework::Event::add(), framework::NtupleManager::addVar(), and framework::Event::getObject().

◆ clear()

void framework::Bus::clear ( )
inline

Reset the objects carried by the passengers.

See also
Passenger::clear for how we clear the individual passengers

Definition at line 126 of file Bus.h.

126 {
127 for (auto& [n, handle] : passengers_) handle->clear();
128 }
void clear()
Reset the objects carried by the passengers.
Definition Bus.h:126

References passengers_.

Referenced by framework::Event::Clear(), and framework::NtupleManager::clear().

◆ everybodyOff()

void framework::Bus::everybodyOff ( )
inline

Kicks all of the passengers off the bus and therefore destroys any objects they are carrying.

Note
This is where the Passengers are destructed and therefore their baggage is deleted.
See also
Bus::Passenger::~Passenger for comments about why you need to be careful.

Definition at line 140 of file Bus.h.

140{ passengers_.clear(); }

References passengers_.

Referenced by framework::Event::onEndOfFile(), framework::NtupleManager::reset(), and framework::Event::setInputTree().

◆ get()

template<typename BaggageType >
const BaggageType & framework::Bus::get ( const std::string &  name)
inline

Get the baggage carried by the passenger with the passed name.

See also
getRef for getting the passenger
Exceptions
std::bad_castif BaggageType does not match type of object passenger is carrying
Template Parameters

in] BaggageType type of object carried by passenger

Parameters
[in]nameName of Passenger (corresponds to branch_name)
Returns
const reference to object carried by passenger

Definition at line 43 of file Bus.h.

43 {
44 return getRef<BaggageType>(name).get();
45 }

Referenced by framework::Event::getObject().

◆ getRef()

template<typename BaggageType >
Passenger< BaggageType > & framework::Bus::getRef ( const std::string &  name)
inlineprivate

Get a reference to a passenger on the bus.

Exceptions
std::bad_castif BaggageType does not match actual type of object the passenger is carrying.
Note
Since we are casting from a non-templated base class to a templated derived class, the exception thrown has no knowledge of why the types don't match. (i.e. There won't be an exception like 'can't convert float to int'.)
Template Parameters

in] BaggageType type of object passenger is carrying

Parameters
[in]nameName of passenger
Returns
reference to passenger

Definition at line 810 of file Bus.h.

810 {
811 return dynamic_cast<Passenger<BaggageType>&>(*passengers_[name]);
812 }

References passengers_.

◆ isOnBoard()

bool framework::Bus::isOnBoard ( const std::string &  name)
inline

Check if a passenger is on the bus.

Parameters
[in]namename of passenger to check for
Returns
true if name is a key in the map

Definition at line 117 of file Bus.h.

117 {
118 return passengers_.find(name) != passengers_.end();
119 }

References passengers_.

Referenced by framework::Event::add(), framework::NtupleManager::addVar(), framework::Event::getObject(), and framework::NtupleManager::setVar().

◆ stream()

void framework::Bus::stream ( std::ostream &  s) const
inline

Write the bus to the input ostream.

Includes new-line characters to separate out the different objects carried on the bus.

Parameters
[in]sostream to write to

Definition at line 150 of file Bus.h.

150 {
151 for (auto& [n, handle] : passengers_)
152 s << n << " : " << handle << std::endl;
153 }

References passengers_.

◆ update()

template<typename BaggageType >
void framework::Bus::update ( const std::string &  name,
const BaggageType &  obj 
)
inline

Update the object a passenger is carrying.

See also
getRef for getting the passenger
Passenger::update for how we update a passenger
Exceptions
std::bad_castif BaggageType does not match type of object passenger is carrying
Template Parameters

in] BaggageType type of object carried by passenger

Parameters
[in]namename of passenger (corresponds to branch name)
[in]objupdate object that should be carried by passenger

Definition at line 83 of file Bus.h.

83 {
84 getRef<BaggageType>(name).update(obj);
85 }

Referenced by framework::Event::add(), and framework::NtupleManager::setVar().

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  s,
const framework::Bus b 
)
friend

Allow for the bus to be streamed to an ostream.

See also
stream for the implmentation
Parameters
[in]sostream to write to
[in]bBus to write out
Returns
modified ostream

Definition at line 164 of file Bus.h.

164 {
165 b.stream(s);
166 return s;
167 }
void stream(std::ostream &s) const
Write the bus to the input ostream.
Definition Bus.h:150

Member Data Documentation

◆ passengers_

std::unordered_map<std::string, std::unique_ptr<Seat> > framework::Bus::passengers_
private

Map of passenger names to the seats filled by passengers.

The passenger names are assumed to correspond to any branch name that the passenger's baggage might be attached to.

Definition at line 821 of file Bus.h.

Referenced by attach(), board(), clear(), everybodyOff(), getRef(), isOnBoard(), and stream().


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