1 #include "AliParamSolver.h"
2 #include "AliSymMatrix.h"
6 ClassImp(AliParamSolver)
8 //______________________________________________________________________________________
9 AliParamSolver::AliParamSolver()
10 : fMatrix(0),fSolGlo(0),fSolLoc(0),fMaxGlobal(0),fNGlobal(0),fNPoints(0),fMaxPoints(0),
11 fRHSGlo(0),fRHSLoc(0),fMatGamma(0),fMatG(0),fCovDGl(0)
13 // default constructor
16 //______________________________________________________________________________________
17 AliParamSolver::AliParamSolver(Int_t maxglo,Int_t locbuff)
18 : fMatrix(0),fSolGlo(0),fSolLoc(0),fMaxGlobal(maxglo),fNGlobal(maxglo),fNPoints(0),fMaxPoints(0),
19 fRHSGlo(0),fRHSLoc(0),fMatGamma(0),fMatG(0),fCovDGl(0)
21 // constructor for nglo globals
25 //______________________________________________________________________________________
26 AliParamSolver::AliParamSolver(AliParamSolver& src)
27 : TObject(src),fMatrix(0),fSolGlo(0),fSolLoc(0),fMaxGlobal(src.fMaxGlobal),fNGlobal(src.fNGlobal),
28 fNPoints(0),fMaxPoints(0),fRHSGlo(0),fRHSLoc(0),fMatGamma(0),fMatG(0),fCovDGl(0)
37 //______________________________________________________________________________________
38 AliParamSolver& AliParamSolver::operator=(const AliParamSolver& src)
40 // assignment operator
41 if (this==&src) return *this;
42 TObject::operator=(src);
43 if (src.fMatrix && (fNGlobal!=src.fNGlobal || fMaxPoints<src.fNPoints)) {
44 fNGlobal = src.fNGlobal;
45 fMaxGlobal = src.fMaxGlobal;
46 if (fMatrix) delete fMatrix; fMatrix = 0;
47 if (fSolGlo) delete[] fSolGlo; fSolGlo = 0;
48 if (fSolLoc) delete[] fSolLoc; fSolLoc = 0;
49 if (fRHSGlo) delete[] fRHSGlo; fRHSGlo = 0;
50 if (fRHSLoc) delete[] fRHSLoc; fRHSLoc = 0;
51 if (fMatGamma) delete[] fMatGamma; fMatGamma = 0;
52 if (fMatG) delete[] fMatG; fMatG = 0;
53 if (fCovDGl) delete[] fCovDGl; fCovDGl = 0;
57 (*fMatrix) = *(src.fMatrix);
58 memcpy(fSolGlo,src.fSolGlo,fNGlobal*sizeof(double));
59 memcpy(fSolLoc,src.fSolLoc,fNPoints*sizeof(double));
60 memcpy(fRHSGlo,src.fRHSGlo,fNGlobal*sizeof(double));
61 memcpy(fRHSLoc,src.fRHSLoc,fNPoints*sizeof(double));
62 memcpy(fMatGamma,src.fMatGamma,fNPoints*sizeof(double));
63 memcpy(fMatG,src.fMatG,fNPoints*fNGlobal*sizeof(double));
69 //______________________________________________________________________________________
70 AliParamSolver::~AliParamSolver()
83 //______________________________________________________________________________________
84 Bool_t AliParamSolver::SolveGlobals(Bool_t obtainCov)
86 if (fNPoints<fNGlobal/3) {
87 AliError(Form("Number of points: %d is not enough for %d globals",fNPoints,fNGlobal));
91 if (!TestBit(kBitGloSol)) {
92 if (!fMatrix->SolveChol(fRHSGlo, fSolGlo, obtainCov)) {
93 AliError("Solution Failed");
97 if (obtainCov) SetBit(kBitCInv);
102 //______________________________________________________________________________________
103 Bool_t AliParamSolver::SolveLocals()
105 const double kTiny = 1e-16;
106 if (TestBit(kBitLocSol)) return kTRUE;
107 if (!TestBit(kBitGloSol)) {
108 AliError("Cannot solve for Locals before SolveGlobals is called");
111 for (int i=fNPoints;i--;) {
112 if (TMath::Abs(fMatGamma[i])<kTiny) {fSolLoc[i] = 0; continue;}
113 double beta = fRHSLoc[i];
114 double *mtG = fMatG + i*fNGlobal; // G_i
115 for (int j=fNGlobal;j--;) beta -= mtG[j]*fSolGlo[j];
116 fSolLoc[i] = beta/fMatGamma[i]; // Gamma^-1 * (beta - G_i * a)
122 //______________________________________________________________________________________
123 AliSymMatrix* AliParamSolver::GetCovMatrix()
125 if (!TestBit(kBitGloSol)) {
126 AliError("Cannot obtain Cov.Matrix before SolveGlobals is called");
129 if (TestBit(kBitCInv)) return fMatrix;
131 if (fMatrix->InvertChol()) {
138 //______________________________________________________________________________________
139 Bool_t AliParamSolver::Solve(Bool_t obtainCov)
141 return (SolveGlobals(obtainCov) && SolveLocals()) ? kTRUE : kFALSE;
144 //______________________________________________________________________________________
145 void AliParamSolver::AddEquation(const Double_t* dGl,const Double_t *dLc,const Double_t *res, const Double_t *covI,Double_t sclErrI)
147 // add the measured point to chi2 normal equations
149 // dGl : NGlo x 3 matrix of derivative for each of the 3 coordinates vs global params
150 // dLc : 3-vector of derivative for each coordinate vs local param
151 // res : residual of the point (extrapolated - measured)
152 // covI: 3 x (3+1)/2 matrix of the (inverted) errors (symmetric)
153 // sclErrI: scaling coefficient to apply to inverted errors (used if >0)
154 // The contribution of the point to chi2 is V * covI * V
155 // with component of the vector V(i) = dGl[i]*glob + dLc[i]*loc - res[i]
157 // Instead of the NGlo + NMeasPoints size matrix we create only NGlo size matrix using the
158 // reduction a la millepede : http://www.desy.de/~blobel/millepede1.ps
159 const double kTiny = 1e-16;
161 if (fNPoints+1 == fMaxPoints) ExpandStorage((fNPoints+1)*2);
162 ResetBit(kBitGloSol|kBitLocSol);
164 if (TestBit(kBitCInv)) { // solution was obtained and the matrix was inverted for previous points
165 fMatrix->InvertChol();
169 double *mtG = fMatG + fNPoints*fNGlobal; // G_i
170 double &beta = fRHSLoc[fNPoints];
171 double &gamma = fMatGamma[fNPoints];
175 cDl[kX] = covI[kXX]*dLc[kX] + covI[kXY]*dLc[kY] + covI[kXZ]*dLc[kZ];
176 cDl[kY] = covI[kXY]*dLc[kX] + covI[kYY]*dLc[kY] + covI[kYZ]*dLc[kZ];
177 cDl[kZ] = covI[kXZ]*dLc[kX] + covI[kYZ]*dLc[kY] + covI[kZZ]*dLc[kZ];
178 if (sclErrI>0) { cDl[kX] *= sclErrI; cDl[kY] *= sclErrI; cDl[kZ] *= sclErrI;}
180 for (int i=fNGlobal;i--;) {
181 const double *dGli = dGl+i*3; // derivatives of XYZ vs i-th global param
182 double *cDgi = fCovDGl+i*3; // cov * dR/dGl_i
183 cDgi[kX] = covI[kXX]*dGli[kX] + covI[kXY]*dGli[kY] + covI[kXZ]*dGli[kZ];
184 cDgi[kY] = covI[kXY]*dGli[kX] + covI[kYY]*dGli[kY] + covI[kYZ]*dGli[kZ];
185 cDgi[kZ] = covI[kXZ]*dGli[kX] + covI[kYZ]*dGli[kY] + covI[kZZ]*dGli[kZ];
186 if (sclErrI>0) { cDgi[kX] *= sclErrI; cDgi[kY] *= sclErrI; cDgi[kZ] *= sclErrI;}
188 mtG[i] = cDl[kX]*dGli[kX] + cDl[kY]*dGli[kY] + cDl[kZ]*dGli[kZ]; // dR/dGl_i * cov * dR/dLoc
190 beta = res[kX]*cDl[kX] + res[kY]*cDl[kY] + res[kZ]*cDl[kZ]; //RHS: res*cov*dR/dloc
191 gamma = dLc[kX]*cDl[kX] + dLc[kY]*cDl[kY] + dLc[kZ]*cDl[kZ]; //RHS: dR/dloc*cov*dR/dloc
192 double locSol = TMath::Abs(gamma)<kTiny ? 0. : beta/gamma; //local solution: gamma^-1 beta
194 AliSymMatrix &matC = *fMatrix;
195 for (int i=fNGlobal;i--;) {
196 const double *cDgi = fCovDGl+i*3; // cov * dR/dGl_i
198 fRHSGlo[i] += cDgi[kX]*res[kX] + cDgi[kY]*res[kY] + cDgi[kZ]*res[kZ] // b_i = dR/dGi * cov * res
199 - mtG[i]*locSol; // - [G gamma^-1 beta ]_i
201 for (int j=i+1;j--;) {
202 // const double *cDgj = fCovDGl+j*3; // cov * dR/dGl_j
203 const double *dGlj = dGl+j*3; // derivatives of XYZ vs i-th global param
204 double add = dGlj[kX]*cDgi[kX] + dGlj[kY]*cDgi[kY] + dGlj[kZ]*cDgi[kZ]; // C_ij = dR/dGi * cov * dR/dGj
205 if (TMath::Abs(gamma)>kTiny) add -= mtG[i]*mtG[j]/gamma; // - [G gamma^-1 T(G) ]_ij
214 //______________________________________________________________________________________
215 void AliParamSolver::AddConstraint(Int_t parID, Double_t val, Double_t err2inv)
217 // add gassian constriant to parameter parID
218 if (parID>=fNGlobal) {
219 AliError(Form("Attempt to constraint non-existing parameter %d",parID));
223 (*fMatrix)(parID,parID) += err2inv;
224 fRHSGlo[parID] += val*err2inv;
228 //______________________________________________________________________________________
229 void AliParamSolver::Init(Int_t npini)
231 // create storage assuming maximum npini measured points
232 fMatrix = new AliSymMatrix(fMaxGlobal);
233 fSolGlo = new Double_t[fMaxGlobal];
234 fRHSGlo = new Double_t[fMaxGlobal];
236 fCovDGl = new Double_t[3*fMaxGlobal];
237 ExpandStorage(npini);
238 fMatrix->SetSizeUsed(fNGlobal);
242 //______________________________________________________________________________________
243 void AliParamSolver::ExpandStorage(Int_t newSize)
245 // increase space to newSize measured points
246 newSize = newSize>fMaxPoints ? newSize : fMaxPoints+1;
248 tmp = new Double_t[newSize];
249 if (fSolLoc) delete[] fSolLoc;
252 tmp = new Double_t[newSize];
254 memcpy(tmp,fMatGamma,fNPoints*sizeof(Double_t));
259 tmp = new Double_t[newSize];
261 memcpy(tmp,fRHSLoc,fNPoints*sizeof(Double_t));
266 tmp = new Double_t[newSize*fMaxGlobal];
268 memcpy(tmp,fMatG,fNPoints*fMaxGlobal*sizeof(Double_t));
273 fMaxPoints = newSize;
277 //______________________________________________________________________________________
278 void AliParamSolver::Clear(Option_t*)
282 for (int i=fNGlobal;i--;) fRHSGlo[i] = 0;
283 ResetBit(kBitGloSol | kBitLocSol | kBitCInv);
286 //______________________________________________________________________________________
287 void AliParamSolver::Print(Option_t*) const
289 AliInfo(Form("Solver with %d globals for %d points",fNGlobal,fNPoints));
292 //______________________________________________________________________________________
293 void AliParamSolver::SetNGlobal(Int_t n)
296 AliError(Form("Maximum number of globals was set to %d",fMaxGlobal));
300 fMatrix->SetSizeUsed(fNGlobal);
303 //______________________________________________________________________________________
304 void AliParamSolver::SetMaxGlobal(Int_t n)
306 if (n>0 && n==fMaxGlobal) return;
309 if (fMatrix) delete fMatrix; fMatrix = 0;
310 if (fSolGlo) delete[] fSolGlo; fSolGlo = 0;
311 if (fSolLoc) delete[] fSolLoc; fSolLoc = 0;
312 if (fRHSGlo) delete[] fRHSGlo; fRHSGlo = 0;
313 if (fRHSLoc) delete[] fRHSLoc; fRHSLoc = 0;
314 if (fMatGamma) delete[] fMatGamma; fMatGamma = 0;
315 if (fMatG) delete[] fMatG; fMatG = 0;
316 if (fCovDGl) delete[] fCovDGl; fCovDGl = 0;
317 n = TMath::Max(16,fMaxPoints);