Jpp  15.0.1-rc.1-highQE
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 
152  parameters.QE *= this->QE;
153 
154  return parameters;
155  }
156 
157 
158  /**
159  * Get QE of given PMT.
160  *
161  * \param id PMT identifier
162  * \return QE
163  */
164  double getQE(const JPMTIdentifier& id) const
165  {
166  return getPMTParameters(id).QE;
167  }
168 
169 
170  /**
171  * Get ratio of hit probabilities of given PMT.
172  *
173  * \param id PMT identifier
174  * \return ratio
175  */
176  double getHitProbability(const JPMTIdentifier& id) const
177  {
179 
180  return getHitProbability(getQE(id), this->mu);
181  }
182 
183 
184  /**
185  * Get default PMT parameters.
186  *
187  * \return PMT parameters
188  */
190  {
191  return defaultPMTParameters;
192  }
193 
194 
195  /**
196  * Get default PMT parameters.
197  *
198  * \return PMT parameters
199  */
201  {
202  return defaultPMTParameters;
203  }
204 
205 
206  /**
207  * Set default PMT parameters.
208  *
209  * \param parameters PMT parameters
210  */
212  {
214  }
215 
216 
217  /**
218  * Get global QE.
219  *
220  * \return QE
221  */
222  double getQE() const
223  {
224  return this->QE;
225  }
226 
227 
228  /**
229  * Set global QE.
230  *
231  * \param QE QE
232  */
233  void setQE(const double QE)
234  {
235  this->QE = QE;
236  }
237 
238 
239  /**
240  * Get expectation value for number of photo-electrons given two-fold (or higher) coincidence rate.
241  *
242  * \return expectation value
243  */
244  double getMu() const
245  {
246  return this->mu;
247  }
248 
249 
250  /**
251  * Check validity of PMT parameters.
252  *
253  * \return true if valid; else false
254  */
255  bool is_valid() const
256  {
257  try {
258 
259  if (this->QE < 0) {
260  THROW(JException, "Invalid global QE " << QE);
261  }
262 
263  if (this->mu < 0) {
264  THROW(JException, "Invalid expectation value number of photo-electrons " << mu);
265  }
266 
268  THROW(JException, "Invalid default PMT parameters");
269  }
270 
271  for (const_iterator i = this->begin(); i != this->end(); ++i) {
272 
273  if (!i->second.is_valid()) {
274  THROW(JException, "Invalid PMT parameters at " << i->first);
275  }
276 
277  if (i->second.QE * this->QE > 1.0 ||
278  i->second.QE * this->QE < 0.0) {
279  THROW(JException, "Invalid PMT QE at " << i->first << ' ' << i->second.QE * this->QE);
280  }
281  }
282 
283  return true;
284  }
285  catch(const JException& error) {
286 
287  Throw(error);
288 
289  return false;
290  }
291  }
292 
293 
294  /**
295  * Convert the hit probabilities to QEs for given expectation value.
296  *
297  * The expectation value corresponds to the number of photo-electrons given
298  * the multiplicity range of coincidences as specified at JCalibrateK40.cc.\n
299  *
300  * \param mu expectation value
301  */
302  void convertHitProbabilityToQE(const double mu)
303  {
304  using namespace std;
306  using JDETECTOR::getQE;
307 
308  this->mu = mu;
309 
310  const double Pmax = getMaximalHitProbability(this->mu);
311 
312  for (JPMTParametersMap::const_iterator i = this->begin(); i != this->end(); ++i) {
313  if (i->second.QE > Pmax) {
314  THROW(JValueOutOfRange, "Hit probability PMT " << i->first << ' ' << i->second.QE << " > maximum probability given expectation value " << this->mu << endl);
315  }
316  }
317 
318  for (JPMTParametersMap::iterator i = this->begin(); i != this->end(); ++i) {
319 
320  if (i->second.QE > 0.0)
321  i->second.QE = getQE(i->second.QE, this->mu);
322  else
323  i->second.QE = 0.0;
324  }
325  }
326 
327 
328  /**
329  * Access parameters corresponding to given PMT identifier.
330  *
331  * Note: The default PMT parameters are inserted if no parameters for the given PMT identifier are contained
332  * at the time of the function call.
333  *
334  * \param id PMT identifier
335  * \return parameters
336  */
338  {
339  JPMTParametersMap_t::iterator i = find(id);
340 
341  if (i == end()) {
342  i = this->insert(i, std::make_pair(id, getDefaultPMTParameters()));
343  }
344 
345  return i->second;
346  }
347 
348 
349  /**
350  * Stream input
351  *
352  * \param in input stream
353  * \param object PMT parameters map
354  * \return input stream
355  */
356  friend inline std::istream& operator>>(std::istream& in, JPMTParametersMap& object)
357  {
358  using namespace std;
359  using namespace JPP;
360 
361  JStringStream is(in);
362 
363  if (getFileStatus(is.str().c_str())) {
364  is.load();
365  }
366 
367  JProperties properties(getEquationParameters(), 1);
368 
370  JProperties demo = object.defaultPMTParameters.getProperties();
371 
372  properties["PMT"] = static_cast<JPMTParametersMap_t&>(object);
373  properties["pmt"] = helper;
374  properties["%"] = demo;
375  properties["QE"] = object.QE;
376  properties["mu"] = object.mu;
377 
378  is >> object.comment;
379  is >> properties;
380 
381  return in;
382  }
383 
384 
385  /**
386  * Stream output
387  *
388  * \param out output stream
389  * \param object PMT parameters map
390  * \return output stream
391  */
392  friend inline std::ostream& operator<<(std::ostream& out, const JPMTParametersMap& object)
393  {
394  using namespace JLANG;
395 
396  JProperties properties(getEquationParameters());
397 
398  JProperties demo = object.defaultPMTParameters.getProperties();
399 
400  properties["PMT"] = static_cast<const JPMTParametersMap_t&>(object);
401  properties["%"] = demo;
402 
403  out << object.comment;
406  out << properties;
407 
408  return out;
409  }
410 
411 
412  /**
413  * Get equation parameters.
414  *
415  * \return equation parameters
416  */
418  {
420 
421  return parameters;
422  }
423 
424 
425  /**
426  * Set equation parameters.
427  *
428  * \param equation equation parameters
429  */
430  static inline void setEquationParameters(const JEquationParameters& equation)
431  {
432  getEquationParameters() = equation;
433  }
434 
435 
437 
438  protected:
439  /**
440  * Default PMT parameters.
441  */
443 
444  /**
445  * Global QE.
446  */
447  double QE;
448 
449 
450  /**
451  * Expectation value for number of photo-electrons given two-fold (or higher) coincidence rate.
452  */
453  double mu;
454 
455  private:
456  /**
457  * Auxiliary class for I/O of PMT parameters map.
458  */
460  public:
461  /**
462  * Constructor.
463  *
464  * \param __object PMT parameters map
465  * \param __equation equation parameters
466  */
468  const JEquationParameters& __equation) :
469  object (__object),
470  equation(__equation)
471  {}
472 
473 
474  /**
475  * Stream input PMT parameters map helper.
476  *
477  * \param in input stream
478  * \param helper PMT parameters map helper
479  * \return input stream
480  */
481  friend inline std::istream& operator>>(std::istream& in, JPMTParametersMapHelper& helper)
482  {
483  JPMTIdentifier id;
484 
485  if (in >> id) {
486  helper.object[id].getProperties().read(in);
487  }
488 
489  return in;
490  }
491 
492 
493  /**
494  * Stream output PMT parameters map helper.
495  *
496  * \param out output stream
497  * \param helper PMT parameters map helper
498  * \return output stream
499  */
500  friend inline std::ostream& operator<<(std::ostream& out, const JPMTParametersMapHelper& helper)
501  {
502  for (JPMTParametersMap::const_iterator i = helper.object.begin(); i != helper.object.end(); ++i) {
503 
504  out << i->first;
505 
506  i->second.getProperties(helper.equation).write(out);
507 
508  out << std::endl;
509  }
510 
511  return out;
512  }
513 
514  private:
517  };
518  };
519 }
520 
521 #endif
Exception for opening of file.
Definition: JException.hh:342
JPMTParameters & operator[](const JPMTIdentifier &id)
Access parameters corresponding to given PMT identifier.
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:41
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.