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