Jpp - the software that should make you happy
 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 which is reserved for the so-called light scaling;
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  * It is set (read overwritten) in the JTriggerEfficiency[RunByRun].sh scripts.
78  *
79  * The default values are provided by the method getDefaultPMTParameters and
80  * can also be modified using method setDefaultPMTParameters.
81  * If no optional values are given, the default values are maintained.
82  *
83  * Finally, <PMT parameters> corresponds to the list of PMT parameter values that should
84  * be compatible with the I/O methods of the JDETECTOR::JPMTParameters data structure.
85  *
86  * Note that multiple input sequences should be seperated by a semicolumn ';' or be terminated by a newline.
87  * When multiple input sequences appear for the same target, the last sequence prevails.
88  */
90  public JPMTParametersMap_t,
91  public JObjectStreamIO<JPMTParametersMap>,
92  public JThrow<JPMTParametersMap>
93  {
94  public:
95  /**
96  * Constructor.
97  *
98  * \param parameters PMT parameters
99  */
102  QE(1.0),
103  mu(0.0)
104  {
106  }
107 
108 
109  /**
110  * Constructor.
111  *
112  * \param file_name file name
113  */
114  JPMTParametersMap(const char* const file_name) :
116  QE(1.0),
117  mu(0.0)
118  {
119  using namespace std;
120 
121  ifstream in(file_name);
122 
123  in >> *this;
124 
125  in.close();
126  }
127 
128 
129  /**
130  * Get PMT parameters.\n
131  * This method returns the default PMT parameters if the parameters
132  * corresponding to the given PMT identifier have not been defined.
133  *
134  * Note that the value of QE as part of the return value has been scaled
135  * with the global QE of the PMT parameters map.
136  *
137  * \param id PMT identifier
138  * \return PMT parameters
139  */
141  {
142  static JPMTParameters parameters;
143 
144  JPMTParametersMap_t::const_iterator i = find(id);
145 
146  if (i != end())
147  parameters = i->second;
148  else
149  parameters = getDefaultPMTParameters();
150 
151  parameters.QE *= this->QE;
152 
153  return parameters;
154  }
155 
156 
157  /**
158  * Get QE of given PMT.
159  *
160  * \param id PMT identifier
161  * \return QE
162  */
163  double getQE(const JPMTIdentifier& id) const
164  {
165  return getPMTParameters(id).QE;
166  }
167 
168 
169  /**
170  * Get ratio of hit probabilities of given PMT.
171  *
172  * \param id PMT identifier
173  * \return ratio
174  */
175  double getHitProbability(const JPMTIdentifier& id) const
176  {
178 
179  return getHitProbability(getQE(id), this->mu);
180  }
181 
182 
183  /**
184  * Get default PMT parameters.
185  *
186  * \return PMT parameters
187  */
189  {
190  return defaultPMTParameters;
191  }
192 
193 
194  /**
195  * Get default PMT parameters.
196  *
197  * \return PMT parameters
198  */
200  {
201  return defaultPMTParameters;
202  }
203 
204 
205  /**
206  * Set default PMT parameters.
207  *
208  * \param parameters PMT parameters
209  */
211  {
213  }
214 
215 
216  /**
217  * Get global QE.
218  *
219  * \return QE
220  */
221  double getQE() const
222  {
223  return this->QE;
224  }
225 
226 
227  /**
228  * Set global QE.
229  *
230  * \param QE QE
231  */
232  void setQE(const double QE)
233  {
234  this->QE = QE;
235  }
236 
237 
238  /**
239  * Get expectation value for number of photo-electrons given two-fold (or higher) coincidence rate.
240  *
241  * \return expectation value
242  */
243  double getMu() const
244  {
245  return this->mu;
246  }
247 
248 
249  /**
250  * Check validity of PMT parameters.
251  *
252  * \return true if valid; else false
253  */
254  bool is_valid() const
255  {
256  try {
257 
258  if (this->QE < 0) {
259  THROW(JException, "Invalid global QE " << QE);
260  }
261 
262  if (this->mu < 0) {
263  THROW(JException, "Invalid expectation value number of photo-electrons " << mu);
264  }
265 
267  THROW(JException, "Invalid default PMT parameters");
268  }
269 
270  for (const_iterator i = this->begin(); i != this->end(); ++i) {
271 
272  if (!i->second.is_valid()) {
273  THROW(JException, "Invalid PMT parameters at " << i->first);
274  }
275 
276  if (i->second.QE * this->QE > 1.0 ||
277  i->second.QE * this->QE < 0.0) {
278  THROW(JException, "Invalid PMT QE at " << i->first << ' ' << i->second.QE * this->QE);
279  }
280  }
281 
282  return true;
283  }
284  catch(const JException& error) {
285 
286  Throw(error);
287 
288  return false;
289  }
290  }
291 
292 
293  /**
294  * Convert the hit probabilities to QEs for given expectation value.
295  *
296  * The expectation value corresponds to the number of photo-electrons given
297  * the multiplicity range of coincidences as specified at JCalibrateK40.cc.\n
298  *
299  * \param mu expectation value
300  */
301  void convertHitProbabilityToQE(const double mu)
302  {
303  using namespace std;
305  using JDETECTOR::getQE;
306 
307  this->mu = mu;
308 
309  const double Pmax = getMaximalHitProbability(this->mu);
310 
311  for (JPMTParametersMap::const_iterator i = this->begin(); i != this->end(); ++i) {
312  if (i->second.QE > Pmax) {
313  THROW(JValueOutOfRange, "Hit probability PMT " << i->first << ' ' << i->second.QE << " > maximum probability given expectation value " << this->mu << endl);
314  }
315  }
316 
317  for (JPMTParametersMap::iterator i = this->begin(); i != this->end(); ++i) {
318 
319  if (i->second.QE > 0.0)
320  i->second.QE = getQE(i->second.QE, this->mu);
321  else
322  i->second.QE = 0.0;
323  }
324  }
325 
326 
327  /**
328  * Stream input
329  *
330  * \param in input stream
331  * \param object PMT parameters map
332  * \return input stream
333  */
334  friend inline std::istream& operator>>(std::istream& in, JPMTParametersMap& object)
335  {
336  using namespace std;
337  using namespace JPP;
338 
339  JStringStream is(in);
340 
341  if (getFileStatus(is.str().c_str())) {
342  is.load();
343  }
344 
345  JProperties properties(getEquationParameters(), 1);
346 
348  JProperties demo = object.defaultPMTParameters.getProperties();
349 
350  properties["PMT"] = static_cast<JPMTParametersMap_t&>(object);
351  properties["pmt"] = helper;
352  properties["%"] = demo;
353  properties["QE"] = object.QE;
354  properties["mu"] = object.mu;
355 
356  is >> object.comment;
357  is >> properties;
358 
359  return in;
360  }
361 
362 
363  /**
364  * Stream output
365  *
366  * \param out output stream
367  * \param object PMT parameters map
368  * \return output stream
369  */
370  friend inline std::ostream& operator<<(std::ostream& out, const JPMTParametersMap& object)
371  {
372  using namespace JLANG;
373 
374  JProperties properties(getEquationParameters());
375 
376  JProperties demo = object.defaultPMTParameters.getProperties();
377 
378  properties["PMT"] = static_cast<const JPMTParametersMap_t&>(object);
379  properties["%"] = demo;
380 
381  out << object.comment;
384  out << properties;
385 
386  return out;
387  }
388 
389 
390  /**
391  * Get equation parameters.
392  *
393  * \return equation parameters
394  */
396  {
398 
399  return parameters;
400  }
401 
402 
403  /**
404  * Set equation parameters.
405  *
406  * \param equation equation parameters
407  */
408  static inline void setEquationParameters(const JEquationParameters& equation)
409  {
410  getEquationParameters() = equation;
411  }
412 
413 
415 
416  protected:
417  /**
418  * Default PMT parameters.
419  */
421 
422  /**
423  * Global QE.
424  */
425  double QE;
426 
427 
428  /**
429  * Expectation value for number of photo-electrons given two-fold (or higher) coincidence rate.
430  */
431  double mu;
432 
433  private:
434  /**
435  * Auxiliary class for I/O of PMT parameters map.
436  */
438  public:
439  /**
440  * Constructor.
441  *
442  * \param __object PMT parameters map
443  * \param __equation equation parameters
444  */
446  const JEquationParameters& __equation) :
447  object (__object),
448  equation(__equation)
449  {}
450 
451 
452  /**
453  * Stream input PMT parameters map helper.
454  *
455  * \param in input stream
456  * \param helper PMT parameters map helper
457  * \return input stream
458  */
459  friend inline std::istream& operator>>(std::istream& in, JPMTParametersMapHelper& helper)
460  {
461  JPMTIdentifier id;
462 
463  if (in >> id) {
464  helper.object[id].getProperties().read(in);
465  }
466 
467  return in;
468  }
469 
470 
471  /**
472  * Stream output PMT parameters map helper.
473  *
474  * \param out output stream
475  * \param helper PMT parameters map helper
476  * \return output stream
477  */
478  friend inline std::ostream& operator<<(std::ostream& out, const JPMTParametersMapHelper& helper)
479  {
480  for (JPMTParametersMap::const_iterator i = helper.object.begin(); i != helper.object.end(); ++i) {
481 
482  out << i->first;
483 
484  i->second.getProperties(helper.equation).write(out);
485 
486  out << std::endl;
487  }
488 
489  return out;
490  }
491 
492  private:
495  };
496  };
497 }
498 
499 #endif
Exception for opening of file.
Definition: JException.hh:342
General exception.
Definition: JException.hh:23
static void setEquationParameters(const JEquationParameters &equation)
Set equation parameters.
Exception for reading of file.
Definition: JException.hh:360
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:670
Utility class to parse parameter values.
Definition: JProperties.hh:496
const JPMTParameters & getPMTParameters(const JPMTIdentifier &id) const
Get PMT parameters.
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
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.
is
Definition: JDAQCHSM.chsm:167
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.
JPMTParameters & getDefaultPMTParameters()
Get default PMT parameters.
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:41
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:162
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.
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:38
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.