00001
00002 #include "FILESpectrum.h"
00003
00004 #include <cmath>
00005 #include <algorithm>
00006 #include <functional>
00007 #include <fstream>
00008 #include <iostream>
00009
00010 #include "SpectrumFactory.h"
00011
00012 static SpectrumFactory<FILESpectrum> factory;
00013 const ISpectrumFactory& FILESpectrumFactory = factory;
00014
00015 FILESpectrum::FILESpectrum(const std::string& params)
00016 {
00017 m_particle_name = "p";
00018
00019
00020 if(params.empty())
00021 initialization_document = "sources/glast_smin_flx.txt";
00022 else
00023 initialization_document = params.c_str();
00024
00025
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 {
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 {
00060 ef.first /= 1000;
00061 ef.second *= 1000;
00062 }
00063
00064 temp_vector.push_back(ef);
00065
00066 {
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 }
00101
00102
00103
00105 double FILESpectrum::flux() const
00106 {
00107 return m_flux;
00108 }
00109
00111 float FILESpectrum::operator() (float r)const
00112 {
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
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 }
00135
00136
00137 std::string FILESpectrum::title() const
00138 {
00139 return "FILESpectrum";
00140 }
00141
00142 const char * FILESpectrum::particleName() const
00143 {
00144 return m_particle_name.c_str();
00145 }
00146