decoder_manager.cc

Go to the documentation of this file.
00001 #ifndef DECODER_MANAGER_CC
00002 #define DECODER_MANAGER_CC
00003 
00004 #include "decoder_manager.hh"
00005 
00006 decoder_manager::decoder_manager()
00007   : decoder_base(),
00008     _fin(FORMAT::ASCII)
00009 {
00010   _input_file="";
00011   _debug_mode=false;
00012   _fin=bin_io_handler();
00013 
00014   _read_block_size = 0;
00015   _read_by_block   = false;
00016 
00017   _storage=storage_manager::get();
00018   _storage->set_io_mode(storage_manager::WRITE);
00019 
00020   //if((_storage->input_filename()).size()==0)
00021   //  _storage->set_in_filename("out.root");
00022 
00023   _decoder=0;
00024 
00025   set_verbosity(MSG::NORMAL);
00026 }
00027 
00028 
00029 void decoder_manager::reset() {
00030   if(_storage->is_open()) {
00031     _storage->close();
00032     _storage->reset();
00033   }
00034   _fin.reset();
00035   _bin_files.clear();
00036 }
00037 
00038 bool decoder_manager::open_file()
00039 {
00040   return _fin.open();
00041 }
00042 
00043 
00044 bool decoder_manager::initialize()
00045 {
00046   if(_verbosity[MSG::DEBUG])
00047     Message::send(MSG::DEBUG,__FUNCTION__," begins...");
00048   if(!_decoder){
00049     Message::send(MSG::ERROR,__FUNCTION__,"Algorithm not attached. Aborting.");
00050     return false;
00051   }
00052   if(!_storage){
00053     Message::send(MSG::ERROR,__FUNCTION__,"Stoarge I/O pointer is empty.");
00054     return false;
00055   }
00056 
00057   _decoder->set_debug_mode(_debug_mode);
00058   _decoder->set_verbosity(_verbosity_level);
00059   _storage->set_verbosity(_verbosity_level);
00060   _fin.set_verbosity(_verbosity_level);
00061 
00062   if(_bin_files.size())
00063     _bin_file_iter = _bin_files.begin();
00064 
00065   _fin.set_filename((*_bin_file_iter).c_str());
00066 
00067   bool status=true;
00068   // Check if a file can be opened
00069   if(_fin.is_open())
00070     Message::send(MSG::WARNING,"Reading alrady-opened file contents!");
00071   else if(!_fin.open()) {
00072     Message::send(MSG::ERROR,__FUNCTION__,"Failed file I/O...");
00073     status=false;
00074   }
00075 
00076   if(!_storage->is_open())
00077     _storage->open();
00078 
00079   if(!_storage->is_ready_io()) {
00080     Message::send(MSG::ERROR,__FUNCTION__,
00081           "Error in data output stream preparation.");
00082     status=false;
00083   }
00084 
00085  _decoder->set_storage_ptr(_storage);
00086 
00087   for(std::vector<ana_base*>::iterator iter(_analyzers.begin());
00088       iter!=_analyzers.end();
00089       ++iter)
00090     status = (status && (*iter)->initialize());
00091 
00092   return status;
00093 }
00094 
00095 bool decoder_manager::decode() {
00096 
00097   if(_verbosity[MSG::DEBUG])
00098     Message::send(MSG::DEBUG,__FUNCTION__," begins...");
00099 
00100   bool status=true;
00101   PMT::word_t word = (_read_by_block) ? _fin.read_multi_word(_read_block_size) : _fin.read_word();
00102   UInt_t ctr=0;
00103   time_t watch;
00104   while(status) {
00105 
00106     if(_fin.eof()){
00107 
00108       _bin_file_iter++;
00109       if(_bin_file_iter == _bin_files.end()) break;
00110       _fin.close();
00111       _fin.set_filename((*_bin_file_iter).c_str());
00112 
00113       if(!_fin.open()) {
00114     Message::send(MSG::ERROR,__FUNCTION__,"Failed file I/O...");
00115     status = false;
00116     break;
00117       }
00118       word = (_read_by_block) ? _fin.read_multi_word(_read_block_size) : _fin.read_word();
00119     }
00120 
00121     status=_decoder->process_word(word);
00122 
00123     /*
00124     if(status) {
00125       for(std::vector<ana_base*>::iterator iter(_analyzers.begin());
00126       iter!=_analyzers.end();
00127         ++iter)
00128     (*iter)->analyze(_storage);
00129     }
00130     */
00131 
00132     if(!status){
00133       //if(_decoder->backtrace_mode())
00134       //_decoder->backtrace();
00135       if(_debug_mode){
00136     Message::send(MSG::ERROR,__FUNCTION__,"Process status failure ... but continue since DEBUG mode!");
00137     status=true;
00138       }
00139     }
00140 
00141     word = (_read_by_block) ? _fin.read_multi_word(_read_block_size) : _fin.read_word();
00142     if(_storage->get_index()==(ctr*2000)){
00143       time(&watch);
00144       sprintf(_buf,"  ... processed %-6d events : %s",ctr*2000,ctime(&watch));
00145       Message::send(MSG::NORMAL,__FUNCTION__,_buf);
00146       ctr+=1;
00147     }
00148   }
00149   
00150   if(!status && !_debug_mode){
00151 
00152     Message::send(MSG::ERROR,__FUNCTION__,Form("Event loop terminated. Stored: %d events",_storage->get_entries()));
00153 
00154   }else if(!(_decoder->is_event_empty())){
00155 
00156     Message::send(MSG::WARNING,__FUNCTION__,"Last event not stored by algorithm. Missing end-of-event word??");
00157 
00158     if(_decoder->check_event_quality()){
00159 
00160       Message::send(MSG::WARNING,__FUNCTION__,"Last event checksum agreed. Saving on file...");
00161 
00162       _storage->next_event();
00163 
00164     }else{
00165 
00166       Message::send(MSG::WARNING,__FUNCTION__,"Skip saving the last event...");
00167 
00168       status=false;
00169 
00170     }    
00171   }
00172     
00173   return status;
00174   
00175 }
00176 
00177 bool decoder_manager::finalize() {
00178 
00179   if(_verbosity[MSG::DEBUG])
00180     Message::send(MSG::DEBUG,__FUNCTION__," begins...");  
00181 
00182   for(std::vector<ana_base*>::iterator iter(_analyzers.begin());
00183       iter!=_analyzers.end();
00184       ++iter)
00185     (*iter)->finalize();
00186 
00187   _storage->close();
00188   _fin.close();
00189 
00190   _decoder->reset();
00191   
00192   return true;
00193 }
00194 
00195 bool decoder_manager::run() {
00196 
00197   if(_verbosity[MSG::DEBUG])
00198     Message::send(MSG::DEBUG,__FUNCTION__," begins...");
00199 
00200   bool status=true;
00201   if(initialize())
00202     status=decode();
00203   return (finalize() && status);
00204   
00205 }
00206 #endif
00207 

Generated on Mon Apr 7 15:35:12 2014 for MyProject by  doxygen 1.4.7