EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHTpcTracker.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHTpcTracker.cc
1 
7 #include "PHTpcTracker.h"
8 
9 #include "PHTpcEventExporter.h"
10 #include "PHTpcLookup.h"
11 #include "PHTpcSeedFinder.h"
12 #include "PHTpcTrackFollower.h"
13 #include "PHTpcVertexFinder.h"
14 
15 #include "Fitter.h"
16 #include "Track.h" // for Track
17 #include "externals/kdfinder.hpp"
18 
19 #include <phfield/PHField.h>
20 #include <phfield/PHFieldConfigv2.h>
21 #include <phfield/PHFieldUtility.h>
22 
23 #include <phgeom/PHGeomUtility.h>
24 
27 #include <trackbase/TrkrDefs.h> // for cluskey
28 
31 
33 
35 
36 #include <phool/PHLog.h>
37 #include <phool/getClass.h>
38 
39 #include <log4cpp/CategoryStream.hh> // for CategoryStream
40 
41 #include <CLHEP/Units/SystemOfUnits.h>
42 
43 #include <TMatrixDSymfwd.h> // for TMatrixDSym
44 #include <TMatrixTSym.h> // for TMatrixTSym
45 #include <TMatrixTUtils.h> // for TMatrixTRow
46 #include <TVector3.h> // for TVector3
47 #include <TVectorDfwd.h> // for TVectorD
48 #include <TVectorT.h> // for TVectorT
49 
50 #include <iostream> // for operator<<, endl, basic...
51 #include <limits> // for numeric_limits
52 #include <memory> // for shared_ptr, __shared_ptr
53 #include <vector> // for vector
54 
55 class PHCompositeNode;
56 
57 using namespace std;
58 
59 PHTpcTracker::PHTpcTracker(const std::string& name)
60  : PHTrackSeeding(name)
61  ,
62  // mSeedFinder(nullptr), mTrackFollower(nullptr),
63  // mVertexFinder(nullptr), mEventExporter(nullptr), mLookup(nullptr),
64  mFitter(nullptr)
65  , mTGeoManager(nullptr)
66  , mField(nullptr)
67  , mB(1.4)
68  , mEnableVertexing(false)
69  , mEnableJsonExport(false)
70 {
71  // if ( !mSeedFinder ) {
73  // }
74  // if ( !mTrackFollower ) {
76  // }
77  // if ( !mVertexFinder ) {
79  // }
80  // if ( !mEventExporter ) {
82  // }
83  // if ( !mLookup ) {
84  mLookup = new PHTpcLookup();
85  // }
86 }
87 
89 {
90  delete mSeedFinder;
91  delete mTrackFollower;
92  delete mVertexFinder;
93  delete mEventExporter;
94  delete mLookup;
95  delete mFitter;
96  delete mField;
97 }
98 
100 {
101  int ret = PHTrackSeeding::Setup(topNode);
102  if (ret != Fun4AllReturnCodes::EVENT_OK) return ret;
103 
105 }
106 
108 {
109  LOG_INFO("tracking.PHTpcTracker.process_event") << "---- process event started -----";
110 
111  // ----- magnetic field -----
112  if (!mField)
113  {
114  mField = getMagField(topNode, mB);
115  }
116 
117  // ----- Setup Geometry and Fitter -----
118  // FIXME: get TGeoManager only once per file?
119  if (!mTGeoManager)
120  {
122  LOG_ERROR_IF("tracking.PHTpcTracker.process_event", !mTGeoManager) << "Cannot find TGeoManager, track propagation will fail";
123  }
124 
125  if (!mFitter && mField && mTGeoManager)
126  {
128  }
129 
130  // ----- Seed finding -----
131  //TrkrClusterContainer* cluster_map = findNode::getClass<TrkrClusterContainer>(topNode, "TRKR_CLUSTER");
132  std::vector<kdfinder::TrackCandidate<double>*> candidates;
133  candidates = mSeedFinder->findSeeds(_cluster_map, _hitsets, mB);
134  LOG_INFO("tracking.PHTpcTracker.process_event") << "SeedFinder produced " << candidates.size() << " track seeds";
135 
136  // ----- Track Following -----
138  std::vector<PHGenFit2::Track*> gtracks;
139  gtracks = mTrackFollower->followTracks(candidates, mField, mLookup, mFitter);
140  LOG_INFO("tracking.PHTpcTracker.process_event") << "TrackFollower reconstructed " << gtracks.size() << " tracks";
141  // write tracks to fun4all server
142  // cout<< gtracks[1]->get_vertex_id() << endl;
143  for (int i = 0, ilen = gtracks.size(); i < ilen; i++)
144  {
145  // for (auto it = gtracks.begin(); it != gtracks.end(); ++it)
146  std::shared_ptr<SvtxTrack_v2> svtx_track(new SvtxTrack_v2());
148 
149  svtx_track->Reset();
150  svtx_track->set_id(1);
151  // cout << gtracks[i]->get_vertex_id() << endl;
152  TVectorD state = gtracks[i]->getGenFitTrack()->getStateSeed();
153  TVector3 pos(state(0), state(1), state(2));
154  TVector3 mom(state(3), state(4), state(5));
155  TMatrixDSym cov = gtracks[i]->getGenFitTrack()->getCovSeed();
156  //cout<< "pt: " << pos.Perp() << endl;
157 
158  double charge = gtracks[i]->get_charge();
159 
160  svtx_track->set_charge(charge);
161 
162  for (int k = 0; k < 6; k++)
163  {
164  for (int j = 0; j < 6; j++)
165  {
166  svtx_track->set_error(k, j, cov[k][j]);
167  }
168  }
169 
170  svtx_track->set_px(mom.Px());
171  svtx_track->set_py(mom.Py());
172  svtx_track->set_pz(mom.Pz());
173 
174  svtx_track->set_x(pos.X());
175  svtx_track->set_y(pos.Y());
176  svtx_track->set_z(pos.Z());
177 
178  for (TrkrDefs::cluskey cluster_key : gtracks[i]->get_cluster_keys())
179  {
180  svtx_track->insert_cluster_key(cluster_key);
181  }
182 
183  if(Verbosity() > 0)
184  {
185  std::cout << PHWHERE << " track identify: " << std::endl;
186  svtx_track->identify();
187  }
188  _track_map->insert(svtx_track.get());
189  }
190 
191  // ----- cleanup -----
192 
193  // candidates cleanup
194  for (auto it = candidates.begin(); it != candidates.end(); ++it)
195  {
196  delete (*it);
197  }
198 
199  // genfit tracks cleanup
200  for (auto it = gtracks.begin(); it != gtracks.end(); ++it)
201  {
202  delete (*it);
203  }
204 
205  // hit lookup cleanup
206  mLookup->clear();
207 
208  LOG_INFO("tracking.PHTpcTracker.process_event") << "---- process event finished -----";
209 
211 }
212 
214 {
215  PHField* field = nullptr;
216  PHFieldConfigv2 bconfig(0, 0, B);
217  // ----- check file -----
218  PHField* field_file = PHFieldUtility::GetFieldMapNode(nullptr, topNode);
219  if (field_file)
220  {
221  const double point[] = {0, 0, 0, 0}; // x,y,z,t
222  double Bx = 0, By = 0, Bz = 0;
223  double Bfield[] = {std::numeric_limits<double>::signaling_NaN(),
224  std::numeric_limits<double>::signaling_NaN(),
225  std::numeric_limits<double>::signaling_NaN(),
226  std::numeric_limits<double>::signaling_NaN(),
227  std::numeric_limits<double>::signaling_NaN(),
228  std::numeric_limits<double>::signaling_NaN()};
229  field_file->GetFieldValue(point, Bfield);
230  Bx = Bfield[0];
231  By = Bfield[1];
232  Bz = Bfield[2];
233  B = Bz / CLHEP::tesla;
234  LOG_DEBUG("tracking.PHTpcTracker.process_event") << "Importing B field from file, Bx,By,Bz Tesla = " << Bx / CLHEP::tesla << "," << By / CLHEP::tesla << "," << Bz / CLHEP::tesla;
235  bconfig.set_field_mag_z(B);
236  }
237  else
238  {
239  LOG_WARN("tracking.PHTpcTracker.process_event") << "No field found in file, using default Bz value = " << B << " Tesla";
240  }
241  field = PHFieldUtility::BuildFieldMap(&bconfig, 1);
242  return field;
243 }
244 
245 void PHTpcTracker::set_seed_finder_options(double maxdistance1, double tripletangle1, size_t minhits1,
246  double maxdistance2, double tripletangle2, size_t minhits2, size_t nthreads)
247 {
248  mSeedFinder->set_options(maxdistance1, tripletangle1, minhits1,
249  maxdistance2, tripletangle2, minhits2, nthreads);
250 }
251 
252 void PHTpcTracker::set_seed_finder_optimization_remove_loopers(bool opt, double minr, double maxr)
253 {
255 }
256 
258 {
260 }
261 
263 {
265 }