| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

RRawStream.cc

Go to the documentation of this file.
00001 /*
00002  *
00003  *   RRawStream, input stream for rraw type raw data.
00004  *   It provides some similar operations like ifstream to handle root TTree.
00005  *
00006  *   Zhe Wang
00007  *   Feb. 10, 2010 at BNL
00008  *
00009  */
00010 
00011 #include "RawData/RRawStream.h"
00012 #include <cstring>
00013 #include <stdlib.h>
00014 
00015 static void sanity()
00016 {
00017   if( sizeof(Int32) != 4 ) {
00018     cerr<<"RawStream:: error: Need a 32-bits type to hold each line of raw data"<<endl;
00019     cerr<<"RawStream:: Redefine type Int32"<<endl;
00020     abort();
00021   }
00022 }
00023 
00024 RRawStream::RRawStream():
00025   m_file(0),m_tree(0),m_size(0),
00026   m_currentEntry(-1),m_currentPosi(0),m_entryEnd(true)
00027 {
00028   sanity();
00029 }
00030 
00031 RRawStream::RRawStream( const char* filename ):
00032   m_file(0),m_tree(0),m_size(0),
00033   m_currentEntry(-1),m_currentPosi(0),m_entryEnd(true)
00034 {
00035   sanity();
00036   this->open(filename);
00037 }
00038 
00039 RRawStream::~RRawStream()
00040 {
00041   this->close();
00042 }
00043 
00044 void RRawStream::close ( )
00045 {
00046   if(!m_file) return;
00047 
00048   m_file->Close();
00049   delete m_file;
00050   m_file=0;
00051 
00052 }
00053 
00054 void RRawStream::open ( const char * filename )
00055 {
00056   // open a file
00057   m_file = TFile::Open( filename, "READ" );
00058   if(!m_file)  {
00059     cout<<"RRawStream::Failed to open "<<filename<<endl;
00060     abort();
00061   }
00062   if( !(m_file->IsOpen()) )  {
00063     cout<<"RRawStream::Failed to open "<<filename<<endl;
00064     abort();
00065   }
00066 
00067   // get the tree
00068   m_tree = (TTree*)m_file->Get("RRawTree");
00069   if(!m_tree)  {
00070     cout<<"RRawStream::Failed to get TTree RRawTree"<<endl;
00071     abort();
00072   }
00073 
00074   // set branch address
00075   m_tree->SetBranchAddress("size",&m_size);
00076   m_tree->SetBranchAddress("buffer",m_buffer);
00077     
00078   // reset current entry number to -1
00079   m_currentEntry=-1;
00080         
00081   // reset reading status of current entry
00082   m_currentPosi=0;
00083   m_entryEnd=true;
00084 }
00085 
00086 bool RRawStream::is_open ( ) const
00087 {
00088   if(!m_file) 
00089     return false;
00090   else
00091     return m_file->IsOpen();
00092 }
00093 
00094 bool RRawStream::good ( ) const
00095 {
00096   if (!is_open()) return false;
00097   return !m_file->IsZombie();
00098 }
00099 
00100 // Equivalant to fail() of ifstream
00101 bool RRawStream::operator ! ( ) const
00102 {
00103   if (!this->is_open()) return true;
00104   return !good();
00105 }
00106 
00107 RRawStream& RRawStream::read ( char* s, int n )
00108 {
00109   // Need to handle the case of reaching to the end of one entry
00110   if(m_entryEnd) {
00111     m_currentEntry++;
00112     m_tree->GetEntry(m_currentEntry);
00113     m_entryEnd=false;
00114     m_currentPosi = 0;
00115 
00116     /*
00117     cout<<"RRawStream::New Entry "<<m_size<<endl;
00118     for(int i=0;i<m_size;i++) {
00119       cout<<(unsigned int)m_buffer[i]<<" ";
00120     }
00121     cout<<endl;
00122     */
00123   }
00124 
00125   if( m_currentPosi+n <= m_size * 4 )  {
00126     memcpy(s, (char*)(m_buffer) + m_currentPosi, n);
00127     m_currentPosi+=n;
00128   }  else  {
00129     cout<<"RRawStream::Asking data over one root entry boundary"<<endl;
00130     abort();
00131   }
00132 
00133   // Mark the end
00134   if(m_currentPosi == m_size * 4 ) {
00135     m_entryEnd = true;
00136   }
00137 
00138   return *this;
00139 }
00140 
00141 RRawStream& RRawStream::ignore ( int n /* 1 */ )
00142 {
00143   // The same as read(...), just without memcpy
00144   
00145   // Need to handle the case of reaching to the end of one entry
00146   if(m_entryEnd) {
00147     m_currentEntry++;
00148     m_tree->GetEntry(m_currentEntry);
00149     m_entryEnd=false;
00150     m_currentPosi = 0;
00151 
00152     /*
00153     cout<<"RRawStream::New Entry "<<m_size<<endl;
00154     for(int i=0;i<m_size;i++) {
00155       cout<<(unsigned int)m_buffer[i]<<" ";
00156     }
00157     cout<<endl;
00158     */
00159   }
00160 
00161   m_currentPosi+=n;
00162 
00163   // Mark the end
00164   if(m_currentPosi == m_size * 4 ) {
00165     m_entryEnd = true;
00166   }
00167 
00168   return *this;
00169 }
00170 
00171 
00172 // Local Variables: **
00173 // c-basic-offset:2 **
00174 // indent-tabs-mode:nil **
00175 // End: **
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:07:27 2011 for RawData by doxygen 1.4.7