00001 #include "Comp.h"
00002 #include "Sct/IS/IOManagerIS.h"
00003 #include "Sct/Exception.h"
00004 #include "Sct/LogicErrors.h"
00005 #include "Sct/SctNames.h"
00006 #include "Sct/SctParameters.h"
00007 #include "SctData/FitScanResult.h"
00008 #include "SctData/FitObject.h"
00009 #include <is/isinfo.h>
00010 #include <boost/shared_ptr.hpp>
00011 #include <iostream>
00012 #include <vector>
00013
00014 using namespace Sct;
00015 using namespace Sct::IS;
00016 using namespace boost;
00017 using namespace SctData;
00018 using namespace std;
00019
00020 bool debug = false;
00021 double meanTol = 1;
00022 double sigmaTol = 0.31;
00023 string lastScanName = "FittedData.SctData::FitScanResult.551.53.SCTTestAPI_PseudoModule";
00024
00025
00026 vector<shared_ptr<FitScanResult> > readAll() {
00027 vector<shared_ptr<FitScanResult> > fits;
00028
00029 ISInfoIterator it( SctNames::getPartition(), "FittedData", ".*" );
00030 while (it()) {
00031 shared_ptr<Serializable> ob = IOManagerIS::instance().read(it);
00032 shared_ptr<FitScanResult> result = dynamic_pointer_cast<FitScanResult>(ob);
00033 if (!result)
00034 throw IllegalStateError(string("Failed to read fit: ") + it.name(), __FILE__, __LINE__);
00035 fits.push_back(result);
00036 }
00037
00038 return fits;
00039 }
00040
00041 void removeAll() {
00042 ISInfoIterator it( SctNames::getPartition(), "FittedData", ".*" );
00043 ISInfoDictionary dict(SctNames::getPartition());
00044 while (it()) {
00045 dict.remove(it.name());
00046 }
00047 }
00048
00049 bool compareFits(FitScanResult& nagFit, FitScanResult& rootFit) {
00050 Comp cMean(meanTol);
00051 Comp cSigma(sigmaTol);
00052 int meanFailCount = 0;
00053 int sigmaFailCount = 0;
00054
00055 cout << endl << endl << "Comparing " << nagFit.getUniqueID() << endl;
00056
00057 for (unsigned int link = 0; link<nLinkModule; ++link) {
00058 for (unsigned int i=0; i<nChannelLink; ++i) {
00059 FitObject& nagFitOb = nagFit.getChannelFit(link, i);
00060 FitObject& rootFitOb = rootFit.getChannelFit(link, i);
00061 int nagMeanIndex = nagFitOb.getParIndex("Mean");
00062 int nagSigmaIndex = nagFitOb.getParIndex("Sigma");
00063 int rootMeanIndex = nagFitOb.getParIndex("Mean");
00064 int rootSigmaIndex = nagFitOb.getParIndex("Sigma");
00065
00066 if (cMean.comp(nagFitOb.getParameter(nagMeanIndex), rootFitOb.getParameter(rootMeanIndex))) {
00067 cout << "Channel: " << (link*nChannelLink+i) << " Mean different. Root: " << rootFitOb.getParameter(rootMeanIndex) << "+-"
00068 << rootFitOb.getParError(rootMeanIndex) << " Nag: " << nagFitOb.getParameter(nagMeanIndex) << " +- "
00069 << nagFitOb.getParError(nagMeanIndex) << endl;
00070 ++meanFailCount;
00071 } else if (debug) {
00072 cout << "Channel: " << (link*nChannelLink+i) << " Mean same. Root: " << rootFitOb.getParameter(rootMeanIndex) << "+-"
00073 << rootFitOb.getParError(rootMeanIndex) << " Nag: " << nagFitOb.getParameter(nagMeanIndex) << " +- "
00074 << nagFitOb.getParError(nagMeanIndex) << endl;
00075 }
00076
00077 if (cSigma.comp(nagFitOb.getParameter(nagSigmaIndex), rootFitOb.getParameter(rootSigmaIndex))) {
00078 cout << "Channel: " << (link*nChannelLink+i) << " Sigma different. Root: " << rootFitOb.getParameter(rootSigmaIndex) << "+-"
00079 << rootFitOb.getParError(rootSigmaIndex) << " Nag: " << nagFitOb.getParameter(nagSigmaIndex) << " +- "
00080 << nagFitOb.getParError(nagSigmaIndex) << endl;
00081 ++sigmaFailCount;
00082 } else if (debug) {
00083 cout << "Channel: " << (link*nChannelLink+i) << " Sigma same Root: " << rootFitOb.getParameter(rootSigmaIndex) << "+-"
00084 << rootFitOb.getParError(rootSigmaIndex) << " Nag: " << nagFitOb.getParameter(nagSigmaIndex) << " +- "
00085 << nagFitOb.getParError(nagSigmaIndex) << endl;
00086 }
00087
00088 }
00089 }
00090 cout << "Summary. " << endl << "Av Mean diff: " << cMean.mean() << " +- " << cMean.stddev() << endl;
00091 cout << "Av Sigma diff: " << cSigma.mean() << " +- " << cSigma.stddev() << endl;
00092 cout << meanFailCount << " channels failed mean test" << endl;
00093 cout << sigmaFailCount << " channels failed sigma test" << endl;
00094 return (meanFailCount + sigmaFailCount < 80);
00095 }
00096
00097
00098 bool compare(vector<shared_ptr<FitScanResult> > nag, vector<shared_ptr<FitScanResult> > root) {
00099 if (nag.size() != root.size()) {
00100 cout << "Numbers of fits different!! Nag: " << nag.size() << " Root: " << root.size() << endl;
00101 return false;
00102 }
00103 bool retVal = true;
00104 for (unsigned int i=0; i<nag.size(); ++i) {
00105 if (!compareFits(*nag[i], *root[i])) retVal = false;
00106 }
00107 return retVal;
00108 }
00109
00110
00111 void waitFor(string name, int nsecs) {
00112 ISInfoDictionary d(SctNames::getPartition());
00113 int tries = 0;
00114 while (!d.contains(name.c_str()) && tries < nsecs) {
00115 if (!sleep(1))
00116 ++tries;
00117 }
00118 if (tries == nsecs) {
00119 throw IllegalStateError("Didn't wait long enough", __FILE__, __LINE__);
00120 }
00121 if (debug) {
00122 cout << "Waited for " << tries << " secs" << endl;
00123 }
00124 }
00125
00126
00127 int main(int argc, char** argv) {
00128 Sct::setExceptionHandlers(argv[0]);
00129
00130 try {
00131 waitFor(lastScanName, 20);
00132 vector<shared_ptr<FitScanResult> > nag = readAll();
00133 removeAll();
00134 system("java -Dipc.ref.file=$IPC_REF_FILE bsh.Interpreter SetFitStrategyRoot.bsh");
00135 waitFor(lastScanName, 40);
00136 vector<shared_ptr<FitScanResult> > root = readAll();
00137
00138 bool retVal = compare(nag, root);
00139 system("java -Dipc.ref.file=$IPC_REF_FILE bsh.Interpreter FittingServiceStatus.bsh");
00140 if (retVal) {
00141 if (debug) cout << "Success!" << endl;
00142 return 0;
00143 }
00144 else {
00145 cout << "Failed" << endl;
00146 return 1;
00147 }
00148
00149 } catch (Throwable& e) {
00150 e.sendToMrs(MRS_FATAL);
00151 return 1;
00152 }
00153 }