ana_processor.cc

Go to the documentation of this file.
00001 #ifndef ANA_PROCESSOR_CC
00002 #define ANA_PROCESSOR_CC
00003 
00004 #include "ana_processor.hh"
00005 
00006 ana_processor::ana_processor(){
00007   _name="ana_processor";
00008   _ofile_name="";
00009   //_mode=storage_manager::READ; // default I/O mode is to produce no output
00010   _storage=storage_manager::get();
00011   _fout=0;
00012   reset();
00013 }
00014 
00015 void ana_processor::set_verbosity(MSG::Level level){
00016   decoder_base::set_verbosity(level);
00017   if(_storage)
00018     _storage->set_verbosity(level);
00019   for(std::vector<ana_base*>::const_iterator iter(_analyzers.begin());
00020       iter!=_analyzers.end();
00021       ++iter){
00022     (*iter)->set_verbosity(level);
00023   }
00024 }
00025 
00026 void ana_processor::reset(){
00027   
00028   if(_verbosity[MSG::DEBUG])
00029     Message::send(MSG::DEBUG,__PRETTY_FUNCTION__,"called...");
00030 
00031   if(_storage)
00032     _storage->reset();
00033 
00034   if(_fout){
00035     _fout->Close();
00036     _fout=0;
00037   }
00038 
00039   _analyzers.clear();
00040   _ana_status.clear();
00041   _nevents=0;
00042   _index=0;
00043 
00044   _process=INIT;
00045 }
00046 
00047 bool ana_processor::initialize(){
00048 
00049   set_verbosity(_verbosity_level);
00050 
00051   if(_verbosity[MSG::DEBUG])
00052     Message::send(MSG::DEBUG,__PRETTY_FUNCTION__,"called...");
00053 
00054   if(_process!=INIT){
00055     Message::send(MSG::ERROR,__FUNCTION__,
00056           "Logic error: the function should not be called.");
00057     return false;
00058   }
00059 
00060   _fout=TFile::Open(_ofile_name.c_str(),"RECREATE");
00061 
00062   if(!_fout){
00063 
00064     sprintf(_buf,"Failed to open an output file: %s",_ofile_name.c_str());
00065 
00066     Message::send(MSG::ERROR,__FUNCTION__,_buf);
00067 
00068     return false;
00069 
00070   }
00071 
00072   //_storage->set_io_mode(storage_manager::READ);
00073   if(!_storage->open()) {
00074 
00075     Message::send(MSG::ERROR,__FUNCTION__,"File I/O failure...");
00076 
00077     return false;
00078 
00079   }
00080 
00081   if(_ofile_name.size()==0){
00082 
00083     Message::send(MSG::ERROR,__FUNCTION__,
00084           "Must provide an output filename!");
00085     return false;
00086   }
00087 
00088   bool status = true;
00089   _fout->cd();
00090   for(std::vector<ana_base*>::iterator iter(_analyzers.begin());
00091       iter!=_analyzers.end();
00092       ++iter) {
00093 
00094     (*iter)->set_output_file(_fout);
00095 
00096     if(!((*iter)->initialize())){
00097 
00098       Message::send(MSG::ERROR,__PRETTY_FUNCTION__,
00099             Form("Failed to initialize: %s",(*iter)->class_name().c_str()));
00100 
00101       status = false;
00102     }
00103 
00104   }    
00105   _process=READY;
00106   _index=0;
00107   _nevents=0;
00108   return status;
00109 }
00110 
00111 bool ana_processor::process_event(UInt_t index){
00112 
00113   if(_process==INIT) {
00114     
00115     if(!initialize()) {
00116       Message::send(MSG::ERROR,__FUNCTION__,"Aborting.");
00117       return false;
00118     }
00119     else
00120       _process=PROCESSING;
00121   }
00122 
00123   if(_process==READY)
00124     _process=PROCESSING;
00125 
00126   bool event_found = index ? _storage->go_to(index) : _storage->next_event();
00127 
00128   if(event_found){
00129     
00130     for(std::vector<ana_base*>::iterator iter(_analyzers.begin());
00131     iter!=_analyzers.end();
00132     ++iter)
00133       _ana_status[(*iter)]=(*iter)->analyze(_storage);
00134 
00135     _index++;
00136     _nevents++;
00137     
00138     return true;
00139 
00140   }
00141   else
00142     return finalize();
00143 }
00144 
00145 bool ana_processor::run(UInt_t start_index, UInt_t nevents){
00146 
00147   if(_verbosity[MSG::DEBUG])
00148     Message::send(MSG::DEBUG,__PRETTY_FUNCTION__,"called...");
00149 
00150   bool status=true;
00151 
00152   if(_process==INIT) status = initialize();
00153 
00154   if(!status){
00155 
00156     Message::send(MSG::ERROR,__PRETTY_FUNCTION__,"Aborting.");
00157 
00158     return false;
00159   }
00160 
00161   _index=start_index;
00162 
00163   if(start_index)
00164     _storage->go_to(start_index);
00165 
00166   if(!nevents)
00167     nevents=_storage->get_entries();
00168   if(nevents > (_storage->get_entries() - start_index))
00169     nevents=_storage->get_entries() - start_index;
00170 
00171   sprintf(_buf,"Processing %d events...",nevents);
00172   Message::send(MSG::NORMAL,__FUNCTION__,_buf);
00173 
00174   int ten_percent_ctr=0;
00175   
00176   while(status){
00177 
00178     status=process_event();
00179 
00180     if(nevents && nevents < _nevents){
00181       Message::send(MSG::NORMAL,__FUNCTION__,Form("Processed %d/%d events! Aborting...",_nevents,nevents));
00182       finalize();
00183       break;
00184     }
00185 
00186     if(nevents > 10 && (_nevents >= ten_percent_ctr * nevents/10)) {
00187       
00188       sprintf(_buf," ... %3d%% done ...",ten_percent_ctr*10);
00189       Message::send(MSG::NORMAL,__FUNCTION__,_buf);
00190       ten_percent_ctr++;
00191     }
00192 
00193     if(_process!=PROCESSING) break;
00194     
00195   }
00196   
00197   return status;
00198   
00199 }
00200 
00201 bool ana_processor::finalize() {
00202 
00203   if(_verbosity[MSG::DEBUG])
00204     Message::send(MSG::DEBUG,__PRETTY_FUNCTION__,"called...");
00205 
00206   if(_process!=PROCESSING && _process!=READY) {
00207     Message::send(MSG::ERROR,__FUNCTION__,
00208           "Logic error: the function should not be called.");
00209     return false;
00210   }
00211 
00212   bool status=true;
00213   for(std::vector<ana_base*>::iterator iter(_analyzers.begin());
00214       iter!=_analyzers.end();
00215       ++iter) {
00216 
00217     _ana_status[(*iter)]=_ana_status[(*iter)] && (*iter)->finalize();
00218 
00219     status = status && _ana_status[(*iter)];
00220   }
00221 
00222   _process=FINISHED;
00223   reset();
00224   return status;  
00225 }
00226 
00227 bool ana_processor::get_ana_status(ana_base* ptr) const{
00228 
00229   std::map<ana_base*,bool>::const_iterator iter(_ana_status.find(ptr));
00230   if(iter==_ana_status.end()) return false;
00231 
00232   else return (*iter).second;
00233   
00234 }
00235 
00236 #endif

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