Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEditPMTParametersMap.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  /**
30  * Auxiliary class to apply modifications to PMT parameters.
31  */
32  template<class JAddress_t>
33  class JModifier {
34  public:
35  /**
36  * Default constructor.
37  */
38  JModifier()
39  {}
40 
41 
42  /**
43  * Apply modification to given parameters.
44  *
45  * \param parameters PMT parameters
46  * \return true if valid action; else false
47  */
48  bool apply(JPMTParameters& parameters) const
49  {
50  using namespace std;
51 
52  try {
53 
54  if (this->action == "set") {
55 
56  parameters.getProperties().getValue<double>(this->key) = this->value;
57 
58  } else if (this->action == "add") {
59 
60  parameters.getProperties().getValue<double>(this->key) += this->value;
61 
62  } else if (this->action == "sub") {
63 
64  parameters.getProperties().getValue<double>(this->key) -= this->value;
65 
66  } else if (this->action == "mul") {
67 
68  parameters.getProperties().getValue<double>(this->key) *= this->value;
69 
70  } else if (this->action == "div") {
71 
72  parameters.getProperties().getValue<double>(this->key) /= this->value;
73 
74  } else {
75 
76  return false;
77  }
78  }
79  catch(const std::exception& error) {
80  cerr << error.what() << endl;
81  return false;
82  }
83 
84  return true;
85  }
86 
87 
88  /**
89  * Read modifier from input.
90  *
91  * \param in input stream
92  * \param modifier modifier
93  * \return input stream
94  */
95  friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
96  {
97  return in >> modifier.address
98  >> modifier.action
99  >> modifier.key
100  >> modifier.value;
101  }
102 
103 
104  /**
105  * Write modifier to output.
106  *
107  * \param out output stream
108  * \param modifier modifier
109  * \return output stream
110  */
111  friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
112  {
113  return out << modifier.address << ' '
114  << modifier.action << ' '
115  << modifier.key << ' '
116  << modifier.value;
117  }
118 
119 
120  JAddress_t address;
121  std::string action;
122  std::string key;
123  double value;
124  };
125 }
126 
127 
128 /**
129  * \file
130  *
131  * Auxiliary program to edit PMT parameters map.
132  *
133  * Syntax:
134  * <pre>
135  * -A "<PMT physical address> (set|add|sub|mul|div) <key> <value>"
136  * </pre>
137  * The PMT physical address corresponds to the data structure JDETECTOR::JPMTPhysicalAddress.
138  * The key corresponds to one of the data members of the JDETECTOR::JPMTParameters data structure.
139  *
140  * Note that in the absence of option <tt>-a</tt>, the detector identifier should be specified
141  * using option <tt>-D</tt> so to obtain the correct PMT address mapping.
142  *
143  * Multiple options <tt>-A</tt> will be processed in order of appearance.
144  * \author mdejong
145  */
146 int main(int argc, char **argv)
147 {
148  using namespace std;
149  using namespace JPP;
150 
151  typedef JModifier<JPMTPhysicalAddress> JModifier_t;
152  typedef JRange<double> JRange_t;
153 
154  string detectorFile;
155  int detectorID;
156  JPMTParametersMap parameters;
157  vector<JModifier_t> modifier;
158  JRange_t T_ns;
159  string outputFile;
160  int debug;
161 
162  try {
163 
164  JParser<> zap("Auxiliary program to edit PMT parameters map.");
165 
166  zap['a'] = make_field(detectorFile, "detector file.") = "";
167  zap['D'] = make_field(detectorID, "detector identifier (in absence of detector file).") = 0;
168  zap['P'] = make_field(parameters, "PMT simulation data (or corresponding file name)") = JPARSER::initialised();
169  zap['A'] = make_field(modifier, "PMT parameter modifier.") = JPARSER::initialised();
170  zap['T'] = make_field(T_ns, "time-over-threshold range.") = JRange_t();
171  zap['o'] = make_field(outputFile, "output file.") = "pmt.txt";
172  zap['d'] = make_field(debug) = 3;
173 
174  zap(argc, argv);
175  }
176  catch(const exception &error) {
177  FATAL(error.what() << endl);
178  }
179 
180 
181  if (detectorFile != "") {
182 
183  // Setting default PMT parameters for given detector.
184 
186 
187  try {
188  load(detectorFile, detector);
189  }
190  catch(const JException& error) {
191  FATAL(error);
192  }
193 
194  if (detectorID == 0) {
195 
196  detectorID = detector.getID();
197 
198  } else if (detectorID != detector.getID()) {
199 
200  FATAL("Inconsistent detector identifier " << detectorID << " != " << detector.getID() << endl);
201  }
202 
203 
204  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
205 
206  for (unsigned int pmt = 0; pmt != module->size(); ++pmt) {
207 
208  const JPMTIdentifier id(module->getID(), pmt);
209 
210  if (parameters.find(id) == parameters.end()) {
211 
212  NOTICE("Setting default parameters for PMT " << id << endl);
213 
214  parameters[id] = parameters.getDefaultPMTParameters();
215  }
216  }
217  }
218  }
219 
220  if (!modifier.empty()) {
221 
222  if (!hasDetectorAddressMap(detectorID)) {
223  FATAL("Invalid detector identifier " << detectorID << endl);
224  }
225 
226  const JDetectorAddressMap& demo = getDetectorAddressMap(detectorID);
227 
228  for (JPMTParametersMap::iterator ps = parameters.begin(); ps != parameters.end(); ++ps) {
229 
230  const JPMTPhysicalAddress& address = demo.get(ps->first);
231 
232  for (vector<JModifier_t>::const_iterator i = modifier.begin(); i != modifier.end(); ++i) {
233 
234  if (address == i->address) {
235 
236  NOTICE("Modifying parameters for PMT " << ps->first << endl);
237 
238  if (!i->apply(ps->second)) {
239  ERROR("No valid action: " << *i << endl);
240  }
241  }
242  }
243  }
244  }
245 
246 
247  if (T_ns != JRange_t()) {
248 
249  const int NPE = 1;
250 
251  for (JPMTParametersMap::iterator i = parameters.begin(); i != parameters.end(); ++i) {
252 
253  const JPMTAnalogueSignalProcessor cpu(i->second);
254 
255  i->second.QE *= (cpu.getIntegralOfProbability(i->second.threshold,
256  cpu.getNPE(T_ns.getUpperLimit(), NPE),
257  NPE)
258  /
259  cpu.getIntegralOfProbability(cpu.getNPE(T_ns.getLowerLimit(), NPE),
260  cpu.getNPE(T_ns.getUpperLimit(), NPE),
261  NPE));
262  }
263  }
264 
265 
266  if (outputFile != "") {
267 
268  parameters.comment.add(JMeta(argc, argv));
269 
270  ofstream out(outputFile.c_str());
271 
272  out << parameters << endl;
273 
274  out.close();
275  }
276 }
277 
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:71
Utility class to parse command line options.
Definition: JParser.hh:1410
General exception.
Definition: JException.hh:40
std::istream & operator>>(std::istream &in, JHead &header)
Read header from input.
Detector data structure.
Definition: JDetector.hh:77
bool hasDetectorAddressMap(const int id)
Check if detector address map is available.
double getNPE(const double tot_ns, const double eps=1.0e-3) const
Get number of photo-electrons.
Lookup table for PMT addresses in detector.
Empty structure for specification of parser element that is initialised (i.e.
Definition: JParser.hh:64
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.
const T & getValue(const std::string &key) const
Get value.
Definition: JProperties.hh:962
Detector specific mapping between logical positions and readout channels of PMTs in optical modules...
Detector file.
Definition: JHead.hh:126
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
int getID() const
Get identifier.
Definition: JObjectID.hh:54
ROOT I/O of application specific meta data.
#define NOTICE(A)
Definition: JMessage.hh:62
#define ERROR(A)
Definition: JMessage.hh:64
Auxiliary class for map of PMT parameters.
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
int debug
debug level
Definition: JSirene.cc:59
JDetectorAddressMap & getDetectorAddressMap()
Get detector address map.
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:65
Auxiliary class to define a range between two values.
Utility class to parse command line options.
PMT analogue signal processor.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Data structure for PMT physical address.
Data structure for PMT parameters.
double getIntegralOfProbability(const double xmin, const double xmax, const int NPE) const
Get integral of probability.
int main(int argc, char *argv[])
Definition: Main.cpp:15