~christopher-hunt08/maus/maus_integrated_kalman

« back to all changes in this revision

Viewing changes to src/map/MapCppTOFSpacePoints/MapCppTOFSpacePoints.cc

  • Committer: Durga Rajaram
  • Date: 2014-07-16 15:13:05 UTC
  • mfrom: (659.1.92 cand)
  • Revision ID: durga@fnal.gov-20140716151305-q27rv1y9p03v9lks
Tags: MAUS-v0.9, MAUS-v0.9.0
MAUS-v0.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include "Interface/Squeak.hh"
24
24
#include "Utils/Exception.hh"
25
25
#include "Interface/dataCards.hh"
 
26
#include "API/PyWrapMapBase.hh"
26
27
 
27
28
#include "src/map/MapCppTOFSpacePoints/MapCppTOFSpacePoints.hh"
28
29
 
29
30
namespace MAUS {
30
 
 
31
 
bool MapCppTOFSpacePoints::birth(std::string argJsonConfigDocument) {
 
31
PyMODINIT_FUNC init_MapCppTOFSpacePoints(void) {
 
32
  PyWrapMapBase<MAUS::MapCppTOFSpacePoints>::PyWrapMapBaseModInit
 
33
                                      ("MapCppTOFSpacePoints", "", "", "", "");
 
34
}
 
35
 
 
36
MapCppTOFSpacePoints::MapCppTOFSpacePoints()
 
37
    : MapBase<Json::Value>("MapCppTOFSpacePoints") {
 
38
}
 
39
 
 
40
void MapCppTOFSpacePoints::_birth(const std::string& argJsonConfigDocument) {
32
41
  // Check if the JSON document can be parsed, else return error only
33
 
  _classname = "MapCppTOFSpacePoints";
34
 
 
35
42
  // JsonCpp setup
36
 
  Json::Value configJSON;
37
 
  try {
38
 
    configJSON = JsonWrapper::StringToJson(argJsonConfigDocument);
39
 
    // this will contain the configuration
40
 
 
41
 
    // Load the calibration.
42
 
    _map_init = true;
43
 
    bool loaded = _map.InitializeFromCards(configJSON);
44
 
    if (!loaded)
45
 
      _map_init = false;
46
 
    std::cout << "_map_init = " << _map_init << std::endl;
47
 
    _makeSpacePointCut =
48
 
    JsonWrapper::GetProperty(configJSON,
49
 
                             "TOF_makeSpacePointCut",
50
 
                             JsonWrapper::realValue).asDouble(); // nanoseconds
51
 
    _findTriggerPixelCut =
52
 
    JsonWrapper::GetProperty(configJSON,
53
 
                             "TOF_findTriggerPixelCut",
54
 
                             JsonWrapper::realValue).asDouble(); // nanoseconds
55
 
 
56
 
    _triggerStation
57
 
      = JsonWrapper::GetProperty(configJSON,
58
 
                                 "TOF_trigger_station",
59
 
                                 JsonWrapper::stringValue).asString();
60
 
 
61
 
    // The first element of the vectro has to be the trigger station.
62
 
    // This is mandatory!!!
63
 
    _stationKeys.push_back(_triggerStation);
64
 
    if (_triggerStation == "tof1") {
65
 
      _stationKeys.push_back("tof0");
66
 
      _stationKeys.push_back("tof2");
67
 
    } else if (_triggerStation == "tof0") {
68
 
      _stationKeys.push_back("tof1");
69
 
      _stationKeys.push_back("tof2");
70
 
    } else if (_triggerStation == "tof2") {
71
 
      _stationKeys.push_back("tof0");
72
 
      _stationKeys.push_back("tof1");
73
 
    } else {
74
 
      Squeak::mout(Squeak::error)
75
 
      << "Error in MapCppTOFSpacePoints::birth. TOF trigger station is wrong."
76
 
      << "It can be tof1 or tof0. The provided trigger station is : "
77
 
      << _triggerStation << std::endl;
78
 
 
79
 
      return false;
80
 
    }
81
 
 
82
 
  return true;
83
 
  } catch (Exception exc) {
84
 
    MAUS::CppErrorHandler::getInstance()->HandleExceptionNoJson(exc, _classname);
85
 
  } catch (std::exception exc) {
86
 
    MAUS::CppErrorHandler::getInstance()->HandleStdExcNoJson(exc, _classname);
 
43
  Json::Value configJSON = JsonWrapper::StringToJson(argJsonConfigDocument);
 
44
  // this will contain the configuration
 
45
 
 
46
  // Load the calibration.
 
47
  _map_init = true;
 
48
  bool loaded = _map.InitializeFromCards(configJSON);
 
49
  if (!loaded)
 
50
    _map_init = false;
 
51
  std::cout << "_map_init = " << _map_init << std::endl;
 
52
  _makeSpacePointCut =
 
53
  JsonWrapper::GetProperty(configJSON,
 
54
                           "TOF_makeSpacePointCut",
 
55
                           JsonWrapper::realValue).asDouble(); // nanoseconds
 
56
  _findTriggerPixelCut =
 
57
  JsonWrapper::GetProperty(configJSON,
 
58
                           "TOF_findTriggerPixelCut",
 
59
                           JsonWrapper::realValue).asDouble(); // nanoseconds
 
60
 
 
61
  _triggerStation
 
62
    = JsonWrapper::GetProperty(configJSON,
 
63
                               "TOF_trigger_station",
 
64
                               JsonWrapper::stringValue).asString();
 
65
 
 
66
  // The first element of the vectro has to be the trigger station.
 
67
  // This is mandatory!!!
 
68
  _stationKeys.push_back(_triggerStation);
 
69
  if (_triggerStation == "tof1") {
 
70
    _stationKeys.push_back("tof0");
 
71
    _stationKeys.push_back("tof2");
 
72
  } else if (_triggerStation == "tof0") {
 
73
    _stationKeys.push_back("tof1");
 
74
    _stationKeys.push_back("tof2");
 
75
  } else if (_triggerStation == "tof2") {
 
76
    _stationKeys.push_back("tof0");
 
77
    _stationKeys.push_back("tof1");
 
78
  } else {
 
79
    throw MAUS::Exception(Exception::recoverable,
 
80
                          "TOF trigger station is wrong. Must be tof1 or tof0.",
 
81
                          "MapCppTOFSpacePoints::_birth");
87
82
  }
88
 
 
89
 
  return false;
90
83
}
91
84
 
92
 
bool MapCppTOFSpacePoints::death()  {return true;}
 
85
void MapCppTOFSpacePoints::_death() {}
93
86
 
94
 
std::string MapCppTOFSpacePoints::process(std::string document) {
 
87
void MapCppTOFSpacePoints::_process(Json::Value* document) const {
95
88
  std::cout << "DEBUG MapCppTOFSpacePoints::process| Entry Checkpoint"
96
89
            << std::endl;
97
90
  //  JsonCpp setup
98
 
  Json::FastWriter writer;
99
 
  Json::Value root;
100
 
  Json::Value xEventType;
101
 
  // Check if the JSON document can be parsed, else return error only
102
 
  try {root = JsonWrapper::StringToJson(document);}
103
 
  catch (...) {
104
 
    Json::Value errors;
105
 
    std::stringstream ss;
106
 
    ss << _classname << " says: Failed to parse input document";
107
 
    errors["bad_json_document"] = ss.str();
108
 
    root["errors"] = errors;
109
 
    return writer.write(root);
110
 
  }
 
91
  Json::Value& root = *document;
111
92
  if (!_map_init) {
112
 
    Json::Value errors;
113
 
    std::stringstream ss;
114
 
    ss << _classname << " says: Failed to initialize calibration map";
115
 
    errors["no_tofcalib"] = ss.str();
116
 
    root["errors"] = errors;
117
 
    return writer.write(root);
 
93
    throw MAUS::Exception(Exception::recoverable,
 
94
                          "Failed to initialize calibration map",
 
95
                          "MapCppTOFSpacePoints::_process");
118
96
  }
119
 
  try {
120
 
    xEventType = JsonWrapper::GetProperty(root,
121
 
                                          "daq_event_type",
122
 
                                          JsonWrapper::stringValue);
123
 
    if (xEventType == "physics_event" || xEventType == "calibration_event") {
124
 
      _triggerhit_pixels.clear();
125
 
      Json::Value xRecEvent = JsonWrapper::GetProperty(root,
126
 
                                                       "recon_events",
127
 
                                                     JsonWrapper::arrayValue);
128
 
      for (unsigned int n_event = 0; n_event < xRecEvent.size(); n_event++) {
129
 
        Json::Value xTofEvent = JsonWrapper::GetItem
130
 
                                 (xRecEvent, n_event, JsonWrapper::objectValue);
131
 
        xTofEvent = JsonWrapper::GetProperty
132
 
                             (xTofEvent, "tof_event", JsonWrapper::objectValue);
133
 
        if (xTofEvent.isMember("tof_slab_hits")) {
134
 
          Json::Value xSlabHits = JsonWrapper::GetProperty
135
 
                         (xTofEvent, "tof_slab_hits", JsonWrapper::objectValue);
136
 
 
137
 
          // NOTE: DR March15
138
 
          // Cheating -- until I figure out how to handle trig-req-time in MC:
139
 
          //   I have to change the triggerpixelcut for MC in order for the
140
 
          //   calib corrections to be applied correctly.
141
 
          //   2 options --
142
 
          //   a) change the cut in ConfigDefaults
143
 
          //      but this <may> mess up data -- though I did run on real data
144
 
          //      with this modified cut and things (resol,time) look OK @ 1st
145
 
          //      glance
146
 
          //   b) use a different cut if it's MC
147
 
          //      this breaks the agreement that we'll treat real data/MC same
148
 
          //      way but for now it at least lets MC get reconstructed without
149
 
          //      clobbering real data
150
 
          // For now I have chosen option a) with option b) commented out below
151
 
          // need to be sure to change ConfigDefaults for switching between
152
 
          // data<->mc
153
 
          if (root.isMember("mc_events")) _findTriggerPixelCut = 50.0;
154
 
 
155
 
          // Loop over each station starting from the trigger station.
156
 
          for (unsigned int n_station = 0;
157
 
               n_station < _stationKeys.size();
158
 
               n_station++) {
159
 
            std::string detector = _stationKeys[n_station];
160
 
            if (xSlabHits.isMember(detector))
161
 
              std::cout << "DEBUG MapCppTOFSpacePoints::process| "
162
 
                        << "processing event " << n_event << " station "
163
 
                        << n_station << std::endl;
164
 
              root["recon_events"][n_event]["tof_event"]["tof_space_points"]
165
 
                   [detector] = processTOFStation(xSlabHits, detector, n_event);
166
 
          }
167
 
          // The slab hit document is now modified. The calibrated time
168
 
          // measurements are added. Save the modifications.
169
 
          root["recon_events"][n_event]["tof_event"]["tof_slab_hits"]
170
 
            = xSlabHits;
 
97
  Json::Value xEventType = JsonWrapper::GetProperty(root,
 
98
                                        "daq_event_type",
 
99
                                        JsonWrapper::stringValue);
 
100
  if (xEventType == "physics_event" || xEventType == "calibration_event") {
 
101
    std::map<int, std::string> triggerhit_pixels;
 
102
    Json::Value xRecEvent = JsonWrapper::GetProperty(root,
 
103
                                                     "recon_events",
 
104
                                                   JsonWrapper::arrayValue);
 
105
    for (unsigned int n_event = 0; n_event < xRecEvent.size(); n_event++) {
 
106
      Json::Value xTofEvent = JsonWrapper::GetItem
 
107
                               (xRecEvent, n_event, JsonWrapper::objectValue);
 
108
      xTofEvent = JsonWrapper::GetProperty
 
109
                           (xTofEvent, "tof_event", JsonWrapper::objectValue);
 
110
      if (xTofEvent.isMember("tof_slab_hits")) {
 
111
        Json::Value xSlabHits = JsonWrapper::GetProperty
 
112
                       (xTofEvent, "tof_slab_hits", JsonWrapper::objectValue);
 
113
 
 
114
        // NOTE: DR March15
 
115
        // Cheating -- until I figure out how to handle trig-req-time in MC:
 
116
        //   I have to change the triggerpixelcut for MC in order for the
 
117
        //   calib corrections to be applied correctly.
 
118
        //   2 options --
 
119
        //   a) change the cut in ConfigDefaults
 
120
        //      but this <may> mess up data -- though I did run on real data
 
121
        //      with this modified cut and things (resol,time) look OK @ 1st
 
122
        //      glance
 
123
        //   b) use a different cut if it's MC
 
124
        //      this breaks the agreement that we'll treat real data/MC same
 
125
        //      way but for now it at least lets MC get reconstructed without
 
126
        //      clobbering real data
 
127
        // For now I have chosen option a) with option b) commented out below
 
128
        // need to be sure to change ConfigDefaults for switching between
 
129
        // data<->mc
 
130
        if (root.isMember("mc_events") && root["mc_events"].size() > 0)
 
131
            const_cast<MapCppTOFSpacePoints*>(this)->_findTriggerPixelCut = 50.0;
 
132
 
 
133
        // Loop over each station starting from the trigger station.
 
134
        for (unsigned int n_station = 0;
 
135
             n_station < _stationKeys.size();
 
136
             n_station++) {
 
137
          std::string detector = _stationKeys[n_station];
 
138
          if (xSlabHits.isMember(detector))
 
139
            std::cout << "DEBUG MapCppTOFSpacePoints::process| "
 
140
                      << "processing event " << n_event << " station "
 
141
                      << n_station << std::endl;
 
142
            root["recon_events"][n_event]["tof_event"]["tof_space_points"]
 
143
                 [detector] = processTOFStation(xSlabHits,
 
144
                                                detector,
 
145
                                                n_event,
 
146
                                                triggerhit_pixels);
171
147
        }
 
148
        // The slab hit document is now modified. The calibrated time
 
149
        // measurements are added. Save the modifications.
 
150
        root["recon_events"][n_event]["tof_event"]["tof_slab_hits"]
 
151
          = xSlabHits;
172
152
      }
173
153
    }
174
 
  } catch (Exception exc) {
175
 
    root = MAUS::CppErrorHandler::getInstance()
176
 
                                       ->HandleException(root, exc, _classname);
177
 
  } catch (std::exception exc) {
178
 
    root = MAUS::CppErrorHandler::getInstance()
179
 
                                         ->HandleStdExc(root, exc, _classname);
180
154
  }
181
 
 
182
 
  // if (root.isMember("slab_hits")) std::cout << root["slab_hits"] << std::endl;
183
 
  // if (root.isMember("space_points")) std::cout << root["space_points"] << std::endl;
184
 
  return writer.write(root);
185
155
}
186
156
 
187
 
Json::Value MapCppTOFSpacePoints::processTOFStation(Json::Value &xSlabHits,
188
 
                                                    std::string detector,
189
 
                                                    unsigned int part_event) {
 
157
Json::Value MapCppTOFSpacePoints::processTOFStation(
 
158
                          Json::Value &xSlabHits,
 
159
                          std::string detector,
 
160
                          unsigned int part_event,
 
161
                          std::map<int, std::string>& triggerhit_pixels) const {
190
162
  std::cout << "DEBUG MapCppTOFSpacePoints::processTOFStation| "
191
163
            << "Entry Checkpoint" << std::endl;
192
164
  // Get the slab hits document for this TOF station.
199
171
    std::cout << "DEBUG MapCppTOFSpacePoints::processTOFStation| "
200
172
              << "# Slab Hits: " << n_slab_hits << std::endl;
201
173
    // Delete the information from the previous particle event.
202
 
    _xPlane0Hits.resize(0);
203
 
    _xPlane1Hits.resize(0);
 
174
    std::vector<int> xPlane0Hits;
 
175
    std::vector<int> xPlane1Hits;
204
176
 
205
177
    // Loop over the slab hits and select the hits in
206
178
    // plane0 (horizontal) and plane1 (vertical).
221
193
      // plane 1.
222
194
      switch (xPlane) {
223
195
        case 0 :
224
 
          _xPlane0Hits.push_back(SlHit);
 
196
          xPlane0Hits.push_back(SlHit);
225
197
          break;
226
198
        case 1 :
227
 
          _xPlane1Hits.push_back(SlHit);
 
199
          xPlane1Hits.push_back(SlHit);
228
200
          break;
229
201
      }
230
202
    }
231
203
 
232
204
    // If this is the trigger station find the pixel that is giving the trigger.
233
205
    if (detector == _triggerStation) {
234
 
      _triggerhit_pixels[part_event] = findTriggerPixel(xDocPartEvent);
 
206
      triggerhit_pixels[part_event] = findTriggerPixel(xDocPartEvent,
 
207
                                                       xPlane0Hits,
 
208
                                                       xPlane1Hits);
235
209
      std::cout << "DEBUG MapCppTOFSpacePoints::processTOFStation| "
236
 
                << "Trigger Pixel: " << _triggerhit_pixels[part_event]
 
210
                << "Trigger Pixel: " << triggerhit_pixels[part_event]
237
211
                << std::endl;
238
212
    }
239
213
    // If we do not know the trigger pixel there is no way to reconstruct the
240
214
    // time.
241
 
    if (_triggerhit_pixels[part_event] != "unknown") {
 
215
    if (triggerhit_pixels[part_event] != "unknown") {
242
216
      // Create the space point. Add the calibrated value of the time to the
243
217
      // slab hits.
244
 
      xDocPartEventSpacePoints = makeSpacePoints(xDocPartEvent);
 
218
      xDocPartEventSpacePoints = makeSpacePoints(xDocPartEvent,
 
219
                                                 xPlane0Hits,
 
220
                                                 xPlane1Hits,
 
221
                                                 triggerhit_pixels);
245
222
 
246
223
      // The slab hit document is now modified. The calibrated time measurements
247
224
      // are added. Save the modifications.
252
229
  return xDocPartEventSpacePoints;
253
230
}
254
231
 
255
 
std::string MapCppTOFSpacePoints::findTriggerPixel(Json::Value xDocPartEvent) {
 
232
std::string MapCppTOFSpacePoints::findTriggerPixel(
 
233
                                       Json::Value xDocPartEvent,
 
234
                                       std::vector<int> xPlane0Hits,
 
235
                                       std::vector<int> xPlane1Hits) const {
256
236
  // Loop over all possible combinations of slab hits in the trigger station.
257
 
  for (unsigned int nX = 0; nX < _xPlane0Hits.size(); nX++) {
258
 
    for (unsigned int nY = 0; nY < _xPlane1Hits.size(); nY++) {
 
237
  for (unsigned int nX = 0; nX < xPlane0Hits.size(); nX++) {
 
238
    for (unsigned int nY = 0; nY < xPlane1Hits.size(); nY++) {
259
239
      // Get the two slab hits.
260
240
      Json::Value xSlabHit_X = JsonWrapper::GetItem(xDocPartEvent,
261
 
                                                    _xPlane0Hits[nX],
 
241
                                                    xPlane0Hits[nX],
262
242
                                                    JsonWrapper::objectValue);
263
243
 
264
244
      Json::Value xSlabHit_Y = JsonWrapper::GetItem(xDocPartEvent,
265
 
                                                    _xPlane1Hits[nY],
 
245
                                                    xPlane1Hits[nY],
266
246
                                                    JsonWrapper::objectValue);
267
247
 
268
248
      int slabX = xSlabHit_X["slab"].asInt();
289
269
  return "unknown";
290
270
}
291
271
 
292
 
Json::Value MapCppTOFSpacePoints::makeSpacePoints(Json::Value &xDocPartEvent) {
 
272
Json::Value MapCppTOFSpacePoints::makeSpacePoints(
 
273
              Json::Value &xDocPartEvent,
 
274
              std::vector<int> xPlane0Hits,
 
275
              std::vector<int> xPlane1Hits,
 
276
              std::map<int, std::string>& triggerhit_pixels) const {
293
277
  Json::Value xDocSpacePoints(Json::arrayValue);
294
278
  // Loop over all possible combinations of slab hits in the trigger station.
295
 
  for (unsigned int nX = 0; nX < _xPlane0Hits.size(); nX++) {
296
 
    for (unsigned int nY = 0; nY < _xPlane1Hits.size(); nY++) {
 
279
  for (unsigned int nX = 0; nX < xPlane0Hits.size(); nX++) {
 
280
    for (unsigned int nY = 0; nY < xPlane1Hits.size(); nY++) {
297
281
      Json::Value xDocSpacePoint;
298
 
      int xPartEvent = JsonWrapper::GetProperty(xDocPartEvent[_xPlane0Hits[0]],
 
282
      int xPartEvent = JsonWrapper::GetProperty(xDocPartEvent[xPlane0Hits[0]],
299
283
                                               "part_event_number",
300
284
                                               JsonWrapper::intValue).asInt();
301
 
      TOFPixelKey xTriggerPixelKey(_triggerhit_pixels[xPartEvent]);
 
285
      TOFPixelKey xTriggerPixelKey(triggerhit_pixels[xPartEvent]);
302
286
      double t_x, t_y;
303
287
      if (calibrateSlabHit(xTriggerPixelKey,
304
 
                           xDocPartEvent[_xPlane0Hits[nX]],
 
288
                           xDocPartEvent[xPlane0Hits[nX]],
305
289
                           t_x) &&
306
290
          calibrateSlabHit(xTriggerPixelKey,
307
 
                           xDocPartEvent[_xPlane1Hits[nY]],
 
291
                           xDocPartEvent[xPlane1Hits[nY]],
308
292
                           t_y)) {
309
293
        // The first argument should be the hit in the horizontal slab and the
310
294
        // second should be the hit in the vertical slab. This is mandatory!!!
311
295
        Json::Value xDocSpacePoint
312
 
          = fillSpacePoint(xDocPartEvent[_xPlane0Hits[nX]],
313
 
                                         xDocPartEvent[_xPlane1Hits[nY]]);
 
296
          = fillSpacePoint(xDocPartEvent[xPlane0Hits[nX]],
 
297
                                         xDocPartEvent[xPlane1Hits[nY]]);
314
298
        double deltaT = xDocSpacePoint["dt"].asInt();
315
299
        if (fabs(deltaT) < _makeSpacePointCut) {
316
300
          xDocSpacePoints.append(xDocSpacePoint);
323
307
}
324
308
 
325
309
Json::Value MapCppTOFSpacePoints::fillSpacePoint(Json::Value &xDocSlabHit_X,
326
 
                                                 Json::Value &xDocSlabHit_Y) {
 
310
                                                 Json::Value &xDocSlabHit_Y) const {
327
311
 
328
312
  Json::Value xDocSpacePoint;
329
313
 
403
387
 
404
388
bool MapCppTOFSpacePoints::calibratePmtHit(TOFPixelKey xTriggerPixelKey,
405
389
                                           Json::Value &xPmtHit,
406
 
                                           double &time) {
 
390
                                           double &time) const {
407
391
  int charge;
408
392
  // Charge of the digit can be unset because of the Zero suppresion of the
409
393
  // fADCs.
443
427
 
444
428
bool MapCppTOFSpacePoints::calibrateSlabHit(TOFPixelKey xTriggerPixelKey,
445
429
                                            Json::Value &xSlabHit,
446
 
                                            double &time) {
 
430
                                            double &time) const {
447
431
 
448
432
  double time_digit0, time_digit1;
449
433