/scratch0/jsnuveri/BDSIM/BDSIMgit/bdsim/src/BDSUtilities.cc

00001 #include "BDSUtilities.hh"
00002 #include <algorithm>
00003 #include <cctype>
00004 #include <functional>
00005 #include <iostream>
00006 #include <limits>
00007 
00008 #include <signal.h>
00009 #include <unistd.h>
00010 #ifdef __APPLE__
00011 #include <mach-o/dyld.h> // for executable path
00012 #endif
00013 
00014 #include "BDSRunManager.hh"
00015 #include "parser/getEnv.h"
00016 
00017 G4bool BDS::non_alpha::operator()(char c)
00018 {
00019   return !isalpha(c);
00020 }
00021 
00022 G4String BDS::PrepareSafeName(G4String name)
00023 {
00024   //remove white space
00025   name.erase(std::remove_if(name.begin(),name.end(),isspace),name.end());
00026   //remove non alpha numeric characters
00027   std::replace_if(name.begin(),name.end(),BDS::non_alpha(),'_');
00028   
00029   return name;
00030 }
00031 
00032 G4int BDS::CalculateOrientation(G4double angle)
00033 {
00034   G4int orientation;
00035   if (angle < 0)
00036     {orientation = 1;}
00037   else
00038     {orientation = -1;}
00039   return orientation;
00040 }
00041 
00042 std::string BDS::GetBDSIMExecPath()
00043 {
00044   // get path to executable (platform dependent)
00045   char path[1024];
00046 #ifdef __linux__
00047   // get path from /proc/self/exe
00048   ssize_t len = ::readlink("/proc/self/exe", path, sizeof(path) - 1);
00049   if (len != -1) {
00050     path[len] = '\0';
00051   }
00052 #elif __APPLE__
00053   uint32_t size = sizeof(path);
00054   if (_NSGetExecutablePath(path, &size) != 0)
00055     std::cout << "buffer too small; need size " << size << std::endl;
00056 #endif
00057   std::string bdsimPath(path);
00058   // remove executable from path
00059   std::string::size_type found = bdsimPath.rfind("/"); // find the last '/'
00060   if (found != std::string::npos){
00061     bdsimPath = bdsimPath.substr(0,found+1); // the path is the bit before that, including the '/'
00062   }
00063   return bdsimPath;
00064 }
00065 
00066 G4String BDS::GetFullPath(G4String fileName, bool excludePathFromFileName)
00067 {
00068   //Set fullPath to mirror what is done in parser.l (i.e. if no environment varible set, assume base filename path is that of the gmad file).
00069   G4String fullPath = getEnv("BDSIMPATH");
00070   if(fullPath.length()<=0){
00071     G4String inputFilepath = "";
00072     if (!excludePathFromFileName) {
00073       // get the path part of the supplied path to the main input file
00074       G4String::size_type found = fileName.rfind("/"); // find the last '/'
00075       if (found != G4String::npos){
00076         inputFilepath = fileName.substr(0,found); // the path is the bit before that
00077       } // else remains empty string
00078     }
00079     // need to know whether it's an absolute or relative path
00080     if ((fileName.substr(0,1)) == "/"){
00081       // the main file has an absolute path
00082       fullPath = inputFilepath;
00083     } else {
00084       // the main file has a relative path or just the file name
00085       char cwdchars[200]; //filepath up to 200 characters
00086       // get current working directory
00087       G4String cwd = (G4String)getcwd(cwdchars, sizeof(cwdchars)) + "/";
00088       fullPath = cwd + inputFilepath;
00089     }
00090   }
00091   // add additional slash just to be safe
00092   fullPath += "/";
00093   return fullPath;
00094 }
00095 
00096 void BDS::HandleAborts(int signal_number)
00097 {
00102   // prevent recursive calling
00103   static int nrOfCalls=0;
00104   if (nrOfCalls>0) exit(1);
00105   nrOfCalls++;
00106   std::cout << "BDSIM is about to crash or was interrupted! " << std::endl;
00107   std::cout << "With signal: " << strsignal(signal_number) << std::endl;
00108   std::cout << "Trying to write and close output file" << std::endl;
00109   std::cout << "Terminate run" << std::endl;
00110   BDSRunManager::GetRunManager()->RunTermination();
00111   std::cout << "Ave, Imperator, morituri te salutant!" << std::endl;
00112 }
00113 
00114 G4bool BDS::IsFinite(const G4double& variable)
00115 {
00116   if (std::abs(variable) > std::numeric_limits<double>::epsilon())
00117     {return true;}
00118   else
00119     {return false;}
00120 }

Generated on 28 Jun 2015 for BDSIM by  doxygen 1.4.7