forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeStream.h
More file actions
222 lines (198 loc) · 6.94 KB
/
TreeStream.h
File metadata and controls
222 lines (198 loc) · 6.94 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// @brief Class for creating debug root trees with std::cout like intervace
/// @author Marian Ivanov, marian.ivanov@cern.ch (original code in AliRoot)
/// Ruben Shahoyan, ruben.shahoyan@cern.ch (porting to O2).
#ifndef ALICEO2_TREESTREAM_H
#define ALICEO2_TREESTREAM_H
#include <TString.h>
#include <TTree.h>
#include <vector>
#include <type_traits>
#include <concepts>
#include "GPUCommonDef.h"
class TBranch;
class TClass;
class TDataType;
namespace o2
{
namespace utils
{
/// The TreeStream class allows creating a root tree of any objects having root
/// dictionary, using operator<< interface, and w/o prior tree declaration.
/// The format is:
/// treeStream << "branchName0="<<objPtr
/// <<"branchName1="<<objRed
/// <<"branchName2="
/// <<elementaryTypeVar<<"\n"
///
/// See testTreeStream.cxx for functional example
///
namespace details
{
template <typename T>
struct IsTrivialRootType {
static constexpr bool value =
std::is_same_v<T, Float_t> || // Float_t
std::is_same_v<T, Double_t> || // Double_t
std::is_same_v<T, ULong64_t> || std::is_same_v<T, ULong_t> || // ULong64_t or ULong_t
std::is_same_v<T, Long64_t> || std::is_same_v<T, Long_t> || // Long64_t or Long_t
std::is_same_v<T, UInt_t> || // UInt_t
std::is_same_v<T, Int_t> || // Int_t
std::is_same_v<T, UShort_t> || // UShort_t
std::is_same_v<T, Short_t> || // Short_t
std::is_same_v<T, UChar_t> || // UChar_t
std::is_same_v<T, Char_t> || std::is_same_v<T, int8_t> || std::is_same_v<T, Bool_t>; // Char_t, int8_t, or Bool_t
};
template <typename T>
struct IsTrivialRootType<T[]> {
static constexpr bool value = IsTrivialRootType<T>::value;
};
template <typename T, std::size_t N>
struct IsTrivialRootType<T[N]> {
static constexpr bool value = IsTrivialRootType<T>::value;
};
template <typename T>
concept TrivialRootType = IsTrivialRootType<T>::value;
template <typename T>
concept ComplexRootType = !IsTrivialRootType<T>::value;
template <TrivialRootType T>
static constexpr char getRootTypeCode()
{
if constexpr (std::is_array_v<T>) {
return getRootTypeCode<std::remove_all_extents_t<T>>();
} else if constexpr (std::is_same_v<T, Float_t>) {
return 'F';
} else if constexpr (std::is_same_v<T, Double_t>) {
return 'D';
} else if constexpr (std::is_same_v<T, ULong64_t> ||
std::is_same_v<T, ULong_t>) {
return 'l';
} else if constexpr (std::is_same_v<T, Long64_t> ||
std::is_same_v<T, Long_t>) {
return 'L';
} else if constexpr (std::is_same_v<T, UInt_t>) {
return 'i';
} else if constexpr (std::is_same_v<T, Int_t>) {
return 'I';
} else if constexpr (std::is_same_v<T, UShort_t>) {
return 's';
} else if constexpr (std::is_same_v<T, Short_t>) {
return 'S';
} else if constexpr (std::is_same_v<T, UChar_t>) {
return 'b';
} else if constexpr (std::is_same_v<T, Char_t> ||
std::is_same_v<T, int8_t> ||
std::is_same_v<T, Bool_t>) {
return 'B';
} else {
static_assert(false, "unsupported type!");
}
}
} // namespace details
class TreeStream
{
public:
struct TreeDataElement {
int arsize = 1; ///< size of array
char type = 0; ///< type of data element
const TClass* cls = nullptr; ///< data type pointer
const void* ptr = nullptr; ///< pointer to element
std::string name; ///< name of the element
};
TreeStream(const char* treename);
TreeStream() = default;
virtual ~TreeStream() = default;
void Close() { mTree.Write(); }
Int_t CheckIn(Char_t type, const void* pointer);
void BuildTree();
void Fill();
Double_t getSize() { return mTree.GetZipBytes(); }
TreeStream& Endl();
TTree& getTree() { return mTree; }
const char* getName() const { return mTree.GetName(); }
void setID(int id) { mID = id; }
int getID() const { return mID; }
template <details::TrivialRootType T>
TreeStream& operator<<(const T& t)
{
CheckIn(details::getRootTypeCode<T>(), &t);
return *this;
}
TreeStream& operator<<(const Char_t* name);
template <class T>
TreeStream& operator<<(const T* obj)
{
CheckIn(obj);
return *this;
}
template <details::ComplexRootType T, typename std::enable_if<!std::is_pointer<GPUgeneric() T>::value, bool>::type* = nullptr>
TreeStream& operator<<(const T& obj)
{
CheckIn(&obj);
return *this;
}
template <class T>
Int_t CheckIn(const T* obj);
private:
//
std::vector<TreeDataElement> mElements;
std::vector<TBranch*> mBranches; ///< pointers to branches
TTree mTree; ///< data storage
int mCurrentIndex = 0; ///< index of current element
int mID = -1; ///< identifier of layout
int mNextNameCounter = 0; ///< next name counter
int mNextArraySize = 0; ///< next array size
int mStatus = 0; ///< status of the layout
TString mNextName; ///< name for next entry
ClassDefNV(TreeStream, 0);
};
template <class T>
Int_t TreeStream::CheckIn(const T* obj)
{
// check in arbitrary class having dictionary
TClass* pClass = nullptr;
if (obj) {
pClass = TClass::GetClass(typeid(*obj));
}
if (mCurrentIndex >= static_cast<int>(mElements.size())) {
auto& element = mElements.emplace_back();
element.cls = pClass;
TString name = mNextName;
if (name.Length()) {
if (mNextNameCounter > 0) {
name += mNextNameCounter;
}
} else {
name = TString::Format("B%d", static_cast<int>(mElements.size()));
}
element.name = name.Data();
element.ptr = obj;
element.arsize = mNextArraySize;
mNextArraySize = 1; // reset
} else {
auto& element = mElements[mCurrentIndex];
if (!element.cls) {
element.cls = pClass;
} else {
if (element.cls != pClass && pClass) {
mStatus++;
return 1; // mismatched data element
}
}
element.ptr = obj;
}
mCurrentIndex++;
return 0;
}
} // namespace utils
} // namespace o2
#endif