Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPDFTable.hh
Go to the documentation of this file.
1 #ifndef __JPHYSICS__JPDFTABLE__
2 #define __JPHYSICS__JPDFTABLE__
3 
4 #include <cmath>
5 
6 #include "JIO/JObjectBinaryIO.hh"
8 #include "JTools/JQuantiles.hh"
9 #include "JTools/JSet.hh"
10 #include "JTools/JRange.hh"
11 #include "JTools/JConstants.hh"
13 
14 
15 /**
16  * \author mdejong
17  */
18 
19 namespace JPHYSICS {}
20 namespace JPP { using namespace JPHYSICS; }
21 
22 namespace JPHYSICS {
23 
24  using JIO::JSerialisable;
25  using JIO::JReader;
26  using JIO::JWriter;
30  using JTOOLS::JRange;
31 
32 
33  /**
34  * Multi-dimensional PDF table for arrival time of Cherenkov light.
35  */
36  template<class JFunction1D_t,
37  class JMaplist_t,
39  class JPDFTable :
40  public JTransformableMultiFunction<JFunction1D_t, JMaplist_t, JDistance_t>,
41  public JSerialisable,
42  public JObjectBinaryIO< JPDFTable<JFunction1D_t, JMaplist_t, JDistance_t> >
43  {
44  public:
45 
47 
51 
54 
56 
60 
62 
63 
64  /**
65  * Default constructor.
66  */
69  {}
70 
71 
72  /**
73  * Constructor.
74  *
75  * \param input multi-dimensional function
76  */
77  template<class JPDF_t, class JPDFMaplist_t, class JPDFDistance_t>
80  {}
81 
82 
83  /**
84  * Constructor.
85  *
86  * \param input multi-dimensional histogram
87  */
88  template<class JHistogram1D_t, class JHistogramMaplist_t, class JHistogramDistance_t>
91  {}
92 
93 
94  /**
95  * Blur PDF.
96  *
97  * The arrival times of Cherenkov light are smeared according to a Gaussian distribution
98  * with the specified width (i.e.\ TTS) using Gauss-Hermite integration.
99  * An exception is made when the time range according the specified quantile is
100  * smaller than the specified width (TTS) of the Gaussian distribution.
101  * In that case, the resulting PDF is a Gaussian distribution with the specified width (TTS)
102  * and normalisation according to the integral value of the input PDF.
103  * A smooth transition is imposed between the normal regime and this exeption.
104  *
105  * \param TTS TTS [ns]
106  * \param numberOfPoints number of points for Gauss-Hermite integration
107  * \param epsilon precision
108  * \param quantile quantile
109  */
110  void blur(const double TTS,
111  const int numberOfPoints = 25,
112  const double epsilon = 1.0e-10,
113  const double quantile = 0.99)
114  {
115  using namespace std;
116  using namespace JTOOLS;
117 
118  typedef typename transformer_type::array_type array_type;
119 
120  const JGaussHermite engine(numberOfPoints, epsilon);
121 
122  for (super_iterator i = this->super_begin(); i != this->super_end(); ++i) {
123 
124  const array_type array = (*i).getKey();
125  function_type& f1 = (*i).getValue();
126 
127  if (!f1.empty()) {
128 
129  const typename function_type::supervisor_type& supervisor = f1.getSupervisor();
130 
131  const JMultiMapGetTransformer<NUMBER_OF_DIMENSIONS - 1, value_type> get(*(this->transformer), array);
132  const JMultiMapPutTransformer<NUMBER_OF_DIMENSIONS - 1, value_type> put(*(this->transformer), array);
133 
134  f1.transform(get);
135  f1.compile();
136 
137  const JQuantiles Q(f1, quantile);
138 
139  // abscissa
140 
141  JSet<double> X;
142 
143  for (JGaussHermite::const_iterator j = engine.begin(); j != engine.end(); ++j) {
144  X.insert(Q.getX() + TTS*sqrt(2.0)*j->getX());
145  }
146 
147  for (typename function_type::const_iterator j = f1.begin(); j != f1.end(); ++j) {
148 
149  if (j->getX() - TTS < X.getXmin()) {
150  X.insert(j->getX() - TTS);
151  }
152 
153  if (j->getX() + TTS > X.getXmax()) {
154  X.insert(j->getX() + TTS);
155  }
156  }
157 
158 
159  const double W = gauss(Q.getUpperLimit() - Q.getLowerLimit(), TTS);
160 
162 
163  for (JSet<double>::const_iterator x = X.begin(); x != X.end(); ++x) {
164 
165  double y = 0.0;
166 
167  for (JGaussHermite::const_iterator j = engine.begin(); j != engine.end(); ++j) {
168 
169  const double u = j->getX();
170  const double v = j->getY() / sqrt(PI);
171  const double w = get_value(f1(*x + u*TTS*sqrt(2.0)));
172 
173  y += v * w;
174  }
175 
176  buffer[*x] = W * Q.getIntegral() * Gauss(*x - Q.getX(), TTS) + (1.0 - W) * y;
177  }
178 
179  buffer.transform(put);
180  buffer.compile();
181 
182  f1 = buffer;
183 
184  f1.setExceptionHandler(supervisor);
185  }
186  }
187  }
188 
189 
190  /**
191  * Compresses PDF to given abscissa range.
192  *
193  * \param range abscissa range
194  */
196  {
197  for (super_iterator i = this->super_begin(); i != this->super_end(); ++i) {
198 
199  function_type& f1 = i.getValue();
200 
201  typename function_type::iterator p = f1.lower_bound(range.getLowerLimit());
202 
203  f1.function_type::container_type::erase(f1.begin(), p);
204 
205  typename function_type::iterator q = f1.lower_bound(range.getUpperLimit());
206 
207  f1.function_type::container_type::erase(++q, f1.end());
208  }
209 
210  this->compile();
211  }
212 
213 
214  /**
215  * Read from input.
216  *
217  * \param in reader
218  * \return reader
219  */
220  virtual JReader& read(JReader& in)
221  {
222  if (in >> static_cast<transformablemultifunction_type&>(*this)) {
223 
224  // read optional transformer
225 
227 
228  if (buffer.read(in)) {
229 
230  this->transformer.reset(buffer.clone());
231 
232  } else {
233 
234  in.clear();
235 
237  }
238  }
239 
240  this->compile();
241 
242  return in;
243  }
244 
245 
246  /**
247  * Write from input.
248  *
249  * \param out writer
250  * \return writer
251  */
252  virtual JWriter& write(JWriter& out) const
253  {
254  out << static_cast<const transformablemultifunction_type&>(*this);
255 
256  this->transformer->write(out);
257 
258  return out;
259  }
260 
261  protected:
262  /**
263  * Gauss function (normalised to 1 at x = 0).
264  *
265  * \param x x
266  * \param sigma sigma
267  * \return function value
268  */
269  static double gauss(const double x, const double sigma)
270  {
271  const double u = x / sigma;
272 
273  if (fabs(u) < 10.0)
274  return exp(-0.5*u*u);
275  else
276  return 0.0;
277  }
278 
279 
280  /**
281  * Normalised Gauss function.
282  *
283  * \param x x
284  * \param sigma sigma
285  * \return function value
286  */
287  static double Gauss(const double x, const double sigma)
288  {
289  return gauss(x, sigma) / sqrt(2.0*JTOOLS::PI) / sigma;
290  }
291 
292 
293  /**
294  * Normalised Gauss function.
295  *
296  * \param x x
297  * \param x0 central value
298  * \param sigma sigma
299  * \return function value
300  */
301  static double Gauss(const double x, const double x0, const double sigma)
302  {
303  return Gauss(x - x0, sigma);
304  }
305  };
306 }
307 
308 #endif
JFunction_t::argument_type argument_type
void compile()
Compilation.
data_type w[N+1][M+1]
Definition: JPolint.hh:708
Interface for binary output.
multifunction_type::result_type result_type
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:215
multimap_type::super_iterator super_iterator
double getIntegral() const
Get integral of function.
Definition: JQuantiles.hh:346
Auxiliary base class for storing and loading a single object to and from a binary file...
transformablemultifunction_type::multimap_type multimap_type
Definition: JPDFTable.hh:52
Transformable multidimensional function.
JMultiMapTransformer< JMapLength< JMaplist_t >::value, argument_type > transformer_type
Template class for distance evaluation.
Definition: JDistance.hh:24
JFunction_t::value_type value_type
Numerical integrator for W(x) = e^-(x^2).
Definition: JQuadrature.hh:251
multifunction_type::argument_type argument_type
transformablemultifunction_type::function_type function_type
Definition: JPDFTable.hh:59
Template definition of transformer of the probability density function (PDF) of the time response of ...
double getX() const
Get position of maximum.
Definition: JQuantiles.hh:313
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
static const double PI
Constants.
Definition: JConstants.hh:20
void blur(const double TTS, const int numberOfPoints=25, const double epsilon=1.0e-10, const double quantile=0.99)
Blur PDF.
Definition: JPDFTable.hh:110
virtual JWriter & write(JWriter &out) const
Write from input.
Definition: JPDFTable.hh:252
virtual JReader & read(JReader &in)
Read from input.
Definition: JPDFTable.hh:220
virtual abscissa_type getXmax() const
Get maximal abscissa value.
Definition: JSet.hh:114
transformablemultifunction_type::value_type value_type
Definition: JPDFTable.hh:50
JTransformableMultiFunction< JFunction1D_t, JMaplist_t, JDistance_t > transformablemultifunction_type
Definition: JPDFTable.hh:46
Auxiliary class to convert JMultiMapTransformer to JCollectionElementTransformer. ...
Quantile calculator for a given function.
Definition: JQuantiles.hh:106
JMultiMap< typename JFunction_t::argument_type, JFunction_t, JMaplist_t, JDistance_t > multimap_type
JArray< NUMBER_OF_DIMENSIONS, argument_type > buffer
static double Gauss(const double x, const double sigma)
Normalised Gauss function.
Definition: JPDFTable.hh:287
void compress(const JRange< typename function_type::abscissa_type > &range)
Compresses PDF to given abscissa range.
Definition: JPDFTable.hh:195
Forward declaration of binary output.
JPDFTable(const JTransformableMultiFunction< JPDF_t, JPDFMaplist_t, JPDFDistance_t > &input)
Constructor.
Definition: JPDFTable.hh:78
Constants.
JPDFTable(const JTransformableMultiHistogram< JHistogram1D_t, JHistogramMaplist_t, JHistogramDistance_t > &input)
Constructor.
Definition: JPDFTable.hh:89
Transformable multidimensional histogram.
Multi-dimensional PDF table for arrival time of Cherenkov light.
Definition: JPDFTable.hh:39
JPDFTable()
Default constructor.
Definition: JPDFTable.hh:67
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:226
static double Gauss(const double x, const double x0, const double sigma)
Normalised Gauss function.
Definition: JPDFTable.hh:301
static JMultiMapTransformer * getClone()
Get clone of default transformer.
Interface for binary input.
One dimensional array of template objects with fixed length.
Definition: JArray.hh:35
Range of values.
Definition: JRange.hh:34
transformablemultifunction_type::transformer_type transformer_type
Definition: JPDFTable.hh:53
transformablemultifunction_type::argument_type argument_type
Definition: JPDFTable.hh:48
Auxiliary class to define a range between two values.
int numberOfPoints
Definition: JResultPDF.cc:22
transformablemultifunction_type::super_const_iterator super_const_iterator
Definition: JPDFTable.hh:57
container_type::const_iterator const_iterator
Definition: JCollection.hh:90
Auxiliary class to convert JMultiMapTransformer to JCollectionElementTransformer. ...
transformablemultifunction_type::super_iterator super_iterator
Definition: JPDFTable.hh:58
int j
Definition: JPolint.hh:634
void insert(const JTransformableMultiFunction< JPDF_t, JPDFMaplist_t, JPDFDistance_t > &input)
Insert multidimensional input.
transformablemultifunction_type::result_type result_type
Definition: JPDFTable.hh:49
virtual abscissa_type getXmin() const
Get minimal abscissa value.
Definition: JSet.hh:103
Simple data structure for an abstract collection of non-equidistant abscissa values.
Definition: JSet.hh:29
multifunction_type::super_iterator super_iterator
data_type v[N+1][M+1]
Definition: JPolint.hh:707
double u[N+1]
Definition: JPolint.hh:706
JLANG::JSharedPointer< transformer_type > transformer
multifunction_type::super_const_iterator super_const_iterator
virtual void clear()
Clear status of reader.
JResultEvaluator< JResult_t >::result_type get_value(const JResult_t &value)
Helper method to recursively evaluate a to function value.
Definition: JResult.hh:936
static double gauss(const double x, const double sigma)
Gauss function (normalised to 1 at x = 0).
Definition: JPDFTable.hh:269
then set_variable FORMULA *[0] exp(-0.5 *(x-[1])*(x-[1])/([2]*[2]))" set_variable OUTPUT_FILE histogram.root JHistogram1D -o $WORKDIR/$OUTPUT_FILE -F "$FORMULA" -