Jpp
JManager.hh
Go to the documentation of this file.
1 #ifndef __JGIZMO__JMANAGER__
2 #define __JGIZMO__JMANAGER__
3 
4 #include <string>
5 #include <sstream>
6 #include <map>
7 
8 #include "TObject.h"
9 #include "TFile.h"
10 #include "TRegexp.h"
11 #include "TKey.h"
12 
13 #include "JLang/JException.hh"
14 #include "JLang/JPointer.hh"
15 
16 
17 /**
18  * \file
19  *
20  * Dynamic ROOT object management.
21  * \author mdejong
22  */
23 namespace JGIZMO {}
24 namespace JPP { using namespace JGIZMO; }
25 
26 namespace JGIZMO {
27 
28  using JLANG::JException;
29  using JLANG::JPointer;
30 
31 
32  /**
33  * Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys.
34  * For each use of the map operator <tt>[]</tt>, it is checked whether the corresponding client object exists.
35  * If the corresponding client object does not yet exists, a clone is made of the master object (i.e. the 'client').
36  * The name of the newly created object is derived from the name of the master object by replacing
37  * the wild card character with the value of the key according the optional format specifier.
38  */
39  template<class JKey_t, class JValue_t = TObject>
40  class JManager :
41  public JPointer<JValue_t>,
42  public std::map<JKey_t, JPointer<JValue_t> >
43  {
44  public:
45 
47 
48  /**
49  * Default constructor.
50  */
52  {}
53 
54 
55  /**
56  * Constructor.
57  *
58  * Note that the manager owns the master object pointed to.
59  * For saving the objects to file, use method Write
60  * (all objects are detached from the current ROOT directory).
61  *
62  * \param master master object
63  * \param wildcard wild card character
64  * \param format format specifier for replacement of wildcard character by value of key
65  */
66  JManager(JValue_t* master,
67  char wildcard = '%',
68  std::ios::fmtflags format = std::ios::fmtflags()) :
69  JPointer<JValue_t>(master),
70  map_type(),
71  wc (wildcard),
72  fmt(format)
73  {
74  using namespace std;
75  using namespace JPP;
76 
77  if (master == NULL) {
78  THROW(JException, "JManager: No master object.");
79  }
80 
81  master->SetDirectory(0);
82 
83  if (string(master->GetName()).find(wc) == string::npos) {
84  THROW(JException, "JManager: Missing wildcard " << master->GetName() << ' ' << wc);
85  }
86  }
87 
88 
89  /**
90  * Copy constructor.
91  *
92  * Note that the master object of the given manager is cloned and the wildcard copied.
93  * The client objects are not copied.
94  *
95  * \param manager manager
96  */
97  JManager(const JManager& manager) :
98  JPointer<JValue_t>(NULL),
99  map_type(),
100  wc (manager.wc),
101  fmt(manager.fmt)
102  {
103  using namespace std;
104  using namespace JPP;
105 
106  if (manager.is_valid()) {
107 
108  this->set((JValue_t*) manager.get()->Clone());
109 
110  this->get()->Reset();
111  this->get()->SetDirectory(0);
112 
113  if (string(this->get()->GetName()).find(wc) == string::npos) {
114  THROW(JException, "JManager: Missing wildcard " << this->get()->GetName() << ' ' << wc);
115  }
116  }
117  }
118 
119 
120  /**
121  * Clear client objects.
122  */
123  void clear()
124  {
125  for (typename map_type::iterator i = this->begin(); i != this->end(); ++i) {
126  i->second.reset();
127  }
128 
129  static_cast<map_type&>(*this).clear();
130  }
131 
132 
133  /**
134  * Get pointer to object for given key.
135  *
136  * \param key key
137  * \return pointer to client object
138  */
139  JValue_t* operator[](JKey_t key)
140  {
141  using namespace std;
142 
143  typename map_type::iterator i = this->find(key);
144 
145  if (i == this->end()) {
146 
147  string buffer = this->get()->GetName();
148  string::size_type ipos = buffer.find(wc);
149 
150  ostringstream os;
151 
152  os.setf(fmt);
153 
154  os << key;
155 
156  buffer.replace(ipos, 1, os.str());
157 
158  JValue_t* p = (JValue_t*) this->get()->Clone(buffer.c_str());
159 
160  p->Reset();
161  p->SetDirectory(0);
162 
163  this->insert(make_pair(key,p));
164 
165  return p;
166 
167  } else {
168 
169  return i->second;
170  }
171  }
172 
173 
174  /**
175  * Read objects from file into manager.
176  *
177  * \param in input file or directory
178  * \param master master name
179  * \param wildcard wild card character
180  * \return manager
181  */
182  static JManager Read(TDirectory& in, const char* const master, const char wildcard)
183  {
184  using namespace std;
185 
186  JManager manager;
187 
188  TString buffer = master;
189 
190  const Ssiz_t pos = buffer.Index(wildcard);
191 
192  const TString a = buffer(0, pos);
193  const TString b = buffer(pos+1, buffer.Length());
194 
195  buffer.ReplaceAll("[", "\\[");
196  buffer.ReplaceAll("]", "\\]");
197 
198  buffer.ReplaceAll(wildcard, ".*");
199 
200  const TRegexp regexp(buffer);
201 
202  TIter iter(in.GetListOfKeys());
203 
204  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
205 
206  const TString tag(key->GetName());
207 
208  // option match
209 
210  if (tag.Index(regexp) != -1) {
211 
212  JValue_t* p = dynamic_cast<JValue_t*>(key->ReadObj());
213 
214  if (p != NULL) {
215 
216  p->SetDirectory(0);
217 
218  string buffer = p->GetName();
219 
220  if (a.Length() != 0) { buffer.replace(buffer.find(a), a.Length(), ""); }
221  if (b.Length() != 0) { buffer.replace(buffer.find(b), b.Length(), ""); }
222 
223  JKey_t key;
224 
225  istringstream(buffer) >> key;
226 
227  manager.insert(make_pair(key, p));
228  }
229  }
230  }
231 
232  return manager;
233  }
234 
235 
236  /**
237  * Read objects from file.
238  *
239  * \param in input file or directory
240  */
241  void Read(TDirectory& in)
242  {
243  if (this->is_valid()) {
244 
245  JManager buffer = Read(in, this->get()->GetName(), this->wc);
246 
247  this->swap(buffer);
248  }
249  }
250 
251 
252  /**
253  * Write objects to file.
254  *
255  * \param out output file or directory
256  * \param wm write master
257  */
258  void Write(TDirectory& out, const bool wm = false)
259  {
260  for (typename map_type::iterator i = this->begin(); i != this->end(); ++i) {
261  out.WriteTObject(i->second);
262  }
263 
264  if (wm) {
265  out.WriteTObject(this->get());
266  }
267  }
268 
269 
270  /**
271  * Write objects to file.
272  *
273  * \param file_name file name
274  * \param wm write master
275  */
276  void Write(const char* file_name, const bool wm = false)
277  {
278  TFile out(file_name, "RECREATE");
279 
280  this->Write(out, wm) ;
281 
282  out.Write();
283  out.Close();
284  }
285 
286 
287  /**
288  * Read manager from file.
289  *
290  * \param file file
291  * \param object manager
292  * \return file
293  */
294  inline friend TFile& operator>>(TFile& file, JManager& object)
295  {
296  object.Read(file);
297 
298  return file;
299  }
300 
301 
302  /**
303  * Write manager to file.
304  *
305  * \param file file
306  * \param object manager
307  * \return file
308  */
309  inline friend TFile& operator<<(TFile& file, JManager& object)
310  {
311  object.Write(file);
312 
313  return file;
314  }
315 
316  char wc;
317  std::ios::fmtflags fmt;
318  };
319 }
320 
321 #endif
JException.hh
JLANG::JPointer::set
virtual void set(JClass_t *p)
Set pointer.
Definition: JPointer.hh:75
JGIZMO::JManager::clear
void clear()
Clear client objects.
Definition: JManager.hh:123
JGIZMO::JManager::Read
void Read(TDirectory &in)
Read objects from file.
Definition: JManager.hh:241
JGIZMO::JManager::wc
char wc
Definition: JManager.hh:316
JLANG::JPointer::get
virtual JClass_t * get() const
Get pointer.
Definition: JPointer.hh:64
JGIZMO::JManager::operator[]
JValue_t * operator[](JKey_t key)
Get pointer to object for given key.
Definition: JManager.hh:139
JGIZMO::JManager::operator>>
friend TFile & operator>>(TFile &file, JManager &object)
Read manager from file.
Definition: JManager.hh:294
JGIZMO::JManager
Auxiliary class to manage set of compatible ROOT objects (e.g.
Definition: JManager.hh:40
JGIZMO::JManager::JManager
JManager(const JManager &manager)
Copy constructor.
Definition: JManager.hh:97
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JLANG::JPointer
Template implementation of class that holds pointer to object(s).
Definition: JPointer.hh:22
JGIZMO::JManager::JManager
JManager()
Default constructor.
Definition: JManager.hh:51
JGIZMO::JManager::fmt
std::ios::fmtflags fmt
Definition: JManager.hh:317
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:669
JGIZMO::JManager::JManager
JManager(JValue_t *master, char wildcard='%', std::ios::fmtflags format=std::ios::fmtflags())
Constructor.
Definition: JManager.hh:66
std::map
Definition: JSTDTypes.hh:16
JGIZMO::JManager::Write
void Write(TDirectory &out, const bool wm=false)
Write objects to file.
Definition: JManager.hh:258
JGIZMO::JManager::Write
void Write(const char *file_name, const bool wm=false)
Write objects to file.
Definition: JManager.hh:276
JGIZMO
Auxiliary applications for use of ROOT and more.
Definition: JCanvas.hh:15
std
Definition: jaanetDictionary.h:36
JGIZMO::JManager::map_type
std::map< JKey_t, JPointer< JValue_t > > map_type
Definition: JManager.hh:46
JPointer.hh
JGIZMO::JManager::Read
static JManager Read(TDirectory &in, const char *const master, const char wildcard)
Read objects from file into manager.
Definition: JManager.hh:182
JGIZMO::JManager::operator<<
friend TFile & operator<<(TFile &file, JManager &object)
Write manager to file.
Definition: JManager.hh:309
JLANG::JException
General exception.
Definition: JException.hh:40
JLANG::JAbstractPointer::is_valid
bool is_valid() const
Check validity of pointer.
Definition: JAbstractPointer.hh:83