LDMX Software
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
framework::NtupleManager Class Reference

Singleton class used to manage the creation and pooling of ntuples. More...

#include <NtupleManager.h>

Public Member Functions

void create (const std::string &tname)
 Create a ROOT tree to hold the ntuple variables (ROOT leaves).
 
template<typename VarType >
void addVar (const std::string &tname, const std::string &vname)
 Add a variable of type VarType to the ROOT tree with name 'tname'.
 
template<typename T >
void setVar (const std::string &vname, const T &value)
 Set the value of the variable named 'vname'.
 
void fill ()
 
void clear ()
 Reset all of the variables to their limits.
 
void reset ()
 Reset NtupleManager to blank state.
 
 NtupleManager (const NtupleManager &)=delete
 Hide Copy Constructor.
 
void operator= (const NtupleManager &)=delete
 Hide Assignment Operator.
 

Static Public Member Functions

static NtupleManagergetInstance ()
 

Private Member Functions

 NtupleManager ()
 Private constructor to prevent instantiation.
 

Private Attributes

std::unordered_map< std::string, TTree * > trees_
 Container for output ROOT trees.
 
framework::Bus bus_
 Container for buffering variables.
 

Detailed Description

Singleton class used to manage the creation and pooling of ntuples.

See also
framework::Bus Similar to the Event bus itself, we use the Bus to buffer the variable values and attach them to their output TTrees. Unlike the event bus, we don't implement retrieval mechanisms for reading these values.

Definition at line 36 of file NtupleManager.h.

Constructor & Destructor Documentation

◆ NtupleManager()

framework::NtupleManager::NtupleManager ( )
private

Private constructor to prevent instantiation.

Definition at line 6 of file NtupleManager.cxx.

6{}

Member Function Documentation

◆ addVar()

template<typename VarType >
void framework::NtupleManager::addVar ( const std::string &  tname,
const std::string &  vname 
)
inline

Add a variable of type VarType to the ROOT tree with name 'tname'.

If the variable already exists in any tree or the requested tree does not exists, an exception is thrown.

Template Parameters

in] VarType type of variable to add to the tree

Parameters
[in]tnameName of the tree to add the variable to.
[in]vnameName of the variable to add to the tree
Exceptions
Exceptionif tree doesn't exist or variable already does

Definition at line 58 of file NtupleManager.h.

58 {
59 // Check if a tree named 'tname' has already been created. If
60 // not, throw an exception.
61 if (trees_.count(tname) == 0)
62 EXCEPTION_RAISE("NtupleManager", "A tree with name " + tname +
63 " has not been been created.");
64
65 // Check if the variable exists in the map. If it does, throw
66 // an exception.
67 if (bus_.isOnBoard(vname)) {
68 EXCEPTION_RAISE("NtupleManager", "A variable with name " + vname +
69 " has already been defined.");
70 }
71
72 // Board the bus
73 bus_.board<VarType>(vname);
74
75 // Attach the tree to the bus
76 bus_.attach(trees_[tname], vname, true);
77 }
bool isOnBoard(const std::string &name)
Check if a passenger is on the bus.
Definition Bus.h:117
TBranch * attach(TTree *tree, const std::string &name, bool can_create)
Attach the input tree to the object a passenger is carrying.
Definition Bus.h:107
void board(const std::string &name)
Board a new passenger onto the bus.
Definition Bus.h:65
framework::Bus bus_
Container for buffering variables.
std::unordered_map< std::string, TTree * > trees_
Container for output ROOT trees.

References framework::Bus::attach(), framework::Bus::board(), bus_, framework::Bus::isOnBoard(), and trees_.

Referenced by ecal::test::EcalCheckEnergyReconstruction::onProcessStart(), and hcal::test::HcalCheckReconstruction::onProcessStart().

◆ clear()

void framework::NtupleManager::clear ( )

Reset all of the variables to their limits.

Definition at line 30 of file NtupleManager.cxx.

30{ bus_.clear(); }
void clear()
Reset the objects carried by the passengers.
Definition Bus.h:126

References bus_, and framework::Bus::clear().

Referenced by framework::Process::run().

◆ create()

