00001 #include "NPtGainAlgorithm.h"
00002 #include "AnalysisAlgorithmMap.h"
00003 #include "AnalysisService.h"
00004 #include "Sct/SctParameters.h"
00005 #include "SctData/NPtGainTestResult.h"
00006 #include "SctData/FitScanResult.h"
00007 #include "SctData/FitObject.h"
00008 #include "SctData/ResponseCurve.h"
00009 #include "SctData/StandardDefects.h"
00010 #include "SctData/ModuleConfiguration.h"
00011 #include "SctData/ChipConfiguration.h"
00012 #include <boost/shared_ptr.hpp>
00013
00014 using namespace Sct;
00015 using namespace SctData;
00016 using namespace boost;
00017
00018 using std::auto_ptr;
00019
00020 namespace SctAnalysis {
00021
00022
00023 shared_ptr<AnalysisAlgorithm> NPtGainAlgorithm::clone(const TestData& testData, const string& moduleName) const throw() {
00024 return shared_ptr<AnalysisAlgorithm>(new NPtGainAlgorithm(testData, moduleName, *this));
00025 }
00026
00027 shared_ptr<ResponseCurve> NPtGainAlgorithm::getResponseCurve(unsigned int nPts) throw(LogicError){
00028 if (nPts <= 3) {
00029 return shared_ptr<ResponseCurve> (new LinearResponseCurve());
00030 } else {
00031 return shared_ptr<ResponseCurve> (new ExponentialResponseCurve());
00032 }
00033 }
00034
00035 void NPtGainAlgorithm::setResponseCurve(auto_ptr<ResponseCurve> rc) throw() {
00036 static shared_ptr<ResponseCurve> s_responseCurve=shared_ptr<ResponseCurve>(rc.release());
00037 }
00038
00039 bool NPtGainAlgorithm::inMap = AnalysisAlgorithmMap::instance().setAlgorithm("NPtGainTest", auto_ptr<AnalysisAlgorithm>(new NPtGainAlgorithm()));
00040
00041 shared_ptr<SctData::TestResult> NPtGainAlgorithm::createTestResult() const {
00042 shared_ptr<NPtGainTestResult> result (new NPtGainTestResult());
00043 result->setChipDataSize(nChipModule);
00044 result->setChannelDataSize(nChannelModule);
00045
00046 return result;
00047 }
00048
00049 void NPtGainAlgorithm::loadData() {
00050 loadAllFits();
00051 }
00052
00053 bool NPtGainAlgorithm::canAnalyze() const {
00054 return hasAllFits();
00055 }
00056
00057 void NPtGainAlgorithm::analyze() {
00058 shared_ptr<NPtGainTestResult> result = dynamic_pointer_cast<NPtGainTestResult> ( getTestResult() );
00059 result->setScanVariable(getFit(0)->getHeader().getVariable());
00060 try{
00061 result->setSpecialScanPointValue(2.0);
00062 } catch (Sct::Throwable& e){
00063 e.sendToMrs(MRS_ERROR);
00064 }
00065
00066 const ModuleConfiguration& config = getFit(0)->getConfiguration();
00067
00068 nOnePointTrimmed = 0;
00069 nTwoPointTrimmed = 0;
00070
00071
00072 for (unsigned int i=0; i<nChipModule; ++i) {
00073 setupGraph(i, ModuleElement::Chip(i), &FitScanResult::getChipFit, *result, result->getChipData(i), true);
00074 doFit(i, &FitScanResult::getChipFit, *result, result->getChipData(i));
00075 }
00076
00077
00078 for (unsigned int i=0; i<nChannelModule; ++i) {
00079 ModuleElement element = ModuleElement::Channel(i);
00080 setupGraph(i, element, &FitScanResult::getChannelFit, *result, result->getChannelData(i), true);
00081
00082
00083 if (config.channelIsMasked(i)) continue;
00084 doFit(i, &FitScanResult::getChannelFit, *result, result->getChannelData(i));
00085 doDefect(element, result->getDefects(),
00086 result->getChannelData(i), result->getChipData(i/nChannelChip));
00087 }
00088
00089
00090
00091
00092 const DefectList::DefectCollection& defects = result->getDefects().getAllDefects();
00093 bool passed = true;
00094 unsigned int totalChannels = 0;
00095 for (DefectList::DefectCollection::const_iterator i=defects.begin(); i!=defects.end(); ++i) {
00096
00097 if (i->getPrototype().getSeverity() > DODGY) {
00098 totalChannels += i->getModuleElement().getNChannels();
00099 if (i->getModuleElement().getNChannels()>7) {
00100 passed = false;
00101 break;
00102 }
00103 }
00104 }
00105 if (totalChannels > 15) passed = false;
00106
00107
00108 if (nOnePointTrimmed != 0) {
00109 ostringstream oss;
00110 oss << nOnePointTrimmed << " channels had 1 point trimmed from fit";
00111 result->addComment(oss.str());
00112 }
00113 if (nTwoPointTrimmed != 0) {
00114 ostringstream oss;
00115 oss << nTwoPointTrimmed << " channels had 2 (or more) points trimmed from fit";
00116 result->addComment(oss.str());
00117 }
00118
00119 result->setPassed(passed);
00120 }
00121
00122
00123 void NPtGainAlgorithm::setupGraph(unsigned int id, const ModuleElement& element, getFitFunction fitFunc, NPtGainTestResult& test, NPtGainTestResultData& testData, bool trimPoints) throw(LogicError) {
00124
00125 unsigned int lastPoint = test.getNScans()-1;
00126
00127 if (trimPoints && test.getNScans() > 5) {
00128 double sigmaCut = 0;
00129 for (unsigned int i=0; i<test.getNScans(); ++i) {
00130 sigmaCut+= (getFit(i).get()->*fitFunc)(id).getParameter("Sigma");
00131 }
00132 sigmaCut *= 1.5/test.getNScans();
00133
00134 for (unsigned int i=test.getNScans() - 1; i>4; --i) {
00135
00136 const DefectList& defects = getFit(i)->getDefects();
00137 if (test.getTestPointAt(i) > 5 && ((getFit(i).get()->*fitFunc)(id).getParameter("Sigma") > sigmaCut ||
00138 defects.defectSeverityAffectingElement(element) >= SERIOUS)) {
00139 lastPoint = i-1;
00140 }
00141 }
00142
00143
00144 if (lastPoint != test.getNScans() - 1) {
00145 if (element.isChip()) {
00146 ostringstream oss;
00147 oss << "Points trimed from response curve for chip: " << id << ". Last point " << lastPoint << " with charge " << test.getTestPointAt(lastPoint);
00148 test.addComment(oss.str());
00149 } else {
00150 if (lastPoint+1-test.getNScans() == 1) ++nOnePointTrimmed;
00151 else ++nTwoPointTrimmed;
00152 }
00153 }
00154 }
00155
00156 mergeDefects(test, element, lastPoint);
00157
00158 shared_ptr<TGraph> g (new TGraph(lastPoint+1));
00159 if (!g)
00160 throw InvariantViolatedError("NPtGainTestResult::setupGraph couldn't make TGraph", __FILE__, __LINE__);
00161
00162
00163 for (unsigned int j = 0; j <= lastPoint; ++j) {
00164 g->SetPoint(j, test.getTestPointAt(j), (getFit(j).get()->*fitFunc)(id).getParameter("Mean"));
00165 }
00166 testData.graph = g;
00167
00168 shared_ptr<ResponseCurve> r=getResponseCurve(test.getNScans());
00169 testData.rc = r;
00170 }
00171
00172 void NPtGainAlgorithm::mergeDefects(NPtGainTestResult& test, const ModuleElement& element, unsigned int lastPoint) {
00173 for (unsigned int i=0; i<lastPoint; ++i) {
00174 auto_ptr<DefectList> defects = getFit(i)->getDefects().getDefectsEncompassingElement(element);
00175 const DefectList::DefectCollection& allDefects = defects->getAllDefects();
00176 for (DefectList::DefectCollection::const_iterator it=allDefects.begin(); it!=allDefects.end(); ++it) {
00177 test.getDefects().addDefect(Defect(it->getPrototype(), element));
00178 }
00179 }
00180 }
00181
00182 void NPtGainAlgorithm::doFit(unsigned int id, getFitFunction fitFunc, NPtGainTestResult& test,
00183 NPtGainTestResultData& testData) throw(LogicError) {
00184
00185
00186
00187 testData.rc->getFunction()->SetRange(0,10);
00188 AnalysisService::instance().getFitStrategy().fitTGraph(*testData.graph, *testData.rc->getFunction() );
00189
00190 testData.gain = testData.rc->getGain(test.getSpecialScanPointValue());
00191
00192 shared_ptr<const FitScanResult> specialFit = getFit(test.getSpecialScanIndex());
00193
00194 if (testData.gain != 0) testData.noise = 6250 * (specialFit.get()->*fitFunc)(id).getParameter("Sigma") / testData.gain;
00195 else testData.noise = 0;
00196 testData.offset= testData.rc->getFunction()->Eval(0.);
00197 }
00198
00199 void NPtGainAlgorithm::doDefect(const ModuleElement& element, DefectList& defects,
00200 const NPtGainTestResultData& data,
00201 const NPtGainTestResultData& comparisonData) throw(LogicError) {
00202
00203 if (data.gain < StandardDefects::VLO_GAIN.getParameter() * comparisonData.gain)
00204 defects.addDefect(Defect(StandardDefects::VLO_GAIN, element));
00205
00206 if (data.gain < StandardDefects::LO_GAIN.getParameter() * comparisonData.gain)
00207 defects.addDefect(Defect(StandardDefects::LO_GAIN, element));
00208 else if (data.gain > StandardDefects::HI_GAIN.getParameter() * comparisonData.gain)
00209 defects.addDefect(Defect(StandardDefects::HI_GAIN, element));
00210
00211 if (data.offset < StandardDefects::LO_OFFSET.getParameter())
00212 defects.addDefect(Defect(StandardDefects::LO_OFFSET, element));
00213 else if (data.offset > StandardDefects::HI_OFFSET.getParameter())
00214 defects.addDefect(Defect(StandardDefects::HI_OFFSET, element));
00215
00216 if (data.noise < StandardDefects::UNBONDED.getParameter())
00217 defects.addDefect(Defect(StandardDefects::UNBONDED, element));
00218 else if (data.noise < StandardDefects::PARTBONDED.getParameter())
00219 defects.addDefect(Defect(StandardDefects::PARTBONDED, element));
00220 else if (data.noise > StandardDefects::NOISY.getParameter() * comparisonData.noise)
00221 defects.addDefect(Defect(StandardDefects::NOISY, element));
00222
00223 if (data.noise > StandardDefects::V_NOISY.getParameter() * comparisonData.noise)
00224 defects.addDefect(Defect(StandardDefects::V_NOISY, element));
00225 }
00226 }