Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JRootFileWriter.hh
Go to the documentation of this file.
1#ifndef __JROOTFILEWRITER__
2#define __JROOTFILEWRITER__
3
4#include "TObject.h"
5#include "TFile.h"
6
8#include "JLang/JTypeList.hh"
9#include "JLang/JNullType.hh"
10#include "JLang/JException.hh"
11#include "JLang/JConversion.hh"
12#include "JLang/JAssert.hh"
15#include "JROOT/JRootFile.hh"
16
17/**
18 * \author mdejong
19 */
20
21namespace JROOT {}
22namespace JPP { using namespace JROOT; }
23
24namespace JROOT {
25
28 using JLANG::JTypeList;
29 using JLANG::JNullType;
31 using JLANG::JAssert;
32
33
34 /**
35 * Write object to ROOT directory.
36 *
37 * \param dir directory
38 * \param object object
39 * \return true if OK; else false
40 */
41 inline bool putObject(TDirectory& dir, const TObject& object)
42 {
43 return dir.WriteTObject(&object) > 0;
44 }
45
46
47 /**
48 * Write object to ROOT directory.
49 *
50 * \param dir directory
51 * \param object object
52 * \return true if OK; else false
53 */
54 template<class T>
55 inline typename JAssert<!JConversion<T, TObject>::is_derived, bool>::type putObject(TDirectory& dir, const T& object)
56 {
57 return dir.WriteObject(&object, T::Class_Name()) > 0;
58 }
59
60
61 /**
62 * Write object to ROOT directory.
63 *
64 * \param dir pointer to directory
65 * \param object object
66 * \return true if OK; else false
67 */
68 inline bool putObject(TDirectory* dir, const TObject& object)
69 {
70 return (dir != NULL && putObject(*dir, object));
71 }
72
73
74 /**
75 * Write object to ROOT directory.
76 *
77 * \param dir pointer to directory
78 * \param object object
79 * \return true if OK; else false
80 */
81 template<class T>
82 inline typename JAssert<!JConversion<T, TObject>::is_derived, bool>::type putObject(TDirectory* dir, const T& object)
83 {
84 return (dir != NULL && putObject(*dir, object));
85 }
86
87
88 /**
89 * Template definition of ROOT object output.
90 */
91 template<class T, bool = JTreeParametersAvailable<T>::result>
93
94
95 /**
96 * Implementation of object output using TDirectory::WriteTObject method.
97 *
98 * This class implements the JLANG::JObjectOutput interface.
99 */
100 template<class T>
101 class JRootObjectOutput<T,false> :
102 public virtual JObjectOutput<T>
103 {
104 protected:
105 /**
106 * Default constructor.
107 */
109 directory(NULL)
110 {}
111
112
113 /**
114 * Set directory.
115 *
116 * \param dir pointer to directory
117 */
118 void SetDirectory(TDirectory* dir)
119 {
120 this->directory = dir;
121 }
122
123
124 TDirectory* directory;
125
126 public:
127 /**
128 * Object output.
129 *
130 * \param object object
131 * \return true if OK; else false
132 */
133 virtual bool put(const T& object) override
134 {
135 if (directory != NULL)
136 return putObject(directory, object);
137 else
138 return false;
139 }
140 };
141
142
143 /**
144 * Implementation of object output using TTree.
145 *
146 * This class implements the JLANG::JObjectOutput interface.
147 */
148 template<class T>
149 class JRootObjectOutput<T,true> :
151 {};
152
153
154 /**
155 * Implementation of object output to ROOT file for single data type.
156 */
157 template<class T>
159 public JRootObjectOutput<T>
160 {};
161
162
163 /**
164 * Implementation of object output to ROOT file for multiple data types.
165 *
166 * This class recursively implements the JLANG::JObjectOutput interface
167 * for all data types by deriving from:
168 * - JRootFileObjectOutput<JHead_t>; and
169 * - JRootFileObjectOutput<JTail_t>.
170 */
171 template<class JHead_t, class JTail_t>
172 class JRootFileObjectOutput< JTypeList<JHead_t, JTail_t> > :
173 public JObjectOutput< JTypeList<JHead_t, JTail_t> >,
174 public JRootFileObjectOutput<JHead_t>,
175 public JRootFileObjectOutput<JTail_t>
176
177 {
178 protected:
179 /**
180 * Set directory.
181 *
182 * \param dir pointer to directory
183 */
189 };
190
191
192 /**
193 * Terminator class of recursive JRootObjectOutput class.
194 */
195 template<class JHead_t>
197 public JRootFileObjectOutput<JHead_t>
198 {};
199
200
201 /**
202 * ROOT file object output.
203 *
204 * This class implements the JLANG::JAccessibleObjectOutput interface.
205 */
206 template<class T>
208 public JRootFileObjectOutput <T>,
209 public JAccessibleObjectOutput<T>,
210 public JRootOutputFile
211 {
212 public:
213 /**
214 * Default constructor.
215 */
220
221
222 /**
223 * Constructor.
224 *
225 * \param file_name file name
226 */
227 JRootFileWriter(const char* file_name) :
230 {
231 open(file_name);
232 }
233
234
235 /**
236 * Open file.
237 *
238 * \param file_name file name
239 */
240 virtual void open(const char* file_name) override
241 {
242 JRootOutputFile::open(file_name);
243
244 if (is_open()) {
245 this->SetDirectory(getFile());
246 }
247 }
248 };
249}
250
251#endif
Exceptions.
Interface for object output with named access.
virtual bool is_open() const =0
Check is device is open.
Template interface of object output for single data type.
Implementation of object output to ROOT file for single data type.
ROOT file object output.
JRootFileWriter()
Default constructor.
JRootFileWriter(const char *file_name)
Constructor.
virtual void open(const char *file_name) override
Open file.
TFile * getFile() const
Get file.
Definition JRootFile.hh:66
void SetDirectory(TDirectory *dir)
Set directory.
virtual bool put(const T &object) override
Object output.
Template definition of ROOT object output.
ROOT output file.
Definition JRootFile.hh:198
virtual void open(const char *file_name) override
Open file.
Definition JRootFile.hh:237
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
bool putObject(TDirectory &dir, const TObject &object)
Write object to ROOT directory.
Generation of compiler error.
Definition JAssert.hh:17
Template class test for polymorphism.
Auxiliary class for no type definition.
Definition JNullType.hh:19
Type list.
Definition JTypeList.hh:23