16 #include "dbclient/KM3NeTDBClient.h"
40 #include "TGraphErrors.h"
49 int main(
int argc,
char **argv)
57 typedef typename JMultiMap_t::super_const_iterator JSuperIt_t;
79 JParser<> zap(
"Example program to find optimal HV-settings.");
84 zap[
'f'] =
make_field(inputFiles,
"map of run numbers and associated ToT-fit root files (JFitToT output)");
86 zap[
'T'] =
make_field(testType,
"test type") =
"HV-TUNING-SEA-GAIN-v1";
87 zap[
'P'] =
make_field(testProduct,
"test product") =
"";
88 zap[
'e'] =
make_field(HVerr,
"Error on high-voltage measurements [V]") = 5.0;
90 zap[
'o'] =
make_field(graphFile,
"Graphical output (root format)") =
"";
95 catch(
const exception &error) {
96 FATAL(error.what() << endl);
100 time_t startTime = time(0);
114 ResultSet& rs =
getResultSet(getTable<JPersons>(), selector);
123 ResultSet& rs =
getResultSet(getTable<JDetectorIntegration>(), selector);
127 if (detint.PMTID >= 0) {
129 const JUPI upi = detint.PMTUPI;
131 locMap[detint.DOMID][detint.DUID][detint.FLOORID][detint.PMTID] = upi;
142 const int runNr = fileIt->first;
144 NOTICE(
"Extracting Gain-/HV-data for run " << runNr << endl);
152 ResultSet& rs =
getResultSet(getTable<JPMTHVRunSettings>(), selector);
157 for (JSuperIt_t locIt = locMap.super_begin(); locIt != locMap.super_end(); ++locIt) {
159 if (parameters.DUID == locIt->second->first &&
160 parameters.FLOORID == locIt->second->second->first &&
161 parameters.PMTINTID == locIt->second->second->second->first) {
163 const JUPI upi = locIt.getValue();
165 HVmap[upi] = log(fabs(parameters.HV_VALUE));
175 TFile
in(fileIt->second.c_str(),
"READ");
176 TIter next(
in.GetListOfKeys());
178 while(TKey* key = (TKey*)next()) {
180 const string name(key->GetName());
181 if (!gROOT->GetClass(key->GetClassName())->InheritsFrom(
"TH1") ||
182 name.find(
"1ToT") == string::npos) {
continue; }
184 const int pos =
name.find(
".");
185 const int modID = stoi(
name.substr(0, pos));
186 const int pmtID = stoi(
name.substr(pos+1, 2));
188 for (JSuperIt_t locIt = locMap.super_begin(); locIt != locMap.super_end(); ++locIt) {
190 if (modID == locIt->first &&
191 pmtID == locIt->second->second->second->first) {
193 const JUPI upi = locIt.getValue();
194 const double logAbsHV = HVmap[upi];
196 const TH1*
h1 = (TH1*) key->ReadObj();
199 double gain, gainErr;
202 const int Ngain = f1->GetParNumber(
"gain");
204 gain = f1->GetParameter(Ngain);
205 gainErr = f1->GetParError (Ngain);
209 const double prevG = (dataMap[upi].size() > 0 ?
210 (--(dataMap[upi].lower_bound(logAbsHV)))->second.first : log(
FITTOT_GAIN_MIN));
216 dataMap[upi][logAbsHV] = element_t{log(gain), gainErr / gain};
222 catch (
const exception& error) {
223 FATAL(error.what() << endl);
233 NOTICE(
"Searching optimal high-voltage settings...\n\t" <<
234 "High-Voltage [V] --- Gain [-]" << endl);
236 for (datamap_t::const_iterator
pmt = (testProduct.empty() ? dataMap.cbegin() : dataMap.find(testProduct)) ;
237 pmt != (testProduct.empty() ? dataMap.cend() : ++(dataMap.find(testProduct)));
243 string location, serialID;
244 for (JSuperIt_t locIt = locMap.super_begin(); locIt != locMap.super_end(); ++locIt) {
246 if (locIt.getValue().getVariant() == upi.
getVariant()) {
250 sprintf(buffer,
"%02d.%02d.%02d",
251 locIt->second->first,
252 locIt->second->second->first,
253 locIt->second->second->second->first);
254 location = string(buffer);
256 sprintf(buffer,
"%08d.%02d",
258 locIt->second->second->second->first);
259 serialID = string(buffer);
263 NOTICE(upi <<
" (a.k.a. " << serialID <<
" / " << location <<
"):\n\t");
265 if (data.size() < 2) {
267 WARNING(
"Insufficient data; skip!" << endl);
271 manager[serialID]->Set(data.size());
272 manager[serialID]->SetMarkerStyle(kFullDotSmall);
278 double minDistance = (D2 > D1 ? D2 : D1);
286 const int dIt =
distance(data.cbegin(), it);
287 manager[serialID]->SetPoint(dIt, it->first / log(10), it->second.first / log(10));
288 manager[serialID]->SetPointError(dIt, fabs(HVerr /
exp(it->first) / log(10)), fabs(it->second.second / log(10)));
292 const double distance = fabs(logNominalGain - it->second.first);
294 if (distance < minDistance) {
298 k1 = ( it->second.first < logNominalGain ?
299 (it != --data.cend() ? next(it,1) : prev(it,1)) :
300 (it != data.cbegin() ? prev(it,1) : next(it,1)) );
311 if ((k1->first < k0->first && k1->second.first < k0->second.first) ||
312 (k1->first > k0->first && k1->second.first > k0->second.first)) {
316 const double logHV0 = k0->first;
317 const double logHV1 = k1->first;
319 const double logHV0err = HVerr /
exp(logHV0);
320 const double logHV1err = HVerr /
exp(logHV1);
322 const double logGain0 = k0->second.first;
323 const double logGain1 = k1->second.first;
325 const double logGain0err = k0->second.second;
326 const double logGain1err = k1->second.second;
329 const double slope = (logGain1 - logGain0) / (logHV1 - logHV0);
330 const double logNominalHV = (logNominalGain - logGain0) / slope + logHV0;
332 const double err1 = logHV0err * logHV0err;
333 const double err2 = logGain0err * logGain0err / slope / slope;
334 const double err3 = ( pow(logNominalGain - logGain0 / slope, 2.0) *
335 ((logGain0err*logGain0err + logGain1err*logGain1err) / pow(logGain1 - logGain0, 2.0) +
336 (logHV0err*logHV0err + logHV1err*logHV1err) / pow(logHV1 - logHV0, 2.0)) );
338 const double logNominalHVerr = sqrt(err1 + err2 + err3);
340 if (fabs(logNominalHV - logHV0) / fabs(logHV1 - logHV0) < 2.0) {
345 errors.first =
exp(logNominalHV) * logNominalHVerr;
350 if ((k0 == data.cbegin() && logNominalGain < logGain0) ||
351 (k0 == --data.cend() && logNominalGain > logGain0)) {
354 "Extrapolation " << (valid ?
"successful!" :
"failed; Setting closest values!") <<
"\n\t");
359 WARNING(
"Could not find optimal high-voltage; Setting closest values!\n\t");
362 NOTICE(setw(8) << setprecision(2) << fixed << left <<
363 result.first << setw(0) <<
" +/- " << setw(4) << errors.first << setw(10) << right <<
364 result.second << setw(0) <<
" +/- " << setw(3) << errors.second << endl);
372 HVcalibrations.push_back(HVcal);
376 if (! graphFile.empty()) {
378 TFile out(graphFile.c_str(),
"RECREATE");
387 time_t stopTime = time(0);
392 js[
User_t] = person.LOGIN;
401 ofs << setw(2) << setprecision(8);
Utility class to parse command line options.
JMultiMap is a general purpose multidimensional map based on a type list of maps. ...
static const std::string Failed_t
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
static const std::string Tests_t
static const std::string OK_t
static const double FITTOT_GAIN_MAX
Maximal gain.
static const double FITTOT_GAIN_MIN
Minimal gain.
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
T get(const JHead &header)
Get object from header.
*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
then for HISTOGRAM in h0 h1
static const std::string Location_t
Dynamic ROOT object management.
Auxiliary class for specifying selection of database data.
Data structure for detector geometry and calibration.
#define MAKE_STRING(A)
Make string.
esac $JPP_DIR examples JDetector JTransitTime o $OUTPUT_FILE n N $NPE T $TTS_NS d $DEBUG for HISTOGRAM in tts tt2 pmt
then echo Variable JPP_DIR undefined exit fi source $JPP_DIR setenv sh $JPP_DIR set_variable DEBUG set_variable NPE set_variable FIT_RANGE set_variable OUTPUT_DIR tmp set_variable OUTPUT_JSON $OUTPUT_DIR HVtuning json set_variable OUTPUT_ROOT $OUTPUT_DIR HVtuning root set_variable FIT_OPTIONS RME set_variable PMT_DEFAULT gain
Auxiliary class to manage set of compatible ROOT objects (e.g.
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
static const std::string Test_t
const std::string & getVariant() const
Get variant.
const double NOMINAL_GAIN
Specification for normalized gain corresponding to a one photo-electron pulse.
General purpose messaging.
static const JDetectorsHelper & getDetector
Function object for mapping serial number and object identifier of detectors.
Universal product identifier (UPI).
static const std::string EndTime_t
Utility class to parse command line options.
Data structure for PMT high-voltage calibration.
static const std::string Type_t
std::string to_string(const T &value)
Convert value to string.
ResultSet & getResultSet(const std::string &query)
Get result set.
static const std::string StartTime_t
std::string get_date_string(const std::time_t &time)
Create ISO 8601 date string for output file header.
static const std::string User_t
Detector calibration key words for JSON I/O.
This file is automatically created by make.
Template definition for getting table specific selector.
static const std::string FITTOT_FNAME
then set_variable FORMULA *[0] exp(-0.5 *(x-[1])*(x-[1])/([2]*[2]))" set_variable OUTPUT_FILE histogram.root JHistogram1D -o $WORKDIR/$OUTPUT_FILE -F "$FORMULA" -
bool putObject(TDirectory *dir, const T &object)
Write object to ROOT directory.
int main(int argc, char *argv[])