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 "TFile.h"
5 
6 #include "JLang/JObjectOutput.hh"
7 #include "JLang/JTypeList.hh"
8 #include "JLang/JNullType.hh"
9 #include "JLang/JException.hh"
11 #include "JROOT/JTreeParameters.hh"
12 #include "JROOT/JRootFile.hh"
13 
14 /**
15  * \author mdejong
16  */
17 
18 
19 namespace JROOT {}
20 namespace JPP { using namespace JROOT; }
21 
22 namespace JROOT {
23 
26  using JLANG::JTypeList;
27  using JLANG::JNullType;
28 
29 
30  /**
31  * Write object to ROOT directory.
32  *
33  * \param dir pointer to directory
34  * \param object object
35  * \return true if OK; else false
36  */
37  template<class T>
38  inline bool putObject(TDirectory* dir, const T& object)
39  {
40  return (dir != NULL && dir->WriteTObject(&object) > 0);
41  }
42 
43 
44  /**
45  * Template definition of ROOT object output.
46  */
49 
50 
51  /**
52  * Implementation of object output using TDirectory::WriteTObject method.
53  *
54  * This class implements the JLANG::JObjectOutput interface.
55  */
56  template<class T>
57  class JRootObjectOutput<T,false> :
58  public virtual JObjectOutput<T>
59  {
60  protected:
61  /**
62  * Default constructor.
63  */
65  directory(NULL)
66  {}
67 
68 
69  /**
70  * Set directory.
71  *
72  * \param dir pointer to directory
73  */
74  void SetDirectory(TDirectory* dir)
75  {
76  this->directory = dir;
77  }
78 
79 
80  TDirectory* directory;
81 
82  public:
83  /**
84  * Object output.
85  *
86  * \param object object
87  * \return true if OK; else false
88  */
89  virtual bool put(const T& object) override
90  {
91  if (directory != NULL)
92  return putObject(directory, object);
93  else
94  return false;
95  }
96  };
97 
98 
99  /**
100  * Implementation of object output using TTree.
101  *
102  * This class implements the JLANG::JObjectOutput interface.
103  */
104  template<class T>
105  class JRootObjectOutput<T,true> :
106  public JTreeWriterObjectOutput<T>
107  {};
108 
109 
110  /**
111  * Implementation of object output to ROOT file for single data type.
112  */
113  template<class T>
115  public JRootObjectOutput<T>
116  {};
117 
118 
119  /**
120  * Implementation of object output to ROOT file for multiple data types.
121  *
122  * This class recursively implements the JLANG::JObjectOutput interface
123  * for all data types by deriving from:
124  * - JRootFileObjectOutput<JHead_t>; and
125  * - JRootFileObjectOutput<JTail_t>.
126  */
127  template<class JHead_t, class JTail_t>
128  class JRootFileObjectOutput< JTypeList<JHead_t, JTail_t> > :
129  public JObjectOutput< JTypeList<JHead_t, JTail_t> >,
130  public JRootFileObjectOutput<JHead_t>,
131  public JRootFileObjectOutput<JTail_t>
132 
133  {
134  protected:
135  /**
136  * Set directory.
137  *
138  * \param dir pointer to directory
139  */
140  void SetDirectory(TDirectory* dir)
141  {
144  }
145  };
146 
147 
148  /**
149  * Terminator class of recursive JRootObjectOutput class.
150  */
151  template<class JHead_t>
153  public JRootFileObjectOutput<JHead_t>
154  {};
155 
156 
157  /**
158  * ROOT file object output.
159  *
160  * This class implements the JLANG::JAccessibleObjectOutput interface.
161  */
162  template<class T>
164  public JRootFileObjectOutput <T>,
165  public JAccessibleObjectOutput<T>,
166  public JRootOutputFile
167  {
168  public:
169  /**
170  * Default constructor.
171  */
175  {}
176 
177 
178  /**
179  * Open file.
180  *
181  * \param file_name file name
182  */
183  virtual void open(const char* file_name) override
184  {
185  JRootOutputFile::open(file_name);
186 
187  if (is_open()) {
188  this->SetDirectory(getFile());
189  }
190  }
191  };
192 }
193 
194 #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
virtual void open(const char *file_name) override
Open file.
return result
Definition: JPolint.hh:727
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Auxiliary class for no type definition.
Definition: JNullType.hh:19
ROOT output file.
Definition: JRootFile.hh:147
JRootObjectOutput()
Default constructor.
virtual bool is_open() const =0
Check is device is open.
Template interface of object output for single data type.
then echo Creating output directory
Definition: JTuneHV.sh:77
virtual void open(const char *file_name) override
Open file.
Definition: JRootFile.hh:177
Interface for object output with named access.
virtual bool put(const T &object) override
Object output.
bool putObject(TDirectory *dir, const T &object)
Write object to ROOT directory.