Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ActsAlignmentStates.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ActsAlignmentStates.cc
1 #include "ActsAlignmentStates.h"
2 #include "ActsPropagator.h"
3 
5 #include <phool/PHDataNode.h>
6 #include <phool/PHNode.h>
7 #include <phool/PHNodeIterator.h>
8 #include <phool/PHObject.h>
9 #include <phool/getClass.h>
10 #include <phool/phool.h>
11 
12 #include <trackbase/ActsGeometry.h>
14 #include <trackbase/TpcDefs.h>
15 #include <trackbase/TrkrCluster.h>
17 #include <trackbase/TrkrDefs.h>
18 
23 
29 
30 namespace
31 {
32  template <class T>
33  inline constexpr T square(const T& x)
34  {
35  return x * x;
36  }
37  template <class T>
38  inline constexpr T get_r(const T& x, const T& y)
39  {
40  return std::sqrt(square(x) + square(y));
41  }
42 } // namespace
43 
45  const std::vector<Acts::MultiTrajectoryTraits::IndexType>& tips,
46  SvtxTrack* track,
48 {
49  const auto& mj = tracks.trackStateContainer();
50  const auto& trackTip = tips.front();
51  const auto crossing = track->get_silicon_seed()->get_crossing();
52 
53  ActsPropagator propagator(m_tGeometry);
54  if (m_fieldMap.find(".root") == std::string::npos)
55  {
56  propagator.constField();
57  propagator.setConstFieldValue(std::stod(m_fieldMap));
58  }
59 
61  if (m_verbosity > 2)
62  {
63  std::cout << "Beginning alignment state creation for track "
64  << track->get_id() << std::endl;
65  }
66 
68 
69  auto silseed = track->get_silicon_seed();
70  int nmaps = 0;
71  int nintt = 0;
72  for (auto iter = silseed->begin_cluster_keys();
73  iter != silseed->end_cluster_keys();
74  ++iter)
75  {
76  TrkrDefs::cluskey ckey = *iter;
78  {
79  nmaps++;
80  }
82  {
83  nintt++;
84  }
85  }
86 
87  if (nmaps < 2 or nintt < 2)
88  {
89  return;
90  }
91 
93  SvtxTrackState* firststate = (*std::next(track->begin_states(), 1)).second;
94  if (get_r(firststate->get_x(), firststate->get_y()) > 5.)
95  {
96  return;
97  }
98 
99  mj.visitBackwards(trackTip, [&](const auto& state)
100  {
102  if (not state.hasSmoothed() or
103  not state.typeFlags().test(Acts::TrackStateFlag::MeasurementFlag))
104  {
105  return true;
106  }
107 
108  const auto& surface = state.referenceSurface();
109  auto sl = state.getUncalibratedSourceLink().template get<ActsSourceLink>();
110  auto ckey = sl.cluskey();
111  Acts::Vector2 localMeas = Acts::Vector2::Zero();
113  std::visit([&](const auto& meas) {
114  localMeas(0) = meas.parameters()[0];
115  localMeas(1) = meas.parameters()[1];
116  }, measurements[sl.index()]);
117 
118  if (m_verbosity > 2)
119  {
120  std::cout << "sl index and ckey " << sl.index() << ", "
121  << sl.cluskey() << " with local position "
122  << localMeas.transpose() << std::endl;
123  }
124 
125  auto clus = m_clusterMap->findCluster(ckey);
126  const auto trkrId = TrkrDefs::getTrkrId(ckey);
127 
128  const Acts::Vector2 localState = state.effectiveProjector() * state.smoothed();
130  const Acts::Vector2 localResidual = localMeas - localState;
131 
132  Acts::Vector3 clusGlobal = m_tGeometry->getGlobalPosition(ckey, clus);
133  if (trkrId == TrkrDefs::tpcId)
134  {
135  makeTpcGlobalCorrections(ckey, crossing, clusGlobal);
136  }
137 
139  clusGlobal *= Acts::UnitConstants::cm;
140 
142  Acts::Vector3 stateGlobal = globalStateParams.segment<3>(Acts::eFreePos0);
143 
144  Acts::Vector3 clus_sigma(0, 0, 0);
145 
146  clus_sigma(2) = clus->getZError() * Acts::UnitConstants::cm;
147  clus_sigma(0) = clus->getRPhiError() / sqrt(2) * Acts::UnitConstants::cm;
148  clus_sigma(1) = clus->getRPhiError() / sqrt(2) * Acts::UnitConstants::cm;
149 
150  if (m_verbosity > 2)
151  {
152  std::cout << "clus global is " << clusGlobal.transpose() << std::endl
153  << "state global is " << stateGlobal.transpose() << std::endl;
154  std::cout << " clus errors are " << clus_sigma.transpose() << std::endl;
155  std::cout << " clus loc is " << localMeas.transpose() << std::endl;
156  std::cout << " state loc is " << localState << std::endl;
157  std::cout << " local residual is " << localResidual.transpose() << std::endl;
158  }
159 
160  // Get the derivative of alignment (global) parameters w.r.t. measurement or residual
162  double phi = state.smoothed()[Acts::eBoundPhi];
163  double theta = state.smoothed()[Acts::eBoundTheta];
164 
165  Acts::Vector3 tangent = Acts::makeDirectionFromPhiTheta(phi,theta);
167  tangent *= -1;
168 
169  if(m_verbosity > 2)
170  {
171  std::cout << "tangent vector to track state is " << tangent.transpose() << std::endl;
172  }
173 
174  std::pair<Acts::Vector3, Acts::Vector3> projxy =
175  get_projectionXY(surface, tangent);
176 
177  Acts::Vector3 sensorCenter = surface.center(m_tGeometry->geometry().getGeoContext());
178  Acts::Vector3 OM = stateGlobal - sensorCenter;
179 
180  auto globDeriv = makeGlobalDerivatives(OM, projxy);
181 
182  if(m_verbosity > 2)
183  {
184  std::cout << " global deriv calcs" << std::endl
185  << "stateGlobal: " << stateGlobal.transpose()
186  << ", sensor center " << sensorCenter.transpose() << std::endl
187  << ", OM " << OM.transpose() << std::endl << " projxy "
188  << projxy.first.transpose() << ", "
189  << projxy.second.transpose() << std::endl
190  << "global derivatives " << std::endl << globDeriv << std::endl;
191  }
192 
195  auto localDeriv = state.effectiveProjector() * state.jacobian();
196  if(m_verbosity > 2)
197  {
198  std::cout << "local deriv " << std::endl << localDeriv << std::endl;
199  }
200 
201  auto svtxstate = std::make_unique<SvtxAlignmentState_v1>();
202 
203  svtxstate->set_residual(localResidual);
204  svtxstate->set_local_derivative_matrix(localDeriv);
205  svtxstate->set_global_derivative_matrix(globDeriv);
206  svtxstate->set_cluster_key(ckey);
207 
208  statevec.push_back(svtxstate.release());
209  return true; });
210 
211  if (m_verbosity > 2)
212  {
213  std::cout << "Inserting track " << track->get_id() << " with nstates "
214  << statevec.size() << std::endl;
215  }
216 
217  m_alignmentStateMap->insertWithKey(track->get_id(), statevec);
218 
219  return;
220 }
221 
224  const std::pair<Acts::Vector3, Acts::Vector3>& projxy)
225 {
226  SvtxAlignmentState::GlobalMatrix globalder = SvtxAlignmentState::GlobalMatrix::Zero();
227  Acts::SquareMatrix3 unit = Acts::SquareMatrix3::Identity();
228 
230  globalder(0, 0) = ((unit.col(0)).cross(OM)).dot(projxy.first);
231  globalder(0, 1) = ((unit.col(1)).cross(OM)).dot(projxy.first);
232  globalder(0, 2) = ((unit.col(2)).cross(OM)).dot(projxy.first);
234  globalder(0, 3) = unit.col(0).dot(projxy.first);
235  globalder(0, 4) = unit.col(1).dot(projxy.first);
236  globalder(0, 5) = unit.col(2).dot(projxy.first);
237 
239  globalder(1, 0) = ((unit.col(0)).cross(OM)).dot(projxy.second);
240  globalder(1, 1) = ((unit.col(1)).cross(OM)).dot(projxy.second);
241  globalder(1, 2) = ((unit.col(2)).cross(OM)).dot(projxy.second);
243  globalder(1, 3) = unit.col(0).dot(projxy.second);
244  globalder(1, 4) = unit.col(1).dot(projxy.second);
245  globalder(1, 5) = unit.col(2).dot(projxy.second);
246 
247  return globalder;
248 }
249 
250 std::pair<Acts::Vector3, Acts::Vector3> ActsAlignmentStates::get_projectionXY(const Acts::Surface& surface, const Acts::Vector3& tangent)
251 {
252  Acts::Vector3 projx = Acts::Vector3::Zero();
253  Acts::Vector3 projy = Acts::Vector3::Zero();
254 
255  // get the plane of the surface
256  Acts::Vector3 sensorCenter = surface.center(m_tGeometry->geometry().getGeoContext());
257  // sensorNormal is the Z vector
259 
260  // get surface X and Y unit vectors in global frame
261  // transform Xlocal = 1.0 to global, subtract the surface center, normalize to 1
262  Acts::Vector3 xloc(1.0, 0.0, 0.0);
263  Acts::Vector3 xglob = surface.transform(m_tGeometry->geometry().getGeoContext()) * xloc;
264 
265  Acts::Vector3 yloc(0.0, 1.0, 0.0);
266  Acts::Vector3 yglob = surface.transform(m_tGeometry->geometry().getGeoContext()) * yloc;
267 
268  Acts::Vector3 X = (xglob - sensorCenter) / (xglob - sensorCenter).norm();
269  Acts::Vector3 Y = (yglob - sensorCenter) / (yglob - sensorCenter).norm();
270 
271  projx = X - (tangent.dot(X) / tangent.dot(Z)) * Z;
272  projy = Y - (tangent.dot(Y) / tangent.dot(Z)) * Z;
273  if (m_verbosity > 2)
274  std::cout << "projxy " << projx.transpose() << ", " << projy.transpose() << std::endl;
275  return std::make_pair(projx, projy);
276 }
277 
279 {
280  // make all corrections to global position of TPC cluster
281  unsigned int side = TpcDefs::getSide(cluster_key);
282  float z = m_clusterCrossingCorrection.correctZ(global[2], side, crossing);
283  global[2] = z;
284 
285  // apply distortion corrections
286  if (m_dcc_static)
287  {
289  }
290  if (m_dcc_average)
291  {
293  }
294  if (m_dcc_fluctuation)
295  {
297  }
298 }