Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JTuneHV.cc File Reference

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

#include <string>
#include <map>
#include <fstream>
#include <sstream>
#include "JSystem/JDateAndTime.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 "JSupport/JSingleFileScanner.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 "JDB/JDBSupportkit.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

◆ main()

int main ( int argc,
char ** argv )

Definition at line 126 of file JTuneHV.cc.

127{
128 using namespace std;
129 using namespace JPP;
130
131 string inputFile;
132 string detectorFile;
133 string outputFile;
134 string UPIFile;
135
136 JDBAPIVersion APIversion;
137 string testType;
138 string login;
139 string locationID;
140 vector<int> runNumbers;
141 int elapsedTime = 0;
142
143 double HVstepMin = 2 * 3.14; // Minimal HV step [V]
144 JRange<double> HVrange (-1500 + 1e-2,
145 -80 - 1e-2); // Allowed HV range [V]
146 JRange<double> gainRange(FITTOT_GAIN_MIN + 1e-2,
147 FITTOT_GAIN_MAX - 1e-2); // Allowed gain fit-range
148 double gainTarget = NOMINAL_GAIN; // Target nominal gain value
149
150 int debug;
151
152
153 try {
154
155 JProperties properties;
156
157 properties.insert(gmake_property(APIversion));
158 properties.insert(gmake_property(testType));
159 properties.insert(gmake_property(login));
160 properties.insert(gmake_property(locationID));
161 properties.insert(gmake_property(runNumbers));
162 properties.insert(gmake_property(elapsedTime));
163
164 JProperties settings;
165
166 settings.insert(gmake_property(HVstepMin));
167 settings.insert(gmake_property(HVrange));
168 settings.insert(gmake_property(gainRange));
169 settings.insert(gmake_property(gainTarget));
170
171 JParser<> zap("Example program to find optimal HV-settings.");
172
173 zap['f'] = make_field(inputFile, "input file (ROOT format)");
174 zap['o'] = make_field(outputFile, "output file (json format)");
175 zap['a'] = make_field(detectorFile, "detector file");
176 zap['b'] = make_field(UPIFile, "PMT UPI ASCII table");
177 zap['#'] = make_field(properties, "database information") = JPARSER::initialised();
178 zap['@'] = make_field(settings, "interpolation settings") = JPARSER::initialised();
179 zap['d'] = make_field(debug, "debug") = 1;
180
181 zap(argc, argv);
182 }
183 catch(const exception &error) {
184 FATAL(error.what() << endl);
185 }
186
187 if (login.empty() || locationID.empty()) {
188
189 FATAL("Missing user information (please specify via -#login and -#locationID).");
190
191 } else if (testType.empty()) {
192
193 FATAL("Missing database test type (please specify via -#testType).");
194
195 } else if (getDBVersionTuneHV(testType) < 3 && runNumbers.empty()) {
196
197 FATAL("Missing run numbers.");
198 }
199
200
202
203 const JUUID& UUID = JUUID::rndm();
204
205 JDateAndTime timer;
206
207 timer.sub(elapsedTime);
208
210
211 try {
212 load(detectorFile, detector);
213 }
214 catch (const exception& error) {
215 FATAL(error.what() << endl);
216 }
217
218 JModuleRouter router (detector);
219
220 JUPITable fetchUPI(UPIFile.c_str());
221
222
223 // Read input
224
225 TFile input(inputFile.c_str(), "READ");
226
227 JManager<string, TMultiGraph> manager(new TMultiGraph(), "%.HVxG");
228
229 input >> manager;
230
231 input.Close();
232
233
234 // Find optimal HV corresponding to a nominal gain of 1.0
235 // via linear interpolation in log-log scale
236
237 JHVCalibration HVcalibrations;
238
242
243 NOTICE("Searching optimal high-voltage settings..." << endl <<
244 LEFT(30) << "UPI" << CENTER(35) << "(identifer / location)" << RIGHT(35) << "Inter-/Extrapolated high-voltage [-]" << endl);
245
246 for (JManager<string, TMultiGraph>::iterator it = manager.begin(); it != manager.end(); ++it) {
247
248 const string& name = it->first;
249 JHVInterpolator data(*(it->second));
250
251 // Retrieve upi, location and serialID
252
253 const int seppos = name.find('.');
254 const int moduleID = stoi(name.substr(0, seppos));
255 const int tdc = stoi(name.substr(seppos + 1, seppos + 3));
256
257 const JModule& module = router.getModule(moduleID);
258 const JPMT& pmt = router.getPMT(JPMTIdentifier(moduleID, tdc));
259
260 const JUPI_t& pmtUPI = fetchUPI(PBS::PMT, pmt.getID());
261 const string location = MAKE_STRING(right << FILL(4,'0') << module.getLocation().getString() << '.' <<
262 right << FILL(2,'0') << module.getLocation().getFloor() << '.' <<
263 right << FILL(2,'0') << tdc);
264
265 NOTICE(LEFT(30) << pmtUPI << "(a.k.a. " << name << " / " << location << "): ");
266
267 if (data.interpolateHV(gainTarget)) {
268
269 const double result = -data.getHV ();
270 const double error = data.getHVError();
271
272 NOTICE(right << FIXED(20,2) << result << " +/- " << FIXED(4,2) << error << endl);
273
274 HVcalibrations.push_back(JHVCalibration_t(pmtUPI, OK_t, result, runNumbers, gainTarget));
275
276 } else {
277
278 WARNING("Could not find high-voltage corresponding to target gain; Setting zero." << endl);
279
280 HVcalibrations.push_back(JHVCalibration_t(pmtUPI, Fail_t, 0.0, runNumbers, 0.0));
281 }
282 }
283
284 // Update graphical output
285
286 vector<JMeta> metaInfo;
287 string metaInfoStr;
288
289 metaInfo.push_back(JMeta(argc, argv));
290 metaInfoStr += MAKE_STRING(metaInfo.back());
291
292 for (JSingleFileScanner<JMeta> in(inputFile); in.hasNext(); ) {
293
294 metaInfo.push_back(*in.next());
295 metaInfoStr += MAKE_STRING(metaInfo.back());
296 }
297
298 TFile output(inputFile.c_str(), "RECREATE");
299
300 for (vector<JMeta>::iterator i = metaInfo.begin(); i != metaInfo.end(); ++i) {
301 putObject(output, *i);
302 }
303
304 output << manager;
305
306 output.Close();
307
308 // Write json output
309
310 json js;
311
312 if (APIversion.getMajorVersion() == 2) {
313
314 json error = { {Message_t, "" },
315 {Code_t, OK_t },
316 {Arguments_t, json::array() } };
317
318 json metaData = { {Configuration_t, metaInfoStr },
319 {UUID_t, MAKE_STRING(UUID) } };
320
321 json data = { {Provenance_t + Info_t, json(metaData) },
322 {User_t, login },
323 {Location_t, locationID },
324 {Start_t + Time_t, timer.toString() },
325 {End_t + Time_t, timer().toString() },
326 {Test_t + Type_t, testType },
327 {Tests_t, json(HVcalibrations) } };
328
329 js[APIVersion_t] = MAKE_STRING(APIversion);
330 js[Data_t + Type_t] = MAKE_STRING("ProductTestSession");
331 js[Encoding_t] = MAKE_STRING("NativeJSON");
332 js[Error_t] = json(error);
333 js[Start_t] = timer.toString();
334 js[End_t] = timer().toString();
335 js[Data_t][0] = json(data);
336
337 } else if (APIversion.getMajorVersion() == 1) {
338
339 js[User_t] = login;
340 js[Location_t] = locationID;
341 js[Test_t + Type_t] = testType;
342 js[Start_t + Time_t] = timer.toString();
343 js[End_t + Time_t] = timer().toString();
344 js[Tests_t] = json(HVcalibrations);
345
346 } else {
347
348 FATAL("Invalid API version <" << APIversion << "> accepted major versions 1 and 2." << endl);
349 }
350
351 ofstream ofs(outputFile.c_str());
352
353 ofs << setw(2) << setprecision(8);
354 ofs << js;
355
356 ofs.close();
357}
string outputFile
#define NOTICE(A)
Definition JMessage.hh:64
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
#define WARNING(A)
Definition JMessage.hh:65
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
#define MAKE_STRING(A)
Make string.
Definition JPrint.hh:63
#define gmake_property(A)
macros to convert (template) parameter to JPropertiesElement object
nlohmann::json json
Detector data structure.
Definition JDetector.hh:96
const JLocation & getLocation() const
Get location.
Definition JLocation.hh:70
int getFloor() const
Get floor number.
Definition JLocation.hh:146
int getString() const
Get string number.
Definition JLocation.hh:135
Router for direct addressing of module data in detector data structure.
Data structure for a composite optical module.
Definition JModule.hh:75
Data structure for PMT geometry, calibration and status.
Definition JPMT.hh:49
Utility class to parse parameter values.
int getID() const
Get identifier.
Definition JObjectID.hh:50
Utility class to parse command line options.
Definition JParser.hh:1698
Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys.
Definition JManager.hh:47
Object reading from a list of files.
virtual bool hasNext() override
Check availability of next element.
Range of values.
Definition JRange.hh:42
static const double FITTOT_GAIN_MAX
Default maximal gain.
Definition JFitToT.hh:45
static const double FITTOT_GAIN_MIN
Default minimal gain.
Definition JFitToT.hh:44
static const JPBS_t PMT(3, 4, 2, 3)
PBS of photo-multiplier tube (PMT)
static JDBTestTypesTuneHV & getDBVersionTuneHV
Function object for looking up the HV-tuning database version number corresponding to a specific test...
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
const double NOMINAL_GAIN
Specification for normalized gain corresponding to a one photo-electron pulse.
@ LEFT
Definition JTwosome.hh:18
@ RIGHT
Definition JTwosome.hh:18
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
bool putObject(TDirectory &dir, const TObject &object)
Write object to ROOT directory.
static const std::string Tests_t
static const std::string APIVersion_t
static const std::string User_t
static const std::string Encoding_t
static const std::string Message_t
static const std::string UUID_t
static const std::string Code_t
static const std::string Location_t
static const std::string Time_t
static const std::string Data_t
static const std::string Test_t
static const std::string OK_t
static const std::string Arguments_t
static const std::string Configuration_t
static const std::string End_t
static const std::string Fail_t
static const std::string Provenance_t
static const std::string Info_t
static const std::string Error_t
static const std::string Type_t
static const std::string Start_t
return result
Definition JPolint.hh:862
Auxiliary data structure for alignment of data.
Definition JManip.hh:368
Auxiliary data structure for sequence of same character.
Definition JManip.hh:330
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Detector file.
Definition JHead.hh:227
Auxiliary data structure to store high-voltage versus gain data and interpolate the nominal high-volt...
static void setHVRange(const JRange< double > range)
Set valid gain range.
static void setMinHVDistance(const double minDist)
Set minimal separation distance for high-voltage.
static void setGainRange(const JRange< double > range)
Set valid gain range.
Universal product identifier (UPI).
Definition JUPI_t.hh:32
Data structure for PMT high-voltage calibration.
static void setVersion(const int version)
Set HV-tuning database test type.
Auxiliary data structure for general purpose version number.
version_type getMajorVersion() const
Get major version.
Simple wrapper for UUID.
Definition JUUID.hh:24
static const JUUID & rndm()
Generate random UUID.
Definition JUUID.hh:78
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:68
Auxiliary class for ROOT I/O of application specific meta data.
Definition JMeta.hh:72
Auxiliary class for date and time.
std::string toString() const
Get ASCII formatted date and time.
void sub(const time_t t1)
Subtract given time.