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