~jan.greis/maus/1811

« back to all changes in this revision

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

  • Committer: Adam Dobbs
  • Date: 2016-01-13 11:31:12 UTC
  • mfrom: (659.2.18 release-candidate)
  • Revision ID: phuccj@gmail.com-20160113113112-bhbguupn50eyvd0z
Tags: MAUS-v1.4, MAUS-v1.4.0
MAUS-v1.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
namespace MAUS {
22
22
 
23
23
EMRCalibrationMap::EMRCalibrationMap() {
 
24
 
24
25
  pymod_ok = true;
25
 
  if (!this->InitializePyMod()) pymod_ok = false;
 
26
  if ( !this->InitializePyMod() )
 
27
      pymod_ok = false;
26
28
}
27
29
 
28
30
EMRCalibrationMap::~EMRCalibrationMap() {
 
31
 
 
32
  this->reset();
 
33
}
 
34
 
 
35
void EMRCalibrationMap::reset() {
 
36
 
29
37
  _Ckey.clear();
30
38
  _eps_MA.resize(0);
31
39
  _eps_SA.resize(0);
 
40
 
 
41
  epsstr.str("");
 
42
  epsstr.clear();
32
43
}
33
44
 
34
45
bool EMRCalibrationMap::InitializeFromCards(Json::Value configJSON) {
 
46
 
35
47
  // Fetch variables
36
48
  _number_of_planes = configJSON["EMRnumberOfPlanes"].asInt();
37
49
  _number_of_bars = configJSON["EMRnumberOfBars"].asInt();
55
67
  }
56
68
 
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;
63
 
    return false;
64
 
  }
65
 
 
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;
 
77
      return false;
 
78
    }
 
79
 
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();
71
83
    fromDB = false;
72
84
  } else {
73
85
    // Get the CDB calibration validity start date
76
88
                                          JsonWrapper::stringValue).asString();
77
89
  }
78
90
 
79
 
  // Load the calibration constants.
 
91
  // Load the calibration constants, return false if it fails
80
92
  bool loaded;
81
 
  if (!fromDB) {
 
93
  if ( !fromDB ) {
82
94
      loaded = this->Initialize(calibFile);
83
95
  } else {
84
96
      // Get calib from CDB instead of file
85
 
      if (!pymod_ok) return false;
 
97
      if ( !pymod_ok )
 
98
          return false;
86
99
      loaded = this->InitializeFromCDB();
87
100
  }
88
 
  if (!loaded)
89
 
    return false;
 
101
  if ( !loaded )
 
102
      return false;
90
103
 
91
104
  return true;
92
105
}
104
117
}
105
118
 
106
119
int EMRCalibrationMap::MakeEMRChannelKeys() {
 
120
 
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"));
116
130
}
117
131
 
118
132
bool EMRCalibrationMap::LoadFromCDB() {
 
133
 
119
134
  this->GetCalib("EMR", "eps", _calibDate);
120
135
 
121
136
  try {
122
 
    while (!epsstr.eof()) {
 
137
    while ( !epsstr.eof() ) {
123
138
      char pmt[10], fom[10];
124
139
      int plane(-9), bar(-9);
125
140
      double epsilon(-1.0);
127
142
 
128
143
      int n = FindEMRChannelKey(EMRChannelKey(plane, plane%2, bar, "emr"));
129
144
 
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;
 
150
        }
133
151
      }
134
152
    }
135
153
  } catch (MAUS::Exception e) {
143
161
}
144
162
 
145
163
void EMRCalibrationMap::GetCalib(std::string devname, std::string caltype, std::string fromdate) {
 
164
 
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) {
 
174
  if ( !py_arg ) {
156
175
    PyErr_Clear();
157
176
    throw(MAUS::Exception(MAUS::Exception::recoverable,
158
177
              "Failed to resolve arguments to get_calib",
159
178
              "MAUSEvaluator::evaluate"));
160
179
    }
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);
166
185
    }
