ATLAS Offline Software
Loading...
Searching...
No Matches
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
11template <typename branchType> class InDetPerfNtupleBranch;
12
13// and a manager class used to work with the branches
14class InDetPerfNtuple;
15
16// base class used to store branches in a manager class
18public:
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
41 // attach to output tree - wrap around addPayloadAsBranch with input validation
42 attachmentOutcome attach(TTree* targetTree);
43
44protected:
45 // method specific to the payload type - handles creation of the TBranch instance
46 virtual TBranch* addPayloadAsBranch(TTree* targetTree)=0;
47
48private:
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
58template<typename branchType> class InDetPerfNtupleBranch : public InDetPerfNtupleBranchBase {
59public:
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
77protected:
78 // for reading
79 virtual TBranch* addPayloadAsBranch(TTree* targetTree){
80 return (targetTree ? targetTree->Branch(getName().c_str(), &m_content) : nullptr);
81 }
82
83private:
84 const branchType m_default;
85 branchType m_content{m_default};
86};
87
88// template specialisation for vector branches
89template<typename branchType> class InDetPerfNtupleBranch<std::vector<branchType>> : public InDetPerfNtupleBranchBase {
90public:
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
114protected:
115 virtual TBranch* addPayloadAsBranch(TTree* targetTree){
116 return (targetTree ? targetTree->Branch(getName().c_str(), m_content.get()) : nullptr);
117 }
118
119private:
120 std::shared_ptr<std::vector<branchType>> m_content;
121};
122
123#endif // INDET__PERF__NTUPLEBRANCH__H
const std::string & getName() const
virtual TBranch * addPayloadAsBranch(TTree *targetTree)=0
virtual void setDummy()=0
attachmentOutcome attach(TTree *targetTree)
InDetPerfNtupleBranchBase(const std::string &branchName, InDetPerfNtuple &mgr)
void set(const std::vector< branchType > &in)
InDetPerfNtupleBranch(const std::string &branchName, InDetPerfNtuple &mgr)
const std::vector< branchType >::const_iterator begin()
std::shared_ptr< std::vector< branchType > > m_content
virtual TBranch * addPayloadAsBranch(TTree *targetTree)
const std::vector< branchType >::const_iterator end()
This defines a helper class used for writing output ntuples in IDPVM.
void set(const branchType &in)
InDetPerfNtupleBranch(const std::string &branchName, branchType defaultValue, InDetPerfNtuple &mgr)
void operator=(const branchType &in)
virtual TBranch * addPayloadAsBranch(TTree *targetTree)
This class is a base class for the actual ntuples used when writing IDPVM ntuples.
STL namespace.