celestialSources celestialSources-01-06-00
genericSources::Util Class Reference

Various static functions of general use for Science Tools applications. More...

#include <Util.h>

List of all members.

Static Public Member Functions

static bool fileExists (const std::string &filename)
static void file_ok (std::string filename)
 This expands any environment variables in filename and checks if the file exists.
static void readLines (std::string inputFile, std::vector< std::string > &lines, const std::string &skip="#")
 Read lines, separated by "\n", from a file.
static void resolve_fits_files (std::string filename, std::vector< std::string > &files)
 Determine if a file is a FITS file by looking for the "SIMPLE" keyword as the first six characters of the file.
static bool isFitsFile (std::string filename)
 Determine if a file is a FITS file by looking for the "SIMPLE" keyword as the first six characters of the file.
static bool isXmlFile (std::string filename)
static double interpolate (const std::vector< double > &x, const std::vector< double > &y, double xx)
 Linear interpolation.
static double bilinear (const std::vector< double > &xx, double x, const std::vector< double > &yy, double y, const std::vector< double > &z)
 A zeroth order bilinear interpolater.
static bool expectedException (const std::exception &eObj, const std::string &targetMessage)
static double drawFromPowerLaw (double emin, double emax, double gamma)
static double logInterpolate (const std::vector< double > &x, const std::vector< double > &y, double xx)
static double powerLawIntegral (double x1, double x2, double y1, double y2)
static bool fileExists (const std::string &filename)
static void file_ok (std::string filename)
 This expands any environment variables in filename and checks if the file exists.
static void readLines (std::string inputFile, std::vector< std::string > &lines, const std::string &skip="#")
 Read lines, separated by "\n", from a file.
static void resolve_fits_files (std::string filename, std::vector< std::string > &files)
 Determine if a file is a FITS file by looking for the "SIMPLE" keyword as the first six characters of the file.
static bool isFitsFile (std::string filename)
 Determine if a file is a FITS file by looking for the "SIMPLE" keyword as the first six characters of the file.
static bool isXmlFile (std::string filename)
static double interpolate (const std::vector< double > &x, const std::vector< double > &y, double xx)
 Linear interpolation.
static double bilinear (const std::vector< double > &xx, double x, const std::vector< double > &yy, double y, const std::vector< double > &z)
 A zeroth order bilinear interpolater.
static bool expectedException (const std::exception &eObj, const std::string &targetMessage)
static double drawFromPowerLaw (double emin, double emax, double gamma)
static double logInterpolate (const std::vector< double > &x, const std::vector< double > &y, double xx)
static double powerLawIntegral (double x1, double x2, double y1, double y2)

Detailed Description

Various static functions of general use for Science Tools applications.

Author:
J. Chiang
Header:
/nfs/slac/g/glast/ground/cvs/GlastRelease-scons/celestialSources/genericSources/src/Util.h,v 1.4 2006/11/08 20:32:17 jchiang Exp

Definition at line 29 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.h.


Member Function Documentation

double genericSources::Util::bilinear ( const std::vector< double > &  xx,
double  x,
const std::vector< double > &  yy,
double  y,
const std::vector< double > &  z 
) [static]

A zeroth order bilinear interpolater.

Definition at line 113 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.cxx.

                                                     {

      std::vector<double>::const_iterator ix;
      if (x < *(xx.begin())) {
         ix = xx.begin() + 1;
      } else if (x >= *(xx.end()-1)) {
         ix = xx.end() - 1;
      } else {
         ix = std::upper_bound(xx.begin(), xx.end(), x);
      }
      int i = ix - xx.begin();
      
      std::vector<double>::const_iterator iy;
      if (y < *(yy.begin())) {
         iy = yy.begin() + 1;
      } else if (y >= *(yy.end()-1)) {
         iy = yy.end() - 1;
      } else {
         iy = std::upper_bound(yy.begin(), yy.end(), y);
      }
      int j = iy - yy.begin();
      
      double tt = (x - *(ix-1))/(*(ix) - *(ix-1));
      double uu = (y - *(iy-1))/(*(iy) - *(iy-1));
      
      double y1 = z[yy.size()*(i-1) + (j-1)];
      double y2 = z[yy.size()*(i) + (j-1)];
      double y3 = z[yy.size()*(i) + (j)];
      double y4 = z[yy.size()*(i-1) + (j)];

      double value = (1. - tt)*(1. - uu)*y1 + tt*(1. - uu)*y2 
         + tt*uu*y3 + (1. - tt)*uu*y4; 
      if (value < 0.) {
         std::ostringstream message;
         message << "irfUtil::Util::bilinear:\n"
                 << "value = " << value << " < 0\n";
         message << xx[i-1] << "  " << *(ix-1) << "  " 
                 << x << "  " << *ix << "\n";
         message << yy[j-1] << "  " << *(iy-1) << "  " 
                 << y << "  " << *iy << "\n";
         message << tt << "  " << uu << "  " 
                 << y1 << "  " << y2 << "  "
                 << y3 << "  " << y4;
         throw std::runtime_error(message.str());
      }
      return value;
   }
