00001 #include "ReAnalyse.h"
00002 #include "AnalysisAlgorithmMap.h"
00003 #include <boost/lexical_cast.hpp>
00004 #include "CalibrationController/IS/TestData.h"
00005 using namespace std;
00006 using boost::lexical_cast;
00007
00008 namespace SctAnalysis{
00009
00010 void ReAnalyse::printHelp() const{
00011 cerr << "Usage: ReAnalyse -t <test> -m <module1> <module2> -r <run> -s <scan1> <scan2> [-v] [-n] ..." << endl;
00012 cerr << "-t <test_name> name of test to analyse (one only)" << endl;
00013 cerr << "-m <module1> [<module2> ...] list of modules to analyse" << endl;
00014 cerr << "-r <run> set run number (can be used more than once)" << endl;
00015 cerr << "-s <scan1> [<scan2> ...] list of scans for current run number" << endl;
00016 cerr << "-p <testpoint1> [<testpoint2> ... ] list of values of test points" << endl;
00017 cerr << "-v verbose mode - print debug information";
00018 cerr << "-n dont actually do the analysis, just parse the arguments and print" << endl;
00019 cerr << "--help print this help message" << endl;
00020 throw Sct::IoException("Print Help", __FILE__, __LINE__);
00021 }
00022
00023 ReAnalyse::ReAnalyse(int argc, char** argv){
00024 if (argc<2) printHelp();
00025 verbosity=0;
00026 m_option_n=false;
00027 string lastswitch;
00028 bool haverun=false;
00029 unsigned long lastrun=0;
00030 for (int i=1; i<argc; ++i) {
00031
00032 string current=argv[i];
00033 if (current == "--help") {
00034 printHelp();
00035 } else if (current=="-v") {
00036 verbosity=1;
00037 } else if (current=="-n") {
00038 verbosity=1;
00039 m_option_n=true;
00040 } else if (current=="-t") {
00041 if (m_testName!=""){
00042 cerr << "Only one -t argument allowed: already have " << m_testName << endl;
00043 printHelp();
00044 }
00045 if (i+1 >= argc) {
00046 cerr << "Invalid arguments for option -t" << endl;
00047 printHelp();
00048 }
00049 m_testName=argv[i+1];
00050 i++;
00051 } else if (current=="-m" || current=="-r" || current=="-s" || current =="-p") {
00052 if (i+1 >= argc) {
00053 cerr << "Invalid arguments for option " << current << endl;
00054 printHelp();
00055 }else{
00056 lastswitch=current;
00057 }
00058 }else if (lastswitch=="-m"){
00059 m_modules.push_back(current);
00060 }else if (lastswitch=="-p"){
00061 try {
00062 m_testPoints.push_back(lexical_cast<double>(current));
00063 }catch(std::exception& e){
00064 cerr << "Can't interpret ``" << current << "'' as a test point value" << endl;
00065 printHelp();
00066 }
00067 }else if (lastswitch=="-r"){
00068 try{
00069 lastrun=lexical_cast<long unsigned>(current);
00070 haverun=true;
00071 }catch(std::exception& e){
00072 cerr << "Can't interpret ``" << current << "'' as a run number" << endl;
00073 printHelp();
00074 }
00075 }else if (lastswitch=="-s"){
00076 if (!haverun) {
00077 cerr << "Please set run number before listing scans" << endl;
00078 printHelp();
00079 }
00080 try{
00081 long unsigned scan=lexical_cast<long unsigned>(current);
00082 m_runScan.push_back( make_pair(lastrun, scan) );
00083 }catch (std::exception& e){
00084 cerr << "Can't interpret ``" << current << "'' as a scan number" << endl;
00085 printHelp();
00086 }
00087 }else{
00088 cerr << "I dont understand ``" << current << "''" << endl;
00089 printHelp();
00090 }
00091 }
00092 if (m_modules.empty()) {
00093 cerr << "No modules listed" << endl;
00094 printHelp();
00095 }
00096 if (m_runScan.empty()){
00097 cerr << "No scans listed" << endl;
00098 printHelp();
00099 }
00100 if (m_testPoints.size()!=m_runScan.size()){
00101 cerr << "You should give exactly one test point per scan!" << endl;
00102 printHelp();
00103 }
00104 }
00105
00106 void ReAnalyse::print(std::ostream& os) const{
00107 os << "ReAnalyse test named: " << m_testName << endl;
00108 os << "Modules:" << endl;
00109 for (list<string>::const_iterator i=m_modules.begin(); i!=m_modules.end(); ++i ){
00110 os << "\t" << *i << endl;
00111 }
00112 os << "Run, Scan, TestPoint sets:" << endl;
00113 list<double>::const_iterator ipt=m_testPoints.begin();
00114 for (list<pair<long unsigned, long unsigned> >::const_iterator i= m_runScan.begin(); i!= m_runScan.end(); ++i){
00115 os << "\t" << (*i).first << ", " << (*i).second << " -> " << (*ipt)<< endl;
00116 ++ipt;
00117 }
00118 }
00119
00120 inline bool pairWiseLessThan(pair<unsigned long, unsigned long> a,
00121 pair<unsigned long, unsigned long> b ){
00122 if (a.first==b.first) {
00123 return a.second<b.second;
00124 } else {
00125 return a.first<b.first;
00126 }
00127 }
00128
00129 void ReAnalyse::go(){
00130
00131
00132
00133 if (verbosity>0) print(cout);
00134
00135
00136 TestData td;
00137 td.testName=m_testName;
00138 td.runNumber=(*m_runScan.begin()).first;
00139 td.nScans=m_runScan.size();
00140 td.startScanNumber=(*m_runScan.begin()).second;
00141 td.testVariable=0;
00142 td.startTime="";
00143 td.endTime="";
00144 td.modules = new std::string[m_modules.size()];
00145 td.modules_size = m_modules.size();
00146 td.testPoints_size=m_runScan.size();
00147 td.testPoints = new double[m_runScan.size()];
00148
00149
00150 std::list<double>::iterator ipt=m_testPoints.begin();
00151 for (unsigned i=0; i<m_runScan.size(); ++i){
00152 td.testPoints[i]=(*ipt); ipt++;
00153 }
00154
00155 try {
00156 unsigned icount=0;
00157 for (list<string>::const_iterator i=m_modules.begin(); i!= m_modules.end(); ++i ){
00158 td.modules[icount]=*i;
00159 shared_ptr<const TestData> tdclone(new TestData(td));
00160 m_algorithms.push_back(AnalysisAlgorithmMap::instance().getAlgorithm(tdclone, *i));
00161 ++icount;
00162 }
00163 } catch(Sct::Throwable& e){
00164 e.sendToMrs();
00165 cerr << e.what() << endl;
00166 return;
00167 }
00168
00169
00170 if (m_option_n) return;
00171
00172 for (list<shared_ptr<AnalysisAlgorithm> >::iterator ialg=m_algorithms.begin(); ialg!= m_algorithms.end(); ++ialg){
00173 try{
00174 unsigned ictrs=0;
00175 for (list<pair<long unsigned,long unsigned> >::const_iterator irs=m_runScan.begin() ; irs!=m_runScan.end(); ++irs){
00176 std::ostringstream ossfit, ossraw;
00177 ossfit << "FittedData.SctData::FitScanResult." << (*irs).first << "." << (*irs).second << "." << (*ialg)->getModuleName();
00178 ossraw << "EventData.SctData::RawScanResult." << (*irs).first << "." << (*irs).second << "." << (*ialg)->getModuleName();
00179 (*ialg)->setFit(ictrs, ossfit.str());
00180 (*ialg)->setRaw(ictrs, ossraw.str());
00181 ictrs++;
00182 }
00183 (*ialg)->checkTestResultExists();
00184 if (!(*ialg)->canAnalyze()) throw Sct::IllegalStateError("Internal error - cant analyze - dont have all data", __FILE__, __LINE__);
00185 if (verbosity>0) std::cout << "Load " << (*ialg)->getModuleName() << endl;
00186 (*ialg)->loadData();
00187 if (verbosity>0) std::cout << "Analyse " << (*ialg)->getModuleName() << endl;
00188 (*ialg)->analyze();
00189 if (verbosity>0) std::cout << "Write " << (*ialg)->getModuleName() << endl;
00190 (*ialg)->finish();
00191 if (verbosity>0) std::cout << "Done " << (*ialg)->getModuleName() << endl;
00192 }catch(Sct::Throwable& e){
00193 e.sendToMrs();
00194 cerr << e.what() << endl;
00195 }
00196 }
00197 }
00198 }