Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JRootPrinter.hh
Go to the documentation of this file.
1#ifndef __JROOT__JROOTPRINTER__
2#define __JROOT__JROOTPRINTER__
3
4#include <string>
5#include <ostream>
6
7#pragma GCC diagnostic push
8#pragma GCC diagnostic ignored "-Wall"
9#include "TString.h"
10#include "TRegexp.h"
11#include "TFormula.h"
12#pragma GCC diagnostic pop
13
15#include "JLang/JException.hh"
16#include "JLang/JLangToolkit.hh"
17#include "JROOT/JRootClass.hh"
20
21/**
22 * \file
23 *
24 * Print objects in ASCII format using ROOT dictionary.
25 * \author mdejong
26 */
27namespace JROOT {}
28namespace JPP { using namespace JROOT; }
29
30namespace JROOT {
31
34
35 /**
36 * Auxiliary class for printing objects in ASCII format using JRootWriter.
37 */
38 struct JRootPrinter {
39 /**
40 * Get equation parameters.
41 *
42 * \return equation parameters
43 */
45 {
46 static JEquationParameters equation("=", "\n", ".", "");
47
48 return equation;
49 }
50
51
52 /**
53 * Set equation parameters.
54 *
55 * \param equation equation parameters
56 */
57 static inline void setEquationParameters(const JEquationParameters& equation)
58 {
59 getEquationParameters() = equation;
60 }
61
62
63 /**
64 * Write class contents to output.
65 *
66 * \param writer ROOT writer
67 * \param cls ROOT class
68 */
69 static inline void print(JRootWriter& writer, const JRootWritableClass& cls)
70 {
71 if (cls.is_valid()) {
72 if (writer.getDictionary().find(cls.getTypename()) != writer.getDictionary().end())
73 writer.putObject(cls);
74 else
75 writer.put(cls);
76 }
77 }
78
79
80 /**
81 * Write part of object to output using ROOT dictionary.
82 *
83 * \param out output stream
84 * \param object object
85 * \param key data member name, optionally separated by division character
86 * \param parameters equation parameters
87 * \param dictionary dictionary
88 */
89 template<class T>
90 static inline void print(std::ostream& out,
91 const T& object,
92 const std::string& key,
93 const JEquationParameters& parameters,
95 {
96 using namespace std;
97 using namespace JPP;
98
99 JRootWritableClass cls(object);
100
101 for (string::size_type il = 0, ir = 0; ir != string::npos && cls.is_valid(); il = ir + 1) {
102 ir = key.substr(il).find(parameters.getDefaultDivision());
103 cls = cls.find(trim(key.substr(il, ir - il)).c_str());
104 }
105
106 if (cls.is_valid()) {
107
108 JRootWriter writer(out, parameters, dictionary);
109
110 print(writer, cls);
111
112 } else {
113
114 THROW(JParseError, "Invalid key " << key);
115 }
116 }
117
118
119 /**
120 * Write object to output using ROOT dictionary.
121 *
122 * \param out output stream
123 * \param object object
124 * \param parameters equation parameters
125 * \param dictionary dictionary
126 */
127 template<class T>
128 static inline void print(std::ostream& out,
129 const T& object,
130 const JEquationParameters& parameters,
132 {
133 JRootWriter writer(out, parameters, dictionary);
134 JRootWritableClass cls(object);
135
136 print(writer, cls);
137 }
138
139
140 /**
141 * Write part of object to output using ROOT dictionary.
142 *
143 * \param out output stream
144 * \param object object
145 * \param key data member name, optionally separated by division character '.'
146 * \param dictionary dictionary
147 */
148 template<class T>
149 static inline void print(std::ostream& out,
150 const T& object,
151 const std::string& key,
153 {
154 print(out, object, key, JRootPrinter::getEquationParameters(), dictionary);
155 }
156
157
158 /**
159 * Write object to output using ROOT dictionary.
160 *
161 * \param out output stream
162 * \param object object
163 * \param dictionary dictionary
164 */
165 template<class T>
166 static inline void print(std::ostream& out,
167 const T& object,
169 {
170 print(out, object, JRootPrinter::getEquationParameters(), dictionary);
171 }
172 };
173
174
175 /**
176 * Get result of given textual formula.
177 *
178 * The formula may contain names of data members of the given object.
179 * For example:
180 * <pre>
181 * getResult("a / b.value", object, ..);
182 * </pre>
183 * In this, the corresponding data type should have (at least) data members <tt>a</tt> and <tt>b</tt>
184 * and <tt>b</tt> should have (at least) data member <tt>value</tt>.
185 *
186 * \param text text
187 * \param object object
188 * \param dictionary dictionary
189 * \return value
190 */
191 template<class T>
192 inline Double_t getResult(const TString& text,
193 const T& object,
195 {
196 using namespace std;
197 using namespace JPP;
198
199 const TRegexp DOUBLE("[0-9.][eE][+-0-9]");
200
201 string buffer = (const char*) text;
202
203 for (string::size_type pos = 0; pos != buffer.size(); ) {
204
205 if (isalpha(buffer[pos]) || buffer[pos] == '_') {
206
207 string::size_type len = 1;
208
209 while (pos + len != buffer.size() && (isalnum(buffer[pos+len]) || buffer[pos+len] == '_' || buffer[pos+len] == '.')) {
210 ++len;
211 }
212
213 if (len != 1 || pos == 0 || TString(buffer.substr(pos-1).c_str()).Index(DOUBLE) != 0) {
214
215 ostringstream os;
216
217 os.setf(ios::fixed);
218 os.precision(10);
219
220 JRootPrinter::print(os, object, buffer.substr(pos,len), dictionary);
221
222 buffer.replace(pos, len, os.str());
223
224 pos += os.str().size();
225
226 continue;
227 }
228 }
229
230 pos += 1;
231 }
232
233 return TFormula("/tmp", buffer.c_str()).Eval(0.0);
234 }
235}
236
237#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
ASCII I/O of objects with ROOT dictionary.
Simple data structure to support I/O of equations (see class JLANG::JEquation).
const char getDefaultDivision() const
Get default division character.
Exception for parsing value.
Implementation for ASCII output of objects with ROOT dictionary.
const JRootDictionary_t & getDictionary() const
Get dictictionary.
JRootWriter & putObject(const T &object)
Write object.
JRootWriter & put(const T &object)
Write object according equation format.
char text[TEXT_SIZE]
Definition elog.cc:72
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
Double_t getResult(const TString &text, const T &object, const JRootDictionary_t &dictionary=JRootDictionary::getInstance())
Get result of given textual formula.
static data_type & getInstance()
Get unique instance of template class.
Definition JSingleton.hh:27
bool is_valid() const
Check validity of this addressable class.
JRootAddressableClass find(const char *const name) const
Find addressable base class or data member with given name within current class.
const char * getTypename() const
Get type name.
Type definition of ROOT based dictionary for ASCII I/O.
Auxiliary class for printing objects in ASCII format using JRootWriter.
static void print(std::ostream &out, const T &object, const JEquationParameters &parameters, const JRootDictionary_t &dictionary=JRootDictionary::getInstance())
Write object to output using ROOT dictionary.
static void print(JRootWriter &writer, const JRootWritableClass &cls)
Write class contents to output.
static void print(std::ostream &out, const T &object, const std::string &key, const JEquationParameters &parameters, const JRootDictionary_t &dictionary=JRootDictionary::getInstance())
Write part of object to output using ROOT dictionary.
static void print(std::ostream &out, const T &object, const JRootDictionary_t &dictionary=JRootDictionary::getInstance())
Write object to output using ROOT dictionary.
static void print(std::ostream &out, const T &object, const std::string &key, const JRootDictionary_t &dictionary=JRootDictionary::getInstance())
Write part of object to output using ROOT dictionary.
static JEquationParameters & getEquationParameters()
Get equation parameters.
static void setEquationParameters(const JEquationParameters &equation)
Set equation parameters.
ROOT class for writing from object.