00001 #include <iostream> 00002 #include "RawFile/EvtIdxHandler.h" 00003 #include <cstring> 00004 #include <cstdlib> 00005 00006 #define DefaultIdxBlockSize 512*1024 00007 00008 int EvtIdxHandler::_nHandler = 0; 00009 EvtIdxHandler* EvtIdxHandler::_instance = 0; 00010 00011 EvtIdxHandler* EvtIdxHandler::instance(const std::vector<std::string>& idxfnames) 00012 { 00013 if ( _instance == 0 ) { 00014 _instance = new EvtIdxHandler(idxfnames); 00015 } 00016 00017 ++_nHandler; 00018 00019 return _instance; 00020 } 00021 00022 void EvtIdxHandler::release() 00023 { 00024 if ( _nHandler > 0 && --_nHandler == 0 ) { 00025 delete _instance; 00026 _instance = 0; 00027 } 00028 } 00029 00030 EvtIdxHandler::EvtIdxHandler(const std::vector<std::string>& idxfnames) 00031 : m_idxPos(0), 00032 m_blockSize(DefaultIdxBlockSize), 00033 m_fnames(idxfnames) 00034 { 00035 m_EIdBlock = new uint32_t[m_blockSize]; 00036 m_PosBlock = new uint32_t[m_blockSize]; 00037 00038 m_curFile = m_fnames.begin(); 00039 init_idx(); 00040 } 00041 00042 EvtIdxHandler::EvtIdxHandler() 00043 : m_totEvt(0), 00044 m_idxPos(-1), 00045 m_blockSize(DefaultIdxBlockSize) 00046 { 00047 m_EIdBlock = new uint32_t[m_blockSize]; 00048 m_PosBlock = new uint32_t[m_blockSize]; 00049 } 00050 00051 EvtIdxHandler::~EvtIdxHandler() 00052 { 00053 delete[] m_EIdBlock; 00054 delete[] m_PosBlock; 00055 } 00056 00057 void EvtIdxHandler::next_file() 00058 { 00059 ++m_curFile; 00060 00061 m_idxPos = 0; 00062 init_idx(); 00063 } 00064 00065 uint32_t EvtIdxHandler::nextPos(int nIgnore) 00066 { 00067 m_idxPos += nIgnore; 00068 return m_PosBlock[m_idxPos++]; 00069 } 00070 00071 uint32_t EvtIdxHandler::findPosById(uint32_t evtId) 00072 { 00073 while ( m_totEvt > m_idxPos ) { 00074 if ( m_EIdBlock[m_idxPos] == evtId ) { 00075 return m_PosBlock[m_idxPos++]; 00076 } 00077 ++m_idxPos; 00078 } 00079 return 0; 00080 } 00081 00082 void EvtIdxHandler::addPos(uint32_t evtId, uint32_t pos) 00083 { 00084 ++m_totEvt; 00085 00086 if ( m_totEvt > m_blockSize ) enlarge_block( m_totEvt ); 00087 00088 m_EIdBlock[m_totEvt-1] = evtId; 00089 m_PosBlock[m_totEvt-1] = pos; 00090 } 00091 00092 void EvtIdxHandler::write(std::string fname) 00093 { 00094 if ( m_idxPos >= 0 ) { 00095 std::cerr << "[RawFile] Writing an EvtIdxHandler for reading !!!" << std::endl; 00096 exit(1); 00097 } 00098 00099 m_fs.open( fname.c_str(), std::ios::out | std::ios::binary ); 00100 00101 uint32_t marker = EvtIdxHandler::IdxFileStartMarker(); 00102 m_fs.write( (char*)&marker, sizeof(marker) ); 00103 m_fs.write( (char*)&m_totEvt, sizeof(m_totEvt) ); 00104 00105 marker = EvtIdxHandler::IdxIdBlockMarker(); 00106 m_fs.write( (char*)&marker, sizeof(marker) ); 00107 m_fs.write( (char*)m_EIdBlock, m_totEvt*sizeof(uint32_t) ); 00108 00109 marker = EvtIdxHandler::IdxPosBlockMarker(); 00110 m_fs.write( (char*)&marker, sizeof(marker) ); 00111 m_fs.write( (char*)m_PosBlock, m_totEvt*sizeof(uint32_t) ); 00112 00113 if ( ! m_fs.good() ) { 00114 std::cerr << "[RawFile] Error writing IDX file: " << fname << std::endl; 00115 exit(1); 00116 } 00117 00118 m_fs.close(); 00119 00120 std::cout << "[RawFile] Written IDX file: " << fname << std::endl; 00121 00122 m_totEvt = 0; 00123 } 00124 00125 void EvtIdxHandler::init_idx() 00126 { 00127 if ( access( m_curFile->c_str(), F_OK ) < 0 ) { 00128 std::cerr << "[RawFile] Invalid IDX file: " << *m_curFile << std::endl; 00129 exit(1); 00130 } 00131 00132 std::cout << "[RawFile] Initialize IDX with file: " << *m_curFile << std::endl; 00133 00134 m_fs.open( m_curFile->c_str(), std::ios::in | std::ios::binary ); 00135 00136 uint32_t marker; 00137 00138 m_fs.read( (char*)&marker, sizeof(marker) ); 00139 if ( marker != EvtIdxHandler::IdxFileStartMarker() ) { 00140 std::cerr << "[RawFile] Wrong IdxFileStartMarker!" << std::endl; 00141 exit(1); 00142 } 00143 00144 m_fs.read( (char*)&m_totEvt, sizeof(m_totEvt) ); 00145 00146 if ( m_totEvt > m_blockSize ) enlarge_block( m_totEvt ); 00147 00148 m_fs.read( (char*)&marker, sizeof(marker) ); 00149 if ( marker != EvtIdxHandler::IdxIdBlockMarker() ) { 00150 std::cerr << "[RawFile] Wrong IdxIdBlockMarker!" << std::endl; 00151 exit(1); 00152 } 00153 00154 m_fs.read( (char*)m_EIdBlock, m_totEvt*sizeof(uint32_t) ); 00155 00156 m_fs.read( (char*)&marker, sizeof(marker) ); 00157 if ( marker != EvtIdxHandler::IdxPosBlockMarker() ) { 00158 std::cerr << "[RawFile] Wrong IdxPosBlockMarker!" << std::endl; 00159 exit(1); 00160 } 00161 00162 m_fs.read( (char*)m_PosBlock, m_totEvt*sizeof(uint32_t) ); 00163 00164 if ( ! m_fs.good() ) { 00165 std::cerr << "[RawFile] Error occured while initialize the IDX file!" << std::endl; 00166 exit(1); 00167 } 00168 00169 m_fs.close(); 00170 } 00171 00172 void EvtIdxHandler::enlarge_block(int min_size) 00173 { 00174 int _initSize = m_blockSize; 00175 while ( min_size > m_blockSize ) { 00176 m_blockSize *= 2; 00177 } 00178 00179 uint32_t* _EIdBlock = new uint32_t[m_blockSize]; 00180 uint32_t* _PosBlock = new uint32_t[m_blockSize]; 00181 00182 memcpy((void*)_EIdBlock, (const void*)m_EIdBlock, sizeof(uint32_t)*_initSize); 00183 memcpy((void*)_PosBlock, (const void*)m_PosBlock, sizeof(uint32_t)*_initSize); 00184 00185 delete[] m_EIdBlock; 00186 delete[] m_PosBlock; 00187 00188 m_EIdBlock = _EIdBlock; 00189 m_PosBlock = _PosBlock; 00190 }