void framework::NtupleManager::create ( const std::string &  tname)

Create a ROOT tree to hold the ntuple variables (ROOT leaves).

Parameters
nameName of the tree.

Definition at line 14 of file NtupleManager.cxx.

14 {
15 // Check if a tree named 'name' has already been created. If so
16 // throw an exception.
17 if (trees_.count(name) != 0)
18 EXCEPTION_RAISE("NtupleManager",
19 "A tree with name " + name + " has already been created.");
20
21 // Create a tree with the given name and add it to the list of trees.
22 trees_[name] = new TTree{name.c_str(), name.c_str()};
23}

References trees_.

Referenced by trigger::NtupleWriter::onProcessStart(), ecal::test::EcalCheckEnergyReconstruction::onProcessStart(), and hcal::test::HcalCheckReconstruction::onProcessStart().

◆ fill()

void framework::NtupleManager::fill ( )

Definition at line 25 of file NtupleManager.cxx.

25 {
26 // Loop over all the trees and fill them
27 for (const auto& [name, tree] : trees_) tree->Fill();
28}

◆ getInstance()

NtupleManager & framework::NtupleManager::getInstance ( )
static
Returns
The NtupleManager instance

Definition at line 8 of file NtupleManager.cxx.

8 {
9 // Create an instance of the NtupleManager if needed
10 static NtupleManager instance;
11 return instance;
12}
NtupleManager()
Private constructor to prevent instantiation.

Referenced by trigger::NtupleWriter::onProcessStart(), trigger::NtupleWriter::produce(), framework::Process::run(), and TEST_CASE().

◆ reset()

void framework::NtupleManager::reset ( )

Reset NtupleManager to blank state.

We assume that ROOT handles cleanup of TTrees when writing them to a blank state.

Definition at line 32 of file NtupleManager.cxx.

32 {
33 // we assume that ROOT handles clean-up
34 // of the TTrees when they are written to the output histogram file
35 trees_.clear();
37}
void everybodyOff()
Kicks all of the passengers off the bus and therefore destroys any objects they are carrying.
Definition Bus.h:140

References bus_, framework::Bus::everybodyOff(), and trees_.

Referenced by framework::Process::run().

◆ setVar()

template<typename T >
void framework::NtupleManager::setVar ( const std::string &  vname,
const T &  value 
)
inline

Set the value of the variable named 'vname'.

If the variable value is not set, the default value will be used when filling the tree. If the requested variable has not been created, a warning will be printed. This allows a user to choose to make a subset of an ntuple by simply not adding the variable to the tree.

Template Parameters

in] T type of variable

Parameters
[in]vnameName of the variable
[in]valueThe value of the variable

Definition at line 92 of file NtupleManager.h.

92 {
93 // Check if the variable already exists in the map. If it
94 // doesn't, warn the user and don't try to set the variable
95 // value.
96 if (not bus_.isOnBoard(vname)) {
97 ldmx_log(warn) << "The variable " << vname
98 << " does not exist in the tree. Skipping.";
99 return;
100 }
101
102 // Set the value of the variable
103 try {
104 bus_.update(vname, value);
105 } catch (const std::bad_cast&) {
106 EXCEPTION_RAISE("TypeMismatch", "Ntuple variable '" + vname +
107 "' is being set by the wrong type '" +
108 typeid(value).name() + "'.");
109 }
110 }
void update(const std::string &name, const BaggageType &obj)
Update the object a passenger is carrying.
Definition Bus.h:83

References bus_, framework::Bus::isOnBoard(), and framework::Bus::update().

Referenced by ecal::test::EcalCheckEnergyReconstruction::analyze(), and hcal::test::HcalCheckReconstruction::analyze().

Member Data Documentation

◆ bus_

framework::Bus framework::NtupleManager::bus_
private

Container for buffering variables.

Definition at line 137 of file NtupleManager.h.

Referenced by addVar(), clear(), reset(), and setVar().

◆ trees_

std::unordered_map<std::string, TTree*> framework::NtupleManager::trees_
private

Container for output ROOT trees.

Definition at line 134 of file NtupleManager.h.

Referenced by addVar(), create(), and reset().


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