Jpp  pmt_effective_area_update
the software that should make you happy
 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 "JIO/JSerialisable.hh"
5 #include "JIO/JObjectBinaryIO.hh"
6 #include "JLang/JException.hh"
8 #include "JMath/JZero.hh"
9 #include "JTools/JArray.hh"
10 #include "JTools/JMultiKey.hh"
14 #include "JTools/JToolsToolkit.hh"
16 
17 
18 /**
19  * \author mdejong
20  */
21 
22 namespace JPHYSICS {}
23 namespace JPP { using namespace JPHYSICS; }
24 
25 namespace JPHYSICS {
26 
27  using JIO::JSerialisable;
28  using JIO::JReader;
29  using JIO::JWriter;
31  using JLANG::JException;
32  using JTOOLS::JFunctional;
35  using JTOOLS::JMultiMap;
36  using JTOOLS::JMultiKey;
37  using JTOOLS::JArray;
38 
39 
40  /**
41  * Multi-dimensional CDF table for arrival time of Cherenkov light.
42  * This class can be used to determine the number of photo-electrons as a function of
43  * the values of the leading parameter values and to generate random photon arrival times.
44  *
45  * N.B. The transformation of the PDF is assumed to be linear.
46  */
47  template<class JFunction1D_t,
48  class JMaplist_t,
50  class JCDFTable :
51  public JSerialisable,
52  public JObjectBinaryIO< JCDFTable<JFunction1D_t, JMaplist_t, JDistance_t> >,
53  public JFunctional<>
54  {
55  public:
56 
57  typedef typename JFunction1D_t::argument_type argument_type;
58  typedef typename JFunction1D_t::result_type result_type;
59  typedef typename JFunction1D_t::value_type value_type;
60 
62 
65 
67 
69 
73 
74 
75  /**
76  * Default constructor.
77  */
79  transformer(transformer_type::getClone())
80  {}
81 
82 
83  /**
84  * Constructor.
85  *
86  * \param input multi-dimensional PDF
87  * \param eps minimal step size for CDF
88  */
89  template<class JPDF_t, class JPDFMaplist_t, class JPDFDistance_t>
91  const typename JPDF_t::ordinate_type eps = JMATH::zero) :
92  transformer(transformer_type::getClone())
93  {
94  this->transformer.reset(input.transformer->clone());
95 
96  for (typename JTransformableMultiFunction<JPDF_t, JPDFMaplist_t, JPDFDistance_t>::super_const_iterator i = input.super_begin(); i != input.super_end(); ++i) {
97  this->insert((*i).getKey(), (*i).getValue(), eps);
98  }
99 
100  this->compile();
101  }
102 
103 
104  /**
105  * Constructor.
106  *
107  * \param input multi-dimensional histogram
108  * \param eps minimal step size for CDF
109  */
110  template<class JHistogram1D_t, class JHistogramMap_t, class JHistogramDistance_t>
112  const typename JHistogram1D_t::ordinate_type eps = JMATH::zero) :
113  transformer(transformer_type::getClone())
114  {
115  this->transformer.reset(input.transformer->clone());
116 
118 
119  this->compile();
120  }
121 
122 
123  /**
124  * Application of weight function.
125  *
126  * \param transformer function transformer
127  */
128  template<class JFunctionTransformer_t>
129  void transform(const JFunctionTransformer_t& transformer)
130  {
131  for (typename JMultiQuantile_t::super_iterator i = intensity.super_begin(); i != intensity.super_end(); ++i) {
132 
133  const typename transformer_type::array_type array = (*i).getKey();
134  JConstantFunction1D_t& function = (*i).getValue();
135 
136  const double W1 = this->transformer->getWeight(array);
137  const double W2 = transformer .getWeight(array);
138 
139  const result_type y = function(JMATH::zero);
140 
141  function = JConstantFunction1D_t(y * W1 / W2);
142  }
143 
144  this->transformer.reset(transformer.clone());
145  this->compile();
146  }
147 
148 
149  /**
150  * Get number of photo-electrons.
151  *
152  * \param args comma separated argument list
153  * \return number of photo-electrons
154  */
155  template<class ...Args>
156  double getNPE(const Args& ...args) const
157  {
158  const JArray<NUMBER_OF_DIMENSIONS - 1, argument_type> buffer(args...);
159 
160  const double W = transformer->getWeight(buffer);
161  const double npe = intensity.evaluate(buffer.data());
162 
163  return W * npe;
164  }
165 
166 
167  /**
168  * Generate arrival time.
169  *
170  * \param args comma seperated argument list (last value is random number between 0 and 1)
171  * \return arrival time
172  */
173  template<class ...Args>
174  double getTime(const Args& ...args) const
175  {
176  const JArray<NUMBER_OF_DIMENSIONS, argument_type> buffer(args...);
177 
178  const argument_type y = function.evaluate(buffer.data());
179 
180  return transformer->getXn(buffer, y);
181  }
182 
183 
184  /**
185  * Read CDF from input.
186  *
187  * \param in reader
188  * \return reader
189  */
190  virtual JReader& read(JReader& in) override
191  {
192  in >> intensity;
193  in >> function;
194 
196 
197  if (buffer.read(in))
198  transformer.reset(buffer.clone());
199  else
201 
202  compile();
203 
204  intensity.setExceptionHandler(new typename JMultiQuantile_t::function_type::JDefaultResult(JMATH::zero));
205  function .setExceptionHandler(new typename JMultiFunction_t::function_type::JDefaultResult(JMATH::zero));
206 
207  return in;
208  }
209 
210 
211  /**
212  * Write CDF to output.
213  *
214  * \param out writer
215  * \return writer
216  */
217  virtual JWriter& write(JWriter& out) const override
218  {
219  out << intensity;
220  out << function;
221 
222  return transformer->write(out);
223  }
224 
225 
226  JMultiQuantile_t intensity; // integrated PDF
227  JMultiFunction_t function; // normalised CDF
229 
230 
231  protected:
232  /**
233  * Function compilation.
234  */
235  virtual void do_compile() override
236  {
237  intensity.compile();
238  function .compile();
239  }
240 
241 
242  /**
243  * Insert value at given multidimensional key.
244  *
245  * \param key multi-dimensional key
246  * \param value function or histogram
247  * \param eps minimal step size for CDF
248  */
249  template<class JPDF_t>
250  void insert(const multikey_t& key,
251  const JPDF_t& value,
252  const typename JPDF_t::ordinate_type eps)
253  {
254  try {
255 
256  const typename transformer_type::array_type array(key);
257 
258  JFunction1D_t buffer;
259 
260  const argument_type z = transformer->getXn(array, 1.0) - transformer->getXn(array, 0.0);
261  const result_type V = JTOOLS::makeCDF(value, buffer, eps);
262 
264  function .insert(key, buffer);
265  }
266  catch(const JException& error) {
268  }
269  }
270 
271 
272  /**
273  * Insert multi-dimensional histogram at multi-dimensional key.
274  *
275  * \param key multidimensional key
276  * \param histogram multidimensional histogram
277  * \param eps minimal step size for CDF
278  */
279  template<unsigned int N, class JKey_t, class JValue_t, class JHistogramMaplist_t, class JHistogramDistance_t>
280  void insert(const JMultiKey<N, JKey_t>& key,
282  const typename JValue_t::contents_type eps)
283  {
284  if (histogram.getSize() > 1) {
285 
286  for (typename JMultiMap<JKey_t, JValue_t, JHistogramMaplist_t, JHistogramDistance_t>::const_iterator j = histogram.begin(), i = j++; j != histogram.end(); ++i, ++j) {
287 
288  const JKey_t x = 0.5 * (i->first + j->first);
289 
290  insert(JMultiKey<N+1, JKey_t>(key, x), i->second, eps);
291  }
292  }
293  }
294  };
295 }
296 
297 #endif
virtual JReader & read(JReader &in) override
Read CDF from input.
Definition: JCDFTable.hh:190
void compile()
Compilation.
collection_type::ordinate_type ordinate_type
General exception.
Definition: JException.hh:23
Exceptions.
Interface for binary output.
Multi-dimensional CDF table for arrival time of Cherenkov light.
Definition: JCDFTable.hh:50
double getNPE(const Args &...args) const
Get number of photo-electrons.
Definition: JCDFTable.hh:156
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:71
Template class for distance evaluation.
Definition: JDistance.hh:24
void transform(const JFunctionTransformer_t &transformer)
Application of weight function.
Definition: JCDFTable.hh:129
JLANG::JSharedPointer< transformer_type > transformer
Definition: JCDFTable.hh:228
Template definition of transformer of the probability density function (PDF) of the time response of ...
double getTime(const Args &...args) const
Generate arrival time.
Definition: JCDFTable.hh:174
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:90
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
Definition of zero value for any class.
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:280
Multidimensional key.
Definition: JMultiKey.hh:34
JTOOLS::JMultiFunction< JFunction1D_t, JMaplist_t, JDistance_t > JMultiFunction_t
Definition: JCDFTable.hh:72
Forward declaration of binary output.
Template implementation of function object in one dimension returning a constant value.
JFunction1D_t::argument_type argument_type
Definition: JCDFTable.hh:57
static result_type getValue(const JFunctional &function, const argument_type *pX)
Recursive function value evaluation.
Definition: JFunctional.hh:107
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:59
Template definition of function object interface.
Definition: JFunctional.hh:32
JMultiKey< NUMBER_OF_DIMENSIONS-1, argument_type > multikey_t
Definition: JCDFTable.hh:68
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:111
Interface for binary input.
One dimensional array of template objects with fixed length.
Definition: JArray.hh:40
transformablemultifunction_t::transformer_type transformer_type
Definition: JCDFTable.hh:64
JCDFTable()
Default constructor.
Definition: JCDFTable.hh:78
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:63
JTOOLS::JConstantFunction1D< double, argument_type > JConstantFunction1D_t
Definition: JCDFTable.hh:70
JFunction1D_t::result_type result_type
Definition: JCDFTable.hh:58
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:250
JTransformableMultiFunction< JFunction1D_t, JMaplist_t, JDistance_t > transformablemultifunction_t
Definition: JCDFTable.hh:61
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:226
int j
Definition: JPolint.hh:666
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:40
JLANG::JSharedPointer< transformer_type > transformer
multifunction_type::super_const_iterator super_const_iterator
virtual JWriter & write(JWriter &out) const override
Write CDF to output.
Definition: JCDFTable.hh:217
const_pointer data() const
Get pointer to data.
Definition: JArray.hh:282
virtual void do_compile() override
Function compilation.
Definition: JCDFTable.hh:235
Multidimensional map.
Definition: JMultiMap.hh:51