Jpp 21.0.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 "JLang/JToken.hh"
9#include "JLang/JClonable.hh"
11
12#include "Jeep/JPrint.hh"
13#include "Jeep/JProperties.hh"
14
16
19
20
21/**
22 * \author bjung
23 */
24
25namespace JAANET {}
26namespace JPP { using namespace JAANET; }
27
28namespace JAANET {
29
30 using JLANG::JClonable;
31
33
35
36
37 /**
38 * Class for product of event-weight factors.
39 *
40 * The input syntax follows the following scheme:
41 *
42 * <tt>[oscillation probabilities configuration] <factor>+</tt>,
43 *
44 * in which:
45 * - <tt>oscillation probabilities configuration</tt> refers to an optional specification
46 * of the oscillation parameters, which can be given according to one of the following formats:
47 * 1. <tt><oscprob_file></tt>,
48 * 2. <tt>oscprob=<oscprob_file></tt>,
49 * 3. <tt><oscprob_key>=<oscprob_value>[<end of line> <oscprob_key>=<oscprob_value>]+</tt> or
50 * 4. <tt>oscprob.<oscprob_key>=<oscprob_value>[<end of line> oscprob.<oscprob_key>=<oscprob_value>]+</tt>,
51 * where:
52 * - <tt>oscprob_key</tt> refers to an oscillation probability calculator configuration key;
53 * - <tt>oscprob_value</tt> to an oscillation probability calculator configuration value and
54 * - <tt>oscprob_file</tt> to an oscillation probability calculator configuration file with key-value pairs,
55 * - <tt>factor</tt> refers to a weighting factor, which needs to be specified according to the following format:
56 * 1. <tt><factor identifier> [<factor arguments> [ & <factor identifier> [<factor arguments] ]+ ];</tt>,
57 * where:
58 * - <tt>factor identifier</tt> refers to an event-weight factor identifier (c.f. `JAANET::JEvtWeighFactorDictionary::factors`); and
59 * - <tt>factor arguments</tt> to the list of arguments for the corresponding event-weight factor,
60 * - multiple weighting factors can be combined into one when specified as a group separated by the '&' character.
61 */
63 public std::vector<JEvtWeightFactorHelper>,
64 public JClonable<JEvtWeightFactor, JEvtWeightFactorProduct>
65 {
66 /**
67 * Default constructor.
68 */
72
73
74 /**
75 * Constructor.
76 *
77 * \param oscProb oscillation probability calculator
78 */
82
83
84 /**
85 * Constructor.
86 *
87 * \param oscProb oscillation probability calculator
88 * \param factor event-weight factor
89 */
91 const JEvtWeightFactorHelper& factor) :
93 {
94 this->push_back(factor);
95 }
96
97
98 /**
99 * Constructor.
100 *
101 * \param oscProb oscillation probability calculator
102 * \param factor event-weight factor
103 * \param args remaining event-weight factors
104 */
105 template<class ...Args>
107 const JEvtWeightFactorHelper& factor,
108 const Args& ...args) :
110 {
111 this->push_back(factor, args...);
112 }
113
114
115
116 /**
117 * Constructor.
118 *
119 * \param factor event-weight factor
120 */
122 oscProb()
123 {
124 this->push_back(factor);
125 }
126
127
128 /**
129 * Constructor.
130 *
131 * \param factor event-weight factor
132 * \param args remaining event-weight factors
133 */
134 template<class ...Args>
136 const Args& ...args) :
137 oscProb()
138 {
139 this->push_back(factor, args...);
140 }
141
142
143 /**
144 * Put event-weight factor.
145 *
146 * \param factor event-weight factor
147 */
149 {
150 const JEvtWeightFactorProduct* p = dynamic_cast<const JEvtWeightFactorProduct*>(factor.get());
151
152 if (p != NULL) {
153 this->insert(this->end(), p->begin(), p->end());
154 } else {
156 }
157 }
158
159
160 /**
161 * Put event-weight factor.
162 *
163 * \param factor event-weight factor
164 * \param args remaining event-weight factors
165 */
166 template<class ...Args>
168 const Args& ...args)
169 {
170 push_back(factor);
171 push_back(args...);
172 }
173
174
175 /**
176 * Join this product of event-weight factors with a given product of event-weight factors.
177 *
178 * \param product product of event-weight factors
179 * \return joined product of event-weight factors
180 */
182 {
183 this->insert(this->end(), product.begin(), product.end());
184
185 return (*this);
186 }
187
188
189 /**
190 * Get oscillation probability calculator.
191 *
192 * \return oscillation probability calculator
193 */
195 {
196 return oscProb;
197 }
198
199
200 /**
201 * Set oscillation probability calculator for all compatible event weight factors.
202 *
203 * \param oscProb oscillation probability calculator
204 * \return number of modified event weighters
205 */
207 {
208 using namespace JPP;
209
210 if (oscProb) {
211
212 size_t n = 0;
213
214 this->oscProb = oscProb;
215
216 for (iterator i = this->begin(); i != this->end(); ++i) {
217
218 JEvtWeightFactorHelper& factor = *i;
219
220 if (!factor) { continue; }
221
222 JOscFlux* p = dynamic_cast<JOscFlux*>(factor.get());
223
224 if (p != NULL) {
225
227 ++n;
228
229 } else {
230
231 JOscProbHelper* q = dynamic_cast<JOscProbHelper*>(factor.get());
232
233 if (q != NULL) {
234 *q = oscProb;
235 ++n;
236 }
237 }
238 }
239
240 return n;
241
242 } else {
243
244 THROW(JNullPointerException, "JEvtWeightFactorProduct::setOscProb(): Given oscillation probability function is invalid.");
245 }
246 }
247
248
249 /**
250 * Get multiplication factor of given event.
251 *
252 * \param evt event
253 * \return multiplication factor
254 */
255 double getFactor(const Evt& evt) const override final
256 {
257 using namespace std;
258
259 double factor = 1.0;
260
261 for (vector<JEvtWeightFactorHelper>::const_iterator i = this->cbegin(); i != this->cend(); ++i) {
262 factor *= i->getFactor(evt);
263 }
264
265 return factor;
266 }
267
268
269 /**
270 * Check whether this event-weight factor is valid.
271 *
272 * \return true if valid; else false
273 */
274 bool is_valid() const override final
275 {
276 for (const_iterator i = this->cbegin(); i != this->cend(); ++i) {
277 if (!(i->is_valid())) { return false; }
278 }
279
280 return true;
281 }
282
283
284 /**
285 * Get properties of this class.
286 *
287 * \param eqpars equation parameters
288 */
290 {
291 return JEvtWeightFactorProductHelper(*this, eqpars);
292 }
293
294
295 /**
296 * Get properties of this class.
297 *
298 * \param eqpars equation parameters
299 */
301 {
302 return JEvtWeightFactorProductHelper(*this, eqpars);
303 }
304
305
306 /**
307 * Read event-weight factor product from stream.
308 *
309 * \param in input stream
310 * \return input stream
311 */
312 std::istream& read(std::istream& in) override final
313 {
314 using namespace std;
315 using namespace JPP;
316
317 static const JEvtWeightFactorDictionary dictionary(oscProb);
318
319 JStringStream is(in);
320
321 // Read event weight factors
322
323 for (JToken<'&'> token; is >> token; ) {
324
325 int factorID = 0;
326
327 istringstream iss(token);
328
329 const streampos poss = iss.tellg();
330
331 if (!(iss >> factorID)) { // Check if token corresponds to oscillation parameter set
332
333 iss.clear();
334 iss.seekg(poss);
335
336 iss >> oscProb;
337
338 if (fail(iss)) {
339 continue;
340 }
341 }
342
343 const JEvtWeightFactorHelper helper = dictionary[factorID];
344
345 JEvtWeightFactor& factor = helper.getFactor();
346
347 iss >> factor;
348
349 push_back(helper);
350 }
351
352 return in;
353 }
354
355
356 /**
357 * Write event-weight factor to output
358 *
359 * \param out output stream
360 * \return output stream
361 */
362 std::ostream& write(std::ostream& out) const override final
363 {
364 return out << getProperties(JEquationParameters("=", "\n", "./", "#"));
365 }
366
367
368 private:
369
370 /**
371 * Auxiliary class for I/O of product of event-weight factors.
372 */
374 public JProperties
375 {
376 /**
377 * Constructor.
378 *
379 * \param product product of event-weight factors
380 * \param eqpars equation parameters
381 */
382 template<class JEvtWeightFactorProduct_t>
383 JEvtWeightFactorProductHelper(JEvtWeightFactorProduct_t& product,
384 const JEquationParameters& eqpars) :
385 JProperties(eqpars, 1)
386 {
387 using namespace std;
388 using namespace JPP;
389
390 (*this)[JEvtWeightFactor::getTypeKey()] = "product";
391
392 JProperties factors(eqpars, 1);
393
394 for (typename JEvtWeightFactorProduct_t::const_iterator i = product.cbegin(); i != product.cend(); ++i) {
395
396 const int index = 1 + distance(product.cbegin(), i);
397
398 const string key = MAKE_STRING("factor" << index);
399
400 JProperties sub = i->getProperties(eqpars);
401
402 (*this)[key] = sub;
403 }
404 }
405 };
406
407 JOscProbHelper oscProb; //!< Oscillation probability calculator
408 };
409}
410
411#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:48
Utility class to parse parameter values.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Dictionary to map distinct event-weight factors to unique identifiers.
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 STL stringstream class to facilitate optional loading of data from file.
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.