Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTuneHV.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <map>
4 #include <fstream>
5 #include <sstream>
6 
7 #include "JSystem/JDate.hh"
8 
9 #include "JLang/JUUID.hh"
10 
11 #include "Jeep/JPrint.hh"
12 #include "Jeep/JParser.hh"
13 #include "Jeep/JMessage.hh"
14 
15 #include "JTools/JRange.hh"
16 
17 #include "JSupport/JMeta.hh"
18 
19 #include "JDetector/JDetector.hh"
23 
24 #include "JDB/JUPI.hh"
25 #include "JDB/JUPI_t.hh"
26 
27 #include "JCalibrate/JFitToT.hh"
29 
30 #include "JROOT/JManager.hh"
31 
32 #include "JSon/JSon.hh"
33 
34 #include "TFile.h"
35 
36 namespace {
37 
38  /**
39  * Auxiliary class for creating a PMT-UPI look-up table from ASCII DB file.
40  */
41  struct JUPITable
42  {
43  /**
44  * Initialise
45  *
46  * \param fileName file name
47  */
48  JUPITable(const char* const fileName)
49  {
50  using namespace std;
51  using namespace JPP;
52 
53  ifstream in(fileName);
54 
55  for (string buffer; getline(in, buffer); ) {
56 
57  istringstream is(buffer);
58 
59  JUPI entry;
60 
61  if (is >> entry.PBS >> entry.VARIANT >> entry.VERSION >> entry.SERIALNUMBER) {
62 
63  if (entry.PBS.is_valid() &&
64  entry.PBS == PBS::PMT &&
65  entry.SERIALNUMBER >= 0) {
66 
67  number2upi[entry.PBS][entry.SERIALNUMBER] = JUPI_t(entry.PBS,
68  entry.VARIANT,
69  entry.VERSION,
70  entry.SERIALNUMBER);
71  }
72  }
73  }
74 
75  in.close();
76  }
77 
78 
79  /**
80  * Get %UPI.
81  *
82  * \param pbs %PBS
83  * \param number serial number
84  * \return %UPI
85  */
86  JDATABASE::JUPI_t operator()(const JDATABASE::JPBS_t& pbs, const int number)
87  {
88  using namespace std;
89  using namespace JPP;
90 
91  map<int, JUPI_t>::const_iterator p = number2upi[pbs].find(number);
92 
93  if (p == number2upi[pbs].end()) {
94 
95  p = number2upi[pbs].find(number);
96 
97  if (p == number2upi[pbs].end()) {
98  THROW(JValueOutOfRange, "Invalid PBS / serial number " << pbs << " / " << number);
99  }
100  }
101 
102  return p->second;
103  }
104 
105  protected:
107  };
108 }
109 
110 
111 /**
112  * \file
113  *
114  * Program for inter-/extrapolation of nominal high-voltage settings.
115  * \author acreusot, mdejong and bjung
116  */
117 int main(int argc, char **argv)
118 {
119  using namespace std;
120  using namespace JPP;
121 
122  string inputFile;
123  string detectorFile;
124  string outputFile;
125  string UPIFile;
126 
127  string login;
128  string locationID;
129  string testType;
130  int elapsedTime = 0;
131 
132  double HVstepMin = 2 * 3.14; //!< Minimal HV step [V]
133  JRange<double> HVrange (-1500 + 1e-2,
134  -80 - 1e-2); //!< Allowed HV range [V]
135  JRange<double> gainRange(FITTOT_GAIN_MIN + 1e-2,
136  FITTOT_GAIN_MAX - 1e-2); //!< Allowed gain fit-range
137  double gainTarget = NOMINAL_GAIN; //!< Target nominal gain value
138 
139  int debug;
140 
141 
142  try {
143 
144  JProperties properties;
145 
146  properties.insert(gmake_property(login));
147  properties.insert(gmake_property(locationID));
148  properties.insert(gmake_property(testType));
149  properties.insert(gmake_property(elapsedTime));
150 
151  JProperties settings;
152 
153  settings.insert(gmake_property(HVstepMin));
154  settings.insert(gmake_property(HVrange));
155  settings.insert(gmake_property(gainRange));
156  settings.insert(gmake_property(gainTarget));
157 
158  JParser<> zap("Example program to find optimal HV-settings.");
159 
160  zap['f'] = make_field(inputFile, "input file (ROOT format)");
161  zap['o'] = make_field(outputFile, "output file (json format)");
162  zap['a'] = make_field(detectorFile, "detector file");
163  zap['b'] = make_field(UPIFile, "PMT UPI ASCII table");
164  zap['#'] = make_field(properties, "database information") = JPARSER::initialised();
165  zap['@'] = make_field(settings, "interpolation settings") = JPARSER::initialised();
166  zap['d'] = make_field(debug, "debug") = 1;
167 
168  zap(argc, argv);
169  }
170  catch(const exception &error) {
171  FATAL(error.what() << endl);
172  }
173 
174  if (login.empty() || locationID.empty()) {
175 
176  FATAL("Missing user information (please specify via -#login and -#locationID");
177 
178  } else if (testType.empty()) {
179 
180  FATAL("Missing database test type (please specify via -#testType)");
181  }
182 
183 
184  const JUUID& UUID = JUUID::rndm();
185 
186  JDateAndTimeFormat format = ISO8601;
187  JDateAndTime timer;
188 
189  timer->tm_sec -= elapsedTime;
190 
192 
193  try {
194  load(detectorFile, detector);
195  }
196  catch (const exception& error) {
197  FATAL(error.what() << endl);
198  }
199 
200  JModuleRouter router (detector);
201 
202  JUPITable fetchUPI(UPIFile.c_str());
203 
204 
205  // Read input
206 
207  TFile input(inputFile.c_str(), "READ");
208 
209  JManager<string, TMultiGraph> manager(new TMultiGraph(), "%.HVxG");
210 
211  input >> manager;
212 
213  input.Close();
214 
215 
216  // Find optimal HV corresponding to a nominal gain of 1.0
217  // via linear interpolation in log-log scale
218 
219  JHVCalibration HVcalibrations;
220 
221  JHVInterpolator::setMinHVDistance(HVstepMin);
222  JHVInterpolator::setHVRange (HVrange);
223  JHVInterpolator::setGainRange (gainRange);
224 
225  NOTICE("Searching optimal high-voltage settings..." << endl <<
226  LEFT(30) << "UPI" << CENTER(35) << "(identifer / location)" << RIGHT(35) << "Inter-/Extrapolated high-voltage [-]" << endl);
227 
228  for (JManager<string, TMultiGraph>::iterator it = manager.begin(); it != manager.end(); ++it) {
229 
230  const string& name = it->first;
231  JHVInterpolator data(*(it->second));
232 
233  // Retrieve upi, location and serialID
234 
235  const int seppos = name.find('.');
236  const int moduleID = stoi(name.substr(0, seppos));
237  const int tdc = stoi(name.substr(seppos + 1, seppos + 3));
238 
239  const JModule& module = router.getModule(moduleID);
240  const JPMT& pmt = router.getPMT(JPMTIdentifier(moduleID, tdc));
241 
242  const JUPI_t& pmtUPI = fetchUPI(PBS::PMT, pmt.getID());
243  const string location = MAKE_STRING(right << FILL(4,'0') << module.getLocation().getString() << '.' <<
244  right << FILL(2,'0') << module.getLocation().getFloor() << '.' <<
245  right << FILL(2,'0') << tdc);
246 
247  NOTICE(LEFT(30) << pmtUPI << "(a.k.a. " << name << " / " << location << "): ");
248 
249  if (data.interpolate(gainTarget)) {
250 
251  const double result = -data.getTargetHV (gainTarget);
252  const double error = data.getTargetHVError(gainTarget);
253 
254  NOTICE(right << FIXED(20,2) << result << " +/- " << FIXED(4,2) << error << endl);
255 
256  HVcalibrations.push_back(JHVCalibration_t(pmtUPI, OK_t, result));
257 
258  } else {
259 
260  WARNING("Could not find high-voltage corresponding to target gain; Setting zero." << endl);
261 
262  HVcalibrations.push_back(JHVCalibration_t(pmtUPI, Failed_t, 0.0));
263  }
264  }
265 
266  // Update graphical output
267 
268  TFile output(inputFile.c_str(), "RECREATE");
269 
270  output << manager;
271 
272  output.Close();
273 
274  // Write json output
275 
276  json js;
277 
278  js[UUID_t] = MAKE_STRING(UUID);
279  js[User_t] = login;
280  js[Location_t] = locationID;
281  js[Test_t + Type_t] = testType;
282  js[StartTime_t] = timer.toString(format);
283  js[EndTime_t] = timer().toString(format);
284  js[Tests_t] = json(HVcalibrations);
285 
286  ofstream ofs(outputFile.c_str());
287 
288  ofs << setw(2) << setprecision(8);
289  ofs << js;
290 
291  ofs.close();
292 }
Utility class to parse command line options.
Definition: JParser.hh:1500
static const std::string UUID_t
#define WARNING(A)
Definition: JMessage.hh:65
int main(int argc, char *argv[])
Definition: Main.cc:15
double getTargetHVError(const double gainTarget)
Get error estimate on high-voltage corresponding to given target gain value.
const JModule & getModule(const JObjectID &id) const
Get module parameters.
Data structure for a composite optical module.
Definition: JModule.hh:57
static const std::string Failed_t
#define gmake_property(A)
macro to convert (template) parameter to JPropertiesElement object
static const std::string Tests_t
static const JPBS_t PMT(3, 4, 2, 3)
PBS of photo-multiplier tube (PMT)
static const std::string OK_t
static const double FITTOT_GAIN_MAX
Default maximal gain.
Definition: JFitToT.hh:44
Detector data structure.
Definition: JDetector.hh:80
static const double FITTOT_GAIN_MIN
Default minimal gain.
Definition: JFitToT.hh:43
Router for direct addressing of module data in detector data structure.
#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
static const std::string Location_t
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
JDateAndTimeFormat
Date and time formats.
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
double getTargetHV(const double gainTarget)
Get high-voltage corresponding to given target gain value.
Dynamic ROOT object management.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
is
Definition: JDAQCHSM.chsm:167
Data structure for detector geometry and calibration.
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
Date and time functions.
Auxiliary data structure for alignment of data.
Definition: JManip.hh:366
Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys...
Definition: JManager.hh:43
int SERIALNUMBER
Definition: JUPI.hh:27
I/O formatting auxiliaries.
Detector file.
Definition: JHead.hh:196
bool interpolate(const double gainTarget)
Inter-/Extrapolate the high-voltage value corresponding to the target gain value. ...
Auxiliary data structure to store high-voltage versus gain data and interpolate the nominal high-volt...
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
return result
Definition: JPolint.hh:727
int getID() const
Get identifier.
Definition: JObjectID.hh:50
Data structure for PMT geometry and calibration.
Definition: JPMT.hh:47
ROOT I/O of application specific meta data.
#define NOTICE(A)
Definition: JMessage.hh:64
then JAsciiDB q upi pbs
Definition: JTuneHV.sh:149
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
static const std::string Test_t
ISO-8601 standard.
bool is_valid() const
Check validity.
Definition: JPBS_t.hh:110
int debug
debug level
Definition: JSirene.cc:63
const double NOMINAL_GAIN
Specification for normalized gain corresponding to a one photo-electron pulse.
General purpose messaging.
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:328
#define FATAL(A)
Definition: JMessage.hh:67
const JPMT & getPMT(const JPMTIdentifier &id) const
Get PMT parameters.
Direct access to module in detector data structure.
then echo n User name
Definition: JCookie.sh:45
int VERSION
Definition: JUPI.hh:26
std::string VARIANT
Definition: JUPI.hh:25
static const std::string EndTime_t
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Auxiliary class to define a range between two values.
Auxililary class to get date and time.
Utility class to parse command line options.
Simple wrapper for UUID.
Definition: JUUID.hh:22
Data structure for PMT high-voltage calibration.
nlohmann::json json
static const std::string Type_t
static const std::string StartTime_t
Product breakdown structure (PBS).
Definition: JPBS_t.hh:25
static const std::string User_t
Detector calibration key words for JSON I/O.
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
JPBS_t PBS
constraint
Definition: JUPI.hh:24
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 CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:38