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 "Sct/SctNames.h"
00013 #include "Sct/ConfigurationException.h"
00014 #include "CalibrationController/IS/TestData.h"
00015 #include "sctConf/configuration.h"
00016 #include <boost/shared_ptr.hpp>
00017
00018 using namespace Sct;
00019 using namespace SctData;
00020 using namespace boost;
00021
00022 using std::auto_ptr;
00023
00024 namespace SctAnalysis {
00025
00026 unsigned NPtGainAlgorithm::NPtGainAlgorithm::s_configurationWarnings=0;
00027
00028 shared_ptr<AnalysisAlgorithm> NPtGainAlgorithm::clone(const TestData& testData, const string& moduleName) const throw() {
00029 return shared_ptr<AnalysisAlgorithm>(new NPtGainAlgorithm(testData, moduleName, *this));
00030 }
00031
00032 NPtGainAlgorithm::NPtGainAlgorithm(const TestData& testData, const string& moduleName, const AnalysisAlgorithm& alg) throw() : AnalysisAlgorithm(testData, moduleName, alg) , m_mur_type(SctConfiguration::UNKNOWN), m_endcap_type(SctConfiguration::ENDCAP_UNKNOWN){
00033 try{
00034 boost::shared_ptr<SctConfiguration::Configuration> config=AnalysisService::instance().getConfiguration();
00035 if (!config.get()) throw ConfigurationException("Configuration not available", __FILE__, __LINE__);
00036 unsigned mur=0, mod=0, quadrant=0, number=0;
00037 int disk=0;
00038 config->translateFromSN(moduleName, mur, mod);
00039 m_mur_type=config->getMURType(mur);
00040 if (m_mur_type==SctConfiguration::ENDCAP){
00041 config->translateToEndcap(mur, mod, disk, quadrant, number);
00042 m_endcap_type= SctConfiguration::getEndcapType(disk, number);
00043 }
00044 }catch(std::exception& e){
00045 const unsigned max_warnings=5;
00046
00047 if (s_configurationWarnings<max_warnings){
00048 std::ostringstream msg;
00049 if (s_configurationWarnings==max_warnings-1){
00050 msg << "NO MORE WARNINGS OF THIS TYPE WILL BE PRINTED:";
00051 }
00052 msg << "Could not find geometry-dependant expected noise levels for test "
00053 << testData.testName
00054 << " run:" << testData.runNumber << " scan:" << testData.startScanNumber
00055 << " module:" << moduleName
00056 << " Problem in configuration look-up: "<<endl
00057 << e.what();
00058 Sct::SctNames::Mrs() << MRS_TEXT(msg.str()) << "CONFIG_LOOKUP_FAIL" << MRS_WARNING << ENDM;
00059 s_configurationWarnings++;
00060 }
00061 }
00062 }
00063
00064 shared_ptr<ResponseCurve> NPtGainAlgorithm::getResponseCurve(unsigned int nPts) throw(LogicError){
00065 if (nPts <= 3) {
00066 return shared_ptr<ResponseCurve> (new LinearResponseCurve());
00067 } else {
00068 return shared_ptr<ResponseCurve> (new ExponentialResponseCurve());
00069 }
00070 }
00071
00072 void NPtGainAlgorithm::setResponseCurve(auto_ptr<ResponseCurve> rc) throw() {
00073 static shared_ptr<ResponseCurve> s_responseCurve=shared_ptr<ResponseCurve>(rc.release());
00074 }
00075
00076 bool NPtGainAlgorithm::inMap = AnalysisAlgorithmMap::instance().setAlgorithm("NPtGainTest", auto_ptr<AnalysisAlgorithm>(new NPtGainAlgorithm()));
00077
00078 shared_ptr<SctData::TestResult> NPtGainAlgorithm::createTestResult() const {
00079 shared_ptr<NPtGainTestResult> result (new NPtGainTestResult());
00080 result->setChipDataSize(nChipModule);
00081 result->setChannelDataSize(nChannelModule);
00082
00083 return result;
00084 }
00085
00086 void NPtGainAlgorithm::loadData() {
00087 loadAllFits();
00088 }
00089
00090 bool NPtGainAlgorithm::canAnalyze() const {
00091 return hasAllFits();
00092 }
00093
00094 void NPtGainAlgorithm::analyze() {
00095 shared_ptr<NPtGainTestResult> result = dynamic_pointer_cast<NPtGainTestResult> ( getTestResult() );
00096 result->setScanVariable(getFit(0)->getHeader().getVariable());
00097 try{
00098 result->setSpecialScanPointValue(2.0);
00099 } catch (Sct::Throwable& e){
00100 e.sendToMrs(MRS_ERROR);
00101 }
00102
00103
00104 nOnePointTrimmed = 0;
00105 nTwoPointTrimmed = 0;
00106
00107
00108 for (unsigned int i=0; i<nChipModule; ++i) {
00109 setupGraph(i, ModuleElement::Chip(i), &FitScanResult::getChipFit, *result, result->getChipData(i), true);
00110 doFit(i, &FitScanResult::getChipFit, *result, result->getChipData(i));
00111 }
00112
00113
00114 for (unsigned int i=0; i<nChannelModule; ++i) {
00115 ModuleElement element = ModuleElement::Channel(i);
00116 setupGraph(i, element, &FitScanResult::getChannelFit, *result, result->getChannelData(i), true);
00117
00118
00119 if (getFit(0)->getConfiguration().channelIsMasked(i)) continue;
00120
00121 doFit(i, &FitScanResult::getChannelFit, *result, result->getChannelData(i));
00122 doDefect(element, result->getDefects(),
00123 result->getChannelData(i), result->getChipData(i/nChannelChip));
00124 }
00125
00126 doSlopes(*result);
00127
00128
00129
00130
00131 const DefectList::DefectCollection& defects = result->getDefects().getAllDefects();
00132 bool passed = true;
00133 unsigned int totalChannels = 0;
00134 for (DefectList::DefectCollection::const_iterator i=defects.begin(); i!=defects.end(); ++i) {
00135
00136 if (i->getPrototype().getSeverity() > DODGY) {
00137 totalChannels += i->getModuleElement().getNChannels();
00138 if (i->getModuleElement().getNChannels()>7) {
00139 passed = false;
00140 break;
00141 }
00142 }
00143 }
00144 if (totalChannels > 15) passed = false;
00145
00146
00147 if (nOnePointTrimmed != 0) {
00148 ostringstream oss;
00149 oss << nOnePointTrimmed << " channels had 1 point trimmed from fit";
00150 result->addComment(oss.str());
00151 }
00152 if (nTwoPointTrimmed != 0) {
00153 ostringstream oss;
00154 oss << nTwoPointTrimmed << " channels had 2 (or more) points trimmed from fit";
00155 result->addComment(oss.str());
00156 }
00157
00158 result->setPassed(passed);
00159 }
00160
00161
00162 void NPtGainAlgorithm::setupGraph(unsigned int id, const ModuleElement& element, getFitFunction fitFunc, NPtGainTestResult& test, NPtGainTestResultData& testData, bool trimPoints) throw(LogicError) {
00163
00164 unsigned int lastPoint = test.getNScans()-1;
00165
00166 if (trimPoints && test.getNScans() > 5) {
00167 double sigmaCut = 0;
00168 for (unsigned int i=0; i<test.getNScans(); ++i) {
00169 sigmaCut+= (getFit(i).get()->*fitFunc)(id).getParameter("Sigma");
00170 }
00171 sigmaCut *= 1.5/test.getNScans();
00172
00173 for (unsigned int i=test.getNScans() - 1; i>4; --i) {
00174
00175 const DefectList& defects = getFit(i)->getDefects();
00176 if (test.getTestPointAt(i) > 5 && ((getFit(i).get()->*fitFunc)(id).getParameter("Sigma") > sigmaCut ||
00177 defects.defectSeverityAffectingElement(element) >= SERIOUS)) {
00178 lastPoint = i-1;
00179 }
00180 }
00181
00182
00183 if (lastPoint != test.getNScans() - 1) {
00184 if (element.isChip()) {
00185 ostringstream oss;
00186 oss << "Points trimed from response curve for chip: " << id << ". Last point " << lastPoint << " with charge " << test.getTestPointAt(lastPoint);
00187 test.addComment(oss.str());
00188 } else {
00189 if (lastPoint+1-test.getNScans() == 1) ++nOnePointTrimmed;
00190 else ++nTwoPointTrimmed;
00191 }
00192 }
00193 }
00194
00195 mergeDefects(test, element, lastPoint);
00196
00197 shared_ptr<TGraph> g (new TGraph(lastPoint+1));
00198 if (!g)
00199 throw InvariantViolatedError("NPtGainTestResult::setupGraph couldn't make TGraph", __FILE__, __LINE__);
00200
00201
00202 for (unsigned int j = 0; j <= lastPoint; ++j) {
00203
00204
00205 unsigned ichip=element.getFirst()/nChannelChip;
00206 const ChipConfiguration& chipconfig = getFit(j)->getConfiguration().getChipConfiguration(ichip);
00207
00208 const unsigned char cal = chipconfig.getCalCharge();
00209
00210 const float qcal= static_cast<float>(cal)*0.0625*chipconfig.getCalFactor();
00211 g->SetPoint(j, qcal, (getFit(j).get()->*fitFunc)(id).getParameter("Mean"));
00212
00213
00214
00215
00216
00217
00218 }
00219 testData.graph = g;
00220
00221 shared_ptr<ResponseCurve> r=getResponseCurve(test.getNScans());
00222 testData.rc = r;
00223 }
00224
00225 void NPtGainAlgorithm::mergeDefects(NPtGainTestResult& test, const ModuleElement& element, unsigned int lastPoint) {
00226 for (unsigned int i=0; i<lastPoint; ++i) {
00227 auto_ptr<DefectList> defects = getFit(i)->getDefects().getDefectsEncompassingElement(element);
00228 const DefectList::DefectCollection& allDefects = defects->getAllDefects();
00229 for (DefectList::DefectCollection::const_iterator it=allDefects.begin(); it!=allDefects.end(); ++it) {
00230 test.getDefects().addDefect(Defect(it->getPrototype(), element));
00231 }
00232 }
00233 }
00234
00235 void NPtGainAlgorithm::doFit(unsigned int id, getFitFunction fitFunc, NPtGainTestResult& test,
00236 NPtGainTestResultData& testData) throw(LogicError) {
00237
00238
00239
00240 testData.rc->getFunction()->SetRange(0,10);
00241 AnalysisService::instance().getFitStrategy().fitTGraph(*testData.graph, *testData.rc->getFunction() );
00242
00243 testData.gain = testData.rc->getGain(test.getSpecialScanPointValue());
00244
00245 shared_ptr<const FitScanResult> specialFit = getFit(test.getSpecialScanIndex());
00246
00247 if (testData.gain != 0) testData.noise = 6250 * (specialFit.get()->*fitFunc)(id).getParameter("Sigma") / testData.gain;
00248 else testData.noise = 0;
00249 testData.offset= testData.rc->getFunction()->Eval(0.);
00250 }
00251
00252 void NPtGainAlgorithm::doDefect(const ModuleElement& element, DefectList& defects,
00253 const NPtGainTestResultData& data,
00254 const NPtGainTestResultData& comparisonData) throw(LogicError) {
00255
00256 if (data.gain < StandardDefects::VLO_GAIN.getParameter() * comparisonData.gain){
00257 defects.addDefect(Defect(StandardDefects::VLO_GAIN, element));
00258 }
00259 if (data.gain < StandardDefects::LO_GAIN.getParameter() * comparisonData.gain){
00260 defects.addDefect(Defect(StandardDefects::LO_GAIN, element));
00261 } else if (data.gain > StandardDefects::HI_GAIN.getParameter() * comparisonData.gain){
00262 defects.addDefect(Defect(StandardDefects::HI_GAIN, element));
00263 }
00264
00265 if (data.offset < StandardDefects::LO_OFFSET.getParameter()) {
00266 defects.addDefect(Defect(StandardDefects::LO_OFFSET, element));
00267 } else if (data.offset > StandardDefects::HI_OFFSET.getParameter()) {
00268 defects.addDefect(Defect(StandardDefects::HI_OFFSET, element));
00269 }
00270
00271 bool short_module = ( m_mur_type==SctConfiguration::ENDCAP &&
00272 ( m_endcap_type==SctConfiguration::ENDCAP_SHORT_MIDDLE
00273 || m_endcap_type==SctConfiguration::ENDCAP_INNER) );
00274
00275 if (data.noise < StandardDefects::UNBONDED.getParameter()) {
00276 defects.addDefect(Defect(StandardDefects::UNBONDED, element));
00277 } else if (data.noise < StandardDefects::PARTBONDED.getParameter() && !short_module) {
00278 defects.addDefect(Defect(StandardDefects::PARTBONDED, element));
00279 } else if (data.noise > StandardDefects::NOISY.getParameter() * comparisonData.noise) {
00280 defects.addDefect(Defect(StandardDefects::NOISY, element));
00281 }
00282
00283 if (data.noise > StandardDefects::V_NOISY.getParameter() * comparisonData.noise) {
00284 defects.addDefect(Defect(StandardDefects::V_NOISY, element));
00285 }
00286 }
00287
00288 void NPtGainAlgorithm::doSlopes(NPtGainTestResult& r){
00289 shared_ptr<TGraph> noise = r.getNoiseGraph();
00290 shared_ptr<TGraph> gain = r.getGainGraph();
00291 shared_ptr<TGraph> offset = r.getOffsetGraph();
00292
00293
00294 doSlopes(noise, r.noiseSlope);
00295
00296
00297 doSlopes(gain, r.gainSlope);
00298
00299
00300 doSlopes(offset, r.offsetSlope);
00301 for (unsigned ichip=0; ichip<nChipModule; ++ichip){
00302 if (abs(r.noiseSlope[ichip]) > StandardDefects::NOISE_SLOPE.getParameter()){
00303 r.getDefects().addDefect(Defect(StandardDefects::NOISE_SLOPE, ModuleElement::Chip(ichip)));
00304 }
00305 if (abs(r.gainSlope[ichip])>StandardDefects::GAIN_SLOPE.getParameter()){
00306 r.getDefects().addDefect(Defect(StandardDefects::GAIN_SLOPE, ModuleElement::Chip(ichip)));
00307 }
00308 if (abs(r.offsetSlope[ichip])>StandardDefects::OFFSET_SLOPE.getParameter()){
00309 r.getDefects().addDefect(Defect(StandardDefects::OFFSET_SLOPE, ModuleElement::Chip(ichip)));
00310 }
00311
00312 }
00313
00314 }
00315
00316 void NPtGainAlgorithm::doSlopes(shared_ptr<TGraph> graph, Sct::RangedVector<float>& result){
00317 result.resize(nChipModule);
00318
00319 for (unsigned ichip=0; ichip<nChipModule; ++ichip){
00320 double xsum=0.;
00321 double x2sum=0.;
00322 double ysum=0.;
00323 double xysum=0.;
00324 unsigned n=0;
00325 for (unsigned ichan = 0; ichan<nChannelChip; ++ichan) {
00326 double x=0;
00327 double y=0;
00328 int bin=ichip*nChannelChip + ichan;
00329 graph->GetPoint(bin, x, y);
00330 if (y>0.0001){
00331 xsum += x;
00332 ysum += y;
00333 x2sum += x*x;
00334 xysum += x*y;
00335 ++n;
00336 }
00337 }
00338 const double det = ( n*x2sum ) - ( xsum*xsum );
00339 if (det>0) {
00340 result[ichip]= (n*xysum - xsum*ysum) / det;
00341 }else{
00342 result[ichip]=0.;
00343 }
00344 }
00345 }
00346
00347 }