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