AmpGen 2.1
Loading...
Searching...
No Matches
TreeReader.h
Go to the documentation of this file.
1#ifndef AMPGEN_TREEREADER_H
2#define AMPGEN_TREEREADER_H 1
3
4#include <vector>
5
6#include "AmpGen/MsgService.h"
7#include "AmpGen/MetaUtils.h"
8
9class TTree;
10
11namespace AmpGen
12{
14 {
15 private:
16 std::string getBranchType(const std::string& name);
17 struct IReadBranch {
18 std::string name;
19 IReadBranch( const std::string& name = "" ) : name( name ) {}
20 virtual void* address() const = 0;
21 virtual void transfer() = 0;
22 virtual ~IReadBranch() = default;
23 };
24
25 template <class InputType, class OutputType> struct ReadBranch : public IReadBranch
26 {
27 InputType thing;
28 OutputType* output;
29 void* address() const override { return (void*)&thing; }
30 ReadBranch( const std::string& name, OutputType* outputBranch ) : IReadBranch( name ), output( outputBranch ) {}
31 void transfer() override { *output = thing; }
32 };
33
34 template <class InputType, class OutputType> struct ReinterpretBranch : public IReadBranch
35 {
36 InputType thing;
37 OutputType* output;
38 void* address() const override { return (void*)&thing; }
39 ReinterpretBranch( const std::string& name, OutputType* outputBranch ) : IReadBranch( name ), output( outputBranch ) {}
40 void transfer() override { *output = reinterpret_cast<OutputType>(thing); }
41 };
42 struct Iterator {
43 size_t m_position;
44 TreeReader* m_parent;
45 Iterator( const size_t& pos, TreeReader* parent ) : m_position( pos ), m_parent( parent ) {}
46 Iterator& operator++()
47 {
48 m_position++;
49 m_parent->getEntry( m_position );
50 return *this;
51 }
52 bool operator==( const Iterator& rhs ) const { return m_position == rhs.m_position; }
53 bool operator!=( const Iterator& rhs ) const { return m_position != rhs.m_position; }
54 size_t operator*() const { return m_position; }
55 unsigned pos() const { return m_position; }
56 };
57 TTree* m_tree = {nullptr};
58 bool m_ready = {false};
59 std::vector<IReadBranch*> m_branches = {};
60 std::vector<size_t> m_entryList = {};
61 public:
62 template <class OutputType> struct Proxy
63 {
64 OutputType* data;
65 Proxy() : data( new OutputType() ) {}
66 operator OutputType() const { return *data; }
67 ~Proxy(){ delete data ; }
68 };
69 explicit TreeReader( TTree* tree );
70 template <typename OutputType> Proxy<OutputType> make_proxy( const std::string& name )
71 {
73 setBranch( name, rt.data );
74 return rt;
75 }
76 template <typename OutputType> void setBranch( const std::string& name, OutputType& thing ) {
77 setBranch(name,&thing) ; }
78 template <typename OutputType> void setBranch( const std::string& name, OutputType* ptr )
79 {
80 IReadBranch* new_branch = nullptr;
81 auto branchType = getBranchType(name);
82 if( branchType == "Double_t" ) new_branch = new ReadBranch<Double_t , OutputType>( name, ptr );
83 if( branchType == "Float_t" ) new_branch = new ReadBranch<Float_t , OutputType>( name, ptr );
84 if( branchType == "Bool_t" ) new_branch = new ReadBranch<Bool_t , OutputType>( name, ptr );
85 if( branchType == "Int_t" ) new_branch = new ReadBranch<Int_t , OutputType>( name, ptr );
86 if( branchType == "UInt_t" ) new_branch = new ReadBranch<UInt_t , OutputType>( name, ptr );
87 if( branchType == "ULong64_t") new_branch = new ReadBranch<ULong64_t, OutputType>( name, ptr );
88 if( new_branch == nullptr ){
89 ERROR( "Branch type:" << branchType << " not recognised" );
90 return;
91 }
92 DEBUG("Making branch with properties: [name = " << name << ", input type = " << branchType << " output type = " << type_string<OutputType>() << "]" );
93 m_ready = false;
94 m_branches.push_back( new_branch );
95 }
96 void setEntryList( const std::vector<size_t>& entryList );
98 void getEntry( const unsigned int& entry );
99 void prepare();
100 size_t nEntries() const;
102 Iterator begin();
103 Iterator end();
104 };
105// ENABLE_DEBUG(TreeReader);
106} // namespace AmpGen
107
108#endif
Proxy< OutputType > make_proxy(const std::string &name)
Definition TreeReader.h:70
void setBranch(const std::string &name, OutputType &thing)
Definition TreeReader.h:76
void setBranch(const std::string &name, OutputType *ptr)
Definition TreeReader.h:78
void setEntryList(const std::vector< size_t > &entryList)
TreeReader(TTree *tree)
size_t nEntries() const
void getEntry(const unsigned int &entry)
#define ERROR(X)
Used for printing errors messages, and will always be printed.
Definition MsgService.h:80
#define DEBUG(X)
Used for printing verbose debugging messages, only if DEBUGLEVEL is defined.
Definition MsgService.h:66
Complex< real_t > operator*(const Complex< real_t > &lhs, const R2_t &rhs)
Definition Complex.h:52
std::string type_string()
Utility classes for compile-time metaprogramming, such as identifying the types of arguments for gene...
Definition MetaUtils.h:18
Expression operator==(const Expression &A, const Expression &B)