ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4AttributeFilterT.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4AttributeFilterT.hh
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 
42 template <typename T>
43 class G4AttributeFilterT : public G4SmartFilter<T> {
44 
45 public:
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 
67 private:
68 
70 
71  typedef std::pair<G4String, Config> Pair;
72  typedef std::vector<Pair> ConfigVect;
73 
74  // Data members
77 
78  // Caching
79  mutable G4bool fFirst;
82 
83 };
84 
85 template <typename T>
87  :G4SmartFilter<T>(name)
88  ,fAttName("")
89  ,fFirst(true)
90  ,fWarnedMissingAttribute(false)
91  ,filter(0)
92 {}
93 
94 template <typename T>
96 {
97  delete filter;
98 }
99 
100 template <typename T>
101 G4bool
102 G4AttributeFilterT<T>::Evaluate(const T& object) const
103 {
104  // Return false if attribute name has not been set. Just print one warning.
105  if (fAttName.isNull()) {
106 
107  if (!fWarnedMissingAttribute) {
108  G4Exception("G4AttributeFilterT::Evaluate", "modeling0101", JustWarning, "Null attribute name");
109  fWarnedMissingAttribute = true;
110  }
111 
112  return false;
113  }
114 
115  if (fFirst) {
116 
117  fFirst = false;
118 
119  // Get attribute definition
120  G4AttDef attDef;
121 
122  // Expect definition to exist
123  if (!G4AttUtils::ExtractAttDef(object, fAttName, attDef)) {
124  static G4bool warnedUnableToExtract = false;
125  if (!warnedUnableToExtract) {
127  ed <<"Unable to extract attribute definition named "<<fAttName;
129  ("G4AttributeFilterT::Evaluate", "modeling0102", JustWarning, ed, "Invalid attribute definition");
130  G4cout << "Available attributes:\n"
131  << object.GetAttDefs();
132  warnedUnableToExtract = true;
133  }
134  return false;
135  }
136 
137  // Get new G4AttValue filter
139 
140  // Load both interval and single valued data.
141  typename ConfigVect::const_iterator iter = fConfigVect.begin();
142 
143  while (iter != fConfigVect.end()) {
144  if (iter->second == G4AttributeFilterT<T>::Interval) {filter->LoadIntervalElement(iter->first);}
145  else if (iter->second == G4AttributeFilterT<T>::SingleValue) {filter->LoadSingleValueElement(iter->first);}
146  iter++;
147  }
148  }
149 
150  // Get attribute value
151  G4AttValue attVal;
152 
153  // Expect value to exist
154  if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) {
155  static G4bool warnedUnableToExtract = false;
156  if (!warnedUnableToExtract) {
158  ed <<"Unable to extract attribute value named "<<fAttName;
160  ("G4AttributeFilterT::Evaluate", "modeling0103", JustWarning, ed, "InvalidAttributeValue");
161  G4cout << "Available attributes:\n"
162  << object.GetAttDefs();
163  warnedUnableToExtract = true;
164  }
165  return false;
166  }
167 
169  G4cout<<"G4AttributeFilterT processing attribute named "<<fAttName;
170  G4cout<<" with value "<<attVal.GetValue()<<G4endl;
171  }
172 
173  // Pass subfilter
174  return (filter->Accept(attVal));
175 }
176 
177 template <typename T>
178 void
180 {
181  fConfigVect.clear();
182  if (0 != filter) filter->Reset();
183 }
184 
185 template <typename T>
186 void
187 G4AttributeFilterT<T>::Print(std::ostream& ostr) const
188 {
189  ostr<<"Printing data for G4Attribute filter named: "<<G4VFilter<T>::Name()<<std::endl;
190  ostr<<"Filtered attribute name: "<<fAttName<<std::endl;
191  ostr<<"Printing sub filter data:"<<std::endl;
192  if (0 != filter) filter->PrintAll(ostr);
193 }
194 
195 template <typename T>
196 void
198 {
199  fAttName = name;
200 }
201 
202 template <typename T>
203 void
205 {
206  std::pair<G4String, Config> myPair(interval, G4AttributeFilterT<T>::Interval);
207 
208  typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair);
209 
210  if (iter != fConfigVect.end()) {
212  ed <<"Interval "<< interval <<" already exists";
214  ("G4AttributeFilterT::AddInterval", "modeling0104", JustWarning, ed);
215  return;
216  }
217 
218  fConfigVect.push_back(myPair);
219 }
220 
221 template <typename T>
222 void
224 {
225  std::pair<G4String, Config> myPair(value, G4AttributeFilterT<T>::SingleValue);
226 
227  typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair);
228 
229  if (iter != fConfigVect.end()) {
231  ed <<"Single value "<< value <<" already exists";
233  ("G4AttributeFilterT::AddValue", "modeling0105", JustWarning, ed);
234  return;
235  }
236  fConfigVect.push_back(myPair);
237 }
238 
239 #endif