Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JCDFTable.hh
Go to the documentation of this file.
1 #ifndef __JPHYSICS__JCDFTABLE__
2 #define __JPHYSICS__JCDFTABLE__
3 
4 #include <stdarg.h>
5 
6 #include "JIO/JSerialisable.hh"
7 #include "JIO/JObjectBinaryIO.hh"
8 #include "JLang/JException.hh"
10 #include "JMath/JZero.hh"
11 #include "JTools/JArray.hh"
12 #include "JTools/JMultiKey.hh"
16 #include "JTools/JToolsToolkit.hh"
18 
19 
20 /**
21  * \author mdejong
22  */
23 
24 namespace JPHYSICS {}
25 namespace JPP { using namespace JPHYSICS; }
26 
27 namespace JPHYSICS {
28 
29  using JIO::JSerialisable;
30  using JIO::JReader;
31  using JIO::JWriter;
33  using JLANG::JException;
34  using JTOOLS::JFunctional;
37  using JTOOLS::JMultiMap;
38  using JTOOLS::JMultiKey;
39 
40 
41  /**
42  * Multi-dimensional CDF table for arrival time of Cherenkov light.
43  * This class can be used to determine the number of photo-electrons as a function of
44  * the values of the leading parameter values and to generate random photon arrival times.
45  *
46  * N.B. The transformation of the PDF is assumed to be linear.
47  */
48  template<class JFunction1D_t,
49  class JMaplist_t,
51  class JCDFTable :
52  public JSerialisable,
53  public JObjectBinaryIO< JCDFTable<JFunction1D_t, JMaplist_t, JDistance_t> >,
54  public JFunctional<>
55  {
56  public:
57 
58  typedef typename JFunction1D_t::argument_type argument_type;
59  typedef typename JFunction1D_t::result_type result_type;
60  typedef typename JFunction1D_t::value_type value_type;
61 
63 
66 
68 
70 
74 
75 
76  /**
77  * Default constructor.
78  */
80  transformer(transformer_type::getClone())
81  {}
82 
83 
84  /**
85  * Constructor.
86  *
87  * \param input multi-dimensional PDF
88  * \param eps minimal step size for CDF
89  */
90  template<class JPDF_t, class JPDFMaplist_t, class JPDFDistance_t>
92  const typename JPDF_t::ordinate_type eps = JMATH::zero) :
93  transformer(transformer_type::getClone())
94  {
95  this->transformer.reset(input.transformer->clone());
96 
97  for (typename JTransformableMultiFunction<JPDF_t, JPDFMaplist_t, JPDFDistance_t>::super_const_iterator i = input.super_begin(); i != input.super_end(); ++i) {
98  this->insert((*i).getKey(), (*i).getValue(), eps);
99  }
100 
101  this->compile();
102  }
103 
104 
105  /**
106  * Constructor.
107  *
108  * \param input multi-dimensional histogram
109  * \param eps minimal step size for CDF
110  */
111  template<class JHistogram1D_t, class JHistogramMap_t, class JHistogramDistance_t>
113  const typename JHistogram1D_t::ordinate_type eps = JMATH::zero) :
114  transformer(transformer_type::getClone())
115  {
116  this->transformer.reset(input.transformer->clone());
117 
119 
120  this->compile();
121  }
122 
123 
124  /**
125  * Application of weight function.
126  *
127  * \param transformer function transformer
128  */
129  template<class JFunctionTransformer_t>
130  void transform(const JFunctionTransformer_t& transformer)
131  {
132  for (typename JMultiQuantile_t::super_iterator i = intensity.super_begin(); i != intensity.super_end(); ++i) {
133 
134  const typename transformer_type::array_type array = (*i).getKey();
135  JConstantFunction1D_t& function = (*i).getValue();
136 
137  const double W1 = this->transformer->getWeight(array);
138  const double W2 = transformer .getWeight(array);
139 
140  const result_type y = function(JMATH::zero);
141 
142  function = JConstantFunction1D_t(y * W1 / W2);
143  }
144 
145  this->transformer.reset(transformer.clone());
146  this->compile();
147  }
148 
149 
150  /**
151  * Get number of photo-electrons.
152  *
153  * \param x comma separated argument list
154  * \return number of photo-electrons
155  */
156  double getNPE(const argument_type x, ...) const
157  {
158  va_start(ap, x);
159 
160  buffer[0] = x;
161 
162  for (int i = 1; i != NUMBER_OF_DIMENSIONS - 1; ++i) {
163  buffer[i] = va_arg(ap, argument_type);
164  }
165 
166  va_end(ap);
167 
168  const double W = transformer->getWeight(buffer);
169  const double npe = intensity.evaluate(buffer.begin());
170 
171  return W * npe;
172  }
173 
174 
175  /**
176  * Generate arrival time.
177  *
178  * \param x comma seperated argument list (last value is random number between 0 and 1)
179  * \return arrival time
180  */
181  double getTime(const argument_type x, ...) const
182  {
183  va_start(ap, x);
184 
185  buffer[0] = x;
186 
187  for (int i = 1; i != NUMBER_OF_DIMENSIONS; ++i) {
188  buffer[i] = va_arg(ap, argument_type);
189  }
190 
191  va_end(ap);
192 
193  const argument_type y = function.evaluate(buffer.begin());
194 
195  return transformer->getXn(buffer, y);
196  }
197 
198 
199  /**
200  * Read CDF from input.
201  *
202  * \param in reader
203  * \return reader
204  */
206  {
207  in >> intensity;
208  in >> function;
209 
211 
212  if (buffer.read(in))
213  transformer.reset(buffer.clone());
214  else
216 
217  compile();
218 
219  intensity.setExceptionHandler(new typename JMultiQuantile_t::function_type::JDefaultResult(JMATH::zero));
220  function .setExceptionHandler(new typename JMultiFunction_t::function_type::JDefaultResult(JMATH::zero));
221 
222  return in;
223  }
224 
225 
226  /**
227  * Write CDF to output.
228  *
229  * \param out writer
230  * \return writer
231  */
232  virtual JWriter& write(JWriter& out) const
233  {
234  out << intensity;
235  out << function;
236 
237  return transformer->write(out);
238  }
239 
240 
241  JMultiQuantile_t intensity; // integrated PDF
242  JMultiFunction_t function; // normalised CDF
244 
245 
246  protected:
247  /**
248  * Function compilation.
249  */
250  virtual void do_compile()
251  {
252  intensity.compile();
253  function .compile();
254  }
255 
256 
257  /**
258  * Insert value at given multidimensional key.
259  *
260  * \param key multi-dimensional key
261  * \param value function or histogram
262  * \param eps minimal step size for CDF
263  */
264  template<class JPDF_t>
265  void insert(const multikey_t& key,
266  const JPDF_t& value,
267  const typename JPDF_t::ordinate_type eps)
268  {
269  try {
270 
271  const typename transformer_type::array_type array(key);
272 
273  JFunction1D_t buffer;
274 
275  const argument_type z = transformer->getXn(array, 1.0) - transformer->getXn(array, 0.0);
276  const result_type V = JTOOLS::makeCDF(value, buffer, eps);
277 
279  function .insert(key, buffer);
280  }
281  catch(const JException& error) {
283  }
284  }
285 
286 
287  /**
288  * Insert multi-dimensional histogram at multi-dimensional key.
289  *
290  * \param key multidimensional key
291  * \param histogram multidimensional histogram
292  * \param eps minimal step size for CDF
293  */
294  template<unsigned int N, class JKey_t, class JValue_t, class JHistogramMaplist_t, class JHistogramDistance_t>
295  void insert(const JMultiKey<N, JKey_t>& key,
297  const typename JValue_t::contents_type eps)
298  {
299  if (histogram.getSize() > 1) {
300 
301  for (typename JMultiMap<JKey_t, JValue_t, JHistogramMaplist_t, JHistogramDistance_t>::const_iterator j = histogram.begin(), i = j++; j != histogram.end(); ++i, ++j) {
302 
303  const JKey_t x = 0.5 * (i->first + j->first);
304 
305  insert(JMultiKey<N+1, JKey_t>(key, x), i->second, eps);
306  }
307  }
308  }
309 
310 
311  mutable va_list ap;
313  };
314 }
315 
316 #endif
void compile()
Compilation.
collection_type::ordinate_type ordinate_type
General exception.
Definition: JException.hh:40
Exceptions.
Interface for binary output.
Multi-dimensional CDF table for arrival time of Cherenkov light.
Definition: JCDFTable.hh:51
Auxiliary base class for storing and loading a single object to and from a binary file...
Transformable multidimensional function.
JTOOLS::JMultiFunction< JConstantFunction1D_t, JMaplist_t, JDistance_t > JMultiQuantile_t
Definition: JCDFTable.hh:72
const_iterator begin() const
get iterator to begin of data
Definition: JArray.hh:172
Template class for distance evaluation.
Definition: JDistance.hh:24
void transform(const JFunctionTransformer_t &transformer)
Application of weight function.
Definition: JCDFTable.hh:130
JLANG::JSharedPointer< transformer_type > transformer
Definition: JCDFTable.hh:243
Template definition of transformer of the Probability Density Functions of the time response of a PMT...
JLANG::JSharedPointer< transformer_type > transformer
JCDFTable(const JTransformableMultiFunction< JPDF_t, JPDFMaplist_t, JPDFDistance_t > &input, const typename JPDF_t::ordinate_type eps=JMATH::zero)
Constructor.
Definition: JCDFTable.hh:91
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:94
Definition of zero value for any class.
JTOOLS::JArray< NUMBER_OF_DIMENSIONS, argument_type > buffer
Definition: JCDFTable.hh:312
void insert(const JMultiKey< N, JKey_t > &key, const JMultiMap< JKey_t, JValue_t, JHistogramMaplist_t, JHistogramDistance_t > &histogram, const typename JValue_t::contents_type eps)
Insert multi-dimensional histogram at multi-dimensional key.
Definition: JCDFTable.hh:295
Forward declaration of template JMultiKey class.
Definition: JMultiKey.hh:29
JTOOLS::JMultiFunction< JFunction1D_t, JMaplist_t, JDistance_t > JMultiFunction_t
Definition: JCDFTable.hh:73
Forward declaration of binary output.
virtual void do_compile()
Function compilation.
Definition: JCDFTable.hh:250
Template implementation of function object in one dimension returning a constant value.
JFunction1D_t::argument_type argument_type
Definition: JCDFTable.hh:58
static result_type getValue(const JFunctional &function, const argument_type *pX)
Recursive function value evaluation.
Definition: JFunctional.hh:106
The template JSharedPointer class can be used to share a pointer to an object.
Transformable multidimensional histogram.
JFunction1D_t::value_type value_type
Definition: JCDFTable.hh:60
Template definition of function object interface.
Definition: JFunctional.hh:31
JMultiKey< NUMBER_OF_DIMENSIONS-1, argument_type > multikey_t
Definition: JCDFTable.hh:69
This include file contains various recursive methods to operate on multi-dimensional collections...
static JMultiMapTransformer * getClone()
Get clone of default transformer.
JCDFTable(const JTransformableMultiHistogram< JHistogram1D_t, JHistogramMap_t, JHistogramDistance_t > &input, const typename JHistogram1D_t::ordinate_type eps=JMATH::zero)
Constructor.
Definition: JCDFTable.hh:112
Interface for binary input.
transformablemultifunction_t::transformer_type transformer_type
Definition: JCDFTable.hh:65
JCDFTable()
Default constructor.
Definition: JCDFTable.hh:79
JContainer_t::ordinate_type makeCDF(const JContainer_t &input, JMappableCollection< JKey_t, JValue_t > &output, const typename JContainer_t::ordinate_type eps=JMATH::zero)
Conversion of data points to cumulative probability distribition (CDF).
transformablemultifunction_t::multimap_type multimap_type
Definition: JCDFTable.hh:64
JTOOLS::JConstantFunction1D< double, argument_type > JConstantFunction1D_t
Definition: JCDFTable.hh:71
double getTime(const argument_type x,...) const
Generate arrival time.
Definition: JCDFTable.hh:181
JFunction1D_t::result_type result_type
Definition: JCDFTable.hh:59
void insert(const multikey_t &key, const JPDF_t &value, const typename JPDF_t::ordinate_type eps)
Insert value at given multidimensional key.
Definition: JCDFTable.hh:265
double getNPE(const argument_type x,...) const
Get number of photo-electrons.
Definition: JCDFTable.hh:156
JReader & read(JReader &in)
Read CDF from input.
Definition: JCDFTable.hh:205
JTransformableMultiFunction< JFunction1D_t, JMaplist_t, JDistance_t > transformablemultifunction_t
Definition: JCDFTable.hh:62
void insert(const JMultiFunction< JPDF_t, JPDFMaplist_t, JPDFDistance_t > &input)
Insert multidimensional input.
Interface for weight application and coordinate transformation of function.
JMultiQuantile_t intensity
Definition: JCDFTable.hh:241
virtual JWriter & write(JWriter &out) const
Write CDF to output.
Definition: JCDFTable.hh:232
JLANG::JSharedPointer< transformer_type > transformer
multifunction_type::super_const_iterator super_const_iterator
Multidimensional map.
Definition: JMultiMap.hh:46