Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AverageSimHits.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file AverageSimHits.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 
18 
19 #include <tuple>
20 
21 namespace ActsExamples {
22 
25 
36 inline std::tuple<Acts::Vector2, Acts::Vector4, Acts::Vector3> averageSimHits(
37  const Acts::GeometryContext& gCtx, const Acts::Surface& surface,
38  const SimHitContainer& simHits, const HitSimHitsRange& hitSimHitsRange,
39  const Acts::Logger& logger) {
40  using namespace Acts::UnitLiterals;
41 
42  Acts::Vector2 avgLocal = Acts::Vector2::Zero();
43  Acts::Vector4 avgPos4 = Acts::Vector4::Zero();
44  Acts::Vector3 avgDir = Acts::Vector3::Zero();
45 
46  size_t n = 0u;
47  for (auto [_, simHitIdx] : hitSimHitsRange) {
48  n += 1u;
49 
50  // we assume that the indices are within valid ranges so we do not need to
51  // check their validity again.
52  const auto& simHit = *simHits.nth(simHitIdx);
53 
54  // transforming first to local positions and average that ensures that the
55  // averaged position is still on the surface. the averaged global position
56  // might not be on the surface anymore.
57  auto result = surface.globalToLocal(gCtx, simHit.position(),
58  simHit.direction(), 0.5_um);
59  if (result.ok()) {
60  avgLocal += result.value();
61  } else {
62  ACTS_WARNING("While averaging simhit, hit "
63  << simHitIdx << " is not on the corresponding surface "
64  << surface.geometryId() << "; use [0,0] as local position");
65  }
66  // global position should already be at the intersection. no need to perform
67  // an additional intersection call.
68  avgPos4 += simHit.fourPosition();
69  avgDir += simHit.direction();
70  }
71 
72  // only need to average if there are at least two inputs
73  if (2u <= n) {
74  double scale = 1.0 / n;
75  avgLocal *= scale;
76  avgPos4 *= scale;
77  avgDir.normalize();
78  }
79 
80  return {avgLocal, avgPos4, avgDir};
81 }
82 
83 } // namespace ActsExamples