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 
11 #include "JLang/JException.hh"
12 #include "JLang/JPointer.hh"
13 
14 
15 /**
16  * \file
17  *
18  * Dynamic ROOT object management.
19  * \author mdejong
20  */
21 namespace JGIZMO {}
22 namespace JPP { using namespace JGIZMO; }
23 
24 namespace JGIZMO {
25 
26  using JLANG::JException;
27  using JLANG::JPointer;
28 
29 
30  /**
31  * Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys.
32  * For each use of the map operator <tt>[]</tt>, it is checked whether the corresponding client object exists.
33  * If the corresponding client object does not yet exists, a clone is made of the master object (i.e. the 'client').
34  * The name of the newly created object is derived from the name of the master object by replacing
35  * the wild card character with the value of the key according the optional format specifier.
36  */
37  template<class JKey_t, class JValue_t = TObject>
38  class JManager :
39  public JPointer<JValue_t>,
40  public std::map<JKey_t, JValue_t*>
41  {
42  public:
43  /**
44  * Constructor.
45  *
46  * Note that the manager owns the master object pointed to.
47  * For saving the objects to file, use method Write
48  * (all objects are detached from the current ROOT directory).
49  *
50  * \param master master object
51  * \param wildcard wild card character
52  * \param format format specifier for replacement of wildcard character by value of key
53  */
54  JManager(JValue_t* master,
55  char wildcard = '%',
56  std::ios::fmtflags format = std::ios::fmtflags()) :
57  JPointer<JValue_t>(master),
58  std::map<JKey_t, JValue_t*>(),
59  wc (wildcard),
60  fmt(format)
61  {
62  using namespace std;
63  using namespace JLANG;
64 
65  if (master == NULL) {
66  THROW(JException, "JManager: No master object.");
67  }
68 
69  master->SetDirectory(0);
70 
71  if (string(master->GetName()).find(wc) == string::npos) {
72  THROW(JException, "JManager: Missing wildcard " << master->GetName() << ' ' << wc);
73  }
74  }
75 
76 
77  /**
78  * Get pointer to object for given key.
79  *
80  * \param key key
81  * \return pointer to client object
82  */
83  JValue_t* operator[](JKey_t key)
84  {
85  using namespace std;
86 
87  typename map<JKey_t, JValue_t*>::iterator i = this->find(key);
88 
89  if (i == this->end()) {
90 
91  string buffer = this->get()->GetName();
92  string::size_type ipos = buffer.find(wc);
93 
94  ostringstream os;
95 
96  os.setf(fmt);
97 
98  os << key;
99 
100  buffer.replace(ipos, 1, os.str());
101 
102  JValue_t* p = (JValue_t*) this->get()->Clone(buffer.c_str());
103 
104  p->Reset();
105  p->SetDirectory(0);
106 
107  this->insert(make_pair(key,p));
108 
109  return p;
110 
111  } else {
112 
113  return i->second;
114  }
115  }
116 
117 
118  /**
119  * Write objects to file.
120  *
121  * \param out output file or directory
122  * \param wm write master
123  */
124  void Write(TDirectory& out, const bool wm = false)
125  {
126  for (typename std::map<JKey_t, JValue_t*>::iterator i = this->begin(); i != this->end(); ++i) {
127  out.WriteTObject(i->second);
128  }
129 
130  if (wm) {
131  out.WriteTObject(this->get());
132  }
133  }
134 
135 
136  /**
137  * Write objects to file.
138  *
139  * \param file_name file name
140  * \param wm write master
141  */
142  void Write(const char* file_name, const bool wm = false)
143  {
144  TFile out(file_name, "RECREATE");
145 
146  this->Write(out, wm) ;
147 
148  out.Write();
149  out.Close();
150  }
151 
152 
153  /**
154  * Write manager to file.
155  *
156  * \param file file
157  * \param object manager
158  * \return file
159  */
160  inline friend TFile& operator<<(TFile& file, JManager& object)
161  {
162  object.Write(file);
163 
164  return file;
165  }
166 
167  protected:
168  char wc;
169  std::ios::fmtflags fmt;
170  };
171 }
172 
173 #endif
General exception.
Definition: JException.hh:40
Exceptions.
friend TFile & operator<<(TFile &file, JManager &object)
Write manager to file.
Definition: JManager.hh:160
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
std::ios::fmtflags fmt
Definition: JManager.hh:169
JManager(JValue_t *master, char wildcard= '%', std::ios::fmtflags format=std::ios::fmtflags())
Constructor.
Definition: JManager.hh:54
Auxiliary class to manage set of compatible ROOT objects (e.g.
Definition: JManager.hh:38
void Write(TDirectory &out, const bool wm=false)
Write objects to file.
Definition: JManager.hh:124
Template implementation of class that holds pointer to object(s).
Definition: JPointer.hh:22
JValue_t * operator[](JKey_t key)
Get pointer to object for given key.
Definition: JManager.hh:83
void Write(const char *file_name, const bool wm=false)
Write objects to file.
Definition: JManager.hh:142