~christopher-hunt08/maus/maus_integrated_kalman

« back to all changes in this revision

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

  • Committer: Adam Dobbs
  • Date: 2017-09-20 15:24:49 UTC
  • mfrom: (659.2.72 release-candidate)
  • Revision ID: phuccj@gmail.com-20170920152449-fjpj92xb5q4wx6y5
Tags: MAUS-v3.0, MAUS-v3.0.0
MAUS-v3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 *
16
16
 */
17
17
 
 
18
#include <exception>
18
19
#include "Utils/KLCalibrationMap.hh"
19
20
#include "Utils/KLChannelMap.hh"
20
21
 
 
22
#include "calibration/calibration.h"
 
23
 
21
24
namespace MAUS {
22
25
 
23
26
KLCalibrationMap::KLCalibrationMap() {
24
 
  pymod_ok = true;
25
 
  if (!this->InitializePyMod()) pymod_ok = false;
26
27
}
27
28
 
28
29
KLCalibrationMap::~KLCalibrationMap() {
90
91
      loaded = this->Initialize(xMapGainFile);
91
92
  } else {
92
93
      // get calib from DB instead of file, the above line is replaced by the one below
93
 
      if (!pymod_ok) return false;
94
94
      loaded = this->InitializeFromCDB();
95
95
  }
96
96
  if (!loaded)
191
191
  std::cout << "=================================================================" << std::endl;
192
192
}
193
193
 
194
 
bool KLCalibrationMap::InitializePyMod() {
195
 
  // import the get_kl_calib module
196
 
  // this python module access and gets calibrations from the DB
197
 
  _calib_mod = PyImport_ImportModule("calibration.get_kl_calib");
198
 
  if (_calib_mod == NULL) {
199
 
    std::cerr << "Failed to import get_kl_calib module" << std::endl;
200
 
    return false;
201
 
  }
202
 
 
203
 
  PyObject* calib_mod_dict = PyModule_GetDict(_calib_mod);
204
 
  if (calib_mod_dict != NULL) {
205
 
    PyObject* calib_init = PyDict_GetItemString
206
 
                                              (calib_mod_dict, "GetCalib");
207
 
    if (PyCallable_Check(calib_init)) {
208
 
        _tcalib = PyObject_Call(calib_init, NULL, NULL);
209
 
    }
210
 
  }
211
 
  if (_tcalib == NULL) {
212
 
    std::cerr << "Failed to instantiate get_kl_calib" << std::endl;
213
 
    return false;
214
 
  }
215
 
 
216
 
    // get the get_calib_func() function
217
 
  _get_calib_func = PyObject_GetAttrString(_tcalib, "get_calib");
218
 
  if (_get_calib_func == NULL) {
219
 
    std::cerr << "Failed to find get_calib function" << std::endl;
220
 
    return false;
221
 
  }
 
194
bool KLCalibrationMap::GetCalibCAPI(std::string devname,
 
195
                                    std::string caltype,
 
196
                                    std::string fromdate) {
 
197
  MAUS::CDB::Calibration cali;
 
198
  std::string result;
 
199
  try {
 
200
      std::string status;
 
201
      cali.getStatus(status);
 
202
      if (status.compare("OK") != 0) {
 
203
          std::cerr << "+++ CDB Error status = " << status << std::endl;
 
204
          return false;
 
205
      }
 
206
      // std::cerr << " Calibration status returned " << status << std::endl;
 
207
      std::cout << "++ Getting KL Calib by DATE for " << fromdate.c_str() << std::endl;
 
208
      if (fromdate.compare("current") == 0)
 
209
          cali.getCurrentDetectorCalibration(devname.c_str(),
 
210
                                             caltype.c_str(),
 
211
                                             result);
 
212
      else
 
213
          cali.getDetectorCalibrationForDate(devname.c_str(),
 
214
                                             caltype.c_str(),
 
215
                                             fromdate.c_str(),
 
216
                                             result);
 
217
      // std::cerr << result << "(" << result.size() << " characters)" << std::endl;
 
218
  } catch (std::exception &e) {
 
219
      std::cerr << e.what() << std::endl;
 
220
      return false;
 
221
  } // end try-catch
 
222
  gainstr.str(result);
222
223
  return true;
223
224
}
224
225
 
225
 
void KLCalibrationMap::GetCalib(std::string devname, std::string caltype, std::string fromdate) {
226
 
  PyObject *py_arg = NULL, *py_value = NULL;
227
 
  // setup the arguments to get_calib_func
228
 
  // the arguments are 3 strings
229
 
  // arg1 = device name (KL) uppercase
230
 
  // arg2 = calibration type (gain) lowercase
231
 
  // arg3 = valid_from_date == either "current" or an actual date 'YYYY-MM-DD HH:MM:SS'
232
 
  // default date argument is "current"
233
 
  // this is set via KL_calib_date_from card in ConfigurationDefaults
234
 
  py_arg = Py_BuildValue("(sss)", devname.c_str(), caltype.c_str(), fromdate.c_str());
235
 
  if (py_arg == NULL) {
236
 
    PyErr_Clear();
237
 
    throw(MAUS::Exceptions::Exception(MAUS::Exceptions::recoverable,
238
 
              "Failed to resolve arguments to get_calib",
239
 
              "MAUSEvaluator::evaluate"));
240
 
    }
241
 
    if (_get_calib_func != NULL && PyCallable_Check(_get_calib_func)) {
242
 
        py_value = PyObject_CallObject(_get_calib_func, py_arg);
243
 
        // setup the streams to hold the different calibs
244
 
        if (py_value != NULL && strcmp(caltype.c_str(), "gain") == 0)
245
 
            gainstr << PyString_AsString(py_value);
246
 
    }
247
 
    if (py_value == NULL) {
248
 
        PyErr_Clear();
249
 
        Py_XDECREF(py_arg);
250
 
        throw(MAUS::Exceptions::Exception(MAUS::Exceptions::recoverable,
251
 
                     "Failed to parse argument "+devname,
252
 
                     "GetCalib::get_calib"));
253
 
    }
254
 
    // clean up
255
 
    Py_XDECREF(py_value);
256
 
    Py_XDECREF(py_arg);
257
 
}
258
 
 
259
226
bool KLCalibrationMap::LoadGainCalib() {
260
 
  this->GetCalib("KL", "gain", _kl_calibdate);
 
227
  this->GetCalibCAPI("KL", "gain", _kl_calibdate);
261
228
  double gain;
262
229
  KLChannelKey key;
263
230
  try {