Jpp 19.3.0-rc.4
the software that should make you happy
Loading...
Searching...
No Matches
JEvtWeightFactorTFormula.hh
Go to the documentation of this file.
1#ifndef __JAANET__JEVTWEIGHTFACTORTFORMULA__
2#define __JAANET__JEVTWEIGHTFACTORTFORMULA__
3
4#include <string>
5
6#include "JLang/JException.hh"
8
9#include "Jeep/JProperties.hh"
10
12
13#pragma GCC diagnostic push
14#pragma GCC diagnostic ignored "-Wall"
15#include "TFormula.h"
16#pragma GCC diagnostic pop
17
18
19/**
20 * \author bjung
21 */
22
23namespace JAANET {}
24namespace JPP { using namespace JAANET; }
25
26namespace JAANET {
27
30
32
33
34 /**
35 * Base class implementation for reweighting factor for simulated events according to a specifiable ROOT TFormula.
36 */
38 public TFormula,
39 public JEvtWeightFactor
40 {
41 public:
42
43 /**
44 * Default constructor.
45 */
47 buffer(*this)
48 {}
49
50
51 /**
52 * Virtual destructor.
53 */
56
57
58 /**
59 * Get formula keyword.
60 *
61 * \return formula keyword
62 */
63 static const std::string getFormulaKey()
64 {
65 return "formula";
66 }
67
68
69 /**
70 * Compile given formula.
71 *
72 * \param formula formula
73 */
74 void load(const char* const formula)
75 {
76 this->Clear();
77
78 if (this->Compile(formula)) {
79 THROW(JValueOutOfRange, "JEvtWeightFactorTFormula::load(): Could not compile formula: " << formula);
80 }
81 }
82
83
84 /**
85 * Compile currently stored formula.
86 */
87 void load()
88 {
89 if (!buffer.empty()) {
90 load(buffer.c_str());
91 }
92 }
93
94
95 /**
96 * Check whether this event-weight factor is valid.
97 *
98 * \return true if valid; else false
99 */
100 bool is_valid() const override
101 {
102 return this->IsValid();
103 }
104
105
106 /**
107 * Get properties of this class.
108 *
109 * \param eqpars equation parameters
110 */
112 {
113 return JEvtWeightFactorTFormulaHelper(*this, eqpars);
114 }
115
116
117 /**
118 * Get properties of this class.
119 *
120 * \param eqpars equation parameters
121 */
123 {
124 return JEvtWeightFactorTFormulaHelper(*this, eqpars);
125 }
126
127
128 /**
129 * Read event-weight factor from input.
130 *
131 * \param in input stream
132 * \return input stream
133 */
134 std::istream& read(std::istream& in) override final
135 {
136 using namespace std;
137
138 this->Clear();
139
140 const streampos pos = in.tellg();
141 const ios_base::iostate state = in.rdstate();
142
143 JProperties properties = getProperties();
144 in >> properties;
145
146 this->load();
147
148 in.clear(state);
149 in.seekg(pos); // Rewind to read parameters
150
151 properties = getProperties();
152
153 in >> properties;
154
156
157 return in;
158 }
159
160
161 private:
162
163 /**
164 * Auxiliary class for I/O of TFormula expression.
165 */
167 public std::string
168 {
169 /**
170 * Read event category from input.
171 *
172 * \param in input stream
173 * \param object event category
174 * \return input stream
175 */
176 friend inline std::istream& operator>>(std::istream& in, JEvtWeightFactorTFormulaExpression& object)
177 {
178 if (in.peek() == (int) FORMULA_DELIMITER) {
179
180 in.ignore();
181
182 return std::getline(in, object, FORMULA_DELIMITER);
183
184 } else {
185
186 return in >> static_cast<std::string&>(object);
187 }
188 }
189
190
191 /**
192 * Write event category to output.
193 *
194 * \param out output stream
195 * \param object event category
196 * \return output stream
197 */
198 friend inline std::ostream& operator<<(std::ostream& out, const JEvtWeightFactorTFormulaExpression& object)
199 {
200 return out << static_cast<const std::string&>(object);
201 }
202
203
204 private:
205
206 static const char FORMULA_DELIMITER = '"'; //!< Formula string delimiter
207 };
208
209
210 /**
211 * Auxiliary class for I/O of TFormula-based event-weight factor.
212 */
214 public JProperties
215 {
216 /**
217 * Constructor.
218 *
219 * \param formula TFormula-based event-weight factor
220 * \param eqpars equation parameters
221 */
222 template<class JEvtWeightFactorTFormula_t>
223 JEvtWeightFactorTFormulaHelper(JEvtWeightFactorTFormula_t& formula,
224 const JEquationParameters& eqpars) :
225 JProperties(eqpars, 1)
226 {
227 using namespace std;
228
229 (*this)[JEvtWeightFactor::getTypeKey()] = "TFormula";
230 (*this)[getFormulaKey()] = formula.buffer;
231
232 Double_t* parameters = formula.GetParameters();
233
234 for (int i = 0; i < formula.GetNpar(); ++i) {
235
236 const string parname = formula.GetParName(i);
237
238 (*this)[parname] = parameters[i];
239 }
240 }
241 };
242
243
246 };
247}
248
249#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Utility class to parse parameter values.
Base class implementation for reweighting factor for simulated events according to a specifiable ROOT...
void load()
Compile currently stored formula.
JProperties getProperties(const JEquationParameters &eqpars=JEvtWeightFactor::getEquationParameters()) override final
Get properties of this class.
void load(const char *const formula)
Compile given formula.
JLoadProperty< JEvtWeightFactorTFormula, JEvtWeightFactorTFormulaExpression > buffer
Formula expression.
JProperties getProperties(const JEquationParameters &eqpars=JEvtWeightFactor::getEquationParameters()) const override final
Get properties of this class.
static const std::string getFormulaKey()
Get formula keyword.
bool is_valid() const override
Check whether this event-weight factor is valid.
std::istream & read(std::istream &in) override final
Read event-weight factor from input.
Utility class to parse parameter values.
Simple data structure to support I/O of equations (see class JLANG::JEquation).
Data structure for object properties which require reloading whenever the property is reread.
Exception for accessing a value in a collection that is outside of its range.
Extensions to Evt data format.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
friend std::istream & operator>>(std::istream &in, JEvtWeightFactorTFormulaExpression &object)
Read event category from input.
friend std::ostream & operator<<(std::ostream &out, const JEvtWeightFactorTFormulaExpression &object)
Write event category to output.
Auxiliary class for I/O of TFormula-based event-weight factor.
JEvtWeightFactorTFormulaHelper(JEvtWeightFactorTFormula_t &formula, const JEquationParameters &eqpars)
Constructor.
Abstract base class for specifiable event-weight factors.
static const char *const getTypeKey()
Get type keyword.
void check_validity() const
Check validity of this event-weight factor.
static JEquationParameters & getEquationParameters()
Get equation parameters.