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