1
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
3
* MAUS is free software: you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation, either version 3 of the License, or
6
* (at your option) any later version.
8
* MAUS is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with MAUS. If not, see <http://www.gnu.org/licenses/>.
21
#include "src/common_cpp/Utils/JsonWrapper.hh"
22
#include "src/common_cpp/Utils/CppErrorHandler.hh"
23
#include "Interface/Squeak.hh"
24
#include "Interface/Squeal.hh"
25
#include "Interface/dataCards.hh"
27
#include "src/map/MapCppTOFSpacePoints/MapCppTOFSpacePoints.hh"
29
bool MapCppTOFSpacePoints::birth(std::string argJsonConfigDocument) {
30
// Check if the JSON document can be parsed, else return error only
31
_classname = "MapCppTOFSpacePoints";
33
if (SetConfiguration(argJsonConfigDocument))
34
return true; // Sucessful completion
39
bool MapCppTOFSpacePoints::death() {return true;}
41
std::string MapCppTOFSpacePoints::process(std::string document) {
43
Json::FastWriter writer;
45
Json::Value xEventType;
46
// Check if the JSON document can be parsed, else return error only
48
root = JsonWrapper::StringToJson(document);
49
xEventType = JsonWrapper::GetProperty(root,
51
JsonWrapper::stringValue);
53
Squeak::mout(Squeak::error)
54
<< "Error in MapCppTOFSlabHits::process. Bad json document."
58
ss << _classname << " says:" << e.GetMessage();
59
errors["bad_json_document"] = ss.str();
60
root["errors"] = errors;
61
return writer.write(root);
64
if (xEventType== "physics_event" || xEventType == "calibration_event") {
65
Json::Value xSlabHits = JsonWrapper::GetProperty(root, "slab_hits", JsonWrapper::objectValue);
66
StationKeys.push_back("tof1");
67
StationKeys.push_back("tof0");
68
StationKeys.push_back("tof2");
69
for (unsigned int i=0; i<_trigger_pixels.size(); i++)
70
delete _trigger_pixels[i];
72
_trigger_pixels.resize(0);
73
// Loop over each station.
74
for (unsigned int n_station = 0; n_station < StationKeys.size(); n_station++) {
76
Json::Value xDocDetectorSlabHits = JsonWrapper::GetProperty(xSlabHits,
77
StationKeys[n_station],
78
JsonWrapper::arrayValue);
80
if (xDocDetectorSlabHits.isArray()) {
81
int n_part_events = xDocDetectorSlabHits.size();
82
_trigger_pixels.resize(n_part_events);
84
// Loop over the particle events.
85
for (int PartEvent = 0; PartEvent < n_part_events; PartEvent++) {
86
Json::Value xDocPartEvent = JsonWrapper::GetItem(xDocDetectorSlabHits,
88
JsonWrapper::anyValue);
90
Json::Value xDocSpacePoints = makeSpacePoints(xDocPartEvent);
91
root["space_points"][StationKeys[n_station]][PartEvent] = xDocSpacePoints;
96
// std::cout << root["space_points"] << std::endl;
97
return writer.write(root);
100
bool MapCppTOFSpacePoints::SetConfiguration(std::string json_configuration) {
102
Json::Value configJSON = JsonWrapper::StringToJson(json_configuration);
103
// this will contain the configuration
105
_map.SetTriggerStation(1);
106
bool loaded = _map.InitializeFromCards(json_configuration);
113
Json::Value MapCppTOFSpacePoints::makeSpacePoints(Json::Value xDocPartEvent) {
115
Json::Value xDocSpacePoints;
117
if (xDocPartEvent.isArray()) {
118
int n_slab_hits = xDocPartEvent.size();
119
std::cout << xDocPartEvent << std::endl;
121
// Create a map of all slab hits.
122
std::vector<int> xPlane0Hits;
123
std::vector<int> xPlane1Hits;
125
// Loop ovew the slab hits.
126
for (int SlHit = 0; SlHit < n_slab_hits; SlHit++) {
128
Json::Value xThisSlabHit =
129
JsonWrapper::GetItem(xDocPartEvent, SlHit, JsonWrapper::objectValue);
131
int xPlane = JsonWrapper::GetProperty(xThisSlabHit,
133
JsonWrapper::intValue).asInt();
137
xPlane0Hits.push_back(SlHit);
140
xPlane1Hits.push_back(SlHit);
144
Json::Value xDocSpacePoint;
145
if (xPlane0Hits.size() == 1 && xPlane1Hits.size() == 1) {
147
Json::Value xDocSlabHit0 = JsonWrapper::GetItem(xDocPartEvent,
149
JsonWrapper::objectValue);
151
Json::Value xDocSlabHit1 = JsonWrapper::GetItem(xDocPartEvent,
153
JsonWrapper::objectValue);
155
xDocSpacePoint = fillSpacePoint(xDocSlabHit0, xDocSlabHit1);
158
xDocSpacePoints[n] = xDocSpacePoint;
159
//xDocSpacePoints.append(xDocSpacePoint);
163
return xDocSpacePoints;
166
Json::Value MapCppTOFSpacePoints::fillSpacePoint(Json::Value xDocSlabHit0,
167
Json::Value xDocSlabHit1) {
169
Json::Value xDocSpacePoint;
171
xDocSpacePoint["part_event_number"] = xDocSlabHit0["part_event_number"];
172
xDocSpacePoint["phys_event_number"] = xDocSlabHit0["phys_event_number"];
174
// Get the two channel keys and make the pixel key.
175
std::string keyStr_Slab0_digit0 = JsonWrapper::GetProperty(xDocSlabHit0["pmt0"],
177
JsonWrapper::stringValue).asString();
178
TOFChannelKey xKey_Slab0_digit0(keyStr_Slab0_digit0);
181
std::string keyStr_Slab1_digit0 = JsonWrapper::GetProperty(xDocSlabHit1["pmt0"],
183
JsonWrapper::stringValue).asString();
184
TOFChannelKey xKey_Slab1_digit0(keyStr_Slab1_digit0);
186
TOFPixelKey *xSPKey = new TOFPixelKey(xKey_Slab0_digit0.station(),
187
xKey_Slab0_digit0.slab(),
188
xKey_Slab1_digit0.slab(),
189
xKey_Slab1_digit0.detector());
191
int nPartEvent = xDocSlabHit0["part_event_number"].asInt();
192
if (xSPKey->station() == 1) _trigger_pixels[nPartEvent] = xSPKey;
193
std::cout << nPartEvent << std::endl;
194
// Fill the information in the Space point.
195
xDocSpacePoint["slab0"] = xKey_Slab0_digit0.slab();
196
xDocSpacePoint["station"] = xKey_Slab0_digit0.station();
197
xDocSpacePoint["detector"] = xKey_Slab0_digit0.detector();
198
xDocSpacePoint["slab1"] = xKey_Slab1_digit0.slab();
199
xDocSpacePoint["pixel_key"] = xSPKey->str();
201
if (_trigger_pixels[nPartEvent]) {
202
double time_Slab0_digit0, time_Slab0_digit1, time_Slab1_digit0, time_Slab1_digit1;
203
// Process Digit0 of Slab0
204
calibratePmtHit(*(_trigger_pixels[nPartEvent]), xDocSlabHit0["pmt0"], time_Slab0_digit0);
206
// Process Digit1 of Slab0
207
calibratePmtHit(*(_trigger_pixels[nPartEvent]), xDocSlabHit0["pmt1"], time_Slab0_digit1);
209
// Process Digit0 of Slab1
210
calibratePmtHit(*(_trigger_pixels[nPartEvent]), xDocSlabHit1["pmt0"], time_Slab1_digit0);
212
// Process Digit1 of Slab1
213
calibratePmtHit(*(_trigger_pixels[nPartEvent]), xDocSlabHit1["pmt1"], time_Slab1_digit1);
215
// Now calculate the time.
217
(time_Slab0_digit0 + time_Slab0_digit1 + time_Slab1_digit0 + time_Slab1_digit1)/4.;
219
xDocSpacePoint["time"] = time;
220
std::cout << *xSPKey << " t = " << time << std::endl;
222
// std::cout << xDocSpacePoint << std::endl;
223
return xDocSpacePoint;
226
bool MapCppTOFSpacePoints::calibratePmtHit(TOFPixelKey xTriggerPixelKey,
227
Json::Value &xPmtHit,
230
std::string keyStr = JsonWrapper::GetProperty(xPmtHit,
232
JsonWrapper::stringValue).asString();
233
TOFChannelKey xChannelKey(keyStr);
235
double raw_time = JsonWrapper::GetProperty(xPmtHit,
237
JsonWrapper::realValue).asDouble();
239
int charge = JsonWrapper::GetProperty(xPmtHit,
241
JsonWrapper::intValue).asInt();
243
double dT = _map.dT(xChannelKey, xTriggerPixelKey, charge);
244
time = raw_time - dT;
245
xPmtHit["time"] = time;
246
// std::cout << xChannelKey << " t = " << raw_time << " - " << dT << " = " << time << std::endl;