Jpp  master_rocky-37-gf0c5bc59d
the software that should make you happy
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 
7 #include "JLang/JObjectOutput.hh"
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"
14 #include "JROOT/JTreeParameters.hh"
15 #include "JROOT/JRootFile.hh"
16 
17 /**
18  * \author mdejong
19  */
20 
21 namespace JROOT {}
22 namespace JPP { using namespace JROOT; }
23 
24 namespace JROOT {
25 
28  using JLANG::JTypeList;
29  using JLANG::JNullType;
30  using JLANG::JConversion;
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  */
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> :
150  public JTreeWriterObjectOutput<T>
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  */
184  void SetDirectory(TDirectory* dir)
185  {
188  }
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  */
217  JRootFileObjectOutput <T>(),
219  {}
220 
221 
222  /**
223  * Open file.
224  *
225  * \param file_name file name
226  */
227  virtual void open(const char* file_name) override
228  {
229  JRootOutputFile::open(file_name);
230 
231  if (is_open()) {
232  this->SetDirectory(getFile());
233  }
234  }
235  };
236 }
237 
238 #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.
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.
JAssert<!JConversion< T, TObject >::is_derived, bool >::type putObject(TDirectory *dir, const T &object)
Write object to ROOT directory.
Generation of compiler error.
Definition: JAssert.hh:17
Template class test for polymorphism.
Definition: JConversion.hh:22
Auxiliary class for no type definition.
Definition: JNullType.hh:19
Type list.
Definition: JTypeList.hh:23
Definition: JRoot.hh:19