Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTreeRecorder.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT_JTREERECORDER__
2 #define __JSUPPORT_JTREERECORDER__
3 
4 #include "JLang/JObjectOutput.hh"
6 #include "JROOT/JRootFile.hh"
7 
8 /**
9  * \author mdejong
10  */
11 
12 namespace JSUPPORT {}
13 namespace JPP { using namespace JSUPPORT; }
14 
15 namespace JSUPPORT {
16 
20 
21 
22  /**
23  * ROOT TTree object output.
24  *
25  * This class implements the JLANG::JAccessibleObjectOutput interface.
26  */
27  template<class T>
28  class JTreeRecorder :
29  public JTreeWriterObjectOutput<T>,
30  public JAccessibleObjectOutput<T>,
31  public JRootOutputFile
32  {
33  public:
34  /**
35  * Default constructor.
36  */
38  {}
39 
40 
41  /**
42  * Constructor.
43  *
44  * \param file_name file name
45  */
46  JTreeRecorder(const char* file_name)
47  {
48  open(file_name);
49  }
50 
51 
52  /**
53  * Open file.
54  *
55  * \param file_name file name
56  */
57  virtual void open(const char* file_name)
58  {
59  JRootOutputFile::open(file_name);
60 
61  if (is_open()) {
62  this->SetDirectory(getFile());
63  }
64  }
65 
66 
67  /**
68  * Close file.
69  */
70  virtual void close()
71  {
72  // Write objects in memory.
73 
74  if (is_open()) {
75  getFile()->Write();
76  }
77 
78  // Detach TTree from TDirectory to avoid deletion of TTree.
79 
80  this->SetDirectory(0);
81 
82  // Close file.
83 
84  if (is_open()) {
85  getFile()->Close();
86  }
87 
88  reset();
89 
90  // Reset TTree.
91 
92  this->Reset();
93  }
94  };
95 }
96 
97 #endif
virtual void reset()
Reset pointer.
Definition: JStorage.hh:42
virtual void close()
Close file.
ROOT TTree object output.
JTreeRecorder(const char *file_name)
Constructor.
void SetDirectory(TDirectory *dir)
Set directory.
TFile * getFile() const
Get file.
Definition: JRootFile.hh:65
ROOT output file.
Definition: JRootFile.hh:147
virtual void open(const char *file_name)
Open file.
Definition: JRootFile.hh:177
virtual bool is_open() const =0
Check is device is open.
virtual void open(const char *file_name)
Open file.
Interface for object output with named access.
JTreeRecorder()
Default constructor.