ATLAS Offline Software
TreeManager.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 #ifndef TREEMANAGER_H_
6 #define TREEMANAGER_H_
7 
8 #include "TTree.h"
9 
10 #include <string>
11 #include <list>
12 #include <sstream>
13 #include <functional>
14 #include <vector>
15 
16 class TFile;
17 
18 namespace top {
26  class TreeManager {
27  public:
31  TreeManager();
32 
40  TreeManager(const std::string& name, TFile* outputFile, const int, const int, const int);
41 
43  TreeManager(const TreeManager&& rhs);
44 
46  TreeManager(const TreeManager&) = delete;
47 
49  TreeManager& operator = (TreeManager const&) = delete;
50 
54  void initialize(const std::string& name, TFile* outputFile, const int, const int, const int);
55 
59  void fill();
60 
64  const std::string& name() const;
65 
67  typedef std::function<int (top::TreeManager const*, std::string const&)> BranchFilter;
68 
70  std::vector<BranchFilter>& branchFilters() {return m_branchFilters;}
71 
73  std::vector<BranchFilter> const& branchFilters() const {return m_branchFilters;}
74 
87  template<class T>
88  void makeOutputVariable(T& obj, const std::string name) {
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  }
105 
106  private:
110  std::string m_name;
111 
112 
129  const char* RootType(const char* typeid_type);
130 
143  const char* TypeidType(const char* root_type);
144 
146  TTree* m_tree;
147 
157  std::list< void* > m_outputVarPointers;
158 
169  std::vector<BranchFilter> m_branchFilters;
170 
171  // Two member variables to manage the size of the tree branch baskets
174  };
175 }
176 
177 #endif
top::TreeManager::TypeidType
const char * TypeidType(const char *root_type)
Stolen from SFrame code.
Definition: TreeManager.cxx:131
top::TreeManager::m_tree
TTree * m_tree
Pointer to the TTree, created by this class in the constructor.
Definition: TreeManager.h:146
top
TopConfig A simple configuration that is NOT a singleton.
Definition: AnalysisTrackingHelper.cxx:58
top::TreeManager::m_basketSizePrimitive
int m_basketSizePrimitive
Definition: TreeManager.h:172
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
top::TreeManager::branchFilters
std::vector< BranchFilter > const & branchFilters() const
list of functions that control which variables are written out
Definition: TreeManager.h:73
top::TreeManager::RootType
const char * RootType(const char *typeid_type)
Stolen from SFrame code.
Definition: TreeManager.cxx:61
top::TreeManager::TreeManager
TreeManager()
Default constructor - note need to initialize the class if you use this.
Definition: TreeManager.cxx:15
top::TreeManager::fill
void fill()
Calls TTree::Fill.
Definition: TreeManager.cxx:53
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::initialize
void initialize(const std::string &name, TFile *outputFile, const int, const int, const int)
Initialize the class with a new output file.
Definition: TreeManager.cxx:38
top::TreeManager::m_basketSizeVector
int m_basketSizeVector
Definition: TreeManager.h:173
top::TreeManager::BranchFilter
std::function< int(top::TreeManager const *, std::string const &)> BranchFilter
function object type used for branch filters
Definition: TreeManager.h:67
top::TreeManager::TreeManager
TreeManager(const TreeManager &)=delete
But not to copy it.
top::TreeManager::name
const std::string & name() const
name of the TTree
Definition: TreeManager.cxx:57
top::TreeManager
A class that hopefully makes making a flat ntuple tree a bit easier since you don't need to worry abo...
Definition: TreeManager.h:26
merge.status
status
Definition: merge.py:17
top::TreeManager::operator=
TreeManager & operator=(TreeManager const &)=delete
Or assign it.
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
top::TreeManager::makeOutputVariable
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",...
Definition: TreeManager.h:88