Jpp  15.0.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTunePMTThreshold.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <vector>
4 
5 #include "TROOT.h"
6 #include "TFile.h"
7 #include "TH2D.h"
8 
10 
11 #include "JDB/JDB.hh"
12 #include "JDB/JSelector.hh"
14 #include "JDB/JDBToolkit.hh"
15 #include "JDB/JPersons.hh"
16 #include "JDB/JPMTThreshold.hh"
20 #include "JTools/JRange.hh"
21 #include "JLang/JLangToolkit.hh"
22 #include "JLang/JComparator.hh"
23 #include "JROOT/JRootFileReader.hh"
24 #include "JSon/JSon.hh"
25 
26 #include "Jeep/JParser.hh"
27 #include "Jeep/JMessage.hh"
28 
29 namespace {
30 
32  using JLANG::JException;
33 
34  /**
35  * Get DAQ header from file.
36  *
37  * \param file_name file name
38  * \return DAQ header
39  */
40  inline JDAQHeader getDAQHeader(const char* const file_name)
41  {
42  using namespace JPP;
43 
45 
46  if (in.hasNext()) {
47 
48  const JDAQHeader* p = in.next();
49 
50  if (!in.hasNext()) {
51  return *p;
52  }
53  }
54 
55  THROW(JException, "Error reading DAQ header from file " << file_name);
56  }
57 }
58 
59 
60 /**
61  * \file
62  * Auxiliary program to compute the PMT thresholds according to the small time-over-threshold fraction.
63  *
64  * \author acreusot, mdejong
65  */
66 int main(const int argc, const char * const argv[])
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;
79  JRange_t range;
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
General exception.
Definition: JException.hh:23
Auxiliary class for PMT thresholds.
#define WARNING(A)
Definition: JMessage.hh:65
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
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
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
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.
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
Detector file.
Definition: JHead.hh:196
#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
General purpose messaging.
#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.
Auxiliary class to define a range between two values.
Auxililary class to get date and time.
Utility class to parse command line options.
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:41
static const std::string End_t
static const std::string Start_t
Template definition for getting table specific selector.
ROOT file reader.