Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPMTParametersMap.hh
Go to the documentation of this file.
1 #ifndef __JDETECTOR__JPMTPARAMETERSMAP__
2 #define __JDETECTOR__JPMTPARAMETERSMAP__
3 
4 #include <istream>
5 #include <ostream>
6 #include <sstream>
7 #include <fstream>
8 #include <map>
9 
14 #include "JLang/JException.hh"
15 #include "JLang/JThrow.hh"
16 #include "JLang/JStringStream.hh"
17 #include "JLang/JObjectStreamIO.hh"
18 #include "Jeep/JComment.hh"
19 #include "Jeep/JProperties.hh"
20 #include "JSystem/JStat.hh"
21 
22 
23 /**
24  * \author mdejong
25  * \file
26  * Map of PMT parameters.
27  */
28 
29 namespace JDETECTOR {}
30 namespace JPP { using namespace JDETECTOR; }
31 
32 namespace JDETECTOR {
33 
39  using JLANG::JException;
40  using JLANG::JThrow;
41  using JEEP::JComment;
42 
43 
44  /**
45  * Type definition of map PMT identifier to PMT parameters.
46  */
48 
49 
50  /**
51  * Auxiliary class for map of PMT parameters.
52  *
53  * Input syntax as follows:
54  * <pre>
55  * QE=<value>
56  * mu=<value>
57  *
58  * %.<key>=<value>
59  *
60  * PMT=<module> <pmt> <PMT parameters>
61  *
62  * pmt=<module> <pmt> <key>=<value>[, <key>=<value>]
63  *
64  * file=<file name>
65  *
66  * </pre>
67  * where
68  * - QE refers to the global quantum efficiency;
69  * - mu refers to the expectation value for number of photo-electrons given two-fold (or higher) coincidence rate;
70  * - '\%' to the default values;
71  * - <module> to the module identifier;
72  * - <pmt> to the readout channel;
73  * - <key> to one of the data members of the JDETECTOR::JPMTParameters data structure; and
74  * - <value> to the corresponding value.
75  *
76  * The global QE is applied as a multiplicative factor to the default and PMT specific QEs.
77  *
78  * The default values are provided by the method getDefaultPMTParameters and
79  * can also be modified using method setDefaultPMTParameters.
80  * If no optional values are given, the default values are maintained.
81  *
82  * Finally, <PMT parameters> corresponds to the list of PMT parameter values that should
83  * be compatible with the I/O methods of the JDETECTOR::JPMTParameters data structure.
84  *
85  * Note that multiple input sequences should be seperated by a semicolumn ';' or be terminated by a newline.
86  * When multiple input sequences appear for the same target, the last sequence prevails.
87  */
89  public JPMTParametersMap_t,
90  public JObjectStreamIO<JPMTParametersMap>,
91  public JThrow<JPMTParametersMap>
92  {
93  public:
94  /**
95  * Constructor.
96  *
97  * \param parameters PMT parameters
98  */
101  QE(1.0),
102  mu(0.0)
103  {
104  setDefaultPMTParameters(parameters);
105  }
106 
107 
108  /**
109  * Constructor.
110  *
111  * \param file_name file name
112  */
113  JPMTParametersMap(const char* const file_name) :
115  QE(1.0),
116  mu(0.0)
117  {
118  using namespace std;
119 
120  ifstream in(file_name);
121 
122  in >> *this;
123 
124  in.close();
125  }
126 
127 
128  /**
129  * Get PMT parameters.\n
130  * This method returns the default PMT parameters if the parameters
131  * corresponding to the given PMT identifier have not been defined.
132  *
133  * Note that the value of QE as part of the return value has been scaled
134  * with the global QE of the PMT parameters map.
135  *
136  * \param id PMT identifier
137  * \return PMT parameters
138  */
140  {
141  static JPMTParameters parameters;
142 
143  JPMTParametersMap_t::const_iterator i = find(id);
144 
145  if (i != end())
146  parameters = i->second;
147  else
148  parameters = getDefaultPMTParameters();
149 
150  parameters.QE *= this->QE;
151 
152  return parameters;
153  }
154 
155 
156  /**
157  * Get QE of given PMT.
158  *
159  * \param id PMT identifier
160  * \return QE
161  */
162  double getQE(const JPMTIdentifier& id) const
163  {
164  return getPMTParameters(id).QE;
165  }
166 
167 
168  /**
169  * Get ratio of hit probabilities of given PMT.
170  *
171  * \param id PMT identifier
172  * \return ratio
173  */
174  double getHitProbability(const JPMTIdentifier& id) const
175  {
177 
178  return getHitProbability(getQE(id), this->mu);
179  }
180 
181 
182  /**
183  * Get default PMT parameters.
184  *
185  * \return PMT parameters
186  */
188  {
189  return defaultPMTParameters;
190  }
191 
192 
193  /**
194  * Set default PMT parameters.
195  *
196  * \param parameters PMT parameters
197  */
198  void setDefaultPMTParameters(const JPMTParameters& parameters)
199  {
200  defaultPMTParameters = parameters;
201  }
202 
203 
204  /**
205  * Get global QE.
206  *
207  * \return QE
208  */
209  double getQE() const
210  {
211  return this->QE;
212  }
213 
214 
215  /**
216  * Set global QE.
217  *
218  * \param QE QE
219  */
220  void setQE(const double QE)
221  {
222  this->QE = QE;
223  }
224 
225 
226  /**
227  * Get expectation value for number of photo-electrons given two-fold (or higher) coincidence rate.
228  *
229  * \return expectation value
230  */
231  double getMu() const
232  {
233  return this->mu;
234  }
235 
236 
237  /**
238  * Check validity of PMT parameters.
239  *
240  * \return true if valid; else false
241  */
242  bool is_valid() const
243  {
244  try {
245 
246  if (this->QE < 0) {
247  THROW(JException, "Invalid global QE " << QE);
248  }
249 
250  if (this->mu < 0) {
251  THROW(JException, "Invalid expectation value number of photo-electrons " << mu);
252  }
253 
255  THROW(JException, "Invalid default PMT parameters");
256  }
257 
258  for (const_iterator i = this->begin(); i != this->end(); ++i) {
259 
260  if (!i->second.is_valid()) {
261  THROW(JException, "Invalid PMT parameters at " << i->first);
262  }
263 
264  if (i->second.QE * this->QE > 1.0 ||
265  i->second.QE * this->QE < 0.0) {
266  THROW(JException, "Invalid PMT QE at " << i->first << ' ' << i->second.QE * this->QE);
267  }
268  }
269 
270  return true;
271  }
272  catch(const JException& error) {
273 
274  Throw(error);
275 
276  return false;
277  }
278  }
279 
280 
281  /**
282  * Convert the hit probabilities to QEs for given expectation value.
283  *
284  * The expectation value corresponds to the number of photo-electrons given
285  * the multiplicity range of coincidences as specified at JCalibrateK40.cc.\n
286  * The survival probability is taken into account in the conversion
287  * (see method JDETECTOR::getSurvivalProbability).
288  *
289  * \param mu expectation value
290  */
291  void convertHitProbabilityToQE(const double mu)
292  {
293  using namespace std;
295  using JDETECTOR::getQE;
296 
297  this->mu = mu;
298 
299  const double Pmax = getMaximalHitProbability(this->mu);
300 
301  for (JPMTParametersMap::const_iterator i = this->begin(); i != this->end(); ++i) {
302  if (i->second.QE > Pmax) {
303  THROW(JValueOutOfRange, "Hit probability PMT " << i->first << ' ' << i->second.QE << " > maximum probability given expectation value " << this->mu << endl);
304  }
305  }
306 
307  for (JPMTParametersMap::iterator i = this->begin(); i != this->end(); ++i) {
308 
309  const double P = getSurvivalProbability(i->second);
310 
311  if (i->second.QE > 0.0 && P > 0.0)
312  i->second.QE = getQE(i->second.QE, this->mu) / P;
313  else
314  i->second.QE = 0.0;
315  }
316  }
317 
318 
319  /**
320  * Stream input
321  *
322  * \param in input stream
323  * \param object PMT parameters map
324  * \return input stream
325  */
326  friend inline std::istream& operator>>(std::istream& in, JPMTParametersMap& object)
327  {
328  using namespace std;
329  using namespace JPP;
330 
331  JStringStream is(in);
332 
333  if (getFileStatus(is.str().c_str())) {
334  is.load();
335  }
336 
337  JProperties properties(getEquationParameters(), 1);
338 
340  JProperties demo = object.defaultPMTParameters.getProperties();
341 
342  properties["PMT"] = static_cast<JPMTParametersMap_t&>(object);
343  properties["pmt"] = helper;
344  properties["%"] = demo;
345  properties["QE"] = object.QE;
346  properties["mu"] = object.mu;
347 
348  is >> object.comment;
349  is >> properties;
350 
351  return in;
352  }
353 
354 
355  /**
356  * Stream output
357  *
358  * \param out output stream
359  * \param object PMT parameters map
360  * \return output stream
361  */
362  friend inline std::ostream& operator<<(std::ostream& out, const JPMTParametersMap& object)
363  {
364  using namespace JLANG;
365 
366  JProperties properties(getEquationParameters());
367 
368  JProperties demo = object.defaultPMTParameters.getProperties();
369 
370  properties["PMT"] = static_cast<const JPMTParametersMap_t&>(object);
371  properties["%"] = demo;
372 
373  out << object.comment;
376  out << properties;
377 
378  return out;
379  }
380 
381 
382  /**
383  * Get equation parameters.
384  *
385  * \return equation parameters
386  */
388  {
389  static JEquationParameters parameters;
390 
391  return parameters;
392  }
393 
394 
395  /**
396  * Set equation parameters.
397  *
398  * \param equation equation parameters
399  */
400  static inline void setEquationParameters(const JEquationParameters& equation)
401  {
402  getEquationParameters() = equation;
403  }
404 
405 
407 
408  protected:
409  /**
410  * Default PMT parameters.
411  */
413 
414  /**
415  * Global QE.
416  */
417  double QE;
418 
419 
420  /**
421  * Expectation value for number of photo-electrons given two-fold (or higher) coincidence rate.
422  */
423  double mu;
424 
425  private:
426  /**
427  * Auxiliary class for I/O of PMT parameters map.
428  */
430  public:
431  /**
432  * Constructor.
433  *
434  * \param __object PMT parameters map
435  * \param __equation equation parameters
436  */
438  const JEquationParameters& __equation) :
439  object (__object),
440  equation(__equation)
441  {}
442 
443 
444  /**
445  * Stream input PMT parameters map helper.
446  *
447  * \param in input stream
448  * \param helper PMT parameters map helper
449  * \return input stream
450  */
451  friend inline std::istream& operator>>(std::istream& in, JPMTParametersMapHelper& helper)
452  {
453  JPMTIdentifier id;
454 
455  if (in >> id) {
456  helper.object[id].getProperties().read(in);
457  }
458 
459  return in;
460  }
461 
462 
463  /**
464  * Stream output PMT parameters map helper.
465  *
466  * \param out output stream
467  * \param helper PMT parameters map helper
468  * \return output stream
469  */
470  friend inline std::ostream& operator<<(std::ostream& out, const JPMTParametersMapHelper& helper)
471  {
472  for (JPMTParametersMap::const_iterator i = helper.object.begin(); i != helper.object.end(); ++i) {
473 
474  out << i->first;
475 
476  i->second.getProperties(helper.equation).write(out);
477 
478  out << std::endl;
479  }
480 
481  return out;
482  }
483 
484  private:
487  };
488  };
489 }
490 
491 #endif
Exception for opening of file.
Definition: JException.hh:324
General exception.
Definition: JException.hh:40
static void setEquationParameters(const JEquationParameters &equation)
Set equation parameters.
Exception for reading of file.
Definition: JException.hh:342
Exceptions.
void setQE(const double QE)
Set global QE.
JPMTParametersMap(const JPMTParameters &parameters=JPMTParameters())
Constructor.
Auxiliary base class for controling the throwing of exceptions.
Definition: JThrow.hh:25
double getMu() const
Get expectation value for number of photo-electrons given two-fold (or higher) coincidence rate...
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
Utility class to parse parameter values.
Definition: JProperties.hh:484
const JPMTParameters & getPMTParameters(const JPMTIdentifier &id) const
Get PMT parameters.
static equation_type< T > make_equation(const std::string &key, const T &value)
Auxiliary method to create equation type.
Definition: JEquation.hh:117
Simple data structure to support I/O of equations (see class JLANG::JEquation).
double mu
Expectation value for number of photo-electrons given two-fold (or higher) coincidence rate...
friend std::ostream & operator<<(std::ostream &out, const JPMTParametersMap &object)
Stream output.
void convertHitProbabilityToQE(const double mu)
Convert the hit probabilities to QEs for given expectation value.
std::map< JPMTIdentifier, JPMTParameters > JPMTParametersMap_t
Type definition of map PMT identifier to PMT parameters.
JPMTParametersMapHelper(JPMTParametersMap &__object, const JEquationParameters &__equation)
Constructor.
Utility class to parse parameter values.
Auxiliary class for I/O of PMT parameters map.
double getQE() const
Get global QE.
double getQE(const double R, const double mu)
Get QE for given ratio of hit probabilities and expectation value of the number of photo-electrons...
bool is_valid() const
Check validity of PMT parameters.
const JPMTParameters & getDefaultPMTParameters() const
Get default PMT parameters.
Auxiliary class for map of PMT parameters.
JPMTParameters defaultPMTParameters
Default PMT parameters.
double getQE(const JPMTIdentifier &id) const
Get QE of given PMT.
friend std::istream & operator>>(std::istream &in, JPMTParametersMap &object)
Stream input.
double getHitProbability(const double QE, const double mu)
Get ratio of hit probabilities for given QE and expectation value of the number of photo-electrons...
static JEquationParameters & getEquationParameters()
Get equation parameters.
bool is_valid() const
Check validity of PMT parameters.
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
Auxiliary class for comment.
Definition: JComment.hh:34
double getSurvivalProbability(const JPMTParameters &parameters)
Probability that a one photo-electron hit survives the simulation of the PMT assuming QE = 1...
JLANG::JEquationFacet setequation
Type definition of stream manipulator for equational I/O.
void setDefaultPMTParameters(const JPMTParameters &parameters)
Set default PMT parameters.
Exception handling.
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:144
Data structure for PMT parameters.
Auxiliary base class for storing and loading a single object to and from an ASCII file...
friend std::istream & operator>>(std::istream &in, JPMTParametersMapHelper &helper)
Stream input PMT parameters map helper.
JPMTParametersMap(const char *const file_name)
Constructor.
friend std::ostream & operator<<(std::ostream &out, const JPMTParametersMapHelper &helper)
Stream output PMT parameters map helper.
double getMaximalHitProbability(const double mu)
Get maximal ratio of hit probabilities for given QE and expectation value of the number of photo-elec...
double QE
relative quantum efficiency
double getHitProbability(const JPMTIdentifier &id) const
Get ratio of hit probabilities of given PMT.
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
File status.