Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
InDetPerfNtupleBranch.h
Go to the documentation of this file.
1 #ifndef INDET__PERF__NTUPLEBRANCH__H
2 #define INDET__PERF__NTUPLEBRANCH__H
3 
4 #include "TTree.h"
5 #include "TBranch.h"
6 #include <memory>
7 
9 
10 // forward declare the template used for providing the actual functionality
11 template <typename branchType> class InDetPerfNtupleBranch;
12 
13 // and a manager class used to work with the branches
14 class InDetPerfNtuple;
15 
16 // base class used to store branches in a manager class
18 public:
19  InDetPerfNtupleBranchBase(const std::string & branchName, InDetPerfNtuple & mgr);
21 
22  // fill the output branch
23  inline void fill(){ if (m_branch) m_branch->Fill(); }
24 
25  // set content to a dummy value. Used to reset this branch
26  // dummy value specified in specific template instantiations
27  virtual void setDummy()=0;
28 
29  // get the branch name
30  inline const std::string & getName() const { return m_branchName; }
31 
32  // get the corresponding TBranch of the underlying TTree
33  inline TBranch* getBranch() const { return m_branch; }
34 
35  // reports outcome of attachment attempt
36  enum class attachmentOutcome {
37  Success,
40  };
41  // attach to output tree - wrap around addPayloadAsBranch with input validation
42  attachmentOutcome attach(TTree* targetTree);
43 
44 protected:
45  // method specific to the payload type - handles creation of the TBranch instance
46  virtual TBranch* addPayloadAsBranch(TTree* targetTree)=0;
47 
48 private:
49  // data members
50 
51  // name of the branch
52  const std::string m_branchName;
53  // TBranch used to store the information
54  TBranch* m_branch{nullptr};
55 };
56 
57 // template specialisation for arbitrary branchType
58 template<typename branchType> class InDetPerfNtupleBranch : public InDetPerfNtupleBranchBase {
59 public:
60  // constructor
61  InDetPerfNtupleBranch(const std::string & branchName, branchType defaultValue, InDetPerfNtuple & mgr):
62  InDetPerfNtupleBranchBase(branchName, mgr),
63  m_default{defaultValue} {
64  }
65 
66  // access the current content of the branch
67  inline branchType & operator()(){ return get(); }
68  inline branchType & get(){ return m_content; }
69 
70  // fill the branch when writing an output tree
71  inline void set(const branchType & in){ m_content=in; }
72  inline void operator=(const branchType & in){ set(in); }
73 
74  // set to a dummy value
75  inline void setDummy(){ set(m_default); }
76 
77 protected:
78  // for reading
79  virtual TBranch* addPayloadAsBranch(TTree* targetTree){
80  return (targetTree ? targetTree->Branch(getName().c_str(), &m_content) : nullptr);
81  }
82 
83 private:
84  const branchType m_default;
85  branchType m_content{m_default};
86 };
87 
88 // template specialisation for vector branches
89 template<typename branchType> class InDetPerfNtupleBranch<std::vector<branchType>> : public InDetPerfNtupleBranchBase {
90 public:
91  // constructor, providing the branch name used and the tree to read from.
92  InDetPerfNtupleBranch(const std::string & branchName, InDetPerfNtuple & mgr):
93  InDetPerfNtupleBranchBase(branchName, mgr) {
94  }
95 
96  // access the vector member for direct manipulation
97  inline std::vector<branchType> & operator()(){ return get(); }
98  inline const std::vector<branchType> & get(){ return *m_content; }
99 
100  // access to elements by index
101  inline branchType operator()(size_t i){ return get(i); }
102  inline branchType get(size_t i){ return m_content->at(i); }
103 
104  // make iterable
105  inline const typename std::vector<branchType>::const_iterator begin(){ return get().begin(); }
106  inline const typename std::vector<branchType>::const_iterator end(){ return get().end(); }
107 
108  // overwrite the content
109  inline void set(const std::vector<branchType> & in){ *m_content = in; }
110 
111  // set to a dummy value - for a vector, this will trigger a reset
112  inline void setDummy(){ m_content->clear(); }
113 
114 protected:
115  virtual TBranch* addPayloadAsBranch(TTree* targetTree){
116  return (targetTree ? targetTree->Branch(getName().c_str(), m_content.get()) : nullptr);
117  }
118 
119 private:
120  std::shared_ptr<std::vector<branchType>> m_content;
121 };
122 
123 #endif // INDET__PERF__NTUPLEBRANCH__H
InDetPerfNtupleBranchBase::attachmentOutcome::FailedToBranch
@ FailedToBranch
InDetPerfNtupleBranchBase::~InDetPerfNtupleBranchBase
virtual ~InDetPerfNtupleBranchBase()
Definition: InDetPerfNtupleBranch.h:20
InDetPerfNtuple
This class is a base class for the actual ntuples used when writing IDPVM ntuples.
Definition: InDetPerfNtuple.h:11
InDetPerfNtupleBranch::setDummy
void setDummy()
Definition: InDetPerfNtupleBranch.h:75
InDetPerfNtupleBranchBase::fill
void fill()
Definition: InDetPerfNtupleBranch.h:23
InDetPerfNtupleBranchBase::m_branchName
const std::string m_branchName
Definition: InDetPerfNtupleBranch.h:52
InDetPerfNtupleBranch::get
branchType & get()
Definition: InDetPerfNtupleBranch.h:68
InDetPerfNtupleBranchBase::attachmentOutcome::InvalidTree
@ InvalidTree
InDetPerfNtupleBranch::InDetPerfNtupleBranch
InDetPerfNtupleBranch(const std::string &branchName, branchType defaultValue, InDetPerfNtuple &mgr)
Definition: InDetPerfNtupleBranch.h:61
InDetPerfNtupleBranch< std::vector< branchType > >::set
void set(const std::vector< branchType > &in)
Definition: InDetPerfNtupleBranch.h:109
InDetPerfNtupleBranchBase::attachmentOutcome
attachmentOutcome
Definition: InDetPerfNtupleBranch.h:36
InDetPerfNtupleBranch::m_content
branchType m_content
Definition: InDetPerfNtupleBranch.h:85
InDetPerfNtupleBranch::operator()
branchType & operator()()
Definition: InDetPerfNtupleBranch.h:67
InDetPerfNtupleBranchBase::attach
attachmentOutcome attach(TTree *targetTree)
Definition: InDetPerfNtupleBranch.cxx:9
InDetPerfNtupleBranch::set
void set(const branchType &in)
Definition: InDetPerfNtupleBranch.h:71
dumpTruth.getName
getName
Definition: dumpTruth.py:34
InDetPerfNtupleBranchBase::InDetPerfNtupleBranchBase
InDetPerfNtupleBranchBase(const std::string &branchName, InDetPerfNtuple &mgr)
Definition: InDetPerfNtupleBranch.cxx:4
InDetPerfNtupleBranch< std::vector< branchType > >::m_content
std::shared_ptr< std::vector< branchType > > m_content
Definition: InDetPerfNtupleBranch.h:120
InDetPerfNtupleBranchBase::setDummy
virtual void setDummy()=0
InDetPerfNtupleBranchBase::addPayloadAsBranch
virtual TBranch * addPayloadAsBranch(TTree *targetTree)=0
BchCleanup.mgr
mgr
Definition: BchCleanup.py:294
InDetPerfNtupleBranch< std::vector< branchType > >::begin
const std::vector< branchType >::const_iterator begin()
Definition: InDetPerfNtupleBranch.h:105
InDetPerfNtupleBranch< std::vector< branchType > >::get
const std::vector< branchType > & get()
Definition: InDetPerfNtupleBranch.h:98
InDetPerfNtupleBranchBase::m_branch
TBranch * m_branch
Definition: InDetPerfNtupleBranch.h:54
lumiFormat.i
int i
Definition: lumiFormat.py:85
vector
Definition: MultiHisto.h:13
InDetPerfNtupleBranchBase
Definition: InDetPerfNtupleBranch.h:17
InDetPerfNtupleBranch::m_default
const branchType m_default
Definition: InDetPerfNtupleBranch.h:84
InDetPerfNtupleBranchBase::attachmentOutcome::Success
@ Success
InDetPerfNtupleBranch::operator=
void operator=(const branchType &in)
Definition: InDetPerfNtupleBranch.h:72
InDetPerfNtupleBranch< std::vector< branchType > >::InDetPerfNtupleBranch
InDetPerfNtupleBranch(const std::string &branchName, InDetPerfNtuple &mgr)
Definition: InDetPerfNtupleBranch.h:92
InDetPerfNtupleBranch< std::vector< branchType > >::operator()
std::vector< branchType > & operator()()
Definition: InDetPerfNtupleBranch.h:97
InDetPerfNtupleBranch
This defines a helper class used for writing output ntuples in IDPVM.
Definition: InDetPerfNtupleBranch.h:11
InDetPerfNtupleBranchBase::getBranch
TBranch * getBranch() const
Definition: InDetPerfNtupleBranch.h:33
InDetPerfNtupleBranch< std::vector< branchType > >::setDummy
void setDummy()
Definition: InDetPerfNtupleBranch.h:112
InDetPerfNtupleBranchBase::getName
const std::string & getName() const
Definition: InDetPerfNtupleBranch.h:30
InDetPerfNtupleBranch::addPayloadAsBranch
virtual TBranch * addPayloadAsBranch(TTree *targetTree)
Definition: InDetPerfNtupleBranch.h:79
InDetPerfNtupleBranch< std::vector< branchType > >::end
const std::vector< branchType >::const_iterator end()
Definition: InDetPerfNtupleBranch.h:106
InDetPerfNtupleBranch< std::vector< branchType > >::get
branchType get(size_t i)
Definition: InDetPerfNtupleBranch.h:102
InDetPerfNtupleBranch< std::vector< branchType > >::addPayloadAsBranch
virtual TBranch * addPayloadAsBranch(TTree *targetTree)
Definition: InDetPerfNtupleBranch.h:115
InDetPerfNtupleBranch< std::vector< branchType > >::operator()
branchType operator()(size_t i)
Definition: InDetPerfNtupleBranch.h:101