Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JTuneTH.cc File Reference

Auxiliary program to compute PMT thresholds from scan. More...

#include <string>
#include <iostream>
#include <vector>
#include "dbclient/KM3NeTDBClient.h"
#include "JDB/JDB.hh"
#include "JDB/JDBToolkit.hh"
#include "JDB/JDBincludes.hh"
#include "JDB/JProductRouter.hh"
#include "JDB/JSelector.hh"
#include "JDB/JSelectorSupportkit.hh"
#include "JDetector/JPMTParametersMap.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JDetectorCalibration.hh"
#include "JSupport/JMeta.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include "JCalibrate/JParameters.hh"
#include "JSon/JSon.hh"
#include "JROOT/JManager.hh"

Go to the source code of this file.

Functions

int main (const int argc, const char *const argv[])
 

Detailed Description

Auxiliary program to compute PMT thresholds from scan.

Author
acreusot

Definition in file JTuneTH.cc.

Function Documentation

int main ( const int  argc,
const char *const  argv[] 
)

Definition at line 38 of file JTuneTH.cc.

39 {
40  using namespace std;
41  using namespace JPP;
42 
43  string usr;
44  string pwd;
45  string cookie;
46  string detectorFile;
47  string inputFile;
48  string outputFile;
49  double stfCut;
50  string testType;
51  int debug;
52 
53  try {
54 
55  JParser<> zap("Auxiliary program to compute the PMT thresholds according to the small ToT fraction (i.e. the noise to signal ratio).");
56 
57  zap['u'] = make_field(usr) = "";
58  zap['!'] = make_field(pwd) = "";
59  zap['C'] = make_field(cookie) = "";
60  zap['a'] = make_field(detectorFile, "detector file");
61  zap['f'] = make_field(inputFile, "output file of JIntegrateToT");
62  zap['o'] = make_field(outputFile, "output file for json");
63  zap['t'] = make_field(stfCut, "small ToT fraction cut-off") = 0.5;
64  zap['T'] = make_field(testType, "test type") = "TH-TUNING-SEA-v1";
65  zap['d'] = make_field(debug) = 1;
66  zap(argc, argv);
67  }
68  catch(const exception& error) {
69  FATAL(error.what() << endl);
70  }
71 
72  JDateAndTime timer;
73  // retrieve the detector
75  try {
76  load(detectorFile, detector);
77  }
78  catch(const JException& error) {
79  FATAL(error);
80  }
81  const JModuleRouter moduleRouter(detector);
82  const int detId = detector.getID();
83 
84  JPersons person;
85  vector<string> runNumbers;
86  vector<JParameters> vParameters;
87 
88  // deal with the database
89  try {
90  NOTICE("connecting to the database..." << endl);
92  JDB::reset(usr, pwd, cookie);
93  {
94  JSelector selector = getSelector<JPersons>(JDB::get()->User());
95  ResultSet& rs = getResultSet(getTable<JPersons>(), selector);
96  rs >> person;
97  rs.Close();
98  }
99 
100  // retrieve the detector integration parameters from the DB
101  vector<JDetectorIntegration> detectorInt;
102  try {
104  ResultSet& rs = getResultSet(getTable<JDetectorIntegration>(), getSelector<JDetectorIntegration>(detId));
105  while (rs >> parameters) {
106  detectorInt.push_back(parameters);
107  }
108  rs.Close();
109  }
110  catch(const exception& error) {
111  FATAL(error.what() << endl);
112  }
113 
114  // read the input file and file up the JParameters vector
115  ifstream istr(inputFile.c_str());
116  int prevRunId = -1;
117  for (JParameters curPar; istr >> curPar;) {
118  if (curPar.runId != prevRunId) {
119  NOTICE("Extracting thresholds for run " << curPar.runId << endl);
120  runNumbers.push_back(to_string(curPar.runId));
121  }
122  // insert this JParameters in the vector of JParameters
123  vParameters.push_back(curPar);
124  prevRunId = curPar.runId;
125  }
126  istr.close();
127 
128  for (unsigned int r = 0; r < runNumbers.size(); ++r) {
129  // retrieve the OID from the database with the detId and the run number
130  string rs_oid;
131  try {
133  ResultSet& rs = getResultSet(getTable<JRuns>(), getSelector<JRuns>(detId, stoi(runNumbers[r])));
134  if (rs >> parameters) {
135  rs_oid = parameters.RUNSETUPID;
136  }
137  rs.Close();
138  DEBUG("Run setup " << rs_oid << endl);
139  }
140  catch(const exception& error) {
141  FATAL(error.what() << endl);
142  }
143 
144  // retrieve OPTICS parameters (for whatever it means) from the database
145  JAllParams upars;
146  try {
148  ResultSet& rs = getResultSet(getTable<JAllParams>(), getSelector<JAllParams>("OPTICS"));
149  while (rs >> parameters) {
150  if (parameters.NAME.find("PMT_THRESHOLD") != string::npos) {
151  upars = parameters;
152  }
153  }
154  rs.Close();
155  }
156  catch(const exception& error) {
157  FATAL(error.what() << endl);
158  }
159 
160  // retrieve the parameters from the database with the OID and detId
161  // and with a cut in the OPTICS
162  map<string, int> umap;
163  try {
165  ResultSet& rs = getResultSet(getTable<JRunsetupParams>(), getSelector<JRunsetupParams>(getDetector(detId), rs_oid));
166  while (rs >> parameters) {
167  if (parameters.PAR_OID == upars.OID && parameters.ISINPUT == 'Y') {
168  if (parameters.VALUE != "") {
169  umap[parameters.UPIFILTER] = to_value<int>(parameters.VALUE);
170  }
171  }
172  }
173  rs.Close();
174  }
175  catch(const exception& error) {
176  FATAL(error.what() << endl);
177  }
178 
179  // match the threshold with the detector integration parameters
180  for (vector<JDetectorIntegration>::const_iterator i = detectorInt.begin(); i != detectorInt.end(); ++i) {
181  if (i->PMTID != -1) {
182  const JUPI_t upi = i->PMTUPI;
183  map<string, int>::const_iterator p = umap.find(upi.toString());
184  if (p == umap.end()) {
185  ostringstream os;
186  os << upi.getPBS() << JUPI_t::SEPARATOR
187  << upi.getVariant() << JUPI_t::SEPARATOR
188  << JUPI_t::DOT;
189  p = umap.find(os.str());
190  }
191  if (p != umap.end()) {
192  const JPMTIdentifier id(i->DOMID, i->CABLEPOS);
193  parameters[id].threshold = p->second;
194  } else {
195  ERROR("Missing threshold data PMT " << upi << endl);
196  }
197  }
198  }
199 
200  // fill the JParameters vector with threshold information
201  for (unsigned int pmt = 0; pmt < vParameters.size(); ++pmt) {
202  const int domId = vParameters[pmt].domId;
203  const int pmtId = vParameters[pmt].pmtId;
204  const JPMTIdentifier id(domId, pmtId);
205  const double threshold = parameters[id].threshold;
206  if (vParameters[pmt].runId == stoi(runNumbers[r])) {
207  vParameters[pmt].threshold = threshold;
208  }
209  }
210  }
211 
212  // read the JParameters vector to find optimal threshold
213  JTHCalibration THcalibrations;
214  int totalPmts = 0;
215  int validPmts = 0;
216  bool newPmt = true;
217  int previousSerial = -1;
218  double optth = -1;
219  double defaultth = -1;
220  bool curbc = true;
221  bool calibratedPmt = false;
222  sort(vParameters.begin(), vParameters.end());
223  for (unsigned int pmt = 0; pmt < vParameters.size(); ++pmt) {
224  const int domId = vParameters[pmt].domId;
225  const int pmtId = vParameters[pmt].pmtId;
226  const int pmtSerial = moduleRouter.getModule(domId).getPMT(pmtId).getID();
227  if (pmtSerial != previousSerial) {
228  newPmt = true;
229  ++totalPmts;
230  }
231  previousSerial = pmtSerial;
232  if ((calibratedPmt == true) && (newPmt == false)) continue;
233  const int duId = moduleRouter.getModule(domId).getString();
234  const int floorId = moduleRouter.getModule(domId).getFloor();
235  const JLocation_t location(duId, floorId, pmtId);
236  if (newPmt == true) {
237  defaultth = vParameters[pmt].threshold;
238  curbc = true;
239  calibratedPmt = false;
240  }
241  const double curth = vParameters[pmt].threshold;
242  const double noise = vParameters[pmt].noise;
243  const double signal = vParameters[pmt].signal;
244  const double ratio = signal ? noise/signal : 0;
245  if ((ratio > stfCut) || (ratio == 0)) {
246  curbc = true;
247  } else {
248  ++validPmts;
249  curbc = false;
250  optth = curth;
251  calibratedPmt = true;
252  }
253  if (curbc == true) {
254  if (vParameters[pmt].runId == stoi(runNumbers.back())) {
255  NOTICE("Bad channel for pmt " << pmtId << "(upi "
256  << pmtSerial << "), floor " << floorId << "(dom " << domId << "), du "
257  << duId << endl);
258  }
259  } else {
260  if (optth > defaultth) {
261  NOTICE("Non default thresholds " << optth << " for pmt " << pmtId << "(upi "
262  << pmtSerial << "), floor " << floorId << "(dom " << domId << "), du "
263  << duId << " with a STF of " << ratio << endl);
264  }
265  }
266  if (calibratedPmt == true) {
267  const JUPI_t& pmtUPI = getUPI(PBS::PMT, pmtSerial);
268  const JTHCalibration_t THcal(pmtUPI, OK_t, optth, runNumbers);
269  THcalibrations.push_back(THcal);
270  }
271  newPmt = false;
272  }
273  json js;
274  js[User_t] = person.LOGIN;
275  js[Location_t] = person.LOCATIONID;
276  js[StartTime_t] = timer.toString();
277  js[EndTime_t] = timer().toString();
278  js[Test_t + Type_t] = testType;
279  js[Tests_t] = json(THcalibrations);
280  ofstream ofs(outputFile.c_str());
281  ofs << setw(2) << setprecision(8);
282  ofs << js;
283  ofs.close();
284  NOTICE(validPmts << " calibrated PMTs among " << totalPmts << endl);
285  }
286  catch(const exception& error) {
287  FATAL(error.what() << endl);
288  }
289 }
Utility class to parse command line options.
Definition: JParser.hh:1500
General exception.
Definition: JException.hh:23
const std::string & getVariant() const
Get variant.
Definition: JUPI_t.hh:108
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
Detector data structure.
Definition: JDetector.hh:80
Router for direct addressing of module data in detector data structure.
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
Definition: diff-Tuna.sh:38
static const std::string Location_t
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
data_type r[M+1]
Definition: JPolint.hh:742
Auxiliary class for specifying selection of database data.
string outputFile
const JPBS_t & getPBS() const
Get PBS.
Definition: JPBS_t.hh:99
JDetectorsHelper getDetector
Function object for mapping serial number to object identifier of detector and vice versa...
Definition: JDBToolkit.cc:5
Detector file.
Definition: JHead.hh:196
Auxiliary data structure for location of product in detector.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
static const std::string Test_t
Auxiliary class for map of PMT parameters.
int debug
debug level
Definition: JSirene.cc:63
#define FATAL(A)
Definition: JMessage.hh:67
std::string toString() const
Convert UPI.
Definition: JUPI_t.hh:141
void reset(T &value)
Reset value.
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.
nlohmann::json json
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.
Definition: JDB.hh:269
static const std::string StartTime_t
static const std::string User_t
Detector calibration key words for JSON I/O.
JUPIHelper getUPI
Function object for mapping PBS and serial number to UPI.
Definition: JDBToolkit.cc:7
do set_variable DETECTOR_TXT $WORKDIR detector
Template definition for getting table specific selector.
Data structure for PMT threshold calibration.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std::string RUNSETUPID
Definition: JRuns.hh:26