00001 #include "RxDelayAlgorithm.h"
00002 #include "AnalysisAlgorithmMap.h"
00003 #include "SctData/RxDelayTestResult.h"
00004 #include "SctData/RawScanResult.h"
00005 #include "SctData/DefectList.h"
00006 #include "SctData/StandardDefects.h"
00007 #include <string>
00008 #include "TH2.h"
00009
00010 using namespace std;
00011 using namespace boost;
00012 using namespace SctData;
00013 using namespace Sct;
00014
00015 namespace SctAnalysis {
00016
00017 bool RxDelayAlgorithm::inMap = AnalysisAlgorithmMap::instance().setAlgorithm("RxDelayTest", auto_ptr<AnalysisAlgorithm>(new RxDelayAlgorithm()));
00018
00019 shared_ptr<AnalysisAlgorithm> RxDelayAlgorithm::clone(shared_ptr<const TestData> testData, const string& moduleName) const throw(){
00020 return shared_ptr<AnalysisAlgorithm>(new RxDelayAlgorithm(testData, moduleName, *this));
00021 }
00022
00023 shared_ptr<TestResult> RxDelayAlgorithm::createTestResult() const {
00024 return shared_ptr<RxDelayTestResult> (new RxDelayTestResult()) ;
00025 }
00026
00027 void RxDelayAlgorithm::loadData() {
00028 loadAllRaws();
00029 }
00030
00031 bool RxDelayAlgorithm::canAnalyze() const {
00032 return hasAllRaws();
00033 }
00034
00035
00036 void RxDelayAlgorithm::analyze() {
00037 RxDelayTestResult& result = *dynamic_pointer_cast<RxDelayTestResult>(getTestResult());
00038 const RawScanResult& raw = *getRaw(0);
00039 result.setScanVariable(raw.getHeader().getVariable());
00040
00041 bool pass = true;
00042 const SctData::ScanPoints points = raw.getPoints();
00043 bool ascending = points.ascending();
00044 result.setNOptima(nLinkModule);
00045
00046 for (short unsigned int ilink=0; ilink<nLinkModule; ++ilink ) {
00047 const TH2& data = raw.getScanData(ilink);
00048 const unsigned nx = data.GetNbinsX();
00049 const unsigned ny = data.GetNbinsY();
00050 double* projection = new double[ny];
00051 int rise=-1;
00052 int fall=-1;
00053 for (unsigned iy=0; iy<ny; ++iy){
00054 const unsigned ipt = ascending ? iy : ny-iy-1;
00055 projection[ipt]=0;
00056 for (unsigned ix=0; ix<nx; ++ix){
00057 projection[iy]+=data.GetBinContent(ix+1,ipt+1);
00058 }
00059
00060 double half = points.getNEvents(ipt) * nx/2.;
00061 if (ipt>0 && projection[ipt]>half && projection[ipt-1]<half) {
00062
00063
00064 rise=ipt;
00065 }
00066 if (ipt>0 && projection[ipt]<half && projection[ipt-1]>half) {
00067
00068
00069 fall=ipt;
00070 }
00071 }
00072
00073 double optimum=-1;
00074 double fraction=0.5;
00075 const unsigned zero = ascending ? 0 : ny-1;
00076 const unsigned end = ascending ? ny-1 : 0;
00077
00078
00079
00080 if (rise==-1&&fall==-1) {
00081
00082 optimum = points[zero] + fraction*(points[end]-points[zero]);
00083 } else if (rise==-1){
00084
00085 optimum = points[fall] + (1-fraction)*25.;
00086 } else if (fall==-1){
00087
00088 optimum = points[rise] + fraction*25.;
00089 } else {
00090
00091 optimum = points[rise] + fraction*(points[fall]-points[rise]);
00092 }
00093 while (optimum>25.) {optimum-=25.;}
00094
00095 if (optimum<0) throw Sct::IllegalStateError("RxDelay algorithm wants to set delay<0", __FILE__, __LINE__);
00096 result.setOptimum(ilink, optimum);
00097 }
00098 result.setPassed(pass);
00099 }
00100
00101
00102 }