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 #include <TFile.h>
00014 #include <TApplication.h>
00015 #include <TGraph.h>
00016
00017 using namespace Sct;
00018 using namespace Sct::IS;
00019 using namespace boost;
00020 using namespace SctData;
00021 using namespace std;
00022
00023 bool debug = false;
00024 double meanTol = 1;
00025 double sigmaTol = 0.37;
00026 string lastScanName = "FittedData.SctData::FitScanResult.551.53.SCTTestAPI_PseudoModule";
00027
00028 static TApplication& getRootApp() {
00029
00030 static TApplication myapp("myapp", 0, 0);
00031 return myapp;
00032 }
00033
00034 vector<shared_ptr<FitScanResult> > readAll() {
00035 vector<shared_ptr<FitScanResult> > fits;
00036
00037 ISInfoIterator it( SctNames::getPartition(), "FittedData", ".*" );
00038 ISInfoDictionary dict(SctNames::getPartition());
00039 while (it()) {
00040 shared_ptr<Serializable> ob = IOManagerIS::instance().read(it);
00041 shared_ptr<FitScanResult> result = dynamic_pointer_cast<FitScanResult>(ob);
00042 if (!result)
00043 throw IllegalStateError(string("Failed to read fit: ") + it.name(), __FILE__, __LINE__);
00044 fits.push_back(result);
00045 }
00046
00047 return fits;
00048 }
00049
00050 TFile* loadSCTDAQData(FitScanResult& fit) {
00051 ostringstream oss;
00052 oss << "$SCT_DAQ_ROOT/installed/share/SctTestApiData/strun" << fit.getHeader().getRunNumber() << "_" << fit.getHeader().getScanNumber() << ".root";
00053 if (debug) cout << oss.str() << endl;
00054 return new TFile(oss.str().c_str());
00055 }
00056
00057
00058 TH1F* getSigmaGraph(unsigned int link, TFile* f) {
00059 ostringstream oss;
00060 oss << "h_sigma" << link << ";" << 1;
00061 if (debug) cout << oss.str() << endl;
00062 return (TH1F*)f->Get(oss.str().c_str());
00063 }
00064
00065 TH1F* getMeanGraph(unsigned int link, TFile* f) {
00066 ostringstream oss;
00067 oss << "h_mean" << link << ";" << 1;
00068 if (debug) cout << oss.str() << endl;
00069 return (TH1F*)f->Get(oss.str().c_str());
00070 }
00071
00072 TH1F* getChiSqGraph(unsigned int link, TFile* f) {
00073 ostringstream oss;
00074 oss << "h_chisq" << link << ";" << 1;
00075 if (debug) cout << oss.str() << endl;
00076 return (TH1F*)f->Get(oss.str().c_str());
00077 }
00078
00079
00080 bool compareFits(FitScanResult& fit) {
00081 Comp cMean(meanTol);
00082 Comp cSigma(sigmaTol);
00083 int meanFailCount = 0;
00084 int sigmaFailCount = 0;
00085 TFile* f = loadSCTDAQData(fit);
00086
00087 cout << endl << endl << "Comparing " << fit.getUniqueID() << endl;
00088
00089 for (unsigned int link = 0; link<nLinkModule; ++link) {
00090 TH1F* gMean = getMeanGraph(link, f);
00091 TH1F* gSigma = getSigmaGraph(link, f);
00092 TH1F* gChiSq = getChiSqGraph(link, f);
00093
00094 if (debug) cout << gMean->GetNbinsX() << endl;
00095 if (gMean->GetNbinsX() != nChannelLink) {
00096 throw IoException("Incorrect data in TH1F", __FILE__, __LINE__);
00097 }
00098
00099 for (unsigned int i=0; i<nChannelLink; ++i) {
00100 FitObject& fitOb = fit.getChannelFit(link, i);
00101
00102 double mean = gMean->GetBinContent(i);
00103 double sigma = gSigma->GetBinContent(i);
00104 double chisq = gChiSq->GetBinContent(i);
00105 int fitMeanIndex = fitOb.getParIndex("Mean");
00106 int fitSigmaIndex = fitOb.getParIndex("Sigma");
00107 double ourChiSq = fitOb.getChiSquared() / (fitOb.getNDF() + fitOb.getNPar());
00108
00109 if (cMean.comp(fitOb.getParameter(fitMeanIndex), mean)) {
00110 cout << "Channel: " << (link*nChannelLink+i) << " Mean different. SCTDAQ: " << mean << " +- "
00111 << gMean->GetBinError(i) << " Chi/NdF: " << chisq << " Us: " << fitOb.getParameter(fitMeanIndex)
00112 << " +- " << fitOb.getParError(fitMeanIndex) << " Chi/NdF: " << ourChiSq << endl;
00113 ++meanFailCount;
00114 } else if (debug) {
00115 cout << "Channel: " << (link*nChannelLink+i) << " Mean same. SCTDAQ: " << mean << " +- "
00116 << gMean->GetBinError(i) << " Chi/NdF: " << chisq << " Us: " << fitOb.getParameter(fitMeanIndex)
00117 << " +- " << fitOb.getParError(fitMeanIndex) << " Chi/NdF: " << ourChiSq << endl;
00118 }
00119
00120 if (cSigma.comp(fitOb.getParameter(fitSigmaIndex), sigma)) {
00121 cout << "Channel: " << (link*nChannelLink+i) << " Sigma different. SCTDAQ: " << sigma << " +- "
00122 << gSigma->GetBinError(i) << " Chi/NdF: " << chisq << " Us: " << fitOb.getParameter(fitSigmaIndex)
00123 << " +- " << fitOb.getParError(fitSigmaIndex) << " Chi/NdF: " << ourChiSq <<endl;
00124
00125 ++sigmaFailCount;
00126 } else if (debug) {
00127 cout << "Channel: " << (link*nChannelLink+i) << " Sigma same. SCTDAQ: " << sigma << " +- "
00128 << gSigma->GetBinError(i) << " Chi/NdF: " << chisq << " Us: " << fitOb.getParameter(fitSigmaIndex)
00129 << " +- " << fitOb.getParError(fitSigmaIndex) << " Chi/NdF: " << ourChiSq <<endl;
00130 }
00131
00132 }
00133 delete gMean;
00134 delete gSigma;
00135 }
00136 f->Close();
00137 cout << "Summary. " << endl << "Av Mean diff: " << cMean.mean() << " +- " << cMean.stddev() << endl;
00138 cout << "Av Sigma diff: " << cSigma.mean() << " +- " << cSigma.stddev() << endl;
00139 cout << meanFailCount << " channels failed mean test" << endl;
00140 cout << sigmaFailCount << " channels failed sigma test" << endl;
00141 return (meanFailCount + sigmaFailCount < 80);
00142 }
00143
00144
00145 bool compare(vector<shared_ptr<FitScanResult> > fits) {
00146 bool retVal = true;
00147 for (unsigned int i=0; i<fits.size(); ++i) {
00148 if (!compareFits(*fits[i])) retVal = false;
00149 }
00150 return retVal;
00151 }
00152
00153
00154 void waitFor(string name, int nsecs) {
00155 ISInfoDictionary d(SctNames::getPartition());
00156 int tries = 0;
00157 while (!d.contains(name.c_str()) && tries < nsecs) {
00158 if (!sleep(1))
00159 ++tries;
00160 }
00161 if (tries == nsecs) {
00162 throw IllegalStateError("Didn't wait long enough", __FILE__, __LINE__);
00163 }
00164 if (debug) {
00165 cout << "Waited for " << tries << " secs" << endl;
00166 }
00167 }
00168
00169
00170 int main(int argc, char** argv) {
00171 Sct::setExceptionHandlers(argv[0]);
00172 getRootApp();
00173 try {
00174 waitFor(lastScanName, 40);
00175 vector<shared_ptr<FitScanResult> > fits = readAll();
00176
00177 if (compare(fits)) {
00178 if (debug) cout << "Success!" << endl;
00179 return 0;
00180 }
00181 else {
00182 cout << "Failed" << endl;
00183 return 1;
00184 }
00185
00186 } catch (Throwable& e) {
00187 e.sendToMrs(MRS_FATAL);
00188 return 1;
00189 }
00190 }