Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JHeadToolkit.hh
Go to the documentation of this file.
1 #ifndef __JAANET_JHEADTOOLKIT__
2 #define __JAANET_JHEADTOOLKIT__
3 
4 #include <istream>
5 #include <ostream>
6 
7 #include "evt/Vec.hh"
8 #include "evt/Head.hh"
9 
12 
13 #include "JLang/JEquation.hh"
15 #include "JLang/JRedirectString.hh"
16 #include "JLang/JNullStream.hh"
17 
18 #include "JROOT/JRootClass.hh"
19 #include "JROOT/JRootDictionary.hh"
20 #include "JROOT/JRootStreamer.hh"
21 
22 #include "JAAnet/JHead.hh"
23 
24 
25 /**
26  * \author mdejong
27  */
28 
29 namespace JROOT {
30 
31 
32  using std::vector; // prevent Doxygen crash
33 
34 
35  /**
36  * Specialisation of std::vector<JAANET::physics> for reading one element at a time.
37  */
38  template<>
39  struct JRootReader::JHelper< vector<JAANET::physics> >
40  {
41  /**
42  * Read object.
43  *
44  * \param reader ROOT reader
45  * \param object object
46  * \return ROOT reader
47  */
49  {
50  JAANET::physics element;
51 
52  JRootReader::getObject(reader, element);
53 
54  object.push_back(element);
55 
56  return reader;
57  }
58  };
59 
60 
61  /**
62  * Specialisation of std::vector<JAANET::physics> for writing only first element.
63  */
64  template<>
65  struct JRootWriter::JHelper< vector<JAANET::physics> > :
66  public JRootWriter::JDefaultHelper< vector<JAANET::physics> >
67  {
68  /**
69  * Write object.
70  *
71  * \param writer ROOT writer
72  * \param object object
73  * \return ROOT writer
74  */
76  {
77  if (!object.empty()) {
78  JRootWriter::putObject(writer, object[0]);
79  }
80 
81  return writer;
82  }
83  };
84 }
85 
86 
87 namespace JAANET {
88 
93 
94 
95  /**
96  * Push data of JHead for subsequent copy to Head.
97  *
98  * \param header header
99  * \param p pointer to data member to be pushed
100  */
101  template<class T>
102  inline void push(JHead& header, T JHead::*p)
103  {
104  using namespace JPP;
105 
106  const int offset = (const char*) &(header.*p) - (const char*) &header;
107 
108  const JRootClass cls(getType<JHead>());
109 
110  TIterator* i = cls.getClass()->GetListOfDataMembers()->MakeIterator();
111 
112  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
113 
114  const JRootClass abc = cls.get(*p);
115 
116  if (abc == JRootClass(JType<T>()) && offset == abc.getOffset()) {
117  header[p->GetName()] = "";
118  }
119  }
120  }
121 
122 
123  /**
124  * Check for generator.
125  *
126  * \param header header
127  * \param generator generator
128  * \return true if this Monte Carlo is produced by given generator; else false
129  */
130  inline bool is_physics(const JHead& header, const std::string& generator)
131  {
132  for (std::vector<physics>::const_iterator i = header.physics.begin(); i != header.physics.end(); ++i) {
133  if (i->program == generator) {
134  return true;
135  }
136  }
137 
138  return false;
139  }
140 
141 
142  /**
143  * Check for generator.
144  *
145  * \param header header
146  * \return true if this Monte Carlo is produced by JSirene; else false
147  */
148  inline bool is_sirene(const JHead& header)
149  {
150  return is_physics(header, JHead::JSIRENE);
151  }
152 
153 
154  /**
155  * Check for generator.
156  *
157  * \param header header
158  * \return true if this Monte Carlo is produced by km3; else false
159  */
160  inline bool is_km3(const JHead& header)
161  {
162  return is_physics(header, JHead::KM3);
163  }
164 
165 
166  /**
167  * Check for generator.
168  *
169  * \param header header
170  * \return true if this Monte Carlo is produced by KM3Sim; else false
171  */
172  inline bool is_km3sim(const JHead& header)
173  {
174  return !is_km3(header) && !is_sirene(header);
175  }
176 
177 
178  /**
179  * Check for generator.
180  *
181  * \param header header
182  * \return true if this Monte Carlo is produced by genhen; else false
183  */
184  inline bool is_genhen(const JHead& header)
185  {
186  return is_physics(header, JHead::GENHEN);
187  }
188 
189 
190  /**
191  * Check for generator.
192  *
193  * \param header header
194  * \return true if this Monte Carlo is produced by genie; else false
195  */
196  inline bool is_genie(const JHead& header)
197  {
198  return is_physics(header, JHead::GENIE) || is_physics(header, JHead::GSEAGEN);
199  }
200 
201 
202  /**
203  * Check for generator.
204  *
205  * \param header header
206  * \return true if this Monte Carlo is produced by mupage; else false
207  */
208  inline bool is_mupage(const JHead& header)
209  {
210  return is_physics(header, JHead::MUPAGE);
211  }
212 
213 
214  /**
215  * Get object from header.
216  *
217  * \param head header
218  * \return object
219  */
220  template<class T>
221  inline T get(const JHead& head);
222 
223 
224  /**
225  * Get position offset of detector due to generator.
226  *
227  * \param header header
228  * \return position
229  */
230  template<>
231  inline Vec get(const JHead& header)
232  {
233  if (is_sirene(header) || is_km3(header)) {
234 
235  return header.coord_origin;
236 
237  } else {
238 
239  if (is_valid(header.can))
240  return Vec(0.0, 0.0, -header.can.zmin);
241  else if (is_valid(header.coord_origin))
242  return header.coord_origin;
243  else
244  return Vec(0.0, 0.0, 0.0);
245  }
246  }
247 
248 
249  /**
250  * Get position offset of detector due to generator.
251  *
252  * \param header header
253  * \return position
254  */
255  template<>
256  inline JPosition3D get(const JHead& header)
257  {
258  const Vec pos = get<Vec>(header);
259 
260  return JPosition3D(pos.x, pos.y, pos.z);
261  }
262 
263 
264  /**
265  * Get cylinder corresponding to can.
266  *
267  * \param head header
268  * \return cylinder
269  */
270  template<>
271  inline JCylinder3D get(const JHead& head)
272  {
273  using namespace JGEOMETRY2D;
274 
275  return JCylinder3D(JCircle2D(JVector2D(), head.can.r),
276  head.can.zmin,
277  head.can.zmax);
278  }
279 
280 
281  /**
282  * Simple wrapper class around JROOT::JRootDictionary so that other classes
283  * could be included in this dictionary if necessary.
284  */
286  public JRootDictionary
287  {
288  private:
289  /**
290  * Default constructor.
291  */
294  {
295  add<Vec>();
296  add< std::vector<JAANET::physics> >();
297  }
298 
299 
300  public:
301  /**
302  * Get reference to unique instance of this class object.
303  * This JROOT::JRootDictionary object includes the various Monte Carlo data types.
304  *
305  * \return reference to this class object
306  */
308  {
309  static JAAnetDictionary dictionary;
310 
311  return dictionary;
312  }
313  };
314 
315 
316  /**
317  * Get equation parameters corresponding to Monte Carlo ASCII format, i.e:
318  * <pre>
319  * <key>: <value [<value>]*
320  * <key>: <value [<value>]*
321  * </pre>
322  *
323  * \return equation parameters
324  */
326  {
327  static const JEquationParameters parameters(":", "\n", "", "");
328 
329  return parameters;
330  }
331 
332 
333  /**
334  * Read header from input.
335  *
336  * \param in input stream
337  * \param header header
338  * \return input stream
339  */
340  inline std::istream& operator>>(std::istream& in, JHead& header)
341  {
342  using namespace JLANG;
343  using namespace JROOT;
344  using namespace std;
345 
347 
348  JRootClassReader cls(header);
349 
350  for (JEquation equation; reader >> equation && equation.getKey() != end_event::Class_Name(); ) {
351 
352  JRedirectString redirect(reader, equation.getValue());
353 
354  const JRootClassReader abc = cls.find(equation.getKey().c_str());
355 
356  if (abc.is_valid()) {
357  reader.getObject(abc);
358  }
359  }
360 
361  return in;
362  }
363 
364 
365  /**
366  * Write header to output.
367  *
368  * \param out output stream
369  * \param header header
370  * \return output stream
371  */
372  inline std::ostream& operator<<(std::ostream& out, const JHead& header)
373  {
374  using namespace JLANG;
375  using namespace JROOT;
376 
378 
379  JRootClassWriter cls(header);
380 
381  TIterator* i = cls.getClass()->GetListOfDataMembers()->MakeIterator();
382 
383  for (const TDataMember* p; (p = (const TDataMember*) i->Next()) != NULL; ) {
384  if (!JRootClass::is_static(*p)) {
385  writer.put(p->GetName(), cls.get(*p), true);
386  }
387  }
388 
389  return out;
390  }
391 }
392 
393 #endif
bool is_mupage(const JHead &header)
Check for generator.
Data structure for vector in two dimensions.
Definition: JVector2D.hh:30
static const std::string GSEAGEN
Definition: JHead.hh:735
std::istream & operator>>(std::istream &in, JHead &header)
Read header from input.
static const std::string KM3
Definition: JHead.hh:738
Implementation for ASCII output of objects with ROOT dictionary.
Data structure for circle in two dimensions.
Definition: JCircle2D.hh:29
Helper class for user formatting.
Simple wrapper class around JROOT::JRootDictionary so that other classes could be included in this di...
std::vector< JAANET::physics > physics
Definition: JHead.hh:856
Simple data structure to support I/O of equations (see class JLANG::JEquation).
void push(JHead &header, T JHead::*p)
Push data of JHead for subsequent copy to Head.
static JRootReader & getObject(JRootReader &reader, T &object)
Read object.
Generator for neutrino interaction.
Definition: JHead.hh:281
Description of Monte Carlo event generation applications.
Definition: JHead.hh:258
General purpose equation class.
Definition: JEquation.hh:47
ROOT class for writing object.
Definition: JRootClass.hh:568
Cylinder object.
Definition: JCylinder3D.hh:37
static JRootWriter & putObject(JRootWriter &writer, const std::vector< JAANET::physics > &object)
Write object.
Definition: JHeadToolkit.hh:75
Implementation for ASCII input of objects with ROOT dictionary.
ASCII I/O of objects with ROOT dictionary.
ROOT class for reading object.
Definition: JRootClass.hh:478
static const std::string MUPAGE
Definition: JHead.hh:736
static JRootWriter & put(JRootWriter &writer, const std::string &key, const T &value)
Write given key and value according equation format.
static const std::string GENIE
Definition: JHead.hh:734
static const std::string JSIRENE
Definition: JHead.hh:737
Default helper class for formatting.
static JRootReader & getObject(JRootReader &reader, std::vector< JAANET::physics > &object)
Read object.
Definition: JHeadToolkit.hh:48
Monte Carlo run header.
Definition: JHead.hh:727
JRootAddressableClass get(const TDataMember &data_member) const
Get addressable class of given data member.
Definition: JRootClass.hh:444
JAAnetDictionary()
Default constructor.
ROOT dictionary.
bool is_genhen(const JHead &header)
Check for generator.
bool is_physics(const JHead &header, const std::string &generator)
Check for generator.
bool is_sirene(const JHead &header)
Check for generator.
TClass * getClass() const
Get class.
Definition: JRootClass.hh:167
static JRootDictionary & getInstance()
Get reference to unique instance of this class object.
bool is_km3sim(const JHead &header)
Check for generator.
static JRootWriter & putObject(JRootWriter &writer, const T &object)
Write object.
Data structure for position in three dimensions.
Definition: JPosition3D.hh:35
static const std::string GENHEN
Generators.
Definition: JHead.hh:733
Helper class for user formatting.
JRootAddressableClass find(const char *name) const
Find addressable base class or data member with given name within current class.
Definition: JRootClass.hh:433
This class can be used to temporarily redirect an input stream to an input string.
const JEquationParameters & getEquationParameters()
Get equation parameters corresponding to Monte Carlo ASCII format, i.e:
std::ostream & operator<<(std::ostream &out, const JHead &header)
Write header to output.
bool is_valid(const T &value)
Check validity of given value.
Definition: JHead.hh:711
bool is_genie(const JHead &header)
Check for generator.
bool is_km3(const JHead &header)
Check for generator.