ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4AttValueFilterT.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4AttValueFilterT.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 // 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 
39 namespace {
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 
70 template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError>
71 class G4AttValueFilterT : public ConversionErrorPolicy, public G4VAttValueFilter {
72 public:
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 
93 private:
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
103 
104 };
105 
106 template <typename T, typename ConversionErrorPolicy>
108 
109 template <typename T, typename ConversionErrorPolicy>
111 
112 template <typename T, typename ConversionErrorPolicy>
113 G4bool
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 
140 template <typename T, typename ConversionErrorPolicy>
141 G4bool
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 
162 template <typename T, typename ConversionErrorPolicy>
163 void
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 
175 template <typename T, typename ConversionErrorPolicy>
176 void
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 
186 template <typename T, typename ConversionErrorPolicy>
187 void
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 
211 template <typename T, typename ConversionErrorPolicy>
212 void
214 {
215  fIntervalMap.clear();
216  fSingleValueMap.clear();
217 }
218 
219 #endif