00001 #include "ChipCounterAlgorithm.h"
00002 #include "AnalysisAlgorithmMap.h"
00003 #include "SctData/ChipCounterTestResult.h"
00004 #include "SctData/StandardDefects.h"
00005
00006 #include "SctData/RawScanResult.h"
00007 #include <cfloat>
00008 #include "TH2.h"
00009 #include "Sct/SctNames.h"
00010
00011
00012 #include <cmath>
00013 #include <set>
00014
00015 using namespace std;
00016 using namespace boost;
00017 using namespace SctData;
00018 using namespace Sct;
00019
00020 namespace SctAnalysis {
00021
00022 bool ChipCounterAlgorithm::inMap = AnalysisAlgorithmMap::instance().setAlgorithm("ChipCounterTest", auto_ptr<AnalysisAlgorithm>(new ChipCounterAlgorithm()));
00023
00024 shared_ptr<AnalysisAlgorithm> ChipCounterAlgorithm::clone(shared_ptr<const TestData> testData, const string& moduleName) const throw() {
00025 return shared_ptr<AnalysisAlgorithm>(new ChipCounterAlgorithm(testData, moduleName, *this));
00026 }
00027
00028 void ChipCounterAlgorithm::loadData() {
00029 loadAllRaws();
00030 }
00031
00032 bool ChipCounterAlgorithm::canAnalyze() const{
00033 return hasAllRaws();
00034 }
00035
00036 shared_ptr<TestResult> ChipCounterAlgorithm::createTestResult() const {
00037 return shared_ptr<ChipCounterTestResult> (new ChipCounterTestResult());
00038 }
00039
00040 void ChipCounterAlgorithm::analyze() {
00041 ChipCounterTestResult & result = *dynamic_pointer_cast<ChipCounterTestResult> ( getTestResult() );
00042
00043 const RawScanResult& raw = *getRaw(0);
00044 result.setScanVariable(raw.getHeader().getVariable());
00045
00046 const SctData::ScanPoints& points=raw.getPoints();
00047 const unsigned int numberOfScanPoints = points.getNPoints();
00048
00049 const TH2& data0 = raw.getScanData(0);
00050 const TH2& data1 = raw.getScanData(1);
00051
00052 const unsigned nx0 = data0.GetNbinsX();
00053 const unsigned ny0 = data0.GetNbinsY();
00054 const unsigned nx1 = data1.GetNbinsX();
00055 const unsigned ny1 = data1.GetNbinsY();
00056 if((!(ny0==numberOfScanPoints)) || (!(ny1==numberOfScanPoints))) {
00057 result.setPassed(false);
00058 ostringstream oss;
00059 oss << "Wrong number of scan points";
00060 result.addComment(oss.str());
00061 return;
00062 };
00063
00064
00065
00066 unsigned int i = 1;
00067 unsigned int nstart0 = 0;
00068 while ( i <= nx0 ) {
00069 if (data0.GetBinContent(i,1) == 0) ++i;
00070 if (data0.GetBinContent(i,1) == 1) {
00071 nstart0=i;
00072 i=nx0+1;
00073 }
00074 }
00075
00076 i = 1;
00077 unsigned int nstart1 = 0;
00078 while ( i <= nx1 ) {
00079 if (data1.GetBinContent(i,1) == 0) ++i;
00080 if (data1.GetBinContent(i,1) == 1) {
00081 nstart1=i;
00082 i=nx1+1;
00083 }
00084 }
00085
00086
00087 if ((nstart0 ==0) || (nstart1 ==0)) {
00088 result.setPassed(false);
00089 ostringstream oss;
00090 oss << "One link returned all 0s.";
00091 result.addComment(oss.str());
00092 return;
00093 }
00094
00095
00096
00097 bool problemInBit[NBC+NL1];
00098 for (unsigned ibit=0; ibit<NBC+NL1; ++ibit){
00099 problemInBit[ibit]=false;
00100 }
00101
00102
00103 for (int ibin=NHEADER; ibin<(NHEADER+NBC+NL1); ++ibin) {
00104 unsigned int allzeros=1;
00105
00106 for (unsigned int j=0; j< numberOfScanPoints; ++j) {
00107 if (!(data0.GetBinContent(nstart0+ibin, j+1) == data1.GetBinContent(nstart1+ibin, j+1))) {
00108
00109 problemInBit[ibin-NHEADER]=true;
00110 }
00111 if ((data0.GetBinContent(nstart0+ibin, j+1) == 1)) allzeros =0;
00112 }
00113
00114
00115 if (allzeros == 1) problemInBit[ibin-NHEADER]=true;
00116
00117 }
00118
00119 bool setpassed=true;
00120
00121 for (int ibit=0; ibit<NBC+NL1; ++ibit){
00122 if ( problemInBit[ibit] == true ) {
00123 if (ibit < (NL1)) {
00124 result.getDefects().addDefect(Defect(StandardDefects::L1_COUNTER, ModuleElement::Module()));
00125 ostringstream oss;
00126 oss << "Counter error in L1 bit (LSB=0) " << NL1-1-(ibit);
00127 result.addComment(oss.str());
00128 setpassed=false;
00129 } else {
00130 result.getDefects().addDefect(Defect(StandardDefects::BC_COUNTER, ModuleElement::Module()));
00131 ostringstream oss;
00132 oss << "Counter error in BC bit (LSB=0) " << NBC-1-(ibit-NL1);
00133 result.addComment(oss.str());
00134 setpassed=false;
00135 }
00136 }
00137 }
00138
00139 result.setPassed(setpassed);
00140 result.setProblem(!setpassed);
00141
00142 }
00143 }