Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JManager.hh
Go to the documentation of this file.
1 #ifndef __JROOT__JMANAGER__
2 #define __JROOT__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 #include "JLang/JManip.hh"
16 
17 #include "JROOT/JRootToolkit.hh"
18 
19 
20 /**
21  * \file
22  *
23  * Dynamic ROOT object management.
24  * \author mdejong
25  */
26 namespace JROOT {}
27 namespace JPP { using namespace JROOT; }
28 
29 namespace JROOT {
30 
31  using JLANG::JException;
32  using JLANG::JPointer;
33 
34 
35  /**
36  * Auxiliary class to manage set of compatible ROOT objects (e.g.\ histograms) using unique keys.\n
37  * For each use of the map operator <tt>[]</tt>, it is checked whether the corresponding client object exists.\n
38  * If the corresponding client object does not yet exists, a clone is made of the master object (i.e.\ the 'client').\n
39  * The name of the newly created object is derived from the name of the master object by replacing
40  * the wild card character with the value of the key according the optional format specifier.
41  */
42  template<class JKey_t, class JValue_t>
43  class JManager :
44  public JPointer<JValue_t>,
45  public std::map<JKey_t, JPointer<JValue_t>>
46  {
47  public:
48 
51 
52  /**
53  * Default constructor.
54  */
56  {}
57 
58 
59  /**
60  * Constructor.
61  *
62  * Note that the manager owns the master object pointed to.\n
63  * For saving the objects to file, use method JManager::Write
64  * (all objects are detached from the current ROOT directory).
65  *
66  * \param master master object
67  * \param wildcard wild card character
68  * \param format format specifier for replacement of wildcard character by value of key
69  */
70  JManager(JValue_t* master,
71  char wildcard = '%',
72  JFormat_t format = JFormat_t()) :
73  ptr_type(master),
74  map_type(),
75  wc (wildcard),
76  fmt(format)
77  {
78  using namespace std;
79  using namespace JPP;
80 
81  if (!this->is_valid()) {
82  THROW(JException, "No valid master");
83  }
84 
85  if (string(this->get()->GetName()).find(wc) == string::npos) {
86  THROW(JException, "Invalid wildcard <" << this->get()->GetName() << "> '" << wc << "'");
87  }
88 
89  resetObject(this->get(), true);
90  }
91 
92 
93  /**
94  * Constructor.
95  *
96  * Note that the manager owns the master object pointed to.\n
97  * For saving the objects to file, use method JManager::Write
98  * (all objects are detached from the current ROOT directory).
99  *
100  * \param master master object
101  * \param name name of master
102  * \param wildcard wild card character
103  * \param format format specifier for replacement of wildcard character by value of key
104  */
105  JManager(JValue_t* master,
106  const std::string& name,
107  char wildcard = '%',
108  std::ios::fmtflags format = std::ios::fmtflags()) :
109  ptr_type(master),
110  map_type(),
111  wc (wildcard),
112  fmt(format)
113  {
114  using namespace std;
115  using namespace JPP;
116 
117  if (!this->is_valid()) {
118  THROW(JException, "No valid master");
119  }
120 
121  this->get()->SetName(name.c_str());
122 
123  if (string(this->get()->GetName()).find(wc) == string::npos) {
124  THROW(JException, "Invalid wildcard <" << this->get()->GetName() << "> '" << wc << "'");
125  }
126 
127  resetObject(this->get(), true);
128  }
129 
130 
131  /**
132  * Copy constructor.
133  *
134  * Note that the master object of the given manager is cloned and the wildcard copied.\n
135  * The client objects are not copied.
136  *
137  * \param manager manager
138  */
139  JManager(const JManager& manager) :
140  ptr_type(NULL),
141  map_type(),
142  wc (manager.wc),
143  fmt(manager.fmt)
144  {
145  using namespace JPP;
146 
147  if (manager.is_valid()) {
148 
149  this->set((JValue_t*) manager.get()->Clone());
150 
151  resetObject(this->get(), true);
152  }
153  }
154 
155 
156  /**
157  * Clear client objects.
158  */
159  void clear()
160  {
161  for (typename map_type::iterator i = this->begin(); i != this->end(); ++i) {
162  i->second.reset();
163  }
164 
165  this->clear();
166  }
167 
168 
169  /**
170  * Get pointer to object for given key.
171  *
172  * \param key key
173  * \return pointer to client object
174  */
175  JValue_t* operator[](JKey_t key)
176  {
177  using namespace std;
178  using namespace JPP;
179 
180  typename map_type::iterator i = this->find(key);
181 
182  if (i == this->end()) {
183 
184  string buffer = this->get()->GetName();
185  string::size_type ipos = buffer.find(wc);
186 
187  ostringstream os;
188 
189  fmt.put(os);
190 
191  os << key;
192 
193  buffer.replace(ipos, 1, os.str());
194 
195  JValue_t* p = (JValue_t*) this->get()->Clone(buffer.c_str());
196 
197  resetObject(p, true);
198 
199  this->insert(make_pair(key,p));
200 
201  return p;
202 
203  } else {
204 
205  return i->second;
206  }
207  }
208 
209 
210  /**
211  * Read objects from file into manager.
212  *
213  * \param in input file or directory
214  * \param master master name
215  * \param wildcard wild card character
216  * \return manager
217  */
218  static JManager Read(TDirectory& in, const char* const master, const char wildcard)
219  {
220  using namespace std;
221  using namespace JPP;
222 
223  JManager manager;
224 
225  TString buffer = master;
226 
227  const Ssiz_t pos = buffer.Index(wildcard);
228 
229  const TString a = buffer(0, pos);
230  const TString b = buffer(pos+1, buffer.Length());
231 
232  buffer.ReplaceAll("[", "\\[");
233  buffer.ReplaceAll("]", "\\]");
234 
235  buffer.ReplaceAll(wildcard, ".*");
236 
237  const TRegexp regexp(buffer);
238 
239  TIter iter(in.GetListOfKeys());
240 
241  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
242 
243  const TString tag(key->GetName());
244 
245  // option match
246 
247  if (tag.Index(regexp) != -1) {
248 
249  JValue_t* p = dynamic_cast<JValue_t*>(key->ReadObj());
250 
251  if (p != NULL) {
252 
253  resetObject(p, false);
254 
255  string buffer = p->GetName();
256 
257  if (a.Length() != 0) { buffer.replace(buffer.find(a), a.Length(), ""); }
258  if (b.Length() != 0) { buffer.replace(buffer.find(b), b.Length(), ""); }
259 
260  JKey_t key;
261 
262  istringstream(buffer) >> key;
263 
264  manager.insert(make_pair(key, p));
265  }
266  }
267  }
268 
269  return manager;
270  }
271 
272 
273  /**
274  * Read objects from file.
275  *
276  * \param in input file or directory
277  */
278  void Read(TDirectory& in)
279  {
280  if (this->is_valid()) {
281 
282  JManager buffer = Read(in, this->get()->GetName(), this->wc);
283 
284  this->swap(buffer);
285  }
286  }
287 
288 
289  /**
290  * Write objects to file.
291  *
292  * \param out output file or directory
293  * \param wm write master
294  */
295  void Write(TDirectory& out, const bool wm = false)
296  {
297  for (typename map_type::iterator i = this->begin(); i != this->end(); ++i) {
298  out.WriteTObject(i->second);
299  }
300 
301  if (wm) {
302  out.WriteTObject(this->get());
303  }
304  }
305 
306 
307  /**
308  * Write objects to file.
309  *
310  * \param file_name file name
311  * \param wm write master
312  */
313  void Write(const char* file_name, const bool wm = false)
314  {
315  TFile out(file_name, "RECREATE");
316 
317  this->Write(out, wm) ;
318 
319  out.Write();
320  out.Close();
321  }
322 
323 
324  /**
325  * Read manager from file.
326  *
327  * \param file file
328  * \param object manager
329  * \return file
330  */
331  inline friend TFile& operator>>(TFile& file, JManager& object)
332  {
333  object.Read(file);
334 
335  return file;
336  }
337 
338 
339  /**
340  * Write manager to file.
341  *
342  * \param file file
343  * \param object manager
344  * \return file
345  */
346  inline friend TFile& operator<<(TFile& file, JManager& object)
347  {
348  object.Write(file);
349 
350  return file;
351  }
352 
353  char wc;
355  };
356 
357 
358  /**
359  * Reset JManager object
360  *
361  * \param object manager
362  * \param reset reset contents if true
363  * \return true if successful; else false
364  */
365  template<class JKey_t, class JValue_t>
366  inline bool resetObject(JManager<JKey_t, JValue_t>* object, const bool reset = false)
367  {
368  if (object->is_valid()) {
369  JROOT::resetObject(object->get(), reset);
370  }
371 
372  for (typename JManager<JKey_t, JValue_t>::iterator i = object->begin(); i != object->end(); ++i) {
373  JROOT::resetObject(i->second.get(), reset);
374  }
375 
376  return true;
377  }
378 }
379 
380 #endif
General exception.
Definition: JException.hh:23
void put(std::ostream &out) const
Put format specificaton to given output stream.
Definition: JManip.hh:597
Exceptions.
void clear()
Clear client objects.
Definition: JManager.hh:159
JManager(JValue_t *master, char wildcard= '%', JFormat_t format=JFormat_t())
Constructor.
Definition: JManager.hh:70
JPointer< JValue_t > ptr_type
Definition: JManager.hh:49
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
then echo Test string reversed by master(hit< return > to continue)." JProcess -c "JEcho" -rC fi if (( 1 ))
void Write(const char *file_name, const bool wm=false)
Write objects to file.
Definition: JManager.hh:313
then usage $script< string identifier >< detectorfile > input file(toashort file)+" "\nNote that the input files and toashort files should be one-to-one related." fi if (( $
std::map< JKey_t, ptr_type > map_type
Definition: JManager.hh:50
JManager(JValue_t *master, const std::string &name, char wildcard= '%', std::ios::fmtflags format=std::ios::fmtflags())
Constructor.
Definition: JManager.hh:105
bool is_valid() const
Check validity of pointer.
bool resetObject(JManager< JKey_t, JValue_t > *object, const bool reset=false)
Reset JManager object.
Definition: JManager.hh:366
Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys...
Definition: JManager.hh:43
static JManager Read(TDirectory &in, const char *const master, const char wildcard)
Read objects from file into manager.
Definition: JManager.hh:218
JManager(const JManager &manager)
Copy constructor.
Definition: JManager.hh:139
Template implementation of class that holds pointer to object(s).
Definition: JPointer.hh:22
void Write(TDirectory &out, const bool wm=false)
Write objects to file.
Definition: JManager.hh:295
void Read(TDirectory &in)
Read objects from file.
Definition: JManager.hh:278
I/O manipulators.
then echo n User name
Definition: JCookie.sh:45
JFormat_t fmt
Definition: JManager.hh:354
then JCalibrateToT a
Definition: JTuneHV.sh:116
JValue_t * operator[](JKey_t key)
Get pointer to object for given key.
Definition: JManager.hh:175
JManager()
Default constructor.
Definition: JManager.hh:55
friend TFile & operator>>(TFile &file, JManager &object)
Read manager from file.
Definition: JManager.hh:331
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:38
Data structure for format specifications.
Definition: JManip.hh:522
virtual void set(JClass_t *p) override
Set pointer.
Definition: JPointer.hh:75
virtual JClass_t * get() const override
Get pointer.
Definition: JPointer.hh:64
friend TFile & operator<<(TFile &file, JManager &object)
Write manager to file.
Definition: JManager.hh:346
virtual void reset() override
Reset pointer.
Definition: JPointer.hh:84