Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JTreeWriter.hh
Go to the documentation of this file.
1#ifndef __JROOT__JTREEWRITER__
2#define __JROOT__JTREEWRITER__
3
4#pragma GCC diagnostic push
5#pragma GCC diagnostic ignored "-Wall"
6#include "TTree.h"
7#include "TBranch.h"
8#pragma GCC diagnostic pop
9
10#include "JIO/JSerialisable.hh"
11#include "JLang/JException.hh"
12#include "JROOT/JRoot.hh"
15#include "TRealData.h"
16
17namespace JROOT {}
18namespace JPP { using namespace JROOT; }
19
20/**
21 * \file
22 * TTree writing for template data type.
23 * \author mdejong
24 */
25namespace JROOT {
26
27 using JIO::JReader;
29
30
31 /**
32 * Auxiliary class for default template TTree writing.
33 */
34 template<class T, bool flat = false>
36 public virtual TTree,
37 public JTreeParameters
38 {
39 public:
40 /**
41 * Constructor.
42 *
43 * Note that the default TTree parameters are obtained using method JROOT::getTreeParameters.
44 *
45 * \param parameters parameters of TTree
46 */
48 JTreeParameters(parameters),
49 address(NULL)
50 {
51 SetNameTitle(this->getTreeName(), this->getTreeTitle());
52
53 SetAutoFlush(this->getAutoFlush());
54 SetCacheSize();
55
56 branch = Branch(this->getBranchName(),
57 T::Class_Name(),
58 &address,
59 this->getBasketSize(),
60 this->getSplitLevel());
61
62 branch->SetCompressionLevel(this->getCompressionLevel());
63 }
64
65
66 /**
67 * Get the pointer to the unique TBranch belonging this TTree.
68 *
69 * \return pointer to TBranch
70 */
71 const TBranch* GetBranch() const
72 {
73 return branch;
74 }
75
76
77 /**
78 * Data object output equivalent of TTree::Fill().
79 *
80 * \param object data object
81 * \return as TTree::Fill
82 */
83 Int_t Write(const T& object)
84 {
85 address = &object;
86
87 return this->Fill();
88 }
89
90 protected:
91 using TTree::GetBranch;
92 using TTree::Write;
93
94 private:
95 TBranch* branch; //!< Pointer to unique branch belonging to this TTree.
96 const T* address; //!< Pointer to unique object belonging to this TTree.
97 };
98
99
100 /**
101 * Template specialisation for flat template TTree writing.
102 */
103 template<class T>
104 class JTreeWriter<T, true> : public virtual TTree
105 {
106 public:
107 /**
108 * Constructor.
109 *
110 * \param parameters parameters of TTree
111 */
112 JTreeWriter(const JTreeParameters& parameters)
113 {
114 SetNameTitle(parameters.getTreeName(), parameters.getTreeTitle());
115
116 // if more than one branch, check those are the data members of T
117
118 TClass* t_class = TClass::GetClass<T>();
119
120 if (t_class == nullptr) {
121 THROW(JException, "Could not get class " << typeid(T).name());
122 }
123
124 TIter next(t_class->GetListOfRealData());
125 TRealData* data{nullptr};
126 T object;
127
128 auto* base = reinterpret_cast<uint8_t*>(&object);
129
130 while ((data = dynamic_cast<TRealData*>(next())) != nullptr) {
131
132 if (!JRoot::is_tobject_member(data->GetName())) {
133
134 auto* member = data->GetDataMember();
135 auto* address = reinterpret_cast<uint8_t*>(base + member->GetOffset());
136 std::string type_name = data->GetDataMember()->GetTypeName();
137
138 if (member->GetArrayDim() > 0) {
139 type_name += "[]";
140 }
141
142 std::string type_code = JRootPrimitiveTypes::getTypeCode(type_name);
143
144 if (type_code.empty()) {
145 THROW(JException, "unknown type " << type_name);
146 }
147
148 std::string leaf = data->GetName();
149 leaf += "/" + type_code;
150 auto branch = Branch(data->GetName(), address, leaf.c_str());
151
152 branches_.push_back(branch);
153 offsets_ .push_back(member->GetOffset());
154 }
155 }
156 }
157
158 /**
159 * Data object output equivalent of TTree::Fill().
160 *
161 * \param object data object
162 * \return as TTree::Fill
163 */
164 Int_t Write(const T& object)
165 {
166 auto* base = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(&object));
167
168 for (size_t i = 0; i < branches_.size(); i++) {
169 branches_[i]->SetAddress(reinterpret_cast<uint8_t*>(base + offsets_[i]));
170 }
171
172 return this->Fill();
173 }
174
175 protected:
176 using TTree::Write;
177
178 private:
180#if ROOT_VERSION_CODE <= ROOT_VERSION(6,26,0)
182#else
183 std::vector<Longptr_t> offsets_;
184#endif
185 };
186
187 /**
188 * Interface for template TTree writing and copying.
189 */
191 public virtual TTree
192 {
193 /**
194 * Copy data.
195 *
196 * \param in binary reader
197 */
198 virtual Int_t copy(JReader& in) = 0;
199 };
200
201
202 /**
203 * Implementation for template TTree writing and copying.
204 * This class implements the JTreeCopyWriter interface.
205 */
206 template<class T>
208 public JTreeWriter<T>,
210 {
211 protected:
212 /**
213 * Constructor.
214 *
215 * \param tree parameters of TTree
216 */
218 JTreeWriter<T>(tree)
219 {}
220
221
222 /**
223 * Hide copy constructor.
224 *
225 * \param writer TTree writer object
226 */
228
229
230 public:
231 /**
232 * Get reference to unique instance of this class object.
233 *
234 * \return reference to this class object
235 */
237 {
239
240 return writer;
241 }
242
243
244 /**
245 * Copy data.
246 *
247 * \param in binary reader
248 */
249 virtual Int_t copy(JReader& in) override
250 {
251 in >> object;
252
253 return static_cast<JTreeWriter<T>&>(*this).Write(object);
254 }
255
256
257 protected:
259 };
260
261
262 /**
263 * Get the TTree writer and copy for this type of object.
264 *
265 * \return TTree writer and copy for this type of object
266 */
267 template<class T>
272} // namespace JROOT
273
274#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Interface for binary input.
General exception.
Definition JException.hh:24
Implementation for template TTree writing and copying.
virtual Int_t copy(JReader &in) override
Copy data.
JTreeCopyWriter(const JTreeCopyWriter< T > &writer)
Hide copy constructor.
static JTreeCopyWriter< T > & getInstance()
Get reference to unique instance of this class object.
JTreeCopyWriter(const JTreeParameters &tree)
Constructor.
Data structure for TTree parameters.
int getBasketSize() const
Get basket size.
const JTreeParameters & getTreeParameters() const
Get TTree parameters.
Long64_t getAutoFlush() const
Get auto flush.
int getCompressionLevel() const
Get compression level.
int getSplitLevel() const
Get split level.
const TString & getBranchName() const
Get TBranch name.
const TString & getTreeTitle() const
Get TTree title.
const TString & getTreeName() const
Get TTree name.
std::vector< TBranch * > branches_
JTreeWriter(const JTreeParameters &parameters)
Constructor.
std::vector< Long_t > offsets_
Int_t Write(const T &object)
Data object output equivalent of TTree::Fill().
Auxiliary class for default template TTree writing.
JTreeWriter(const JTreeParameters &parameters=JROOT::getTreeParameters< T >())
Constructor.
const TBranch * GetBranch() const
Get the pointer to the unique TBranch belonging this TTree.
const T * address
Pointer to unique object belonging to this TTree.
TBranch * branch
Pointer to unique branch belonging to this TTree.
Int_t Write(const T &object)
Data object output equivalent of TTree::Fill().
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
JTreeParameters & getTreeParameters()
Template definition for method returning TTree parameters.
JTreeCopyWriter< T > & getTreeCopyWriter()
Get the TTree writer and copy for this type of object.
static std::string getTypeCode(const std::string &type_name)
Return the type code (used to create primitive leaves in basic Root tree branches) corresponding to t...
static bool is_tobject_member(const char *name)
Check if name is one of TObject own data members (fBits or fUniqueID, for ROOT <= 6....
Definition JRoot.hh:122
Interface for template TTree writing and copying.
virtual Int_t copy(JReader &in)=0
Copy data.