Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:58:04

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 #include <utility>
0040 
0041 namespace {
0042   
0043   // Helper classes
0044   template <typename T>
0045   class IsEqual{
0046   public:
0047     IsEqual(const T& value): fValue(value) {};
0048     bool operator()(const std::pair<const G4String, T>& myPair) const
0049     {
0050       return myPair.second == fValue;
0051     }
0052   private:
0053     T fValue;
0054   };
0055   
0056   template <typename T>
0057   class InInterval{
0058   public:
0059     InInterval(const T& value): fValue(value) {};
0060     bool operator()(const std::pair<const G4String, std::pair<T, T> >& myPair) const
0061     {
0062       T min = myPair.second.first;
0063       T max = myPair.second.second;
0064       return ((fValue > min || fValue == min) && (fValue < max));
0065     }
0066   private:
0067     T fValue;
0068   };
0069 
0070 }
0071 
0072 template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError>
0073 class G4AttValueFilterT : public ConversionErrorPolicy, public G4VAttValueFilter {
0074 public:
0075 
0076   // Constructor
0077   G4AttValueFilterT();
0078 
0079   // Destructor
0080   virtual ~G4AttValueFilterT();
0081 
0082   // Filter methods
0083   G4bool Accept(const G4AttValue& attVal) const;
0084   G4bool GetValidElement(const G4AttValue& input, G4String& interval) const;
0085 
0086   // Print configuration
0087   virtual void PrintAll(std::ostream& ostr) const;
0088   
0089   // Reset 
0090   virtual void Reset();
0091 
0092   void LoadIntervalElement(const G4String& input);
0093   void LoadSingleValueElement(const G4String& input);
0094 
0095 private:
0096 
0097   typedef std::pair<T, T> Pair;
0098   typedef typename std::map<G4String, Pair> IntervalMap;
0099   typedef std::map<G4String, T> SingleValueMap;
0100 
0101 
0102   // Data members  
0103   IntervalMap fIntervalMap;
0104   SingleValueMap fSingleValueMap;
0105   
0106 };
0107 
0108 template <typename T, typename ConversionErrorPolicy> 
0109 G4AttValueFilterT<T, ConversionErrorPolicy>::G4AttValueFilterT() {}
0110 
0111 template <typename T, typename ConversionErrorPolicy> 
0112 G4AttValueFilterT<T, ConversionErrorPolicy>::~G4AttValueFilterT() {}
0113 
0114 template <typename T, typename ConversionErrorPolicy>
0115 G4bool 
0116 G4AttValueFilterT<T, ConversionErrorPolicy>::GetValidElement(const G4AttValue& attValue, G4String& element) const 
0117 {
0118   T value{};
0119   
0120   G4String input = attValue.GetValue();
0121   if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); 
0122   
0123   typename SingleValueMap::const_iterator iterValues = 
0124     std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value));
0125 
0126   if (iterValues != fSingleValueMap.end()) {
0127     element = iterValues->first;
0128     return true;
0129   }
0130   
0131   typename IntervalMap::const_iterator iterIntervals = 
0132     std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value));
0133 
0134   if (iterIntervals != fIntervalMap.end()) {
0135     element = iterIntervals->first;
0136     return true;
0137   }
0138 
0139   return false;
0140 }
0141 
0142 template <typename T, typename ConversionErrorPolicy>
0143 G4bool 
0144 G4AttValueFilterT<T, ConversionErrorPolicy>::Accept(const G4AttValue& attValue) const 
0145 {
0146   T value{};
0147 
0148   G4String input = attValue.GetValue();
0149   if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); 
0150   
0151   typename SingleValueMap::const_iterator iterValues = 
0152     std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value));
0153 
0154   if (iterValues != fSingleValueMap.end()) return true;
0155   
0156   typename IntervalMap::const_iterator iterIntervals = 
0157     std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value));
0158 
0159   if (iterIntervals != fIntervalMap.end()) return true;
0160 
0161   return false;
0162 }
0163 
0164 template <typename T, typename ConversionErrorPolicy>
0165 void 
0166 G4AttValueFilterT<T, ConversionErrorPolicy>::LoadIntervalElement(const G4String& input) 
0167 {
0168   T min{};
0169   T max{};
0170 
0171   if (!G4ConversionUtils::Convert(input, min, max)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
0172 
0173   fIntervalMap[input] = std::pair<T, T> (min, max);
0174 }
0175 
0176 template <typename T, typename ConversionErrorPolicy>
0177 void 
0178 G4AttValueFilterT<T, ConversionErrorPolicy>::LoadSingleValueElement(const G4String& input) 
0179 {
0180   T output{};
0181   
0182   if (!G4ConversionUtils::Convert(input, output)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
0183 
0184   fSingleValueMap[input] = std::move(output);
0185 }
0186 
0187 template <typename T, typename ConversionErrorPolicy>
0188 void 
0189 G4AttValueFilterT<T, ConversionErrorPolicy>::PrintAll(std::ostream& ostr) const
0190 {
0191   ostr<<"Printing data for filter: "<<Name()<<std::endl;
0192 
0193   ostr<<"Interval data:"<<std::endl;
0194 
0195   typename IntervalMap::const_iterator iterIntervals = fIntervalMap.begin();
0196  
0197   while (iterIntervals != fIntervalMap.end()) {
0198     ostr<<iterIntervals->second.first<<" : "<<iterIntervals->second.second<<std::endl;
0199     iterIntervals++;
0200   }
0201 
0202   ostr<<"Single value data:"<<std::endl;
0203 
0204   typename SingleValueMap::const_iterator iterValues = fSingleValueMap.begin();
0205  
0206   while (iterValues != fSingleValueMap.end()) {
0207     ostr<<iterValues->second<<std::endl;
0208     iterValues++;
0209   }
0210 }
0211 
0212 template <typename T, typename ConversionErrorPolicy>
0213 void 
0214 G4AttValueFilterT<T, ConversionErrorPolicy>::Reset() 
0215 {
0216   fIntervalMap.clear();
0217   fSingleValueMap.clear();
0218 }
0219 
0220 #endif