ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fitter.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fitter.cc
1 
8 //PHGenFit
9 #include "Fitter.h"
10 
11 #include "Track.h"
12 
13 //ROOT
14 #include <TGeoManager.h>
15 #include <RVersion.h> // for ROOT_VERSION, ROOT_VERSION...
16 
17 //GenFit
18 #include <GenFit/AbsKalmanFitter.h>
19 #include <GenFit/DAF.h>
20 #include <GenFit/EventDisplay.h>
21 #include <GenFit/FieldManager.h>
22 #include <GenFit/FitStatus.h>
23 #include <GenFit/KalmanFitter.h>
24 #include <GenFit/KalmanFitterRefTrack.h>
25 #include <GenFit/MaterialEffects.h>
26 #include <GenFit/TGeoMaterialInterface.h>
27 #include <GenFit/Track.h>
28 
29 //GenFitExp
30 #include <genfitexp/Field.h>
31 
32 #include <cassert>
33 #include <cstddef>
34 #include <iostream>
35 
36 namespace genfit { class AbsTrackRep; }
37 
38 #define LogDEBUG(exp) std::cout << "DEBUG: " << __FILE__ << ": " << __LINE__ << ": " << exp << std::endl
39 #define LogERROR(exp) std::cout << "ERROR: " << __FILE__ << ": " << __LINE__ << ": " << exp << std::endl
40 #define LogWARNING(exp) std::cout << "WARNING: " << __FILE__ << ": " << __LINE__ << ": " << exp << std::endl
41 
42 namespace PHGenFit
43 {
45  const std::string& tgeo_file_name,
46  const PHField* field,
47  const std::string& fitter_choice,
48  const std::string& /*track_rep_choice*/,
49  const bool doEventDisplay)
50  : verbosity(1000)
51  , _doEventDisplay(doEventDisplay)
52 {
53  _tgeo_manager = new TGeoManager("Default", "Geane geometry");
54  TGeoManager::Import(tgeo_file_name.data());
55 
56  assert(field);
57  genfit::Field* fieldMap = new genfit::Field(field);
58 
59  genfit::FieldManager::getInstance()->init(
60  fieldMap);
61  genfit::MaterialEffects::getInstance()->init(
62  new genfit::TGeoMaterialInterface());
63 
64  // init event display
65  if (_doEventDisplay)
66  _display = genfit::EventDisplay::getInstance();
67  else
68  _display = NULL;
69 
70  // init fitter
71  if (fitter_choice.compare("KalmanFitterRefTrack") == 0)
72  _fitter = new genfit::KalmanFitterRefTrack();
73  else if (fitter_choice.compare("KalmanFitter") == 0)
74  _fitter = new genfit::KalmanFitter();
75  else if (fitter_choice.compare("DafSimple") == 0)
76  _fitter = new genfit::DAF(false);
77  else if (fitter_choice.compare("DafRef") == 0)
78  _fitter = new genfit::DAF(true);
79  else
80  _fitter = new genfit::KalmanFitter();
81 
82  genfit::Exception::quiet(true);
83 }
84 
86 {
87  delete _fitter;
88  //delete _tgeo_manager;
89  //_tgeo_manager->Delete();
90  delete _display;
91 }
92 
93 int Fitter::processTrack(PHGenFit::Track* track, const bool save_to_evt_disp)
94 {
95  genfit::Track* fitTrack = track->getGenFitTrack();
96 
97 #if ROOT_VERSION_CODE >= ROOT_VERSION(6, 00, 0)
98  try
99  {
100  fitTrack->checkConsistency();
101  }
102  catch (genfit::Exception& e)
103  {
104  if (verbosity >= 2)
105  {
106  std::cerr << "genfit::Track::checkConsistency() failed!" << std::endl;
107  std::cerr << e.what();
108  }
109  return -1;
110  }
111 #else
112  if (!fitTrack->checkConsistency())
113  {
114  if (verbosity >= 2) LogWARNING("genfit::Track::checkConsistency() failed!");
115  return -1;
116  }
117 #endif
118  try
119  {
120  _fitter->processTrack(fitTrack);
121  }
122  catch (genfit::Exception& e)
123  {
124  if (verbosity >= 1)
125  {
126  std::cerr << "PHGenFit::Fitter::processTrack::Exception: \n";
127  std::cerr << e.what();
128  std::cerr << "Exception, next track" << std::endl;
129  }
130  return -1;
131  }
132 #if ROOT_VERSION_CODE >= ROOT_VERSION(6, 00, 0)
133  try
134  {
135  fitTrack->checkConsistency();
136  }
137  catch (genfit::Exception& e)
138  {
139  if (verbosity >= 2)
140  {
141  std::cerr << "genfit::Track::checkConsistency() failed!" << std::endl;
142  std::cerr << e.what();
143  }
144  return -1;
145  }
146 #else
147 
148  if (!fitTrack->checkConsistency())
149  {
150  if (verbosity >= 2) LogWARNING("genfit::Track::checkConsistency() failed!");
151  return -1;
152  }
153 #endif
154  genfit::AbsTrackRep* rep = fitTrack->getCardinalRep();
155  if (!fitTrack->getFitStatus(rep)->isFitConverged())
156  {
157  if (verbosity >= 2) LogWARNING("Track could not be fitted successfully! Fit is not converged!");
158  return -1;
159  }
160 
161  if (_display and save_to_evt_disp)
162  _display->addEvent(track->getGenFitTrack());
163 
164  return 0;
165 }
166 
167 Fitter* Fitter::getInstance(const std::string& tgeo_file_name,
168  const PHField* field,
169  const std::string& fitter_choice, const std::string& track_rep_choice,
170  const bool doEventDisplay)
171 {
172  TGeoManager* tgeo_manager = TGeoManager::Import(tgeo_file_name.data(), "Default");
173  if (!tgeo_manager)
174  {
175  LogERROR("No TGeoManager found!");
176  return NULL;
177  }
178 
179  assert(field);
180  genfit::Field* fieldMap = new genfit::Field(field);
181  return new Fitter(tgeo_manager, fieldMap, fitter_choice, track_rep_choice, doEventDisplay);
182 }
183 
184 Fitter::Fitter(TGeoManager* tgeo_manager, genfit::AbsBField* fieldMap,
185  const PHGenFit::Fitter::FitterType& fitter_choice,
186  const PHGenFit::Fitter::TrackRepType& /*track_rep_choice*/,
187  const bool doEventDisplay)
188  : verbosity(0)
189  , _tgeo_manager(tgeo_manager)
190  , _doEventDisplay(doEventDisplay)
191 {
192  genfit::FieldManager::getInstance()->init(
193  fieldMap);
194  genfit::MaterialEffects::getInstance()->init(
195  new genfit::TGeoMaterialInterface());
196 
197  // init event display
198  if (_doEventDisplay)
199  _display = genfit::EventDisplay::getInstance();
200  else
201  _display = NULL;
202 
203  // init fitter
204  if (fitter_choice == PHGenFit::Fitter::KalmanFitter)
205  _fitter = new genfit::KalmanFitter();
206  else if (fitter_choice == PHGenFit::Fitter::KalmanFitterRefTrack)
207  _fitter = new genfit::KalmanFitterRefTrack();
208  if (fitter_choice == PHGenFit::Fitter::DafSimple)
209  _fitter = new genfit::DAF(false);
210  else if (fitter_choice == PHGenFit::Fitter::DafRef)
211  _fitter = new genfit::DAF(true);
212  else
213  {
214  _fitter = nullptr;
215  LogERROR("This fitter not implemented!");
216  }
217 }
218 
219 Fitter* Fitter::getInstance(TGeoManager* tgeo_manager,
220  const PHField* field,
221  const std::string& fitter_choice, const std::string& track_rep_choice,
222  const bool doEventDisplay)
223 {
224  if (!tgeo_manager)
225  {
226  LogERROR("No TGeoManager found!");
227  return NULL;
228  }
229 
230  assert(field);
231  genfit::Field* fieldMap = new genfit::Field(field);
232  return new Fitter(tgeo_manager, fieldMap, fitter_choice, track_rep_choice, doEventDisplay);
233 }
234 
235 Fitter::Fitter(TGeoManager* tgeo_manager, genfit::AbsBField* fieldMap,
236  const std::string& fitter_choice, const std::string& /*track_rep_choice*/,
237  const bool doEventDisplay)
238  : verbosity(0)
239  , _tgeo_manager(tgeo_manager)
240  , _doEventDisplay(doEventDisplay)
241 {
242  genfit::FieldManager::getInstance()->init(
243  fieldMap);
244  genfit::MaterialEffects::getInstance()->init(
245  new genfit::TGeoMaterialInterface());
246 
247  // init event display
248  if (_doEventDisplay)
249  _display = genfit::EventDisplay::getInstance();
250  else
251  _display = NULL;
252 
253  // init fitter
254  if (fitter_choice.compare("KalmanFitterRefTrack") == 0)
255  _fitter = new genfit::KalmanFitterRefTrack();
256  else if (fitter_choice.compare("KalmanFitter") == 0)
257  _fitter = new genfit::KalmanFitter();
258  else if (fitter_choice.compare("DafSimple") == 0)
259  _fitter = new genfit::DAF(false);
260  else if (fitter_choice.compare("DafRef") == 0)
261  _fitter = new genfit::DAF(true);
262  else
263  {
264  _fitter = nullptr;
265  LogERROR("This fitter not implemented!");
266  }
267 }
268 
270 {
271  if (_display)
272  _display->open();
273  else if (verbosity >= 0)
274  LogERROR("No genfit::EventDisplay found!");
275 
276  return 0;
277 }
278 
279 Fitter* Fitter::getInstance(TGeoManager* tgeo_manager,
280  const PHField* field,
281  const PHGenFit::Fitter::FitterType& fitter_choice,
282  const PHGenFit::Fitter::TrackRepType& track_rep_choice,
283  const bool doEventDisplay)
284 {
285  if (!tgeo_manager)
286  {
287  LogERROR("No TGeoManager found!");
288  return NULL;
289  }
290 
291  assert(field);
292  genfit::Field* fieldMap = new genfit::Field(field);
293 
294  return new Fitter(tgeo_manager, fieldMap, fitter_choice, track_rep_choice, doEventDisplay);
295 }
296 
297 } // namespace PHGenFit