Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Related Pages

AnalysisWorkerGroup.cpp

00001 #include "AnalysisWorkerGroup.h"
00002 #include "AnalysisAlgorithm.h"
00003 #include "AnalysisAlgorithmMap.h"
00004 #include "AnalysisService.h"
00005 #include "DcsInterface.h"
00006 
00007 #include "Sct/IS/IOManagerIS.h"
00008 #include "Sct/ISProxy/IOManagerISProxy.h"
00009 #include "Sct/IS/IONameIS.h"
00010 #include "Sct/StdExceptionWrapper.h"
00011 #include "Sct/SctNames.h"
00012 #include "Sct/UniqueID.h"
00013 #include "SctData/TestResult.h"
00014 #include "SctData/DcsData.h"
00015 
00016 #include "CalibrationController/IS/TestData.h"
00017 #include <boost/timer.hpp>
00018 
00019 using namespace SctService;
00020 using namespace boost;
00021 using namespace std;
00022 using namespace Sct;
00023 using namespace Sct::IS;
00024 using namespace SctData;
00025 
00026 namespace SctAnalysis {
00027 
00028 //TestAlgs methods
00029 AnalysisWorkerGroup::TestAlgs::TestAlgs(boost::shared_ptr<const TestData> testdata) : m_testdata(testdata){
00030   bool dcsProblemsReported=false;
00031   for (unsigned imodule=0; imodule<m_testdata->modules_size; ++imodule){
00032     // make dcs snapshots of all modules in test
00033     try{
00034       const std::string& module = m_testdata->modules[imodule];
00035       boost::shared_ptr<AnalysisAlgorithm> alg = findAlgorithm(module);
00036       alg->checkTestResultExists();
00037 
00038       // try to get DcsInformation for this module
00039       try{
00040     if (!AnalysisService::instance().getDcsInterface().getConfigurationInterface().empty()) {  
00041       shared_ptr<SctData::DcsData> dcs (AnalysisService::instance().getDcsInterface().getData(module) );
00042       
00043       alg->getTestResult()->setDcsData(dcs);
00044     }
00045       }catch(Sct::Throwable& e){
00046     if (!dcsProblemsReported) {
00047       e.sendToMrs(MRS_DIAGNOSTIC);
00048       dcsProblemsReported=true;
00049     }
00050       }catch(std::exception& e){
00051     if (!dcsProblemsReported) {
00052       StdExceptionWrapper(e,__FILE__,__LINE__).sendToMrs(MRS_DIAGNOSTIC);
00053       dcsProblemsReported=true;
00054     }
00055       }
00056     }catch(Sct::Throwable& e){
00057       e.sendToMrs();
00058     }
00059   }
00060 }
00061 
00062 AnalysisWorkerGroup::TestAlgs::~TestAlgs() {
00063     boost::recursive_mutex::scoped_lock lock(m_access);
00064 
00065     //We need to explicitly delete the list here so the lock is in force
00066     //Note that this just removes the pointer..actual deletion will only occur
00067     //if noone else has grabbed a reference to the object
00068     m_algorithms.clear();
00069 }
00070 
00071 void AnalysisWorkerGroup::TestAlgs::removeAlgorithm(shared_ptr<AnalysisAlgorithm> alg) {
00072     boost::recursive_mutex::scoped_lock lock(m_access);
00073     list<shared_ptr<AnalysisAlgorithm> >::iterator it =
00074         find(m_algorithms.begin(), m_algorithms.end(), alg);
00075 
00076     if ( it != m_algorithms.end() )
00077         m_algorithms.erase(it);
00078 }
00079 
00080 ostream& AnalysisWorkerGroup::TestAlgs::printStatus(ostream& os) const throw() {
00081     boost::recursive_mutex::scoped_lock lock(m_access);
00082     os << "   Test name: "  << getTest().testName 
00083        << ", run number = " << getTest().runNumber
00084        << ", first scan number = " << getTest().startScanNumber
00085        << ", nscans = " << getTest().nScans 
00086        <<  endl;
00087 
00088     return os;
00089 }
00090 
00091 shared_ptr<AnalysisAlgorithm> AnalysisWorkerGroup::TestAlgs::findAlgorithm(const string& modulename) {
00092     boost::recursive_mutex::scoped_lock lock(m_access);
00093     shared_ptr<AnalysisAlgorithm> alg;
00094     for (list<shared_ptr<AnalysisAlgorithm> >::const_iterator it=m_algorithms.begin();
00095             it != m_algorithms.end();
00096             ++it) {
00097         if ( (*it)->getModuleName()==modulename ) {
00098             alg=(*it);
00099             break;
00100         }
00101     }
00102     if (!alg) {
00103         alg=addAlgorithm(modulename);
00104     }
00105     return alg;
00106 }
00107 
00108 shared_ptr<AnalysisAlgorithm> AnalysisWorkerGroup::TestAlgs::addAlgorithm(const string& modulename) {
00109     boost::recursive_mutex::scoped_lock lock(m_access);
00110     shared_ptr<AnalysisAlgorithm> alg = AnalysisAlgorithmMap::instance().getAlgorithm(getTest(), modulename);
00111     m_algorithms.push_back(alg);
00112     return alg;
00113 }
00114 
00115 //AnalysisWorkerGroup methods
00116 
00117 
00118 ostream&  AnalysisWorkerGroup::printStatus(ostream& os) const throw() {
00119     //cout << "Getting tests lock" << endl;
00120     boost::recursive_mutex::scoped_lock lock(m_tests_access);
00121     //cout << "got tests lock" << endl;
00122     os << "\t === Analysis Service ===\n" << endl;
00123     os << "AnalysisWorkerGroup tests:" << endl;
00124     unsigned ntest=0;
00125     for (list<shared_ptr<TestAlgs> >::const_iterator it = m_tests.begin();
00126             it != m_tests.end() ; ++it ) {
00127         (*it)->printStatus(os);
00128         os << "-------------------------------------------------"<<endl;
00129         ++ntest;
00130     }
00131     os << endl << "Total number of tests = " << ntest << endl;
00132     //cout << "done ostreaming" << endl;
00133     return os;
00134 }
00135 
00136 void AnalysisWorkerGroup::addTest(shared_ptr<const TestData> testdata) {
00137     boost::recursive_mutex::scoped_lock lock(m_tests_access);
00138     m_tests.push_back(shared_ptr<TestAlgs>(new TestAlgs(testdata) ) );
00139 }
00140 
00141 void AnalysisWorkerGroup::removeTestsUpTo(shared_ptr<const TestData> testdata) {
00142     boost::recursive_mutex::scoped_lock lock(m_tests_access);
00143     list<shared_ptr<TestAlgs> >::iterator it = m_tests.begin();
00144 
00145     while( it != m_tests.end() ) { // no ++it here since we need to delete it in the function!
00146         if ((*it)->getTest().runNumber==testdata->runNumber
00147                 && (*it)->getTest().startScanNumber<=testdata->startScanNumber) {
00148             
00149       cout << "Deleting old test for run "
00150            << (*it)->getTest().runNumber << ", start scan "
00151            << (*it)->getTest().startScanNumber
00152            << endl;
00153             it=m_tests.erase(it);   // erase returns ++it
00154         } else {
00155             ++it;                   // increment ourselves
00156         }
00157     }
00158 }
00159 
00160 shared_ptr<AnalysisWorkerGroup::TestAlgs> AnalysisWorkerGroup::findTest(const TestData& testdata) const throw() {
00161     return findTest(testdata.runNumber, testdata.startScanNumber);
00162 }
00163 
00164 shared_ptr<AnalysisWorkerGroup::TestAlgs> AnalysisWorkerGroup::findTest(const unsigned long runno, const unsigned long scanno) const throw() {
00165     boost::recursive_mutex::scoped_lock lock(m_tests_access);
00166     shared_ptr<AnalysisWorkerGroup::TestAlgs> talgs;
00167     for (list<shared_ptr<TestAlgs> >::const_iterator it = m_tests.begin();
00168             it != m_tests.end();
00169             ++it ) {
00170         const TestData& td=(*it)->getTest();
00171         if ( runno == td.runNumber &&
00172                 scanno >= td.startScanNumber &&
00173                 scanno < td.startScanNumber + td.nScans) {
00174             talgs=*it;
00175             //      cout << "found test for run " << runno<< ", scan " << scanno << endl;
00176         }
00177     }
00178     return talgs;
00179 }
00180 
00181 
00182 
00183 
00184 void AnalysisWorkerGroup::purge() throw() {
00185     //Stop the workers from trying to get on with things
00186     this->setPaused(true);
00187     while (paused() != nWorkers()) sleep(1);
00188     
00189     // Clear the queue.
00190     shared_ptr<IOName> s;
00191     do {s=pop(); } while(s);
00192     boost::recursive_mutex::scoped_lock lock(m_tests_access);
00193 
00194     // Clear the test data.
00195     cout << "Clearing tests ... " << endl;
00196     m_tests.clear();
00197     cout << " ... done clearing tests" << endl;
00198     this->setPaused(false);    
00199 }
00200 
00201 void AnalysisWorkerGroup::work(shared_ptr<IOName> name) throw() {
00202     using namespace SctData;
00203 
00204     try {
00206       if (name->getIOName().find("TestData") != string::npos){
00207     boost::shared_ptr<TestData> testdata( new TestData() );
00208     ISInfoDictionary& id = Sct::SctNames::getISDictionary();
00209     ISInfo::Status result = id.findValue(name->getIOName().c_str(), *testdata);
00210     if (result != ISInfo::Success) {
00211       throw IsException(result,std::string("Error reading from IS server.  Couldn't read: ")
00212                 + name->getIOName(), __FILE__, __LINE__);   
00213     }
00214     if (findTest(*testdata).get() == 0 ) {
00215       addTest(testdata);
00216     } else {
00217       throw Sct::IoException(std::string("AnalysisService::TestCallback Test already exists: ")
00218                  +name->getIOName(), __FILE__, __LINE__);
00219     }
00220     return;
00221       }
00222     
00223       // (2) Other case is callback for scan data 
00224     timer t;
00225     UniqueID id (name->getUniqueID());
00226         const unsigned runno = id.getRunNumber();
00227         const unsigned scanno = id.getScanNumber();
00228         const string modulename= id.getModule();
00229 
00230         //      cout << "run="<< runno << ", scan="<< scanno << ", modulename="<<modulename
00231         //       << ", classname="<<isname.getClassName()<<endl;
00232 
00233         shared_ptr<AnalysisWorkerGroup::TestAlgs> talgs = findTest(runno, scanno);
00234 
00235         //      cout << "Found test" << endl;
00236 
00237         if (!talgs) {
00238            std::ostringstream oss; oss << "AnalysisWorker::work. No test found for run " 
00239                                << runno << " scan" << scanno;
00240             throw IllegalStateError(oss.str(), __FILE__, __LINE__);
00241         }
00242         // Get a testresult. Add a new testresult if one dosen't exist for this module
00243         shared_ptr<AnalysisAlgorithm> algorithm = talgs->findAlgorithm(modulename);
00244 
00245         //      cout << "algorithm.get()=" << algorithm.get() << endl;
00246 
00247         bool isfit=(name->getClassName()=="SctData::FitScanResult");
00248 
00249         // lock the approprate result and do your stuff, analysis-algorithm.
00250         boost::recursive_mutex::scoped_lock lock (algorithm->getMutex());
00251         if (isfit) {
00252             //      cout << "got fit from queue" << endl;
00253             algorithm->addFitScanResult(name);
00254         } else {
00255             //      cout << "got raw from queue" << endl;
00256             algorithm->addRawScanResult(name);
00257         }
00258     if (algorithm->canAnalyze()) {
00259         if (algorithm->isDone()) throw IllegalStateError("AnalysisWorker::work.  Algorithm has additional, unnecessary Scan - analysis already done", __FILE__, __LINE__);
00260         algorithm->checkTestResultExists();
00261         algorithm->addOverheadTime(t.elapsed());
00262         
00263         t.restart();
00264         algorithm->loadData();
00265         algorithm->addIOTime(t.elapsed());
00266         
00267         t.restart();
00268         algorithm->analyze();
00269         algorithm->addAnalysisTime(t.elapsed());
00270         
00271         t.restart();
00272         algorithm->finish();
00273         algorithm->addIOTime(t.elapsed());
00274         talgs->removeAlgorithm(algorithm);
00275     } else {
00276         algorithm->addOverheadTime(t.elapsed());
00277     }
00278     
00279     } catch (InvalidArgumentError& e) {
00280     e.sendToMrs();
00281     } catch(Throwable& e) {
00282         e.sendToMrs(MRS_ERROR);
00283     } catch(std::exception& e) {
00284     StdExceptionWrapper sew(e);
00285         sew.sendToMrs(MRS_ERROR);        
00286     } catch(...) {
00287     Error e("uncaught unknown exception", __FILE__, __LINE__);
00288     e.sendToMrs(MRS_ERROR);
00289     }
00290 }
00291 
00292 
00293 }// end of namespace SctAnalysis

Generated on Thu Feb 3 17:37:33 2005 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5