67{
71
73
75
76 string inputFile;
78 string detectorFile;
79 string pmtFile;
81 bool reverse;
82 bool overwriteDetector;
84 bool fitAngle;
85 bool fitNoise;
86 bool fitModel;
87 double QE;
91 string transmittanceFile;
94
95 try {
96
98
114
115 JParser<> zap(
"Auxiliary program to fit PMT parameters from JMergeCalibrateK40 output.");
116
118 zap[
'f'] =
make_field(inputFile,
"input file (output from JMergeCalibrateK40).");
120 zap[
'a'] =
make_field(detectorFile,
"detector file.");
121 zap[
'P'] =
make_field(pmtFile,
"specify PMT file name that can be given to JTriggerEfficiency.") =
"";
123 "Fix time offset(s) of PMT(s) of certain module(s), e.g."
124 "\n-! \"808969848 0 808982077 23\" will fix time offsets of PMT 0 of module 808969848 and of PMT 23 of module 808982077."
125 "\nSame PMT offset can be fixed for all optical modules, e.g."
126 "\n-! \"-1 0 -1 23\" will fix time offsets of PMTs 0 and 23 of all optical modules.") =
JPARSER::initialised();
127 zap[
'r'] =
make_field(reverse,
"reverse TDC constraints due to option -! <TDC>.");
128 zap[
'A'] =
make_field(overwriteDetector,
"overwrite detector file provided through '-a' with fitted time offsets.");
129 zap[
'w'] =
make_field(writeFits,
"write fit results to ROOT file; -ww also write fitted TTS to PMT file.");
130 zap[
'D'] =
make_field(fitAngle,
"fit angular distribution; fix normalisation.");
131 zap[
'B'] =
make_field(fitNoise,
"fit background.");
132 zap[
'M'] =
make_field(fitModel,
"fit angular distribution as well as normalisation; fix PMT QEs = 1.0.");
133 zap[
'Q'] =
make_field(QE,
"fix PMT QEs") = 0.0;
139
140 zap(argc, argv);
141 }
142 catch(const exception &error) {
143 FATAL(error.what() << endl);
144 }
145
146
147 if ((fitModel ? 1 : 0) +
148 (fitAngle ? 1 : 0) +
149 (fitNoise ? 1 : 0) +
150 (QE != 0.0 ? 1 : 0) > 1) {
151 FATAL(
"Use either option -M, -D, -B or -Q" << endl);
152 }
153
159
160 if (reverse) {
162 }
163
164 for (JTDC_t::const_iterator i = TDC.begin(); i != TDC.end(); ++i) {
165 DEBUG(
"PMT " << setw(10) << i->first <<
' ' << setw(2) << i->second <<
" constrain t0." << endl);
166 }
167
168 try {
170 }
171 catch(const exception &error) {
172 FATAL(error.what() << endl);
173 }
174
176
177 try {
179 }
182 }
183
184
186
187 if (pmtFile != "") {
188 try {
189 parameters.
load(pmtFile.c_str());
190 }
191 catch(const exception& error) {}
192 }
193
194
196
197 if (transmittanceFile != "") {
198 try {
199 transmittances.
load(transmittanceFile.c_str());
200 }
201 catch(const exception& error) {}
202 }
203
204 TFile* in = TFile::Open(inputFile.c_str(), "exist");
205
206 if (in == NULL || !in->IsOpen()) {
207 FATAL(
"File: " << inputFile <<
" not opened." << endl);
208 }
209
210
212
213 TH1D h0("chi2", NULL, 500, 0.0, 5.0);
214 TH1D hn("hn", NULL, 501, -0.5, 500.0);
215 TH1D hr("rate", NULL, 500, 0.0, 25.0);
216 TH1D h1("p1", NULL, 500, -5.0, +5.0);
217 TH1D h2("p2", NULL, 500, -5.0, +5.0);
218 TH1D h3("p3", NULL, 500, -5.0, +5.0);
219 TH1D h4("p4", NULL, 500, -5.0, +5.0);
220 TH1D hc("cc", NULL, 500, -0.1, +0.1);
221 TH1D hb("bc", NULL, 500, -0.1, +0.1);
222
224
227 }
228
231
232 TH2D H2("detector", NULL,
233 string.size() + 0, -0.5, string.size() - 0.5,
235
236 for (Int_t i = 1; i <= H2.GetXaxis()->GetNbins(); ++i) {
237 H2.GetXaxis()->SetBinLabel(i,
MAKE_CSTRING(
string.at(i-1)));
238 }
239 for (Int_t i = 1; i <= H2.GetYaxis()->GetNbins(); ++i) {
241 }
242
243 TH2D* HN = (TH2D*) H2.Clone("iterations");
244
246
247 for (JDetector::iterator module =
detector.begin(); module !=
detector.end(); ++module) {
248
249 if (module->getFloor() == 0) {
250 continue;
251 }
252
254
255 NOTICE(
"Module " << setw(10) << module->getID() <<
' ' <<
getLabel(module->getLocation()) <<
" !" <<
distance(range.first, range.second) << endl);
256
258
259 if (h2d == NULL || h2d->GetEntries() == 0) {
260
261 NOTICE(
"No data for module " << module->getID() <<
" -> set QEs to 0." << endl);
262
265 }
266
267 continue;
268 }
269
271
272 for (const char c : transmittance) {
274 }
275
276 if (transmittances.count(module->getID())) {
278 model.transmittance[i].fix(transmittances[module->getID()][i]);
279 }
280 }
281
282 if (QE != 0.0) {
284 model.parameters[pmt].QE.fix(QE);
285 }
286 }
287
289
293 };
294
295 for (int ix = 1; ix <= h2d->GetXaxis()->GetNbins(); ++ix) {
296
298
300
301 double V = 0.0;
302 double W = 0.0;
303
304 for (int iy = 1; iy <= h2d->GetYaxis()->GetNbins(); ++iy) {
305
306 const double x = h2d->GetXaxis()->GetBinCenter(ix);
307 const double y = h2d->GetYaxis()->GetBinCenter(iy);
308
310
311 double value = h2d->GetBinContent(ix,iy);
312 double error = h2d->GetBinError (ix,iy);
313
314 buffer.push_back(
rate_type(y, value, error));
315
316 double width = h2d->GetYaxis()->GetBinWidth(iy);
317
318 value *= width;
319 error *= width;
320
321 V += value;
322 W += error * error;
323 }
324 }
325
326 W = sqrt(W);
327
328 if (V <= 0.0 - STDEV*W) {
329 count[0][
pair.first] += 1;
330 count[0][
pair.second] += 1;
331 }
332
333 if (V <= MINIMAL_RATE_HZ + STDEV*W) {
334 count[1][
pair.first] += 1;
335 count[1][
pair.second] += 1;
336 }
337 }
338
340
341 if (count[0][pmt] >= MAXIMAL_COUNTS) {
342
343 WARNING(
"PMT " << setw(10) << module->getID() <<
'.' <<
FILL(2,
'0') << pmt <<
FILL() <<
" some rates negative -> fit background" << endl);
344
345 if (fit.value.parameters[pmt].status) {
346 model.parameters[pmt].bg.set();
347 }
348 }
349
350 if (count[1][pmt] == NUMBER_OF_PMTS) {
351
352 WARNING(
"PMT " << setw(10) << module->getID() <<
'.' <<
FILL(2,
'0') << pmt <<
FILL() <<
" all rates to low -> disable" << endl);
353
354 model.parameters[pmt].disable();
355 }
356 }
357
358 DEBUG(
"Start value:" << endl <<
model << endl);
359
360 try {
361
363
365
367
369
370 ERROR(
"Fit result " << setw(10) << module->getID() <<
" NDF " << setw(5) <<
result.ndf <<
" -> skip" << endl);
371
372 continue;
373 }
374
375 bool refit = false;
376
378
379 if (fit.value.parameters[pmt].status) {
380
381 if (fit.value.parameters[pmt].QE() <= QE_MIN ) {
382
383 WARNING(
"PMT " << setw(10) << module->getID() <<
'.' <<
FILL(2,
'0') << pmt <<
FILL() <<
' '
384 << "QE = "
385 <<
FIXED(5,3) << fit.value.parameters[pmt].QE() <<
" +/- "
386 <<
FIXED(5,3) << fit.error.parameters[pmt].QE() <<
" "
387 << " -> disable" << (!refit ? " and refit" : "") << endl);
388
389 fit.value.parameters[pmt].disable();
390
391 refit = true;
392 }
393 }
394 }
395
397
398 if (fit.value.parameters[pmt].status) {
399
400 if (fit.value.parameters[pmt].t0.atLimit(T0_NS)) {
401
402 WARNING(
"PMT " << setw(10) << module->getID() <<
'.' <<
FILL(2,
'0') << pmt <<
FILL() <<
' '
403 << "t0 at limit "
404 <<
FIXED(7,3) << fit.value.parameters[pmt].t0() <<
" +/- "
405 <<
FIXED(7,3) << fit.error.parameters[pmt].t0());
406
407 if (refit == false) {
408
409 WARNING(
" -> disable and refit" << endl);
410
411 fit.value.parameters[pmt].disable();
412
413 refit = true;
414
415 } else {
416
418
419 fit.value.parameters[pmt].t0.set(0.0);
420 }
421 }
422 }
423 }
424
425 if (refit) {
426
428
430 if (fit.value.parameters[pmt].status) {
432 }
433 }
434
435 refit = false;
437 }
438
439 NOTICE(
"Fit result " << setw(10) << module->getID() <<
" chi2 / NDF " <<
FIXED(10,2) <<
result.chi2 <<
" / " << setw(5) <<
result.ndf <<
' ' << setw(5) << fit.numberOfIterations << endl);
440
442 fit.value.model.print(cout);
443 }
444
446
447
448
449 transmittances[module->getID()] = fit.value.transmittance;
450
451 if (writeFits) {
452
454 hn.Fill(fit.numberOfIterations);
455 hr.Fill(fit.value.model.R );
456 h1.Fill(fit.value.model.p1);
457 h2.Fill(fit.value.model.p2);
458 h3.Fill(fit.value.model.p3);
459 h4.Fill(fit.value.model.p4);
460 hc.Fill(fit.value.model.cc);
461 hb.Fill(fit.value.model.bc);
462
464 T[i]->Fill(fit.value.transmittance[i]());
465 }
466
467 TH1D h1t(
MAKE_CSTRING(module->getID() <<
".1t0"), NULL, NUMBER_OF_PMTS, -0.5, NUMBER_OF_PMTS - 0.5);
468 TH1D h1s(
MAKE_CSTRING(module->getID() <<
".1TTS"), NULL, NUMBER_OF_PMTS, -0.5, NUMBER_OF_PMTS - 0.5);
469 TH1D h1q(
MAKE_CSTRING(module->getID() <<
".1QE"), NULL, NUMBER_OF_PMTS, -0.5, NUMBER_OF_PMTS - 0.5);
470
472 h1t.SetBinContent(pmt + 1, fit.value.parameters[pmt].t0 ());
473 h1t.SetBinError (pmt + 1, fit.error.parameters[pmt].t0 () + numeric_limits<double>::epsilon());
474 h1s.SetBinContent(pmt + 1, fit.value.parameters[pmt].TTS());
475 h1s.SetBinError (pmt + 1, fit.error.parameters[pmt].TTS() + numeric_limits<double>::epsilon());
476 h1q.SetBinContent(pmt + 1, fit.value.parameters[pmt].QE ());
477 h1q.SetBinError (pmt + 1, fit.error.parameters[pmt].QE () + numeric_limits<double>::epsilon());
478 }
479
480 out << h1t << h1s << h1q;
481
482 for (int ix = 1; ix <= h2d->GetXaxis()->GetNbins(); ++ix) {
483
485
486 for (int iy = 1; iy <= h2d->GetYaxis()->GetNbins(); ++iy) {
487
488 const double dt_ns = h2d->GetYaxis()->GetBinCenter(iy);
489
490 h2d->SetBinContent(ix, iy, fit.value.getValue(
pair, dt_ns));
491 h2d->SetBinError (ix, iy, 0.0);
492 }
493 }
494
496 h2d->Write();
497
498 const double x =
string.getIndex(module->getString());
499 const double y =
module->getFloor();
500
502 HN->Fill(x, y, fit.numberOfIterations);
503 }
504
505 const double t0 = (fit.value.hasFixedTimeOffset() ? fit.value.getFixedTimeOffset() : 0.0);
506
508
510
512
513
514
516
517 if (R > 0.0)
518 data.QE = fit.value.parameters[pmt].QE() * fit.value.transmittance[ring]() / R;
519 else
521
522 if (writeFits > 1) {
523 data.TTS_ns = fit.value.parameters[pmt].TTS();
524 }
525
526 module->getPMT(pmt).addT0(fit.value.parameters[pmt].t0() - t0);
527 }
528 }
529 catch(const exception& error) {
530
531 ERROR(
"Module " << setw(10) << module->getID() <<
' ' << error.what() <<
" -> set QEs to 0." << endl);
532
535 }
536 }
537 }
538
539
541
542 {
544 JSTDObjectWriter <JMeta> writer(meta);
545
546 writer << reader;
547 }
548
549 for (vector<JMeta>::const_reverse_iterator i = meta.rbegin(); i != meta.rend(); ++i) {
552 }
553
554 if (overwriteDetector) {
555
556 NOTICE(
"Store calibration data on file " << detectorFile << endl);
557
559 }
560
561 if (pmtFile != "") {
562 parameters.
store(pmtFile.c_str());
563 }
564
565 if (transmittanceFile != "") {
566 transmittances.
store(transmittanceFile.c_str());
567 }
568
569 for (vector<JMeta>::const_iterator i = meta.begin(); i != meta.end(); ++i) {
571 }
572
575 }
576
577 if (writeFits) {
578
579 out << h0 << hn << hr << h1 << h2 << h3 << h4 << hc << hb << H2 << *HN;
580
582 out << *T[i];
583 }
584 }
585
586 out.Close();
587
588 return 0;
589}
#define DEBUG(A)
Message macros.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
#define MAKE_CSTRING(A)
Make C-string.
#define gmake_property(A)
macros to convert (template) parameter to JPropertiesElement object
#define TEST(T)
Test data member.
Auxiliary class for map of PMT parameters.
Data structure for PMT parameters.
Utility class to parse parameter values.
Utility class to parse command line options.
Object reading from a list of files.
static double TEROSTAT_R1
scaling factor
static const char *const _2F
Name extension for 2F rate fitted.
@ FIT_PMTS_QE_FIXED_t
fit parameters of PMTs with QE fixed
@ FIT_PMTS_AND_ANGULAR_DEPENDENCE_t
fit parameters of PMTs and angular dependence of K40 rate
@ FIT_MODEL_t
fit parameters of K40 rate and TTSs of PMTs
@ FIT_PMTS_AND_BACKGROUND_t
fit parameters of PMTs and background
@ FIT_PMTS_t
fit parameters of PMTs
static const char *const _2R
Name extension for 2D rate measured.
static const int NUMBER_OF_RINGS
number of rings in optical module.
static double TEROSTAT_DZ
maximal PMT inclination
ring_type getRing(const double dz)
Get ring.
static double BELL_SHAPE
Bell shape.
std::string getLabel(const JLocation &location)
Get module label for monitoring and other applications.
floor_range getRangeOfFloors(const JDetector &detector)
Get range of floors.
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
double getSurvivalProbability(const JPMTParameters ¶meters)
Get model dependent probability that a one photo-electron hit survives the simulation of the PMT assu...
void model(JModel_t &value)
Auxiliary function to constrain model during fit.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
bool putObject(TDirectory &dir, const TObject &object)
Write object to ROOT directory.
KM3NeT DAQ data structures and auxiliaries.
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Auxiliary data structure for sequence of same character.
Auxiliary data structure for floating point format specification.
Type definition of range.
Model for fit to acoustics data.
Fit parameters for two-fold coincidence rate due to K40.
static const JK40Parameters & getInstance()
Get default values.
static const JPMTParameters_t & getInstance()
Get default values.
Auxiliary class for TDC constraints.
range_type equal_range(const int id)
Get range of constraints for given module.
void reverse()
Reverse constraints.
bool is_valid(const bool option=false) const
Check validity of TDC constrants.
static const JTransmittance & getInstance()
Get default values.
Data structure for measured coincidence rates of all pairs of PMTs in optical module.
Data structure for measured coincidence rate of pair of PMTs.
Auxiliary data structure to define ring.
int getIndex() const
Get index.
static ring_type getRing(const int index)
Get ring.
Auxiliary data structure for I/O of transmittances of optical modules in detector.
Router for mapping of string identifier to index.
void store(const char *file_name) const
Store to output file.
void load(const char *file_name)
Load from input file.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...