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/>.
18
#include "src/map/MapCppGlobalReconImport/MapCppGlobalReconImport.hh"
20
#include "src/common_cpp/DataStructure/Data.hh"
21
#include "Interface/Squeak.hh"
22
#include "src/common_cpp/Converter/DataConverters/JsonCppSpillConverter.hh"
23
#include "src/common_cpp/Converter/DataConverters/CppJsonSpillConverter.hh"
26
MapCppGlobalReconImport::MapCppGlobalReconImport() {
27
_classname = "MapCppGlobalReconImport";
30
bool MapCppGlobalReconImport::birth(std::string argJsonConfigDocument) {
31
// Check if the JSON document can be parsed, else return error only.
32
bool parsingSuccessful = _reader.parse(argJsonConfigDocument, _configJSON);
33
if (!parsingSuccessful) {
41
bool MapCppGlobalReconImport::death() {
45
std::string MapCppGlobalReconImport::process(std::string document) const {
46
Json::FastWriter writer;
49
if (document.empty()) {
52
ss << _classname << " says: Empty document passed to process";
53
errors["bad_json_document"] = ss.str();
54
root["errors"] = errors;
55
return writer.write(root);
61
ss << _classname << " says: process passed an invalid configuration";
62
errors["bad_json_document"] = ss.str();
63
root["errors"] = errors;
64
return writer.write(root);
67
// Prepare converters, spill and json objects
68
JsonCppSpillConverter json2cppconverter;
69
CppJsonSpillConverter cpp2jsonconverter;
70
Json::Value *data_json = NULL;
71
MAUS::Data *data_cpp = NULL;
73
// Read string and convert to a Json object
75
Json::Value imported_json = JsonWrapper::StringToJson(document);
76
data_json = new Json::Value(imported_json);
80
ss << _classname << " says: Bad json document";
81
errors["bad_json_document"] = ss.str();
82
root["errors"] = errors;
84
return writer.write(root);
88
if (!data_json || data_json->isNull()) {
89
if (data_json) delete data_json;
90
return std::string("{\"errors\":{\"bad_json_document\":")+
91
std::string("\"Failed to parse input document\"}}");
94
if (data_json->empty()) {
96
return std::string("{\"errors\":{\"bad_json_document\":")+
97
std::string("\"Failed to parse input document\"}}");
100
std::string maus_event = JsonWrapper::GetProperty(
101
*data_json, "maus_event_type",
102
JsonWrapper::stringValue).asString();
104
if ( maus_event.compare("Spill") != 0 ) {
105
Squeak::mout(Squeak::error) << "Line of json document did not contain "
106
<< "a Spill" << std::endl;
111
std::string daq_event = JsonWrapper::GetProperty(
112
*data_json, "daq_event_type",
113
JsonWrapper::stringValue).asString();
115
if ( daq_event.compare("physics_event") != 0 ) {
116
Squeak::mout(Squeak::error) << "daq_event_type did not return a "
117
<< "physics event" << std::endl;
122
// Convert Json into MAUS::Spill object. In future, this will all
123
// be done for me, and process will take/return whichever object we
126
data_cpp = json2cppconverter(data_json);
129
Squeak::mout(Squeak::error) << "Missing required branch daq_event_type"
130
<< "converting json->cpp, MapCppGlobalReconImport" << std::endl;
134
return std::string("{\"errors\":{\"failed_json_cpp_conversion\":")+
135
std::string("\"Failed to convert Json to Cpp Spill object\"}}");
138
const MAUS::Spill* spill = data_cpp->GetSpill();
140
MAUS::ReconEventPArray* recon_events = spill->GetReconEvents();
147
MAUS::GlobalEvent* global_event;
148
MAUS::ReconEventPArray::iterator recon_event_iter;
149
for (recon_event_iter = recon_events->begin();
150
recon_event_iter != recon_events->end();
151
++recon_event_iter) {
152
// Load the ReconEvent, and import it into the GlobalEvent
153
MAUS::ReconEvent* recon_event = (*recon_event_iter);
154
global_event = recon_event->GetGlobalEvent();
155
MAUS::recon::global::TrackMatching track_matching;
156
global_event = Import(recon_event);
159
data_json = cpp2jsonconverter(data_cpp);
163
return std::string("{\"errors\":{\"failed_cpp_json_conversion\":")+
164
std::string("\"Failed to convert Cpp to Json Spill object\"}}");
167
std::string output_document = JsonWrapper::JsonToString(*data_json);
170
return output_document;
174
MapCppGlobalReconImport::Import(MAUS::ReconEvent* recon_event) const {
176
throw(Exception(Exception::recoverable,
177
"Trying to import an empty recon event.",
178
"MapCppGlobalReconImport::Import"));
181
MAUS::GlobalEvent* global_event = recon_event->GetGlobalEvent();
183
MAUS::TOFEvent* tof_event = recon_event->GetTOFEvent();
186
MAUS::recon::global::ImportTOFRecon tofrecon_importer;
187
tofrecon_importer.process((*tof_event), global_event, _classname);
188
// currently TrackMatching only uses TOF spacepoints, leave here for now
189
MAUS::recon::global::TrackMatching track_matching;
190
track_matching.FormTracks(global_event);
192
// Return the new GlobalEvent, to be added to the ReconEvent