Jpp master_rocky-44-g75b7c4f75
the software that should make you happy
Loading...
Searching...
No Matches
JEvtCategoryMap.hh
Go to the documentation of this file.
1#ifndef __JAANET__JEVTCATEGORYMAP__
2#define __JAANET__JEVTCATEGORYMAP__
3
4#include <map>
5#include <vector>
6
7#include "JSystem/JStat.hh"
8
9#include "JLang/JToken.hh"
10#include "JLang/JClonable.hh"
11#include "JLang/JException.hh"
13
14#include "Jeep/JPrint.hh"
15#include "Jeep/JProperties.hh"
16
22
26
27#include "JAAnet/JFlux.hh"
28#include "JAAnet/JFluxHelper.hh"
29#include "JAAnet/JFluxDictionary.hh"
30
31
32/**
33 * \author bjjung
34 */
35
36namespace JAANET {}
37namespace JPP { using namespace JAANET; }
38
39namespace JAANET {
40
41 using JLANG::JClonable;
42
44
45
46 /**
47 * Auxiliary class for reading a map of event categories.
48 *
49 * The template argument corresponds to the mapped value type.
50 */
51 template<class T>
53 public std::map<JEvtCategoryHelper, T>
54 {
55 public:
56
58
60
61 typedef typename map_type::iterator iterator;
62 typedef typename map_type::const_iterator const_iterator;
63
64
65 /**
66 * Default constructor.
67 */
69 map_type()
70 {}
71
72
73 /**
74 * Constructor.
75 *
76 * \param category event category
77 * \param value value
78 */
80 const T& value) :
81 map_type()
82 {
83 insert(category, value);
84 }
85
86
87
88 /**
89 * Constructor.
90 *
91 * \param category event category
92 * \param value value
93 * \param args remaining pairs of event categories and values
94 */
95 template<class ...Args>
97 const T& value,
98 const Args& ...args) :
99 map_type()
100 {
101 insert(category, value, args...);
102 }
103
104
105 /**
106 * Insert pair of event category and value.
107 *
108 * \param category event category
109 * \param value value
110 */
111 void insert(const JEvtCategoryHelper& category,
112 const T& value)
113 {
114 map_type::insert(std::make_pair(category, value));
115 }
116
117
118 /**
119 * Insert pairs of event categories and values.
120 *
121 * \param category event category
122 * \param value value
123 * \param args remaining pairs of event categories and values
124 */
125 template<class ...Args>
126 void insert(const JEvtCategoryHelper& category,
127 const T& value,
128 const Args& ...args)
129 {
130 insert(category, value);
131 insert(args...);
132 }
133 };
134
135
136 /**
137 * Template specialisation for a map between event categories and event-weight factor products.
138 *
139 * Input syntax as follows:
140 * <pre>
141 * <PDG type> [ & <PDG type>]...; <factor identifier> [<factor arguments> [& <factor identifier> [<factor arguments]]...];
142 * <key>=<value>[,<key>=<value>]... [ & <key>=<value>[,<key>=<value>]...]...; <factor identifier> [<factor arguments> [& <factor identifier> [<factor arguments]]...];
143 * </pre>
144 * where
145 * - <PDG type> refers to a PDG identifier;
146 * - <key> to one of the data members of a data structure derived from `JAANET::JEvtCategory`;
147 * - <value> to the corresponding value;
148 * - <factor identifier> to an event-weight factor identifier (c.f. `JAANET::JEvtWeighFactorDictionary::factors`); and
149 * - <factor arguments> to the list of arguments for the corresponding event-weight factor.
150 * Multiple event categories and flux factors can be specified, separated by the '&' character.
151 */
152 template<>
154 public std::map<JEvtCategoryHelper, JEvtWeightFactorProduct>,
155 public JClonable<JEvtWeightFactor, JEvtCategoryMap<JEvtWeightFactorProduct> >
156 {
157 public:
158
160
162
163 typedef typename map_type::iterator iterator;
164 typedef typename map_type::const_iterator const_iterator;
165
166
167 /**
168 * Default constructor.
169 */
171 map_type()
172 {}
173
174
175 /**
176 * Constructor.
177 *
178 * \param oscProb oscillation probability calculator
179 */
181 map_type(),
182 oscProb(oscProb)
183 {}
184
185
186 /**
187 * Constructor.
188 *
189 * \param oscProb oscillation probability calculator
190 * \param category event category
191 * \param factor event-weight factor
192 */
194 const JEvtCategoryHelper& category,
195 const JEvtWeightFactorHelper& factor) :
196 map_type(),
197 oscProb(oscProb)
198 {
199 insert(category, factor);
200 }
201
202
203
204 /**
205 * Constructor.
206 *
207 * \param oscProb oscillation probability calculator
208 * \param category event category
209 * \param factor event-weight factor
210 * \param args remaining pairs of event categories and event-weight factors
211 */
212 template<class ...Args>
214 const JEvtCategoryHelper& category,
215 const JEvtWeightFactorHelper& factor,
216 const Args& ...args) :
217 map_type(),
218 oscProb(oscProb)
219 {
220 insert(category, factor, args...);
221 }
222
223
224 /**
225 * Constructor.
226 *
227 * \param category event category
228 * \param factor event-weight factor
229 */
231 const JEvtWeightFactorHelper& factor) :
232 map_type(),
233 oscProb()
234 {
235 insert(category, factor);
236 }
237
238
239
240 /**
241 * Constructor.
242 *
243 * \param category event category
244 * \param factor event-weight factor
245 * \param args remaining pairs of event categories and event-weight factors
246 */
247 template<class ...Args>
249 const JEvtWeightFactorHelper& factor,
250 const Args& ...args) :
251 map_type(),
252 oscProb()
253 {
254 insert(category, factor, args...);
255 }
256
257
258 /**
259 * Insert pair of an event category and a product of event-weight factors.
260 *
261 * \param category pointer to event category
262 * \param product product of event-weight factors
263 */
264 void insert(const JEvtCategoryHelper& category,
265 const JEvtWeightFactorProduct& product)
266 {
267 iterator i = this->find(category);
268
269 if (i != this->end()) {
270 i->second.join(product);
271 } else {
272 map_type::insert(std::make_pair(category, product));
273 }
274 }
275
276
277 /**
278 * Insert event category.
279 *
280 * \param category event category
281 * \param factor event-weight factor
282 */
283 void insert(const JEvtCategoryHelper& category,
284 const JEvtWeightFactorHelper& factor)
285 {
286 iterator i = this->find(category);
287
288 if (i == this->end()) {
289
290 const JEvtWeightFactorProduct product(oscProb, factor);
291
292 map_type::insert(std::make_pair(category, product));
293
294 } else {
295
296 i->second.push_back(factor);
297 }
298 }
299
300
301 /**
302 * Insert pairs of event categories and event-weight factors.
303 *
304 * \param category event category
305 * \param factor event-weight factor
306 * \param args remaining pairs of event categories and event-weight factors
307 */
308 template<class ...Args>
309 void insert(const JEvtCategoryHelper& category,
310 const JEvtWeightFactorHelper& factor,
311 const Args& ...args)
312 {
313 insert(category, factor);
314 insert(args...);
315 }
316
317
318 /**
319 * Join this map between event categories and weight factors with a given map between event categories and weight factors.
320 *
321 * \param object map between event categories and fluxes
322 * \return joined map between event categories and fluxes
323 */
325 {
326 for (const_iterator i = object.cbegin(); i != object.cend(); ++i) {
327
328 iterator j = this->find(i->first);
329
330 if (j != this->end()) {
331 j->second.join(i->second);
332 } else {
333 this->insert(j->first, j->second);
334 }
335 }
336
337 return (*this);
338 }
339
340
341 /**
342 * Get weight factor of given event.
343 *
344 * \param evt event
345 * \return weight-factor for given event
346 */
347 double getFactor(const Evt& evt) const override final
348 {
349 using namespace JPP;
350
351 for (typename map_type::const_iterator i = this->cbegin(); i != this->cend(); ++i) {
352 if (i->first.match(evt)) {
353 return i->second.getFactor(evt);
354 }
355 }
356
357 THROW(JValueOutOfRange, "JEvtCategoryMap<JEvtWeightFactorHelper>::getFactor(): No event-weight factor for given event.");
358 }
359
360
361 /**
362 * Check whether this event-weight factor is valid.
363 *
364 * \return true if valid; else false
365 */
366 bool is_valid() const override final
367 {
368 for (typename map_type::const_iterator i = this->cbegin(); i != this->cend(); ++i) {
369 if (!i->second.is_valid()) { return false; }
370 }
371
372 return true;
373 }
374
375
376 /**
377 * Get properties of this class.
378 *
379 * \param eqpars equation parameters
380 */
381 JProperties getProperties(const JEquationParameters& eqpars) override final
382 {
383 return JEvtCategoryMapHelper(*this, eqpars);
384 }
385
386
387 /**
388 * Get properties of this class.
389 *
390 * \param eqpars equation parameters
391 */
392 JProperties getProperties(const JEquationParameters& eqpars) const override final
393 {
394 return JEvtCategoryMapHelper(*this, eqpars);
395 }
396
397
398 /**
399 * Read map between event categories and event-weight factors
400 *
401 * \param in input stream
402 * \return input stream
403 */
404 std::istream& read(std::istream& in) override final
405 {
406 using namespace std;
407 using namespace JPP;
408
409 JStringStream is(in);
410
411 if (getFileStatus(is.str().c_str())) {
412 is.load();
413 }
414
415 for (JToken<';'> token1, token2; is >> token1 >> token2; ) {
416
417 istringstream iss1(token1); // Read event categories
418
419 JEvtCategorySet categories;
420
421 iss1 >> categories;
422
423 istringstream iss2(token2); // Read event-weight factors
424
425 JEvtWeightFactorProduct factors(oscProb);
426
427 iss2 >> factors;
428
429 if (!fail(iss2)) {
430
431 for (JEvtCategorySet::const_iterator i = categories.cbegin(); i != categories.cend(); ++i) {
432 this->insert(*i, factors);
433 }
434 }
435 }
436
437 return in;
438 }
439
440
441 /**
442 * Write event-weight factor to output
443 *
444 * \param out output stream
445 * \return output stream
446 */
447 std::ostream& write(std::ostream& out) const override final
448 {
449 return out << getProperties(JEquationParameters("=", "\n", "./", "#"));
450 }
451
452
453 JOscProbHelper oscProb; //!< oscillation probability calculator
454
455
456 private:
457
458 /**
459 * Auxiliary class for I/O of map of event categories and weight factors.
460 */
462 public JProperties
463 {
464 /**
465 * Constructor.
466 *
467 * \param map event category map
468 * \param eqpars equation parameters
469 */
470 template<class JEvtCategoryMap_type>
471 JEvtCategoryMapHelper(JEvtCategoryMap_type& map,
472 const JEquationParameters& eqpars) :
473 JProperties(eqpars, 1)
474 {
475 using namespace std;
476 using namespace JPP;
477
478 for (typename map_type::const_iterator i = map.cbegin(); i != map.cend(); ++i) {
479
480 const int index = 1 + distance(map.cbegin(), i);
481
482 const string key = MAKE_STRING("category" << index);
483
484 JProperties properties1 = i->first .getProperties();
485 JProperties properties2 = i->second.getProperties();
486
487 (*this)[key] = properties1.join(properties2);
488 }
489 }
490 };
491 };
492
494
495
496 /**
497 * Template specialisation for a map between event categories and flux factors.
498 *
499 * Input syntax as follows:
500 * <pre>
501 * <PDG type> [<PDG type>]...; <flux identifier> [<flux arguments>];
502 * <key>=<value>[,<key>=<value>]...; <flux identifier> [<flux arguments>];
503 * </pre>
504 * where
505 * - <PDG type> refers to a PDG identifier;
506 * - <key> to one of the data members of a data structure derived from JAANET::JEvtCategory;
507 * - <value> to the corresponding value; and
508 * - <flux identifier> to a flux identifier (c.f. `JAANET::JFluxDictionary`) and
509 * - <flux arguments> to the list of arguments of the corresponding flux factor.
510 */
511 template<>
513 public std::map<JEvtCategoryHelper, JFluxHelper>,
514 public JClonable<JFlux, JEvtCategoryMap<JFluxHelper> >
515 {
516 public:
517
519
521
522 typedef typename map_type::iterator iterator;
523 typedef typename map_type::const_iterator const_iterator;
524
525
526
527 /**
528 * Default constructor.
529 */
531 map_type(),
532 oscProb()
533 {}
534
535
536 /**
537 * Constructor.
538 *
539 * \param oscProb oscillation probability calculator
540 */
542 map_type(),
543 oscProb(oscProb)
544 {}
545
546
547 /**
548 * Constructor.
549 *
550 * \param oscProb oscillation probability calculator
551 * \param category event category
552 * \param factor event-weight factor
553 */
555 const JEvtCategoryHelper& category,
556 const JFluxHelper& factor) :
557 map_type(),
558 oscProb(oscProb)
559 {
560 insert(category, factor);
561 }
562
563
564 /**
565 * Constructor.
566 *
567 * \param oscProb oscillation probability calculator
568 * \param category event category
569 * \param factor event-weight factor
570 * \param args remaining pairs of event categories and event-weight factors
571 */
572 template<class ...Args>
574 const JEvtCategoryHelper& category,
575 const JFluxHelper& factor,
576 const Args& ...args) :
577 map_type(),
578 oscProb(oscProb)
579 {
580 insert(category, factor, args...);
581 }
582
583
584 /**
585 * Constructor.
586 *
587 * \param category event category
588 * \param flux flux function
589 */
591 const JFluxHelper& flux) :
592 map_type(),
593 oscProb()
594 {
595 insert(category, flux);
596 }
597
598
599
600 /**
601 * Constructor.
602 *
603 * \param category event category
604 * \param flux flux function
605 * \param args remaining pairs of event categories and flux functions
606 */
607 template<class ...Args>
609 const JFluxHelper& flux,
610 const Args& ...args) :
611 map_type()
612 {
613 insert(category, flux, args...);
614 }
615
616
617 /**
618 * Insert pair of an event category and a flux function.
619 *
620 * \param category event category
621 * \param flux flux function
622 */
623 void insert(const JEvtCategoryHelper& category,
624 const JFluxHelper& flux)
625 {
626 map_type::insert(std::make_pair(category, flux));
627 }
628
629
630 /**
631 * Insert pairs of event categories and flux functions.
632 *
633 * \param category event category
634 * \param flux flux function
635 * \param args remaining pairs of event categories and flux functions
636 */
637 template<class ...Args>
638 void insert(const JEvtCategoryHelper& category,
639 const JFluxHelper& flux,
640 const Args& ...args)
641 {
642 insert(category, flux);
643 insert(args...);
644 }
645
646
647 /**
648 * Get weight factor of given event.
649 *
650 * \param evt event
651 * \return weight-factor for given event
652 */
653 double getFactor(const Evt& evt) const override final
654 {
655 using namespace JPP;
656
657 for (typename map_type::const_iterator i = this->cbegin(); i != this->cend(); ++i) {
658 if (i->first.match(evt)) {
659 return i->second.getFactor(evt);
660 }
661 }
662
663 THROW(JValueOutOfRange, "JEvtCategoryMap<JFluxHelper>::getFactor(): No flux factor for given event.");
664 }
665
666
667 /**
668 * Check whether this flux factor is valid.
669 *
670 * \return true if valid; else false
671 */
672 bool is_valid() const override final
673 {
674 for (typename map_type::const_iterator i = this->cbegin(); i != this->cend(); ++i) {
675 if (!(i->second.is_valid())) { return false; }
676 }
677
678 return true;
679 }
680
681
682 /**
683 * Get properties of this class.
684 *
685 * \param eqpars equation parameters
686 */
688 {
689 return JEvtCategoryMapHelper(*this, eqpars);
690 }
691
692
693 /**
694 * Get properties of this class.
695 *
696 * \param eqpars equation parameters
697 */
699 {
700 return JEvtCategoryMapHelper(*this, eqpars);
701 }
702
703
704 /**
705 * Read map between event categories and flux factors.
706 *
707 * \param in input stream
708 * \return input stream
709 */
710 std::istream& read(std::istream& in) override final
711 {
712 using namespace std;
713 using namespace JPP;
714
715 static const JFluxDictionary dictionary(oscProb);
716
717 JStringStream is(in);
718
719 if (getFileStatus(is.str().c_str())) {
720 is.load();
721 }
722
723 for (JToken<';'> token1, token2; is >> token1 >> token2; ) {
724
725 istringstream iss1(token1); // Read event categories
726
727 JEvtCategorySet categories;
728
729 iss1 >> categories;
730
731 istringstream iss2(token2); // Read event-weight factors
732
733 int fluxID = 0;
734
735 if (iss2 >> fluxID) {
736
737 JFluxHelper helper(dictionary.at(fluxID).getFlux());
738
739 JFlux& flux = helper.getFlux();
740
741 iss2 >> flux;
742
743 if (!fail(iss2)) {
744 for (JEvtCategorySet::const_iterator i = categories.cbegin(); i != categories.cend(); ++i) {
745 this->insert(*i, helper);
746 }
747 }
748 }
749 }
750
751 return in;
752 }
753
754
755 /**
756 * Write event-weight factor to output
757 *
758 * \param out output stream
759 * \return output stream
760 */
761 std::ostream& write(std::ostream& out) const override final
762 {
763 return out << getProperties(JEquationParameters("=", "\n", "./", "#"));
764 }
765
766
767 JOscProbHelper oscProb; //!< oscillation probability calculator
768
769
770 private:
771
772 /**
773 * Auxiliary class for I/O of map of event categories and weight factors.
774 */
776 public JProperties
777 {
778 /**
779 * Constructor.
780 *
781 * \param map event category map
782 * \param eqpars equation parameters
783 */
784 template<class JEvtCategoryMap_type>
785 JEvtCategoryMapHelper(JEvtCategoryMap_type& map,
786 const JEquationParameters& eqpars) :
787 JProperties(eqpars, 1)
788 {
789 using namespace std;
790 using namespace JPP;
791
792 for (typename map_type::const_iterator i = map.cbegin(); i != map.cend(); ++i) {
793
794 const int index = 1 + distance(map.cbegin(), i);
795
796 const string key = MAKE_STRING("category" << index);
797
798 JProperties sub1 = i->first .getProperties();
799 JProperties sub2 = i->second.getProperties();
800
801 (*this)[key] = sub1.join(sub2);
802 }
803 }
804 };
805 };
806
807 using JFluxMap = JEvtCategoryMap<JFluxHelper>; //!< Type alias for flux maps
808}
809
810#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Classes and methods for defining muon bundle categories.
Classes and methods for defining neutrino interaction categories.
I/O formatting auxiliaries.
Utility class to parse parameter values.
File status.
Template specialisation for a map between event categories and event-weight factor products.
JProperties getProperties(const JEquationParameters &eqpars) override final
Get properties of this class.
bool is_valid() const override final
Check whether this event-weight factor is valid.
JProperties getProperties(const JEquationParameters &eqpars) const override final
Get properties of this class.
JEvtCategoryMap(const JOscProbHelper &oscProb, const JEvtCategoryHelper &category, const JEvtWeightFactorHelper &factor)
Constructor.
JEvtCategoryMap(const JOscProbHelper &oscProb)
Constructor.
JEvtCategoryMap< JEvtWeightFactorProduct > JEvtCategoryMap_t
JEvtCategoryMap_t & join(const JEvtCategoryMap_t &object)
Join this map between event categories and weight factors with a given map between event categories a...
JOscProbHelper oscProb
oscillation probability calculator
std::istream & read(std::istream &in) override final
Read map between event categories and event-weight factors.
double getFactor(const Evt &evt) const override final
Get weight factor of given event.
void insert(const JEvtCategoryHelper &category, const JEvtWeightFactorHelper &factor)
Insert event category.
std::ostream & write(std::ostream &out) const override final
Write event-weight factor to output.
JEvtCategoryMap(const JOscProbHelper &oscProb, const JEvtCategoryHelper &category, const JEvtWeightFactorHelper &factor, const Args &...args)
Constructor.
void insert(const JEvtCategoryHelper &category, const JEvtWeightFactorProduct &product)
Insert pair of an event category and a product of event-weight factors.
JEvtCategoryMap(const JEvtCategoryHelper &category, const JEvtWeightFactorHelper &factor, const Args &...args)
Constructor.
void insert(const JEvtCategoryHelper &category, const JEvtWeightFactorHelper &factor, const Args &...args)
Insert pairs of event categories and event-weight factors.
std::map< JEvtCategoryHelper, JEvtWeightFactorProduct > map_type
JEvtCategoryMap(const JEvtCategoryHelper &category, const JEvtWeightFactorHelper &factor)
Constructor.
Template specialisation for a map between event categories and flux factors.
JProperties getProperties(const JEquationParameters &eqpars=JEvtWeightFactor::getEquationParameters()) const override final
Get properties of this class.
std::ostream & write(std::ostream &out) const override final
Write event-weight factor to output.
std::istream & read(std::istream &in) override final
Read map between event categories and flux factors.
JEvtCategoryMap(const JEvtCategoryHelper &category, const JFluxHelper &flux, const Args &...args)
Constructor.
bool is_valid() const override final
Check whether this flux factor is valid.
void insert(const JEvtCategoryHelper &category, const JFluxHelper &flux, const Args &...args)
Insert pairs of event categories and flux functions.
JEvtCategoryMap(const JOscProbHelper &oscProb)
Constructor.
JOscProbHelper oscProb
oscillation probability calculator
std::map< JEvtCategoryHelper, JFluxHelper > map_type
JEvtCategoryMap< JFluxHelper > JEvtCategoryMap_t
JProperties getProperties(const JEquationParameters &eqpars=JEvtWeightFactor::getEquationParameters()) override final
Get properties of this class.
void insert(const JEvtCategoryHelper &category, const JFluxHelper &flux)
Insert pair of an event category and a flux function.
JEvtCategoryMap(const JOscProbHelper &oscProb, const JEvtCategoryHelper &category, const JFluxHelper &factor)
Constructor.
JEvtCategoryMap(const JEvtCategoryHelper &category, const JFluxHelper &flux)
Constructor.
JEvtCategoryMap(const JOscProbHelper &oscProb, const JEvtCategoryHelper &category, const JFluxHelper &factor, const Args &...args)
Constructor.
double getFactor(const Evt &evt) const override final
Get weight factor of given event.
Auxiliary class for reading a map of event categories.
void insert(const JEvtCategoryHelper &category, const T &value)
Insert pair of event category and value.
JEvtCategoryMap()
Default constructor.
map_type::const_iterator const_iterator
JEvtCategoryMap< T > JEvtCategoryMap_t
JEvtCategoryMap(const JEvtCategoryHelper &category, const T &value, const Args &...args)
Constructor.
void insert(const JEvtCategoryHelper &category, const T &value, const Args &...args)
Insert pairs of event categories and values.
JEvtCategoryMap(const JEvtCategoryHelper &category, const T &value)
Constructor.
map_type::iterator iterator
std::map< JEvtCategoryHelper, T > map_type
Utility class to parse parameter values.
Simple data structure to support I/O of equations (see class JLANG::JEquation).
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
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).
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition Evt.hh:21
Helper class for event categories.
Auxiliary class for I/O of map of event categories and weight factors.
JEvtCategoryMapHelper(JEvtCategoryMap_type &map, const JEquationParameters &eqpars)
Constructor.
Auxiliary class for I/O of map of event categories and weight factors.
JEvtCategoryMapHelper(JEvtCategoryMap_type &map, const JEquationParameters &eqpars)
Constructor.
Container for a set of event categories.
Helper class for event-weight factor.
Class for product of event-weight factors.
static JEquationParameters & getEquationParameters()
Get equation parameters.
Helper class for flux function.
JFlux & getFlux() const
Get reference to flux function.
Low-level interface for retrieving the flux corresponding to a given event.
Definition JFlux.hh:23
Neutrino flux.
Definition JHead.hh:906
Template class for object cloning.
Definition JClonable.hh:59
Helper class for oscillation probabilities.