static double genericSources::Util::bilinear ( const std::vector< double > &  xx,
double  x,
const std::vector< double > &  yy,
double  y,
const std::vector< double > &  z 
) [static]

A zeroth order bilinear interpolater.

double genericSources::Util::drawFromPowerLaw ( double  emin,
double  emax,
double  gamma 
) [static]

Definition at line 170 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.cxx.

Referenced by SpectralTransient::ModelInterval::drawEnergy(), SpectralTransient::drawEnergy(), and MapCube::drawEnergy().

                                                                       {
      double xi = CLHEP::RandFlat::shoot();
      double energy;
      if (gamma == 1) {
         energy = emin*std::exp(xi*std::log(emax/emin));
      } else {
         double one_m_gamma = 1. - gamma;
         double arg = xi*(std::pow(emax, one_m_gamma) - 
                          std::pow(emin, one_m_gamma)) 
            + std::pow(emin, one_m_gamma);
         energy = std::pow(arg, 1./one_m_gamma);
      }
      return energy;
   }
static double genericSources::Util::drawFromPowerLaw ( double  emin,
double  emax,
double  gamma 
) [static]
bool genericSources::Util::expectedException ( const std::exception &  eObj,
const std::string &  targetMessage 
) [static]
Returns:
true if eObj.what() contains the targetMessage as a substring.

Definition at line 163 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.cxx.

                                                                 {
      std::string message(eObj.what());
      return message.find_first_of(targetMessage.c_str()) 
         != std::string::npos;
   }
static bool genericSources::Util::expectedException ( const std::exception &  eObj,
const std::string &  targetMessage 
) [static]
Returns:
true if eObj.what() contains the targetMessage as a substring.
void genericSources::Util::file_ok ( std::string  filename) [static]

This expands any environment variables in filename and checks if the file exists.

If it doesn't, a runtime_error exception is thrown. Otherwise, it does nothing.

Definition at line 34 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.cxx.

References fileExists().

Referenced by Pulsar::computeIntegralDist(), FitsTransient::createEvents(), TransientTemplate::createEventTimes(), RadialSource::fillRadialDist(), FileSpectrum::read_file(), MapSource::readFitsFile(), SpectralTransient::readModel(), SourcePopulation::readSourceFile(), and SpectralTransient::readSpectrum().

                                        {
      facilities::Util::expandEnvVar(&filename);
      if (fileExists(filename)) {
         return;
      } else {
         throw std::runtime_error("File not found: " + filename);
      }
   }
static void genericSources::Util::file_ok ( std::string  filename) [static]

This expands any environment variables in filename and checks if the file exists.

If it doesn't, a runtime_error exception is thrown. Otherwise, it does nothing.

bool genericSources::Util::fileExists ( const std::string &  filename) [static]
Returns:
true if the file exists. Environment variables are not* expanded.

Definition at line 29 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.cxx.

Referenced by file_ok().

                                                   {
      std::ifstream file(filename.c_str());
      return file.is_open();
   }
static bool genericSources::Util::fileExists ( const std::string &  filename) [static]
Returns:
true if the file exists. Environment variables are not* expanded.
static double genericSources::Util::interpolate ( const std::vector< double > &  x,
const std::vector< double > &  y,
double  xx 
) [static]

Linear interpolation.

double genericSources::Util::interpolate ( const std::vector< double > &  x,
const std::vector< double > &  y,
double  xx 
) [static]

Linear interpolation.

Definition at line 88 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.cxx.

                                       {
      if (xx < x.front() || xx > x.back()) {
         std::ostringstream message;
         message << "Util::interpolate:\n"
                 << "abscissa value out-of-range, "
                 << xx << " is not in (" 
                 << x.front() << ", "
                 << x.back() << ")";
         throw std::range_error(message.str());
      }
      std::vector<double>::const_iterator it 
         = std::upper_bound(x.begin(), x.end(), xx) - 1;
      unsigned int indx = it - x.begin();
      double yy;
      if (*(it+1) != *it) {
         yy = (xx - *it)/(*(it+1) - *it)*(y[indx+1] - y[indx]) + y[indx];
      } else {
         yy = (y[indx+1] + y[indx])/2.;
         assert(false);
      }
      return yy;
   }
static bool genericSources::Util::isFitsFile ( std::string  filename) [static]

Determine if a file is a FITS file by looking for the "SIMPLE" keyword as the first six characters of the file.

bool genericSources::Util::isFitsFile ( std::string  filename) [static]

Determine if a file is a FITS file by looking for the "SIMPLE" keyword as the first six characters of the file.

Definition at line 71 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.cxx.

Referenced by SpectralTransient::readModel(), and resolve_fits_files().

                                           {
      facilities::Util::expandEnvVar(&filename);
      std::ifstream file(filename.c_str());
      std::string firstLine;
      std::getline(file, firstLine, '\n');
      return firstLine.find("SIMPLE") == 0;
   }
