00001 #include "ThresholdFitAlgorithm2.h"
00002 #include "FitAlgorithmMap.h"
00003 #include "FitterMode.h"
00004 #include "SctData/mVThresholdVariable.h"
00005 #include "SctData/mVfromTrimTargetThresholdVariable.h"
00006 #include "SctData/ErfcFitObject.h"
00007 #include "SctData/StandardDefects.h"
00008 #include <TH1.h>
00009
00010 using namespace SctData;
00011
00012 namespace SctFitter {
00013
00014
00015 ThresholdFitAlgorithm2::ThresholdFitAlgorithm2() throw() {
00016 FitterMode mode = getMode();
00017 mode.fitNone();
00018 mode.fitChips();
00019 mode.fitChannels() ;
00020 mode.doSummary(false);
00021 setMode(mode);
00022 }
00023
00024 bool ThresholdFitAlgorithm2::putInMap() throw() {
00025 inMap = FitAlgorithmMap::instance().setAlgorithm(mVThresholdVariable::instance().getVariableName(), auto_ptr<FitAlgorithm>(new ThresholdFitAlgorithm2));
00026 inMap |= FitAlgorithmMap::instance().setAlgorithm(mVfromTrimTargetThresholdVariable::instance().getVariableName(), auto_ptr<FitAlgorithm>(new ThresholdFitAlgorithm2));
00027 return inMap;
00028 }
00029
00030 bool ThresholdFitAlgorithm2::inMap = false;
00031
00032 void ThresholdFitAlgorithm2::doFit(const TH1& hist, FitObject& fitObject,
00033 const ModuleElement& element, DefectList& defects) const throw (LogicError) {
00034
00035 double amp = hist.GetBinContent(1);
00036 unsigned int ampCount = 1;
00037 double mean = 0;
00038 double meanx2 = 0;
00039 double norm = 0;
00040 double last = hist.GetBinContent(1);
00041 bool flatTop = true;
00042
00043 for (unsigned int i=2; i<=hist.GetNbinsX(); ++i) {
00044 double bin = hist.GetBinContent(i);
00045 double bincentre = hist.GetBinLowEdge(i);
00046 double error = hist.GetBinError(i);
00047 double diff = bin-last;
00048 norm += diff;
00049 if (flatTop && error < (amp/ampCount - bin)) {
00050
00051 flatTop = false;
00052 }
00053 if (flatTop) {
00054 amp += bin;
00055 ++ampCount;
00056 }
00057 mean += diff * bincentre;
00058 meanx2 += diff*bincentre* bincentre;
00059 last = bin;
00060 }
00061
00062
00063
00064 amp /= ampCount;
00065
00066 mean /= norm;
00067
00068 double sigma = sqrt(meanx2/norm - mean * mean);
00069
00070
00071
00072
00073 fitObject.setParameter(0, amp);
00074 fitObject.setParameter(1, mean);
00075 fitObject.setParameter(2, sigma);
00076 fitObject.setParError(0, 0);
00077 fitObject.setParError(1, 0);
00078 fitObject.setParError(2, 0);
00079 }
00080
00081
00082 auto_ptr<FitObject> ThresholdFitAlgorithm2::getPrototype() const throw() {
00083 return auto_ptr<FitObject> (new ErfcFitObject());
00084 }
00085
00086 void ThresholdFitAlgorithm2::guessParameters(const TH1& hist, FitObject& fitOb) const throw (LogicError, MathsError) {
00087 }
00088
00089
00090 void ThresholdFitAlgorithm2::checkForDefects(const FitObject& fo, const ModuleElement& element,
00091 DefectList& defects) const {
00092 if (fo.getNDF() && fo.getChiSquared()/fo.getNDF() > 5000 ) {
00094 defects.addDefect(Defect(StandardDefects::DEAD,element));
00095 }
00096
00097 }
00098
00099 void ThresholdFitAlgorithm2::checkForDefects(const TH1& hist, const ModuleElement& element,
00100 DefectList& defects) const throw (LogicError) {
00101 }
00102
00103 void ThresholdFitAlgorithm2::createSummaryHistograms(FitScanResult& fits) const throw() {
00104 }
00105 }