~christopher-hunt08/maus/maus_integrated_kalman

« back to all changes in this revision

Viewing changes to src/common_cpp/Utils/TOFCalibrationMap.cc

MergeĀ forĀ 0.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
#include "Utils/TOFCalibrationMap.hh"
19
19
 
 
20
TOFCalibrationMap::TOFCalibrationMap() {
 
21
  this->Reset();
 
22
}
 
23
 
20
24
TOFCalibrationMap::~TOFCalibrationMap() {
21
25
  _Pkey.clear();
22
26
  _Tkey.clear();
30
34
  this->MakeTOFChannelKeys();
31
35
 
32
36
  // Get the calibration text files from the Json document.
 
37
  // This is no longer done. Calibration is got from CDB now
 
38
  // - DR, 12/11/12
 
39
  /*
33
40
  Json::Value t0_file = JsonWrapper::GetProperty(configJSON,
34
41
                                                 "TOF_T0_calibration_file",
35
42
                                                 JsonWrapper::stringValue);
41
48
  Json::Value trigger_file = JsonWrapper::GetProperty(configJSON,
42
49
                                                      "TOF_Trigger_calibration_file",
43
50
                                                      JsonWrapper::stringValue);
44
 
 
 
51
  */
45
52
  // Check what needs to be done.
46
53
  _do_timeWalk_correction = JsonWrapper::GetProperty(configJSON,
47
54
                                                     "Enable_timeWalk_correction",
53
60
                                               "Enable_t0_correction",
54
61
                                               JsonWrapper::booleanValue).asBool();
55
62
 
 
63
  _tof_station = JsonWrapper::GetProperty(configJSON,
 
64
                                               "TOF_trigger_station",
 
65
                                               JsonWrapper::stringValue).asString();
 
66
 
 
67
  // convert trigger station name to upper case
 
68
  // the DB holds detector names in upper case
 
69
  std::transform(_tof_station.begin(), _tof_station.end(),
 
70
                                       _tof_station.begin(),
 
71
                                       std::ptr_fun<int, int>(std::toupper));
 
72
 
 
73
  _tof_calibdate = JsonWrapper::GetProperty(configJSON,
 
74
                                               "TOF_calib_date_from",
 
75
                                               JsonWrapper::stringValue).asString();
 
76
  // std::cout << "calib date: " << _tof_calibdate << std::endl;
 
77
 
56
78
  char* pMAUS_ROOT_DIR = getenv("MAUS_ROOT_DIR");
57
79
  if (!pMAUS_ROOT_DIR) {
58
80
    Squeak::mout(Squeak::error)
61
83
    return false;
62
84
  }
63
85
 
 
86
  /*
64
87
  std::string xMapT0File = std::string(pMAUS_ROOT_DIR) + t0_file.asString();
65
88
  std::string xMapTWFile = std::string(pMAUS_ROOT_DIR) + tw_file.asString();
66
89
  std::string xMapTriggerFile = std::string(pMAUS_ROOT_DIR) + trigger_file.asString();
67
 
 
68
90
  // Load the calibration constants.
69
 
  bool loaded = this->Initialize(xMapT0File, xMapTWFile, xMapTriggerFile);
 
91
  // bool loaded = this->Initialize(xMapT0File, xMapTWFile, xMapTriggerFile);
 
92
  */
 
93
  // get calib from DB instead of file, the above line is replaced by the one below
 
94
  bool loaded = this->InitializeFromCDB();
70
95
  if (!loaded)
71
96
    return false;
72
97
 
83
108
  return status;
84
109
}
85
110
 
 
111
bool TOFCalibrationMap::InitializeFromCDB() {
 
112
  // bool status = LoadT0Calib() &&
 
113
  //              LoadTWCalib() &&
 
114
  //              LoadTriggerCalib();
 
115
  // The above lines have been replaces by the line below
 
116
  // The new functions get the calibs from the DB
 
117
  bool status = LoadT0Calib() && LoadTWCalib() && LoadTriggerCalib();
 
118
 
 
119
  return status;
 
120
}
 
121
 
