Jpp
JAutoTreeWriter.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JAUTOTREEWRITER__
2 #define __JSUPPORT__JAUTOTREEWRITER__
3 
4 #include "JTools/JAutoMap.hh"
5 #include "JROOT/JTreeWriter.hh"
6 #include "JROOT/JRootFile.hh"
7 
8 
9 /**
10  * \author mdejong
11  */
12 
13 namespace JSUPPORT {}
14 namespace JPP { using namespace JSUPPORT; }
15 
16 namespace JSUPPORT {
17 
18  using JTOOLS::JAutoMap;
21 
22 
23  /**
24  * Auxiliary class to copy input data to corresponding TTree.
25  *
26  * This class derives from JROOT::JRootOutputFile.
27  * It re-implements the methods open and close of the underlying JLANG::JAccessible class
28  * so that each TTree is properly attached to and detached from the corresponding TDirectory, respectively.
29  */
30  template<class JKey_t>
32  public JAutoMap<JKey_t, JTreeCopyWriterInterface*>,
33  public JRootOutputFile
34  {
35  public:
36 
39 
40  typedef typename map_type::const_iterator const_iterator;
41  typedef typename map_type::iterator iterator;
42  typedef typename map_type::const_reverse_iterator const_reverse_iterator;
43  typedef typename map_type::reverse_iterator reverse_iterator;
44 
46 
47 
48  /**
49  * Default constrtuctor.
50  */
52  {}
53 
54 
55  /**
56  * Constructor.
57  *
58  * \param type data type
59  */
60  template<class T>
62  {
63  this->insert<T>();
64  }
65 
66 
67  /**
68  * Get key.
69  *
70  * This method should be overloaded for the requested data types.
71  *
72  * \param type data type
73  * \return key
74  */
75  template<class T>
76  static JKey_t getKey(JType<T> type);
77 
78 
79  /**
80  * Insert (list of) data type(s).
81  */
82  template<class T>
83  void insert()
84  {
85  insert<T>(JTOOLS::JAutomate<JElement_t>());
86  }
87 
88 
89  /**
90  * Open file.
91  *
92  * \param file_name file name
93  */
94  virtual void open(const char* file_name)
95  {
96  JRootOutputFile::open(file_name);
97 
98  if (is_open()) {
99  for (iterator i = this->begin(); i != this->end(); ++i) {
100  i->second->SetDirectory(getFile());
101  }
102  }
103  }
104 
105 
106  /**
107  * Close file.
108  */
109  virtual void close()
110  {
111  // Write objects in memory.
112 
113  if (is_open()) {
114  getFile()->Write();
115  }
116 
117  // Detach TTree from TDirectory to avoid deletion of TTree.
118 
119  for (iterator i = this->begin(); i != this->end(); ++i) {
120  i->second->SetDirectory(0);
121  }
122 
123  // Close file.
124 
125  if (is_open()) {
126  getFile()->Close();
127  }
128 
129  reset();
130 
131  // Reset TTree for next run.
132 
133  for (iterator i = this->begin(); i != this->end(); ++i) {
134  i->second->Reset();
135  }
136  }
137 
138 
139  protected:
140  /**
141  * Auxiliary class for element insertion.
142  */
143  struct JElement_t :
144  public value_type
145  {
146  /**
147  * Constructor.
148  *
149  * \param type data type
150  */
151  template<class T>
154  {}
155  };
156  };
157 }
158 
159 #endif
JSUPPORT::JAutoTreeWriter::open
virtual void open(const char *file_name)
Open file.
Definition: JAutoTreeWriter.hh:94
JSUPPORT::JAutoTreeWriter
Auxiliary class to copy input data to corresponding TTree.
Definition: JAutoTreeWriter.hh:31
JLANG::JType
Auxiliary class for a type holder.
Definition: JType.hh:19
JSUPPORT::JAutoTreeWriter::JAutoTreeWriter
JAutoTreeWriter(JType< T > type)
Constructor.
Definition: JAutoTreeWriter.hh:61
JROOT::JRootOutputFile::open
virtual void open(const char *file_name)
Open file.
Definition: JRootFile.hh:177
JROOT
Auxiliary classes and methods for ROOT I/O.
Definition: JAbstractStreamer.hh:13
JSUPPORT::JAutoTreeWriter::reverse_iterator
map_type::reverse_iterator reverse_iterator
Definition: JAutoTreeWriter.hh:43
JROOT::JRootOutputFile
ROOT output file.
Definition: JRootFile.hh:147
JTOOLS::JAutoMap
Forward declaration of class JAutoMap for specialisation of class JAutomate.
Definition: JAutoMap.hh:29
JSUPPORT::JAutoTreeWriter::map_type
JAutoMap< JKey_t, JTreeCopyWriterInterface * >::map_type map_type
Definition: JAutoTreeWriter.hh:37
JROOT::JRootFile::getFile
TFile * getFile() const
Get file.
Definition: JRootFile.hh:65
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JROOT::JTreeCopyWriterInterface
Interface for template TTree writing and copying.
Definition: JTreeWriter.hh:90
JSUPPORT::JAutoTreeWriter::JElement_t
Auxiliary class for element insertion.
Definition: JAutoTreeWriter.hh:143
JSUPPORT::JAutoTreeWriter::close
virtual void close()
Close file.
Definition: JAutoTreeWriter.hh:109
JSUPPORT::JAutoTreeWriter::const_reverse_iterator
map_type::const_reverse_iterator const_reverse_iterator
Definition: JAutoTreeWriter.hh:42
JSUPPORT::JAutoTreeWriter::iterator
map_type::iterator iterator
Definition: JAutoTreeWriter.hh:41
JSUPPORT::JAutoTreeWriter::const_iterator
map_type::const_iterator const_iterator
Definition: JAutoTreeWriter.hh:40
JSUPPORT::JAutoTreeWriter::getKey
static JKey_t getKey(JType< T > type)
Get key.
JSUPPORT::JAutoTreeWriter::JElement_t::JElement_t
JElement_t(JType< T > type)
Constructor.
Definition: JAutoTreeWriter.hh:152
JTreeWriter.hh
JSUPPORT
Support classes and methods for experiment specific I/O.
Definition: JDataWriter.cc:38
JTOOLS::JAutomate
Auxiliary class for automatic element creation.
Definition: JAutoMap.hh:40
JSUPPORT::JAutoTreeWriter::value_type
JAutoMap< JKey_t, JTreeCopyWriterInterface * >::value_type value_type
Definition: JAutoTreeWriter.hh:38
JSUPPORT::JAutoTreeWriter::insert
void insert()
Insert (list of) data type(s).
Definition: JAutoTreeWriter.hh:83
JSUPPORT::JAutoTreeWriter::JAutoTreeWriter
JAutoTreeWriter()
Default constrtuctor.
Definition: JAutoTreeWriter.hh:51
JLANG::JStorage::reset
virtual void reset()
Reset pointer.
Definition: JStorage.hh:42
JROOT::JRootFile::is_open
virtual bool is_open() const
Check is file is open.
Definition: JRootFile.hh:76
JRootFile.hh
JROOT::getTreeCopyWriter
JTreeCopyWriter< T > & getTreeCopyWriter()
Get the TTree writer and copy for this type of object.
Definition: JTreeWriter.hh:168
JAutoMap.hh