00001
00002
00003
00004
00005
00006
00007
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
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
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
00075 m_tree->SetBranchAddress("size",&m_size);
00076 m_tree->SetBranchAddress("buffer",m_buffer);
00077
00078
00079 m_currentEntry=-1;
00080
00081
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
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
00110 if(m_entryEnd) {
00111 m_currentEntry++;
00112 m_tree->GetEntry(m_currentEntry);
00113 m_entryEnd=false;
00114 m_currentPosi = 0;
00115
00116
00117
00118
00119
00120
00121
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
00134 if(m_currentPosi == m_size * 4 ) {
00135 m_entryEnd = true;
00136 }
00137
00138 return *this;
00139 }
00140
00141 RRawStream& RRawStream::ignore ( int n )
00142 {
00143
00144
00145
00146 if(m_entryEnd) {
00147 m_currentEntry++;
00148 m_tree->GetEntry(m_currentEntry);
00149 m_entryEnd=false;
00150 m_currentPosi = 0;
00151
00152
00153
00154
00155
00156
00157
00158
00159 }
00160
00161 m_currentPosi+=n;
00162
00163
00164 if(m_currentPosi == m_size * 4 ) {
00165 m_entryEnd = true;
00166 }
00167
00168 return *this;
00169 }
00170
00171
00172
00173
00174
00175