Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

FILESpectrum Class Reference

Spectrum that reads its differential spectrum from a table. More...

#include <FILESpectrum.h>

Inheritance diagram for FILESpectrum::

Spectrum ISpectrum List of all members.

Public Methods

 FILESpectrum (const std::string &params)
 params is the filename to read. More...

virtual double flux () const
 return total flux. More...

virtual float operator() (float) const
 sample a single particle energy from the spectrum. More...

virtual std::string title () const
 return a title describing the spectrum. More...

virtual const char * particleName () const
 subclasses need to specify correct particle type. More...

const char * nameOf () const

Private Types

typedef std::pair< double,
double > 
efpair

Private Attributes

float m_flux
std::vector< efpairinteg_flux
std::string initialization_document
std::string m_particle_name

Detailed Description

Spectrum that reads its differential spectrum from a table.

Author:
Theodore Hierath
$Header $

Definition at line 19 of file FILESpectrum.h.


Member Typedef Documentation

typedef std::pair<double,double> FILESpectrum::efpair [private]
 

Definition at line 46 of file FILESpectrum.h.


Constructor & Destructor Documentation

FILESpectrum::FILESpectrum const std::string &    params
 

params is the filename to read.

Definition at line 15 of file FILESpectrum.cxx.

References initialization_document, integ_flux, m_flux, and m_particle_name.

00016 {
00017     m_particle_name = "p";
00018     // TODO: have params be an index 
00019     
00020     if(params.empty())
00021         initialization_document = "sources/glast_smin_flx.txt";
00022     else
00023         initialization_document = params.c_str();
00024     
00025     // construct filename, assuming data files in folder <packageroot>/CREME
00026     std::string fileName = "";
00027     const char* flux_root = ::getenv("FLUXROOT");
00028     std::string doc_path= (flux_root? std::string(flux_root)+"/" : "");
00029     fileName = doc_path+initialization_document;
00030     std::ifstream input_file;
00031     input_file.open(fileName.c_str());
00032     
00033     if(false == input_file.is_open())
00034     {
00035         std::cerr << "ERROR:  Unable to open:  " << fileName.c_str() << std::endl;
00036         exit(0);
00037     }
00038     else
00039     {
00040         const int buffer_size = 256;
00041         char buffer[buffer_size];
00042         input_file.getline(buffer,buffer_size,'\n');
00043         std::pair<double,double> ef;
00044         std::vector<efpair> temp_vector;
00045         double total_flux = 0;
00046         {/* Skip whitespace */
00047             char temp = input_file.peek();
00048             while(temp == ' ' || temp == '\n' || temp == '\t')
00049             {
00050                 temp = input_file.get();
00051                 temp = input_file.peek();
00052             }
00053         }
00054         while('.' == input_file.peek() || isdigit(input_file.peek()))
00055         {
00056             input_file >> ef.first;
00057             input_file >> ef.second;
00058             
00059             {/* Convert units */
00060                 ef.first /= 1000;  // MeV -> GeV
00061                 ef.second *= 1000; // particles/m2-s-sr-MeV -> particles/m2-s-sr-GeV
00062             }
00063             
00064             temp_vector.push_back(ef);
00065             
00066             {/* Skip whitespace */
00067                 char temp = input_file.peek();
00068                 while(temp == ' ' || temp == '\n' || temp == '\t')
00069                 {
00070                     temp = input_file.get();
00071                     temp = input_file.peek();
00072                 }
00073             }
00074         }
00075         
00076         std::vector<efpair>::iterator it; 
00077         for(it = temp_vector.begin(); it != temp_vector.end(); it++)
00078         {
00079             ef = (*it);
00080             double factor;
00081             
00082             if(it == temp_vector.begin() && (it+1) != temp_vector.end() )
00083                 factor = ( (it+1)->first - ef.first );
00084             else if( it == temp_vector.begin() && (it+1) == temp_vector.end() )
00085                 factor = 1;
00086             else if( (it+1) != temp_vector.end() )
00087                 factor = (((it+1)->first - (it-1)->first)/2);
00088             else if( (it+1) == temp_vector.end() )
00089                 factor = ( ef.first - (it-1)->first );
00090             
00091             ef.second *= factor;
00092             total_flux += ef.second;
00093             
00094             ef.second = total_flux;
00095             
00096             integ_flux.push_back(ef);
00097         }
00098         m_flux = total_flux;
00099     }
00100 }


Member Function Documentation

double FILESpectrum::flux   const [virtual]
 

return total flux.

Definition at line 103 of file FILESpectrum.cxx.

References m_flux.

00104 {
00106     return m_flux;
00107 }

const char* FILESpectrum::nameOf   const [inline]
 

Definition at line 36 of file FILESpectrum.h.

00036 {return "FILESpectrum";}

float FILESpectrum::operator() float    r const [virtual]
 

sample a single particle energy from the spectrum.

Reimplemented from Spectrum.

Definition at line 110 of file FILESpectrum.cxx.

00111 {
00113     double target_flux = r * m_flux;
00114     
00115     std::vector<efpair>::const_iterator i;
00116     
00117     std::pair<double,double> previous;
00118     
00119     i = integ_flux.begin();
00120     previous = (*i);
00121     
00122     for(i = integ_flux.begin(); i != integ_flux.end(); i++)
00123     {
00124         if((*i).second >= target_flux)
00125             break;
00126         previous = (*i);
00127     }
00128     
00129     // Use linear interpolation between bins
00130     double m = ( (*i).first - previous.first ) / ( (*i).second - previous.second );
00131     double b = (*i).first - m * (*i).second;
00132     
00133     return m * target_flux + b;
00134 }

const char * FILESpectrum::particleName   const [virtual]
 

subclasses need to specify correct particle type.

Reimplemented from Spectrum.

Definition at line 142 of file FILESpectrum.cxx.

References m_particle_name.

00143 {
00144     return m_particle_name.c_str();
00145 }

std::string FILESpectrum::title   const [virtual]
 

return a title describing the spectrum.

Reimplemented from Spectrum.

Definition at line 137 of file FILESpectrum.cxx.

00138 {
00139     return "FILESpectrum";
00140 }


Member Data Documentation

std::string FILESpectrum::initialization_document [private]
 

Definition at line 50 of file FILESpectrum.h.

Referenced by FILESpectrum().

std::vector<efpair> FILESpectrum::integ_flux [private]
 

Definition at line 47 of file FILESpectrum.h.

Referenced by FILESpectrum().

float FILESpectrum::m_flux [private]
 

Definition at line 43 of file FILESpectrum.h.

Referenced by FILESpectrum(), and flux().

std::string FILESpectrum::m_particle_name [private]
 

Definition at line 51 of file FILESpectrum.h.

Referenced by FILESpectrum(), and particleName().


The documentation for this class was generated from the following files:
Generated on Wed Oct 16 14:01:32 2002 by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001