Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4AttValueFilterT.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//
27// Templated class for G4AttValue filters.
28//
29// Jane Tinslay, September 2006
30//
31#ifndef G4ATTVALUEFILTERT_HH
32#define G4ATTVALUEFILTERT_HH
33
34#include "G4AttValue.hh"
35#include "G4VAttValueFilter.hh"
37#include "G4ConversionUtils.hh"
38
39namespace {
40
41 // Helper classes
42 template <typename T>
43 class IsEqual{
44 public:
45 IsEqual(const T& value): fValue(value) {};
46 bool operator()(const std::pair<const G4String, T>& myPair) const
47 {
48 return myPair.second == fValue;
49 }
50 private:
51 T fValue;
52 };
53
54 template <typename T>
55 class InInterval{
56 public:
57 InInterval(const T& value): fValue(value) {};
58 bool operator()(const std::pair<const G4String, std::pair<T, T> >& myPair) const
59 {
60 T min = myPair.second.first;
61 T max = myPair.second.second;
62 return ((fValue > min || fValue == min) && (fValue < max));
63 }
64 private:
65 T fValue;
66 };
67
68}
69
70template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError>
71class G4AttValueFilterT : public ConversionErrorPolicy, public G4VAttValueFilter {
72public:
73
74 // Constructor
76
77 // Destructor
78 virtual ~G4AttValueFilterT();
79
80 // Filter methods
81 G4bool Accept(const G4AttValue& attVal) const;
82 G4bool GetValidElement(const G4AttValue& input, G4String& interval) const;
83
84 // Print configuration
85 virtual void PrintAll(std::ostream& ostr) const;
86
87 // Reset
88 virtual void Reset();
89
90 void LoadIntervalElement(const G4String& input);
91 void LoadSingleValueElement(const G4String& input);
92
93private:
94
95 typedef std::pair<T, T> Pair;
96 typedef typename std::map<G4String, Pair> IntervalMap;
97 typedef std::map<G4String, T> SingleValueMap;
98
99
100 // Data members
101 IntervalMap fIntervalMap;
102 SingleValueMap fSingleValueMap;
103
104};
105
106template <typename T, typename ConversionErrorPolicy>
108
109template <typename T, typename ConversionErrorPolicy>
111
112template <typename T, typename ConversionErrorPolicy>
113G4bool
115{
116 T value{};
117
118 G4String input = attValue.GetValue();
119 if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
120
121 typename SingleValueMap::const_iterator iterValues =
122 std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value));
123
124 if (iterValues != fSingleValueMap.end()) {
125 element = iterValues->first;
126 return true;
127 }
128
129 typename IntervalMap::const_iterator iterIntervals =
130 std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value));
131
132 if (iterIntervals != fIntervalMap.end()) {
133 element = iterIntervals->first;
134 return true;
135 }
136
137 return false;
138}
139
140template <typename T, typename ConversionErrorPolicy>
141G4bool
143{
144 T value{};
145
146 G4String input = attValue.GetValue();
147 if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
148
149 typename SingleValueMap::const_iterator iterValues =
150 std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value));
151
152 if (iterValues != fSingleValueMap.end()) return true;
153
154 typename IntervalMap::const_iterator iterIntervals =
155 std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value));
156
157 if (iterIntervals != fIntervalMap.end()) return true;
158
159 return false;
160}
161
162template <typename T, typename ConversionErrorPolicy>
163void
165{
166 T min{};
167 T max{};
168
169 if (!G4ConversionUtils::Convert(input, min, max)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
170
171 std::pair<T, T> myPair(min, max);
172 fIntervalMap[input] = myPair;
173}
174
175template <typename T, typename ConversionErrorPolicy>
176void
178{
179 T output{};
180
181 if (!G4ConversionUtils::Convert(input, output)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
182
183 fSingleValueMap[input] = output;
184}
185
186template <typename T, typename ConversionErrorPolicy>
187void
189{
190 ostr<<"Printing data for filter: "<<Name()<<std::endl;
191
192 ostr<<"Interval data:"<<std::endl;
193
194 typename IntervalMap::const_iterator iterIntervals = fIntervalMap.begin();
195
196 while (iterIntervals != fIntervalMap.end()) {
197 ostr<<iterIntervals->second.first<<" : "<<iterIntervals->second.second<<std::endl;
198 iterIntervals++;
199 }
200
201 ostr<<"Single value data:"<<std::endl;
202
203 typename SingleValueMap::const_iterator iterValues = fSingleValueMap.begin();
204
205 while (iterValues != fSingleValueMap.end()) {
206 ostr<<iterValues->second<<std::endl;
207 iterValues++;
208 }
209}
210
211template <typename T, typename ConversionErrorPolicy>
212void
214{
215 fIntervalMap.clear();
216 fSingleValueMap.clear();
217}
218
219#endif
bool G4bool
Definition G4Types.hh:86
virtual void PrintAll(std::ostream &ostr) const
G4bool GetValidElement(const G4AttValue &input, G4String &interval) const
G4bool Accept(const G4AttValue &attVal) const
void LoadSingleValueElement(const G4String &input)
void LoadIntervalElement(const G4String &input)
const G4String & GetValue() const
Definition G4AttValue.hh:64
G4bool Convert(const G4String &myInput, Value &output)
T max(const T t1, const T t2)
brief Return the largest of the two arguments
T min(const T t1, const T t2)
brief Return the smallest of the two arguments