00001
00002
00003 #include "instrument/Instrument.h"
00004
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"
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
00019 if(m_ownsDetector)delete m_rootDetector;
00020 m_rootDetector = 0;
00021
00022 xml::IFileManager::destroy ();
00023
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
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
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;
00052
00053
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
00064 int Instrument::loadPersFile () {
00065
00066
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;
00071
00072
00073 DOM_Element root = doc.getDocumentElement();
00074 if (!((root.getTagName()).equals("root")) ) {
00075 delete parser;
00076 return -1;
00077 }
00078
00079
00080 GlastDetector* det = 0;
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
00092
00093
00094
00095 if (det) m_rootDetector = det;
00096 else return 0;
00097
00098 return GlastDetector::detector_count();
00099 }
00100
00101 int Instrument::loadIniFile (){
00102
00103
00104 xml::IFileManager::instance()->load (new xml::IFile(m_iniFile.c_str()));
00105
00106 return 0;
00107 }
00108
00109
00110
00111
00112
00113 int Instrument::readIRF ()
00114 {
00115 if (m_rootDetector) {
00116
00117 m_rootDetector->clear_all ();
00118
00119
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
00130 m_respFile = new ResponseFile ( filename.c_str(), true );
00131
00132
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;}