Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TpcPrototypeGenFitTrkFitter.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TpcPrototypeGenFitTrkFitter.cc
1 
9 #include "TpcPrototypeTrack.h"
10 #include "TpcPrototypeCluster.h"
11 
12 #include <trackbase/TrkrCluster.h> // for TrkrCluster
14 #include <trackbase/TrkrDefs.h>
16 
20 #include <trackbase_historic/SvtxTrackState.h> // for SvtxTrackState
22 #include <trackbase_historic/SvtxVertex.h> // for SvtxVertex
23 #include <trackbase_historic/SvtxVertexMap.h> // for SvtxVertexMap
24 #include <trackbase_historic/SvtxVertexMap_v1.h>
25 #include <trackbase_historic/SvtxVertex_v1.h>
26 
27 #include <phgenfit/Fitter.h>
28 #include <phgenfit/Measurement.h> // for Measurement
29 #include <phgenfit/PlanarMeasurement.h>
30 #include <phgenfit/SpacepointMeasurement.h>
31 #include <phgenfit/Track.h>
32 
34 #include <fun4all/PHTFileServer.h>
35 #include <fun4all/SubsysReco.h> // for SubsysReco
36 
37 #include <phool/PHCompositeNode.h>
38 #include <phool/PHIODataNode.h>
39 #include <phool/PHNode.h> // for PHNode
40 #include <phool/PHNodeIterator.h>
41 #include <phool/PHObject.h> // for PHObject
42 #include <phool/getClass.h>
43 #include <phool/phool.h>
44 
45 #include <phfield/PHFieldUtility.h>
46 #include <phgeom/PHGeomUtility.h>
47 
48 #include <GenFit/AbsMeasurement.h> // for AbsMeasurement
49 #include <GenFit/EventDisplay.h> // for EventDisplay
50 #include <GenFit/Exception.h> // for Exception
51 #include <GenFit/GFRaveConverters.h>
52 #include <GenFit/GFRaveTrackParameters.h> // for GFRaveTrackParame...
53 #include <GenFit/GFRaveVertex.h>
54 #include <GenFit/GFRaveVertexFactory.h>
55 #include <GenFit/KalmanFitterInfo.h>
56 #include <GenFit/MeasuredStateOnPlane.h>
57 #include <GenFit/RKTrackRep.h>
58 #include <GenFit/Track.h>
59 #include <GenFit/TrackPoint.h> // for TrackPoint
60 
61 //Rave
62 #include <rave/ConstantMagneticField.h>
63 #include <rave/VacuumPropagator.h> // for VacuumPropagator
64 #include <rave/VertexFactory.h>
65 
66 #include <TClonesArray.h>
67 #include <TMatrixDSymfwd.h> // for TMatrixDSym
68 #include <TMatrixFfwd.h> // for TMatrixF
69 #include <TMatrixT.h> // for TMatrixT, operator*
70 #include <TMatrixTSym.h> // for TMatrixTSym
71 #include <TMatrixTUtils.h> // for TMatrixTRow
72 #include <TRotation.h>
73 #include <TTree.h>
74 #include <TVector3.h>
75 #include <TVectorDfwd.h> // for TVectorD
76 #include <TVectorT.h> // for TVectorT
77 
78 #include <cassert>
79 #include <cmath> // for sqrt, NAN
80 #include <iostream>
81 #include <map>
82 #include <memory>
83 #include <set>
84 #include <utility>
85 #include <vector>
86 
87 class PHField;
88 class TGeoManager;
89 namespace genfit
90 {
91 class AbsTrackRep;
92 }
93 
94 #define LogDebug(exp) std::cout << "DEBUG: " << __FILE__ << ": " << __LINE__ << ": " << exp << std::endl
95 #define LogError(exp) std::cout << "ERROR: " << __FILE__ << ": " << __LINE__ << ": " << exp << std::endl
96 #define LogWarning(exp) std::cout << "WARNING: " << __FILE__ << ": " << __LINE__ << ": " << exp << std::endl
97 
98 #define WILD_FLOAT -9999.
99 
100 #define _DEBUG_MODE_ 0
101 
102 //#define _DEBUG_
103 
104 using namespace std;
105 
107 {
108  public:
111  {
112  rave::ConstantMagneticField mfield(0., 0., 0.); // RAVE use Tesla
113  _factory = new rave::VertexFactory(mfield, rave::VacuumPropagator(),
114  "default", Verbosity());
115 
116  IdGFTrackStateMap_.clear();
117  }
118 
121  {
122  clearMap();
123 
124  delete _factory;
125  }
126 
127  void findVertices(std::vector<genfit::GFRaveVertex*>* vertices,
128  const std::vector<genfit::Track*>& tracks, const bool use_beamspot = false)
129  {
130  clearMap();
131 
132  try
133  {
134  genfit::RaveToGFVertices(vertices,
135  _factory->create(
136  genfit::GFTracksToTracks(tracks, NULL,
137  IdGFTrackStateMap_, 0),
138  use_beamspot),
139  IdGFTrackStateMap_);
140  }
141  catch (genfit::Exception& e)
142  {
143  std::cerr << e.what();
144  }
145  }
146 
147  void findVertices(std::vector<genfit::GFRaveVertex*>* vertices,
148  const std::vector<genfit::Track*>& tracks,
149  std::vector<genfit::MeasuredStateOnPlane*>& GFStates,
150  const bool use_beamspot = false)
151  {
152  clearMap();
153 
154  try
155  {
156  genfit::RaveToGFVertices(vertices,
157  _factory->create(
158  genfit::GFTracksToTracks(tracks, &GFStates,
159  IdGFTrackStateMap_, 0),
160  use_beamspot),
161  IdGFTrackStateMap_);
162  }
163  catch (genfit::Exception& e)
164  {
165  std::cerr << e.what();
166  }
167  }
168 
169  private:
170  void clearMap()
171  {
172  for (unsigned int i = 0; i < IdGFTrackStateMap_.size(); ++i)
173  delete IdGFTrackStateMap_[i].state_;
174 
175  IdGFTrackStateMap_.clear();
176  }
177 
178  std::map<int, genfit::trackAndState> IdGFTrackStateMap_;
179 
180  rave::VertexFactory* _factory;
181 };
182 
183 /*
184  * Constructor
185  */
187  : SubsysReco(name)
188  , _flags(NONE)
189  , _output_mode(TpcPrototypeGenFitTrkFitter::MakeNewNode)
190  , _over_write_svtxtrackmap(true)
191  , _over_write_svtxvertexmap(true)
192  , _fit_primary_tracks(false)
193  , _use_truth_vertex(false)
194  , _fitter(NULL)
195  , _track_fitting_alg_name("DafRef")
196  , _primary_pid_guess(2212)
197  , _fit_min_pT(0.1)
198  , _vertex_min_ndf(20)
199  , _vertex_finder(NULL)
200  , _vertexing_method("avf-smoothing:1")
201  // , _truth_container(NULL)
202  , _clustermap(NULL)
203  , _trackmap(NULL)
204  , _vertexmap(NULL)
205  , _trackmap_refit(NULL)
206  , _primary_trackmap(NULL)
207  , _vertexmap_refit(NULL)
208  , _do_eval(false)
209  , _eval_outname("TpcPrototypeGenFitTrkFitter_eval.root")
210  , _eval_tree(NULL)
211  , _tca_ntrack(-1)
212  , _tca_particlemap(NULL)
213  , _tca_vtxmap(NULL)
214  , _tca_trackmap(NULL)
215  , _tca_tpctrackmap(nullptr)
216  , _tca_vertexmap(NULL)
217  , _tca_trackmap_refit(NULL)
218  , _tca_primtrackmap(NULL)
219  , _tca_vertexmap_refit(NULL)
220  , _do_evt_display(false)
221 {
222  Verbosity(0);
223 
224  _event = 0;
225 
226  _cluster_eval_tree = NULL;
233 }
234 
235 /*
236  * Init
237  */
239 {
240  // CreateNodes(topNode);
241 
243 }
244 
245 /*
246  * Init run
247  */
249 {
250  CreateNodes(topNode);
251 
252  TGeoManager* tgeo_manager = PHGeomUtility::GetTGeoManager(topNode);
253  PHField* field = PHFieldUtility::GetFieldMapNode(nullptr, topNode);
254 
255  //_fitter = new PHGenFit::Fitter("sPHENIX_Geo.root","sPHENIX.2d.root", 1.4 / 1.5);
256  _fitter = PHGenFit::Fitter::getInstance(tgeo_manager,
258  "RKTrackRep", _do_evt_display);
259 
260  if (!_fitter)
261  {
262  cerr << PHWHERE << endl;
264  }
265 
267 
268  //LogDebug(genfit::FieldManager::getInstance()->getFieldVal(TVector3(0, 0, 0)).Z());
269 
271  //_vertex_finder->setMethod("kalman-smoothing:1"); //! kalman-smoothing:1 is the defaul method
273  //_vertex_finder->setBeamspot();
274 
275  //_vertex_finder = new PHRaveVertexFactory(Verbosity());
276 
277  if (!_vertex_finder)
278  {
279  cerr << PHWHERE << endl;
281  }
282 
283  if (_do_eval)
284  {
285  if (Verbosity() >= 1)
286  cout << PHWHERE << " Openning file: " << _eval_outname << endl;
287  PHTFileServer::get().open(_eval_outname, "RECREATE");
288  init_eval_tree();
289  }
290 
292 }
293 /*
294  * process_event():
295  * Call user instructions for every event.
296  * This function contains the analysis structure.
297  *
298  */
300 {
301  _event++;
302 
303  if (Verbosity() > 1)
304  std::cout << PHWHERE << "Events processed: " << _event << std::endl;
305  // if (_event % 1000 == 0)
306  // cout << PHWHERE << "Events processed: " << _event << endl;
307 
308  //cout << "Start TpcPrototypeGenFitTrkFitter::process_event" << endl;
309 
310  GetNodes(topNode);
311 
313  vector<genfit::Track*> rf_gf_tracks;
314  rf_gf_tracks.clear();
315 
316  vector<std::shared_ptr<PHGenFit::Track> > rf_phgf_tracks;
317  // rf_phgf_tracks.clear();
318 
319  map<unsigned int, unsigned int> svtxtrack_genfittrack_map;
320 
321  if (_trackmap_refit)
323 
324  // _trackmap is SvtxTrackMap from the node tree
325  for (SvtxTrackMap::Iter iter = _trackmap->begin(); iter != _trackmap->end();
326  ++iter)
327  {
328  SvtxTrack* svtx_track = iter->second;
329  if (Verbosity() > 50)
330  {
331  cout << " process SVTXTrack " << iter->first << endl;
332  svtx_track->identify();
333  }
334  if (!svtx_track)
335  continue;
336  if (!(svtx_track->get_pt() > _fit_min_pT))
337  continue;
338 
340  std::shared_ptr<PHGenFit::Track> rf_phgf_track = ReFitTrack(topNode, svtx_track);
341 
342  if (rf_phgf_track)
343  {
344  svtxtrack_genfittrack_map[svtx_track->get_id()] =
345  rf_phgf_tracks.size();
346  rf_phgf_tracks.push_back(rf_phgf_track);
347  if (rf_phgf_track->get_ndf() > _vertex_min_ndf)
348  rf_gf_tracks.push_back(rf_phgf_track->getGenFitTrack());
349  }
350  }
351 
352  /*
353  * add tracks to event display
354  * needs to make copied for smart ptrs will be destroied even
355  * there are still references in TGeo::EventView
356  */
357  if (_do_evt_display)
358  {
359  //search for unused clusters
360 
362  //unused clusters
363  set<TrkrDefs::cluskey> cluster_ids;
364  //all clusters
365  auto cluster_range = _clustermap->getClusters(TrkrDefs::tpcId);
366  for (auto iter = cluster_range.first; iter != cluster_range.second; ++iter)
367  {
368  cluster_ids.insert(iter->first);
369  }
370  // minus used clusters
371  for (SvtxTrackMap::Iter iter = _trackmap->begin(); iter != _trackmap->end();
372  ++iter)
373  {
374  SvtxTrack* svtx_track = iter->second;
375  for (auto iter = svtx_track->begin_cluster_keys();
376  iter != svtx_track->end_cluster_keys(); ++iter)
377  {
378  cluster_ids.erase(*iter);
379  }
380  }
381 
382  // // add tracks
383  vector<genfit::Track*> copy;
384  for (genfit::Track* t : rf_gf_tracks)
385  {
386  copy.push_back(new genfit::Track(*t));
387  }
388  //
389 
390  //add clusters
391  for (const auto cluster_id : cluster_ids)
392  {
393  const TrkrCluster* cluster =
394  _clustermap->findCluster(cluster_id);
395  assert(cluster);
396 
397  std::shared_ptr<PHGenFit::Track> cluster_holder = DisplayCluster(cluster);
398  if (cluster_holder.get())
399  copy.push_back(new genfit::Track(*cluster_holder->getGenFitTrack()));
400  }
401 
402  _fitter->getEventDisplay()->addEvent(copy);
403  }
404 
405  for (SvtxTrackMap::Iter iter = _trackmap->begin(); iter != _trackmap->end();)
406  {
407  std::shared_ptr<PHGenFit::Track> rf_phgf_track = NULL;
408 
409  if (svtxtrack_genfittrack_map.find(iter->second->get_id()) != svtxtrack_genfittrack_map.end())
410  {
411  unsigned int itrack =
412  svtxtrack_genfittrack_map[iter->second->get_id()];
413  rf_phgf_track = rf_phgf_tracks[itrack];
414  }
415 
416  if (rf_phgf_track)
417  {
418  //FIXME figure out which vertex to use.
419  SvtxVertex* vertex = NULL;
421  {
422  if (_vertexmap->size() > 0)
423  vertex = _vertexmap->get(0);
424  //cout << PHWHERE << " will use vertex " << vertex->get_x() << " " << vertex->get_y() << " " << vertex->get_z() << endl;
425  }
426  else
427  {
428  if (_vertexmap_refit->size() > 0)
429  vertex = _vertexmap_refit->get(0);
430  }
431 
432  std::shared_ptr<SvtxTrack> rf_track = MakeSvtxTrack(iter->second, rf_phgf_track,
433  vertex);
434  if (!rf_track)
435  {
436  //if (_output_mode == OverwriteOriginalNode)
438  {
439  auto key = iter->first;
440  ++iter;
441  _trackmap->erase(key);
442  continue;
443  }
444  }
445 
446  // delete vertex;//DEBUG
447 
448  // rf_phgf_tracks.push_back(rf_phgf_track);
449  // rf_gf_tracks.push_back(rf_phgf_track->getGenFitTrack());
450 
452  if (_trackmap_refit)
453  {
454  _trackmap_refit->insert(rf_track.get());
455  // delete rf_track;
456  }
457 
459  {
460  *(dynamic_cast<SvtxTrack_v1*>(iter->second)) =
461  *(dynamic_cast<SvtxTrack_v1*>(rf_track.get()));
462  // delete rf_track;
463  }
464  }
465  else
466  {
468  {
469  auto key = iter->first;
470  ++iter;
471  _trackmap->erase(key);
472  continue;
473  }
474  }
475 
476  ++iter;
477  }
478  // Need to keep tracks if _do_evt_display
479  if (!_do_evt_display)
480  {
481  rf_phgf_tracks.clear();
482  }
483 
484  if (_do_eval)
485  {
486  fill_eval_tree(topNode);
487  }
488 #ifdef _DEBUG_
489  cout << __LINE__ << endl;
490 #endif
492 }
493 
494 /*
495  * End
496  */
498 {
499  if (_do_eval)
500  {
501  if (Verbosity() >= 1)
502  cout << PHWHERE << " Writing to file: " << _eval_outname << endl;
504  _eval_tree->Write();
505  _cluster_eval_tree->Write();
506  }
507 
508  if (_do_evt_display)
509  {
510  // if(opts[i] == 'A') drawAutoScale_ = true;
511  // if(opts[i] == 'B') drawBackward_ = true;
512  // if(opts[i] == 'D') drawDetectors_ = true;
513  // if(opts[i] == 'E') drawErrors_ = true;
514  // if(opts[i] == 'F') drawForward_ = true;
515  // if(opts[i] == 'H') drawHits_ = true;
516  // if(opts[i] == 'M') drawTrackMarkers_ = true;
517  // if(opts[i] == 'P') drawPlanes_ = true;
518  // if(opts[i] == 'S') drawScaleMan_ = true;
519  // if(opts[i] == 'T') drawTrack_ = true;
520  // if(opts[i] == 'X') drawSilent_ = true;
521  // if(opts[i] == 'G') drawGeometry_ = true;
522  _fitter->getEventDisplay()->setOptions("ADEHPTG");
523 
525  }
527 }
528 
529 /*
530  * dtor
531  */
533 {
534  delete _fitter;
535  delete _vertex_finder;
536 }
537 
538 /*
539  * fill_eval_tree():
540  */
542 {
545 
546  int i = 0;
548  itr != _trackmap->end(); ++itr)
549  {
550  // new ((*_tca_trackmap)[i])(SvtxTrack_v1)(
551  // *dynamic_cast<SvtxTrack_v1*>(itr->second));
552 
553  new ((*_tca_tpctrackmap)[i])(TpcPrototypeTrack)(*(MakeTpcPrototypeTrack(itr->second)));
554 
555  i++;
556  }
557  _tca_ntrack = i;
558 
559  i = 0;
560  if (_vertexmap)
562  itr != _vertexmap->end(); ++itr)
563  new ((*_tca_vertexmap)[i++])(SvtxVertex_v1)(
564  *dynamic_cast<SvtxVertex_v1*>(itr->second));
565 
566  if (_trackmap_refit)
567  {
568  i = 0;
570  itr != _trackmap_refit->end(); ++itr)
571  new ((*_tca_trackmap_refit)[i++])(SvtxTrack_v1)(
572  *dynamic_cast<SvtxTrack_v1*>(itr->second));
573  }
574 
576  {
577  i = 0;
579  itr != _primary_trackmap->end(); ++itr)
580  new ((*_tca_primtrackmap)[i++])(SvtxTrack_v1)(
581  *dynamic_cast<SvtxTrack_v1*>(itr->second));
582  }
583 
584  if (_vertexmap_refit)
585  {
586  i = 0;
588  itr != _vertexmap_refit->end(); ++itr)
589  new ((*_tca_vertexmap_refit)[i++])(SvtxVertex_v1)(
590  *dynamic_cast<SvtxVertex_v1*>(itr->second));
591  }
592 
593  _eval_tree->Fill();
594 
595  return;
596 }
597 
598 /*
599  * init_eval_tree
600  */
602 {
603  if (!_tca_particlemap)
604  _tca_particlemap = new TClonesArray("PHG4Particlev2");
605  if (!_tca_vtxmap)
606  _tca_vtxmap = new TClonesArray("PHG4VtxPointv1");
607 
608  if (!_tca_trackmap)
609  _tca_trackmap = new TClonesArray("SvtxTrack_v1");
610  if (!_tca_tpctrackmap)
611  _tca_tpctrackmap = new TClonesArray("TpcPrototypeTrack");
612  if (!_tca_vertexmap)
613  _tca_vertexmap = new TClonesArray("SvtxVertex_v1");
614  if (!_tca_trackmap_refit)
615  _tca_trackmap_refit = new TClonesArray("SvtxTrack_v1");
617  if (!_tca_primtrackmap)
618  _tca_primtrackmap = new TClonesArray("SvtxTrack_v1");
620  _tca_vertexmap_refit = new TClonesArray("SvtxVertex_v1");
621 
623  _eval_tree = new TTree("T", "TpcPrototypeGenFitTrkFitter Evaluation");
624  _eval_tree->Branch("nTrack", &_tca_ntrack, "nTrack/I");
625 
626  // _eval_tree->Branch("PrimaryParticle", _tca_particlemap);
627  // _eval_tree->Branch("TruthVtx", _tca_vtxmap);
628 
629  _eval_tree->Branch("SvtxTrack", _tca_trackmap);
630  _eval_tree->Branch("TPCTrack", _tca_tpctrackmap);
631  // _eval_tree->Branch("SvtxVertex", _tca_vertexmap);
632  // _eval_tree->Branch("SvtxTrackRefit", _tca_trackmap_refit);
634  _eval_tree->Branch("PrimSvtxTrack", _tca_primtrackmap);
635  // _eval_tree->Branch("SvtxVertexRefit", _tca_vertexmap_refit);
636 
637  _cluster_eval_tree = new TTree("cluster_eval", "cluster eval tree");
638  _cluster_eval_tree->Branch("x", &_cluster_eval_tree_x, "x/F");
639  _cluster_eval_tree->Branch("y", &_cluster_eval_tree_y, "y/F");
640  _cluster_eval_tree->Branch("z", &_cluster_eval_tree_z, "z/F");
641  _cluster_eval_tree->Branch("gx", &_cluster_eval_tree_gx, "gx/F");
642  _cluster_eval_tree->Branch("gy", &_cluster_eval_tree_gy, "gy/F");
643  _cluster_eval_tree->Branch("gz", &_cluster_eval_tree_gz, "gz/F");
644 }
645 
646 /*
647  * reset_eval_variables():
648  * Reset all the tree variables to their default values.
649  * Needs to be called at the start of every event
650  */
652 {
653  _tca_particlemap->Clear();
654  _tca_vtxmap->Clear();
655 
656  _tca_ntrack = -1;
657  _tca_trackmap->Clear();
658  _tca_tpctrackmap->Clear();
659  _tca_vertexmap->Clear();
660  _tca_trackmap_refit->Clear();
662  _tca_primtrackmap->Clear();
663  _tca_vertexmap_refit->Clear();
664 
671 }
672 
674 {
675  // create nodes...
676  PHNodeIterator iter(topNode);
677 
678  PHCompositeNode* dstNode = static_cast<PHCompositeNode*>(iter.findFirst(
679  "PHCompositeNode", "DST"));
680  if (!dstNode)
681  {
682  cerr << PHWHERE << "DST Node missing, doing nothing." << endl;
684  }
685  PHNodeIterator iter_dst(dstNode);
686 
687  // Create the SVTX node
688  PHCompositeNode* tb_node = dynamic_cast<PHCompositeNode*>(iter_dst.findFirst(
689  "PHCompositeNode", "SVTX"));
690  if (!tb_node)
691  {
692  tb_node = new PHCompositeNode("SVTX");
693  dstNode->addNode(tb_node);
694  if (Verbosity() > 0)
695  cout << "SVTX node added" << endl;
696  }
697 
699  {
702  _trackmap_refit, "SvtxTrackMapRefit", "PHObject");
703  tb_node->addNode(tracks_node);
704  if (Verbosity() > 0)
705  cout << "Svtx/SvtxTrackMapRefit node added" << endl;
706  }
707 
709  {
711  PHIODataNode<PHObject>* primary_tracks_node =
712  new PHIODataNode<PHObject>(_primary_trackmap, "PrimaryTrackMap",
713  "PHObject");
714  tb_node->addNode(primary_tracks_node);
715  if (Verbosity() > 0)
716  cout << "Svtx/PrimaryTrackMap node added" << endl;
717  }
718 
720  {
722  PHIODataNode<PHObject>* vertexes_node = new PHIODataNode<PHObject>(
723  _vertexmap_refit, "SvtxVertexMapRefit", "PHObject");
724  tb_node->addNode(vertexes_node);
725  if (Verbosity() > 0)
726  cout << "Svtx/SvtxVertexMapRefit node added" << endl;
727  }
728  else if (!findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMap"))
729  {
731  PHIODataNode<PHObject>* vertexes_node = new PHIODataNode<PHObject>(
732  _vertexmap, "SvtxVertexMap", "PHObject");
733  tb_node->addNode(vertexes_node);
734  if (Verbosity() > 0)
735  cout << "Svtx/SvtxVertexMap node added" << endl;
736  }
737 
739 }
740 
741 /*
742  * GetNodes():
743  * Get all the all the required nodes off the node tree
744  */
746 {
747  //DST objects
748  //Truth container
749  // _truth_container = findNode::getClass<PHG4TruthInfoContainer>(topNode,
750  // "G4TruthInfo");
751  // if (!_truth_container && _event < 2)
752  // {
753  // cout << PHWHERE << " PHG4TruthInfoContainer node not found on node tree"
754  // << endl;
755  // return Fun4AllReturnCodes::ABORTEVENT;
756  // }
757 
758  // Input Svtx Clusters
759  //_clustermap = findNode::getClass<SvtxClusterMap>(topNode, "SvtxClusterMap");
760  _clustermap = findNode::getClass<TrkrClusterContainer>(topNode, "TRKR_CLUSTER");
761  if (!_clustermap && _event < 2)
762  {
763  cout << PHWHERE << " TRKR_CLUSTER node not found on node tree"
764  << endl;
766  }
767 
768  // Input Svtx Tracks
769  _trackmap = findNode::getClass<SvtxTrackMap>(topNode, "SvtxTrackMap");
770  if (!_trackmap && _event < 2)
771  {
772  cout << PHWHERE << " SvtxTrackMap node not found on node tree"
773  << endl;
775  }
776 
777  // Input Svtx Vertices
778  _vertexmap = findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMap");
779  if (!_vertexmap && _event < 2)
780  {
781  cout << PHWHERE << " SvtxVertexrMap node not found on node tree"
782  << endl;
784  }
785 
786  // Output Svtx Tracks
788  {
789  _trackmap_refit = findNode::getClass<SvtxTrackMap>(topNode,
790  "SvtxTrackMapRefit");
791  if (!_trackmap_refit && _event < 2)
792  {
793  cout << PHWHERE << " SvtxTrackMapRefit node not found on node tree"
794  << endl;
796  }
797  }
798 
799  // Output Primary Svtx Tracks
801  {
802  _primary_trackmap = findNode::getClass<SvtxTrackMap>(topNode,
803  "PrimaryTrackMap");
804  if (!_primary_trackmap && _event < 2)
805  {
806  cout << PHWHERE << " PrimaryTrackMap node not found on node tree"
807  << endl;
809  }
810  }
811 
812  // Output Svtx Vertices
814  {
815  _vertexmap_refit = findNode::getClass<SvtxVertexMap>(topNode,
816  "SvtxVertexMapRefit");
817  if (!_vertexmap_refit && _event < 2)
818  {
819  cout << PHWHERE << " SvtxVertexMapRefit node not found on node tree"
820  << endl;
822  }
823  }
824 
826 }
827 
828 std::shared_ptr<PHGenFit::Track> TpcPrototypeGenFitTrkFitter::DisplayCluster(const TrkrCluster* cluster)
829 {
830  assert(cluster);
831 
832  if (Verbosity() >= 1)
833  {
834  cout << __PRETTY_FUNCTION__ << ": process cluster: ";
835  cluster->identify();
836  }
837 
838  // prepare seed
839  TVector3 seed_mom(100, 0, 0);
840  TVector3 seed_pos(0, 0, 0);
841  TMatrixDSym seed_cov(6);
842  for (int i = 0; i < 6; i++)
843  {
844  for (int j = 0; j < 6; j++)
845  {
846  seed_cov[i][j] = 100.;
847  }
848  }
849 
850  // Create measurements
851  std::vector<PHGenFit::Measurement*> measurements;
852 
853  TrkrDefs::cluskey cluster_key = cluster->getClusKey();
854 
855  TVector3 pos(cluster->getPosition(0), cluster->getPosition(1), cluster->getPosition(2));
856 
857  seed_mom.SetPhi(pos.Phi());
858  seed_mom.SetTheta(pos.Theta());
859 
860  seed_pos = pos;
861 
862  //TODO use u, v explicitly?
863  TVector3 n(cluster->getPosition(0), cluster->getPosition(1), 0);
864 
865  for (int radial_shift = -1; radial_shift <= 1; ++radial_shift)
866  {
867  const double new_radial = pos.Perp() + radial_shift * 0.1;
868  TVector3 new_pos(pos);
869  new_pos.SetPerp(new_radial);
870 
871  //------------------------------
872  // new
873 
874  // Replace n for the silicon subsystems
875 
876  // get the trkrid
877  int layer = TrkrDefs::getLayer(cluster_key);
878 
879  // end new
880  //-----------------
881 
883  cluster->getRPhiError(), cluster->getZError());
884 
885  if (Verbosity() > 50)
886  {
887  cout << "Add meas layer " << layer << " cluskey " << cluster_key
888  << endl
889  << " pos.X " << pos.X() << " pos.Y " << pos.Y() << " pos.Z " << pos.Z()
890  << " n.X " << n.X() << " n.Y " << n.Y()
891  << " RPhiErr " << cluster->getRPhiError()
892  << " ZErr " << cluster->getZError()
893  << endl;
894  }
895  measurements.push_back(meas);
896  }
897 
906  //TODO Add multiple TrackRep choices.
907  //int pid = 211;
908  genfit::AbsTrackRep* rep = new genfit::RKTrackRep(-13);
909  std::shared_ptr<PHGenFit::Track> track(new PHGenFit::Track(rep, seed_pos, seed_mom,
910  seed_cov));
911 
912  //TODO unsorted measurements, should use sorted ones?
913  track->addMeasurements(measurements);
914 
915  // if (measurements.size()==1) return track;
916 
921  if (_fitter->processTrack(track.get(), false) != 0)
922  {
923  if (Verbosity() >= 1)
924  {
925  LogWarning("Track fitting failed");
926  cout << __PRETTY_FUNCTION__ << ": failed cluster build: track->getChisq() " << track->get_chi2() << " get_ndf " << track->get_ndf()
927  << " mom.X " << track->get_mom().X()
928  << " mom.Y " << track->get_mom().Y()
929  << " mom.Z " << track->get_mom().Z()
930  << endl;
931  }
932  //delete track;
933  return nullptr;
934  }
935 
936  if (Verbosity() > 50)
937  cout << " track->getChisq() " << track->get_chi2() << " get_ndf " << track->get_ndf()
938  << " mom.X " << track->get_mom().X()
939  << " mom.Y " << track->get_mom().Y()
940  << " mom.Z " << track->get_mom().Z()
941  << endl;
942 
943  return track;
944 }
945 
946 //struct CompMeasurementByR {
947 // bool operator() (PHGenFit::Measurement *m1,PHGenFit::Measurement *m2) {
948 // float x1 = m1->getMeasurement()
949 //
950 // return (i<j);}
951 //} myobject;
952 
953 /*
954  * fit track with SvtxTrack as input seed.
955  * \param intrack Input SvtxTrack
956  * \param invertex Input Vertex, if fit track as a primary vertex
957  */
958 //PHGenFit::Track* TpcPrototypeGenFitTrkFitter::ReFitTrack(PHCompositeNode *topNode, const SvtxTrack* intrack,
959 std::shared_ptr<PHGenFit::Track> TpcPrototypeGenFitTrkFitter::ReFitTrack(PHCompositeNode* topNode, const SvtxTrack* intrack,
960  const SvtxVertex* invertex)
961 {
962  //std::shared_ptr<PHGenFit::Track> empty_track(NULL);
963  if (!intrack)
964  {
965  cerr << PHWHERE << " Input SvtxTrack is NULL!" << endl;
966  return NULL;
967  }
968 
969  // prepare seed
970  TVector3 seed_mom(100, 0, 0);
971  TVector3 seed_pos(0, 0, 0);
972  TMatrixDSym seed_cov(6);
973  for (int i = 0; i < 6; i++)
974  {
975  for (int j = 0; j < 6; j++)
976  {
977  seed_cov[i][j] = 100.;
978  }
979  }
980 
981  // Create measurements
982  std::vector<PHGenFit::Measurement*> measurements;
983 
985  const double vertex_chi2_over_dnf_cut = 1000;
986  const double vertex_cov_element_cut = 10000; //arbitrary cut cm*cm
987 
988  if (invertex and invertex->size_tracks() > 1 and invertex->get_chisq() / invertex->get_ndof() < vertex_chi2_over_dnf_cut)
989  {
990  TVector3 pos(invertex->get_x(), invertex->get_y(), invertex->get_z());
991  TMatrixDSym cov(3);
992  cov.Zero();
993  bool is_vertex_cov_sane = true;
994  for (unsigned int i = 0; i < 3; i++)
995  for (unsigned int j = 0; j < 3; j++)
996  {
997  cov(i, j) = invertex->get_error(i, j);
998 
999  if (i == j)
1000  {
1001  if (!(invertex->get_error(i, j) > 0 and invertex->get_error(i, j) < vertex_cov_element_cut))
1002  is_vertex_cov_sane = false;
1003  }
1004  }
1005 
1006  if (is_vertex_cov_sane)
1007  {
1009  pos, cov);
1010  measurements.push_back(meas);
1011  if (Verbosity() >= 2)
1012  {
1013  meas->getMeasurement()->Print();
1014  }
1015  }
1016  }
1017 
1018  // sort clusters with radius before fitting
1019  if (Verbosity() > 20) intrack->identify();
1020  std::map<float, TrkrDefs::cluskey> m_r_cluster_id;
1021  for (auto iter = intrack->begin_cluster_keys();
1022  iter != intrack->end_cluster_keys(); ++iter)
1023  {
1024  TrkrDefs::cluskey cluster_key = *iter;
1025  TrkrCluster* cluster = _clustermap->findCluster(cluster_key);
1026  float x = cluster->getPosition(0);
1027  float y = cluster->getPosition(1);
1028  float r = sqrt(x * x + y * y);
1029  m_r_cluster_id.insert(std::pair<float, TrkrDefs::cluskey>(r, cluster_key));
1030  int layer_out = TrkrDefs::getLayer(cluster_key);
1031  if (Verbosity() > 20) cout << " Layer " << layer_out << " cluster " << cluster_key << " radius " << r << endl;
1032  }
1033 
1034  for (auto iter = m_r_cluster_id.begin();
1035  iter != m_r_cluster_id.end();
1036  ++iter)
1037  {
1038  TrkrDefs::cluskey cluster_key = iter->second;
1039  TrkrCluster* cluster = _clustermap->findCluster(cluster_key);
1040  if (!cluster)
1041  {
1042  LogError("No cluster Found!");
1043  continue;
1044  }
1045 
1046  TVector3 pos(cluster->getPosition(0), cluster->getPosition(1), cluster->getPosition(2));
1047 
1048  seed_mom.SetPhi(pos.Phi());
1049  seed_mom.SetTheta(pos.Theta());
1050 
1051  //TODO use u, v explicitly?
1052  TVector3 n(cluster->getPosition(0), cluster->getPosition(1), 0);
1053 
1054  //------------------------------
1055  // new
1056 
1057  // Replace n for the silicon subsystems
1058 
1059  // get the trkrid
1060  unsigned int trkrid = TrkrDefs::getTrkrId(cluster_key);
1061  int layer = TrkrDefs::getLayer(cluster_key);
1062 
1063  if (trkrid == TrkrDefs::mvtxId)
1064  {
1065  assert(0);
1066  }
1067  else if (trkrid == TrkrDefs::inttId)
1068  {
1069  assert(0);
1070  }
1071  // end new
1072  //-----------------
1073 
1075  cluster->getRPhiError(), cluster->getZError());
1076 
1077  if (Verbosity() > 50)
1078  {
1079  cout << "Add meas layer " << layer << " cluskey " << cluster_key
1080  << endl
1081  << " pos.X " << pos.X() << " pos.Y " << pos.Y() << " pos.Z " << pos.Z()
1082  << " n.X " << n.X() << " n.Y " << n.Y()
1083  << " RPhiErr " << cluster->getRPhiError()
1084  << " ZErr " << cluster->getZError()
1085  << endl;
1086  }
1087  measurements.push_back(meas);
1088  }
1089 
1098  //TODO Add multiple TrackRep choices.
1099  //int pid = 211;
1101  std::shared_ptr<PHGenFit::Track> track(new PHGenFit::Track(rep, seed_pos, seed_mom,
1102  seed_cov));
1103  track->addMeasurements(measurements);
1104 
1105  // if (measurements.size()==1) return track;
1106 
1111  if (_fitter->processTrack(track.get(), false) != 0)
1112  {
1113  if (Verbosity() >= 1)
1114  {
1115  LogWarning("Track fitting failed");
1116  cout << __PRETTY_FUNCTION__ << " track->getChisq() " << track->get_chi2() << " get_ndf " << track->get_ndf()
1117  << " mom.X " << track->get_mom().X()
1118  << " mom.Y " << track->get_mom().Y()
1119  << " mom.Z " << track->get_mom().Z()
1120  << endl;
1121  }
1122  //delete track;
1123  return nullptr;
1124  }
1125 
1126  if (Verbosity() > 50)
1127  cout << " track->getChisq() " << track->get_chi2() << " get_ndf " << track->get_ndf()
1128  << " mom.X " << track->get_mom().X()
1129  << " mom.Y " << track->get_mom().Y()
1130  << " mom.Z " << track->get_mom().Z()
1131  << endl;
1132 
1133  return track;
1134 }
1135 
1136 shared_ptr<TpcPrototypeTrack> TpcPrototypeGenFitTrkFitter::MakeTpcPrototypeTrack(const SvtxTrack* svtxtrack)
1137 {
1138  assert(svtxtrack);
1139  if (Verbosity() >= 1)
1140  {
1141  cout << __PRETTY_FUNCTION__ << " refit ";
1142  svtxtrack->identify();
1143  }
1144 
1145  shared_ptr<TpcPrototypeTrack> track(new TpcPrototypeTrack);
1146  track->event = _event;
1147  track->trackID = svtxtrack->get_id();
1148  track->chisq = svtxtrack->get_chisq();
1149  track->ndf = svtxtrack->get_ndf();
1150  track->px = svtxtrack->get_px();
1151  track->py = svtxtrack->get_py();
1152  track->pz = svtxtrack->get_pz();
1153  track->x = svtxtrack->get_x();
1154  track->y = svtxtrack->get_y();
1155  track->z = svtxtrack->get_z();
1156 
1157  track->nCluster = 0;
1158 
1159  // sort intput cluters to one per layer
1161  vector<TrkrCluster*> clusterLayer(track->nLayer, nullptr);
1162  for (auto iter = svtxtrack->begin_cluster_keys();
1163  iter != svtxtrack->end_cluster_keys(); ++iter)
1164  {
1165  TrkrDefs::cluskey cluster_key = *iter;
1166  TrkrCluster* cluster = _clustermap->findCluster(cluster_key);
1167  int layer = TrkrDefs::getLayer(cluster_key);
1168 
1169  if (Verbosity())
1170  {
1171  cout << __PRETTY_FUNCTION__ << " - layer sorting cluster ";
1172  cluster->identify();
1173  }
1174 
1175  assert(layer >= 0);
1176  assert(layer < track->nLayer);
1177  assert(cluster);
1178 
1179  if (clusterLayer[layer])
1180  {
1181  cout << __PRETTY_FUNCTION__ << " -WARNING- more than one cluster at layer " << layer << ": " << endl;
1182  clusterLayer[layer]->identify();
1183  cluster->identify();
1184  }
1185  else
1186  {
1187  clusterLayer[layer] = cluster;
1188  track->nCluster += 1;
1189  }
1190  }
1191 
1192  if (track->nCluster < 4)
1193  return track;
1194 
1195  // refit by "excluding" each cluster
1196  // prepare seed
1197  TVector3 seed_mom(svtxtrack->get_px(), svtxtrack->get_py(), svtxtrack->get_pz());
1198  seed_mom.SetMag(120);
1199 
1200  TVector3 seed_pos(svtxtrack->get_x(), svtxtrack->get_y(), svtxtrack->get_z());
1201  TMatrixDSym seed_cov(6);
1202  for (int i = 0; i < 6; i++)
1203  {
1204  for (int j = 0; j < 6; j++)
1205  {
1206  seed_cov[i][j] = 100.;
1207  }
1208  }
1209  for (int layerStudy = 0; layerStudy < track->nLayer; ++layerStudy)
1210  {
1211  //scale uncertainty for the cluster in study with this factor
1212  const static double errorScaleFactor = 100;
1213 
1214  if (clusterLayer[layerStudy] == nullptr) continue;
1215 
1216  // Create measurements
1217  std::vector<PHGenFit::Measurement*> measurements;
1218 
1219  int indexStudy = -1;
1220  int currentIndex = 0;
1221  for (const auto cluster : clusterLayer)
1222  {
1223  if (!cluster) continue;
1224 
1225  assert(cluster);
1226  TrkrDefs::cluskey cluster_key = cluster->getClusKey();
1227  int layer = TrkrDefs::getLayer(cluster_key);
1228 
1229  const double scale_this_layer = (layer == layerStudy) ? errorScaleFactor : 1;
1230 
1231  TVector3 pos(cluster->getPosition(0), cluster->getPosition(1), cluster->getPosition(2));
1232 
1233  // seed_mom.SetPhi(pos.Phi());
1234  // seed_mom.SetTheta(pos.Theta());
1235 
1236  //TODO use u, v explicitly?
1237  TVector3 n(cluster->getPosition(0), cluster->getPosition(1), 0);
1238 
1240  cluster->getRPhiError() * scale_this_layer,
1241  cluster->getZError() * scale_this_layer);
1242 
1243  measurements.push_back(meas);
1244 
1245  if (layer == layerStudy) indexStudy = currentIndex;
1246  ++currentIndex;
1247  } // for (const auto cluster : clusterLayer)
1248  assert(indexStudy >= 0);
1249  assert(indexStudy < track->nLayer);
1250 
1251  // do the fit
1253  std::shared_ptr<PHGenFit::Track> phgf_track(new PHGenFit::Track(rep, seed_pos, seed_mom,
1254  seed_cov));
1255  phgf_track->addMeasurements(measurements);
1256  if (_fitter->processTrack(phgf_track.get(), false) != 0)
1257  {
1258  if (Verbosity() >= 1)
1259  {
1260  LogWarning("Track fitting failed");
1261  cout << __PRETTY_FUNCTION__ << " track->getChisq() " << phgf_track->get_chi2() << " get_ndf " << phgf_track->get_ndf()
1262  << " mom.X " << phgf_track->get_mom().X()
1263  << " mom.Y " << phgf_track->get_mom().Y()
1264  << " mom.Z " << phgf_track->get_mom().Z()
1265  << endl;
1266  }
1267  //delete track;
1268  continue;
1269  }
1270 
1271  //propagate to state
1272  {
1273  std::shared_ptr<const genfit::MeasuredStateOnPlane> gf_state = NULL;
1274  try
1275  {
1276  const genfit::Track* gftrack = phgf_track->getGenFitTrack();
1277  assert(gftrack);
1278  const genfit::AbsTrackRep* rep = gftrack->getCardinalRep();
1279  assert(rep);
1280  genfit::TrackPoint* trpoint = gftrack->getPointWithMeasurementAndFitterInfo(indexStudy, gftrack->getCardinalRep());
1281  assert(trpoint);
1282  genfit::KalmanFitterInfo* kfi = static_cast<genfit::KalmanFitterInfo*>(trpoint->getFitterInfo(rep));
1283  assert(kfi);
1284  //gf_state = std::shared_ptr <genfit::MeasuredStateOnPlane> (const_cast<genfit::MeasuredStateOnPlane*> (&(kfi->getFittedState(true))));
1285  const genfit::MeasuredStateOnPlane* temp_state = &(kfi->getFittedState(true));
1286  gf_state = std::shared_ptr<genfit::MeasuredStateOnPlane>(new genfit::MeasuredStateOnPlane(*temp_state));
1287  }
1288  catch (...)
1289  {
1290  if (Verbosity() > 1)
1291  LogWarning("Exrapolation failed!");
1292  continue;
1293  }
1294  if (!gf_state)
1295  {
1296  if (Verbosity() > 1)
1297  LogWarning("Exrapolation failed!");
1298  continue;
1299  }
1300  TVector3 extra_pos(gf_state->getPos());
1301 
1302  const TrkrCluster* cluster(clusterLayer[layerStudy]);
1303  assert(cluster);
1304  TVector3 cluster_pos(cluster->getPosition(0), cluster->getPosition(1), cluster->getPosition(2));
1305 
1306  TVector3 n_dir(cluster->getPosition(0), cluster->getPosition(1), 0);
1307  n_dir.SetMag(1);
1308  TVector3 z_dir(0, 0, 1);
1309  TVector3 azimuth_dir(z_dir.Cross(n_dir));
1310  TVector3 pos_diff = cluster_pos - extra_pos;
1311  const double n_residual = pos_diff.Dot(n_dir);
1312  const double z_residual = pos_diff.Dot(z_dir);
1313  const double azimuth_residual = pos_diff.Dot(azimuth_dir);
1314 
1315  if (Verbosity() >= 1)
1316  {
1317  cout << __PRETTY_FUNCTION__;
1318  cout << " - layer " << layerStudy << " at index " << indexStudy;
1319  cout << " cluster @ " << cluster_pos.x() << ", " << cluster_pos.y() << ", " << cluster_pos.z();
1320  cout << " extrapolate to " << extra_pos.x() << ", " << extra_pos.y() << ", " << extra_pos.z();
1321  cout << ", n_residual = " << n_residual;
1322  cout << ", z_residual = " << z_residual;
1323  cout << ", azimuth_residual = " << azimuth_residual;
1324  cout << endl;
1325 
1326  cout << "Cluster data which provide much more detailed information on the raw signals: "<<endl;
1327 
1328  // in case TpcPrototypeCluster specific functionality is needed, first to a conversion and check
1329  const TpcPrototypeCluster * prototype_cluster = dynamic_cast<const TpcPrototypeCluster *>(cluster);
1330  assert(prototype_cluster);
1331 
1332  prototype_cluster->identify();
1333  }
1334  assert(abs(n_residual) < 1e-4); //same layer check
1335 
1336  (track->clusterKey)[layerStudy] = cluster->getClusKey();
1337  (track->clusterlayer)[layerStudy] = layerStudy;
1338  (track->clusterid)[layerStudy] = TrkrDefs::getClusIndex(cluster->getClusKey());
1339 
1340  (track->clusterX)[layerStudy] = cluster->getPosition(0);
1341  (track->clusterY)[layerStudy] = cluster->getPosition(1);
1342  (track->clusterZ)[layerStudy] = cluster->getPosition(2);
1343  (track->clusterE)[layerStudy] = cluster->getAdc();
1344  (track->clusterSizePhi)[layerStudy] = cluster->getPhiSize();
1345  (track->clusterResidualPhi)[layerStudy] = azimuth_residual;
1346  (track->clusterProjectionPhi)[layerStudy] = extra_pos.Phi();
1347  (track->clusterResidualZ)[layerStudy] = z_residual;
1348  } //propagate to state
1349 
1350  } // refit by "excluding" each cluster for (int layer = 0; layer < track->nLayer; ++layer)
1351 
1352  return track;
1353 }
1354 
1355 /*
1356  * Make SvtxTrack from PHGenFit::Track and SvtxTrack
1357  */
1358 //SvtxTrack* TpcPrototypeGenFitTrkFitter::MakeSvtxTrack(const SvtxTrack* svtx_track,
1359 std::shared_ptr<SvtxTrack> TpcPrototypeGenFitTrkFitter::MakeSvtxTrack(const SvtxTrack* svtx_track,
1360  const std::shared_ptr<PHGenFit::Track>& phgf_track, const SvtxVertex* vertex)
1361 {
1362  double chi2 = phgf_track->get_chi2();
1363  double ndf = phgf_track->get_ndf();
1364 
1365  TVector3 vertex_position(0, 0, 0);
1366  TMatrixF vertex_cov(3, 3);
1367  double dvr2 = 0;
1368  double dvz2 = 0;
1369 
1370  // if (_use_truth_vertex)
1371  // {
1372  // PHG4VtxPoint* first_point = _truth_container->GetPrimaryVtx(_truth_container->GetPrimaryVertexIndex());
1373  // vertex_position.SetXYZ(first_point->get_x(), first_point->get_y(), first_point->get_z());
1374  // if (Verbosity() > 1)
1375  // {
1376  // cout << PHWHERE << "Using: truth vertex: {" << vertex_position.X() << ", " << vertex_position.Y() << ", " << vertex_position.Z() << "} " << endl;
1377  // }
1378  // }
1379  // else if (vertex)
1380  // {
1381  // vertex_position.SetXYZ(vertex->get_x(), vertex->get_y(),
1382  // vertex->get_z());
1383  // dvr2 = vertex->get_error(0, 0) + vertex->get_error(1, 1);
1384  // dvz2 = vertex->get_error(2, 2);
1385  //
1386  // for (int i = 0; i < 3; i++)
1387  // for (int j = 0; j < 3; j++)
1388  // vertex_cov[i][j] = vertex->get_error(i, j);
1389  // }
1390 
1391  //genfit::MeasuredStateOnPlane* gf_state_beam_line_ca = NULL;
1392  std::shared_ptr<genfit::MeasuredStateOnPlane> gf_state_beam_line_ca = NULL;
1393  try
1394  {
1395  gf_state_beam_line_ca = std::shared_ptr<genfit::MeasuredStateOnPlane>(phgf_track->extrapolateToLine(vertex_position,
1396  TVector3(0., 0., 1.)));
1397  }
1398  catch (...)
1399  {
1400  if (Verbosity() >= 2)
1401  LogWarning("extrapolateToLine failed!");
1402  }
1403  if (!gf_state_beam_line_ca) return NULL;
1404 
1412  double u = gf_state_beam_line_ca->getState()[3];
1413  double v = gf_state_beam_line_ca->getState()[4];
1414 
1415  double du2 = gf_state_beam_line_ca->getCov()[3][3];
1416  double dv2 = gf_state_beam_line_ca->getCov()[4][4];
1417  //cout << PHWHERE << " u " << u << " v " << v << " du2 " << du2 << " dv2 " << dv2 << " dvr2 " << dvr2 << endl;
1418  //delete gf_state_beam_line_ca;
1419 
1420  //const SvtxTrack_v1* temp_track = static_cast<const SvtxTrack_v1*> (svtx_track);
1421  // SvtxTrack_v1* out_track = new SvtxTrack_v1(
1422  // *static_cast<const SvtxTrack_v1*>(svtx_track));
1423  std::shared_ptr<SvtxTrack_v1> out_track = std::shared_ptr<SvtxTrack_v1>(new SvtxTrack_v1(*static_cast<const SvtxTrack_v1*>(svtx_track)));
1424 
1425  out_track->set_dca2d(u);
1426  out_track->set_dca2d_error(sqrt(du2 + dvr2));
1427 
1428  std::shared_ptr<genfit::MeasuredStateOnPlane> gf_state_vertex_ca = NULL;
1429  try
1430  {
1431  gf_state_vertex_ca = std::shared_ptr<genfit::MeasuredStateOnPlane>(phgf_track->extrapolateToPoint(vertex_position));
1432  }
1433  catch (...)
1434  {
1435  if (Verbosity() >= 2)
1436  LogWarning("extrapolateToPoint failed!");
1437  }
1438  if (!gf_state_vertex_ca)
1439  {
1440  //delete out_track;
1441  return NULL;
1442  }
1443 
1444  TVector3 mom = gf_state_vertex_ca->getMom();
1445  TVector3 pos = gf_state_vertex_ca->getPos();
1446  TMatrixDSym cov = gf_state_vertex_ca->get6DCov();
1447 
1448  // genfit::MeasuredStateOnPlane* gf_state_vertex_ca =
1449  // phgf_track->extrapolateToLine(vertex_position,
1450  // TVector3(0., 0., 1.));
1451 
1452  u = gf_state_vertex_ca->getState()[3];
1453  v = gf_state_vertex_ca->getState()[4];
1454 
1455  du2 = gf_state_vertex_ca->getCov()[3][3];
1456  dv2 = gf_state_vertex_ca->getCov()[4][4];
1457 
1458  double dca3d = sqrt(u * u + v * v);
1459  double dca3d_error = sqrt(du2 + dv2 + dvr2 + dvz2);
1460 
1461  out_track->set_dca(dca3d);
1462  out_track->set_dca_error(dca3d_error);
1463 
1464  //
1465  // in: X, Y, Z; out; r: n X Z, Z X r, Z
1466 
1467  float dca3d_xy = NAN;
1468  float dca3d_z = NAN;
1469  float dca3d_xy_error = NAN;
1470  float dca3d_z_error = NAN;
1471 
1472  try
1473  {
1474  TMatrixF pos_in(3, 1);
1475  TMatrixF cov_in(3, 3);
1476  TMatrixF pos_out(3, 1);
1477  TMatrixF cov_out(3, 3);
1478 
1479  TVectorD state6(6); // pos(3), mom(3)
1480  TMatrixDSym cov6(6, 6); //
1481 
1482  gf_state_vertex_ca->get6DStateCov(state6, cov6);
1483 
1484  TVector3 vn(state6[3], state6[4], state6[5]);
1485 
1486  // mean of two multivariate gaussians Pos - Vertex
1487  pos_in[0][0] = state6[0] - vertex_position.X();
1488  pos_in[1][0] = state6[1] - vertex_position.Y();
1489  pos_in[2][0] = state6[2] - vertex_position.Z();
1490 
1491  for (int i = 0; i < 3; ++i)
1492  {
1493  for (int j = 0; j < 3; ++j)
1494  {
1495  cov_in[i][j] = cov6[i][j] + vertex_cov[i][j];
1496  }
1497  }
1498 
1499  // vn is momentum vector, pos_in is position vector (of what?)
1500  pos_cov_XYZ_to_RZ(vn, pos_in, cov_in, pos_out, cov_out);
1501 
1502  if (Verbosity() > 50)
1503  {
1504  cout << " vn.X " << vn.X() << " vn.Y " << vn.Y() << " vn.Z " << vn.Z() << endl;
1505  cout << " pos_in.X " << pos_in[0][0] << " pos_in.Y " << pos_in[1][0] << " pos_in.Z " << pos_in[2][0] << endl;
1506  cout << " pos_out.X " << pos_out[0][0] << " pos_out.Y " << pos_out[1][0] << " pos_out.Z " << pos_out[2][0] << endl;
1507  }
1508 
1509  dca3d_xy = pos_out[0][0];
1510  dca3d_z = pos_out[2][0];
1511  dca3d_xy_error = sqrt(cov_out[0][0]);
1512  dca3d_z_error = sqrt(cov_out[2][2]);
1513  }
1514  catch (...)
1515  {
1516  if (Verbosity() > 0)
1517  LogWarning("DCA calculationfailed!");
1518  }
1519 
1520  out_track->set_dca3d_xy(dca3d_xy);
1521  out_track->set_dca3d_z(dca3d_z);
1522  out_track->set_dca3d_xy_error(dca3d_xy_error);
1523  out_track->set_dca3d_z_error(dca3d_z_error);
1524 
1525  //if(gf_state_vertex_ca) delete gf_state_vertex_ca;
1526 
1527  out_track->set_chisq(chi2);
1528  out_track->set_ndf(ndf);
1529  out_track->set_charge(phgf_track->get_charge());
1530 
1531  out_track->set_px(mom.Px());
1532  out_track->set_py(mom.Py());
1533  out_track->set_pz(mom.Pz());
1534 
1535  out_track->set_x(pos.X());
1536  out_track->set_y(pos.Y());
1537  out_track->set_z(pos.Z());
1538 
1539  for (int i = 0; i < 6; i++)
1540  {
1541  for (int j = i; j < 6; j++)
1542  {
1543  out_track->set_error(i, j, cov[i][j]);
1544  }
1545  }
1546 
1547  const genfit::Track* gftrack = phgf_track->getGenFitTrack();
1548  const genfit::AbsTrackRep* rep = gftrack->getCardinalRep();
1549  for (unsigned int id = 0; id < gftrack->getNumPointsWithMeasurement(); ++id)
1550  {
1551  genfit::TrackPoint* trpoint = gftrack->getPointWithMeasurementAndFitterInfo(id, gftrack->getCardinalRep());
1552 
1553  if (!trpoint)
1554  {
1555  if (Verbosity() > 1)
1556  LogWarning("!trpoint");
1557  continue;
1558  }
1559 
1560  genfit::KalmanFitterInfo* kfi = static_cast<genfit::KalmanFitterInfo*>(trpoint->getFitterInfo(rep));
1561  if (!kfi)
1562  {
1563  if (Verbosity() > 1)
1564  LogWarning("!kfi");
1565  continue;
1566  }
1567 
1568  std::shared_ptr<const genfit::MeasuredStateOnPlane> gf_state = NULL;
1569  try
1570  {
1571  //gf_state = std::shared_ptr <genfit::MeasuredStateOnPlane> (const_cast<genfit::MeasuredStateOnPlane*> (&(kfi->getFittedState(true))));
1572  const genfit::MeasuredStateOnPlane* temp_state = &(kfi->getFittedState(true));
1573  gf_state = std::shared_ptr<genfit::MeasuredStateOnPlane>(new genfit::MeasuredStateOnPlane(*temp_state));
1574  }
1575  catch (...)
1576  {
1577  if (Verbosity() > 1)
1578  LogWarning("Exrapolation failed!");
1579  }
1580  if (!gf_state)
1581  {
1582  if (Verbosity() > 1)
1583  LogWarning("Exrapolation failed!");
1584  continue;
1585  }
1587  float pathlength = -phgf_track->extrapolateToPoint(temp, vertex_position, id);
1588 
1589  std::shared_ptr<SvtxTrackState> state = std::shared_ptr<SvtxTrackState>(new SvtxTrackState_v1(pathlength));
1590  state->set_x(gf_state->getPos().x());
1591  state->set_y(gf_state->getPos().y());
1592  state->set_z(gf_state->getPos().z());
1593 
1594  state->set_px(gf_state->getMom().x());
1595  state->set_py(gf_state->getMom().y());
1596  state->set_pz(gf_state->getMom().z());
1597 
1598  //gf_state->getCov().Print();
1599 
1600  for (int i = 0; i < 6; i++)
1601  {
1602  for (int j = i; j < 6; j++)
1603  {
1604  state->set_error(i, j, gf_state->get6DCov()[i][j]);
1605  }
1606  }
1607 
1608  out_track->insert_state(state.get());
1609  }
1610 
1611  return out_track;
1612 }
1613 
1614 /*
1615  * Fill SvtxVertexMap from GFRaveVertexes and Tracks
1616  */
1618  const std::vector<genfit::GFRaveVertex*>& rave_vertices,
1619  const std::vector<genfit::Track*>& gf_tracks)
1620 {
1622  {
1623  _vertexmap->clear();
1624  }
1625 
1626  for (unsigned int ivtx = 0; ivtx < rave_vertices.size(); ++ivtx)
1627  {
1628  genfit::GFRaveVertex* rave_vtx = rave_vertices[ivtx];
1629 
1630  if (!rave_vtx)
1631  {
1632  cerr << PHWHERE << endl;
1633  return false;
1634  }
1635 
1636  std::shared_ptr<SvtxVertex> svtx_vtx(new SvtxVertex_v1());
1637 
1638  svtx_vtx->set_chisq(rave_vtx->getChi2());
1639  svtx_vtx->set_ndof(rave_vtx->getNdf());
1640  svtx_vtx->set_position(0, rave_vtx->getPos().X());
1641  svtx_vtx->set_position(1, rave_vtx->getPos().Y());
1642  svtx_vtx->set_position(2, rave_vtx->getPos().Z());
1643 
1644  for (int i = 0; i < 3; i++)
1645  for (int j = 0; j < 3; j++)
1646  svtx_vtx->set_error(i, j, rave_vtx->getCov()[i][j]);
1647 
1648  for (unsigned int i = 0; i < rave_vtx->getNTracks(); i++)
1649  {
1650  //TODO Assume id's are sync'ed between _trackmap_refit and gf_tracks, need to change?
1651  const genfit::Track* rave_track =
1652  rave_vtx->getParameters(i)->getTrack();
1653  for (unsigned int j = 0; j < gf_tracks.size(); j++)
1654  {
1655  if (rave_track == gf_tracks[j])
1656  svtx_vtx->insert_track(j);
1657  }
1658  }
1659 
1661  {
1662  if (_vertexmap)
1663  {
1664  _vertexmap->insert_clone(svtx_vtx.get());
1665  }
1666  else
1667  {
1668  LogError("!_vertexmap");
1669  }
1670  }
1671  else
1672  {
1673  if (_vertexmap_refit)
1674  {
1675  _vertexmap_refit->insert_clone(svtx_vtx.get());
1676  }
1677  else
1678  {
1679  LogError("!_vertexmap_refit");
1680  }
1681  }
1682 
1683  // if (Verbosity() >= 2) {
1684  // cout << PHWHERE << endl;
1685  // svtx_vtx->Print();
1686  // _vertexmap_refit->Print();
1687  // }
1688 
1689  //delete svtx_vtx;
1690  } //loop over RAVE vertices
1691 
1692  return true;
1693 }
1694 
1695 bool TpcPrototypeGenFitTrkFitter::pos_cov_uvn_to_rz(const TVector3& u, const TVector3& v,
1696  const TVector3& n, const TMatrixF& pos_in, const TMatrixF& cov_in,
1697  TMatrixF& pos_out, TMatrixF& cov_out) const
1698 {
1699  if (pos_in.GetNcols() != 1 || pos_in.GetNrows() != 3)
1700  {
1701  if (Verbosity() > 0) LogWarning("pos_in.GetNcols() != 1 || pos_in.GetNrows() != 3");
1702  return false;
1703  }
1704 
1705  if (cov_in.GetNcols() != 3 || cov_in.GetNrows() != 3)
1706  {
1707  if (Verbosity() > 0) LogWarning("cov_in.GetNcols() != 3 || cov_in.GetNrows() != 3");
1708  return false;
1709  }
1710 
1711  TVector3 Z_uvn(u.Z(), v.Z(), n.Z());
1712  TVector3 up_uvn = TVector3(0., 0., 1.).Cross(Z_uvn); // n_uvn X Z_uvn
1713 
1714  if (up_uvn.Mag() < 0.00001)
1715  {
1716  if (Verbosity() > 0) LogWarning("n is parallel to z");
1717  return false;
1718  }
1719 
1720  // R: rotation from u,v,n to n X Z, nX(nXZ), n
1721  TMatrixF R(3, 3);
1722  TMatrixF R_T(3, 3);
1723 
1724  try
1725  {
1726  // rotate u along z to up
1727  float phi = -atan2(up_uvn.Y(), up_uvn.X());
1728  R[0][0] = cos(phi);
1729  R[0][1] = -sin(phi);
1730  R[0][2] = 0;
1731  R[1][0] = sin(phi);
1732  R[1][1] = cos(phi);
1733  R[1][2] = 0;
1734  R[2][0] = 0;
1735  R[2][1] = 0;
1736  R[2][2] = 1;
1737 
1738  R_T.Transpose(R);
1739  }
1740  catch (...)
1741  {
1742  if (Verbosity() > 0)
1743  LogWarning("Can't get rotation matrix");
1744 
1745  return false;
1746  }
1747 
1748  pos_out.ResizeTo(3, 1);
1749  cov_out.ResizeTo(3, 3);
1750 
1751  pos_out = R * pos_in;
1752  cov_out = R * cov_in * R_T;
1753 
1754  return true;
1755 }
1756 
1758  const TVector3& v, const TVector3& n, const TMatrixF& cov_in,
1759  TMatrixF& cov_out) const
1760 {
1766  TMatrixF R = get_rotation_matrix(u, v, n);
1767  //
1768  // LogDebug("TpcPrototypeGenFitTrkFitter::get_vertex_error_uvn::R = ");
1769  // R.Print();
1770  // cout<<"R.Determinant() = "<<R.Determinant()<<"\n";
1771 
1772  if (!(abs(R.Determinant() - 1) < 0.01))
1773  {
1774  if (Verbosity() > 0)
1775  LogWarning("!(abs(R.Determinant()-1)<0.0001)");
1776  return false;
1777  }
1778 
1779  if (R.GetNcols() != 3 || R.GetNrows() != 3)
1780  {
1781  if (Verbosity() > 0)
1782  LogWarning("R.GetNcols() != 3 || R.GetNrows() != 3");
1783  return false;
1784  }
1785 
1786  if (cov_in.GetNcols() != 3 || cov_in.GetNrows() != 3)
1787  {
1788  if (Verbosity() > 0)
1789  LogWarning("cov_in.GetNcols() != 3 || cov_in.GetNrows() != 3");
1790  return false;
1791  }
1792 
1793  TMatrixF R_T(3, 3);
1794 
1795  R_T.Transpose(R);
1796 
1797  cov_out.ResizeTo(3, 3);
1798 
1799  cov_out = R * cov_in * R_T;
1800 
1801  return true;
1802 }
1803 
1805  const TVector3& n, const TMatrixF& pos_in, const TMatrixF& cov_in,
1806  TMatrixF& pos_out, TMatrixF& cov_out) const
1807 {
1808  if (pos_in.GetNcols() != 1 || pos_in.GetNrows() != 3)
1809  {
1810  if (Verbosity() > 0) LogWarning("pos_in.GetNcols() != 1 || pos_in.GetNrows() != 3");
1811  return false;
1812  }
1813 
1814  if (cov_in.GetNcols() != 3 || cov_in.GetNrows() != 3)
1815  {
1816  if (Verbosity() > 0) LogWarning("cov_in.GetNcols() != 3 || cov_in.GetNrows() != 3");
1817  return false;
1818  }
1819 
1820  // produces a vector perpendicular to both the momentum vector and beam line - i.e. in the direction of the dca_xy
1821  // only the angle of r will be used, not the magnitude
1822  TVector3 r = n.Cross(TVector3(0., 0., 1.));
1823  if (r.Mag() < 0.00001)
1824  {
1825  if (Verbosity() > 0) LogWarning("n is parallel to z");
1826  return false;
1827  }
1828 
1829  // R: rotation from u,v,n to n X Z, nX(nXZ), n
1830  TMatrixF R(3, 3);
1831  TMatrixF R_T(3, 3);
1832 
1833  try
1834  {
1835  // rotate u along z to up
1836  float phi = -atan2(r.Y(), r.X());
1837  R[0][0] = cos(phi);
1838  R[0][1] = -sin(phi);
1839  R[0][2] = 0;
1840  R[1][0] = sin(phi);
1841  R[1][1] = cos(phi);
1842  R[1][2] = 0;
1843  R[2][0] = 0;
1844  R[2][1] = 0;
1845  R[2][2] = 1;
1846 
1847  R_T.Transpose(R);
1848  }
1849  catch (...)
1850  {
1851  if (Verbosity() > 0)
1852  LogWarning("Can't get rotation matrix");
1853 
1854  return false;
1855  }
1856 
1857  pos_out.ResizeTo(3, 1);
1858  cov_out.ResizeTo(3, 3);
1859 
1860  pos_out = R * pos_in;
1861  cov_out = R * cov_in * R_T;
1862 
1863  return true;
1864 }
1865 
1871  const TVector3 y, const TVector3 z, const TVector3 xp, const TVector3 yp,
1872  const TVector3 zp) const
1873 {
1874  TMatrixF R(3, 3);
1875 
1876  TVector3 xu = x.Unit();
1877  TVector3 yu = y.Unit();
1878  TVector3 zu = z.Unit();
1879 
1880  const float max_diff = 0.01;
1881 
1882  if (!(
1883  abs(xu * yu) < max_diff and
1884  abs(xu * zu) < max_diff and
1885  abs(yu * zu) < max_diff))
1886  {
1887  if (Verbosity() > 0)
1888  LogWarning("input frame error!");
1889  return R;
1890  }
1891 
1892  TVector3 xpu = xp.Unit();
1893  TVector3 ypu = yp.Unit();
1894  TVector3 zpu = zp.Unit();
1895 
1896  if (!(
1897  abs(xpu * ypu) < max_diff and
1898  abs(xpu * zpu) < max_diff and
1899  abs(ypu * zpu) < max_diff))
1900  {
1901  if (Verbosity() > 0)
1902  LogWarning("output frame error!");
1903  return R;
1904  }
1905 
1911  TVector3 u(xpu.Dot(xu), xpu.Dot(yu), xpu.Dot(zu));
1912  TVector3 v(ypu.Dot(xu), ypu.Dot(yu), ypu.Dot(zu));
1913  TVector3 n(zpu.Dot(xu), zpu.Dot(yu), zpu.Dot(zu));
1914 
1915  try
1916  {
1917  std::shared_ptr<TRotation> rotation(new TRotation());
1918  //TRotation *rotation = new TRotation();
1919 
1921  rotation->RotateAxes(u, v, n);
1922 
1923  R[0][0] = rotation->XX();
1924  R[0][1] = rotation->XY();
1925  R[0][2] = rotation->XZ();
1926  R[1][0] = rotation->YX();
1927  R[1][1] = rotation->YY();
1928  R[1][2] = rotation->YZ();
1929  R[2][0] = rotation->ZX();
1930  R[2][1] = rotation->ZY();
1931  R[2][2] = rotation->ZZ();
1932  //
1933  // LogDebug("TpcPrototypeGenFitTrkFitter::get_rotation_matrix: TRotation:");
1934  // R.Print();
1935  // cout<<"R.Determinant() = "<<R.Determinant()<<"\n";
1936 
1937  //delete rotation;
1938 
1939  // TMatrixF ROT1(3, 3);
1940  // TMatrixF ROT2(3, 3);
1941  // TMatrixF ROT3(3, 3);
1942  //
1943  // // rotate n along z to xz plane
1944  // float phi = -atan2(n.Y(), n.X());
1945  // ROT1[0][0] = cos(phi);
1946  // ROT1[0][1] = -sin(phi);
1947  // ROT1[0][2] = 0;
1948  // ROT1[1][0] = sin(phi);
1949  // ROT1[1][1] = cos(phi);
1950  // ROT1[1][2] = 0;
1951  // ROT1[2][0] = 0;
1952  // ROT1[2][1] = 0;
1953  // ROT1[2][2] = 1;
1954  //
1955  // // rotate n along y to z
1956  // TVector3 n1(n);
1957  // n1.RotateZ(phi);
1958  // float theta = -atan2(n1.X(), n1.Z());
1959  // ROT2[0][0] = cos(theta);
1960  // ROT2[0][1] = 0;
1961  // ROT2[0][2] = sin(theta);
1962  // ROT2[1][0] = 0;
1963  // ROT2[1][1] = 1;
1964  // ROT2[1][2] = 0;
1965  // ROT2[2][0] = -sin(theta);
1966  // ROT2[2][1] = 0;
1967  // ROT2[2][2] = cos(theta);
1968  //
1969  // // rotate u along z to x
1970  // TVector3 u2(u);
1971  // u2.RotateZ(phi);
1972  // u2.RotateY(theta);
1973  // float phip = -atan2(u2.Y(), u2.X());
1974  // ROT3[0][0] = cos(phip);
1975  // ROT3[0][1] = -sin(phip);
1976  // ROT3[0][2] = 0;
1977  // ROT3[1][0] = sin(phip);
1978  // ROT3[1][1] = cos(phip);
1979  // ROT3[1][2] = 0;
1980  // ROT3[2][0] = 0;
1981  // ROT3[2][1] = 0;
1982  // ROT3[2][2] = 1;
1983  //
1984  // // R: rotation from u,v,n to (z X n), v', z
1985  // R = ROT3 * ROT2 * ROT1;
1986  //
1987  // R.Invert();
1988  // LogDebug("TpcPrototypeGenFitTrkFitter::get_rotation_matrix: Home Brew:");
1989  // R.Print();
1990  // cout<<"R.Determinant() = "<<R.Determinant()<<"\n";
1991  }
1992  catch (...)
1993  {
1994  if (Verbosity() > 0)
1995  LogWarning("Can't get rotation matrix");
1996 
1997  return R;
1998  }
1999 
2000  return R;
2001 }