Jpp
JMath.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 
5 #include "TRandom3.h"
6 
7 #include "JLang/JException.hh"
8 #include "JMath/JMath.hh"
9 #include "JMath/JMathToolkit.hh"
10 
11 #include "Jeep/JPrint.hh"
12 #include "Jeep/JParser.hh"
13 #include "Jeep/JMessage.hh"
14 
15 
16 namespace {
17 
18  using namespace JPP;
19 
20  /**
21  * A random object with some arithmetic capabilities.
22  */
23  struct JObject :
24  public JMath<JObject>
25  {
26  public:
27  /**
28  * Default constructor.
29  */
30  JObject() :
31  x(0.0),
32  y(0.0),
33  z(0.0)
34  {}
35 
36 
37  /**
38  * Constructor.
39  *
40  * \param __x x
41  * \param __y y
42  * \param __z z
43  */
44  JObject(const double __x,
45  const double __y,
46  const double __z) :
47  x(__x),
48  y(__y),
49  z(__z)
50  {}
51 
52 
53  /**
54  * Add object.
55  *
56  * \param object object
57  * \return this object
58  */
59  JObject& add(const JObject& object)
60  {
61  x += object.x;
62  y += object.y;
63  z += object.z;
64 
65  return *this;
66  }
67 
68 
69  /**
70  * Scale object.
71  *
72  * \param factor multiplication factor
73  * \return this object
74  */
75  JObject& mul(const double factor)
76  {
77  x *= factor;
78  y *= factor;
79  z *= factor;
80 
81  return *this;
82  }
83 
84 
85  /**
86  * Write object to output.
87  *
88  * \param out output stream
89  * \param object object
90  * \return output stream
91  */
92  friend inline std::ostream& operator<<(std::ostream& out, const JObject& object)
93  {
94  using namespace std;
95 
96  out << showpos << FIXED(5,2) << object.x << ' '
97  << showpos << FIXED(5,2) << object.y << ' '
98  << showpos << FIXED(5,2) << object.z;
99 
100  return out;
101  }
102 
103 
104  double x;
105  double y;
106  double z;
107  };
108 }
109 
110 
111 /**
112  * \file
113  *
114  * Example program to test user class with arithmetic capabilities (based on class JMATH::JMath).
115  * \author mdejong
116  */
117 int main(int argc, char**argv)
118 {
119  using namespace std;
120 
121  double alpha;
122  int debug;
123 
124  try {
125 
126  JParser<> zap("Example program to test user class with arithmetic capabilities.");
127 
128  zap['a'] = make_field(alpha);
129  zap['d'] = make_field(debug) = 0;
130 
131  zap(argc, argv);
132  }
133  catch(const exception &error) {
134  FATAL(error.what() << endl);
135  }
136 
137  using namespace JPP;
138 
139 
140  gRandom->SetSeed(0);
141 
142  const Double_t xmin = -1.0;
143  const Double_t xmax = +1.0;
144 
145 
146  const JObject A(gRandom->Uniform(xmin, xmax),
147  gRandom->Uniform(xmin, xmax),
148  gRandom->Uniform(xmin, xmax));
149 
150  const JObject B(gRandom->Uniform(xmin, xmax),
151  gRandom->Uniform(xmin, xmax),
152  gRandom->Uniform(xmin, xmax));
153 
154  cout << "A " << A << endl;
155  cout << "B " << B << endl;
156  cout << "interpolate " << interpolate(A, B, alpha) << endl;
157 
158  cout << "A + B = " << A + B << endl;
159 
160  cout << "A * " << noshowpos << FIXED(5,2) << alpha << " = " << A * alpha << endl;
161 }
JException.hh
FIXED
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:481
JMessage.hh
JPrint.hh
JLANG::JObject
Auxiliary base class for inline creation of a static value or clone from a temporary object.
Definition: JObject.hh:18
JMATH::interpolate
T interpolate(const T &first, const T &second, const double alpha)
Interpolation between objects.
Definition: JMathToolkit.hh:185
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
JMATH::JMath
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
main
int main(int argc, char **argv)
Definition: JMath.cc:117
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JMathToolkit.hh
debug
int debug
debug level
Definition: JSirene.cc:59
operator<<
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Definition: clb_common_header.hh:72
JParser.hh
JMath.hh
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
std
Definition: jaanetDictionary.h:36
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
JLANG::JObject::JObject
JObject()
Default constructor.
Definition: JObject.hh:53