167
 
    if (py_value == NULL) {
 
186
    if ( !py_value ) {
168
187
        PyErr_Clear();
169
188
        Py_XDECREF(py_arg);
170
189
        throw(MAUS::Exception(MAUS::Exception::recoverable,
181
200
 
182
201
  // Check the calibration file
183
202
  std::ifstream stream(calibFile.c_str());
184
 
  if (!stream) {
 
203
  if ( !stream ) {
185
204
    Squeak::mout(Squeak::error)
186
205
    << "Error in EMRCalibrationMap::Load : Can't open EMR calibration file."
187
206
    << calibFile << std::endl;
190
209
 
191
210
  // Fill the arrays of correction factors
192
211
  try {
193
 
    while (!stream.eof()) {
 
212
    while ( !stream.eof() ) {
194
213
      char pmt[10], fom[10];
195
214
      int plane(-9), bar(-9);
196
215
      double epsilon(-1.0);
198
217
 
199
218
      int n = FindEMRChannelKey(EMRChannelKey(plane, plane%2, bar, "emr"));
200
219
 
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;
 
225
        }
204
226
      }
205
227
    }
206
228
  } catch (MAUS::Exception e) {
214
236
}
215
237
 
216
238
int EMRCalibrationMap::FindEMRChannelKey(EMRChannelKey key) const {
217
 
  for (unsigned int i = 0; i < _Ckey.size(); i++ )
218
 
    if (_Ckey.at(i) == key)
 
239
 
 
240
  for (size_t i = 0; i < _Ckey.size(); i++ )
 
241
    if ( _Ckey.at(i) == key )
219
242
      return i;
220
243
 
221
244
  return NOCALIB;
224
247
double EMRCalibrationMap::Eps(EMRChannelKey key, const char *pmt) const {
225
248
 
226
249
  int n = FindEMRChannelKey(key);
227
 
 
228
 
  double epsilon = 1.0;
229
 
 
230
 
  if (n != NOCALIB) {
231
 
    if (strcmp(pmt, "MA") == 0) epsilon = _eps_MA[n];
232
 
    else if (strcmp(pmt, "SA") == 0)
 
250
  double epsilon(0.);
 
251
 
 
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];
234
 
    else
 
257
    } else {
235
258
      Squeak::mout(Squeak::error) << "Wrong PMT ID" << std::endl;
236
 
 
237
 
    if ( epsilon ) return epsilon;
 
259
    }
 
260
    if ( epsilon )
 
261
        return epsilon;
238
262
  }
239
263
 
240
 
  // std::cout << "EMRCalibrationMap -> No " << pmt << " calibration for " << key << std::endl;
241
264
  return NOCALIB;
242
265
}
243
266
 
244
267
void EMRCalibrationMap::Print() {
245
 
  std::cout << "====================== EMRCalibrationMap =========================" << std::endl;
246
 
  std::cout << " Number of channels : " << _Ckey.size() << std::endl;
247
 
 
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;
251
 
  }
252
 
 
253
 
  std::cout<< "===================================================================" << std::endl;
 
268
  std::cerr << "====================== EMRCalibrationMap =========================" << std::endl;
 
269
  std::cerr << " Number of channels : " << _Ckey.size() << std::endl;
 
270
 
 
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;
 
273
 
 
274
  std::cerr<< "===================================================================" << std::endl;
254
275
}
255
276
 
256
277
bool EMRCalibrationMap::InitializePyMod() {
257
278
  PyObject* calib_mod = PyImport_ImportModule("calibration.get_emr_calib");
258
 
  if (calib_mod == NULL) {
 
279
  if ( !calib_mod ) {
259
280
    std::cerr << "Failed to import get_emr_calib module" << std::endl;
260
281
    return false;
261
282
  }
262
283
 
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);
269
290
    }
270
291
  }
271
 
  if (t_calib == NULL) {
 
292
  if ( !t_calib ) {
272
293
    std::cerr << "Failed to instantiate get_emr_calib" << std::endl;
273
294
    return false;
274
295
  }
275
296
 
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;
280
301
    return false;
281
302
  }
282
303
  return true;
283
304
}
284
 
}
 
305
} // namespace MAUS