#include "AliTPCkalmanTime.h"
#include "TTreeStream.h"
#include "TRandom.h"
AliTPCkalmanTime::AliTPCkalmanTime():
TNamed(),
fState(0),
fCovariance(0),
fTime(0)
{
}
AliTPCkalmanTime::AliTPCkalmanTime(Double_t time, Double_t xoff, Double_t k, Double_t sigmaxoff, Double_t sigmak):
TNamed(),
fState(0),
fCovariance(0),
fTime(0)
{
Init(time,xoff,k,sigmaxoff,sigmak);
}
void AliTPCkalmanTime::Init(Double_t time, Double_t xoff, Double_t k, Double_t sigmaxoff, Double_t sigmak){
fState = new TMatrixD(2,1);
fCovariance = new TMatrixD(2,2);
(*fState)(0,0)= xoff;
(*fState)(1,0)= k;
fTime=time;
(*fCovariance)(0,0)=sigmaxoff*sigmaxoff;
(*fCovariance)(1,1)=sigmak*sigmak;
(*fCovariance)(0,1)=0;
(*fCovariance)(1,0)=0;
}
void AliTPCkalmanTime::Propagate(Double_t time, Double_t sigma, TTreeSRedirector *debug){
if (!fCovariance) return;
if (!fState) return;
Double_t deltaT =time-fTime;
Double_t sigmaT2 =(deltaT*deltaT)*sigma*sigma;
if (debug){
(*debug)<<"matP"<<
"time="<<time<<
"fTime="<<fTime<<
"sigmaT2="<<sigmaT2<<
"cov.="<<fCovariance<<
"\n";
}
(*fCovariance)(0,0)+=sigmaT2;
fTime=time;
}
void AliTPCkalmanTime::Update(Double_t x, Double_t xerr, Double_t ptratio,TTreeSRedirector *debug){
static TMatrixD matHk(1,2);
static TMatrixD measR(1,1);
static TMatrixD matQk(2,2);
static TMatrixD vecZk(1,1);
static TMatrixD vecYk(1,1);
static TMatrixD matHkT(2,1);
static TMatrixD matSk(1,1);
static TMatrixD matKk(2,1);
static TMatrixD mat1(2,2);
static TMatrixD covXk2(2,2);
TMatrixD covXk(*fCovariance);
TMatrixD vecXk(*fState);
vecZk(0,0) = x;
measR(0,0) = xerr*xerr;
matHk(0,0)=1; matHk(0,1)= ptratio;
vecYk = vecZk-matHk*vecXk;
matHkT=matHk.T(); matHk.T();
matSk = (matHk*(covXk*matHkT))+measR;
matSk.Invert();
matKk = (covXk*matHkT)*matSk;
vecXk += matKk*vecYk;
mat1(0,0)=1; mat1(1,1)=1;
mat1(1,0)=0; mat1(0,1)=0;
covXk2= (mat1-(matKk*matHk));
covXk = covXk2*covXk;
if (debug){
(*debug)<<"matU"<<
"time="<<fTime<<
"x="<<x<<
"xerr="<<xerr<<
"pt="<<ptratio<<
"matHk.="<<&matHk<<
"matHkT.="<<&matHkT<<
"matSk.="<<&matSk<<
"matKk.="<<&matKk<<
"covXk2.="<<&covXk2<<
"covXk.="<<&covXk<<
"cov.="<<fCovariance<<
"vecYk.="<<&vecYk<<
"vecXk.="<<&vecXk<<
"vec.="<<fState<<
"\n";
}
(*fCovariance)=covXk;
(*fState)=vecXk;
}
void AliTPCkalmanTime::TestMC(const char * fname){
TTreeSRedirector *pcstream = new TTreeSRedirector(fname);
const Double_t kp0=0;
const Double_t kp1=1;
const Int_t klength=10*24*60*60;
const Double_t ksigmap0=0.001/(24*60*60.);
const Int_t deltat=5*60;
const Double_t kmessError=0.0005;
AliTPCkalmanTime testKalman;
for (Int_t iter=0; iter<100;iter++){
Double_t sp0 = kp0+gRandom->Gaus(0,0.01);
Double_t sp1 = kp1+gRandom->Gaus(0,0.2);
Double_t cp0 = sp0;
Double_t cp1 = sp1;
testKalman.Init(0,cp0+gRandom->Gaus(0,0.05),cp1+gRandom->Gaus(0,0.2),0.05,0.2);
Double_t dptratio= 0;
for (Int_t itime=0; itime<klength; itime+=deltat){
dptratio+=gRandom->Gaus(0,0.0005);
cp0 +=gRandom->Gaus(0,ksigmap0*deltat);
Double_t vdrift = cp0+dptratio*cp1+gRandom->Gaus(0,kmessError);
testKalman.Propagate(itime,ksigmap0,pcstream);
Double_t fdrift = (*(testKalman.fState))(0,0) + dptratio*(*(testKalman.fState))(1,0);
(*pcstream)<<"drift"<<
"iter="<<iter<<
"time="<<itime<<
"vdrift="<<vdrift<<
"fdrift="<<fdrift<<
"pt="<<dptratio<<
"sp0="<<sp0<<
"sp1="<<sp1<<
"cp0="<<cp0<<
"cp1="<<cp1<<
"vecXk.="<<testKalman.fState<<
"covXk.="<<testKalman.fCovariance<<
"\n";
testKalman.Update(vdrift,kmessError,dptratio,pcstream);
}
}
delete pcstream;
}