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

Instrument.cxx

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/instrument/src/Instrument.cxx,v 1.10 2001/03/03 04:42:08 igable Exp $
00002 
00003 #include "instrument/Instrument.h"
00004 // Test program for the instrument classes
00005 
00006 #include "instrument/ResponseFile.h"
00007 #include "instrument/GlastDetector.h"
00008 #include "xml/IFileManager.h"
00009 #include <dom/DOM_Element.hpp>
00010 #include "xml/IFile.h"           // probably need this
00011 #include "xml/XmlParser.h"
00012 #include <iostream>
00013 
00014 Instrument::Instrument(): m_rootDetector(0), m_respFile(0), m_ownsDetector(true){}
00015     
00016     
00017 Instrument::~Instrument(){
00018   // get rid of a glast detector hierarchy
00019   if(m_ownsDetector)delete m_rootDetector;
00020   m_rootDetector = 0;
00021   // kill the IFileManager
00022   xml::IFileManager::destroy ();
00023   // close the IRF file
00024   delete m_respFile;
00025   m_respFile = 0;
00026 }
00028 void Instrument::setDetector(GlastDetector* d)
00029 {
00030     m_ownsDetector=false;
00031     m_rootDetector= d;
00032 }
00033 
00034 
00035 // initialize
00036 int Instrument::initialize (std::string iniFile, std::string xmlFile) 
00037 {
00038   ResponseFile::createFactories();
00039 
00040   const char * instrument_root = ::getenv("INSTRUMENTROOT");
00041   std::string root = std::string(instrument_root? instrument_root : ""); 
00042   m_iniFile = ( iniFile.empty())? root + "/xml/instrument.xml": iniFile;
00043   m_xmlFile = ( xmlFile.empty())? root + "/xml/glast.prs" : xmlFile;
00044     
00045   // attempt to load the ini data from the instrument.xml file
00046   if( loadIniFile () ) {
00047     std::cerr << "Could not load ini file: " <<  iniFile << std::endl;
00048     return 1;
00049   }
00050     
00051   if( xmlFile=="(none)" ) return 0; //if don't need
00052 
00053   // attempt to create the persistency data file
00054   int count = loadPersFile ();
00055   if ( count ==0 ) {
00056     std::cerr <<  
00057       "Failed to load detector elements from the persistency file: " 
00058               <<  xmlFile << std::endl;  
00059     return 1;
00060   }
00061   return 0;
00062 }
00063 //--->    do something with this method   <-----
00064 int Instrument::loadPersFile () {
00065     
00066   // find and parse the xml file
00067   xml::XmlParser* parser = new xml::XmlParser;
00068   DOM_Document doc = parser->parse(m_xmlFile.c_str());
00069 
00070   if (doc == DOM_Document()) return -1;   // parse failed
00071 
00072 
00073   DOM_Element root = doc.getDocumentElement();
00074   if (!((root.getTagName()).equals("root")) ) {
00075     delete parser;
00076     return -1;
00077   }
00078   
00079   // create a detector for the root element's first child
00080   GlastDetector*  det = 0;  // detector to create
00081 
00082   DOM_Element glastChild = xml::Dom::findFirstChildByName(root, "*");
00083 
00084   if (glastChild == DOM_Element() ) {
00085     delete parser;
00086     return -1;
00087   }
00088   
00089   det = (GlastDetector*)(GlastDetector::createPersistence(glastChild));
00090   
00091   // now clean up.  We don't need the dom anymore or the parser.
00092 //  delete parser;
00093   
00094   // check the object we created
00095   if (det)    m_rootDetector = det;
00096   else  return 0;
00097   
00098   return  GlastDetector::detector_count();
00099 }
00100 
00101 int Instrument::loadIniFile (){
00102   // load the ini style XML file to pull in all of the various
00103   // instrument parameters
00104   xml::IFileManager::instance()->load (new xml::IFile(m_iniFile.c_str()));
00105   
00106   return 0;
00107 }
00108 
00109 
00110 
00111 // loadNextEvent - attempt to load the next event from the
00112 //                 IRF file.
00113 int  Instrument::readIRF ()
00114 {
00115   if (m_rootDetector) {
00116     // clear out the old detectors
00117     m_rootDetector->clear_all ();
00118     
00119     // try to load the next event
00120     if (!m_respFile->read_event ()) return 1;
00121   }
00122   else return 1;
00123   
00124   return 0;
00125 }
00126 
00127 int Instrument::openIRF(std::string filename)
00128 {
00129   // assign the response (IRF) file
00130   m_respFile = new ResponseFile ( filename.c_str(), true );
00131   
00132   // check validity
00133   if (m_respFile ==0 || !m_respFile->good() ) {
00134     delete m_respFile;
00135     m_respFile = 0;
00136     return 1;
00137   }
00138   m_irfFile = filename;
00139   return 0;
00140 }
00141 
00142 void Instrument::accept(DetectorConverter& dc) const {
00143   
00144   static_cast<const GlastDetector*>(m_rootDetector)->accept(dc);
00145 }
00146 
00147 
00148 int Instrument::detector_count() const { 
00149   return m_rootDetector? GlastDetector::detector_count():0;
00150 }
00151 
00152 int Instrument::detectors_with_data() const { 
00153   return m_respFile? m_respFile->ndet_read(): 0;
00154 }
00155 
00156 const xml::IFile* Instrument::iniFile()const {
00157     return xml::IFileManager::instance()->ifile();
00158 }
00159 
00160 std::string Instrument::irfFileName() const { return m_irfFile; }
00161 std::string Instrument::iniFileName() const { return m_iniFile; }
00162 std::string Instrument::xmlFileName() const { return m_xmlFile; }
00163 GlastDetector* Instrument::rootDetector() const { return m_rootDetector;}

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