86
122
int TOFCalibrationMap::MakeTOFChannelKeys() {
87
123
 /** Makes one TOFChannelKey for each channel of the TOF detector.
88
124
  * The size of _t0, _reff and _twPar is set here.
387
423
  return xConv.str();
388
424
}
389
425
 
390
 
 
391
 
 
392
 
 
 
426
void TOFCalibrationMap::Reset() {
 
427
  // import the get_tof_calib module
 
428
  // this python module access and gets calibrations from the DB
 
429
  _calib_mod = PyImport_ImportModule("calibration.get_tof_calib");
 
430
  if (_calib_mod == NULL) {
 
431
    std::cerr << "Failed to import get_tof_calib module" << std::endl;
 
432
    return;
 
433
  }
 
434
 
 
435
  PyObject* calib_mod_dict = PyModule_GetDict(_calib_mod);
 
436
  if (calib_mod_dict != NULL) {
 
437
    PyObject* calib_init = PyDict_GetItemString
 
438
                                              (calib_mod_dict, "GetCalib");
 
439
    if (PyCallable_Check(calib_init)) {
 
440
        _tcalib = PyObject_Call(calib_init, NULL, NULL);
 
441
    }
 
442
  }
 
443
  if (_tcalib == NULL) {
 
444
    std::cerr << "Failed to instantiate get_tof_calib" << std::endl;
 
445
    return;
 
446
  }
 
447
 
 
448
    // get the get_calib_func() function
 
449
  _get_calib_func = PyObject_GetAttrString(_tcalib, "get_calib");
 
450
  if (_get_calib_func == NULL) {
 
451
    std::cerr << "Failed to find get_calib function" << std::endl;
 
452
    return;
 
453
  }
 
454
}
 
455
 
 
456
void TOFCalibrationMap::GetCalib(std::string devname, std::string caltype, std::string fromdate) {
 
457
  PyObject *py_arg = NULL, *py_value = NULL;
 
458
  // setup the arguments to get_calib_func
 
459
  // the arguments are 3 strings
 
460
  // arg1 = device name (TOF0/TOF1/TOF2) uppercase
 
461
  // arg2 = calibration type (tw/t0/trigger) lowercase
 
462
  // arg3 = valid_from_date == either "current" or an actual date 'YYYY-MM-DD HH:MM:SS'
 
463
  // default date argument is "current"
 
464
  // this is set via TOF_calib_date_from card in ConfigurationDefaults
 
465
  py_arg = Py_BuildValue("(sss)", devname.c_str(), caltype.c_str(), fromdate.c_str());
 
466
  if (py_arg == NULL) {
 
467
    PyErr_Clear();
 
468
    throw(Squeal(Squeal::recoverable,
 
469
              "Failed to resolve arguments to get_calib",
 
470
              "MAUSEvaluator::evaluate"));
 
471
    }
 
472
    if (_get_calib_func != NULL && PyCallable_Check(_get_calib_func)) {
 
473
        py_value = PyObject_CallObject(_get_calib_func, py_arg);
 
474
        // setup the streams to hold the different calibs
 
475
        if (py_value != NULL && strcmp(caltype.c_str(), "t0") == 0)
 
476
            t0str << PyString_AsString(py_value);
 
477
        if (strcmp(caltype.c_str(), "tw") == 0)
 
478
            twstr << PyString_AsString(py_value);
 
479
        if (strcmp(caltype.c_str(), "trigger") == 0)
 
480
            trigstr << PyString_AsString(py_value);
 
481
    }
 
482
    if (py_value == NULL) {
 
483
        PyErr_Clear();
 
484
        Py_XDECREF(py_arg);
 
485
        throw(Squeal(Squeal::recoverable,
 
486
                     "Failed to parse argument "+devname,
 
487
                     "GetCalib::get_calib"));
 
488
    }
 
489
    // clean up
 
490
    Py_XDECREF(py_value);
 
491
    Py_XDECREF(py_arg);
 
492
}
 
493
 
 
494
bool TOFCalibrationMap::LoadT0Calib() {
 
495
  this->GetCalib(_tof_station, "t0", _tof_calibdate);
 
496
  int reff;
 
497
  double p0;
 
498
  TOFChannelKey key;
 
499
  try {
 
500
    while (!t0str.eof()) {
 
501
      t0str >> key >> p0 >> reff;
 
502
 
 
503
      int n = FindTOFChannelKey(key);
 
504
      _t0[n] = p0;
 
505
      _reff[n] = reff;
 
506
      // std::cout << key << " pos:" << n << "  t0:" << p0 << "  reff:" << reff << std::endl;
 
507
    }
 
508
  } catch(Squeal e) {
 
509
    Squeak::mout(Squeak::error)
 
510
    << "Error in TOFCalibrationMap::LoadT0File : Error during loading. " << std::endl
 
511
    << e.GetMessage() << std::endl;
 
512
    return false;
 
513
  }
 
514
 
 
515
  return true;
 
516
}
 
517
 
 
518
bool TOFCalibrationMap::LoadTWCalib() {
 
519
  this->GetCalib(_tof_station, "tw", _tof_calibdate);
 
520
  double p0, p1, p2, p3;
 
521
  TOFChannelKey key;
 
522
  try {
 
523
    while (!twstr.eof()) {
 
524
      twstr >> key >> p0 >> p1 >> p2 >> p3;
 
525
      // std::cout << "tw: " << key.str() << " " << key << std::endl;
 
526
 
 
527
      int n = FindTOFChannelKey(key);
 
528
      _twPar[n].resize(4);
 
529
      _twPar[n][0] = p0;
 
530
      _twPar[n][1] = p1;
 
531
      _twPar[n][2] = p2;
 
532
      _twPar[n][3] = p3;
 
533
       // std::cout<< key << " pos:" << n << "  p0:" << p0 << "  p1:" << p1 << std::endl;
 
534
    }
 
535
  } catch(Squeal e) {
 
536
    Squeak::mout(Squeak::error)
 
537
    << "Error in TOFCalibrationMap::LoadTWFile : Error during loading. " << std::endl
 
538
    << e.GetMessage() << std::endl;
 
539
    return false;
 
540
  }
 
541
 
 
542
  return true;
 
543
}
 
544
 
 
545
bool TOFCalibrationMap::LoadTriggerCalib() {
 
546
  this->GetCalib(_tof_station, "trigger", _tof_calibdate);
 
547
  TOFPixelKey Pkey;
 
548
  double dt;
 
549
  try {
 
550
    while (!trigstr.eof()) {
 
551
      trigstr >> Pkey >> dt;
 
552
 
 
553
      _Tkey.push_back(Pkey);
 
554
      _Trt0.push_back(dt);
 
555
       // std::cout<< Pkey << "  dt:" << dt << std::endl;
 
556
    }
 
557
  } catch(Squeal e) {
 
558
    Squeak::mout(Squeak::error)
 
559
    << "Error in TOFCalibrationMap::LoadTriggerFile. Error during loading. " << std::endl
 
560
    << e.GetMessage() << std::endl;
 
561
    return false;
 
562
  }
 
563
  // Use the last readed pixel key to set the number of the trigger station.
 
564
  _triggerStation = Pkey.station();
 
565
 
 
566
  return true;
 
567
}