23
23
EMRCalibrationMap::EMRCalibrationMap() {
25
if (!this->InitializePyMod()) pymod_ok = false;
26
if ( !this->InitializePyMod() )
28
30
EMRCalibrationMap::~EMRCalibrationMap() {
35
void EMRCalibrationMap::reset() {
34
45
bool EMRCalibrationMap::InitializeFromCards(Json::Value configJSON) {
36
48
_number_of_planes = configJSON["EMRnumberOfPlanes"].asInt();
37
49
_number_of_bars = configJSON["EMRnumberOfBars"].asInt();
57
69
// Load the calibration file or find the right CDB entry
58
char* pMAUS_ROOT_DIR = getenv("MAUS_ROOT_DIR");
59
if (!pMAUS_ROOT_DIR) {
60
Squeak::mout(Squeak::error)
61
<< "Could not find the $MAUS_ROOT_DIR environmental variable." << std::endl;
62
Squeak::mout(Squeak::error) << "Did you try running: source env.sh ?" << std::endl;
66
70
bool fromDB = true;
67
if (source == "file") {
71
if ( source == "file" ) {
72
char* pMAUS_ROOT_DIR = getenv("MAUS_ROOT_DIR");
73
if ( !pMAUS_ROOT_DIR ) {
74
Squeak::mout(Squeak::error)
75
<< "Could not find the $MAUS_ROOT_DIR environmental variable." << std::endl;
76
Squeak::mout(Squeak::error) << "Did you try running: source env.sh ?" << std::endl;
68
80
// Get the calibration text files from the Json document.
69
81
calibFile = std::string(pMAUS_ROOT_DIR)
70
+ configJSON["EMR_calib_file"].asString();
82
+ configJSON["EMR_calib_file"].asString();
73
85
// Get the CDB calibration validity start date
76
88
JsonWrapper::stringValue).asString();
79
// Load the calibration constants.
91
// Load the calibration constants, return false if it fails
82
94
loaded = this->Initialize(calibFile);
84
96
// Get calib from CDB instead of file
85
if (!pymod_ok) return false;
86
99
loaded = this->InitializeFromCDB();
106
119
int EMRCalibrationMap::MakeEMRChannelKeys() {
107
121
for (int iPlane = -1; iPlane < _number_of_planes; iPlane++) // NB: global average (-1)
108
122
for (int iBar = -1; iBar < _number_of_bars; iBar++) // NB: average (-1), test channels (0)
109
123
_Ckey.push_back(EMRChannelKey(iPlane, iPlane%2, iBar, "emr"));
128
143
int n = FindEMRChannelKey(EMRChannelKey(plane, plane%2, bar, "emr"));
130
if (strcmp(fom, _fom.c_str()) == 0 && n != NOCALIB) {
131
if (strcmp(pmt, "MA") == 0) _eps_MA[n] = epsilon;
132
if (strcmp(pmt, "SA") == 0) _eps_SA[n] = epsilon;
145
if ( strcmp(fom, _fom.c_str()) == 0 && n != NOCALIB ) {
146
if ( strcmp(pmt, "MA") == 0 ) {
147
_eps_MA[n] = epsilon;
148
} else if ( strcmp(pmt, "SA") == 0 ) {
149
_eps_SA[n] = epsilon;
135
153
} catch (MAUS::Exception e) {
145
163
void EMRCalibrationMap::GetCalib(std::string devname, std::string caltype, std::string fromdate) {
146
165
PyObject *py_arg = NULL, *py_value = NULL;
147
166
// setup the arguments to get_calib_func
148
167
// the arguments are 3 strings
152
171
// default date argument is "current"
153
172
// this is set via EMR_calib_date_from card in ConfigurationDefaults
154
173
py_arg = Py_BuildValue("(sss)", devname.c_str(), caltype.c_str(), fromdate.c_str());
155
if (py_arg == NULL) {
157
176
throw(MAUS::Exception(MAUS::Exception::recoverable,
158
177
"Failed to resolve arguments to get_calib",
159
178
"MAUSEvaluator::evaluate"));
161
if (_get_calib_func != NULL && PyCallable_Check(_get_calib_func)) {
180
if ( _get_calib_func && PyCallable_Check(_get_calib_func) ) {
162
181
py_value = PyObject_CallObject(_get_calib_func, py_arg);
163
182
// setup the streams to hold the different calibs
164
if (py_value != NULL && strcmp(caltype.c_str(), "eps") == 0)
183
if ( py_value && strcmp(caltype.c_str(), "eps") == 0 )
165
184
epsstr << PyString_AsString(py_value);
167
if (py_value == NULL) {
169
188
Py_XDECREF(py_arg);
170
189
throw(MAUS::Exception(MAUS::Exception::recoverable,
199
218
int n = FindEMRChannelKey(EMRChannelKey(plane, plane%2, bar, "emr"));
201
if (strcmp(fom, _fom.c_str()) == 0 && n != NOCALIB) {
202
if (strcmp(pmt, "MA") == 0) _eps_MA[n] = epsilon;
203
if (strcmp(pmt, "SA") == 0) _eps_SA[n] = epsilon;
220
if ( strcmp(fom, _fom.c_str()) == 0 && n != NOCALIB ) {
221
if (strcmp(pmt, "MA") == 0) {
222
_eps_MA[n] = epsilon;
223
} else if (strcmp(pmt, "SA") == 0) {
224
_eps_SA[n] = epsilon;
206
228
} catch (MAUS::Exception e) {
224
247
double EMRCalibrationMap::Eps(EMRChannelKey key, const char *pmt) const {
226
249
int n = FindEMRChannelKey(key);
228
double epsilon = 1.0;
231
if (strcmp(pmt, "MA") == 0) epsilon = _eps_MA[n];
232
else if (strcmp(pmt, "SA") == 0)
252
if ( n != NOCALIB ) {
253
if ( strcmp(pmt, "MA") == 0 ) {
254
epsilon = _eps_MA[n];
255
} else if ( strcmp(pmt, "SA") == 0 ) {
233
256
epsilon = _eps_SA[n];
235
258
Squeak::mout(Squeak::error) << "Wrong PMT ID" << std::endl;
237
if ( epsilon ) return epsilon;
240
// std::cout << "EMRCalibrationMap -> No " << pmt << " calibration for " << key << std::endl;
244
267
void EMRCalibrationMap::Print() {
245
std::cout << "====================== EMRCalibrationMap =========================" << std::endl;
246
std::cout << " Number of channels : " << _Ckey.size() << std::endl;
248
for (unsigned int i = 0; i < _Ckey.size(); i++) {
249
std::cout << _Ckey[i] << " MA :" << _eps_MA[i] << std::endl;
250
std::cout << _Ckey[i] << " SA :" << _eps_SA[i] << std::endl;
253
std::cout<< "===================================================================" << std::endl;
268
std::cerr << "====================== EMRCalibrationMap =========================" << std::endl;
269
std::cerr << " Number of channels : " << _Ckey.size() << std::endl;
271
for (unsigned int i = 0; i < _Ckey.size(); i++)
272
std::cerr << _Ckey[i] << " MA :" << _eps_MA[i] << ", SA :" << _eps_SA[i] << std::endl;
274
std::cerr<< "===================================================================" << std::endl;
256
277
bool EMRCalibrationMap::InitializePyMod() {
257
278
PyObject* calib_mod = PyImport_ImportModule("calibration.get_emr_calib");
258
if (calib_mod == NULL) {
259
280
std::cerr << "Failed to import get_emr_calib module" << std::endl;
263
284
PyObject* calib_mod_dict = PyModule_GetDict(calib_mod);
264
285
PyObject* t_calib = NULL;
265
if (calib_mod_dict != NULL) {
286
if ( calib_mod_dict ) {
266
287
PyObject* calib_init = PyDict_GetItemString(calib_mod_dict, "GetCalib");
267
if (PyCallable_Check(calib_init)) {
288
if ( PyCallable_Check(calib_init) ) {
268
289
t_calib = PyObject_Call(calib_init, NULL, NULL);
271
if (t_calib == NULL) {
272
293
std::cerr << "Failed to instantiate get_emr_calib" << std::endl;
276
297
// Get the get_calib_func() function
277
298
_get_calib_func = PyObject_GetAttrString(t_calib, "get_calib");
278
if (_get_calib_func == NULL) {
299
if ( !_get_calib_func ) {
279
300
std::cerr << "Failed to find get_calib function" << std::endl;