Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4MoleculeCounterTemplates.hh
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// Author: Christian Velten (2025)
27
28#ifndef G4MOLECULECOUNTERTEMPLATES_HH
29#define G4MOLECULECOUNTERTEMPLATES_HH 1
30
31#include "G4Types.hh"
32#include "G4VMoleculeCounter.hh"
33
34#include <map>
35#include <set>
36#include <vector>
37
38namespace G4
39{
41{
42
43//------------------------------------------------------------------------------
44
45template<typename T>
47{
48 return G4String(typeid(T).name());
49}
50
51//------------------------------------------------------------------------------
52
53template<typename T>
54G4bool Contains(const std::set<T>& _set, const T& _value)
55{
56 return std::binary_search(_set.cbegin(), _set.cend(), _value);
57}
58
59template<typename T>
60G4bool Contains(const std::vector<T>& _vector, const T& _value)
61{
62 auto p = std::find(_vector.cbegin(), _vector.cend(), _value);
63 return p != _vector.cend();
64}
65
66template<typename T, typename U>
67G4bool ContainsKey(const std::map<T, U>& _map, const T& _key)
68{
69 auto keys = GetMapIndices(_map);
70 return Contains(keys, _key);
71}
72
73//------------------------------------------------------------------------------
74
75template<typename T, typename U>
76const std::vector<T> GetMapIndices(const std::map<T, U>& _map)
77{
78 std::vector<T> output;
79 for (auto const& it : _map)
80 output.push_back(it.first);
81 return output;
82}
83
84//------------------------------------------------------------------------------
85
86template<typename T>
87void DumpCounterMapIndices(const std::map<T, InnerCounterMapType>& map, G4bool includeEmpty = false)
88{
89 G4cout << "--- BEGIN COUNTER MAP INDEX DUMP ---" << G4endl;
90 auto i = 0;
91 for (auto const& it : map) {
92 if (!includeEmpty && it.second.size() == 0) continue;
93 G4cout << i++ << ": " << it.first.GetInfo() << G4endl;
94 }
95 G4cout << "--- END COUNTER MAP INDEX DUMP ---" << G4endl;
96}
97
98//------------------------------------------------------------------------------
99
100template<typename T>
101void DumpCounterMapContents(const std::map<T, InnerCounterMapType>& map,
102 G4bool includeEmpty = false)
103{
104 G4cout << "--- BEGIN COUNTER MAP DUMP ---" << G4endl;
105 for (auto const& it : map) {
106 if (!includeEmpty && it.second.size() == 0) continue;
107 G4cout << " :: " << it.first.GetInfo() << G4endl;
108 for (auto const& it2 : it.second) {
109 G4cout << std::setw(3) << std::setprecision(3) << " " << G4BestUnit(it2.first, "Time")
110 << " " << std::setw(5) << it2.second << G4endl;
111 }
112 }
113 G4cout << "--- END COUNTER MAP DUMP ---" << G4endl;
114}
115
116//------------------------------------------------------------------------------
117
118template<typename T>
119std::set<G4double> GetRecordedTimes(const std::map<T, InnerCounterMapType>& map)
120{
121 std::set<G4double> output{};
122 for (const auto& it : map) {
123 G4cerr << it.second.size() << G4endl;
124 for (const auto& it2 : it.second) {
125 output.insert(it2.first);
126 }
127 }
128 return output;
129}
130
131//------------------------------------------------------------------------------
132
133template<typename TKey, typename TValue, typename TComp>
134typename std::map<TKey, TValue, TComp>::iterator
135FindClosestEntryForKey(std::map<TKey, TValue, TComp>& map, TKey key)
136{
137 if (map.empty()) return map.end();
138
139 auto it_lb = map.lower_bound(key);
140
141 if (it_lb == map.begin()) return it_lb;
142 if (it_lb == map.end()) return --it_lb;
143
144 auto prev_it = std::prev(it_lb);
145
146 if (std::abs(it_lb->first - key) < std::abs(prev_it->first - key))
147 return it_lb;
148 else
149 return prev_it;
150}
151
152//------------------------------------------------------------------------------
153
154} // namespace MoleculeCounter
155} // namespace G4
156
157#endif
#define G4BestUnit(a, b)
bool G4bool
Definition G4Types.hh:86
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
void DumpCounterMapIndices(const std::map< T, InnerCounterMapType > &map, G4bool includeEmpty=false)
const std::vector< T > GetMapIndices(const std::map< T, U > &_map)
void DumpCounterMapContents(const std::map< T, InnerCounterMapType > &map, G4bool includeEmpty=false)
std::map< TKey, TValue, TComp >::iterator FindClosestEntryForKey(std::map< TKey, TValue, TComp > &map, TKey key)
std::set< G4double > GetRecordedTimes(const std::map< T, InnerCounterMapType > &map)
G4bool Contains(const std::set< T > &_set, const T &_value)
G4bool ContainsKey(const std::map< T, U > &_map, const T &_key)