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