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 totalChannels += i->getModuleElement().getNChannels();
00097 if (i->getModuleElement().getNChannels()>7) {
00098 passed = false;
00099 break;
00100 }
00101 }
00102 if (totalChannels > 15) passed = false;
00103
00104
00105 if (nOnePointTrimmed != 0) {
00106 ostringstream oss;
00107 oss << nOnePointTrimmed << " channels had 1 point trimmed from fit";
00108 result->addComment(oss.str());
00109 }
00110 if (nTwoPointTrimmed != 0) {
00111 ostringstream oss;
00112 oss << nTwoPointTrimmed << " channels had 2 (or more) points trimmed from fit";
00113 result->addComment(oss.str());
00114 }
00115
00116 result->setPassed(passed);
00117 }
00118
00119
00120 void NPtGainAlgorithm::setupGraph(unsigned int id, const ModuleElement& element, getFitFunction fitFunc, NPtGainTestResult& test, NPtGainTestResultData& testData, bool trimPoints) throw(LogicError) {
00121
00122 unsigned int lastPoint = test.getNScans()-1;
00123
00124 if (trimPoints && test.getNScans() > 5) {
00125 double sigmaCut = 0;
00126 for (unsigned int i=0; i<test.getNScans(); ++i) {
00127 sigmaCut+= (getFit(i).get()->*fitFunc)(id).getParameter("Sigma");
00128 }
00129 sigmaCut *= 1.5/test.getNScans();
00130
00131 for (unsigned int i=test.getNScans() - 1; i>4; --i) {
00132
00133 const DefectList& defects = getFit(i)->getDefects();
00134 if (test.getTestPointAt(i) > 5 && ((getFit(i).get()->*fitFunc)(id).getParameter("Sigma") > sigmaCut ||
00135 defects.defectSeverityAffectingElement(element) >= SERIOUS)) {
00136 lastPoint = i-1;
00137 }
00138 }
00139
00140
00141 if (lastPoint != test.getNScans() - 1) {
00142 if (element.isChip()) {
00143 ostringstream oss;
00144 oss << "Points trimed from response curve for chip: " << id << ". Last point " << lastPoint << " with charge " << test.getTestPointAt(lastPoint);
00145 test.addComment(oss.str());
00146 } else {
00147 if (lastPoint+1-test.getNScans() == 1) ++nOnePointTrimmed;
00148 else ++nTwoPointTrimmed;
00149 }
00150 }
00151 }
00152
00153 mergeDefects(test, element, lastPoint);
00154
00155 shared_ptr<TGraph> g (new TGraph(lastPoint+1));
00156 if (!g)
00157 throw InvariantViolatedError("NPtGainTestResult::setupGraph couldn't make TGraph", __FILE__, __LINE__);
00158
00159
00160 for (unsigned int j = 0; j <= lastPoint; ++j) {
00161 g->SetPoint(j, test.getTestPointAt(j), (getFit(j).get()->*fitFunc)(id).getParameter("Mean"));
00162 }
00163 testData.graph = g;
00164
00165 shared_ptr<ResponseCurve> r=getResponseCurve(test.getNScans());
00166 testData.rc = r;
00167 }
00168
00169 void NPtGainAlgorithm::mergeDefects(NPtGainTestResult& test, const ModuleElement& element, unsigned int lastPoint) {
00170 for (unsigned int i=0; i<lastPoint; ++i) {
00171 auto_ptr<DefectList> defects = getFit(i)->getDefects().getDefectsEncompassingElement(element);
00172 const DefectList::DefectCollection& allDefects = defects->getAllDefects();
00173 for (DefectList::DefectCollection::const_iterator it=allDefects.begin(); it!=allDefects.end(); ++it) {
00174 test.getDefects().addDefect(Defect(it->getPrototype(), element));
00175 }
00176 }
00177 }
00178
00179 void NPtGainAlgorithm::doFit(unsigned int id, getFitFunction fitFunc, NPtGainTestResult& test,
00180 NPtGainTestResultData& testData) throw(LogicError) {
00181
00182
00183
00184 testData.rc->getFunction()->SetRange(0,10);
00185 AnalysisService::instance().getFitStrategy().fitTGraph(*testData.graph, *testData.rc->getFunction() );
00186
00187 testData.gain = testData.rc->getGain(test.getSpecialScanPointValue());
00188
00189 shared_ptr<const FitScanResult> specialFit = getFit(test.getSpecialScanIndex());
00190
00191 if (testData.gain != 0) testData.noise = 6250 * (specialFit.get()->*fitFunc)(id).getParameter("Sigma") / testData.gain;
00192 else testData.noise = 0;
00193 testData.offset= testData.rc->getFunction()->Eval(0.);
00194 }
00195
00196 void NPtGainAlgorithm::doDefect(const ModuleElement& element, DefectList& defects,
00197 const NPtGainTestResultData& data,
00198 const NPtGainTestResultData& comparisonData) throw(LogicError) {
00199
00200 if (data.gain < StandardDefects::VLO_GAIN.getParameter() * comparisonData.gain)
00201 defects.addDefect(Defect(StandardDefects::VLO_GAIN, element));
00202
00203 if (data.gain < StandardDefects::LO_GAIN.getParameter() * comparisonData.gain)
00204 defects.addDefect(Defect(StandardDefects::LO_GAIN, element));
00205 else if (data.gain > StandardDefects::HI_GAIN.getParameter() * comparisonData.gain)
00206 defects.addDefect(Defect(StandardDefects::HI_GAIN, element));
00207
00208 if (data.offset < StandardDefects::LO_OFFSET.getParameter())
00209 defects.addDefect(Defect(StandardDefects::LO_OFFSET, element));
00210 else if (data.offset > StandardDefects::HI_OFFSET.getParameter())
00211 defects.addDefect(Defect(StandardDefects::HI_OFFSET, element));
00212
00213 if (data.noise < StandardDefects::UNBONDED.getParameter())
00214 defects.addDefect(Defect(StandardDefects::UNBONDED, element));
00215 else if (data.noise < StandardDefects::PARTBONDED.getParameter())
00216 defects.addDefect(Defect(StandardDefects::PARTBONDED, element));
00217 else if (data.noise > StandardDefects::NOISY.getParameter() * comparisonData.noise)
00218 defects.addDefect(Defect(StandardDefects::NOISY, element));
00219
00220 if (data.noise > StandardDefects::V_NOISY.getParameter() * comparisonData.noise)
00221 defects.addDefect(Defect(StandardDefects::V_NOISY, element));
00222 }
00223 }