Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRootFileWriter.hh
Go to the documentation of this file.
1 #ifndef __JROOTFILEWRITER__
2 #define __JROOTFILEWRITER__
3 
4 #include <string>
5 
6 #include "TFile.h"
7 
8 #include "JLang/JObjectOutput.hh"
9 #include "JLang/JTypeList.hh"
10 #include "JLang/JNullType.hh"
11 #include "JLang/JException.hh"
13 #include "JROOT/JTreeParameters.hh"
14 /**
15  * \author mdejong
16  */
17 
18 //#include "JROOT/JRootTree.hh"
19 #include "JROOT/JRootFile.hh"
20 
21 
22 namespace JROOT {}
23 namespace JPP { using namespace JROOT; }
24 
25 namespace JROOT {
26 
29  using JLANG::JTypeList;
30  using JLANG::JNullType;
31 
32 
33  /**
34  * Write object to ROOT directory.
35  *
36  * \param dir pointer to directory
37  * \param object object
38  * \return true if OK; else false
39  */
40  template<class T>
41  inline bool putObject(TDirectory* dir, const T& object)
42  {
43  return (dir != NULL && dir->WriteTObject(&object) > 0);
44  }
45 
46 
47  /**
48  * Template definition of ROOT object output.
49  */
50  template<class T, bool = JTreeParametersAvailable<T>::result>
52 
53 
54  /**
55  * Implementation of object output using TDirectory::WriteTObject method.
56  *
57  * This class implements the JLANG::JObjectOutput interface.
58  */
59  template<class T>
60  class JRootObjectOutput<T,false> :
61  public virtual JObjectOutput<T>
62  {
63  protected:
64  /**
65  * Default constructor.
66  */
68  directory(NULL)
69  {}
70 
71 
72  /**
73  * Set directory.
74  *
75  * \param dir pointer to directory
76  */
77  void SetDirectory(TDirectory* dir)
78  {
79  this->directory = dir;
80  }
81 
82 
83  TDirectory* directory;
84 
85  public:
86  /**
87  * Object output.
88  *
89  * \param object object
90  * \return true if OK; else false
91  */
92  virtual bool put(const T& object)
93  {
94  if (directory != NULL)
95  return putObject(directory, object);
96  else
97  return false;
98  }
99  };
100 
101 
102  /**
103  * Implementation of object output using TTree.
104  *
105  * This class implements the JLANG::JObjectOutput interface.
106  */
107  template<class T>
108  class JRootObjectOutput<T,true> :
109  public JTreeWriterObjectOutput<T>
110  {};
111 
112 
113  /**
114  * Implementation of object output to ROOT file for single data type.
115  */
116  template<class T>
118  public JRootObjectOutput<T>
119  {};
120 
121 
122  /**
123  * Implementation of object output to ROOT file for multiple data types.
124  *
125  * This class recursively implements the JLANG::JObjectOutput interface
126  * for all data types by deriving from:
127  * - JRootFileObjectOutput<JHead_t>; and
128  * - JRootFileObjectOutput<JTail_t>.
129  */
130  template<class JHead_t, class JTail_t>
131  class JRootFileObjectOutput< JTypeList<JHead_t, JTail_t> > :
132  public JObjectOutput< JTypeList<JHead_t, JTail_t> >,
133  public JRootFileObjectOutput<JHead_t>,
134  public JRootFileObjectOutput<JTail_t>
135 
136  {
137  protected:
138  /**
139  * Set directory.
140  *
141  * \param dir pointer to directory
142  */
143  void SetDirectory(TDirectory* dir)
144  {
147  }
148  };
149 
150 
151  /**
152  * Terminator class of recursive JRootObjectOutput class.
153  */
154  template<class JHead_t>
156  public JRootFileObjectOutput<JHead_t>
157  {};
158 
159 
160  /**
161  * ROOT file object output.
162  *
163  * This class implements the JLANG::JAccessibleObjectOutput interface.
164  */
165  template<class T>
167  public JRootFileObjectOutput <T>,
168  public JAccessibleObjectOutput<T>,
169  public JRootOutputFile
170  {
171  public:
172  /**
173  * Default constructor.
174  */
176  JRootFileObjectOutput <T>(),
178  {}
179 
180 
181  /**
182  * Open file.
183  *
184  * \param file_name file name
185  */
186  virtual void open(const char* file_name)
187  {
188  JRootOutputFile::open(file_name);
189 
190  if (is_open()) {
191  this->SetDirectory(getFile());
192  }
193  }
194  };
195 }
196 
197 #endif
Exceptions.
Implementation of object output to ROOT file for single data type.
Template definition of ROOT object output.
ROOT file object output.
void SetDirectory(TDirectory *dir)
Set directory.
JRootFileWriter()
Default constructor.
Type list.
Definition: JTypeList.hh:22
TFile * getFile() const
Get file.
Definition: JRootFile.hh:65
Auxiliary class for no type definition.
Definition: JNullType.hh:19
ROOT output file.
Definition: JRootFile.hh:147
virtual void open(const char *file_name)
Open file.
Definition: JRootFile.hh:177
JRootObjectOutput()
Default constructor.
virtual bool is_open() const =0
Check is device is open.
Forward declarations for definitions of I/O redirect and pipe operators.
virtual void open(const char *file_name)
Open file.
Interface for object output with named access.
virtual bool put(const T &object)
Object output.
bool putObject(TDirectory *dir, const T &object)
Write object to ROOT directory.