ghit
Loading...
Searching...
No Matches
calculated_state.cc
Go to the documentation of this file.
1
5
6#include "ghit.h"
7#include "gtouchable.h"
9
10#include <algorithm>
11#include <cmath>
12#include <cstdlib>
13#include <memory>
14#include <numeric>
15#include <optional>
16#include <string>
17#include <vector>
18
19namespace {
20
21bool nearly_equal(double left, double right) {
22 const double scale = std::max({1.0, std::abs(left), std::abs(right)});
23 return std::abs(left - right) <= 1.0e-12 * scale;
24}
25
26bool nearly_equal(const G4ThreeVector& left, const G4ThreeVector& right) {
27 return nearly_equal(left.x(), right.x()) && nearly_equal(left.y(), right.y()) &&
28 nearly_equal(left.z(), right.z());
29}
30
31} // namespace
32
33int main(int argc, char* argv[]) {
34 auto options = std::make_shared<GOptions>(argc, argv, gtouchable::defineOptions());
35 auto touchable = std::make_shared<GTouchable>(options, "readout", "sector: 1", std::vector<double>{}, 1.0);
36 GHit hit(touchable);
37
38 if (hit.getTotalEnergyDeposited() != 0 || hit.getAverageTime() != 0 ||
39 !nearly_equal(hit.getAvgGlobalPosition(), G4ThreeVector{}) ||
40 !nearly_equal(hit.getAvgLocalPosition(), G4ThreeVector{}) ||
41 hit.getProcessName()) {
42 return EXIT_FAILURE;
43 }
44
45 hit.randomizeHitForTesting(2);
46 if (hit.getMotherInfos().size() != hit.getStepCount() ||
47 hit.getMotherInfo().vertex || hit.getMotherInfo().pid) {
48 return EXIT_FAILURE;
49 }
50 const auto energies = hit.getEdeps();
51 const auto times = hit.getTimes();
52 const auto global_positions = hit.getGlobalPositions();
53 const auto local_positions = hit.getLocalPositions();
54 const double expected_total = std::accumulate(energies.begin(), energies.end(), 0.0);
55 double expected_time = 0;
56 G4ThreeVector expected_global;
57 G4ThreeVector expected_local;
58 for (size_t step = 0; step < energies.size(); ++step) {
59 double weight = 1.0 / static_cast<double>(energies.size());
60 if (expected_total > 0) { weight = energies[step] / expected_total; }
61 expected_time += times[step] * weight;
62 expected_global += global_positions[step] * weight;
63 expected_local += local_positions[step] * weight;
64 }
65
66 if (!nearly_equal(hit.getTotalEnergyDeposited(), expected_total) ||
67 !nearly_equal(hit.getAverageTime(), expected_time) ||
68 !nearly_equal(hit.getAvgGlobalPosition(), expected_global) ||
69 !nearly_equal(hit.getAvgLocalPosition(), expected_local) ||
70 hit.getProcessName() != std::optional<std::string>{"placeholder"}) {
71 return EXIT_FAILURE;
72 }
73
74 // Appending more step data must invalidate and rebuild the complete cache.
75 hit.randomizeHitForTesting(1);
76 const auto updated_energies = hit.getEdeps();
77 const double updated_total = std::accumulate(updated_energies.begin(), updated_energies.end(), 0.0);
78 return nearly_equal(hit.getTotalEnergyDeposited(), updated_total) ? EXIT_SUCCESS : EXIT_FAILURE;
79}
Stores step-by-step and aggregated information for a detector hit.
Definition ghit.h:38
GOptions defineOptions()
int main(int argc, char *argv[])