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