00001 #include "TAPGeneratorFactory.h"
00002 #include "GaussFitAPGenerator.h"
00003 #include "TPulseIsland.h"
00004 #include "TGaussFitAnalysedPulse.h"
00005 #include "ExportPulse.h"
00006 #include "Functions.h"
00007 #include <iostream>
00008 #include <sstream>
00009 #include <TF1.h>
00010 #include <TFitResult.h>
00011 #include <TMath.h>
00012 using std::cout;
00013 using std::endl;
00014
00015 const char* GaussFitAPGenerator::fFitName="GausLinear";
00016
00017 GaussFitAPGenerator::GaussFitAPGenerator(TAPGeneratorOptions* opts):
00018 TVAnalysedPulseGenerator("GaussFit",opts){
00019
00020 fMean=opts->GetDouble("time",200);
00021 fWidth=opts->GetDouble("width",60);
00022 fAmplitude=opts->GetDouble("amplitude",200);
00023 fPedestal=opts->GetDouble("pedestal",0);
00024 fGradient=opts->GetDouble("gradient",0);
00025
00026 fFitFunc=new TF1(fFitName,functions::gauss_lin,0,1000,5);
00027 fFitFunc->SetParName(kPedestal,"pedestal");
00028 fFitFunc->SetParName(kAmplitude,"amplitude");
00029 fFitFunc->SetParName(kWidth,"width");
00030 fFitFunc->SetParName(kGradient,"gradient");
00031 fFitFunc->SetParName(kTime,"time");
00032 }
00033
00034 int GaussFitAPGenerator::ProcessPulses(
00035 const PulseIslandList& pulseList,
00036 AnalysedPulseList& analysedList){
00037
00038
00039
00040
00041 TGaussFitAnalysedPulse* tap;
00042 FittedVals tap_data;
00043 int index;
00044 for (PulseIslandList::const_iterator tpi=pulseList.begin();
00045 tpi!=pulseList.end(); tpi++){
00046
00047
00048 index=tpi-pulseList.begin();
00049 FitPulse(*tpi,index,tap_data);
00050
00051
00052 tap = MakeNewTAP<TGaussFitAnalysedPulse>(index);
00053
00054
00055 tap->SetAmplitude ( tap_data.value[kAmplitude] , tap_data.error[kAmplitude] );
00056 tap->SetPedestal ( tap_data.value[kPedestal] , tap_data.error[kPedestal] );
00057 tap->SetGradient ( tap_data.value[kGradient] , tap_data.error[kGradient] );
00058 tap->SetWidth ( tap_data.value[kWidth] , tap_data.error[kWidth] );
00059 tap->SetChi2 ( tap_data.chi2 );
00060 tap->SetFitStatus ( tap_data.status );
00061
00062 if(ExportPulse::Instance() && tap_data.status!=0){
00063 ExportPulse::Instance()->AddToExportList(tap);
00064 }
00065
00066
00067 analysedList.push_back(tap);
00068 }
00069
00070
00071 return 0;
00072 }
00073
00074 void GaussFitAPGenerator::FitPulse(const TPulseIsland* tpi,const int& id,FittedVals& tap){
00075
00076
00077 std::stringstream histname("tpi_");
00078 histname<<id;
00079 TH1I* hPulse = tpi->GetPulseWaveform(histname.str(), histname.str());
00080
00081
00082 fFitFunc->SetParameters(fPedestal,fGradient,fAmplitude,fMean,fWidth);
00083 fFitFunc->SetRange(0,tpi->GetPulseLength());
00084
00085
00086 std::string options ="S R";
00087 if(!Debug()) options+=" Q";
00088 TFitResultPtr result=hPulse->Fit(fFitFunc,options.c_str());
00089
00090
00091 tap.value[kAmplitude] = fFitFunc->GetParameter(kAmplitude);
00092 tap.value[kTime ] = fFitFunc->GetParameter(kTime);
00093 tap.value[kPedestal ] = fFitFunc->GetParameter(kPedestal);
00094 tap.value[kGradient ] = fFitFunc->GetParameter(kGradient);
00095 tap.value[kWidth ] = fFitFunc->GetParameter(kWidth);
00096
00097
00098 tap.error[kAmplitude] = fFitFunc->GetParError(kAmplitude);
00099 tap.error[kTime ] = fFitFunc->GetParError(kTime);
00100 tap.error[kPedestal ] = fFitFunc->GetParError(kPedestal);
00101 tap.error[kGradient ] = fFitFunc->GetParError(kGradient);
00102 tap.error[kWidth ] = fFitFunc->GetParError(kWidth);
00103
00104
00105 tap.chi2 = result->Chi2();
00106 tap.status = result;
00107
00108 delete hPulse;
00109
00110 }
00111
00112 const char* GaussFitAPGenerator::TapType(){
00113 return TGaussFitAnalysedPulse::Class()->GetName();
00114 }
00115
00116
00117
00118
00119
00120
00121
00122 ALCAP_TAP_GENERATOR(GaussFit,time,width,amplitude,pedestal,gradient);