Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGauss.hh
Go to the documentation of this file.
1 #ifndef __JMATH__JGAUSS__
2 #define __JMATH__JGAUSS__
3 
4 #include <limits>
5 #include <cmath>
6 #include <istream>
7 #include <ostream>
8 #include <iomanip>
9 
10 #include "JLang/JEquals.hh"
11 #include "Jeep/JPrint.hh"
12 #include "JMath/JMath.hh"
13 
14 /**
15  * \author mdejong
16  */
17 
18 namespace JMATH {}
19 namespace JPP { using namespace JMATH; }
20 
21 namespace JMATH {
22 
23  using JLANG::JEquals;
24 
25  /**
26  * Data structure for Gaussian function on top of a flat background.
27  *
28  * This class implements the JMATH::JMath interface.
29  */
30  class JGauss :
31  public JMATH::JMath<JGauss>
32  {
33  public:
34 
35  /**
36  * Type definition of fit parameter.
37  */
38  typedef double JGauss::*parameter_type;
39 
40 
41  /**
42  * Default constructor.
43  */
44  JGauss() :
45  mean (0.0),
46  sigma (0.0),
47  signal (0.0),
48  background(0.0)
49  {}
50 
51 
52  /**
53  * Constructor.
54  *
55  * \param mean mean
56  * \param sigma sigma
57  * \param signal signal
58  * \param background background
59  */
60  JGauss(const double mean,
61  const double sigma,
62  const double signal,
63  const double background) :
64  mean (mean),
65  sigma (sigma),
66  signal (signal),
67  background(background)
68  {}
69 
70 
71  /**
72  * Add gauss.
73  *
74  * \param gauss gauss
75  * \return this gauss
76  */
77  JGauss& add(const JGauss& gauss)
78  {
79  mean += gauss.mean;
80  sigma += gauss.sigma;
81  signal += gauss.signal;
82  background += gauss.background;
83 
84  return *this;
85  }
86 
87 
88  /**
89  * Subtract gauss.
90  *
91  * \param gauss gauss
92  * \return this gauss
93  */
94  JGauss& sub(const JGauss& gauss)
95  {
96  mean -= gauss.mean;
97  sigma -= gauss.sigma;
98  signal -= gauss.signal;
99  background -= gauss.background;
100 
101  return *this;
102  }
103 
104 
105  /**
106  * Scale gauss.
107  *
108  * \param factor multiplication factor
109  * \return this gauss
110  */
111  JGauss& mul(const double factor)
112  {
113  mean *= factor;
114  sigma *= factor;
115  signal *= factor;
116  background *= factor;
117 
118  return *this;
119  }
120 
121 
122  /**
123  * Equality.
124  *
125  * \param gauss gauss
126  * \param eps numerical precision
127  * \return true if gauss's identical; else false
128  */
129  bool equals(const JGauss& gauss,
130  const double eps = std::numeric_limits<double>::min()) const
131  {
132  return (fabs(mean - gauss.mean) <= eps &&
133  fabs(sigma - gauss.sigma) <= eps &&
134  fabs(signal - gauss.signal) <= eps &&
135  fabs(background - gauss.background) <= eps);
136  }
137 
138 
139  /**
140  * Write Gauss to input stream.
141  *
142  * \param in input stream
143  * \param gauss gauss
144  * \return input stream
145  */
146  friend inline std::istream& operator>>(std::istream& in, JGauss& gauss)
147  {
148  return in >> gauss.mean >> gauss.sigma >> gauss.signal >> gauss.background;
149  }
150 
151 
152  /**
153  * Write Gauss to output stream.
154  *
155  * \param out output stream
156  * \param gauss gauss
157  * \return output stream
158  */
159  friend inline std::ostream& operator<<(std::ostream& out, const JGauss& gauss)
160  {
161  using namespace std;
162 
163  return out << FIXED(7,3) << gauss.mean << ' '
164  << FIXED(7,3) << gauss.sigma << ' '
165  << FIXED(9,3) << gauss.signal << ' '
166  << FIXED(9,3) << gauss.background;
167  }
168 
169  double mean;
170  double sigma;
171  double signal;
172  double background;
173  };
174 }
175 
176 #endif
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
friend std::ostream & operator<<(std::ostream &out, const JGauss &gauss)
Write Gauss to output stream.
Definition: JGauss.hh:159
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:461
double mean
Definition: JGauss.hh:169
JGauss & mul(const double factor)
Scale gauss.
Definition: JGauss.hh:111
I/O formatting auxiliaries.
JGauss & add(const JGauss &gauss)
Add gauss.
Definition: JGauss.hh:77
bool equals(const JGauss &gauss, const double eps=std::numeric_limits< double >::min()) const
Equality.
Definition: JGauss.hh:129
double JGauss::* parameter_type
Type definition of fit parameter.
Definition: JGauss.hh:38
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
friend std::istream & operator>>(std::istream &in, JGauss &gauss)
Write Gauss to input stream.
Definition: JGauss.hh:146
JGauss & sub(const JGauss &gauss)
Subtract gauss.
Definition: JGauss.hh:94
double sigma
Definition: JGauss.hh:170
Data structure for Gaussian function on top of a flat background.
Definition: JGauss.hh:30
Base class for data structures with artithmetic capabilities.
double signal
Definition: JGauss.hh:171
JGauss()
Default constructor.
Definition: JGauss.hh:44
JGauss(const double mean, const double sigma, const double signal, const double background)
Constructor.
Definition: JGauss.hh:60
double background
Definition: JGauss.hh:172