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

ResponseFile.cxx

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/instrument/src/ResponseFile.cxx,v 1.8 2001/08/08 01:44:05 burnett Exp $
00002 //  ResponseFile.cxx
00003 //  Author:     Sawyer Gillespie
00004 //              Department of Physics, University of Washington
00005 //              hgillesp@u.washington.edu
00006 //
00007 //  Implementation of the ResponseFile class
00008 //
00009 
00010 #include "instrument/ResponseFile.h"
00011 #include "instrument/CsIDetector.h"
00012 #include "instrument/MCTruth.h"
00013 #include "instrument/Scintillator.h"
00014 #include "instrument/SiDetector.h"
00015 #include "instrument/Calorimeter.h"
00016 #include "instrument/Glast.h"
00017 #include "instrument/Tower.h"
00018 #include "instrument/DetectorVolumes.h"
00019 #include "instrument/SiTracker.h"
00020 
00021 
00022 #include "xml/IFileManager.h"
00023 #include "xml/IFile.h"
00024 
00025 #include <iterator>
00026 
00027 // class implementation
00028 
00029 const char* ResponseFile::s_version = "1.0.0";
00030 const char* ResponseFile::s_revision = "1.0";
00031 
00032 namespace {
00033   bool responsefileParamLoad = 
00034     xml::IFileManager::loadParams(ResponseFile::loadParameters);
00035 }
00036 
00037 void ResponseFile::createFactories(){
00038   //  handles loading all of the factories for the 
00039   // various GlastDetector classes
00040   DetectorBox::_PersistKey detvl(DetectorBox::classPersistKey);
00041   Calorimeter::_PersistKey cal(Calorimeter::classPersistKey);
00042   SiDetector::_PersistKey  si(SiDetector::classPersistKey);
00043   MCTruth::_PersistKey     mc(MCTruth::classPersistKey);
00044   CsIDetector::_PersistKey csi = CsIDetector::classPersistKey;
00045   Glast::_PersistKey       glast(Glast::classPersistKey);
00046   Tower::_PersistKey       tow(Tower::classPersistKey);
00047   SiTracker::_PersistKey   track(SiTracker::classPersistKey);
00048   Scintillator::_PersistKey scin(Scintillator::classPersistKey);
00049 
00050 }
00051 // constructors
00052 
00053 ResponseFile::ResponseFile (std::ostream* o, bool owns, ios_mode mode) 
00054   : m_binin (0), m_stdin(0), m_ownsin (false), m_binout(0), m_stdout(0), m_ownsout (false)
00055   , m_version(""), m_revision("")
00056 {
00057   setout (o, mode, owns);
00058 }
00059 
00060 ResponseFile::ResponseFile (std::istream* i, bool owns, ios_mode mode) 
00061   : m_binin (0), m_stdin(0), m_ownsin (false), m_binout(0), m_stdout(0), 
00062     m_ownsout (false), m_version(""), m_revision("")
00063 {
00064   setin (i, mode, owns);
00065 }
00066 
00067 ResponseFile::ResponseFile (const char* fname, bool read, ios_mode mode)
00068   : m_binin (0), m_stdin(0), m_ownsin (false), m_binout(0), m_stdout(0), 
00069     m_ownsout (false), m_version(""), m_revision("")
00070 {
00071   if (read) {
00072     std::ifstream*  inf = new std::ifstream (fname, (mode == binary_mode) ?
00073                                              std::ios::binary : std::ios::in);
00074     if (inf->good())
00075       setin (inf, mode, true);
00076     else    
00077       delete inf;
00078   } else {
00079     std::ofstream*  off = new std::ofstream (fname, (mode == binary_mode) ? 
00080                                              std::ios::binary : std::ios::out);
00081     if (off->good())
00082       setout (off, mode, true);
00083     else
00084       delete off;
00085   }
00086 }
00087 
00088 void    ResponseFile::setin (std::istream* in, ios_mode mode, bool owns)
00089 {
00090   if (m_ownsin)   {
00091     delete m_stdin;
00092     delete m_binin;
00093   }
00094   if (mode == binary_mode) {
00095 #ifdef _MSC_VER
00096     m_binin = new bpkt::istream (in);
00097 #else
00098     m_binin = in;
00099 #endif
00100   } else 
00101     m_stdin = in;
00102 
00103   m_ownsin = true;
00104 
00105   if (!verify ((mode == binary_mode) ? *m_binin : *m_stdin, mode)) { 
00106     // check response file validity
00107     // warn user & dump the file.
00108     WARNING ("Incompatible IRF file format");
00109     // delete m_in;    
00110     // m_in = 0;
00111   }
00112 }
00113 
00114 void    ResponseFile::setout (std::ostream* out, ios_mode mode, bool owns)
00115 {
00116   if (m_ownsout)  {
00117     delete m_stdout;
00118     delete m_binout;
00119   }
00120   if (mode == binary_mode) {
00121 #ifdef _MSC_VER
00122     m_binout = new bpkt::ostream (out);
00123 #else
00124     m_binout = out;
00125 #endif
00126   } else
00127     m_stdout = out;
00128 
00129   m_ownsout = true;
00130 
00131   // write the version control sequence
00132   verify ((mode == binary_mode) ? *m_binout : *m_stdout, mode); 
00133 }
00134 
00135 // destructor
00136 
00137 ResponseFile::~ResponseFile ()
00138 {
00139   if (m_ownsin) {
00140     delete m_stdin;    // close files
00141     delete m_binin;
00142   }
00143   if (m_ownsout) {
00144     delete m_stdout;
00145     delete m_binout;
00146   }
00147 }
00148 
00149 // loadParameters - read paramter data from an INI structure
00150 void    ResponseFile::loadParameters (xml::IFile& ini)
00151 {
00152   s_version = ini.getString("configuration", "version", s_version);
00153   s_revision = ini.getString("configuration", "revision", s_revision);
00154 }
00155 
00156 // verify - write version control sequence (determines compatibility)
00157 
00158 void    ResponseFile::verify (std::ostream& o, ios_mode mode)
00159 {
00160   if (o.good()) {
00161     m_version = s_version;
00162     m_revision = s_revision;
00163     o << m_version << '\n';
00164     o << m_revision << '\n';
00165     if (mode == binary_mode)    o << '\0' << std::flush;
00166   }
00167 }
00168 
00169 // verify - read the response file and determine its validity
00170 
00171 bool    ResponseFile::verify (std::istream& i, ios_mode mode)
00172 {
00173   std::getline(i, m_version, '\n');
00174   std::getline(i, m_revision, '\n');
00175 
00176   if (m_revision != s_revision) {
00177     std::strstream msg; 
00178     msg << "Response file has was created using a different instrument.ini revision ("
00179         << m_revision << "), than currently ("
00180         << s_revision << "). Results MAY be unreliable" << std::ends;
00181     WARNING(msg.str());
00182     msg.freeze(false);
00183   } 
00184   if (mode == binary_mode)    i.sync();
00185 
00186   if (m_version == s_version) return true;
00187   return false;
00188 }
00189 
00190 // write_event - dump event data to a file
00191 
00192 void    ResponseFile::write_event ()
00193 {
00194   if (!good() || (!m_stdout && !m_binout))    return;
00195 
00196   GlastDetector::MemberList::const_iterator it;
00197   for (it = GlastDetector::allbegin(); it != GlastDetector::allend(); ++it) {
00198     (*this) << *(*it).second;
00199   }
00200   end_event ();
00201 }
00202 
00203 void    ResponseFile::write_event (std::vector<int> header)
00204 {
00205   if (!good() || (!m_stdout && !m_binout))    return;
00206   if( m_stdout ) {
00207           (*m_stdout) << "\n0 " ; // this is a pseudo-id, that should not match any real ones!
00208           std::copy(header.begin(), header.end(), 
00209                   std::ostream_iterator<int> (*m_stdout, " "));
00210           (*m_stdout) << "|" ;
00211   }
00212   write_event();
00213 
00214 }
00215 
00216 // read_event - read event data in from a file: returns true if successful,
00217 // false otherwise
00218 
00219 bool    ResponseFile::read_event ()
00220 {
00221   if (!good() || (!m_stdin && !m_binin))  return false;
00222 
00223   //clear_all(); // perhaps not, if overlapping
00224   int i = 0;
00225   m_det_read=0;
00226 
00227   // read in all of the event data...
00228   while(1) {
00229     if (!good()) break;
00230     i = next_id(); if(i<0) break;   // if in < 0 (event boundary)
00231     m_det_read ++;
00232 
00233     GlastDetector*  d = GlastDetector::findDetector (i);
00234     if (d) {
00235       (*this) >> *d;
00236     } else {
00237     }
00238     flush_record ();
00239   }
00240 
00241   return true;
00242 }
00243 
00244 // read the individual GlastDetector subclasses:
00245 
00246 void    ResponseFile::backward (CsIDetector& d)
00247 {
00248   if (good()) {
00249     float Le, Re, E;
00250     *this >> Le >> Re >> E;
00251     d.addEnergy (Le, Re, E);
00252   }
00253 }
00254 
00255 void    ResponseFile::backward (MCTruth& d)
00256 {
00257   if (good()) {
00258     unsigned int    id;
00259     if (m_binin)    *m_binin >> id;
00260     if (m_stdin)    *m_stdin >> id;
00261     d.id (id); 
00262     if (d.particle()) {
00263       if (m_binin)    d.particle()->read_data (*m_binin);
00264       if (m_stdin)    d.particle()->read (*m_stdin);
00265     }
00266   }
00267 }
00268 
00269 void    ResponseFile::backward (Scintillator& d)
00270 {
00271   if (good()) {
00272     float e;
00273     *this >> e;
00274     d.addEnergy (e);
00275   }
00276 }
00277 
00278 void    ResponseFile::backward (SiDetector& d)
00279 {
00280   if (good()) {
00281     float   maxE;
00282     *this >> maxE;
00283     d.maxELoss (maxE);
00284 
00285     int count;
00286     *this >> count;
00287 
00288     while ((count--) && (good()))
00289     {
00290       int id;
00291       float e;
00292       bool noise;
00293       *this >> id >> e >> noise;
00294       d.addStrip (id, e, noise);
00295     }
00296   }
00297 }
00298 
00299 // write the individual GlastDetector subclasses
00300 
00301 void    ResponseFile::forward (const CsIDetector& d)
00302 {
00303   if (good()) {
00304     *this << d.Lresp() << d.Rresp() << d.energy();
00305   }
00306 }
00307 
00308 void    ResponseFile::forward (const MCTruth& d)
00309 {
00310   if (good()) {
00311     *this << d.id();
00312     if (m_binout)   d.particle()->write_data (*m_binout);
00313     if (m_stdout)   {
00314       d.particle()->write (*m_stdout);
00315       (*m_stdout) << ' ';
00316     }
00317   }
00318 }
00319 
00320 void    ResponseFile::forward (const Scintillator& d)
00321 {
00322   if (good()) {
00323     *this << d.energy();
00324   }
00325 }
00326 
00327 void    ResponseFile::forward (const SiDetector& d)
00328 {
00329   if (good()) {
00330     *this << d.maxELoss ();;
00331 
00332     int count = d.size ();
00333     *this << count;
00334 
00335     for (SiDetector::const_iterator it = d.begin(); it != d.end(); ++it) {
00336       *this << (*it).index() << (*it).energy() << (*it).noise();
00337     }
00338   }
00339 }
00340 
00341 
00342 

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