Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
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"
24#include "JSon/JSon.hh"
25
26#include "Jeep/JParser.hh"
27#include "Jeep/JMessage.hh"
28
29namespace {
30
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
44 JRootFileReader<JDAQHeader> in(file_name);
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 */
66int main(const int argc, const char * const argv[])
67{
68 using namespace std;
69 using namespace JPP;
70
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
139
140 for (JDetectorIntegration parameters; rs >> parameters; ) {
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
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
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}
string outputFile
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ASSERT(A,...)
Assert macro.
Definition JMessage.hh:90
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
#define WARNING(A)
Definition JMessage.hh:65
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
#define MAKE_CSTRING(A)
Make C-string.
Definition JPrint.hh:72
Auxiliary class to define a range between two values.
nlohmann::json json
int main(const int argc, const char *const argv[])
Auxiliary class for PMT thresholds.
General exception.
Definition JException.hh:24
Utility class to parse command line options.
Definition JParser.hh:1698
Range of values.
Definition JRange.hh:42
int getDetectorID() const
Get detector identifier.
int getRunNumber() const
Get run number.
static const char *const _2SToT
Histogram naming.
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition JDB.hh:438
const char * getTable()
Get table name.
Definition JDB.hh:373
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition JDB.hh:108
@ debug_t
debug
Definition JMessage.hh:29
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
std::vector< event_type > data_type
Definition JPerth.cc:81
static const std::string Tests_t
static const std::string User_t
static const std::string Location_t
static const std::string Time_t
static const std::string Test_t
static const std::string OK_t
static const std::string End_t
static const std::string Type_t
static const std::string Start_t
std::map< int, range_type > map_type
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Type definition of range.
Definition JHead.hh:43
Calibration.
Definition JHead.hh:330
Detector file.
Definition JHead.hh:227
std::string LOGIN
Definition JPersons.hh:26
std::string LOCATIONID
Definition JPersons.hh:29
Wrapper class for server name.
Definition JDB.hh:54
Universal product identifier (UPI).
Definition JUPI_t.hh:32
Template definition for getting table specific selector.
Data structure for PMT threshold calibration.
Auxiliary class for date and time.
std::string toString() const
Get ASCII formatted date and time.