ATLAS Offline Software
NtupleVars.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef _VrtSecInclusive_NtupleVars_H
6 #define _VrtSecInclusive_NtupleVars_H
7 
8 
9 // Normal STL and physical vectors
10 
12 
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 #include <map>
18 #include <iostream>
19 #include <cassert>
20 #include <cxxabi.h>
21 
22 #include <TTree.h>
23 
24 namespace VKalVrtAthena {
25  //____________________________________________________________________________________________________
26  // This base holder allows for NtupleVars to hold any type of variable.
27  class BaseHolder {
28  public:
30  virtual ~BaseHolder() throw() {}
31 
32  // This get_typename() function gives back the type of the variable
33  // stored in the derived class varHolder<T>.
34  // This ensures to check the type of a given variable name.
35  virtual std::string& get_typename() = 0;
36  virtual void* get_var() = 0;
37  virtual void clear() = 0;
38  virtual void branch_tree( const std::string&, TTree* ) = 0;
39  };
40 
41 
42  //____________________________________________________________________________________________________
43  // This is a class template which holds a variable m_var.
44  // This class is a BaseHolder, then can be stored in
45  // std::map<string, BaseHoder*> variable list
46 
47  namespace varHolder_detail {
48  template<typename T> void clear( T& var ) { var = 0; }
49  template<typename T> void clear( std::vector<T>& var ) { var.clear(); }
50  template<typename T> void clear( std::vector< std::vector<T> >& var ) {
51  for( auto & v : var ) v.clear();
52  var.clear();
53  }
54 
55  static std::map<const std::string, const std::string> create_tree_str() {
56  std::map<const std::string, const std::string> m;
57  int stat(0);
58  m.insert( std::make_pair( std::string(abi::__cxa_demangle(typeid( char ) .name(), 0, 0, &stat)), std::string("/B") ) );
59  m.insert( std::make_pair( std::string(abi::__cxa_demangle(typeid( unsigned char ) .name(), 0, 0, &stat)), std::string("/b") ) );
60  m.insert( std::make_pair( std::string(abi::__cxa_demangle(typeid( short ) .name(), 0, 0, &stat)), std::string("/S") ) );
61  m.insert( std::make_pair( std::string(abi::__cxa_demangle(typeid( unsigned short ) .name(), 0, 0, &stat)), std::string("/s") ) );
62  m.insert( std::make_pair( std::string(abi::__cxa_demangle(typeid( int ) .name(), 0, 0, &stat)), std::string("/I") ) );
63  m.insert( std::make_pair( std::string(abi::__cxa_demangle(typeid( unsigned int ) .name(), 0, 0, &stat)), std::string("/i") ) );
64  m.insert( std::make_pair( std::string(abi::__cxa_demangle(typeid( long long int ) .name(), 0, 0, &stat)), std::string("/L") ) );
65  m.insert( std::make_pair( std::string(abi::__cxa_demangle(typeid( unsigned long long int ) .name(), 0, 0, &stat)), std::string("/l") ) );
66  m.insert( std::make_pair( std::string(abi::__cxa_demangle(typeid( float ) .name(), 0, 0, &stat)), std::string("/F") ) );
67  m.insert( std::make_pair( std::string(abi::__cxa_demangle(typeid( double ) .name(), 0, 0, &stat)), std::string("/D") ) );
68 
69  return m;
70  }
71 
72  static const std::map<const std::string, const std::string> tree_str_map = create_tree_str();
73 
74  template<typename T>
75  void branch_tree( const std::string& name, const std::string& type_name, TTree *tree, T* var )
76  { tree->Branch( name.c_str(), var, ( name+tree_str_map.at(type_name) ).c_str() ); }
77 
78  template<typename T>
79  void branch_tree( const std::string& name, const std::string& type_name, TTree *tree, std::vector<T>* var )
80  { tree->Branch( name.c_str(), type_name.c_str(), var ); }
81  }
82 
83 
84  //____________________________________________________________________________________________________
85  template<class T>
86  class varHolder : public BaseHolder {
87  private:
88  std::string m_typename;
89  public:
90  std::shared_ptr<T> m_var;
91 
92  varHolder() : m_var( new T ){
93  int stat(0);
94  char* name = abi::__cxa_demangle(typeid( T ).name(), 0, 0, &stat);
95  m_typename = name;
96  delete name;
97  }
98  virtual ~varHolder() {}
99  T& get() { return (*m_var); }
100  void* get_var() { return (void*)( m_var.get() ); }
101  std::string& get_typename() { return m_typename; }
102 
103  // Partial template specialization for member functions are not allowed in C++.
104  // --> defined a set of detailed functions, and call it.
105  void clear() { varHolder_detail::clear( this->get() ); }
106 
107  void branch_tree( const std::string& name, TTree *tree ) {
109  //tree->Branch(name.c_str(), m_typename.c_str(), m_var.get() );
110  }
111 
112 
113  };
114 
115 
116  //____________________________________________________________________________________________________
117  class NtupleVars {
118  private:
119  std::map<std::string, BaseHolder*> m_varHolder;
120 
121  public:
122 
123  std::vector<IntersectionPos*> m_RecoTrk_Int;
124  std::vector<IntersectionPos*> m_SVTrk_Int;
125 
128 
129  void setupNtupleVariables();
130  void branchNtuple( TTree *tree );
131  void deleteNtupleVariables();
132 
133 
134  template<class T> void addNewVar(const std::string& varname) {
135  m_varHolder.insert( make_pair(std::string(varname), new varHolder<T>) );
136  }
137 
138  template<class T> bool isAvailable(const std::string& varname) {
139  if( m_varHolder.find(varname) == m_varHolder.end() ) {
140  std::cerr << "Warning<TImprovedTree::isAvailable>: "
141  << "couldn't find the variable name " << varname
142  << std::endl;
143  return false;
144  } else {
146  int stat;
147  char* name = abi::__cxa_demangle(typeid( T ).name(), 0, 0, &stat);
148 
149  if( var->get_typename() == name ) {
150  delete name;
151  return true;
152  } else {
153  delete name;
154  std::cerr << "Info<TImprovedTree::isAvailable>: " << "typeid didn't match." << std::endl;
155  std::cerr << "Possible candidates are..." << std::endl;
156  std::cerr << "========" << std::endl;
157  std::cerr << varname << " (" << var->get_typename() << ")" << std::endl;
158  std::cerr << "========" << std::endl;
159  return false;
160  }
161 
162  }
163  }
164 
165  template<class T> T& get(const std::string& varname) {
166  try{
167  if( !isAvailable<T>(varname) ) {
168  throw( varname );
169  }
170 
171  varHolder<T>* holder = static_cast< varHolder<T>* >( m_varHolder.at( varname ) );
172  return holder->get();
173 
174  } catch( std::string& varname ) {
175  std::cerr << "Variable " << varname << " is not found in the list!" << std::endl;
176  std::cerr << "Candidates are..." << std::endl;
177  int stat(0);
178  for( auto pair : m_varHolder ) {
179  char* name = abi::__cxa_demangle(typeid( T ).name(), 0, 0, &stat);
180  if( pair.second->get_typename() == name ) {
181  std::cerr << " " << pair.second->get_typename() << " : " << pair.first << std::endl;
182  }
183  delete name;
184  }
185 
186  }
187 
188  std::abort();
189  }
190 
191  void clear();
192 
193 
194  };
195 
196 
197 } // end of namespace bracket
198 
199 
200 #endif /* _VrtSecInclusive_NtupleVars_H */
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
VKalVrtAthena::varHolder::m_var
std::shared_ptr< T > m_var
Definition: NtupleVars.h:90
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
VKalVrtAthena::varHolder::~varHolder
virtual ~varHolder()
Definition: NtupleVars.h:98
VKalVrtAthena::varHolder::get
T & get()
Definition: NtupleVars.h:99
VKalVrtAthena::NtupleVars::deleteNtupleVariables
void deleteNtupleVariables()
Definition: NtupleVars.cxx:24
VKalVrtAthena::NtupleVars::m_SVTrk_Int
std::vector< IntersectionPos * > m_SVTrk_Int
Definition: NtupleVars.h:124
VKalVrtAthena::NtupleVars::setupNtupleVariables
void setupNtupleVariables()
Definition: NtupleVars.cxx:20
tree
TChain * tree
Definition: tile_monitor.h:30
VKalVrtAthena::varHolder::branch_tree
void branch_tree(const std::string &name, TTree *tree)
Definition: NtupleVars.h:107
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
VKalVrtAthena::NtupleVars::clear
void clear()
Definition: NtupleVars.cxx:50
VKalVrtAthena::BaseHolder::branch_tree
virtual void branch_tree(const std::string &, TTree *)=0
VKalVrtAthena::NtupleVars::isAvailable
bool isAvailable(const std::string &varname)
Definition: NtupleVars.h:138
VKalVrtAthena::varHolder::get_var
void * get_var()
Definition: NtupleVars.h:100
VKalVrtAthena::varHolder
Definition: NtupleVars.h:86
VKalVrtAthena::varHolder::get_typename
std::string & get_typename()
Definition: NtupleVars.h:101
VKalVrtAthena::NtupleVars::branchNtuple
void branchNtuple(TTree *tree)
Definition: NtupleVars.cxx:28
VKalVrtAthena::NtupleVars::addNewVar
void addNewVar(const std::string &varname)
Definition: NtupleVars.h:134
VKalVrtAthena::varHolder::m_typename
std::string m_typename
Definition: NtupleVars.h:88
VKalVrtAthena::BaseHolder::BaseHolder
BaseHolder()
Definition: NtupleVars.h:29
VKalVrtAthena::BaseHolder
Definition: NtupleVars.h:27
VKalVrtAthena
Definition: AANT_Tools.cxx:25
VKalVrtAthena::BaseHolder::~BaseHolder
virtual ~BaseHolder()
Definition: NtupleVars.h:30
VKalVrtAthena::varHolder::clear
void clear()
Definition: NtupleVars.h:105
VKalVrtAthena::varHolder_detail::branch_tree
void branch_tree(const std::string &name, const std::string &type_name, TTree *tree, T *var)
Definition: NtupleVars.h:75
VKalVrtAthena::BaseHolder::get_typename
virtual std::string & get_typename()=0
VKalVrtAthena::BaseHolder::clear
virtual void clear()=0
beamspotman.stat
stat
Definition: beamspotman.py:266
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
VKalVrtAthena::NtupleVars::m_varHolder
std::map< std::string, BaseHolder * > m_varHolder
Definition: NtupleVars.h:119
VKalVrtAthena::varHolder::varHolder
varHolder()
Definition: NtupleVars.h:92
VKalVrtAthena::BaseHolder::get_var
virtual void * get_var()=0
VKalVrtAthena::NtupleVars::get
T & get(const std::string &varname)
Definition: NtupleVars.h:165
VKalVrtAthena::NtupleVars
Definition: NtupleVars.h:117
python.PyAthena.v
v
Definition: PyAthena.py:154
LArG4AODNtuplePlotter.varname
def varname(hname)
Definition: LArG4AODNtuplePlotter.py:37
VKalVrtAthena::NtupleVars::m_RecoTrk_Int
std::vector< IntersectionPos * > m_RecoTrk_Int
Definition: NtupleVars.h:123
VKalVrtAthena::NtupleVars::~NtupleVars
~NtupleVars()
VKalVrtAthena::varHolder_detail::clear
void clear(T &var)
Definition: NtupleVars.h:48
VKalVrtAthena::NtupleVars::NtupleVars
NtupleVars()
IntersectionPos.h