Jpp test-rotations-old
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 streampos pos = in.tellg();
141
142 JProperties properties = getProperties();
143 in >> properties;
144
145 this->load();
146
147 in.clear(); // Rewind to read parameters
148 in.seekg(pos);
149
150 properties = getProperties();
151
152 in >> properties;
153
155
156 return in;
157 }
158
159
160 private:
161
162 /**
163 * Auxiliary class for I/O of TFormula expression.
164 */
166 public std::string
167 {
168 /**
169 * Read event category from input.
170 *
171 * \param in input stream
172 * \param object event category
173 * \return input stream
174 */
175 friend inline std::istream& operator>>(std::istream& in, JEvtWeightFactorTFormulaExpression& object)
176 {
177 if (in.peek() == (int) FORMULA_DELIMITER) {
178
179 in.ignore();
180
181 return std::getline(in, object, FORMULA_DELIMITER);
182
183 } else {
184
185 return in >> static_cast<std::string&>(object);
186 }
187 }
188
189
190 /**
191 * Write event category to output.
192 *
193 * \param out output stream
194 * \param object event category
195 * \return output stream
196 */
197 friend inline std::ostream& operator<<(std::ostream& out, const JEvtWeightFactorTFormulaExpression& object)
198 {
199 return out << static_cast<const std::string&>(object);
200 }
201
202
203 private:
204
205 static const char FORMULA_DELIMITER = '"'; //!< Formula string delimiter
206 };
207
208
209 /**
210 * Auxiliary class for I/O of TFormula-based event-weight factor.
211 */
213 public JProperties
214 {
215 /**
216 * Constructor.
217 *
218 * \param formula TFormula-based event-weight factor
219 * \param eqpars equation parameters
220 */
221 template<class JEvtWeightFactorTFormula_t>
222 JEvtWeightFactorTFormulaHelper(JEvtWeightFactorTFormula_t& formula,
223 const JEquationParameters& eqpars) :
224 JProperties(eqpars, 1)
225 {
226 using namespace std;
227
228 (*this)[JEvtWeightFactor::getTypeKey()] = "TFormula";
229 (*this)[getFormulaKey()] = formula.buffer;
230
231 Double_t* parameters = formula.GetParameters();
232
233 for (int i = 0; i < formula.GetNpar(); ++i) {
234
235 const string parname = formula.GetParName(i);
236
237 (*this)[parname] = parameters[i];
238 }
239 }
240 };
241
242
245 };
246}
247
248#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.