20
20
#include "Interface/Squeak.hh"
21
21
#include "src/common_cpp/DataStructure/Data.hh"
22
#include "src/common_cpp/API/PyWrapMapBase.hh"
22
23
#include "src/common_cpp/Converter/DataConverters/JsonCppSpillConverter.hh"
23
24
#include "src/common_cpp/Converter/DataConverters/CppJsonSpillConverter.hh"
27
MapCppGlobalPID::MapCppGlobalPID() {
28
_classname = "MapCppGlobalPID";
31
bool MapCppGlobalPID::birth(std::string argJsonConfigDocument) {
29
PyMODINIT_FUNC init_MapCppGlobalPID(void) {
30
PyWrapMapBase<MAUS::MapCppGlobalPID>::PyWrapMapBaseModInit
31
("MapCppGlobalPID", "", "", "", "");
35
MapCppGlobalPID::MapCppGlobalPID()
36
: MapBase<Data>("MapCppGlobalPID"), _configCheck(false) {
39
void MapCppGlobalPID::_birth(const std::string& argJsonConfigDocument) {
32
40
// Check if the JSON document can be parsed, else return error only.
33
42
bool parsingSuccessful = _reader.parse(argJsonConfigDocument, _configJSON);
34
43
if (!parsingSuccessful) {
44
throw MAUS::Exception(Exception::recoverable,
45
"Failed to parse Json configuration file",
46
"MapCppGlobalPID::_birth");
39
49
char* pMAUS_ROOT_DIR = getenv("MAUS_ROOT_DIR");
40
50
if (!pMAUS_ROOT_DIR) {
41
Squeak::mout(Squeak::error)
42
<< "Could not find the $MAUS_ROOT_DIR env variable." << std::endl;
43
Squeak::mout(Squeak::error)
44
<< "Did you try running: source env.sh ?" << std::endl;
51
throw MAUS::Exception(Exception::recoverable,
52
std::string("Could not find the $MAUS_ROOT_DIR env variable. ")+\
53
std::string("Did you try running: source env.sh?"),
54
"MapCppGlobalPID::_birth");
50
59
_hypotheses.clear();
75
bool MapCppGlobalPID::death() {
79
std::string MapCppGlobalPID::process(std::string document) const {
80
Json::FastWriter writer;
83
if (document.empty()) {
86
ss << _classname << " says: Empty document passed to process";
87
errors["bad_json_document"] = ss.str();
88
root["errors"] = errors;
89
return writer.write(root);
83
void MapCppGlobalPID::_death() {
86
void MapCppGlobalPID::_process(MAUS::Data* data) const {
87
MAUS::Data* data_cpp = data;
89
throw Exception(Exception::recoverable,
91
"MapCppGlobalPID::process");
92
93
if (!_configCheck) {
95
ss << _classname << " says: process was not passed a valid configuration";
96
errors["bad_json_document"] = ss.str();
97
root["errors"] = errors;
98
return writer.write(root);
101
// Prepare converters, spill and json objects
102
JsonCppSpillConverter json2cppconverter;
103
CppJsonSpillConverter cpp2jsonconverter;
104
Json::Value *data_json = NULL;
105
MAUS::Data *data_cpp = NULL;
107
// Read string and convert to a Json object
109
Json::Value imported_json = JsonWrapper::StringToJson(document);
110
data_json = new Json::Value(imported_json);
111
} catch (Exception& exception) {
112
MAUS::CppErrorHandler::getInstance()->
113
HandleExceptionNoJson(exception, _classname);
114
Squeak::mout(Squeak::error) << "String to Json conversion failed,"
115
<< "MapCppGlobalPID::process" << std::endl;
117
std::stringstream ss;
118
ss << _classname << " says: Bad json document";
119
errors["bad_json_document"] = ss.str();
120
root["errors"] = errors;
122
return writer.write(root);
123
} catch (std::exception& exc) {
124
MAUS::CppErrorHandler::getInstance()->HandleStdExcNoJson(exc, _classname);
125
Squeak::mout(Squeak::error) << "String to Json conversion failed,"
126
<< "MapCppGlobalPID::process" << std::endl;
128
std::stringstream ss;
129
ss << _classname << " says: Bad json document";
130
errors["bad_json_document"] = ss.str();
131
root["errors"] = errors;
133
return writer.write(root);
136
if (!data_json || data_json->isNull()) {
137
if (data_json) delete data_json;
138
return std::string("{\"errors\":{\"bad_json_document\":")+
139
std::string("\"Failed to parse input document\"}}");
142
if (data_json->empty()) {
144
return std::string("{\"errors\":{\"bad_json_document\":")+
145
std::string("\"Failed to parse input document\"}}");
148
std::string maus_event = JsonWrapper::GetProperty(
149
*data_json, "maus_event_type",
150
JsonWrapper::stringValue).asString();
152
if ( maus_event.compare("Spill") != 0 ) {
153
Squeak::mout(Squeak::error) << "Line of json document did not contain "
154
<< "a Spill" << std::endl;
159
std::string daq_event = JsonWrapper::GetProperty(
160
*data_json, "daq_event_type",
161
JsonWrapper::stringValue).asString();
163
if ( daq_event.compare("physics_event") != 0 ) {
164
Squeak::mout(Squeak::error) << "daq_event_type did not return a "
165
<< "physics event" << std::endl;
170
// Convert Json into MAUS::Spill object. In future, this will all
171
// be done for me, and process will take/return whichever object we
174
data_cpp = json2cppconverter(data_json);
177
Squeak::mout(Squeak::error) << "Missing required branch daq_event_type"
178
<< " converting json->cpp, MapCppGlobalPID" << std::endl;
182
return std::string("{\"errors\":{\"failed_json_cpp_conversion\":")+
183
std::string("\"Failed to convert Json to Cpp Spill object\"}}");
94
throw Exception(Exception::recoverable,
95
"Birth was not called successfully",
96
"MapCppGlobalPID::process");
186
99
const MAUS::Spill* _spill = data_cpp->GetSpill();
188
101
if ( _spill->GetReconEvents() ) {
189
102
for ( unsigned int event_i = 0;
190
event_i < _spill->GetReconEvents()->size(); ++event_i ) {
103
event_i < _spill->GetReconEvents()->size(); ++event_i ) {
191
104
MAUS::GlobalEvent* global_event =
192
_spill->GetReconEvents()->at(event_i)->GetGlobalEvent();
105
_spill->GetReconEvents()->at(event_i)->GetGlobalEvent();
193
106
std::vector<MAUS::DataStructure::Global::Track*> *GlobalTrackArray =
194
global_event->get_tracks();
107
global_event->get_tracks();
195
108
for (unsigned int track_i = 0; track_i < GlobalTrackArray->size();
197
110
MAUS::DataStructure::Global::Track* track =
198
GlobalTrackArray->at(track_i);
199
if (track->get_mapper_name() != "MapCppGlobalTrackMatching") continue;
111
GlobalTrackArray->at(track_i);
112
if (track->get_mapper_name() != "MapCppGlobalTrackMatching") continue;
200
113
// doubles to hold cumulative log likelihoods for each hypothesis
201
114
double logL_200MeV_mu_plus = 0;
202
115
double logL_200MeV_e_plus = 0;
203
116
double logL_200MeV_pi_plus = 0;
204
117
for (size_t pid_var_count = 0; pid_var_count < _pid_vars.size();
206
119
std::string hyp = _pid_vars[pid_var_count]->Get_hyp();
207
120
if (hyp == "200MeV_mu_plus") {
208
121
if (_pid_vars[pid_var_count]->logL(track) == 1) {
226
139
Squeak::mout(Squeak::error) << "Unrecognised particle hypothesis,"
227
<< " MapCppGlobalPID::process" << std::endl;
140
<< " MapCppGlobalPID::process" << std::endl;
230
143
if ((logL_200MeV_mu_plus - logL_200MeV_e_plus > 0.5) &&
231
144
(logL_200MeV_mu_plus - logL_200MeV_pi_plus > 0.5)) {
232
145
track->set_pid(MAUS::DataStructure::Global::kMuPlus);
233
146
} else if ((logL_200MeV_e_plus - logL_200MeV_mu_plus > 0.5) &&
234
(logL_200MeV_e_plus - logL_200MeV_pi_plus > 0.5)) {
147
(logL_200MeV_e_plus - logL_200MeV_pi_plus > 0.5)) {
235
148
track->set_pid(MAUS::DataStructure::Global::kEPlus);
236
149
} else if ((logL_200MeV_pi_plus - logL_200MeV_mu_plus > 0.5) &&
237
(logL_200MeV_pi_plus - logL_200MeV_e_plus > 0.5)) {
150
(logL_200MeV_pi_plus - logL_200MeV_e_plus > 0.5)) {
238
151
track->set_pid(MAUS::DataStructure::Global::kPiPlus);
240
153
Squeak::mout(Squeak::error) << "PID for track could not be" <<
241
" determined." << std::endl;
154
" determined." << std::endl;
248
data_json = cpp2jsonconverter(data_cpp);
252
return std::string("{\"errors\":{\"failed_cpp_json_conversion\":")+
253
std::string("\"Failed to convert Cpp to Json Spill object\"}}");
256
std::string output_document = JsonWrapper::JsonToString(*data_json);
259
return output_document;