Jpp  master_rocky-43-ge265d140c
the software that should make you happy
JEvtCategoryHelper.hh
Go to the documentation of this file.
1 #ifndef __JAANET__JEVTCATEGORYHELPER__
2 #define __JAANET__JEVTCATEGORYHELPER__
3 
4 #include <memory>
5 
8 
9 #include "JLang/JComparable.hh"
10 
11 #include "JAAnet/JHead.hh"
12 #include "JAAnet/JEvtCategory.hh"
15 
16 
17 /**
18  * \author bjung
19  */
20 
21 namespace JAANET {}
22 namespace JPP { using namespace JAANET; }
23 
24 namespace JAANET {
25 
26  using JLANG::JComparable;
27 
28 
29  /**
30  * Helper class for event categories.
31  */
33  public std::shared_ptr<JEvtCategory>,
34  public JComparable<JEvtCategoryHelper>
35  {
36  typedef std::shared_ptr<JEvtCategory> pointer_type;
37 
38  /**
39  * Default constructor.
40  */
42  pointer_type()
43  {}
44 
45 
46  /**
47  * Constructor.
48  *
49  * \param p shared pointer to event category
50  */
52  pointer_type(p)
53  {}
54 
55 
56  /**
57  * Constructor.
58  *
59  * \param category event category
60  */
62  {
63  this->configure(category);
64  }
65 
66 
67  /**
68  * Constructor.
69  *
70  * \param header MC header
71  */
72  JEvtCategoryHelper(const JHead& header)
73  {
74  this->configure(header);
75  }
76 
77 
78  /**
79  * Constructor.
80  *
81  * \param type PDG type
82  */
83  JEvtCategoryHelper(const int type)
84  {
85  this->configure(type);
86  }
87 
88 
89  /**
90  * Configuration.
91  *
92  * \param p shared pointer to event category
93  */
94  void configure(const pointer_type& p)
95  {
96  static_cast<pointer_type&>(*this) = p;
97  }
98 
99 
100  /**
101  * Configuration.
102  *
103  * \param category event category
104  */
105  void configure(const JEvtCategory& category)
106  {
107  reset(category.clone());
108  }
109 
110 
111  /**
112  * Configuration.
113  *
114  * \param category event category
115  */
116  void configure(const JHead& header)
117  {
118  using namespace JPP;
119 
120  if (is_neutrino_primary(header.primary.type)) {
121  reset(new JNeutrinoInteractionCategory(header));
122  } else if (is_muon_bundle_primary(header.primary.type)) {
123  reset(new JMuonBundleCategory(header));
124  } else {
125  THROW(JValueOutOfRange, "JEvtCategoryHelper::configure(): Cannot construct event category for given header with primary ID " << header.primary.type);
126  }
127  }
128 
129 
130  /**
131  * Configuration.
132  *
133  * \param type PDG type
134  */
135  void configure(const int type)
136  {
137  using namespace JPP;
138 
139  if (is_neutrino_primary(type)) {
141  } else if (is_muon_bundle_primary(type)) {
142  reset(new JMuonBundleCategory(type));
143  } else {
144  THROW(JValueOutOfRange, "JEvtCategoryHelper::configure(): Cannot construct event category for PDG type " << type);
145  }
146  }
147 
148 
149  /**
150  * Get reference to event category.
151  *
152  * \return reference to event category
153  */
155  {
156  using namespace JPP;
157 
158  if (static_cast<const JEvtCategoryHelper&>(*this)) {
159  return *(this->get());
160  } else {
161  THROW(JNullPointerException, "JEvtCategoryHelper::getEvtCategory(): Event category is not set.");
162  }
163  }
164 
165 
166  /**
167  * Check if event category is valid.
168  *
169  * \return true if event category is valid; else false
170  */
171  bool is_valid() const
172  {
173  return static_cast<const JEvtCategoryHelper&>(*this) && this->get()->is_valid();
174  }
175 
176 
177  /**
178  * Check whether given MC header matches with this event category.
179  *
180  * \param header MC header
181  * \return true if matching; else false
182  */
183  bool match(const JHead& header) const
184  {
185  const JEvtCategory& category = getEvtCategory();
186 
187  return category.match(header);
188  }
189 
190 
191  /**
192  * Check whether given event matches with this event category.
193  *
194  * \param event event
195  * \return true if matching; else false
196  */
197  bool match(const Evt& event) const
198  {
199  const JEvtCategory& category = getEvtCategory();
200 
201  return category.match(event);
202  }
203 
204 
205  /**
206  * Less-than method.
207  *
208  * \param category event category
209  * \return true if this event category is less than given event category; else false
210  */
211  bool less(const JEvtCategory& category) const
212  {
213  const JEvtCategory& cat = getEvtCategory();
214 
215  return cat.less(category);
216  }
217 
218 
219  /**
220  * Less-than method.
221  *
222  * \param helper shared pointer to event category
223  * \return true if this event category is less than given event category; else false
224  */
225  bool less(const JEvtCategoryHelper& helper) const
226  {
227  return this->less(helper.getEvtCategory());
228  }
229 
230 
231  /**
232  * Get properties of this class.
233  *
234  * \param equation equation parameters
235  */
237  {
238  const JEvtCategory& cat = getEvtCategory();
239 
240  return cat.getProperties();
241  }
242 
243 
244  /**
245  * Read event category from input.
246  *
247  * \param in input stream
248  * \param object event category
249  * \return input stream
250  */
251  friend inline std::istream& operator>>(std::istream& in, JEvtCategoryHelper& object)
252  {
253  using namespace std;
254 
255  if (object) {
256 
257  return object.get()->read(in);
258 
259  } else {
260 
261  streampos pos = in.tellg();
262 
263  int type = 0;
264 
265  if (in >> type) {
266  object.configure(type);
267  return in;
268  }
269 
270  in.clear();
271  in.seekg(pos);
272 
273  JNeutrinoInteractionCategory neutrinoInteraction;
274  neutrinoInteraction.getProperties().read(in);
275 
276  if (neutrinoInteraction.is_valid()) {
277  object.configure(neutrinoInteraction);
278  return in;
279  }
280 
281  in.clear();
282  in.seekg(pos);
283 
284  JMuonBundleCategory muonBundle;
285  muonBundle.getProperties().read(in);
286 
287  if (muonBundle.is_valid()) {
288  object.configure(muonBundle);
289  }
290 
291  return in;
292  }
293  }
294 
295 
296  /**
297  * Write event category to output.
298  *
299  * \param out output stream
300  * \param object event category
301  * \return output stream
302  */
303  friend inline std::ostream& operator<<(std::ostream& out, const JEvtCategoryHelper& object)
304  {
305  if (object.is_valid()) {
306  return out << object.getEvtCategory();
307  } else {
308  return out << 0;
309  }
310  }
311  };
312 }
313 
314 #endif
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Classes and methods for defining muon bundle categories.
Classes and methods for defining neutrino interaction categories.
Monte Carlo run header.
Definition: JHead.hh:1236
JAANET::primary primary
Definition: JHead.hh:1611
Utility class to parse parameter values.
Definition: JProperties.hh:501
bool read(const JEquation &equation)
Read equation.
Definition: JProperties.hh:679
Simple data structure to support I/O of equations (see class JLANG::JEquation).
Exception for null pointer operation.
Definition: JException.hh:234
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:180
Extensions to Evt data format.
bool is_neutrino_primary(const int type)
Auxiliary function to check if given PDG code corresponds to a neutrino.
bool is_muon_bundle_primary(const int type)
Auxiliary function to check if given PDG code corresponds to a valid muon bundle primary type.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
void reset(T &value)
Reset value.
Definition: JSTDTypes.hh:14
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition: Evt.hh:21
Helper class for event categories.
std::shared_ptr< JEvtCategory > pointer_type
bool is_valid() const
Check if event category is valid.
JEvtCategoryHelper(const pointer_type &p)
Constructor.
JEvtCategoryHelper(const int type)
Constructor.
JEvtCategoryHelper(const JHead &header)
Constructor.
JEvtCategoryHelper()
Default constructor.
JEvtCategory & getEvtCategory() const
Get reference to event category.
void configure(const JHead &header)
Configuration.
bool less(const JEvtCategoryHelper &helper) const
Less-than method.
JEvtCategoryHelper(const JEvtCategory &category)
Constructor.
void configure(const int type)
Configuration.
void configure(const pointer_type &p)
Configuration.
JProperties getProperties(const JEquationParameters &equation=JEvtCategory::getEquationParameters()) const
Get properties of this class.
bool less(const JEvtCategory &category) const
Less-than method.
bool match(const Evt &event) const
Check whether given event matches with this event category.
void configure(const JEvtCategory &category)
Configuration.
friend std::istream & operator>>(std::istream &in, JEvtCategoryHelper &object)
Read event category from input.
friend std::ostream & operator<<(std::ostream &out, const JEvtCategoryHelper &object)
Write event category to output.
bool match(const JHead &header) const
Check whether given MC header matches with this event category.
Low-level interface for event categories.
Definition: JEvtCategory.hh:38
virtual JProperties getProperties(const JEquationParameters &eqpars=JEvtCategory::getEquationParameters())
Get properties of this class.
virtual bool less(const JEvtCategory &category) const
Less-than method.
virtual bool match(const JHead &header) const =0
Check whether given MC header matches with this event category.
static JEquationParameters & getEquationParameters()
Get equation parameters.
Class for muon bundle categories.
bool is_valid() const override final
Check if muon bundle category is valid.
JProperties getProperties(const JEquationParameters &equation=JEvtCategory::getEquationParameters()) override final
Get properties of this class.
Class for neutrino interaction categories.
bool is_valid() const override final
Check if neutrino interaction categories is valid.
JProperties getProperties(const JEquationParameters &eqpars=JEvtCategory::getEquationParameters()) override final
Get properties of this class.
int type
Particle type.
Definition: JHead.hh:1204
virtual clone_type clone() const override
Get clone of this object.
Definition: JClonable.hh:69
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:139