Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JMeta.hh
Go to the documentation of this file.
1#ifndef __JSUPPORT__JMETA__
2#define __JSUPPORT__JMETA__
3
4#include <string>
5#include <ostream>
6#include <map>
7
8#include "RVersion.h"
9#include "TFile.h"
10#include "TNamed.h"
11#include "TString.h"
12
13#include "JLang/JType.hh"
14#include "JLang/JException.hh"
15#include "JLang/JEquation.hh"
17#include "JLang/Jpp.hh"
18#include "JLang/JManip.hh"
20#include "JSystem/JUTSName.hh"
21#include "Jeep/JComment.hh"
22#include "Jeep/JeepToolkit.hh"
23
24
25/**
26 * \file
27 *
28 * ROOT I/O of application specific meta data.
29 * \author mdejong
30 */
31
32namespace JSUPPORT {}
33namespace JPP { using namespace JSUPPORT; }
34
35namespace JSUPPORT {
36
37 using JLANG::JType;
40 using JEEP::JComment_t;
41
42
43 /**
44 * ROOT name for meta data.
45 */
46 static const char* const META_NAME = "JMeta";
47
48
49 /**
50 * ROOT sub-directory for meta data.
51 */
52 static const char* const META_DIRECTORY = "META";
53
54
55 /**
56 * Definition of meta data parameters.
57 */
58 static const char* const application_t = "application"; //!< application name
59 static const char* const SVNrelease_t = "SVN"; //!< SVN release
60 static const char* const GITrelease_t = "GIT"; //!< GIT release
61 static const char* const ROOTrelease_t = "ROOT"; //!< ROOT release
62 static const char* const namespace_t = "namespace"; //!< name space
63 static const char* const command_t = "command"; //!< Linux command
64 static const char* const system_t = "system"; //!< system information
65
66
67 /**
68 * Auxiliary class for ROOT I/O of application specific meta data.
69 */
70 struct JMeta :
71 public std::map<std::string, std::string>
72 {
73 /**
74 * Default constructor.
75 */
77 {}
78
79
80 /**
81 * Constructor.
82 *
83 * \param argc number of command line arguments
84 * \param argv array of command line arguments
85 */
86 JMeta(const int argc, const char* const argv[])
87 {
88 using namespace std;
89 using namespace JPP;
90
91 if (argc > 0) {
92
93 (*this)[application_t] = getFilename(argv[0]);
94 (*this)[GITrelease_t] = getGITVersion();
95 (*this)[ROOTrelease_t] = ROOT_RELEASE;
96 (*this)[namespace_t] = getNamespace();
97 (*this)[command_t] = MAKE_STRING(getPath(PATH, argv[0]) << '/' << argv[0]);
98
99 for (int i = 1; i != argc; ++i) {
100 if (get_number_of_tokens(argv[i]) == 1)
101 (*this)[command_t] += MAKE_STRING(' ' << argv[i]);
102 else
103 (*this)[command_t] += MAKE_STRING(' ' << "\\\"" << argv[i] << "\\\"");
104 }
105
106 const JUTSName buffer;
107
108 (*this)[system_t] = MAKE_STRING(""
109 << buffer.sysname << ' '
110 << buffer.nodename << ' '
111 << buffer.release << ' '
112 << buffer.version << ' '
113 << buffer.machine);
114 }
115 }
116
117
118 /**
119 * Type conversion operator.
120 *
121 * \return comment
122 */
123 operator JComment_t() const
124 {
125 JComment_t comment;
126
127 for (const_iterator i = this->begin(); i != this->end(); ++i) {
128 comment.push_back(i->first + ' ' + i->second);
129 }
130
131 return comment;
132 }
133
134
135 /**
136 * Copy meta data.
137 *
138 * \param file_name name of input file
139 * \param out output file
140 */
141 static void copy(const char* const file_name, TFile& out);
142
143
144 /**
145 * Get equation parameters.
146 *
147 * \return equation parameters
148 */
150 {
151 static const JEquationParameters parameters("=", "\n", "", "");
152
153 return parameters;
154 }
155
156
157 /**
158 * Extract meta data.
159 *
160 * \param buffer meta data
161 * \return meta data
162 */
163 static JMeta valueOf(const std::string& buffer)
164 {
165 using namespace std;
166 using namespace JPP;
167
168 JMeta meta;
169
170 istringstream is(buffer);
171
173
174 for (JEquation equation; is >> equation; ) {
175 meta[equation.getKey()] = equation.getValue();
176 }
177
178 return meta;
179 }
180
181
182 /**
183 * Convert meta data to string.
184 *
185 * \return string
186 */
187 std::string toString() const
188 {
189 using namespace std;
190 using namespace JPP;
191
192 ostringstream os;
193
195
196 for (JMeta::const_iterator i = this->begin(); i != this->end(); ++i) {
197 os << JEquation::make_equation(i->first, i->second);
198 }
199
200 return os.str();
201 }
202
203
204 /**
205 * Write meta data to output.
206 *
207 * \param out output stream
208 * \param meta meta data
209 * \return output stream
210 */
211 friend inline std::ostream& operator<<(std::ostream& out, const JMeta& meta)
212 {
213 using namespace std;
214
215 const_iterator p = meta.find(application_t);
216
217 if (p != meta.end()) {
218 for (const_iterator i = meta.begin(); i != meta.end(); ++i) {
219 if (i != p) {
220 out << p->second << ' ' << i->second << endl;
221 }
222 }
223 }
224
225 return out;
226 }
227 };
228
229
230 /**
231 * Get ROOT name of given data type.
232 *
233 * \param type JMeta type
234 * \return name of object to be read
235 */
236 inline const char* getName(const JType<JMeta>& type)
237 {
238 return META_NAME;
239 }
240
241
242 /**
243 * Get number of cycles of named objects with same title as given key.
244 *
245 * \param file pointer to file
246 * \param key ROOT key (including possible directory)
247 * \return number of cycles
248 */
249 inline int getNumberOfCycles(TFile* file, const TString& key)
250 {
251 using namespace std;
252
253 int counter = 0;
254
255 if (file != NULL) {
256
257 TNamed* __p = NULL;
258
259 file->GetObject(key, __p);
260
261 if (__p != NULL) {
262
263 ++counter;
264
265 const TString title = __p->GetTitle();
266
267 string buffer(key);
268
269 const string::size_type pos = buffer.find(';');
270
271 if (pos != string::npos) {
272
273 int cycle = -1;
274
275 istringstream(buffer.substr(pos + 1)) >> cycle;
276
277 buffer = buffer.substr(0, pos);
278
279 for (int i = 1; i < cycle; ++i) {
280
281 file->GetObject(MAKE_CSTRING(buffer << ';' << i), __p);
282
283 if (__p != NULL && __p->GetTitle() == title) {
284 ++counter;
285 }
286 }
287 }
288 }
289 }
290
291 return counter;
292 }
293
294
295 /**
296 * Read object from ROOT file.
297 *
298 * \param file pointer to file
299 * \param key key of object to be read
300 * \param ps pointer to object
301 */
302 inline void getObject(TFile* file, const TString& key, JMeta*& ps)
303 {
304 using namespace std;
305 using namespace JPP;
306
307 if (ps != NULL) {
308
309 delete ps;
310
311 ps = NULL;
312 }
313
314 if (file != NULL) {
315
316 const int number_of_cycles = getNumberOfCycles(file, MAKE_CSTRING(META_DIRECTORY << '/' << key));
317
318 if (number_of_cycles != 0) {
319
320 TNamed* __p = NULL;
321
322 file->GetObject(MAKE_CSTRING(META_DIRECTORY << '/' << key), __p);
323
324 ps = new JMeta();
325
326 (*ps)[application_t] = __p->GetTitle();
327
328 file->GetObject(MAKE_CSTRING(META_DIRECTORY << '/' << (*ps)[application_t] << ';' << number_of_cycles), __p);
329
330 if (__p != NULL) {
331 *ps = JMeta::valueOf(__p->GetTitle());
332 }
333 }
334 }
335 }
336
337
338 /**
339 * Write meta data as ROOT TNamed object.
340 *
341 * \param dir directory
342 * \param key ROOT key
343 * \param buffer user data
344 * \return true if OK; else false
345 */
346 inline bool putMeta(TDirectory& dir, const std::string& key, const std::string& buffer)
347 {
348 static TNamed object;
349
350 if (dir.GetListOfKeys()->FindObject(META_DIRECTORY) == NULL) {
351 dir.mkdir(META_DIRECTORY);
352 }
353
354 TDirectory* p = dir.GetDirectory(META_DIRECTORY);
355
356 object.SetName (key.c_str()); // ROOT key
357 object.SetTitle(buffer.c_str()); // user data
358
359 return (p->WriteTObject(&object) > 0);
360 }
361
362
363 /**
364 * Write meta data to ROOT directory.
365 *
366 * \param dir directory
367 * \param meta meta data
368 * \return true if OK; else false
369 */
370 inline bool putObject(TDirectory& dir, const JMeta& meta)
371 {
372 JMeta::const_iterator p = meta.find(application_t);
373
374 if (p != meta.end()) {
375 if (putMeta(dir, getName(JType<JMeta>()), p->second)) {
376 return putMeta(dir, p->second, meta.toString());
377 }
378 }
379
380 return false;
381 }
382
383
384 /**
385 * Write meta data to ROOT directory.
386 *
387 * \param dir pointer to directory
388 * \param meta meta data
389 * \return true if OK; else false
390 */
391 inline bool putObject(TDirectory* dir, const JMeta& meta)
392 {
393 if (dir != NULL)
394 return putObject(*dir, meta);
395 else
396 return false;
397 }
398
399
400 /**
401 * Write meta data to ROOT file.
402 *
403 * \param file ROOT file
404 * \param meta meta data
405 * \return ROOT file
406 */
407 inline TFile& operator<<(TFile& file, const JMeta& meta)
408 {
409 putObject(file, meta);
410
411 return file;
412 }
413
414
415 /**
416 * Copy meta data.
417 *
418 * \param file_name name of input file
419 * \param out output file
420 */
421 void JMeta::copy(const char* const file_name, TFile& out)
422 {
423 using namespace JPP;
424
425 JRootFileReader<JMeta> in(file_name);
426
427 while (in.hasNext()) {
428
429 const JMeta* p = in.next();
430
431 putObject(out, *p);
432 }
433 }
434
435
436 /**
437 * Type definition of old meta data.
438 */
439 struct JMetaOld_t : public JMeta {};
440
441
442 /**
443 * Get ROOT name of given data type.
444 *
445 * \param type JMetaOld_t type
446 * \return name of object to be read
447 */
448 inline const char* getName(const JType<JMetaOld_t>& type)
449 {
450 return META_NAME;
451 }
452
453
454 /**
455 * Read object from ROOT file.
456 *
457 * \param file pointer to file
458 * \param key key of object to be read
459 * \param ps pointer to object
460 */
461 inline void getObject(TFile* file, const TString& key, JMetaOld_t*& ps)
462 {
463 enum JParameter_t {
464 JApplication_t = 0, //!< application name
465 JSVNRelease_t, //!< SVN release
466 JROOTRelease_t, //!< ROOT release
467 JNamespace_t, //!< name space
468 JCommand_t, //!< Linux command
469 JSystem_t, //!< system information
470 number_of_items //!< number of items
471 };
472
473 const char* buffer[number_of_items] = {
478 command_t,
480 };
481
482 if (ps != NULL) {
483
484 delete ps;
485
486 ps = NULL;
487 }
488
489 if (file != NULL) {
490
491 const int number_of_cycles = getNumberOfCycles(file, key);
492
493 if (number_of_cycles != 0) {
494
495 TNamed* __p = NULL;
496
497 file->GetObject(key, __p);
498
499 ps = new JMetaOld_t();
500
501 (*ps)[application_t] = __p->GetTitle();
502
503 for (int i = 1; i != number_of_items; ++i) {
504
505 file->GetObject(MAKE_CSTRING((*ps)[application_t] << ';' << ((number_of_cycles - 1) * (number_of_items - 1) + i)), __p);
506
507 if (__p != NULL)
508 (*ps)[buffer[i]] = __p->GetTitle();
509 else
510 break;
511 }
512 }
513 }
514 }
515
516
517 /**
518 * Write old meta data to ROOT directory in new format.
519 *
520 * \param dir directory
521 * \param meta meta data
522 * \return true if OK; else false
523 */
524 inline bool putObject(TDirectory& dir, const JMetaOld_t& meta)
525 {
526 return putObject(dir, static_cast<const JMeta&>(meta));
527 }
528}
529
530#endif
Exceptions.
I/O manipulators.
#define MAKE_CSTRING(A)
Make C-string.
Definition JPrint.hh:72
#define MAKE_STRING(A)
Make string.
Definition JPrint.hh:63
System information.
Auxiliary methods for handling file names, type names and environment.
Jpp environment information.
Auxiliary class for fit parameter with optional limits.
Definition JFitK40.hh:111
Facet class to specify parsing of equations in currect locale (see class JLANG::JEquation).
Simple data structure to support I/O of equations (see class JLANG::JEquation).
General purpose equation class.
Definition JEquation.hh:47
General exception.
Definition JException.hh:24
std::vector< std::string > JComment_t
Type definition of comment block.
Definition JComment.hh:31
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Support classes and methods for experiment specific I/O.
static const char *const SVNrelease_t
SVN release.
Definition JMeta.hh:59
bool putMeta(TDirectory &dir, const std::string &key, const std::string &buffer)
Write meta data as ROOT TNamed object.
Definition JMeta.hh:346
static const char *const command_t
Linux command.
Definition JMeta.hh:63
bool putObject(TDirectory &dir, const JMeta &meta)
Write meta data to ROOT directory.
Definition JMeta.hh:370
static const char *const META_NAME
ROOT name for meta data.
Definition JMeta.hh:46
void getObject(TFile *file, const TString &key, JMeta *&ps)
Read object from ROOT file.
Definition JMeta.hh:302
static const char *const GITrelease_t
GIT release.
Definition JMeta.hh:60
static const char *const META_DIRECTORY
ROOT sub-directory for meta data.
Definition JMeta.hh:52
static const char *const application_t
Definition of meta data parameters.
Definition JMeta.hh:58
const char * getName(const JType< JMeta > &type)
Get ROOT name of given data type.
Definition JMeta.hh:236
static const char *const namespace_t
name space
Definition JMeta.hh:62
int getNumberOfCycles(TFile *file, const TString &key)
Get number of cycles of named objects with same title as given key.
Definition JMeta.hh:249
TFile & operator<<(TFile &file, const JMeta &meta)
Write meta data to ROOT file.
Definition JMeta.hh:407
static const char *const ROOTrelease_t
ROOT release.
Definition JMeta.hh:61
static const char *const system_t
system information
Definition JMeta.hh:64
Auxiliary class for a type holder.
Definition JType.hh:19
Type definition of old meta data.
Definition JMeta.hh:439
Auxiliary class for ROOT I/O of application specific meta data.
Definition JMeta.hh:72
std::string toString() const
Convert meta data to string.
Definition JMeta.hh:187
JMeta()
Default constructor.
Definition JMeta.hh:76
friend std::ostream & operator<<(std::ostream &out, const JMeta &meta)
Write meta data to output.
Definition JMeta.hh:211
static JMeta valueOf(const std::string &buffer)
Extract meta data.
Definition JMeta.hh:163
static void copy(const char *const file_name, TFile &out)
Copy meta data.
Definition JMeta.hh:421
static const JEquationParameters & getEquationParameters()
Get equation parameters.
Definition JMeta.hh:149
JMeta(const int argc, const char *const argv[])
Constructor.
Definition JMeta.hh:86
Auxiliary class for operating system information.
Definition JUTSName.hh:23