Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Related Pages

NoiseOccupancyAlgorithm.cpp

00001 #include "NoiseOccupancyAlgorithm.h"
00002 #include "AnalysisAlgorithmMap.h"
00003 #include "AnalysisService.h"
00004 #include "OpeTool.h"
00005 #include "SctData/Stat.h"
00006 #include "SctData/NoiseOccupancyTestResult.h"
00007 #include "SctData/OccupancyProjector.h"
00008 #include "SctData/FitScanResult.h"
00009 #include "SctData/FitObject.h"
00010 #include "SctData/ResponseCurve.h"
00011 #include "SctData/StandardDefects.h"
00012 #include "SctData/ConfigurationVariable.h"
00013 #include "Sct/UnsupportedOperationError.h"
00014 #include <CommonWithDsp/ABCD/ABCDscans.h>
00015 #include <TH1.h>
00016 #include <TGraphAsymmErrors.h>
00017 #include <string>
00018 
00019 using namespace std;
00020 using namespace boost;
00021 using namespace SctData;
00022 using namespace Sct;
00023 
00024 namespace SctAnalysis {
00025     
00026     NoiseOccupancyAlgorithm::~NoiseOccupancyAlgorithm() throw() {}
00027 
00028     shared_ptr<AnalysisAlgorithm> NoiseOccupancyAlgorithm::clone(const TestData& testData, const string& moduleName) const throw() {
00029     return shared_ptr<AnalysisAlgorithm>(new NoiseOccupancyAlgorithm(testData, moduleName, *this));
00030     }
00031 
00032     bool NoiseOccupancyAlgorithm::inMap = AnalysisAlgorithmMap::instance().setAlgorithm("NoiseOccupancyTest", auto_ptr<AnalysisAlgorithm>(new NoiseOccupancyAlgorithm()));
00033 
00034     //----------------------------------------------------------------------
00035     
00036     void NoiseOccupancyAlgorithm::loadData() {
00037     loadAllRaws();
00038     loadAllFits();
00039     }
00040     
00041     bool NoiseOccupancyAlgorithm::canAnalyze() const {
00042     return hasAllFits() && hasAllRaws();
00043     }
00044     
00045     shared_ptr<SctData::TestResult> NoiseOccupancyAlgorithm::createTestResult() const {
00046     return shared_ptr<TestResult>(new NoiseOccupancyTestResult());
00047     }
00048 
00049     void NoiseOccupancyAlgorithm::analyze() {
00050     shared_ptr<NoiseOccupancyTestResult> result = dynamic_pointer_cast<NoiseOccupancyTestResult>(getTestResult());
00051     const SctData::ConfigurationVariable& scanVariable = getRaw(0)->getHeader().getVariable();
00052     result->setScanVariable(scanVariable);
00053     const ModuleConfiguration config=getRaw(0)->getConfiguration();
00054     
00055     m_scanVariable = SctData::ConfigurationVariableIOHelper::getTypeRep(scanVariable);
00056     
00057     //cout << " NoiseOccupancyAlgorithm::analyze()" << endl;
00058     //Loop over chips
00059     for (unsigned int ichip=0; ichip<nChipModule; ++ichip) {
00060         // get an raw result occupany projector 
00061         OccupancyProjector opr(*getRaw(0) );
00062         opr.vetoSeriousDefects(getFit(0)->getDefects());
00063         // get the chip configuration
00064         const ChipConfiguration& chipconfig=config.getChipConfiguration(ichip);
00065         // get the new defects
00066         DefectList& defects = result->getDefects();
00067         result->getChipResult(ichip) = analyzeChip(ichip, opr, chipconfig, defects);
00068 
00069         const float fittedOffset = getFit(0)->getChipFit(ichip).getParameter(1);
00070         switch(m_scanVariable) {
00071         case ST_VTHR:
00072         result->getChipResult(ichip).offset = fittedOffset;
00073         break;
00074         case ST_TTHR:
00075         result->getChipResult(ichip).offset = fittedOffset + config.getChipConfiguration(ichip).getTrimTarget()*2.5;
00076         break;
00077         case ST_QTHR: {
00078         auto_ptr<ResponseCurve> rc = ResponseCurveMap::getMap().get(config.getChipConfiguration(ichip));
00079         result->getChipResult(ichip).offset = rc->getFunction()->Eval(fittedOffset);
00080             }
00081         break;
00082         default: {
00083             ostringstream oss;
00084         oss << "Internal Error: NoiseOccupancy Algorithm doesn't know how to deal with scan of type : " << m_scanVariable;
00085         throw Sct::UnsupportedOperationError(oss.str(), __FILE__, __LINE__);
00086             }
00087         }
00088     }
00089     OpeTool tool;
00090     shared_ptr<SctData::OpeResult> ope = tool.analyzeModule(*getRaw(0));
00091     result->setOpeResult(ope);
00092     result->setPassed(true);
00093     }
00094     
00095     ChipNOResult NoiseOccupancyAlgorithm::analyzeChip(const unsigned ichip, const OccupancyProjector& opr, const ChipConfiguration& chipconfig, DefectList& defects) {
00096     
00097     auto_ptr<TH1> occ = opr.getOccupancy("temp", ModuleElement::Chip(ichip)) ;
00098     auto_ptr<ResponseCurve> rc = ResponseCurveMap::getMap().get(chipconfig);
00099     shared_ptr<TF1> invFn = rc->getInverseFunction();
00100     
00101     //cout << "Config RC: " << (int)chipconfig.getRcFunctionIndex() << " params: " << chipconfig.getRcParam(0) << "  " << chipconfig.getRcParam(1) << "  " << chipconfig.getRcParam(2) << endl;
00102     //cout << "RC: " << rc->getIndex() << " params: " << rc->getFunction()->GetParameter(0) << "  " << rc->getFunction()->GetParameter(1) << "  " << rc->getFunction()->GetParameter(2) << endl;
00103 
00104     int ttbin=-1; //< trim target bin
00105     int fcbin=-1; //< 1 fC bin
00106 
00107     const float mVstep=2.5;  // number of mv per threshold step
00108     const float trimTargetmV=chipconfig.getTrimTarget()*mVstep;
00109 
00110     if (m_scanVariable==ST_VTHR){
00111       ttbin = occ->GetXaxis()->FindBin(chipconfig.getTrimTarget()*mVstep);
00112       fcbin = occ->GetXaxis()->FindBin(rc->getFunction()->Eval(1.));
00113     }else if(m_scanVariable==ST_TTHR){
00114       ttbin = occ->GetXaxis()->FindBin(0.);
00115       fcbin = occ->GetXaxis()->FindBin(rc->getFunction()->Eval(1.)-trimTargetmV);
00116     }else if(m_scanVariable==ST_QTHR){
00117       ttbin = occ->GetXaxis()->FindBin(invFn->Eval(trimTargetmV));
00118       fcbin = occ->GetXaxis()->FindBin(1.);
00119     }else{
00120       ostringstream oss;
00121       oss << "Internal Error: NoiseOccupancy Algorithm doesn't know how to deal with scan of type : " << m_scanVariable;
00122       throw Sct::UnsupportedOperationError(oss.str(), __FILE__, __LINE__);
00123     }
00124 
00125     //cout << "TrimTarget: " << chipconfig.getTrimTarget()*2.5 << " mV, bin: " << ttbin << " Occ: " << occ->GetBinContent(ttbin) << endl;
00126     //cout << "1fC: " << rc->getFunction()->Eval(1.) << " mV, bin: " << fcbin << " Occ: " << occ->GetBinContent(fcbin) << endl;
00127     
00128     // Construct TGraph of log occupancy against thr^2
00129     // project raw data from scan 0 of the test
00130     // Construction of TGraphAsymmetricErrors in this way because root is so 
00131     // inefficient at re-allocating memory for graphs
00132     double x[occ->GetNbinsX()];
00133     double xUpperErr[occ->GetNbinsX()];
00134     double xLowerErr[occ->GetNbinsX()];
00135     double yUpperErr[occ->GetNbinsX()];
00136     double yLowerErr[occ->GetNbinsX()];
00137     double y[occ->GetNbinsX()];
00138     unsigned int nBins = 0;
00139     
00140     double factor = 1;//11.3;  //warning - Should be 1.  Use 11.3 to simulate SCTDAQ
00141     
00142     for(int bin=1; bin<=occ->GetNbinsX(); ++bin){
00143         //We only want the "bottom" half of the threshold scan (threshold > 0.5)
00144         if (occ->GetBinContent(bin) > 0 && occ->GetBinContent(bin) < 0.5 ) {
00145         y[nBins] = log(occ->GetBinContent(bin));            
00146         yUpperErr[nBins] = log(occ->GetBinContent(bin) + factor*occ->GetBinError(bin)) - y[nBins];
00147         if (occ->GetBinContent(bin) - factor*occ->GetBinError(bin) > 0) {
00148             yLowerErr[nBins] = y[nBins] - log(occ->GetBinContent(bin) - factor*occ->GetBinError(bin));
00149         } else {
00150             yLowerErr[nBins] = yUpperErr[nBins];//occ->GetBinContent(bin);
00151             //yLowerErr[nBins] = 50;
00152             //cout << yLowerErr[nBins] << endl;         
00153         } 
00154         
00155         //Convert into fC:
00156         switch(m_scanVariable) {
00157         case ST_VTHR:
00158             x[nBins] = invFn->Eval(occ->GetBinCenter(bin));
00159             break;
00160         case ST_TTHR:
00161             x[nBins] = invFn->Eval(occ->GetBinCenter(bin)+trimTargetmV);
00162             break;
00163         case ST_QTHR:
00164             x[nBins] = occ->GetBinCenter(bin);
00165             break;
00166         default:
00167             ostringstream oss;
00168             oss << "Internal Error: NoiseOccupancy Algorithm doesn't know how to deal with scan of type : " << m_scanVariable;
00169             throw Sct::UnsupportedOperationError(oss.str(), __FILE__, __LINE__);
00170         }
00171         
00172         x[nBins] *= x[nBins];
00173         xUpperErr[nBins] = xLowerErr[nBins] = 0;            
00174                 
00175         nBins++;
00176         }
00177     }
00178 
00179         auto_ptr<TF1> f = NoiseOccupancyTestResult::createFitFunction();
00180 
00181         //Check for a dead chip (which hasnt been caused by vetoing due to other serious defect!)
00182         if (nBins == 0 && defects.defectSeverityEncompassingElement(ModuleElement::Chip(ichip)) < SERIOUS ) {
00183             defects.addDefect(Defect(StandardDefects::DEAD, ModuleElement::Chip(ichip)));
00184             shared_ptr<TGraphAsymmErrors> g( new TGraphAsymmErrors());
00185             return ChipNOResult(g, shared_ptr<TF1>(f), 0, 0);
00186         }
00187 
00188     shared_ptr<TGraphAsymmErrors> g( new TGraphAsymmErrors(nBins, x, y, xLowerErr, xUpperErr, yLowerErr, yUpperErr));
00189 
00190     //Don't use FitStrategy because there is no asymm errors method
00191 //  AnalysisService::instance().getFitStrategy().fitTGraphAsymmErrors(*g, *f);
00192     g->Fit(f.get(), "NQR");
00193 
00194     // Calculate mean and RMS NO at 1fC for channels in this chip
00195     Stats<double> occAtOnefC(nChannelChip);
00196     for (unsigned ichan=0; ichan < nChannelChip ; ++ichan ){
00197         unsigned channelInModule = ichip*nChannelChip + ichan;
00198 
00199         // get chip projection:
00200         char name[10];
00201         sprintf(name,"temp%d",channelInModule);
00202         auto_ptr<TH1> occChannel = opr.getOccupancy(name, ModuleElement::Channel(channelInModule)) ;
00203         
00204         // find 1 fC point - gets threshold in mV
00205         int bin = 0;
00206         switch(m_scanVariable) {
00207         case ST_VTHR: {
00208         double vthr = rc->getFunction()->Eval(1.);
00209         bin = occChannel->GetXaxis()->FindBin(vthr);        
00210             } break;
00211         case ST_TTHR: {
00212         double vthr = rc->getFunction()->Eval(1.);
00213         bin = occChannel->GetXaxis()->FindBin(vthr-trimTargetmV);       
00214             } break;
00215         case ST_QTHR:
00216         bin = occChannel->GetXaxis()->FindBin(1.0);
00217         break;
00218         default:
00219         ostringstream oss;
00220         oss << "Internal Error: NoiseOccupancy Algorithm doesn't know how to deal with scan of type : " << m_scanVariable;
00221         throw Sct::UnsupportedOperationError(oss.str(), __FILE__, __LINE__);
00222         }       
00223         
00224         //cout << "Thr bin="<<bin<<endl;
00225         occAtOnefC.modifyAt(ichan).value = occChannel->GetBinContent(bin);
00226         
00227         //cout << ichip << "\t " << ichan << "\t" << occAtOnefC.getAt(ichan).value << endl;
00228         
00229         // check for defects:
00230         if (occAtOnefC.getAt(ichan).value > StandardDefects::NO_HI.getParameter() ){
00231         defects.addDefect(Defect(StandardDefects::NO_HI, ModuleElement::Channel(channelInModule)));
00232                 occAtOnefC.modifyAt(ichan).valid=false;
00233         }
00234     }
00235     double mean = occAtOnefC.mean();
00236     //cout << "NoiseOcc mean = " << mean << endl;
00237     double rms  = sqrt( occAtOnefC.var() );
00238     
00239     return ChipNOResult(g,shared_ptr<TF1>(f),mean,rms);
00240     }
00241 } // end of namespace SctAnalysis

Generated on Thu Feb 10 02:40:11 2005 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5