Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JEvtWeightFactorProduct.hh
Go to the documentation of this file.
1#ifndef __JAANET__JEVTWEIGHTFACTORPRODUCT__
2#define __JAANET__JEVTWEIGHTFACTORPRODUCT__
3
4#include <vector>
5
7
8#include "JSystem/JStat.hh"
9
10#include "JLang/JToken.hh"
11#include "JLang/JClonable.hh"
12
13#include "Jeep/JPrint.hh"
14#include "Jeep/JProperties.hh"
15
17
19#include "JAAnet/JEvtWeightFactorDictionary.hh"
20
21
22/**
23 * \author bjung
24 */
25
26namespace JAANET {}
27namespace JPP { using namespace JAANET; }
28
29namespace JAANET {
30
31 using JLANG::JClonable;
32
34
36
37
38 /**
39 * Class for product of event-weight factors.
40 *
41 * Input syntax as follows:
42 * <pre>
43 * <oscprob_key>=<oscprob_value>[,<oscprob_key>=<oscprob_value>]...
44 *
45 * oscprob.<oscprob_key>=<oscprob_value>[,oscprob.<oscprob_key>=<oscprob_value>]...
46 *
47 * oscprob=<oscprob_file>
48 *
49 * <oscprob_file>
50 *
51 * <factor identifier> [<factor arguments> [& <factor identifier> [<factor arguments]]...];
52 * </pre>
53 * where
54 * - <oscprob_key> refers to an oscillation probability calculator configuration key;
55 * - <oscprob_value> to an oscillation probability calculator configuration value;
56 * - <oscprob_file> to an oscillation probability calculator configuration file;
57 * - <factor identifier> to an event-weight factor identifier (c.f. `JAANET::JEvtWeighFactorDictionary::factors`); and
58 * - <factor arguments> to the list of arguments for the corresponding event-weight factor
59 *
60 */
62 public std::vector<JEvtWeightFactorHelper>,
63 public JClonable<JEvtWeightFactor, JEvtWeightFactorProduct>
64 {
65 /**
66 * Default constructor.
67 */
71
72
73 /**
74 * Constructor.
75 *
76 * \param oscProb oscillation probability calculator
77 */
81
82
83 /**
84 * Constructor.
85 *
86 * \param oscProb oscillation probability calculator
87 * \param factor event-weight factor
88 */
90 const JEvtWeightFactorHelper& factor) :
92 {
93 this->push_back(factor);
94 }
95
96
97 /**
98 * Constructor.
99 *
100 * \param oscProb oscillation probability calculator
101 * \param factor event-weight factor
102 * \param args remaining event-weight factors
103 */
104 template<class ...Args>
106 const JEvtWeightFactorHelper& factor,
107 const Args& ...args) :
109 {
110 this->push_back(factor, args...);
111 }
112
113
114
115 /**
116 * Constructor.
117 *
118 * \param factor event-weight factor
119 */
121 oscProb()
122 {
123 this->push_back(factor);
124 }
125
126
127 /**
128 * Constructor.
129 *
130 * \param factor event-weight factor
131 * \param args remaining event-weight factors
132 */
133 template<class ...Args>
135 const Args& ...args) :
136 oscProb()
137 {
138 this->push_back(factor, args...);
139 }
140
141
142 /**
143 * Put event-weight factor.
144 *
145 * \param factor event-weight factor
146 */
148 {
149 const JEvtWeightFactorProduct* p = dynamic_cast<const JEvtWeightFactorProduct*>(factor.get());
150
151 if (p != NULL) {
152 this->insert(this->end(), p->begin(), p->end());
153 } else {
155 }
156 }
157
158
159 /**
160 * Put event-weight factor.
161 *
162 * \param factor event-weight factor
163 * \param args remaining event-weight factors
164 */
165 template<class ...Args>
167 const Args& ...args)
168 {
169 push_back(factor);
170 push_back(args...);
171 }
172
173
174 /**
175 * Join this product of event-weight factors with a given product of event-weight factors.
176 *
177 * \param product product of event-weight factors
178 * \return joined product of event-weight factors
179 */
181 {
182 this->insert(this->end(), product.begin(), product.end());
183
184 return (*this);
185 }
186
187
188 /**
189 * Get oscillation probability calculator.
190 *
191 * \return oscillation probability calculator
192 */
194 {
195 return oscProb;
196 }
197
198
199 /**
200 * Set oscillation probability calculator for all compatible event weight factors.
201 *
202 * \param oscProb oscillation probability calculator
203 * \return number of modified event weighters
204 */
206 {
207 using namespace JPP;
208
209 if (oscProb) {
210
211 size_t n = 0;
212
213 this->oscProb = oscProb;
214
215 for (iterator i = this->begin(); i != this->end(); ++i) {
216
217 JEvtWeightFactorHelper& factor = *i;
218
219 if (!factor) { continue; }
220
221 JOscFlux* p = dynamic_cast<JOscFlux*>(factor.get());
222
223 if (p != NULL) {
224
226 ++n;
227
228 } else {
229
230 JOscProbHelper* q = dynamic_cast<JOscProbHelper*>(factor.get());
231
232 if (q != NULL) {
233 *q = oscProb;
234 ++n;
235 }
236 }
237 }
238
239 return n;
240
241 } else {
242
243 THROW(JNullPointerException, "JEvtWeightFactorProduct::setOscProb(): Given oscillation probability function is invalid.");
244 }
245 }
246
247
248 /**
249 * Get multiplication factor of given event.
250 *
251 * \param evt event
252 * \return multiplication factor
253 */
254 double getFactor(const Evt& evt) const override final
255 {
256 using namespace std;
257
258 double factor = 1.0;
259
260 for (vector<JEvtWeightFactorHelper>::const_iterator i = this->cbegin(); i != this->cend(); ++i) {
261 factor *= i->getFactor(evt);
262 }
263
264 return factor;
265 }
266
267
268 /**
269 * Check whether this event-weight factor is valid.
270 *
271 * \return true if valid; else false
272 */
273 bool is_valid() const override final
274 {
275 for (const_iterator i = this->cbegin(); i != this->cend(); ++i) {
276 if (!(i->is_valid())) { return false; }
277 }
278
279 return true;
280 }
281
282
283 /**
284 * Get properties of this class.
285 *
286 * \param eqpars equation parameters
287 */
289 {
290 return JEvtWeightFactorProductHelper(*this, eqpars);
291 }
292
293
294 /**
295 * Get properties of this class.
296 *
297 * \param eqpars equation parameters
298 */
300 {
301 return JEvtWeightFactorProductHelper(*this, eqpars);
302 }
303
304
305 /**
306 * Read event-weight factor product from stream.
307 *
308 * \param in input stream
309 * \return input stream
310 */
311 std::istream& read(std::istream& in) override final
312 {
313 using namespace std;
314 using namespace JPP;
315
316 static const JEvtWeightFactorDictionary dictionary(oscProb);
317
318 streampos pos = in.tellg();
319
320 // Optional loading of data from file
321
322 std::string buffer;
323 in >> buffer;
324
325 if (getFileStatus(buffer.c_str())) {
326
327 ifstream ifs(buffer.c_str());
328
329 read(ifs);
330
331 ifs.close();
332
333 return in;
334
335 } else {
336
337 in.clear();
338 in.seekg(pos);
339 }
340
341 // Read optional oscillation probability calculator configuration
342
343 in >> oscProb;
344
345 if (fail(in)) {
346
347 in.clear();
348 in.seekg(pos);
349 }
350
351 // Read event weight factors
352
353 for (JToken<'&'> token; in >> token; ) {
354
355 int factorID = 0;
356
357 istringstream iss(token);
358
359 if (!(iss >> factorID)) { continue; }
360
361 const JEvtWeightFactorHelper helper(dictionary.at(factorID).getFactor());
362
363 JEvtWeightFactor& factor = helper.getFactor();
364
365 iss >> factor;
366
367 push_back(helper);
368 }
369
370 return in;
371 }
372
373
374 /**
375 * Write event-weight factor to output
376 *
377 * \param out output stream
378 * \return output stream
379 */
380 std::ostream& write(std::ostream& out) const override final
381 {
382 return out << getProperties(JEquationParameters("=", "\n", "./", "#"));
383 }
384
385
386 private:
387
388 /**
389 * Auxiliary class for I/O of product of event-weight factors.
390 */
392 public JProperties
393 {
394 /**
395 * Constructor.
396 *
397 * \param product product of event-weight factors
398 * \param eqpars equation parameters
399 */
400 template<class JEvtWeightFactorProduct_t>
401 JEvtWeightFactorProductHelper(JEvtWeightFactorProduct_t& product,
402 const JEquationParameters& eqpars) :
403 JProperties(eqpars, 1)
404 {
405 using namespace std;
406 using namespace JPP;
407
408 (*this)[JEvtWeightFactor::getTypeKey()] = "product";
409
410 JProperties factors(eqpars, 1);
411
412 for (typename JEvtWeightFactorProduct_t::const_iterator i = product.cbegin(); i != product.cend(); ++i) {
413
414 const int index = 1 + distance(product.cbegin(), i);
415
416 const string key = MAKE_STRING("factor" << index);
417
418 JProperties sub = i->getProperties(eqpars);
419
420 (*this)[key] = sub;
421 }
422 }
423 };
424
425 JOscProbHelper oscProb; //!< Oscillation probability calculator
426 };
427}
428
429#endif
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
I/O formatting auxiliaries.
#define MAKE_STRING(A)
Make string.
Definition JPrint.hh:63
Utility class to parse parameter values.
File status.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Utility class to parse parameter values.
Simple data structure to support I/O of equations (see class JLANG::JEquation).
Exception for null pointer operation.
Wrapper class around string.
Definition JToken.hh:26
Extensions to Evt data format.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition Evt.hh:21
Helper class for event-weight factor.
JEvtWeightFactor & getFactor() const
Get reference to event-weight factor.
Auxiliary class for I/O of product of event-weight factors.
JEvtWeightFactorProductHelper(JEvtWeightFactorProduct_t &product, const JEquationParameters &eqpars)
Constructor.
Class for product of event-weight factors.
void push_back(const JEvtWeightFactorHelper &factor)
Put event-weight factor.
std::istream & read(std::istream &in) override final
Read event-weight factor product from stream.
JEvtWeightFactorProduct(const JOscProbHelper &oscProb, const JEvtWeightFactorHelper &factor, const Args &...args)
Constructor.
const JOscProbHelper & getOscProb() const
Get oscillation probability calculator.
JEvtWeightFactorProduct(const JEvtWeightFactorHelper &factor)
Constructor.
JEvtWeightFactorProduct(const JOscProbHelper &oscProb)
Constructor.
size_t setOscProb(const JOscProbHelper &oscProb)
Set oscillation probability calculator for all compatible event weight factors.
bool is_valid() const override final
Check whether this event-weight factor is valid.
JProperties getProperties(const JEquationParameters &eqpars=JEvtWeightFactor::getEquationParameters()) const override final
Get properties of this class.
JEvtWeightFactorProduct(const JOscProbHelper &oscProb, const JEvtWeightFactorHelper &factor)
Constructor.
JProperties getProperties(const JEquationParameters &eqpars=JEvtWeightFactor::getEquationParameters()) override final
Get properties of this class.
JOscProbHelper oscProb
Oscillation probability calculator.
double getFactor(const Evt &evt) const override final
Get multiplication factor of given event.
JEvtWeightFactorProduct(const JEvtWeightFactorHelper &factor, const Args &...args)
Constructor.
void push_back(const JEvtWeightFactorHelper &factor, const Args &...args)
Put event-weight factor.
JEvtWeightFactorProduct & join(const JEvtWeightFactorProduct &product)
Join this product of event-weight factors with a given product of event-weight factors.
std::ostream & write(std::ostream &out) const override final
Write event-weight factor to output.
Abstract base class for specifiable event-weight factors.
static const char *const getTypeKey()
Get type keyword.
static JEquationParameters & getEquationParameters()
Get equation parameters.
Implementation of oscillated neutrino flux.
Definition JOscFlux.hh:44
void setOscProb(const JOscProbHelper oscProb)
Set oscillation probability calculator.
Definition JOscFlux.hh:76
Template class for object cloning.
Definition JClonable.hh:59
Helper class for oscillation probability calculators.