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

Auxiliary program to compute the PMT thresholds according to the small time-over-threshold fraction. More...

#include <string>
#include <iostream>
#include <vector>
#include "TROOT.h"
#include "TFile.h"
#include "TH2D.h"
#include "km3net-dataformat/online/JDAQHeader.hh"
#include "JDB/JDB.hh"
#include "JDB/JSelector.hh"
#include "JDB/JSelectorSupportkit.hh"
#include "JDB/JDBToolkit.hh"
#include "JDB/JPersons.hh"
#include "JDB/JPMTThreshold.hh"
#include "JDB/JDetectorIntegration.hh"
#include "JCalibrate/JCalibrateToT.hh"
#include "JDetector/JDetectorCalibration.hh"
#include "JTools/JRange.hh"
#include "JLang/JLangToolkit.hh"
#include "JLang/JComparator.hh"
#include "JROOT/JRootFileReader.hh"
#include "JSon/JSon.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.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 the PMT thresholds according to the small time-over-threshold fraction.

Author
acreusot, mdejong

Definition in file JTunePMTThreshold.cc.

Function Documentation

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

Definition at line 66 of file JTunePMTThreshold.cc.

67 {
68  using namespace std;
69  using namespace JPP;
70 
71  typedef JRange<double> JRange_t;
72 
73  JServer server;
74  string usr;
75  string pwd;
76  string cookie;
77  vector<string> inputFile;
78  string outputFile;
80  double fraction;
81  string testType;
82  int debug;
83 
84  try {
85 
86  JParser<> zap("Auxiliary program to compute the PMT thresholds according to the small time-over-threshold fraction.");
87 
88  zap['s'] = make_field(server) = getServernames();
89  zap['u'] = make_field(usr) = "";
90  zap['!'] = make_field(pwd) = "";
91  zap['C'] = make_field(cookie) = "";
92  zap['f'] = make_field(inputFile, "list of file names (output of JCalibrateToT)");
93  zap['o'] = make_field(outputFile, "output file for JSon") = "";
94  zap['x'] = make_field(range, "integration range for noise.") = JRange_t(0.0, 8.0);
95  zap['t'] = make_field(fraction, "maximal fraction of signal allowed for noise.") = 0.5;
96  zap['T'] = make_field(testType, "test type") = "TH-TUNING-SEA-v1";
97  zap['d'] = make_field(debug, "debug.") = 2;
98 
99  zap(argc, argv);
100  }
101  catch(const exception& error) {
102  FATAL(error.what() << endl);
103  }
104 
105 
106  if (inputFile.empty()) {
107  FATAL("No input files.");
108  }
109 
110  JDateAndTime timer;
111  JPersons person;
112 
113  try {
114 
115  JDB::reset(usr, pwd, cookie);
116 
117  ResultSet& rs = getResultSet(getTable<JPersons>(), getSelector<JPersons>(JDB::get()->User()));
118 
119  rs >> person;
120 
121  rs.Close();
122  }
123  catch(const exception& error) {
124  FATAL(error.what() << endl);
125  }
126 
127 
128  const JDAQHeader header = getDAQHeader(inputFile[0].c_str());
129  const int ID = header.getDetectorID();
130 
131  typedef map<int, JDetectorIntegration> module_type;
132  typedef map<int, module_type> detector_type;
133 
134  detector_type detector;
135 
136  try {
137 
138  ResultSet& rs = getResultSet(getTable<JDetectorIntegration>(), getSelector<JDetectorIntegration>(ID));
139 
141  detector[parameters.DOMID][parameters.CABLEPOS] = parameters;
142  }
143 
144  rs.Close();
145  }
146  catch(const exception& error) {
147  FATAL(error.what() << endl);
148  }
149 
150 
151  struct parameters_type {
152  int threshold;
153  double signal;
154  double noise;
155  };
156 
157  typedef vector<parameters_type> data_type;
158  typedef map<JUPI_t, data_type> map_type;
159 
160  map_type data;
161  vector<int> runs;
162 
163  for (vector<string>::const_iterator i = inputFile.begin(); i != inputFile.end(); ++i) {
164 
165  const JDAQHeader header = getDAQHeader(i->c_str());
166 
167  ASSERT(header.getDetectorID() == ID, "file: " << *i);
168 
169  runs.push_back(header.getRunNumber());
170 
171  const JPMTThreshold getPMTThreshold(header.getDetectorID(), header.getRunNumber());
172 
173  TFile* in = TFile::Open(i->c_str(), "exist");
174 
175  if (in == NULL || !in->IsOpen()) {
176  FATAL("File: " << *i << " not opened." << endl);
177  }
178 
179  for (detector_type::const_iterator module = detector.begin(); module != detector.end(); ++module) {
180 
181  if (!module->second.empty()) {
182 
183  TH2D* h2s = (TH2D*) in->Get(MAKE_CSTRING(module->first << _2SToT));
184 
185  if (h2s != NULL) {
186 
187  for (int ix = 1; ix <= h2s->GetXaxis()->GetNbins(); ++ix) {
188 
189  double noise = 0.0;
190  double signal = 0.0;
191 
192  for (int iy = 1; iy <= h2s->GetYaxis()->GetNbins(); ++iy) {
193 
194  const double x = h2s->GetYaxis()->GetBinCenter(iy);
195  const double y = h2s->GetBinContent(ix, iy);
196 
197  signal += y;
198 
199  if (range(x)) {
200  noise += y;
201  }
202  }
203 
204  const JUPI_t upi = module->second.at(ix-1).PMTUPI;
205 
206  data[upi].push_back({ getPMTThreshold(upi).value, signal, noise});
207  }
208 
209  } else {
210 
211  WARNING("No histogram for module " << module->first << "; skip." << endl);
212  }
213  }
214  }
215 
216  in->Close();
217  }
218 
219 
220  JPMTThresholdCalibration calibration;
221 
222  for (map_type::iterator i = data.begin(); i != data.end(); ++i) {
223 
224  sort(i->second.begin(), i->second.end(), make_comparator(&parameters_type::threshold));
225 
226  int threshold = i->second.begin()->threshold;
227 
228  for (data_type::const_iterator p = i->second.begin(); p != i->second.end(); ++p) {
229 
230  if (p->noise <= fraction * p->signal) {
231 
232  threshold = p->threshold;
233 
234  break;
235  }
236  }
237 
238  if (debug >= debug_t || threshold > i->second.begin()->threshold) {
239 
240  cout << "PMT " << left << setw(32) << i->first << " -> " << right << setw(3) << threshold << " (" << i->second.begin()->threshold << ")" << endl;
241 
242  for (data_type::const_iterator p = i->second.begin(); p != i->second.end(); ++p) {
243  DEBUG(setw(3) << p->threshold << ' ' << FIXED(7,0) << p->noise << "/" << FIXED(7,0) << p->signal << ' ' << (p->noise <= fraction * p->signal) << endl);
244  }
245  }
246 
247  calibration.push_back(JPMTThresholdCalibration_t(i->first.toString(), OK_t, threshold, runs));
248  }
249 
250 
251  if (outputFile != "") {
252 
253  json js;
254 
255  js[User_t] = person.LOGIN;
256  js[Location_t] = person.LOCATIONID;
257  js[Start_t + Time_t] = timer.toString();
258  js[End_t + Time_t] = timer().toString();
259  js[Test_t + Type_t] = testType;
260  js[Tests_t] = json(calibration);
261 
262  ofstream out(outputFile.c_str());
263 
264  out << setw(2) << setprecision(8);
265  out << js;
266 
267  out.close();
268  }
269 }
Utility class to parse command line options.
Definition: JParser.hh:1500
Auxiliary class for PMT thresholds.
#define WARNING(A)
Definition: JMessage.hh:65
debug
Definition: JMessage.hh:29
Data structure for PMT threshold calibration.
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
Definition: JComparator.hh:185
static const std::string Time_t
int getDetectorID() const
Get detector identifier.
static const std::string Tests_t
static const std::string OK_t
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
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:151
static const std::string Location_t
then let ID
Definition: JAcoustics.sh:31
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
int getRunNumber() const
Get run number.
Type definition of range.
Definition: JHead.hh:39
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
Detector file.
Definition: JHead.hh:224
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
static const std::string Test_t
int debug
debug level
Definition: JSirene.cc:63
#define FATAL(A)
Definition: JMessage.hh:67
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
void reset(T &value)
Reset value.
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
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 char *const _2SToT
Histogram naming.
static const std::string User_t
Detector calibration key words for JSON I/O.
do set_variable DETECTOR_TXT $WORKDIR detector
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:42
static const std::string End_t
static const std::string Start_t
Template definition for getting table specific selector.