Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHG4HitKalmanFitter.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHG4HitKalmanFitter.cc
1 
8 #include <cmath>
9 #include <map>
10 
11 #include <GenFit/RKTrackRep.h>
13 #include <phool/PHCompositeNode.h>
14 #include <phool/PHIODataNode.h>
15 #include <phool/PHNodeIterator.h>
16 #include <phool/getClass.h>
17 #include <phool/phool.h>
18 
19 #include <GenFit/StateOnPlane.h>
20 
21 #include <TMath.h>
22 #include <TRandom.h>
23 #include <TString.h>
24 
25 #include <g4hough/SvtxTrackMap.h>
26 #include <g4hough/SvtxTrackMap_v1.h>
27 #include <g4hough/SvtxTrack_v1.h>
28 #include <g4main/PHG4Hit.h>
29 #include <g4main/PHG4Particle.h>
31 #include <phgenfit/Fitter.h>
32 #include <phgenfit/PlanarMeasurement.h>
33 #include <phgenfit/Track.h>
34 
35 #include <phfield/PHFieldUtility.h>
36 #include <phgeom/PHGeomUtility.h>
37 
38 #include "PHG4HitKalmanFitter.h"
39 
40 #define LogDebug(exp) std::cout << "DEBUG: " << __FILE__ << ": " << __LINE__ << ": " << exp << "\n"
41 #define LogError(exp) std::cout << "ERROR: " << __FILE__ << ": " << __LINE__ << ": " << exp << "\n"
42 #define LogWarning(exp) std::cout << "WARNING: " << __FILE__ << ": " << __LINE__ << ": " << exp << "\n"
43 
44 #define _N_DETECTOR_LAYER 5
45 
46 using namespace std;
47 
49  : SubsysReco(name)
50  , _truth_container(NULL)
51  , _trackmap_out(NULL)
52  , _fitter(NULL)
53  , _fit_alg_name("KalmanFitterRefTrack")
54  , _do_evt_display(false)
55  , _phi_resolution(50E-4)
56  , //100um
57  _r_resolution(1.)
58  , _pat_rec_hit_finding_eff(1.)
59  , _pat_rec_nosise_prob(0.)
60 
61 {
62  _event = 0;
63 
64  for (int i = 0; i < _N_DETECTOR_LAYER; i++)
65  {
66  _phg4hits_names.push_back(Form("G4HIT_FGEM_%1d", i));
67  }
68 }
69 
71 {
72  delete _fitter;
73 }
74 
75 /*
76  * Init
77  */
79 {
81 }
82 
84 {
85  CreateNodes(topNode);
86 
87  TGeoManager* tgeo_manager = PHGeomUtility::GetTGeoManager(topNode);
88  PHField* field = PHFieldUtility::GetFieldMapNode(nullptr, topNode);
89  //_fitter = new PHGenFit::Fitter("sPHENIX_Geo.root","sPHENIX.2d.root", 1.4 / 1.5);
91  field, "KalmanFitterRefTrack", "RKTrackRep",
93 
94  if (!_fitter)
95  {
96  cerr << PHWHERE << endl;
98  }
99 
101 }
102 
104 {
106 }
107 
109 {
110  GetNodes(topNode);
111 
112  if (_trackmap_out)
113  _trackmap_out->empty();
114  else
115  {
116  LogError("_trackmap_out not found!");
118  }
119 
122  itr != _truth_container->GetPrimaryParticleRange().second; ++itr)
123  {
124  PHG4Particle* particle = itr->second;
125 
126  TVector3 seed_pos(0, 0, 0);
127  TVector3 seed_mom(0, 0, 0);
128  TMatrixDSym seed_cov(6);
129 
131  std::vector<PHGenFit::Measurement*> measurements;
132 
133  const bool _use_vertex_in_fitting = true;
134 
135  PHGenFit::Measurement* vtx_meas = NULL;
136 
137  if (_use_vertex_in_fitting)
138  {
139  vtx_meas = VertexMeasurement(
140  TVector3(0, 0, 0), 0.0050, 0.0050);
141  measurements.push_back(vtx_meas);
142  }
143 
144  PseudoPatternRecognition(particle, measurements, seed_pos, seed_mom, seed_cov);
145 
146  if (measurements.size() < 3)
147  {
148  if (verbosity >= 2)
149  LogWarning("measurements.size() < 3");
150  continue;
151  }
152 
154 
162  int pid = 13; //
163  //SMART(genfit::AbsTrackRep) rep = NEW(genfit::RKTrackRep)(pid);
164  genfit::AbsTrackRep* rep = new genfit::RKTrackRep(pid);
165 
167  PHGenFit::Track* track = new PHGenFit::Track(rep, seed_pos, seed_mom,
168  seed_cov);
169 
170  //LogDEBUG;
172  track->addMeasurements(measurements);
173 
174  //LogDEBUG;
176  int fitting_err = _fitter->processTrack(track, _do_evt_display);
177 
178  if (fitting_err != 0)
179  {
180  // LogDEBUG;
181  // std::cout<<"event: "<<ientry<<"\n";
182  continue;
183  }
184 
185  SvtxTrack* svtx_track_out = MakeSvtxTrack(track);
186 
187  _trackmap_out->insert(svtx_track_out);
188  }
189 
191 }
192 
194 {
195  // create nodes...
196  PHNodeIterator iter(topNode);
197 
198  PHCompositeNode* dstNode = static_cast<PHCompositeNode*>(iter.findFirst(
199  "PHCompositeNode", "DST"));
200  if (!dstNode)
201  {
202  cerr << PHWHERE << "DST Node missing, doing nothing." << endl;
204  }
205  PHNodeIterator iter_dst(dstNode);
206 
207  // Create the FGEM node
208  PHCompositeNode* tb_node = dynamic_cast<PHCompositeNode*>(iter_dst.findFirst(
209  "PHCompositeNode", "FGEM"));
210  if (!tb_node)
211  {
212  tb_node = new PHCompositeNode("FGEM");
213  dstNode->addNode(tb_node);
214  if (verbosity > 0)
215  cout << "FGEM node added" << endl;
216  }
217 
219 
221  _trackmap_out, "ForwardTrackMap", "PHObject");
222  tb_node->addNode(tracks_node);
223  if (verbosity > 0)
224  cout << "FGEM/ForwardTrackMap node added" << endl;
225 
227 }
228 
230 {
231  //DST objects
232  //Truth container
233  _truth_container = findNode::getClass<PHG4TruthInfoContainer>(topNode,
234  "G4TruthInfo");
235  if (!_truth_container && _event < 2)
236  {
237  cout << PHWHERE << " PHG4TruthInfoContainer node not found on node tree"
238  << endl;
240  }
241 
242  for (int i = 0; i < _N_DETECTOR_LAYER; i++)
243  {
244  PHG4HitContainer* phg4hit = findNode::getClass<PHG4HitContainer>(topNode,
245  _phg4hits_names[i].c_str());
246  if (!phg4hit && _event < 2)
247  {
248  cout << PHWHERE << _phg4hits_names[i].c_str() << " node not found on node tree"
249  << endl;
251  }
252 
253  _phg4hits.push_back(phg4hit);
254  }
255 
256  _trackmap_out = findNode::getClass<SvtxTrackMap>(topNode,
257  "ForwardTrackMap");
258  if (!_trackmap_out && _event < 2)
259  {
260  cout << PHWHERE << " ForwardTrackMap node not found on node tree"
261  << endl;
263  }
264 
266 }
267 
269  const PHG4Particle* particle,
270  std::vector<PHGenFit::Measurement*>& meas_out, TVector3& seed_pos,
271  TVector3& seed_mom, TMatrixDSym& seed_cov, const bool do_smearing)
272 {
273  seed_pos.SetXYZ(0, 0, 0);
274  seed_mom.SetXYZ(0, 0, 10);
275  seed_cov.ResizeTo(6, 6);
276 
277  for (int i = 0; i < 3; i++)
278  {
279  seed_cov[i][i] = _phi_resolution * _phi_resolution;
280  }
281 
282  for (int i = 3; i < 6; i++)
283  {
284  seed_cov[i][i] = 10;
285  }
286 
287  if (particle)
288  {
289  TVector3 True_mom(particle->get_px(), particle->get_py(),
290  particle->get_pz());
291 
292  seed_mom.SetXYZ(particle->get_px(), particle->get_py(),
293  particle->get_pz());
294  if (do_smearing)
295  {
296  const double momSmear = 3. / 180. * TMath::Pi(); // rad
297  const double momMagSmear = 0.1; // relative
298 
299  seed_mom.SetPhi(gRandom->Gaus(True_mom.Phi(), momSmear));
300  seed_mom.SetTheta(gRandom->Gaus(True_mom.Theta(), momSmear));
301  seed_mom.SetMag(
302  gRandom->Gaus(True_mom.Mag(),
303  momMagSmear * True_mom.Mag()));
304  }
305  }
306 
307  meas_out.clear();
308 
309  for (int ilayer = 0; ilayer < _N_DETECTOR_LAYER; ilayer++)
310  {
311  if (!_phg4hits[ilayer])
312  {
313  LogError("No _phg4hits[i] found!");
314  continue;
315  }
316 
318  _phg4hits[ilayer]->getHits().first;
319  itr != _phg4hits[ilayer]->getHits().second; ++itr)
320  {
321  PHG4Hit* hit = itr->second;
322  if (!hit)
323  {
324  LogDebug("No PHG4Hit Found!");
325  continue;
326  }
327  if (hit->get_trkid() == particle->get_track_id() || gRandom->Uniform(0, 1) < _pat_rec_nosise_prob)
328  {
330  if (gRandom->Uniform(0, 1) <= _pat_rec_hit_finding_eff)
331  meas_out.push_back(meas);
332  }
333  }
334 
335  } /*Loop detector layers*/
336 
338 }
339 
341  const PHGenFit::Track* phgf_track)
342 {
343  double chi2 = phgf_track->get_chi2();
344  double ndf = phgf_track->get_ndf();
345 
346  genfit::MeasuredStateOnPlane* gf_state = phgf_track->extrapolateToPlane(TVector3(0., 0., 0.), TVector3(0., 0., 1.));
347  TVector3 mom = gf_state->getMom();
348  TVector3 pos = gf_state->getPos();
349  TMatrixDSym cov = gf_state->get6DCov();
350 
351  // SvtxTrack_v1* out_track = new SvtxTrack_v1(*static_cast<const SvtxTrack_v1*> (svtx_track));
352  SvtxTrack_v1* out_track = new SvtxTrack_v1();
353 
360  double dca2d = gf_state->getState()[3];
361  out_track->set_dca2d(dca2d);
362  out_track->set_dca2d_error(gf_state->getCov()[3][3]);
363  double dca3d = sqrt(
364  dca2d * dca2d +
365  gf_state->getState()[4] * gf_state->getState()[4]);
366  out_track->set_dca(dca3d);
367 
368  out_track->set_chisq(chi2);
369  out_track->set_ndf(ndf);
370  out_track->set_charge(phgf_track->get_charge());
371 
372  out_track->set_px(mom.Px());
373  out_track->set_py(mom.Py());
374  out_track->set_pz(mom.Pz());
375 
376  out_track->set_x(pos.X());
377  out_track->set_y(pos.Y());
378  out_track->set_z(pos.Z());
379 
380  for (int i = 0; i < 6; i++)
381  {
382  for (int j = i; j < 6; j++)
383  {
384  out_track->set_error(i, j, cov[i][j]);
385  }
386  }
387 
388  return out_track;
389 }
390 
392 {
393  PHGenFit::PlanarMeasurement* meas = NULL;
394 
395  TVector3 pos(
396  g4hit->get_avg_x(),
397  g4hit->get_avg_y(),
398  g4hit->get_avg_z());
399 
400  TVector3 v(pos.X(), pos.Y(), 0);
401  v = 1 / v.Mag() * v;
402 
403  TVector3 u = v.Cross(TVector3(0, 0, 1));
404  u = 1 / u.Mag() * u;
405 
406  double u_smear = gRandom->Gaus(0, _phi_resolution);
407  double v_smear = gRandom->Gaus(0, _r_resolution);
408  pos.SetX(g4hit->get_avg_x() + u_smear * u.X() + v_smear * v.X());
409  pos.SetY(g4hit->get_avg_y() + u_smear * u.Y() + v_smear * v.Y());
410 
412 
413  // std::cout<<"------------\n";
414  // pos.Print();
415  // std::cout<<"at "<<istation<<" station, "<<ioctant << " octant \n";
416  // u.Print();
417  // v.Print();
418 
419  //dynamic_cast<PHGenFit::PlanarMeasurement*> (meas)->getMeasurement()->Print();
420 
421  return meas;
422 }
423 
425  const double dphi)
426 {
427  PHGenFit::PlanarMeasurement* meas = NULL;
428 
429  TVector3 u(1, 0, 0);
430  TVector3 v(0, 1, 0);
431 
432  TVector3 pos = vtx;
433 
434  double u_smear = gRandom->Gaus(0, dphi);
435  double v_smear = gRandom->Gaus(0, dr);
436  pos.SetX(vtx.X() + u_smear * u.X() + v_smear * v.X());
437  pos.SetY(vtx.Y() + u_smear * u.Y() + v_smear * v.Y());
438 
439  meas = new PHGenFit::PlanarMeasurement(pos, u, v, dr, dphi);
440 
441  return meas;
442 }