Jpp  18.5.2
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 #include "JTools/JHistogramMap.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  using JTOOLS::JArray;
41 
42 
43  /**
44  * Multi-dimensional CDF table for arrival time of Cherenkov light.
45  * This class can be used to determine the number of photo-electrons as a function of
46  * the values of the leading parameter values and to generate random photon arrival times.
47  *
48  * N.B. The transformation of the PDF is assumed to be linear.
49  */
50  template<class JFunction1D_t,
51  class JMaplist_t,
53  class JCDFTable :
54  public JSerialisable,
55  public JObjectBinaryIO< JCDFTable<JFunction1D_t, JMaplist_t, JDistance_t> >,
56  public JFunctional<>
57  {
58  public:
59 
60  typedef typename JFunction1D_t::argument_type argument_type;
61  typedef typename JFunction1D_t::result_type result_type;
62  typedef typename JFunction1D_t::value_type value_type;
63 
65 
68 
70 
72 
76 
77 
78  /**
79  * Default constructor.
80  */
82  transformer(transformer_type::getClone())
83  {}
84 
85 
86  /**
87  * Constructor.
88  *
89  * \param input multi-dimensional PDF
90  * \param eps minimal step size for CDF
91  */
92  template<class __JFunction_t, class __JMaplist_t, class __JDistance_t>
94  const typename __JFunction_t::ordinate_type eps = JMATH::zero) :
95  transformer(transformer_type::getClone())
96  {
97  this->transformer.reset(input.transformer->clone());
98 
99  for (auto i = input.super_begin(); i != input.super_end(); ++i) {
100  this->insert((*i).getKey(), (*i).getValue(), eps);
101  }
102 
103  this->compile();
104  }
105 
106 
107  /**
108  * Constructor.
109  *
110  * \param input multi-dimensional histogram
111  * \param eps minimal step size for CDF
112  */
113  template<class JHistogram_t, class __JMaplist_t, class __JDistance_t>
115  const typename JHistogram_t::ordinate_type eps = JMATH::zero) :
116  transformer(transformer_type::getClone())
117  {
118  this->transformer.reset(input.transformer->clone());
119 
121 
122  this->compile();
123  }
124 
125 
126  /**
127  * Application of weight function.
128  *
129  * \param transformer function transformer
130  */
131  template<class JFunctionTransformer_t>
132  void transform(const JFunctionTransformer_t& transformer)
133  {
134  for (typename JMultiQuantile_t::super_iterator i = intensity.super_begin(); i != intensity.super_end(); ++i) {
135 
136  const typename transformer_type::array_type array = (*i).getKey();
137  JConstantFunction1D_t& function = (*i).getValue();
138 
139  const double W1 = this->transformer->getWeight(array);
140  const double W2 = transformer .getWeight(array);
141 
142  const result_type y = function(JMATH::zero);
143 
144  function = JConstantFunction1D_t(y * W1 / W2);
145  }
146 
147  this->transformer.reset(transformer.clone());
148  this->compile();
149  }
150 
151 
152  /**
153  * Get number of photo-electrons.
154  *
155  * \param args comma separated argument list
156  * \return number of photo-electrons
157  */
158  template<class ...Args>
159  double getNPE(const Args& ...args) const
160  {
161  const JArray<NUMBER_OF_DIMENSIONS - 1, argument_type> buffer(args...);
162 
163  const double W = transformer->getWeight(buffer);
164  const double npe = intensity.evaluate(buffer.data());
165 
166  return W * npe;
167  }
168 
169 
170  /**
171  * Generate arrival time.
172  *
173  * \param args comma seperated argument list (last value is random number between 0 and 1)
174  * \return arrival time
175  */
176  template<class ...Args>
177  double getTime(const Args& ...args) const
178  {
179  const JArray<NUMBER_OF_DIMENSIONS, argument_type> buffer(args...);
180 
181  const argument_type y = function.evaluate(buffer.data());
182 
183  return transformer->getXn(buffer, y);
184  }
185 
186 
187  /**
188  * Read CDF from input.
189  *
190  * \param in reader
191  * \return reader
192  */
193  virtual JReader& read(JReader& in) override
194  {
195  in >> intensity;
196  in >> function;
197 
199 
200  if (buffer.read(in))
201  transformer.reset(buffer.clone());
202  else
204 
205  compile();
206 
207  intensity.setExceptionHandler(new typename JMultiQuantile_t::function_type::JDefaultResult(JMATH::zero));
208  function .setExceptionHandler(new typename JMultiFunction_t::function_type::JDefaultResult(JMATH::zero));
209 
210  return in;
211  }
212 
213 
214  /**
215  * Write CDF to output.
216  *
217  * \param out writer
218  * \return writer
219  */
220  virtual JWriter& write(JWriter& out) const override
221  {
222  out << intensity;
223  out << function;
224 
225  return transformer->write(out);
226  }
227 
228 
229  JMultiQuantile_t intensity; // integrated PDF
230  JMultiFunction_t function; // normalised CDF
232 
233 
234  protected:
235  /**
236  * Function compilation.
237  */
238  virtual void do_compile() override
239  {
240  intensity.compile();
241  function .compile();
242  }
243 
244 
245  /**
246  * Insert value at given multidimensional key.
247  *
248  * \param key multi-dimensional key
249  * \param value function or histogram
250  * \param eps minimal step size for method JTOOLS::makeCDF
251  */
252  template<class JValue_t>
253  void insert(const multikey_type& key,
254  const JValue_t& value,
255  const typename JValue_t::ordinate_type eps)
256  {
257  using namespace JPP;
258 
259  try {
260 
261  const typename transformer_type::array_type array(key);
262 
263  JFunction1D_t buffer;
264 
265  const argument_type z = transformer->getXn(array, 1.0) - transformer->getXn(array, 0.0);
266  const result_type V = makeCDF(value, buffer, eps);
267 
269  function .insert(key, buffer);
270  }
271  catch(const JException& error) {
273  }
274  }
275 
276 
277  /**
278  * Insert multi-dimensional histogram at multi-dimensional key.
279  *
280  * \param key multidimensional key
281  * \param value multidimensional histogram
282  * \param eps minimal step size for CDF
283  */
284  template<unsigned int N,
285  class __JAbscissa_t,
286  class __JContents_t,
287  template<class, class, class> class __JMap_t,
288  class __JDistance_t>
291  const __JContents_t eps)
292  {
293  if (value.getSize() > 1) {
294 
295  for (auto j = value.begin(), i = j++; j != value.end(); ++i, ++j) {
296 
297  const __JAbscissa_t x = 0.5 * (i->first + j->first);
298 
299  insert(JMultiKey<N+1, __JAbscissa_t>(key, x), i->second, eps);
300  }
301  }
302  }
303  };
304 }
305 
306 #endif
virtual JReader & read(JReader &in) override
Read CDF from input.
Definition: JCDFTable.hh:193
void compile()
Compilation.
General exception.
Definition: JException.hh:24
Exceptions.
Interface for binary output.
JMultiKey< NUMBER_OF_DIMENSIONS-1, argument_type > multikey_type
Definition: JCDFTable.hh:71
Multi-dimensional CDF table for arrival time of Cherenkov light.
Definition: JCDFTable.hh:53
double getNPE(const Args &...args) const
Get number of photo-electrons.
Definition: JCDFTable.hh:159
Auxiliary base class for storing and loading a single object to and from a binary file...
Histogram map.
Transformable multidimensional function.
JTOOLS::JMultiFunction< JConstantFunction1D_t, JMaplist_t, JDistance_t > JMultiQuantile_t
Definition: JCDFTable.hh:74
Template class for distance evaluation.
Definition: JDistance.hh:24
void transform(const JFunctionTransformer_t &transformer)
Application of weight function.
Definition: JCDFTable.hh:132
JLANG::JSharedPointer< transformer_type > transformer
Definition: JCDFTable.hh:231
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:177
JLANG::JSharedPointer< transformer_type > transformer
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
V(JDAQEvent-JTriggerReprocessor)*1.0/(JDAQEvent+1.0e-10)
Definition of zero value for any class.
void insert(const JMultiKey< N, argument_type > &key, const JHistogramMap< __JAbscissa_t, __JContents_t, __JMap_t, __JDistance_t > &value, const __JContents_t eps)
Insert multi-dimensional histogram at multi-dimensional key.
Definition: JCDFTable.hh:289
Multidimensional key.
Definition: JMultiKey.hh:34
JTOOLS::JMultiFunction< JFunction1D_t, JMaplist_t, JDistance_t > JMultiFunction_t
Definition: JCDFTable.hh:75
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:60
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.
void insert(const JMultiFunction< __JFunction_t, __JMaplist_t, __JDistance_t > &input)
Insert multidimensional input.
JFunction1D_t::value_type value_type
Definition: JCDFTable.hh:62
Template definition of function object interface.
Definition: JFunctional.hh:32
JCDFTable(const JTransformableMultiFunction< __JFunction_t, __JMaplist_t, __JDistance_t > &input, const typename __JFunction_t::ordinate_type eps=JMATH::zero)
Constructor.
Definition: JCDFTable.hh:93
This include file contains various recursive methods to operate on multi-dimensional collections...
static JMultiMapTransformer * getClone()
Get clone of default transformer.
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:67
JCDFTable()
Default constructor.
Definition: JCDFTable.hh:81
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:66
JTOOLS::JConstantFunction1D< double, argument_type > JConstantFunction1D_t
Definition: JCDFTable.hh:73
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
JFunction1D_t::result_type result_type
Definition: JCDFTable.hh:61
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:48
void insert(const multikey_type &key, const JValue_t &value, const typename JValue_t::ordinate_type eps)
Insert value at given multidimensional key.
Definition: JCDFTable.hh:253
JTransformableMultiFunction< JFunction1D_t, JMaplist_t, JDistance_t > transformablemultifunction_t
Definition: JCDFTable.hh:64
Interface for weight application and coordinate transformation of function.
JMultiQuantile_t intensity
Definition: JCDFTable.hh:229
int j
Definition: JPolint.hh:792
JLANG::JSharedPointer< transformer_type > transformer
virtual JWriter & write(JWriter &out) const override
Write CDF to output.
Definition: JCDFTable.hh:220
const_pointer data() const
Get pointer to data.
Definition: JArray.hh:284
virtual void do_compile() override
Function compilation.
Definition: JCDFTable.hh:238
Multidimensional map.
Definition: JMultiMap.hh:52
JCDFTable(const JTransformableMultiHistogram< JHistogram_t, __JMaplist_t, __JDistance_t > &input, const typename JHistogram_t::ordinate_type eps=JMATH::zero)
Constructor.
Definition: JCDFTable.hh:114