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

SimpleSpectrum.cxx

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/FluxSvc/src/SimpleSpectrum.cxx,v 1.6 2002/06/24 22:37:16 srobinsn Exp $
00002 
00003 
00004 #include "SimpleSpectrum.h"
00005 
00006 #include "dom/DOM_Element.hpp"
00007 #include "xml/Dom.h"
00008 
00009 #include "FluxException.h" // for FATAL_MACRO
00010 #include <utility>
00011 #include <strstream>
00012 #include <cmath>
00013 #include "SpectrumFactory.h"
00014 
00015 static SpectrumFactory<SimpleSpectrum> factory;
00016 
00017 
00018 SimpleSpectrum::SimpleSpectrum(){}//default constructor
00019 SimpleSpectrum::SimpleSpectrum(const std::string& params)
00020 :m_name("gamma")
00021 ,m_E0(parseParamList(params,0))
00022 ,m_index(parseParamList(params,1))
00023 {}
00024 
00025 
00026 SimpleSpectrum::SimpleSpectrum(const char* name, float E0, float index)
00027 :m_E0(E0)
00028 ,m_name(name)
00029 ,m_index( index)
00030 ,m_emax(100.)
00031 {}
00032 
00033 SimpleSpectrum::SimpleSpectrum(const char* name, float Emin, float Emax, float index)
00034 :m_E0(Emin)
00035 ,m_name(name)
00036 ,m_index(index)
00037 ,m_emax(Emax)
00038 {}
00039 
00040 SimpleSpectrum::SimpleSpectrum(const DOM_Element& xelem){
00041     m_name = xml::Dom::getAttribute(xelem, "name").c_str();
00042     
00043     const DOM_Element spectrum = xml::Dom::findFirstChildByName(xelem, "*");
00044     if (spectrum.getTagName().equals(DOMString("power_law"))) {
00045         m_E0 = atof(xml::Dom::getAttribute(spectrum, "emin").c_str());
00046         m_emax = atof(xml::Dom::getAttribute(spectrum, "emax").c_str());
00047         m_index = atof(xml::Dom::getAttribute(spectrum, "gamma").c_str());
00048     }
00049     else if (spectrum.getTagName().equals(DOMString("energy"))) {
00050         m_E0 = atof(xml::Dom::getAttribute(spectrum, "e").c_str());
00051         m_emax = 100.0;
00052         m_index = 0.0;
00053     }
00054     else FATAL_MACRO("Unknown particle spectrum!");
00055 }
00056 
00057 
00058 std::string SimpleSpectrum::title()const
00059 {
00060     std::strstream s;
00061     s << particleName() << '(' << m_E0 << " GeV";
00062     if( m_index >=1 ) s << ',' << m_index ;
00063     s << ")" << '\0';
00064     std::string t(s.str()); s.freeze(false);
00065     return t;
00066 }
00067 
00068 float
00069 SimpleSpectrum::operator()(float f)const
00070 {
00071     if( m_index == 0.0 )     return m_E0;
00072     
00073     if( m_index == 1.0 ) return m_E0*exp(f*log(m_emax/m_E0));
00074     
00075     float x = 1 - exp((1-m_index)*log(m_emax/m_E0));
00076     return m_E0*exp(log(1-x*f)/(1-m_index));
00077 }
00078 
00079 const char*
00080 SimpleSpectrum::particleName() const
00081 {
00082     return m_name.c_str();
00083 }
00084 
00085 double SimpleSpectrum::calculate_rate(double old_rate)
00086 {
00087     return old_rate;
00088 }
00089 
00090 float SimpleSpectrum::parseParamList(std::string input, int index)
00091 {
00092     std::vector<float> output;
00093     int i=0;
00094     for(;!input.empty() && i!=std::string::npos;){
00095         float f = ::atof( input.c_str() );
00096         output.push_back(f);
00097         i=input.find_first_of(",");
00098         input= input.substr(i+1);
00099     } 
00100     return output[index];
00101 }

Generated on Wed Oct 16 14:01:31 2002 by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001