~christopher-hunt08/maus/maus_integrated_kalman

« back to all changes in this revision

Viewing changes to src/map/MapCppTrackerRecon/MapCppTrackerRecon.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:
15
15
 *
16
16
 */
17
17
 
 
18
#include "src/common_cpp/API/PyWrapMapBase.hh"
18
19
#include "src/map/MapCppTrackerRecon/MapCppTrackerRecon.hh"
19
20
 
20
21
namespace MAUS {
 
22
PyMODINIT_FUNC init_MapCppTrackerRecon(void) {
 
23
  PyWrapMapBase<MapCppTrackerRecon>::PyWrapMapBaseModInit
 
24
                                        ("MapCppTrackerRecon", "", "", "", "");
 
25
}
21
26
 
22
 
MapCppTrackerRecon::MapCppTrackerRecon()
23
 
    : _spill_json(NULL), _spill_cpp(NULL) {
 
27
MapCppTrackerRecon::MapCppTrackerRecon() : MapBase<Data>("MapCppTrackerRecon") {
24
28
}
25
29
 
26
30
MapCppTrackerRecon::~MapCppTrackerRecon() {
27
 
  if (_spill_json != NULL) {
28
 
      delete _spill_json;
29
 
  }
30
 
  if (_spill_cpp != NULL) {
31
 
      delete _spill_cpp;
32
 
  }
33
 
}
34
 
 
35
 
bool MapCppTrackerRecon::birth(std::string argJsonConfigDocument) {
36
 
  _classname = "MapCppTrackerRecon";
37
 
 
38
 
  try {
39
 
    if (!Globals::HasInstance()) {
40
 
      GlobalsManager::InitialiseGlobals(argJsonConfigDocument);
41
 
    }
42
 
    Json::Value *json = Globals::GetConfigurationCards();
43
 
    _helical_pr_on  = (*json)["SciFiPRHelicalOn"].asBool();
44
 
    _straight_pr_on = (*json)["SciFiPRStraightOn"].asBool();
45
 
    _kalman_on      = (*json)["SciFiKalmanOn"].asBool();
46
 
    _size_exception = (*json)["SciFiClustExcept"].asInt();
47
 
    _min_npe        = (*json)["SciFiNPECut"].asDouble();
48
 
 
49
 
    MiceModule* module = Globals::GetReconstructionMiceModules();
50
 
    std::vector<const MiceModule*> modules =
51
 
      module->findModulesByPropertyString("SensitiveDetector", "SciFi");
52
 
    _geometry_helper = SciFiGeometryHelper(modules);
53
 
    _geometry_helper.Build();
54
 
    return true;
55
 
  } catch (Exception& exception) {
56
 
    MAUS::CppErrorHandler::getInstance()->HandleExceptionNoJson(exception, _classname);
57
 
  } catch (std::exception& exc) {
58
 
    MAUS::CppErrorHandler::getInstance()->HandleStdExcNoJson(exc, _classname);
59
 
  }
60
 
  return false;
61
 
}
62
 
 
63
 
bool MapCppTrackerRecon::death() {
64
 
  return true;
65
 
}
66
 
 
67
 
std::string MapCppTrackerRecon::process(std::string document) {
68
 
  Json::FastWriter writer;
69
 
 
70
 
  // Read in json data
71
 
  read_in_json(document);
72
 
  Spill& spill = *_spill_cpp;
73
 
 
74
 
  try { // ================= Reconstruction =========================
75
 
    if ( spill.GetReconEvents() ) {
76
 
      for ( unsigned int k = 0; k < spill.GetReconEvents()->size(); k++ ) {
77
 
        SciFiEvent *event = spill.GetReconEvents()->at(k)->GetSciFiEvent();
78
 
        // Build Clusters.
79
 
        if ( event->digits().size() ) {
80
 
          cluster_recon(*event);
81
 
        }
82
 
        // Build SpacePoints.
83
 
        if ( event->clusters().size() ) {
84
 
          spacepoint_recon(*event);
85
 
        }
86
 
        // Pattern Recognition.
87
 
        if ( event->spacepoints().size() ) {
88
 
          pattern_recognition(_helical_pr_on, _straight_pr_on, *event);
89
 
        }
90
 
        // Kalman Track Fit.
91
 
        if ( _kalman_on ) {
92
 
          if ( event->straightprtracks().size() || event->helicalprtracks().size() ) {
93
 
            track_fit(*event);
94
 
          }
95
 
        }
96
 
        // print_event_info(*event);
97
 
      }
98
 
    } else {
99
 
      std::cout << "No recon events found\n";
100
 
    }
101
 
    save_to_json(spill);
102
 
  } catch (Exception& exception) {
103
 
    exception.Print();
104
 
  } catch (...) {
105
 
    Json::Value errors;
106
 
    std::stringstream ss;
107
 
    ss << _classname << " says:" << reader.getFormatedErrorMessages();
108
 
    errors["recon_failed"] = ss.str();
109
 
    (*_spill_json)["errors"] = errors;
110
 
    return writer.write(_spill_json);
111
 
  }
112
 
  return writer.write(*_spill_json);
113
 
}
114
 
 
115
 
void MapCppTrackerRecon::read_in_json(std::string json_data) {
116
 
  Json::Reader reader;
117
 
  Json::Value json_root;
118
 
  Json::FastWriter writer;
119
 
 
120
 
  if (_spill_cpp != NULL) {
121
 
    delete _spill_cpp;
122
 
    _spill_cpp = NULL;
123
 
  }
124
 
 
125
 
  try {
126
 
    json_root = JsonWrapper::StringToJson(json_data);
127
 
    SpillProcessor spill_proc;
128
 
    _spill_cpp = spill_proc.JsonToCpp(json_root);
129
 
  } catch (...) {
130
 
    std::cerr << "Bad json document" << std::endl;
131
 
    _spill_cpp = new Spill();
132
 
    MAUS::ErrorsMap errors = _spill_cpp->GetErrors();
133
 
    std::stringstream ss;
134
 
    ss << _classname << " says:" << reader.getFormatedErrorMessages();
135
 
    errors["bad_json_document"] = ss.str();
136
 
    _spill_cpp->GetErrors();
137
 
  }
138
 
}
139
 
 
140
 
void MapCppTrackerRecon::save_to_json(Spill &spill) {
141
 
    SpillProcessor spill_proc;
142
 
    if (_spill_json != NULL) {
143
 
        delete _spill_json;
144
 
        _spill_json = NULL;
145
 
    }
146
 
    _spill_json = spill_proc.CppToJson(spill, "");
147
 
}
148
 
 
149
 
void MapCppTrackerRecon::cluster_recon(SciFiEvent &evt) {
 
31
}
 
32
 
 
33
void MapCppTrackerRecon::_birth(const std::string& argJsonConfigDocument) {
 
34
  if (!Globals::HasInstance()) {
 
35
    GlobalsManager::InitialiseGlobals(argJsonConfigDocument);
 
36
  }
 
37
  Json::Value *json = Globals::GetConfigurationCards();
 
38
  _helical_pr_on  = (*json)["SciFiPRHelicalOn"].asBool();
 
39
  _straight_pr_on = (*json)["SciFiPRStraightOn"].asBool();
 
40
  _kalman_on      = (*json)["SciFiKalmanOn"].asBool();
 
41
  _size_exception = (*json)["SciFiClustExcept"].asInt();
 
42
  _min_npe        = (*json)["SciFiNPECut"].asDouble();
 
43
 
 
44
  MiceModule* module = Globals::GetReconstructionMiceModules();
 
45
  std::vector<const MiceModule*> modules =
 
46
    module->findModulesByPropertyString("SensitiveDetector", "SciFi");
 
47
  _geometry_helper = SciFiGeometryHelper(modules);
 
48
  _geometry_helper.Build();
 
49
}
 
50
 
 
51
void MapCppTrackerRecon::_death() {
 
52
}
 
53
 
 
54
void MapCppTrackerRecon::_process(Data* data) const {
 
55
  Spill& spill = *(data->GetSpill());
 
56
 
 
57
  if ( spill.GetReconEvents() ) {
 
58
    for ( unsigned int k = 0; k < spill.GetReconEvents()->size(); k++ ) {
 
59
      SciFiEvent *event = spill.GetReconEvents()->at(k)->GetSciFiEvent();
 
60
 
 
61
      // Build Clusters.
 
62
      if ( event->digits().size() ) {
 
63
        cluster_recon(*event);
 
64
      }
 
65
      // Build SpacePoints.
 
66
      if ( event->clusters().size() ) {
 
67
        spacepoint_recon(*event);
 
68
      }
 
69
      // Pattern Recognition.
 
70
      if ( event->spacepoints().size() ) {
 
71
        pattern_recognition(*event);
 
72
      }
 
73
      // Kalman Track Fit.
 
74
      if ( _kalman_on ) {
 
75
        if ( event->straightprtracks().size() || event->helicalprtracks().size() ) {
 
76
          track_fit(*event);
 
77
        }
 
78
      }
 
79
      // print_event_info(*event);
 
80
    }
 
81
  } else {
 
82
    std::cout << "No recon events found\n";
 
83
  }
 
84
}
 
85
 
 
86
void MapCppTrackerRecon::cluster_recon(SciFiEvent &evt) const {
150
87
  SciFiClusterRec clustering(_size_exception, _min_npe, _geometry_helper.GeometryMap());
151
88
  clustering.process(evt);
152
89
}
153
90
 
154
 
void MapCppTrackerRecon::spacepoint_recon(SciFiEvent &evt) {
 
91
void MapCppTrackerRecon::spacepoint_recon(SciFiEvent &evt) const {
155
92
  SciFiSpacePointRec spacepoints;
156
93
  spacepoints.process(evt);
157
94
}
158
95
 
159
 
void MapCppTrackerRecon::pattern_recognition(const bool helical_pr_on, const bool straight_pr_on,
160
 
                                             SciFiEvent &evt) {
161
 
  PatternRecognition pr1;
162
 
  pr1.set_verbosity(0);
163
 
  pr1.process(helical_pr_on, straight_pr_on, evt);
 
96
void MapCppTrackerRecon::pattern_recognition(SciFiEvent &evt) const {
 
97
  PatternRecognition pr1; // Pat rec constructor calls Globals again
 
98
  // We let the Map's helical and straight flags overide the interal pat rec variables for these,
 
99
  // (pulled by pat rec from Globals) as this way we can customise which type runs for
 
100
  // testing purposes.
 
101
  pr1.set_helical_pr_on(_helical_pr_on);
 
102
  pr1.set_straight_pr_on(_straight_pr_on);
 
103
  pr1.process(evt);
164
104
}
165
105
 
166
 
void MapCppTrackerRecon::track_fit(SciFiEvent &evt) {
 
106
void MapCppTrackerRecon::track_fit(SciFiEvent &evt) const {
167
107
  std::vector<KalmanSeed*> seeds;
168
108
  size_t number_helical_tracks  = evt.helicalprtracks().size();
169
109
  size_t number_straight_tracks = evt.straightprtracks().size();
185
125
 
186
126
  if ( seeds.size() ) {
187
127
    KalmanTrackFit fit;
 
128
    fit.SaveGeometry(_geometry_helper.RefPos(), _geometry_helper.Rot());
188
129
    fit.Process(seeds, evt);
189
130
  }
190
131
}
191
132
 
192
 
void MapCppTrackerRecon::print_event_info(SciFiEvent &event) {
193
 
  Squeak::mout(Squeak::info) << event.digits().size() << " "
194
 
                              << event.clusters().size() << " "
195
 
                              << event.spacepoints().size() << "; "
196
 
                              << event.straightprtracks().size() << " "
197
 
                              << event.helicalprtracks().size() << "; ";
198
 
  for ( size_t track_i = 0; track_i < event.scifitracks().size(); track_i++ ) {
199
 
    Squeak::mout(Squeak::info) << " Chi2: " << event.scifitracks()[track_i]->f_chi2() << "; "
200
 
                                << " P-Value: " << event.scifitracks()[track_i]->P_value() << "; ";
201
 
  }
202
 
  Squeak::mout(Squeak::info) << std::endl;
203
 
}
 
133
// void MapCppTrackerRecon::print_event_info(SciFiEvent &event) {
 
134
//   std::cerr << event.digits().size() << " "
 
135
//                               << event.clusters().size() << " "
 
136
//                               << event.spacepoints().size() << "; "
 
137
//                               << event.straightprtracks().size() << " "
 
138
//                               << event.helicalprtracks().size() << "; ";
 
139
//   for ( size_t track_i = 0; track_i < event.scifitracks().size(); track_i++ ) {
 
140
//     std::cerr << " Chi2: " << event.scifitracks()[track_i]->f_chi2() << "; "
 
141
//                            << " Chi2: " << event.scifitracks()[track_i]->s_chi2() << "; "
 
142
//                            << " P-Value: " << event.scifitracks()[track_i]->P_value() << "; ";
 
143
//   }
 
144
//   std::cerr << std::endl;
 
145
//   /*
 
146
//   Squeak::mout(Squeak::info) << event.digits().size() << " "
 
147
//                               << event.clusters().size() << " "
 
148
//                               << event.spacepoints().size() << "; "
 
149
//                               << event.straightprtracks().size() << " "
 
150
//                               << event.helicalprtracks().size() << "; ";
 
151
//   for ( size_t track_i = 0; track_i < event.scifitracks().size(); track_i++ ) {
 
152
//     Squeak::mout(Squeak::info) << " Chi2: " << event.scifitracks()[track_i]->f_chi2() << "; "
 
153
//                  << " Chi2: " << event.scifitracks()[track_i]->s_chi2() << "; "
 
154
//                  << " P-Value: " << event.scifitracks()[track_i]->P_value() << "; ";
 
155
//   }
 
156
//   Squeak::mout(Squeak::info) << std::endl;
 
157
//   */
 
158
// }
204
159
 
205
160
} // ~namespace MAUS