~chris-rogers/maus/extend_virt

« back to all changes in this revision

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

  • Committer: Durga Rajaram
  • Date: 2015-09-10 14:05:56 UTC
  • mfrom: (910.1.16 trunk)
  • Revision ID: durga@fnal.gov-20150910140556-5vv3ksi139185wrf
daq channel maps are now read from cdb

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 *
16
16
 */
17
17
 
 
18
#include "Utils/DAQChannelMap.hh"
18
19
#include "Interface/Squeak.hh"
19
 
#include "Utils/DAQChannelMap.hh"
 
20
#include "Globals/PyLibMausCpp.hh"
20
21
 
21
22
namespace MAUS {
22
23
 
94
95
  _chKey.resize(0);
95
96
}
96
97
 
 
98
 
 
99
DAQChannelMap::DAQChannelMap() {
 
100
  pymod_ok = true;
 
101
  if (!this->InitializePyMod()) pymod_ok = false;
 
102
}
 
103
 
97
104
// load channel map based on data cards
98
105
bool DAQChannelMap::InitFromCards(Json::Value configJSON) {
 
106
  _daq_devicename = "DAQ";
 
107
  std::string _cabling_source = JsonWrapper::GetProperty(configJSON,
 
108
                                   "DAQ_cabling_source",
 
109
                                   JsonWrapper::stringValue).asString();
 
110
 
 
111
  if (_cabling_source != "CDB" && _cabling_source != "file")
 
112
      throw(MAUS::Exception(Exception::recoverable,
 
113
                  std::string("DAQChannelMap::InitFromCards"),
 
114
                  "Invalid DAQ_cabling_source card"));
 
115
 
 
116
  _daq_cabling_by = JsonWrapper::GetProperty(configJSON,
 
117
                                             "DAQ_cabling_by",
 
118
                                             JsonWrapper::stringValue).asString();
 
119
 
 
120
  if (_daq_cabling_by != "run_number" && _daq_cabling_by != "date")
 
121
      throw(MAUS::Exception(Exception::recoverable,
 
122
                  std::string("DAQChannelMap::InitFromCards"),
 
123
                  "Invalid DAQ_cabling_by card"));
 
124
 
 
125
  _daq_cablingdate = "current";
 
126
  if (_daq_cabling_by == "date")
 
127
      _daq_cablingdate = JsonWrapper::GetProperty(configJSON,
 
128
                                               "DAQ_cabling_date_from",
 
129
                                               JsonWrapper::stringValue).asString();
 
130
 
 
131
  runNumber = -1;
99
132
  _datafiles = JsonWrapper::GetProperty(configJSON,
100
 
                                               "daq_data_file",
101
 
                                               JsonWrapper::stringValue).asString();
 
133
                                        "daq_data_file",
 
134
                                        JsonWrapper::stringValue).asString();
102
135
 
103
 
  int runNum = -1;
104
136
  // check if daa_data_file is a number, if it is get run-number from it
105
137
  // else parse it and extract the run number
106
138
  if (is_number(_datafiles)) {
107
 
      runNum = atoi(_datafiles.c_str());
 
139
      runNumber = atoi(_datafiles.c_str());
108
140
  } else {
109
141
      std::string delimiter = " ";
110
142
      std::string token = _datafiles.substr(0, _datafiles.find(delimiter));
111
 
      runNum = atoi(token.c_str());
112
 
  }
113
 
 
114
 
  // load the appropriate cabling file depending on runNum being > or < 6541
115
 
  // DR -- this is a hack which should go away once the maps are in the CDB
116
 
  std::string map_file_name = "";
117
 
  if (runNum < 6541) {
118
 
      assert(configJSON.isMember("DAQ_cabling_file_StepI"));
119
 
      map_file_name = configJSON["DAQ_cabling_file_StepI"].asString();
120
 
     // load old file
121
 
  } else {
122
 
      assert(configJSON.isMember("DAQ_cabling_file"));
123
 
      map_file_name = configJSON["DAQ_cabling_file"].asString();
124
 
  }
