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