LDMX Software
NtupleManager.h
1
2#ifndef _NTUPLE_MANAGER_H_
3#define _NTUPLE_MANAGER_H_
4
5/*~~~~~~~~~~~~*/
6/* StdLib */
7/*~~~~~~~~~~~~*/
8#include <string>
9#include <unordered_map>
10
11/*~~~~~~~~~~*/
12/* ROOT */
13/*~~~~~~~~~~*/
14#include "TTree.h"
15
16/*~~~~~~~~~~*/
17/* Core */
18/*~~~~~~~~~~*/
19#include "Framework/Bus.h"
20#include "Framework/Exception/Exception.h"
21#include "Framework/Logger.h"
22
23namespace framework {
24
36 public:
38 static NtupleManager& getInstance();
39
44 void create(const std::string& tname);
45
56 template <typename VarType>
57 void addVar(const std::string& tname, const std::string& vname) {
58 // Check if a tree named 'tname' has already been created. If
59 // not, throw an exception.
60 if (trees_.count(tname) == 0)
61 EXCEPTION_RAISE("NtupleManager", "A tree with name " + tname +
62 " has not been been created.");
63
64 // Check if the variable exists in the map. If it does, throw
65 // an exception.
66 if (bus_.isOnBoard(vname)) {
67 EXCEPTION_RAISE("NtupleManager", "A variable with name " + vname +
68 " has already been defined.");
69 }
70
71 // Board the bus
72 bus_.board<VarType>(vname);
73
74 // Attach the tree to the bus
75 bus_.attach(trees_[tname], vname, true);
76 }
77
90 template <typename T>
91 void setVar(const std::string& vname, const T& value) {
92 // Check if the variable already exists in the map. If it
93 // doesn't, warn the user and don't try to set the variable
94 // value.
95 if (not bus_.isOnBoard(vname)) {
96 ldmx_log(warn) << "The variable " << vname
97 << " does not exist in the tree. Skipping.";
98 return;
99 }
100
101 // Set the value of the variable
102 try {
103 bus_.update(vname, value);
104 } catch (const std::bad_cast&) {
105 EXCEPTION_RAISE("TypeMismatch", "Ntuple variable '" + vname +
106 "' is being set by the wrong type '" +
107 typeid(value).name() + "'.");
108 }
109 }
110
111 // Fill all of the ROOT trees.
112 void fill();
113
115 void clear();
116
123 void reset();
124
126 NtupleManager(const NtupleManager&) = delete;
127
129 void operator=(const NtupleManager&) = delete;
130
131 private:
133 std::unordered_map<std::string, TTree*> trees_;
134
137
140
142 enableLogging("NtupleManager")
143
144}; // NtupleManager
145
146} // namespace framework
147
148#endif // _NTUPLE_MANAGER_H_
A map of bus passengers.
Definition Bus.h:29
void update(const std::string &name, const BaggageType &obj)
Update the object a passenger is carrying.
Definition Bus.h:83
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
Singleton class used to manage the creation and pooling of ntuples.
void addVar(const std::string &tname, const std::string &vname)
Add a variable of type VarType to the ROOT tree with name 'tname'.
NtupleManager()
Private constructor to prevent instantiation.
void create(const std::string &tname)
Create a ROOT tree to hold the ntuple variables (ROOT leaves).
framework::Bus bus_
Container for buffering variables.
void clear()
Reset all of the variables to their limits.
static NtupleManager & getInstance()
void reset()
Reset NtupleManager to blank state.
void operator=(const NtupleManager &)=delete
Hide Assignment Operator.
std::unordered_map< std::string, TTree * > trees_
Container for output ROOT trees.
void setVar(const std::string &vname, const T &value)
Set the value of the variable named 'vname'.
NtupleManager(const NtupleManager &)=delete
Hide Copy Constructor.
All classes in the ldmx-sw project use this namespace.