Jpp  15.0.1-rc.2-highQE
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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) override
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() override
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>
153  value_type(getKey(type), &JROOT::getTreeCopyWriter<T>())
154  {}
155  };
156  };
157 }
158 
159 #endif
map_type::reverse_iterator reverse_iterator
Auxiliary class to copy input data to corresponding TTree.
virtual void reset() override
Reset pointer.
Definition: JStorage.hh:42
Auxiliary class for automatic element creation.
Definition: JAutoMap.hh:46
Auxiliary class for a type holder.
Definition: JType.hh:19
virtual bool is_open() const override
Check is file is open.
Definition: JRootFile.hh:76
JAutoMap< JKey_t, JTreeCopyWriterInterface * >::map_type map_type
Auxiliary class for element insertion.
static JKey_t getKey(JType< T > type)
Get key.
TFile * getFile() const
Get file.
Definition: JRootFile.hh:65
Interface for template TTree writing and copying.
Definition: JTreeWriter.hh:90
JElement_t(JType< T > type)
Constructor.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
map_type::iterator iterator
map_type::const_iterator const_iterator
virtual void close() override
Close file.
ROOT output file.
Definition: JRootFile.hh:147
virtual void open(const char *file_name) override
Open file.
void insert()
Insert (list of) data type(s).
map_type::const_reverse_iterator const_reverse_iterator
Wrapper class around std::map for automatic insertion of elements based on data types.
Definition: JAutoMap.hh:31
JAutoTreeWriter(JType< T > type)
Constructor.
JTreeCopyWriter< T > & getTreeCopyWriter()
Get the TTree writer and copy for this type of object.
Definition: JTreeWriter.hh:168
virtual void open(const char *file_name) override
Open file.
Definition: JRootFile.hh:177
TTree writing for template data type.
JAutoTreeWriter()
Default constrtuctor.
JAutoMap< JKey_t, JTreeCopyWriterInterface * >::value_type value_type