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

FluxMgr.cxx

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/flux/src/FluxMgr.cxx,v 1.14 2001/10/31 23:20:04 srobinsn Exp $
00002 
00003 
00004 #include "flux/FluxMgr.h"
00005 #include "flux/FluxSource.h"
00006 #include "SpectrumFactoryTable.h"
00007 #include "flux/GPS.h"
00008 
00009 #include "dom/DOM_Document.hpp"
00010 #include "dom/DOM_Element.hpp"
00011 #include "xml/Dom.h"
00012 #include "facilities/error.h"
00013 #include "xml/IFile.h"
00014 
00015 
00016 #define DLL_DECL_SPECTRUM(x)   extern const ISpectrumFactory& x##Factory; x##Factory.addRef();
00017 
00018 
00019 FluxMgr::FluxMgr(std::string  fileName)
00020 {
00021     std::vector<std::string> fileList;
00022     if( fileName.empty() ){             
00023         defaultFile();  
00024     }else{
00025         
00026         fileList.push_back(fileName);
00027         init(fileList);
00028     }   
00029 }
00030 
00031 FluxMgr::FluxMgr(const std::vector<std::string>& fileList)
00032 {
00033     if( fileList.empty() ){
00034         defaultFile();
00035     }else{              
00036         init(fileList);         
00037     }   
00038 }
00039 
00040 void FluxMgr::defaultFile(){
00041     std::vector<std::string> input;
00042     // must find the source_library.xml file.
00043     // set up the xml document to use for initialization parameters
00044     static const char* initialization_document="source_library.xml";
00045     const char* flux_root = ::getenv("FLUXROOT");
00046     std::string doc_path= (flux_root? std::string(flux_root)+"/xml/" : "");
00047     input.push_back(doc_path+initialization_document);  
00048     init(input);
00049 }
00050 
00051 void FluxMgr::init(const std::vector<std::string>& fileList){   
00052     std::string fileName;
00053     
00054     xml::XmlParser parser;
00055     std::vector<std::string>::const_iterator iter = fileList.begin();
00056     
00057     std::string xmlFileIn="";
00058     
00059     writeXmlFile(&xmlFileIn, fileList);
00060     
00061     //a quick way of displaying what goes to the parser
00062     //std::cout << xmlFileIn <<std::endl;
00063 
00064     m_library_doc = parser.parse(xmlFileIn);
00065     
00066     if (m_library_doc == DOM_Document()) {
00067         FATAL_MACRO("can't load initialization file \"" 
00068             << fileName << "\"");
00069     }
00070     
00071     // Root element is of type source_library.  Content is
00072     // one or more source elements.
00073     
00074     
00075     s_library = m_library_doc.getDocumentElement();
00076     
00077     // loop through the source elements to create a map of names, DOM_Elements
00078     if (s_library != DOM_Element()) {
00079         
00080         DOM_Element child = xml::Dom::getFirstChildElement(s_library);
00081         DOM_Element toplevel = xml::Dom::getFirstChildElement(s_library);
00082         while(child != DOM_Element()){
00083             char * b = xml::Dom::transToChar(child.getAttribute("name"));
00084             char * c = "";
00085             while(*b == *c){
00086                 s_library = child;
00087                 child = xml::Dom::getFirstChildElement(s_library);
00088                 b = xml::Dom::transToChar(child.getAttribute("name"));
00089             }
00090             while (child != DOM_Element()) {
00091                 std::string name = xml::Dom::transToChar(child.getAttribute("name"));
00092                 m_sources[name]=child;
00093                 child = xml::Dom::getSiblingElement(child);
00094             }
00095             
00096             child = xml::Dom::getSiblingElement(toplevel);
00097             toplevel=child;
00098         }
00099         
00100     }
00101     // these are the spectra that we want to make available
00102     DLL_DECL_SPECTRUM( CHIMESpectrum);
00103     DLL_DECL_SPECTRUM( AlbedoPSpectrum);
00104     DLL_DECL_SPECTRUM( HeSpectrum);
00105     DLL_DECL_SPECTRUM( GalElSpectrum);
00106     DLL_DECL_SPECTRUM( CrElectron);
00107     DLL_DECL_SPECTRUM( CrProton);
00108     DLL_DECL_SPECTRUM( FILESpectrum);
00109     
00110 }
00111 
00112 
00113 FluxMgr::~FluxMgr(){
00114 }
00115 
00116 EventSource* FluxMgr::source(std::string name)
00117 {
00118     // first check that it is in the library
00119     if( m_sources.find(name)==m_sources.end() ) {
00120         // nope. Maybe a Spectrum object
00121         Spectrum* s = SpectrumFactoryTable::instance()->instantiate(name);
00122         
00123         return s? new FluxSource(s) : (EventSource*)0;
00124     }
00125     return getSourceFromXML(m_sources[name]);
00126 }
00127 
00128 
00129 // sourceFromXML - create a new EventSource from a DOM element
00130 // instantiated, e.g., from a description in source_library.xml
00131 EventSource*  FluxMgr::getSourceFromXML(const DOM_Element& src)
00132 {
00133     DOM_Node    childNode = src.getFirstChild();
00134     if (childNode == DOM_Node()) {
00135     /*
00136     FATAL_MACRO("Improperly formed XML event source");
00137     return 0;
00138         */
00139         // no child node: expect to find the name defined.
00140         return  new FluxSource(src);
00141     }
00142     
00143     DOM_Element sname = xml::Dom::getFirstChildElement(src);
00144     if (sname == DOM_Element() ) {
00145         FATAL_MACRO("Improperly formed XML event source");
00146         return 0;
00147     }
00148     // If we got here, should have legit child element
00149     if ((sname.getTagName()).equals("spectrum")) {
00150         
00151         return  new FluxSource(src);
00152     }
00153     else if ((sname.getTagName()).equals("nestedSource")) {
00154         
00155         // Search for and process immediate child elements.  All must
00156         // be of type "nestedSource".  There may be more than one.
00157         // Content model for nestedSource is EMPTY, so can omit check
00158         // for that in the code
00159         
00160         CompositeSource* cs = new CompositeSource();
00161         do { 
00162             //        DOM_Element sourceChild = (DOM_Element &) childNode;
00163             DOM_Element selem = 
00164                 getLibrarySource(sname.getAttribute("sourceRef"));
00165             if (selem == DOM_Element()) {
00166                 FATAL_MACRO("source name" << 
00167                     xml::Dom::transToChar(sname.getAttribute("sourceRef")) << 
00168                     "' not in source library");
00169             }
00170             cs->addSource(getSourceFromXML(selem));
00171             
00172             sname = xml::Dom::getSiblingElement(sname);
00173         } 
00174         while (sname != DOM_Element() );
00175         return cs;
00176     }
00177     else {
00178         FATAL_MACRO("Unexpected element: "<< 
00179             xml::Dom::transToChar(sname.getTagName()) );
00180     }
00181     return 0;
00182 }
00183 
00184 
00185 
00186 // source library lookup.  Each source is uniquely identified
00187 // by its "name" attribute because "name" is of type ID
00188 DOM_Element    FluxMgr::getLibrarySource(const DOMString& id)
00189 {
00190     // quit if the library was unitialized
00191     if (s_library == DOM_Element() ) return DOM_Element(); 
00192     
00193     return m_library_doc.getElementById(id);
00194 }
00195 
00196 std::list<std::string> FluxMgr::sourceList() const
00197 {
00198     std::list<std::string> s;
00199     for( std::map<std::string, DOM_Element>::const_iterator it = m_sources.begin();
00200     it != m_sources.end();
00201     ++it){
00202         s.push_back((*it).first);
00203     }
00204     return s;
00205 }
00207 void FluxMgr::test(std::ostream& cout, std::string source_name, int count)
00208 {   
00209     EventSource* e = source(source_name);
00210     setExpansion(-1.);
00211     
00212     cout << "running source: " << e->fullTitle() << std::endl;
00213     cout << " Total rate is: " << e->rate() << " Hz into " << e->totalArea() << " m^2" << std::endl;
00214     //cout << "LaunchType" << f->retLaunch() << "Pointtype" << f->retPoint() <<std::endl;
00215     cout << "    Generating " << count << " trials " << std::endl;
00216     cout << " --------------------------------" << std::endl;
00217     for( int i = 0; i< count; ++i) {
00218         FluxSource* f = e->event();
00219 
00220         //testing - pass time
00221         pass(100.0);
00222         //cout << std::endl << GPS::instance()->lat() << GPS::instance()->lon() << std::endl;
00223 
00224         cout << "LaunchType = " << f->refLaunch() << " , Pointtype = " << f->refPoint() <<std::endl;
00225         cout << f->spectrum()->particleName();
00226         cout << "(" << f->energy();
00227         cout << " GeV), Launch: " << f->launchPoint() 
00228             << " Dir " << f->launchDir() << std::endl;
00229     }
00230     cout << "------------------------------------------------------" << std::endl;
00231     
00232     delete e;
00233     
00234 }
00235 
00236 
00237 void FluxMgr::addFactory(std::string name, const ISpectrumFactory* factory ) {
00238     SpectrumFactoryTable::instance()->addFactory(name,factory);
00239 }
00240 
00241 void FluxMgr::setGlastAngles(std::pair<double,double> ang){
00242     GPS::instance()->rotateAngles(ang);
00243 }
00244 
00245 
00246 void FluxMgr::setGlastPosition(std::pair<double,double> pos){
00247     GPS::instance()->ascendingLon(pos.first);
00248     GPS::instance()->ascendingLon(pos.second);
00249 }
00250 
00251 void FluxMgr::setExpansion (double p){
00252     // set the expansion factor for the orbit (-1) = random
00253     GPS::instance()->expansion(p);
00254 }
00255 
00256 // pass a specific amount of time
00257 void FluxMgr::pass(double t){
00258     GPS::instance()->pass(t);
00259     synch();
00260 }
00261 
00262 void FluxMgr::synch(){
00263     GPS::instance()->synch();
00264 }
00265 
00266 void sampleintvl ( /*GPStime*/double t ){
00267     GPS::instance()->sampleintvl(t);
00268 }
00269 
00270 //get the current satellite location
00271 std::pair<double,double> FluxMgr::location(){
00272     return std::make_pair<double,double>(GPS::instance()->lat(),GPS::instance()->lon());
00273 }
00274 
00275 
00276 void FluxMgr::writeXmlFile(std::string *fileStr, const std::vector<std::string>& fileList) {
00277   
00278     std::strstream fileString;
00279     // Unique tag to add to ENTITY elements in the DTD.
00280     char libchar = 'a';
00281     std::string inFileName;
00282     
00283     std::vector<std::string>::const_iterator iter = fileList.begin();
00284  
00285     //the default DTD file
00286     inFileName="$(FLUXROOT)/xml/source.dtd";
00287     //replace $(FLUXROOT) by its system variable
00288     xml::IFile::extractEnvVar(&inFileName);
00289 
00290     //this stuff goes in the beginnning of the XML file to be read into the parser
00291     fileString << "<?xml version='1.0' ?>" << std::endl << "<!DOCTYPE source_library" 
00292         << " SYSTEM " << '"' << inFileName << '"' << " [" << std::endl;
00293  
00294     //as long as there are files in the file list...
00295     for (;iter != fileList.end(); iter++) {
00296   
00297         // get the file name, and evaluate any system variables in it
00298         inFileName=(*iter).c_str();
00299         xml::IFile::extractEnvVar(&inFileName);
00300 
00301         //then make an ENTITY entry specifying where the file is
00302         fileString << "<!ENTITY " << "library" << libchar << " SYSTEM " << '"' 
00303             << inFileName << "\" >" << std::endl;      
00304         libchar++;
00305     }
00306     
00307     fileString << "]>" << std::endl << "<source_library>" << std::endl;
00308     iter = fileList.begin();
00309     libchar = 'a';
00310 
00311     //as long as there are files in the file list...
00312     for (;iter != fileList.end(); iter++) {
00313         // add a reference to the file name
00314         fileString << "&library" << libchar << ";" << std::endl;       
00315         libchar++;
00316     }
00317     
00318     fileString << "</source_library>";
00319     fileStr->append(fileString.str());
00320 
00321 }

Generated at Wed Nov 21 12:20:34 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000