Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4AttributeFilterT.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// Generic attribute filter.
28//
29// Jane Tinslay, May 2006
30//
31#ifndef G4ATTRIBUTEFILTERT_HH
32#define G4ATTRIBUTEFILTERT_HH
33
34#include "G4AttDef.hh"
35#include "G4AttFilterUtils.hh"
36#include "G4AttUtils.hh"
37#include "G4AttValue.hh"
38#include "G4SmartFilter.hh"
39#include "G4VAttValueFilter.hh"
40#include <vector>
41
42template <typename T>
44
45public:
46
47 // Construct with filter name
48 G4AttributeFilterT(const G4String& name = "Unspecified");
49
50 // Destructor
51 virtual ~G4AttributeFilterT();
52
53 // Evaluate
54 virtual bool Evaluate(const T&) const;
55
56 // Print configuration
57 virtual void Print(std::ostream& ostr) const;
58
59 // Clear filter
60 virtual void Clear();
61
62 // Configuration functions
63 void Set(const G4String& name);
64 void AddInterval(const G4String&);
65 void AddValue(const G4String&);
66
67private:
68
69 enum Config {Interval, SingleValue};
70
71 typedef std::pair<G4String, Config> Pair;
72 typedef std::vector<Pair> ConfigVect;
73
74 // Data members
75 G4String fAttName;
76 ConfigVect fConfigVect;
77
78 // Caching
79 mutable G4bool fFirst;
80 mutable G4VAttValueFilter* filter;
81
82};
83
84template <typename T>
86 :G4SmartFilter<T>(name)
87 ,fAttName("")
88 ,fFirst(true)
89 ,filter(0)
90{}
91
92template <typename T>
97
98template <typename T>
101{
102 // Return true (i.e., do not filter out) if attribute name has not yet been set.
103 if (fAttName.empty()) return true;
104
105 // ...or required attribute value has not yet been set
106 if (fConfigVect.size() == 0) return true;
107
108 if (fFirst) {
109
110 fFirst = false;
111
112 // Get attribute definition
113 G4AttDef attDef;
114
115 // Expect definition to exist
116 if (!G4AttUtils::ExtractAttDef(object, fAttName, attDef)) {
117 static G4bool warnedUnableToExtract = false;
118 if (!warnedUnableToExtract) {
120 ed <<"Unable to extract attribute definition named "<<fAttName<<'\n'
121 << "Available attributes:\n"
122 << *object.GetAttDefs();
124 ("G4AttributeFilterT::Evaluate", "modeling0102", JustWarning, ed, "Invalid attribute definition");
125 warnedUnableToExtract = true;
126 }
127 return false;
128 }
129
130 // Get new G4AttValue filter
131 filter = G4AttFilterUtils::GetNewFilter(attDef);
132
133 // Load both interval and single valued data.
134 typename ConfigVect::const_iterator iter = fConfigVect.begin();
135
136 while (iter != fConfigVect.end()) {
137 if (iter->second == G4AttributeFilterT<T>::Interval) {filter->LoadIntervalElement(iter->first);}
138 else if (iter->second == G4AttributeFilterT<T>::SingleValue) {filter->LoadSingleValueElement(iter->first);}
139 iter++;
140 }
141 }
142
143 // Get attribute value
144 G4AttValue attVal;
145
146 // Expect value to exist
147 if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) {
148 static G4bool warnedUnableToExtract = false;
149 if (!warnedUnableToExtract) {
151 ed <<"Unable to extract attribute definition named "<<fAttName<<'\n'
152 << "Available attributes:\n"
153 << *object.GetAttDefs();
155 ("G4AttributeFilterT::Evaluate", "modeling0103", JustWarning, ed, "InvalidAttributeValue");
156 warnedUnableToExtract = true;
157 }
158 return false;
159 }
160
162 G4cout<<"G4AttributeFilterT processing attribute named "<<fAttName;
163 G4cout<<" with value "<<attVal.GetValue()<<G4endl;
164 }
165
166 // Pass subfilter
167 return (filter->Accept(attVal));
168}
169
170template <typename T>
171void
173{
174 fConfigVect.clear();
175 if (0 != filter) filter->Reset();
176}
177
178template <typename T>
179void
180G4AttributeFilterT<T>::Print(std::ostream& ostr) const
181{
182 ostr<<"Printing data for G4Attribute filter named: "<<G4VFilter<T>::Name()<<std::endl;
183 ostr<<"Filtered attribute name: "<<fAttName<<std::endl;
184 ostr<<"Printing sub filter data:"<<std::endl;
185 if (0 != filter) filter->PrintAll(ostr);
186}
187
188template <typename T>
189void
191{
192 fAttName = name;
193}
194
195template <typename T>
196void
198{
199 std::pair<G4String, Config> myPair(interval, G4AttributeFilterT<T>::Interval);
200
201 typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair);
202
203 if (iter != fConfigVect.end()) {
205 ed <<"Interval "<< interval <<" already exists";
207 ("G4AttributeFilterT::AddInterval", "modeling0104", JustWarning, ed);
208 return;
209 }
210
211 fConfigVect.push_back(myPair);
212}
213
214template <typename T>
215void
217{
218 std::pair<G4String, Config> myPair(value, G4AttributeFilterT<T>::SingleValue);
219
220 typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair);
221
222 if (iter != fConfigVect.end()) {
224 ed <<"Single value "<< value <<" already exists";
226 ("G4AttributeFilterT::AddValue", "modeling0105", JustWarning, ed);
227 return;
228 }
229 fConfigVect.push_back(myPair);
230}
231
232#endif
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
bool G4bool
Definition G4Types.hh:86
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
const G4String & GetValue() const
Definition G4AttValue.hh:64
void AddValue(const G4String &)
void Set(const G4String &name)
G4AttributeFilterT(const G4String &name="Unspecified")
virtual bool Evaluate(const T &) const
void AddInterval(const G4String &)
virtual void Print(std::ostream &ostr) const
G4String Name() const
Definition G4VFilter.hh:80
G4VAttValueFilter * GetNewFilter(const G4AttDef &def)
G4bool ExtractAttDef(const T &object, const G4String &name, G4AttDef &def)
Definition G4AttUtils.hh:61
G4bool ExtractAttValue(const T &object, const G4String &name, G4AttValue &attVal)
Definition G4AttUtils.hh:76