LDMX Software
NtupleManager.h
1
2#ifndef _NTUPLE_MANAGER_H_
3#define _NTUPLE_MANAGER_H_
4
5/*~~~~~~~~~~~~*/
6/* StdLib */
7/*~~~~~~~~~~~~*/
8#include <map>
9#include <string>
10#include <unordered_map>
11
12/*~~~~~~~~~~*/
13/* ROOT */
14/*~~~~~~~~~~*/
15#include "TTree.h"
16
17/*~~~~~~~~~~*/
18/* Core */
19/*~~~~~~~~~~*/
20#include "Framework/Bus.h"
21#include "Framework/Exception/Exception.h"
22#include "Framework/Logger.h"
23
24namespace framework {
25
37 public:
39 static NtupleManager& getInstance();
40
45 void create(const std::string& tname);
46
57 template <typename VarType>
58 void addVar(const std::string& tname, const std::string& vname) {
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 }
78
91 template <typename T>
92 void setVar(const std::string& vname, const T& value) {
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 }
111
112 // Fill all of the ROOT trees.
113 void fill();
114
116 void clear();
117
124 void reset();
125
127 NtupleManager(const NtupleManager&) = delete;
128
130 void operator=(const NtupleManager&) = delete;
131
132 private:
134 std::unordered_map<std::string, TTree*> trees_;
135
138
141
143 enableLogging("NtupleManager")
144
145}; // NtupleManager
146
147} // namespace framework
148
149#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.
Definition PerfDict.cxx:45