Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEditPMTParameters.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <fstream>
5 #include <iomanip>
6 #include <vector>
7 
8 #include "JLang/JLangToolkit.hh"
9 #include "JDetector/JDetector.hh"
18 #include "JSupport/JMeta.hh"
19 #include "JTools/JRange.hh"
20 
21 #include "Jeep/JParser.hh"
22 #include "Jeep/JMessage.hh"
23 
24 
25 namespace {
26 
27  using namespace JPP;
28 
29  static const char WILDCARD_RING = '*'; //<! Wild card for ring of PMT in optical module.
30  static const int WILDCARD_POSITION = -1; //<! Wild card for position of PMT in ring.
31  static const int WILDCARD = -1; //<! Wild card for PMT identifier.
32 
33 
34  /**
35  * Compare PMT physical addresses taking into account wild cards.
36  *
37  * \param first first PMT physical address
38  * \param second second PMT physical address
39  * \return true if addresses are equal; else false
40  */
41  inline bool compare(const JPMTPhysicalAddress& first, const JPMTPhysicalAddress& second)
42  {
43  return (((first == second)) ||
44 
45  ((first.ring == WILDCARD_RING || second.ring == WILDCARD_RING) && first.position == second.position) ||
46  ((first.position == WILDCARD_POSITION || second.position == WILDCARD_POSITION) && first.ring == second.ring) ||
47 
48  ((first.ring == WILDCARD_RING || second.ring == WILDCARD_RING) &&
49  (first.position == WILDCARD_POSITION || second.position == WILDCARD_POSITION)));
50  }
51 
52 
53  /**
54  * Compare PMT identifiers taking into account wild cards.
55  *
56  * \param first first PMT identifier
57  * \param second second PMT identifier
58  * \return true if addresses are equal; else false
59  */
60  inline bool compare(const JPMTIdentifier& first, const JPMTIdentifier& second)
61  {
62  return (((first == second)) ||
63 
64  ((first.getID() == WILDCARD || second.getID() == WILDCARD) && first.getTDC() == second.getTDC()) ||
65  ((first.getTDC() == WILDCARD || second.getTDC() == WILDCARD) && first.getID() == second.getID()) ||
66 
67  ((first.getID() == WILDCARD || second.getID() == WILDCARD) &&
68  (first.getTDC() == WILDCARD || second.getTDC() == WILDCARD)));
69  }
70 
71 
72  /**
73  * Auxiliary class to apply modifications to PMT parameters.
74  */
75  template<class JAddress_t>
76  class JModifier {
77  public:
78  /**
79  * Default constructor.
80  */
81  JModifier()
82  {}
83 
84 
85  /**
86  * Apply modification to given parameters.
87  *
88  * \param parameters PMT parameters
89  * \return true if valid action; else false
90  */
91  bool apply(JPMTParameters& parameters) const
92  {
93  using namespace std;
94 
95  try {
96 
97  if (this->action == "set") {
98 
99  parameters.getProperties().getValue<double>(this->key) = this->value;
100 
101  } else if (this->action == "add") {
102 
103  parameters.getProperties().getValue<double>(this->key) += this->value;
104 
105  } else if (this->action == "sub") {
106 
107  parameters.getProperties().getValue<double>(this->key) -= this->value;
108 
109  } else if (this->action == "mul") {
110 
111  parameters.getProperties().getValue<double>(this->key) *= this->value;
112 
113  } else if (this->action == "div") {
114 
115  parameters.getProperties().getValue<double>(this->key) /= this->value;
116 
117  } else {
118 
119  return false;
120  }
121  }
122  catch(const std::exception& error) {
123  cerr << error.what() << endl;
124  return false;
125  }
126 
127  return true;
128  }
129 
130 
131  /**
132  * Read modifier from input.
133  *
134  * \param in input stream
135  * \param modifier modifier
136  * \return input stream
137  */
138  friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
139  {
140  return in >> modifier.address
141  >> modifier.action
142  >> modifier.key
143  >> modifier.value;
144  }
145 
146 
147  /**
148  * Write modifier to output.
149  *
150  * \param out output stream
151  * \param modifier modifier
152  * \return output stream
153  */
154  friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
155  {
156  return out << modifier.address << ' '
157  << modifier.action << ' '
158  << modifier.key << ' '
159  << modifier.value;
160  }
161 
162 
163  JAddress_t address;
164  std::string action;
165  std::string key;
166  double value;
167  };
168 }
169 
170 
171 /**
172  * \file
173  *
174  * Auxiliary program to edit PMT parameters map.
175  *
176  * Syntax:
177  * <pre>
178  * -A "<PMT physical address> (set|add|sub|mul|div) <key> <value>"
179  * -M "<PMT identifier> (set|add|sub|mul|div) <key> <value>"
180  * </pre>
181  * The PMT physical address corresponds to the data structure JDETECTOR::JPMTPhysicalAddress and
182  * the PMT identifier to JDETECTOR::JPMTIdentifier.\n
183  * The key corresponds to one of the data members of the JDETECTOR::JPMTParameters data structure.
184  *
185  * Note that in the absence of option <tt>-a</tt>, the detector identifier should be specified
186  * using option <tt>-D</tt> so to obtain the correct PMT address mapping.
187  *
188  * Multiple options <tt>-A</tt> will be processed in order of appearance.
189  * \author mdejong
190  */
191 int main(int argc, char **argv)
192 {
193  using namespace std;
194  using namespace JPP;
195 
196  typedef JRange<double> JRange_t;
197 
198  string detectorFile;
199  int detectorID;
203  double mu;
204  JRange_t T_ns;
205  string outputFile;
206  int debug;
207 
208  try {
209 
210  JParser<> zap("Auxiliary program to edit PMT parameters map.");
211 
212  zap['a'] = make_field(detectorFile, "detector file.") = "";
213  zap['D'] = make_field(detectorID, "detector identifier (in absence of detector file).") = 0;
214  zap['P'] = make_field(parameters, "PMT simulation data (or corresponding file name)") = JPARSER::initialised();
215  zap['A'] = make_field(mod, "PMT parameter modifier by physical address (e.g. B1).") = JPARSER::initialised();
216  zap['M'] = make_field(daq, "PMT parameter modifier by DAQ address (e.g. <module> <channel>.") = JPARSER::initialised();
217  zap['E'] = make_field(mu, "expectation value for npe given two-fold coincidence") = 0.0;
218  zap['T'] = make_field(T_ns, "time-over-threshold range.") = JRange_t();
219  zap['o'] = make_field(outputFile, "output file.");
220  zap['d'] = make_field(debug) = 3;
221 
222  zap(argc, argv);
223  }
224  catch(const exception &error) {
225  FATAL(error.what() << endl);
226  }
227 
228 
229  if (detectorFile != "") {
230 
231  // Setting default PMT parameters for given detector.
232 
234 
235  try {
236  load(detectorFile, detector);
237  }
238  catch(const JException& error) {
239  FATAL(error);
240  }
241 
242  if (detectorID == 0) {
243 
244  detectorID = detector.getID();
245 
246  } else if (detectorID != detector.getID()) {
247 
248  FATAL("Inconsistent detector identifier " << detectorID << " != " << detector.getID() << endl);
249  }
250 
251 
252  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
253 
254  for (unsigned int pmt = 0; pmt != module->size(); ++pmt) {
255 
256  const JPMTIdentifier id(module->getID(), pmt);
257 
258  if (parameters.find(id) == parameters.end()) {
259 
260  DEBUG("Setting default parameters for PMT " << id << endl);
261 
262  parameters[id] = parameters.getDefaultPMTParameters();
263  }
264  }
265  }
266  }
267 
268  if (!mod.empty()) {
269 
270  if (!hasDetectorAddressMap(detectorID)) {
271  FATAL("Invalid detector identifier " << detectorID << endl);
272  }
273 
274  const JDetectorAddressMap& demo = getDetectorAddressMap(detectorID);
275 
276  for (JPMTParametersMap::iterator ps = parameters.begin(); ps != parameters.end(); ++ps) {
277 
278  const JPMTPhysicalAddress& address = demo.get(ps->first);
279 
280  for (vector< JModifier<JPMTPhysicalAddress> >::const_iterator i = mod.begin(); i != mod.end(); ++i) {
281 
282  if (compare(i->address, address)) {
283 
284  DEBUG("Modifying parameters for PMT " << ps->first << ' ' << i->action << ' ' << i->key << ' ' << i->value << endl);
285 
286  if (!i->apply(ps->second)) {
287  ERROR("No valid action: " << *i << endl);
288  }
289  }
290  }
291  }
292  }
293 
294  if (!daq.empty()) {
295 
296  for (JPMTParametersMap::iterator ps = parameters.begin(); ps != parameters.end(); ++ps) {
297 
298  for (vector< JModifier<JPMTIdentifier> >::const_iterator i = daq.begin(); i != daq.end(); ++i) {
299 
300  if (compare(ps->first, i->address)) {
301 
302  DEBUG("Modifying parameters for PMT " << ps->first << ' ' << i->action << ' ' << i->key << ' ' << i->value << endl);
303 
304  if (!i->apply(ps->second)) {
305  ERROR("No valid action: " << *i << endl);
306  }
307  }
308  }
309  }
310  }
311 
312 
313  if (mu > 0.0) {
314 
315  DEBUG("Correct measured QE for two-hit probability " << mu << endl);
316 
317  try {
318  parameters.convertHitProbabilityToQE(mu);
319  }
320  catch(const JException& error) {
321  FATAL(error.what());
322  }
323 
324  } else if (mu < 0.0) {
325 
326  FATAL("Invalid expection value for two-hit probability " << mu << endl);
327  }
328 
329 
330  if (T_ns != JRange_t()) {
331 
332  DEBUG("Correct measured QE for time-over-threshold range " << T_ns << endl);
333 
334  const int NPE = 1;
335 
336  for (JPMTParametersMap::iterator i = parameters.begin(); i != parameters.end(); ++i) {
337 
338  const JPMTAnalogueSignalProcessor cpu(i->second);
339 
340  i->second.QE *= (cpu.getIntegralOfChargeProbability(i->second.threshold,
341  cpu.getNPE(T_ns.getUpperLimit(), NPE),
342  NPE)
343  /
344  cpu.getIntegralOfChargeProbability(cpu.getNPE(T_ns.getLowerLimit(), NPE),
345  cpu.getNPE(T_ns.getUpperLimit(), NPE),
346  NPE));
347  }
348  }
349 
350 
351  if (outputFile != "") {
352 
353  parameters.comment.add(JMeta(argc, argv));
354 
355  ofstream out(outputFile.c_str());
356 
357  out << parameters << endl;
358 
359  out.close();
360  }
361 }
362 
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Utility class to parse command line options.
Definition: JParser.hh:1500
General exception.
Definition: JException.hh:23
double getIntegralOfChargeProbability(const double xmin, const double xmax, const int NPE) const
Get integral of probability.
Detector data structure.
Definition: JDetector.hh:80
bool hasDetectorAddressMap(const int id)
Check if detector address map is available.
*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
Lookup table for PMT addresses in detector.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
JProperties getProperties(const JEquationParameters &equation=JPMTParameters::getEquationParameters())
Get properties of this class.
string outputFile
Data structure for detector geometry and calibration.
const JModuleAddressMap & get(const int id) const
Get module address map.
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
const T & getValue(const std::string &key) const
Get value.
Definition: JProperties.hh:974
Detector specific mapping between logical positions and readout channels of PMTs in optical modules...
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
int getID() const
Get identifier.
Definition: JObjectID.hh:50
ROOT I/O of application specific meta data.
#define ERROR(A)
Definition: JMessage.hh:66
Auxiliary class for map of PMT parameters.
char ring
ring number [&#39;A&#39;,&#39;F&#39;]
int debug
debug level
Definition: JSirene.cc:63
JDetectorAddressMap & getDetectorAddressMap()
Get detector address map.
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
virtual double getNPE(const double tot_ns, const double eps=1.0e-3) const
Get number of photo-electrons.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1593
virtual const char * what() const override
Get error message.
Definition: JException.hh:48
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Auxiliary class to define a range between two values.
Utility class to parse command line options.
int position
position within ring [1,6]
PMT analogue signal processor.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Data structure for PMT physical address.
Data structure for PMT parameters.
do set_variable DETECTOR_TXT $WORKDIR detector
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 typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
int main(int argc, char *argv[])
Definition: Main.cpp:15