125
 
  char* pMAUS_ROOT_DIR = getenv("MAUS_ROOT_DIR");
126
 
  if (!pMAUS_ROOT_DIR) {
127
 
        Squeak::mout(Squeak::error) << "Could not find the $MAUS_ROOT_DIR environmental variable."
128
 
        << std::endl;
129
 
        Squeak::mout(Squeak::error) << "Did you try running: source env.sh ?" << std::endl;
130
 
        throw(MAUS::Exception(Exception::recoverable, "STRING", "DAQChannelMap::_InitFromCards"));
131
 
  }
132
 
 
133
 
  // Initialize the map by using text file.
134
 
  bool loaded = InitFromFile(std::string(pMAUS_ROOT_DIR) + map_file_name);
135
 
 
 
143
      runNumber = atoi(token.c_str());
 
144
  }
 
145
 
 
146
  bool loaded = false;
 
147
  if (_cabling_source == "CDB") {
 
148
      if (!pymod_ok) return false;
 
149
      loaded = this->InitFromCDB();
 
150
  } else if (_cabling_source == "file") {
 
151
      // load the appropriate cabling file depending on runNumber being > or < 6541
 
152
      // DR -- this is a hack which should go away once the maps are in the CDB
 
153
      std::string map_file_name = "";
 
154
      if (runNumber < 6541) {
 
155
          assert(configJSON.isMember("DAQ_cabling_file_StepI"));
 
156
          map_file_name = configJSON["DAQ_cabling_file_StepI"].asString();
 
157
         // load old file
 
158
      } else {
 
159
          assert(configJSON.isMember("DAQ_cabling_file"));
 
160
          map_file_name = configJSON["DAQ_cabling_file"].asString();
 
161
      }
 
162
      char* pMAUS_ROOT_DIR = getenv("MAUS_ROOT_DIR");
 
163
      if (!pMAUS_ROOT_DIR) {
 
164
            Squeak::mout(Squeak::error) <<
 
165
            "Could not find the $MAUS_ROOT_DIR environmental variable."
 
166
            << std::endl;
 
167
            Squeak::mout(Squeak::error)
 
168
            << "Did you try running: source env.sh ?" << std::endl;
 
169
            throw(MAUS::Exception(Exception::recoverable,
 
170
                        "STRING", "DAQChannelMap::_InitFromCards"));
 
171
      }
 
172
      // Initialize the map by using text file.
 
173
      loaded = InitFromFile(std::string(pMAUS_ROOT_DIR) + map_file_name);
 
174
  }
136
175
  return loaded;
137
176
}
138
177
 
172
211
}
173
212
 
174
213
bool DAQChannelMap::InitFromCDB() {
175
 
  return false;
 
214
  this->GetCabling(_daq_devicename);
 
215
  int lineNum = 0;
 
216
  DAQChannelKey* key;
 
217
  try {
 
218
    while (!cblstr.eof()) {
 
219
      key = new DAQChannelKey();
 
220
      cblstr >> *key;
 
221
      _chKey.push_back(key);
 
222
      lineNum++;
 
223
    }
 
224
  } catch (MAUS::Exception e) {
 
225
    Squeak::mout(Squeak::error)
 
226
    << "Error in DAQChannelMap::InitFromCDB : Error during loading map." << std::endl
 
227
    << e.GetMessage() << std::endl;
 
228
    return false;
 
229
  }
 
230
 
 
231
  if (_chKey.size() == 0) {
 
232
    Squeak::mout(Squeak::error)
 
233
    << "Error in DAQChannelMap::InitFromCDB : No DAQ Channel Keys loaded. "
 
234
    << std::endl;
 
235
    return false;
 
236
  }
 
237
  return true;
176
238
}
177
239
 
