Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09: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 // Jane Tinslay September 2006
0028 //
0029 // Conversion utility functions.
0030 //
0031 #ifndef G4CONVERSIONUTILS_HH
0032 #define G4CONVERSIONUTILS_HH
0033 
0034 #include "globals.hh"
0035 #include "G4DimensionedDouble.hh"
0036 #include "G4DimensionedThreeVector.hh"
0037 #include <sstream>
0038 
0039 namespace G4ConversionUtils 
0040 {
0041   // Generic single value istringstream conversion.
0042   // Returns false if conversion failed or if extra characters
0043   // exist in input.
0044   template <typename Value>
0045   G4bool Convert(const G4String& myInput, Value& output) 
0046   {
0047     G4String input = G4StrUtil::strip_copy(myInput);
0048 
0049     std::istringstream is(input);
0050     char tester;
0051     
0052     return ((is >> output) && !is.get(tester));
0053   }
0054   
0055   // Conversion specialisations.
0056   template<>
0057   inline G4bool Convert(const G4String& myInput, G4DimensionedDouble& output) 
0058   {
0059     G4String input = G4StrUtil::strip_copy(myInput);
0060 
0061     G4double value;
0062     G4String unit;
0063     
0064     std::istringstream is(input);
0065     char tester;
0066     
0067     if (!(is >> value >> unit) || is.get(tester)) return false;
0068 
0069     output = G4DimensionedDouble(value, unit);
0070     
0071     return true;      
0072   }
0073   
0074   template<> inline G4bool Convert(const G4String& myInput, 
0075                                    G4DimensionedThreeVector& output) 
0076   {
0077     G4String input = G4StrUtil::strip_copy(myInput);
0078 
0079     G4double value1, value2, value3;
0080     G4String unit;
0081     
0082     std::istringstream is(input);
0083     char tester;
0084     
0085     if (!(is >> value1 >> value2 >> value3 >>unit) || is.get(tester)) return false;
0086 
0087     output = G4DimensionedThreeVector(G4ThreeVector(value1, value2, value3), unit);
0088     
0089     return true;      
0090   }
0091   
0092   template<> inline G4bool Convert(const G4String& myInput, G4ThreeVector& output) 
0093   {
0094     G4String input = G4StrUtil::strip_copy(myInput);
0095 
0096     G4double value1, value2, value3;
0097     
0098     std::istringstream is(input);
0099     char tester;
0100 
0101     if (!(is >> value1 >> value2 >> value3) || is.get(tester)) return false;
0102     output = G4ThreeVector(value1, value2, value3);
0103 
0104     return true;
0105   }
0106   
0107   // Generic double value istringstream conversion.
0108   // Return false if conversion failed or if extra characters
0109   // exist in input.
0110   template <typename Value> G4bool Convert(const G4String& myInput, Value& value1, 
0111                                            Value& value2) 
0112   {
0113     G4String input = G4StrUtil::strip_copy(myInput);
0114 
0115     std::istringstream is(input);
0116     char tester;
0117     
0118     return ((is >> value1 >> value2) && (!is.get(tester)));
0119   }
0120   
0121   // Conversion specialisations.
0122   template<> inline G4bool Convert(const G4String& myInput, G4DimensionedDouble& min, 
0123                                    G4DimensionedDouble& max) 
0124   {
0125     G4String input = G4StrUtil::strip_copy(myInput);
0126 
0127     G4double valueMin, valueMax;  
0128     G4String unitsMin, unitsMax;
0129     
0130     std::istringstream is(input);
0131     char tester;
0132     
0133     if (!(is >> valueMin >> unitsMin >> valueMax >> unitsMax) || is.get(tester)) return false;;
0134     
0135     min = G4DimensionedDouble(valueMin, unitsMin);
0136     max = G4DimensionedDouble(valueMax, unitsMax);
0137     
0138     return true;      
0139   }
0140   
0141   template<> inline G4bool Convert(const G4String& myInput, G4DimensionedThreeVector& min, 
0142                                    G4DimensionedThreeVector& max) 
0143   {   
0144     G4String input = G4StrUtil::strip_copy(myInput);
0145    
0146     G4double valueMinX, valueMinY, valueMinZ;
0147     G4double valueMaxX, valueMaxY, valueMaxZ;
0148     G4String unitMin, unitMax;
0149     
0150     std::istringstream is(input);
0151     char tester;
0152     
0153     if (!(is >> valueMinX >> valueMinY >> valueMinZ >> unitMin >> valueMaxX >> valueMaxY >> valueMaxZ >> unitMax)
0154         || is.get(tester)) return false;
0155     
0156       min = G4DimensionedThreeVector(G4ThreeVector(valueMinX, valueMinY, valueMinZ), unitMin);
0157       max = G4DimensionedThreeVector(G4ThreeVector(valueMaxX, valueMaxY, valueMaxZ), unitMax);
0158       
0159       return true;      
0160   }
0161   
0162   template<> inline G4bool Convert(const G4String& myInput, G4ThreeVector& min, 
0163                                    G4ThreeVector& max) 
0164   {
0165     G4String input = G4StrUtil::strip_copy(myInput);      
0166 
0167     G4double valueMinX, valueMinY, valueMinZ;
0168     G4double valueMaxX, valueMaxY, valueMaxZ;
0169     
0170     std::istringstream is(input);
0171     char tester;
0172 
0173     if (!(is >> valueMinX >> valueMinY >> valueMinZ >> valueMaxX >> valueMaxY >> valueMaxZ)
0174         || is.get(tester)) return false;
0175     
0176     min = G4ThreeVector(valueMinX, valueMinY, valueMinZ);
0177     max = G4ThreeVector(valueMaxX, valueMaxY, valueMaxZ);
0178     
0179     return true;      
0180   }
0181 
0182 }
0183 
0184 #endif