~christopher-hunt08/maus/maus_integrated_kalman

« back to all changes in this revision

Viewing changes to src/map/MapCppGlobalPID/MapCppGlobalPID.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:
1
 
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
 
1
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
2
2
 *
3
3
 * MAUS is free software: you can redistribute it and/or modify
4
4
 * it under the terms of the GNU General Public License as published by
19
19
 
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"
24
25
 
25
26
 
26
27
namespace MAUS {
27
 
  MapCppGlobalPID::MapCppGlobalPID() {
28
 
    _classname = "MapCppGlobalPID";
29
 
  }
30
 
 
31
 
  bool MapCppGlobalPID::birth(std::string argJsonConfigDocument) {
 
28
 
 
29
  PyMODINIT_FUNC init_MapCppGlobalPID(void) {
 
30
    PyWrapMapBase<MAUS::MapCppGlobalPID>::PyWrapMapBaseModInit
 
31
                                            ("MapCppGlobalPID", "", "", "", "");
 
32
  }
 
33
 
 
34
 
 
35
  MapCppGlobalPID::MapCppGlobalPID()
 
36
     : MapBase<Data>("MapCppGlobalPID"), _configCheck(false) {
 
37
  }
 
38
 
 
39
  void MapCppGlobalPID::_birth(const std::string& argJsonConfigDocument) {
32
40
    // Check if the JSON document can be parsed, else return error only.
 
41
    _configCheck = false;
33
42
    bool parsingSuccessful = _reader.parse(argJsonConfigDocument, _configJSON);
34
43
    if (!parsingSuccessful) {
35
 
      _configCheck = false;
36
 
      return false;
 
44
      throw MAUS::Exception(Exception::recoverable,
 
45
                                  "Failed to parse Json configuration file",
 
46
                            "MapCppGlobalPID::_birth");
37
47
    }
38
48
 
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;
45
 
      return false;
 
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");
46
55
    }
47
56
 
48
 
    _configCheck = true;
 
57
 
49
58
 
50
59
    _hypotheses.clear();
51
60
    _pid_vars.clear();
68
77
                                                           _hypotheses[i]));
69
78
      // etc.
70
79
      }
71
 
 
72
 
    return true;
73
 
  }
74
 
 
75
 
  bool MapCppGlobalPID::death() {
76
 
    return true;
77
 
  }
78
 
 
79
 
  std::string MapCppGlobalPID::process(std::string document) const {
80
 
    Json::FastWriter writer;
81
 
    Json::Value root;
82
 
 
83
 
    if (document.empty()) {
84
 
      Json::Value errors;
85
 
      std::stringstream ss;
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);
 
80
    _configCheck = true;
 
81
  }
 
82
 
 
83
  void MapCppGlobalPID::_death() {
 
84
  }
 
85
 
 
86
  void MapCppGlobalPID::_process(MAUS::Data* data) const {
 
87
    MAUS::Data* data_cpp = data;
 
88
    if (!data_cpp) {
 
89
      throw Exception(Exception::recoverable,
 
90
                      "Data was NULL",
 
91
                      "MapCppGlobalPID::process");
90
92
    }
91
 
 
92
93
    if (!_configCheck) {
93
 
      Json::Value errors;
94
 
      std::stringstream ss;
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);
99
 
    }
100
 
 
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;
106
 
 
107
 
    // Read string and convert to a Json object
108
 
    try {
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;
116
 
      Json::Value errors;
117
 
      std::stringstream ss;
118
 
      ss << _classname << " says: Bad json document";
119
 
      errors["bad_json_document"] = ss.str();
120
 
      root["errors"] = errors;
121
 
      delete data_json;
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;
127
 
      Json::Value errors;
128
 
      std::stringstream ss;
129
 
      ss << _classname << " says: Bad json document";
130
 
      errors["bad_json_document"] = ss.str();
131
 
      root["errors"] = errors;
132
 
      delete data_json;
133
 
      return writer.write(root);
134
 
    }
135
 
 
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\"}}");
140
 
    }
141
 
 
142
 
    if (data_json->empty()) {
143
 
      delete data_json;
144
 
      return std::string("{\"errors\":{\"bad_json_document\":")+
145
 
        std::string("\"Failed to parse input document\"}}");
146
 
    }
147
 
 
148
 
    std::string maus_event = JsonWrapper::GetProperty(
149
 
        *data_json, "maus_event_type",
150
 
        JsonWrapper::stringValue).asString();
151
 
 
152
 
    if ( maus_event.compare("Spill") != 0 ) {
153
 
      Squeak::mout(Squeak::error) << "Line of json document did not contain "
154
 
          << "a Spill" << std::endl;
155
 
      delete data_json;
156
 
      return document;
157
 
    }
158
 
 
159
 
    std::string daq_event = JsonWrapper::GetProperty(
160
 
        *data_json, "daq_event_type",
161
 
        JsonWrapper::stringValue).asString();
162
 
 
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;
166
 
      delete data_json;
167
 
      return document;
168
 
    }
169
 
 
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
172
 
    // prefer.
173
 
    try {
174
 
      data_cpp = json2cppconverter(data_json);
175
 
      delete data_json;
176
 
    } catch (...) {
177
 
      Squeak::mout(Squeak::error) << "Missing required branch daq_event_type"
178
 
          << " converting json->cpp, MapCppGlobalPID" << std::endl;
179
 
    }
180
 
 
181
 
    if (!data_cpp) {
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");
184
97
    }
185
98
 
186
99
    const MAUS::Spill* _spill = data_cpp->GetSpill();
187
100
 
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();
196
 
             ++track_i) {
 
109
             ++track_i) {
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();
205
 
               ++pid_var_count) {
 
118
               ++pid_var_count) {
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) {
224
137
              }
225
138
            } else {
226
139
              Squeak::mout(Squeak::error) << "Unrecognised particle hypothesis,"
227
 
                  << " MapCppGlobalPID::process" << std::endl;
228
 
            }
 
140
                  << " MapCppGlobalPID::process" << std::endl;
 
141
            }
229
142
          }
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);
239
152
          } else {
240
153
            Squeak::mout(Squeak::error) << "PID for track could not be" <<
241
 
              " determined." << std::endl;
 
154
                                           " determined." << std::endl;
242
155
            continue;
243
156
          }
244
157
        }
245
158
      }
246
 
      }
247
 
 
248
 
    data_json = cpp2jsonconverter(data_cpp);
249
 
 
250
 
    if (!data_json) {
251
 
      delete data_cpp;
252
 
      return std::string("{\"errors\":{\"failed_cpp_json_conversion\":")+
253
 
        std::string("\"Failed to convert Cpp to Json Spill object\"}}");
254
159
    }
255
 
 
256
 
    std::string output_document = JsonWrapper::JsonToString(*data_json);
257
 
    delete data_json;
258
 
    delete data_cpp;
259
 
    return output_document;
260
160
  }
261
161
} // ~MAUS