Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JTuneHV.cc File Reference

Program for inter-/extrapolation of nominal high-voltage settings. More...

#include <string>
#include <map>
#include <fstream>
#include <sstream>
#include "JSystem/JDate.hh"
#include "JLang/JUUID.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include "JTools/JRange.hh"
#include "JSupport/JMeta.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JModuleRouter.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JDetectorCalibration.hh"
#include "JDB/JUPI.hh"
#include "JDB/JUPI_t.hh"
#include "JCalibrate/JFitToT.hh"
#include "JCalibrate/JHVInterpolator.hh"
#include "JROOT/JManager.hh"
#include "JSon/JSon.hh"
#include "TFile.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Program for inter-/extrapolation of nominal high-voltage settings.

Author
acreusot, mdejong and bjung

Definition in file JTuneHV.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

< Minimal HV step [V]

< Allowed HV range [V]

< Allowed gain fit-range

< Target nominal gain value

Definition at line 117 of file JTuneHV.cc.

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
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.
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
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
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
Detector file.
Definition: JHead.hh:196
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
#define NOTICE(A)
Definition: JMessage.hh:64
static const std::string Test_t
ISO-8601 standard.
int debug
debug level
Definition: JSirene.cc:63
const double NOMINAL_GAIN
Specification for normalized gain corresponding to a one photo-electron pulse.
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:328
#define FATAL(A)
Definition: JMessage.hh:67
then echo n User name
Definition: JCookie.sh:45
static const std::string EndTime_t
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Auxililary class to get date and time.
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
static const std::string User_t
Detector calibration key words for JSON I/O.
do set_variable DETECTOR_TXT $WORKDIR detector