ATLAS Offline Software
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
top::TreeManager Class Reference

A class that hopefully makes making a flat ntuple tree a bit easier since you don't need to worry about what type to make the branches and stuff. More...

#include <TreeManager.h>

Collaboration diagram for top::TreeManager:

Public Types

typedef std::function< int(top::TreeManager const *, std::string const &)> BranchFilter
 function object type used for branch filters More...
 

Public Member Functions

 TreeManager ()
 Default constructor - note need to initialize the class if you use this. More...
 
 TreeManager (const std::string &name, TFile *outputFile, const int, const int, const int)
 You need to construct this at the start (in the same way as for the plot manager. More...
 
 TreeManager (const TreeManager &&rhs)
 We do allow you to move it. More...
 
 TreeManager (const TreeManager &)=delete
 But not to copy it. More...
 
TreeManageroperator= (TreeManager const &)=delete
 Or assign it. More...
 
void initialize (const std::string &name, TFile *outputFile, const int, const int, const int)
 Initialize the class with a new output file. More...
 
void fill ()
 Calls TTree::Fill. More...
 
const std::string & name () const
 name of the TTree More...
 
std::vector< BranchFilter > & branchFilters ()
 list of functions that control which variables are written out More...
 
std::vector< BranchFilter > constbranchFilters () const
 list of functions that control which variables are written out More...
 
template<class T >
void makeOutputVariable (T &obj, const std::string name)
 The idea is to simplify the creation of branches so that you don't have to write tree->Branch("blah", &blah, "blah/I") because the computer can figure out the "blah/I" bit for you. More...
 

Private Member Functions

const char * RootType (const char *typeid_type)
 Stolen from SFrame code. More...
 
const char * TypeidType (const char *root_type)
 Stolen from SFrame code. More...
 

Private Attributes

std::string m_name
 name of the tree More...
 
TTree * m_tree
 Pointer to the TTree, created by this class in the constructor. More...
 
std::list< void * > m_outputVarPointers
 Stolen from SFrame code. More...
 
std::vector< BranchFilterm_branchFilters
 list of functions that control which variables are written out More...
 
int m_basketSizePrimitive
 
int m_basketSizeVector
 

Detailed Description

A class that hopefully makes making a flat ntuple tree a bit easier since you don't need to worry about what type to make the branches and stuff.

Almost all this code is stolen from SFrame: http://sourceforge.net/p/sframe

Definition at line 26 of file TreeManager.h.

Member Typedef Documentation

◆ BranchFilter

typedef std::function<int (top::TreeManager const*, std::string const&)> top::TreeManager::BranchFilter

function object type used for branch filters

Definition at line 67 of file TreeManager.h.

Constructor & Destructor Documentation

◆ TreeManager() [1/4]

top::TreeManager::TreeManager ( )

Default constructor - note need to initialize the class if you use this.

Definition at line 15 of file TreeManager.cxx.

15  :
16  m_name("NULL"),
17  m_tree(nullptr) {
18  }

◆ TreeManager() [2/4]

top::TreeManager::TreeManager ( const std::string &  name,
TFile *  outputFile,
const int  nEventAutoFlush,
const int  basketSizePrimitive,
const int  basketSizeVector 
)

You need to construct this at the start (in the same way as for the plot manager.

Parameters
fileThe file to write the tree to.
nameThe name of the tree (e.g. nominal, systematic1 etc).

Definition at line 20 of file TreeManager.cxx.

21  :
22  m_name(name),
23  m_tree(nullptr) {
24  outputFile->cd();
25  m_tree = new TTree(name.c_str(), "tree");
26  m_tree->SetAutoFlush(nEventAutoFlush);
27  m_tree->SetAutoSave(0);
28  m_basketSizePrimitive = basketSizePrimitive;
29  m_basketSizeVector = basketSizeVector;
30  }

◆ TreeManager() [3/4]

top::TreeManager::TreeManager ( const TreeManager &&  rhs)

We do allow you to move it.

Definition at line 32 of file TreeManager.cxx.

32  :
33  m_name(std::move(rhs.m_name)),
34  m_tree(std::move(rhs.m_tree)),
35  m_outputVarPointers(std::move(rhs.m_outputVarPointers)) {
36  }

◆ TreeManager() [4/4]

top::TreeManager::TreeManager ( const TreeManager )
delete

But not to copy it.

Member Function Documentation

◆ branchFilters() [1/2]

std::vector<BranchFilter>& top::TreeManager::branchFilters ( )
inline

list of functions that control which variables are written out

The function objects are called in order, with the TreeManager instance and the variable name as parameters. A return value of 1 (0) tells the TreeManager that the variable should (not) be written to the n-tuple. If the function returns -1, the decision is left to the next function in the list. If there are no functions in the list, or all functions return -1, the variable is written to the n-tuple.

Definition at line 70 of file TreeManager.h.

70 {return m_branchFilters;}

◆ branchFilters() [2/2]

std::vector<BranchFilter> const& top::TreeManager::branchFilters ( ) const
inline

list of functions that control which variables are written out

The function objects are called in order, with the TreeManager instance and the variable name as parameters. A return value of 1 (0) tells the TreeManager that the variable should (not) be written to the n-tuple. If the function returns -1, the decision is left to the next function in the list. If there are no functions in the list, or all functions return -1, the variable is written to the n-tuple.

Definition at line 73 of file TreeManager.h.

73 {return m_branchFilters;}

◆ fill()

void top::TreeManager::fill ( )

Calls TTree::Fill.

Needed to fill the tree.

Definition at line 53 of file TreeManager.cxx.

53  {
54  m_tree->Fill();
55  }

◆ initialize()

void top::TreeManager::initialize ( const std::string &  name,
TFile *  outputFile,
const int  nEventAutoFlush,
const int  basketSizePrimitive,
const int  basketSizeVector 
)

Initialize the class with a new output file.

Definition at line 38 of file TreeManager.cxx.

39  {
40  if (m_tree) {
41  ATH_MSG_WARNING("Tried to call initialize, but tree is already created. Doing nothing.");
42  return;
43  }
44 
45  outputFile->cd();
46  m_tree = new TTree(name.c_str(), "tree");
47  m_tree->SetAutoFlush(nEventAutoFlush);
48  m_tree->SetAutoSave(0);
49  m_basketSizePrimitive = basketSizePrimitive;
50  m_basketSizeVector = basketSizeVector;
51  }

◆ makeOutputVariable()

template<class T >
void top::TreeManager::makeOutputVariable ( T &  obj,
const std::string  name 
)
inline

The idea is to simplify the creation of branches so that you don't have to write tree->Branch("blah", &blah, "blah/I") because the computer can figure out the "blah/I" bit for you.

Stolen from SFrame.

Parameters
objThe object that you would like to save in a branch. This can be a primitive type (e.g. int, unsigned int, etc.) or an object (e.g. std::vector<float>).
nameThe name you would like to give this branch.

Definition at line 88 of file TreeManager.h.

88  {
89  for (auto branchFilter : branchFilters()) {
90  int status = branchFilter(this, name);
91  if (status > 0) break;
92  if (status == 0) return;
93  }
94  const char* type_name = typeid(obj).name();
95  if (strlen(type_name) == 1) {
96  std::ostringstream leaflist;
97  leaflist << name << "/" << RootType(type_name);
98  m_tree->Branch(name.c_str(), &obj, leaflist.str().c_str(), m_basketSizePrimitive);
99  } else {
100  m_outputVarPointers.push_back(&obj);
101  T** pointer = reinterpret_cast< T** >(&m_outputVarPointers.back());
102  m_tree->Branch(name.c_str(), pointer, m_basketSizeVector);
103  }
104  }

◆ name()

const std::string & top::TreeManager::name ( ) const

name of the TTree

Definition at line 57 of file TreeManager.cxx.

57  {
58  return m_name;
59  }

◆ operator=()

TreeManager& top::TreeManager::operator= ( TreeManager const )
delete

Or assign it.

◆ RootType()

const char * top::TreeManager::RootType ( const char *  typeid_type)
private

Stolen from SFrame code.

This is a tricky one. In SCycleBaseNTuple::DeclareVariable(...) the function automatically detects the type of the variable to be put into the output tree. Since ROOT uses a different naming scheme for the primitives than C++'s typeid call, the typeid names have to be translated for ROOT. This is the function doing that.

Warning
The translation is probably only valid on various UNIX systems, probably doesn't work on Windows. (Did anyone ever try SFrame on Windows anyway???)
Parameters
typeid_typePrimitive type name in the "typeid" format
Returns
The primitive type name in ROOT's format

Definition at line 61 of file TreeManager.cxx.

61  {
62  // Check that we received a reasonable input:
63  if (strlen(typeid_type) != 1) {
64  // SLogger m_logger( "SCycleBaseNTuple" );
65  // REPORT_ERROR( "Received a complex object description: " << typeid_type );
66  // throw SError( "SCycleBaseNTuple::RootType received complex object "
67  // "description", SError::StopExecution );
68  }
69 
70  // Do the hard-coded translation:
71  switch (typeid_type[ 0 ]) {
72  case 'c':
73  return "B";
74 
75  break;
76 
77  case 'h':
78  return "b";
79 
80  break;
81 
82  case 's':
83  return "S";
84 
85  break;
86 
87  case 't':
88  return "s";
89 
90  break;
91 
92  case 'i':
93  return "I";
94 
95  break;
96 
97  case 'j':
98  return "i";
99 
100  break;
101 
102  case 'f':
103  return "F";
104 
105  break;
106 
107  case 'd':
108  return "D";
109 
110  break;
111 
112  case 'x':
113  return "L";
114 
115  break;
116 
117  case 'y':
118  return "l";
119 
120  break;
121 
122  case 'b':
123  return "O";
124 
125  break;
126  }
127 
128  return "";
129  }

◆ TypeidType()

const char * top::TreeManager::TypeidType ( const char *  root_type)
private

Stolen from SFrame code.

This function is used internally to check whether the user tries to connect the correct type of primitive to the branches. ROOT can have some trouble identifying such code problems...

Warning
The implementation might be platform specific!
Parameters
root_typeROOT primitive type name
Returns
The typeid type name for the ROOT primitive type

Definition at line 131 of file TreeManager.cxx.

131  {
132  // Do the hard-coded translation:
133  if (!strcmp(root_type, "Char_t")) {
134  return "c";
135  } else if (!strcmp(root_type, "UChar_t")) {
136  return "h";
137  } else if (!strcmp(root_type, "Short_t")) {
138  return "s";
139  } else if (!strcmp(root_type, "UShort_t")) {
140  return "t";
141  } else if (!strcmp(root_type, "Int_t")) {
142  return "i";
143  } else if (!strcmp(root_type, "UInt_t")) {
144  return "j";
145  } else if (!strcmp(root_type, "Float_t")) {
146  return "f";
147  } else if (!strcmp(root_type, "Double_t")) {
148  return "d";
149  } else if (!strcmp(root_type, "Long64_t")) {
150  return "x";
151  } else if (!strcmp(root_type, "ULong64_t")) {
152  return "y";
153  } else if (!strcmp(root_type, "Bool_t")) {
154  return "b";
155  }
156 
157  return "";
158  }

Member Data Documentation

◆ m_basketSizePrimitive

int top::TreeManager::m_basketSizePrimitive
private

Definition at line 172 of file TreeManager.h.

◆ m_basketSizeVector

int top::TreeManager::m_basketSizeVector
private

Definition at line 173 of file TreeManager.h.

◆ m_branchFilters

std::vector<BranchFilter> top::TreeManager::m_branchFilters
private

list of functions that control which variables are written out

The function objects are called in order, with the TreeManager instance and the variable name as parameters. A return value of 1 (0) tells the TreeManager that the variable should (not) be written to the n-tuple. If the function returns -1, the decision is left to the next function in the list. If there are no functions in the list, or all functions return -1, the variable is written to the n-tuple.

Definition at line 169 of file TreeManager.h.

◆ m_name

std::string top::TreeManager::m_name
private

name of the tree

Definition at line 110 of file TreeManager.h.

◆ m_outputVarPointers

std::list< void* > top::TreeManager::m_outputVarPointers
private

Stolen from SFrame code.

We have to keep the pointers to the output variables defined by the user. ROOT keeps track of the objects by storing pointers to pointers to the objects. Since the user probably wants to use the output objects directly and not through pointers, the base class has to take care of this pointer issue by itself...

Definition at line 157 of file TreeManager.h.

◆ m_tree

TTree* top::TreeManager::m_tree
private

Pointer to the TTree, created by this class in the constructor.

Definition at line 146 of file TreeManager.h.


The documentation for this class was generated from the following files:
top::TreeManager::m_tree
TTree * m_tree
Pointer to the TTree, created by this class in the constructor.
Definition: TreeManager.h:146
top::TreeManager::m_basketSizePrimitive
int m_basketSizePrimitive
Definition: TreeManager.h:172
top::TreeManager::RootType
const char * RootType(const char *typeid_type)
Stolen from SFrame code.
Definition: TreeManager.cxx:61
top::TreeManager::m_branchFilters
std::vector< BranchFilter > m_branchFilters
list of functions that control which variables are written out
Definition: TreeManager.h:169
top::TreeManager::branchFilters
std::vector< BranchFilter > & branchFilters()
list of functions that control which variables are written out
Definition: TreeManager.h:70
compareGeometries.outputFile
string outputFile
Definition: compareGeometries.py:25
top::TreeManager::m_name
std::string m_name
name of the tree
Definition: TreeManager.h:110
top::TreeManager::m_basketSizeVector
int m_basketSizeVector
Definition: TreeManager.h:173
top::TreeManager::name
const std::string & name() const
name of the TTree
Definition: TreeManager.cxx:57
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
merge.status
status
Definition: merge.py:17
top::TreeManager::m_outputVarPointers
std::list< void * > m_outputVarPointers
Stolen from SFrame code.
Definition: TreeManager.h:157
python.PyAthena.obj
obj
Definition: PyAthena.py:135
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35