~chris-rogers/maus/emr_mc_digitization

« back to all changes in this revision

Viewing changes to src/map/MapCppGlobalReconImport/MapCppGlobalReconImport.cc

  • Committer: Chris Rogers
  • Date: 2014-04-16 11:48:45 UTC
  • mfrom: (707 merge)
  • mto: This revision was merged to the branch mainline in revision 711.
  • Revision ID: chris.rogers@stfc.ac.uk-20140416114845-h3u3q7pdcxkxvovs
Update to trunk

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
 
2
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 */
 
17
 
 
18
#include "src/map/MapCppGlobalReconImport/MapCppGlobalReconImport.hh"
 
19
 
 
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"
 
24
 
 
25
namespace MAUS {
 
26
  MapCppGlobalReconImport::MapCppGlobalReconImport() {
 
27
    _classname = "MapCppGlobalReconImport";
 
28
  }
 
29
 
 
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) {
 
34
      _configCheck = false;
 
35
      return false;
 
36
    }
 
37
    _configCheck = true;
 
38
    return true;
 
39
  }
 
40
 
 
41
  bool MapCppGlobalReconImport::death() {
 
42
    return true;
 
43
  }
 
44
 
 
45
  std::string MapCppGlobalReconImport::process(std::string document) const {
 
46
    Json::FastWriter writer;
 
47
    Json::Value root;
 
48
 
 
49
    if (document.empty()) {
 
50
      Json::Value errors;
 
51
      std::stringstream ss;
 
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);
 
56
    }
 
57
 
 
58
    if (!_configCheck) {
 
59
      Json::Value errors;
 
60
      std::stringstream ss;
 
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);
 
65
    }
 
66
 
 
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;
 
72
 
 
73
    // Read string and convert to a Json object
 
74
    try {
 
75
      Json::Value imported_json = JsonWrapper::StringToJson(document);
 
76
      data_json = new Json::Value(imported_json);
 
77
    } catch (...) {
 
78
      Json::Value errors;
 
79
      std::stringstream ss;
 
80
      ss << _classname << " says: Bad json document";
 
81
      errors["bad_json_document"] = ss.str();
 
82
      root["errors"] = errors;
 
83
      delete data_json;
 
84
      return writer.write(root);
 
85
    }
 
86
 
 
87
 
 
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\"}}");
 
92
    }
 
93
 
 
94
    if (data_json->empty()) {
 
95
      delete data_json;
 
96
      return std::string("{\"errors\":{\"bad_json_document\":")+
 
97
        std::string("\"Failed to parse input document\"}}");
 
98
    }
 
99
 
 
100
    std::string maus_event = JsonWrapper::GetProperty(
 
101
        *data_json, "maus_event_type",
 
102
        JsonWrapper::stringValue).asString();
 
103
 
 
104
    if ( maus_event.compare("Spill") != 0 ) {
 
105
      Squeak::mout(Squeak::error) << "Line of json document did not contain "
 
106
          << "a Spill" << std::endl;
 
107
      delete data_json;
 
108
      return document;
 
109
    }
 
110
 
 
111
    std::string daq_event = JsonWrapper::GetProperty(
 
112
        *data_json, "daq_event_type",
 
113
        JsonWrapper::stringValue).asString();
 
114
 
 
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;
 
118
      delete data_json;
 
119
      return document;
 
120
    }
 
121
 
 
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
 
124
    // prefer.
 
125
    try {
 
126
      data_cpp = json2cppconverter(data_json);
 
127
      delete data_json;
 
128
    } catch (...) {
 
129
      Squeak::mout(Squeak::error) << "Missing required branch daq_event_type"
 
130
          << "converting json->cpp, MapCppGlobalReconImport" << std::endl;
 
131
    }
 
132
 
 
133
    if (!data_cpp) {
 
134
      return std::string("{\"errors\":{\"failed_json_cpp_conversion\":")+
 
135
        std::string("\"Failed to convert Json to Cpp Spill object\"}}");
 
136
    }
 
137
 
 
138
    const MAUS::Spill* spill = data_cpp->GetSpill();
 
139
 
 
140
    MAUS::ReconEventPArray* recon_events = spill->GetReconEvents();
 
141
 
 
142
    if (!recon_events) {
 
143
      delete data_cpp;
 
144
      return document;
 
145
    }
 
146
 
 
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);
 
157
    }
 
158
 
 
159
    data_json = cpp2jsonconverter(data_cpp);
 
160
 
 
161
    if (!data_json) {
 
162
      delete data_cpp;
 
163
      return std::string("{\"errors\":{\"failed_cpp_json_conversion\":")+
 
164
        std::string("\"Failed to convert Cpp to Json Spill object\"}}");
 
165
    }
 
166
 
 
167
    std::string output_document = JsonWrapper::JsonToString(*data_json);
 
168
    delete data_json;
 
169
    delete data_cpp;
 
170
    return output_document;
 
171
  }
 
172
 
 
173
  MAUS::GlobalEvent*
 
174
  MapCppGlobalReconImport::Import(MAUS::ReconEvent* recon_event) const {
 
175
    if (!recon_event) {
 
176
      throw(Exception(Exception::recoverable,
 
177
                      "Trying to import an empty recon event.",
 
178
                      "MapCppGlobalReconImport::Import"));
 
179
    }
 
180
 
 
181
    MAUS::GlobalEvent* global_event = recon_event->GetGlobalEvent();
 
182
 
 
183
    MAUS::TOFEvent* tof_event = recon_event->GetTOFEvent();
 
184
 
 
185
    if (tof_event) {
 
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);
 
191
    }
 
192
    // Return the new GlobalEvent, to be added to the ReconEvent
 
193
    return global_event;
 
194
  }
 
195
} // ~MAUS