178
240
DAQChannelKey* DAQChannelMap::find(int ldc, int geo, int ch, int eqType) {
216
278
    return !s.empty() && it == s.end();
217
279
}
218
280
 
 
281
bool DAQChannelMap::InitializePyMod() {
 
282
  // import the get_tof_cabling module
 
283
  // this python module access and gets cabling from the DB
 
284
  PyLibMausCpp::initlibMausCpp();
 
285
  _cabling_mod = PyImport_ImportModule("cabling.get_daq_cabling");
 
286
  if (_cabling_mod == NULL) {
 
287
    std::cerr << "Failed to import get_daq_cabling module" << std::endl;
 
288
    return false;
 
289
  }
 
290
 
 
291
  PyObject* cabling_mod_dict = PyModule_GetDict(_cabling_mod);
 
292
  if (cabling_mod_dict != NULL) {
 
293
    PyObject* cabling_init = PyDict_GetItemString
 
294
                                              (cabling_mod_dict, "GetCabling");
 
295
    if (PyCallable_Check(cabling_init)) {
 
296
        _tcabling = PyObject_Call(cabling_init, NULL, NULL);
 
297
    }
 
298
  }
 
299
  if (_tcabling == NULL) {
 
300
    std::cerr << "Failed to instantiate get_daq_cabling" << std::endl;
 
301
    return false;
 
302
  }
 
303
 
 
304
  return true;
 
305
}
 
306
 
 
307
void DAQChannelMap::GetCabling(std::string devname) {
 
308
  PyObject *py_arg = NULL, *py_value = NULL;
 
309
  // setup the arguments to get_cabling_func
 
310
  // the functions available are
 
311
  // get_cabling_for_date(DEVICE, DATE)
 
312
  // get_cabling_for_run(DEVICE, RUN)
 
313
 
 
314
  _get_cabling_func = NULL;
 
315
  if (_daq_cabling_by == "date") {
 
316
      py_arg = Py_BuildValue("(ss)", devname.c_str(), _daq_cablingdate.c_str());
 
317
      _get_cabling_func = PyObject_GetAttrString(_tcabling, "get_cabling_for_date");
 
318
  } else if (_daq_cabling_by == "run_number") {
 
319
      py_arg = Py_BuildValue("(si)", devname.c_str(), runNumber);
 
320
      _get_cabling_func = PyObject_GetAttrString(_tcabling, "get_cabling_for_run");
 
321
  } else {
 
322
      throw(MAUS::Exception(MAUS::Exception::recoverable,
 
323
                     "Invalid daq_cabling_by type "+_daq_cabling_by,
 
324
                     "DAQChannelMap::GetCalib"));
 
325
  }
 
326
 
 
327
  if (_get_cabling_func == NULL)
 
328
      throw(MAUS::Exception(MAUS::Exception::recoverable,
 
329
                     "Failed to find get_calib function",
 
330
                     "DAQChannelMap::GetCalib"));
 
331
 
 
332
  if (py_arg == NULL) {
 
333
    PyErr_Clear();
 
334
    throw(MAUS::Exception(MAUS::Exception::recoverable,
 
335
              "Failed to resolve arguments to get_cabling",
 
336
              "MAUSEvaluator::evaluate"));
 
337
    }
 
338
    if (_get_cabling_func != NULL && PyCallable_Check(_get_cabling_func)) {
 
339
        py_value = PyObject_CallObject(_get_cabling_func, py_arg);
 
340
        // setup the streams to hold the different calibs
 
341
        if (py_value != NULL)
 
342
            cblstr << PyString_AsString(py_value);
 
343
    }
 
344
    if (py_value == NULL) {
 
345
        PyErr_Clear();
 
346
        Py_XDECREF(py_arg);
 
347
        throw(MAUS::Exception(MAUS::Exception::recoverable,
 
348
                     "Failed to parse argument "+devname,
 
349
                     "GetCalib::get_calib"));
 
350
    }
 
351
    // clean up
 
352
    Py_XDECREF(py_value);
 
353
    Py_XDECREF(py_arg);
 
354
}
 
355
 
219
356
}  // namespace MAUS