23
23
#include "Interface/Squeak.hh"
24
24
#include "Utils/Exception.hh"
25
25
#include "Interface/dataCards.hh"
26
#include "API/PyWrapMapBase.hh"
27
28
#include "src/map/MapCppTOFSpacePoints/MapCppTOFSpacePoints.hh"
31
bool MapCppTOFSpacePoints::birth(std::string argJsonConfigDocument) {
31
PyMODINIT_FUNC init_MapCppTOFSpacePoints(void) {
32
PyWrapMapBase<MAUS::MapCppTOFSpacePoints>::PyWrapMapBaseModInit
33
("MapCppTOFSpacePoints", "", "", "", "");
36
MapCppTOFSpacePoints::MapCppTOFSpacePoints()
37
: MapBase<Json::Value>("MapCppTOFSpacePoints") {
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";
36
Json::Value configJSON;
38
configJSON = JsonWrapper::StringToJson(argJsonConfigDocument);
39
// this will contain the configuration
41
// Load the calibration.
43
bool loaded = _map.InitializeFromCards(configJSON);
46
std::cout << "_map_init = " << _map_init << std::endl;
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
57
= JsonWrapper::GetProperty(configJSON,
58
"TOF_trigger_station",
59
JsonWrapper::stringValue).asString();
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");
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;
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
46
// Load the calibration.
48
bool loaded = _map.InitializeFromCards(configJSON);
51
std::cout << "_map_init = " << _map_init << std::endl;
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
62
= JsonWrapper::GetProperty(configJSON,
63
"TOF_trigger_station",
64
JsonWrapper::stringValue).asString();
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");
79
throw MAUS::Exception(Exception::recoverable,
80
"TOF trigger station is wrong. Must be tof1 or tof0.",
81
"MapCppTOFSpacePoints::_birth");
92
bool MapCppTOFSpacePoints::death() {return true;}
85
void MapCppTOFSpacePoints::_death() {}
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"
98
Json::FastWriter writer;
100
Json::Value xEventType;
101
// Check if the JSON document can be parsed, else return error only
102
try {root = JsonWrapper::StringToJson(document);}
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);
91
Json::Value& root = *document;
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");
120
xEventType = JsonWrapper::GetProperty(root,
122
JsonWrapper::stringValue);
123
if (xEventType == "physics_event" || xEventType == "calibration_event") {
124
_triggerhit_pixels.clear();
125
Json::Value xRecEvent = JsonWrapper::GetProperty(root,
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);
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.
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
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
153
if (root.isMember("mc_events")) _findTriggerPixelCut = 50.0;
155
// Loop over each station starting from the trigger station.
156
for (unsigned int n_station = 0;
157
n_station < _stationKeys.size();
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);
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"]
97
Json::Value xEventType = JsonWrapper::GetProperty(root,
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,
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);
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.
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
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
130
if (root.isMember("mc_events") && root["mc_events"].size() > 0)
131
const_cast<MapCppTOFSpacePoints*>(this)->_findTriggerPixelCut = 50.0;
133
// Loop over each station starting from the trigger station.
134
for (unsigned int n_station = 0;
135
n_station < _stationKeys.size();
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,
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"]
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);
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);
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.
289
269
return "unknown";
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]);
303
287
if (calibrateSlabHit(xTriggerPixelKey,
304
xDocPartEvent[_xPlane0Hits[nX]],
288
xDocPartEvent[xPlane0Hits[nX]],
306
290
calibrateSlabHit(xTriggerPixelKey,
307
xDocPartEvent[_xPlane1Hits[nY]],
291
xDocPartEvent[xPlane1Hits[nY]],
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);