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