static bool genericSources::Util::isXmlFile ( std::string  filename) [static]
Returns:
true if the filename ends in ".xml" extension
bool genericSources::Util::isXmlFile ( std::string  filename) [static]
Returns:
true if the filename ends in ".xml" extension

Definition at line 79 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.cxx.

                                          {
      std::vector<std::string> tokens;
      facilities::Util::stringTokenize(filename, ".", tokens);
      if (*(tokens.end()-1) == "xml") {
         return true;
      }
      return false;
   }
double genericSources::Util::logInterpolate ( const std::vector< double > &  x,
const std::vector< double > &  y,
double  xx 
) [static]

Definition at line 185 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.cxx.

Referenced by SpectralTransient::readSpectrum(), and Util_tests().

                                          {
      std::vector<double>::const_iterator it 
         = std::upper_bound(x.begin(), x.end(), xx) - 1;
      int indx(it - x.begin());
      if (indx < 0) {
         indx = 0;
      }
      if (static_cast<size_t>(indx) > x.size() - 2) {
         indx = x.size() - 2;
      }
      double yy = 
         y[indx]*std::exp(std::log(xx/x[indx])/std::log(x[indx+1]/x[indx])
                          *std::log(y[indx+1]/y[indx]));
      return yy;
   }
static double genericSources::Util::logInterpolate ( const std::vector< double > &  x,
const std::vector< double > &  y,
double  xx 
) [static]
double genericSources::Util::powerLawIntegral ( double  x1,
double  x2,
double  y1,
double  y2 
) [static]

Definition at line 203 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.cxx.

Referenced by SpectralTransient::readSpectrum(), and Util_tests().

                                                                           {
      if (x1 <= 0 || x2 <= 0 || y1 <= 0 || y2 <= 0) {
         throw std::range_error("Util::powerLawIntegral"
                                "Negative or zero argument passed.");
      }
      double gamma(std::log(y2/y1)/std::log(x2/x1));
      if (gamma == -1) {
         return y1/x1*log(x2/x1);
      }
      return (y1/std::pow(x1, gamma)/(gamma + 1)
              *(std::pow(x2, gamma+1) - std::pow(x1, gamma+1)));
   }
static double genericSources::Util::powerLawIntegral ( double  x1,
double  x2,
double  y1,
double  y2 
) [static]
static void genericSources::Util::readLines ( std::string  inputFile,
std::vector< std::string > &  lines,
const std::string &  skip = "#" 
) [static]

Read lines, separated by "\n", from a file.

Blank lines are skipped.

Parameters:
inputFilefile to be read; environment variables are expanded
linesOn return, this vector is filled with each line read from the file.
skipThe comment string. Lines beginning with this string are not put into lines.
void genericSources::Util::readLines ( std::string  inputFile,
std::vector< std::string > &  lines,
const std::string &  skip = "#" 
) [static]

Read lines, separated by "\n", from a file.

Blank lines are skipped.

Parameters:
inputFilefile to be read; environment variables are expanded
linesOn return, this vector is filled with each line read from the file.
skipThe comment string. Lines beginning with this string are not put into lines.

Definition at line 43 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.cxx.

Referenced by RadialSource::fillRadialDist(), FileSpectrum::read_file(), SpectralTransient::readLightCurve(), Pulsar::readLightCurve(), SpectralTransient::readSpectrum(), and resolve_fits_files().

                                                {
      facilities::Util::expandEnvVar(&inputFile);
      std::ifstream file(inputFile.c_str());
      lines.clear();
      std::string line;
      while (std::getline(file, line, '\n')) {
         if (line != "" && line != " "             //skip (most) blank lines 
             && line.find_first_of(skip) != 0) {   //and commented lines
            lines.push_back(line);
         }
      }
   }
void genericSources::Util::resolve_fits_files ( std::string  filename,
std::vector< std::string > &  files 
) [static]

Determine if a file is a FITS file by looking for the "SIMPLE" keyword as the first six characters of the file.

If it is not a FITS file, then it is assumed to be a list if FITS files.

Parameters:
filenameThe name of the candidate file; enviroment variables are expanded.
filesIf filename is a FITS file, this is filled with the name of that file; otherwise, it is filled with the "\n"-separated lines in the file.

Definition at line 58 of file build/redhat5-i686-32bit-gcc41-Debug/src/Util.cxx.

References isFitsFile(), and readLines().

                                                              {
      facilities::Util::expandEnvVar(&filename);
      files.clear();
      if (isFitsFile(filename)) {
         files.push_back(filename);
         return;
      } else { // filename contains a list of fits files.
         readLines(filename, files);
         return;
      }
   }
static void genericSources::Util::resolve_fits_files ( std::string  filename,
std::vector< std::string > &  files 
) [static]

Determine if a file is a FITS file by looking for the "SIMPLE" keyword as the first six characters of the file.

If it is not a FITS file, then it is assumed to be a list if FITS files.

Parameters:
filenameThe name of the candidate file; enviroment variables are expanded.
filesIf filename is a FITS file, this is filled with the name of that file; otherwise, it is filled with the "\n"-separated lines in the file.

The documentation for this class was generated from the following files: