forked from rutgersnu/xsec_analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeUtils.hh
75 lines (63 loc) · 2.42 KB
/
TreeUtils.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
// ROOT includes
#include "TTree.h"
// **** Helper code to facilitate setting TTree branch addresses, etc. ****
// A std::unique_ptr with redundant storage of a bare pointer to the managed
// object. This is a hacky workaround for setting TTree branch addresses for
// objects managed by a std::unique_ptr.
template <typename T> class MyPointer : public std::unique_ptr<T> {
public:
MyPointer() : std::unique_ptr<T>( new T ) {}
T*& get_bare_ptr() {
bare_ptr_ = this->get();
return bare_ptr_;
}
protected:
T* bare_ptr_ = nullptr;
};
// Helper function template that sets a new address for a pointer to an object
// in an input TTree
template <typename T> void set_object_input_branch_address( TTree& in_tree,
const std::string& branch_name, T*& address )
{
in_tree.SetBranchAddress( branch_name.c_str(), &address );
}
// Overloaded version that uses a MyPointer argument instead
template <typename T> void set_object_input_branch_address( TTree& in_tree,
const std::string& branch_name, MyPointer<T>& u_ptr )
{
T*& address = u_ptr.get_bare_ptr();
set_object_input_branch_address( in_tree, branch_name, address );
}
// Helper function that creates a branch (or just sets a new address) for a
// simple variable in an output TTree
void set_output_branch_address( TTree& out_tree, const std::string& branch_name,
void* address, bool create = false, const std::string& leaf_spec = "" )
{
if ( create ) {
if ( leaf_spec != "" ) {
out_tree.Branch( branch_name.c_str(), address, leaf_spec.c_str() );
}
else {
out_tree.Branch( branch_name.c_str(), address );
}
}
else {
out_tree.SetBranchAddress( branch_name.c_str(), address );
}
}
// Helper function template that creates a branch (or just sets a new address)
// for a pointer to an object in an output TTree
template <typename T> void set_object_output_branch_address( TTree& out_tree,
const std::string& branch_name, T*& address, bool create = false )
{
if ( create ) out_tree.Branch( branch_name.c_str(), &address );
else out_tree.SetBranchAddress( branch_name.c_str(), &address );
}
// Overloaded version that uses a MyPointer argument instead
template <typename T> void set_object_output_branch_address( TTree& out_tree,
const std::string& branch_name, MyPointer<T>& u_ptr, bool create = false )
{
T*& address = u_ptr.get_bare_ptr();
set_object_output_branch_address( out_tree, branch_name, address, create );
}