Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:56

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 //
0027 // Templated class for G4AttValue filters.
0028 //
0029 // Jane Tinslay, September 2006
0030 //
0031 #ifndef G4ATTVALUEFILTERT_HH
0032 #define G4ATTVALUEFILTERT_HH
0033 
0034 #include "G4AttValue.hh"
0035 #include "G4VAttValueFilter.hh"
0036 #include "G4ConversionFatalError.hh"
0037 #include "G4ConversionUtils.hh"
0038 
0039 namespace {
0040   
0041   // Helper classes
0042   template <typename T>
0043   class IsEqual{
0044   public:
0045     IsEqual(const T& value): fValue(value) {};
0046     bool operator()(const std::pair<const G4String, T>& myPair) const
0047     {
0048       return myPair.second == fValue;
0049     }
0050   private:
0051     T fValue;
0052   };
0053   
0054   template <typename T>
0055   class InInterval{
0056   public:
0057     InInterval(const T& value): fValue(value) {};
0058     bool operator()(const std::pair<const G4String, std::pair<T, T> >& myPair) const
0059     {
0060       T min = myPair.second.first;
0061       T max = myPair.second.second;
0062       return ((fValue > min || fValue == min) && (fValue < max));
0063     }
0064   private:
0065     T fValue;
0066   };
0067 
0068 }
0069 
0070 template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError>
0071 class G4AttValueFilterT : public ConversionErrorPolicy, public G4VAttValueFilter {
0072 public:
0073 
0074   // Constructor
0075   G4AttValueFilterT();
0076 
0077   // Destructor
0078   virtual ~G4AttValueFilterT();
0079 
0080   // Filter methods
0081   G4bool Accept(const G4AttValue& attVal) const;
0082   G4bool GetValidElement(const G4AttValue& input, G4String& interval) const;
0083 
0084   // Print configuration
0085   virtual void PrintAll(std::ostream& ostr) const;
0086   
0087   // Reset 
0088   virtual void Reset();
0089 
0090   void LoadIntervalElement(const G4String& input);
0091   void LoadSingleValueElement(const G4String& input);
0092 
0093 private:
0094 
0095   typedef std::pair<T, T> Pair;
0096   typedef typename std::map<G4String, Pair> IntervalMap;
0097   typedef std::map<G4String, T> SingleValueMap;
0098 
0099 
0100   // Data members  
0101   IntervalMap fIntervalMap;
0102   SingleValueMap fSingleValueMap;
0103   
0104 };
0105 
0106 template <typename T, typename ConversionErrorPolicy> 
0107 G4AttValueFilterT<T, ConversionErrorPolicy>::G4AttValueFilterT() {}
0108 
0109 template <typename T, typename ConversionErrorPolicy> 
0110 G4AttValueFilterT<T, ConversionErrorPolicy>::~G4AttValueFilterT() {}
0111 
0112 template <typename T, typename ConversionErrorPolicy>
0113 G4bool 
0114 G4AttValueFilterT<T, ConversionErrorPolicy>::GetValidElement(const G4AttValue& attValue, G4String& element) const 
0115 {
0116   T value{};
0117   
0118   G4String input = attValue.GetValue();
0119   if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); 
0120   
0121   typename SingleValueMap::const_iterator iterValues = 
0122     std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value));
0123 
0124   if (iterValues != fSingleValueMap.end()) {
0125     element = iterValues->first;
0126     return true;
0127   }
0128   
0129   typename IntervalMap::const_iterator iterIntervals = 
0130     std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value));
0131 
0132   if (iterIntervals != fIntervalMap.end()) {
0133     element = iterIntervals->first;
0134     return true;
0135   }
0136 
0137   return false;
0138 }
0139 
0140 template <typename T, typename ConversionErrorPolicy>
0141 G4bool 
0142 G4AttValueFilterT<T, ConversionErrorPolicy>::Accept(const G4AttValue& attValue) const 
0143 {
0144   T value{};
0145 
0146   G4String input = attValue.GetValue();
0147   if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); 
0148   
0149   typename SingleValueMap::const_iterator iterValues = 
0150     std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value));
0151 
0152   if (iterValues != fSingleValueMap.end()) return true;
0153   
0154   typename IntervalMap::const_iterator iterIntervals = 
0155     std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value));
0156 
0157   if (iterIntervals != fIntervalMap.end()) return true;
0158 
0159   return false;
0160 }
0161 
0162 template <typename T, typename ConversionErrorPolicy>
0163 void 
0164 G4AttValueFilterT<T, ConversionErrorPolicy>::LoadIntervalElement(const G4String& input) 
0165 {
0166   T min{};
0167   T max{};
0168 
0169   if (!G4ConversionUtils::Convert(input, min, max)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
0170 
0171   std::pair<T, T> myPair(min, max);
0172   fIntervalMap[input] = myPair;
0173 }
0174 
0175 template <typename T, typename ConversionErrorPolicy>
0176 void 
0177 G4AttValueFilterT<T, ConversionErrorPolicy>::LoadSingleValueElement(const G4String& input) 
0178 {
0179   T output{};
0180   
0181   if (!G4ConversionUtils::Convert(input, output)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
0182 
0183   fSingleValueMap[input] = output;
0184 }
0185 
0186 template <typename T, typename ConversionErrorPolicy>
0187 void 
0188 G4AttValueFilterT<T, ConversionErrorPolicy>::PrintAll(std::ostream& ostr) const
0189 {
0190   ostr<<"Printing data for filter: "<<Name()<<std::endl;
0191 
0192   ostr<<"Interval data:"<<std::endl;
0193 
0194   typename IntervalMap::const_iterator iterIntervals = fIntervalMap.begin();
0195  
0196   while (iterIntervals != fIntervalMap.end()) {
0197     ostr<<iterIntervals->second.first<<" : "<<iterIntervals->second.second<<std::endl;
0198     iterIntervals++;
0199   }
0200 
0201   ostr<<"Single value data:"<<std::endl;
0202 
0203   typename SingleValueMap::const_iterator iterValues = fSingleValueMap.begin();
0204  
0205   while (iterValues != fSingleValueMap.end()) {
0206     ostr<<iterValues->second<<std::endl;
0207     iterValues++;
0208   }
0209 }
0210 
0211 template <typename T, typename ConversionErrorPolicy>
0212 void 
0213 G4AttValueFilterT<T, ConversionErrorPolicy>::Reset() 
0214 {
0215   fIntervalMap.clear();
0216   fSingleValueMap.clear();
0217 }
0218 
0219 #endif