EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EventDataView3DBase.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file EventDataView3DBase.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
37 
38 #include <cmath>
39 #include <fstream>
40 #include <optional>
41 #include <random>
42 #include <sstream>
43 #include <string>
44 
46 
47 namespace Acts {
48 namespace EventDataView3DTest {
51 
52 template <BoundIndices... params>
54 
55 std::normal_distribution<double> gauss(0., 1.);
56 std::default_random_engine generator(42);
57 
63 static inline std::string testBoundTrackParameters(IVisualization3D& helper) {
64  std::stringstream ss;
65 
66  ViewConfig pcolor({20, 120, 20});
67  ViewConfig scolor({235, 198, 52});
68 
69  auto gctx = GeometryContext();
70  auto identity = Transform3D::Identity();
71 
72  // rectangle and plane
73  auto rectangle = std::make_shared<RectangleBounds>(15., 15.);
74  auto plane = Surface::makeShared<PlaneSurface>(identity, rectangle);
75 
76  double momentumScale = 0.005;
77  double localErrorScale = 10.;
78  double directionErrorScale = 100.;
79 
80  // now create parameters on this surface
81  // l_x, l_y, phi, theta, q/p (1/p), t
82  std::array<double, 6> pars_array = {
83  {-0.1234, 4.8765, 0.45, 0.128, 0.001, 21.}};
84 
86  BoundTrackParameters::ParametersVector::Zero();
87  pars << pars_array[0], pars_array[1], pars_array[2], pars_array[3],
88  pars_array[4], pars_array[5];
89 
90  BoundSymMatrix cov = BoundSymMatrix::Zero();
91  cov << 0.25, 0.0042, -0.00076, 6.156e-06, -2.11e-07, 0, 0.0042, 0.859,
92  -0.000173, 0.000916, -4.017e-08, 0, -0.00076, -0.000173, 2.36e-04,
93  -2.76e-07, 1.12e-08, 0, 6.15e-06, 0.000916, -2.76e-07, 8.84e-04,
94  -2.85e-11, 0, -2.11 - 07, -4.017e-08, 1.123e-08, -2.85 - 11, 1.26e-10, 0,
95  0, 0, 0, 0, 0, 1;
96 
98  helper, BoundTrackParameters(plane, pars, std::move(cov)), gctx,
99  momentumScale, localErrorScale, directionErrorScale, pcolor, scolor);
100 
101  helper.write("EventData_BoundAtPlaneParameters");
102  helper.write(ss);
103 
104  return ss.str();
105 }
106 
107 static inline std::string testMultiTrajectory(IVisualization3D& helper) {
108  std::stringstream ss;
109 
110  // Create a test context
114 
115  // Construct the rotation
116  RotationMatrix3D rotation = RotationMatrix3D::Identity();
117  double rotationAngle = 90_degree;
118  Vector3D xPos(cos(rotationAngle), 0., sin(rotationAngle));
119  Vector3D yPos(0., 1., 0.);
120  Vector3D zPos(-sin(rotationAngle), 0., cos(rotationAngle));
121  rotation.col(0) = xPos;
122  rotation.col(1) = yPos;
123  rotation.col(2) = zPos;
124 
125  // Boundaries of the surfaces
126  const auto rBounds =
127  std::make_shared<const RectangleBounds>(RectangleBounds(0.1_m, 0.1_m));
128 
129  // Material of the surfaces
130  MaterialSlab matProp(Acts::Test::makeSilicon(), 0.5_mm);
131  const auto surfaceMaterial =
132  std::make_shared<HomogeneousSurfaceMaterial>(matProp);
133 
134  // Set translation vectors
135  std::vector<Vector3D> translations;
136  translations.reserve(6);
137  translations.push_back({-500_mm, 0., 0.});
138  translations.push_back({-300_mm, 0., 0.});
139  translations.push_back({-100_mm, 0., 0.});
140  translations.push_back({100_mm, 0., 0.});
141  translations.push_back({300_mm, 0., 0.});
142  translations.push_back({500_mm, 0., 0.});
143 
144  // Construct layer configs
145  std::vector<CuboidVolumeBuilder::LayerConfig> lConfs;
146  lConfs.reserve(6);
147  unsigned int i;
148  for (i = 0; i < translations.size(); i++) {
150  sConf.position = translations[i];
151  sConf.rotation = rotation;
152  sConf.rBounds = rBounds;
153  sConf.surMat = surfaceMaterial;
154  // The thickness to construct the associated detector element
155  sConf.thickness = 1._um;
156  sConf.detElementConstructor =
157  [](const Transform3D& trans,
158  std::shared_ptr<const RectangleBounds> bounds, double thickness) {
159  return new Test::DetectorElementStub(trans, bounds, thickness);
160  };
162  lConf.surfaceCfg = sConf;
163  lConfs.push_back(lConf);
164  }
165 
166  // Construct volume config
168  vConf.position = {0., 0., 0.};
169  vConf.length = {1.2_m, 1._m, 1._m};
170  vConf.layerCfg = lConfs;
171  vConf.name = "Tracker";
172 
173  // Construct volume builder config
175  conf.position = {0., 0., 0.};
176  conf.length = {1.2_m, 1._m, 1._m};
177  conf.volumeCfg = {vConf}; // one volume
178 
179  // Build detector
180  std::cout << "Build the detector" << std::endl;
182  cvb.setConfig(conf);
184  tgbCfg.trackingVolumeBuilders.push_back(
185  [=](const auto& context, const auto& inner, const auto& vb) {
186  return cvb.trackingVolume(context, inner, vb);
187  });
188  TrackingGeometryBuilder tgb(tgbCfg);
189  std::shared_ptr<const TrackingGeometry> detector =
190  tgb.trackingGeometry(tgContext);
191 
192  // Get the surfaces;
193  std::vector<const Surface*> surfaces;
194  surfaces.reserve(6);
195  detector->visitSurfaces([&](const Surface* surface) {
196  if (surface and surface->associatedDetectorElement()) {
197  std::cout << "surface " << surface->geometryId() << " placed at: ("
198  << surface->center(tgContext).transpose() << " )" << std::endl;
199  surfaces.push_back(surface);
200  }
201  });
202  std::cout << "There are " << surfaces.size() << " surfaces" << std::endl;
203 
204  // Create measurements (assuming they are for a linear track parallel to
205  // global x-axis)
206  std::cout << "Creating measurements:" << std::endl;
207  std::vector<FittableMeasurement<SourceLink>> measurements;
208  measurements.reserve(6);
209  Vector2D lPosCenter{10_mm, 10_mm};
210  std::array<double, 2> resolution = {30_um, 50_um};
212  cov2D << resolution[eBoundLoc0] * resolution[eBoundLoc0], 0., 0.,
213  resolution[eBoundLoc1] * resolution[eBoundLoc1];
214  for (const auto& surface : surfaces) {
215  // 2D measurements
216  double dx = resolution[eBoundLoc0] * gauss(generator);
217  double dy = resolution[eBoundLoc1] * gauss(generator);
219  surface->getSharedPtr(), {}, cov2D, lPosCenter[eBoundLoc0] + dx,
220  lPosCenter[eBoundLoc1] + dy);
221  measurements.push_back(std::move(m01));
222  }
223 
224  // Make a vector of source links as input to the KF
225  std::vector<SourceLink> sourcelinks;
226  std::transform(measurements.begin(), measurements.end(),
227  std::back_inserter(sourcelinks),
228  [](const auto& m) { return SourceLink{&m}; });
229 
230  // The KalmanFitter - we use the eigen stepper for covariance transport
231  std::cout << "Construct KalmanFitter and perform fit" << std::endl;
232  Navigator rNavigator(detector);
233  rNavigator.resolvePassive = false;
234  rNavigator.resolveMaterial = true;
235  rNavigator.resolveSensitive = true;
236 
237  // Configure propagation with deactivated B-field
238  ConstantBField bField(Vector3D(0., 0., 0.));
239  using RecoStepper = EigenStepper<ConstantBField>;
240  RecoStepper rStepper(bField);
241  using RecoPropagator = Propagator<RecoStepper, Navigator>;
242  RecoPropagator rPropagator(rStepper, rNavigator);
243 
244  // Set initial parameters for the particle track
245  Covariance cov;
246  cov << std::pow(100_um, 2), 0., 0., 0., 0., 0., 0., std::pow(100_um, 2), 0.,
247  0., 0., 0., 0., 0., 0.025, 0., 0., 0., 0., 0., 0., 0.025, 0., 0., 0., 0.,
248  0., 0., 0.01, 0., 0., 0., 0., 0., 0., 1.;
249  Vector3D rPos(-1_m, 100_um * gauss(generator), 100_um * gauss(generator));
250  Vector3D rDir(1, 0.025 * gauss(generator), 0.025 * gauss(generator));
251  CurvilinearTrackParameters rStart(makeVector4(rPos, 42_ns), rDir, 1_GeV, 1_e,
252  cov);
253 
254  const Surface* rSurface = &rStart.referenceSurface();
255 
256  using Updater = GainMatrixUpdater;
257  using Smoother = GainMatrixSmoother;
259 
260  KalmanFitter kFitter(rPropagator);
261 
262  auto logger = getDefaultLogger("KalmanFilter", Logging::WARNING);
264  tgContext, mfContext, calContext, VoidOutlierFinder(),
265  LoggerWrapper{*logger}, PropagatorPlainOptions(), rSurface);
266 
267  // Fit the track
268  auto fitRes = kFitter.fit(sourcelinks, rStart, kfOptions);
269  if (not fitRes.ok()) {
270  std::cout << "Fit failed" << std::endl;
271  return ss.str();
272  }
273  auto& fittedTrack = *fitRes;
274 
275  // Draw the track
276  std::cout << "Draw the fitted track" << std::endl;
277  double momentumScale = 15;
278  double localErrorScale = 100.;
279  double directionErrorScale = 500000;
280 
281  ViewConfig scolor({235, 198, 52});
282  ViewConfig mcolor({255, 145, 48});
283  mcolor.offset = -0.01;
284  ViewConfig ppcolor({138, 214, 255});
285  ppcolor.offset = -0.02;
286  ViewConfig fpcolor({92, 149, 255});
287  fpcolor.offset = -0.03;
288  ViewConfig spcolor({20, 120, 20});
289  spcolor.offset = -0.04;
290 
292  helper, fittedTrack.fittedStates, fittedTrack.trackTip, tgContext,
293  momentumScale, localErrorScale, directionErrorScale, scolor, mcolor,
294  ppcolor, fpcolor, spcolor);
295 
296  helper.write("EventData_MultiTrajectory");
297  helper.write(ss);
298 
299  return ss.str();
300 }
301 
302 } // namespace EventDataView3DTest
303 } // namespace Acts