GeneralBrokenLines  V01-11-00
example1.cpp
Go to the documentation of this file.
00001 /*
00002  * example1.cpp
00003  *
00004  *  Created on: Aug 24, 2011
00005  *      Author: kleinwrt
00006  */
00007 
00008 #include <time.h>
00009 #include "example1.h"
00010 #include "TRandom3.h"
00011 #include "GblTrajectory.h"
00012 
00013 TMatrixD gblSimpleJacobian(double ds, double cosl, double bfac) {
00014         /* Simple jacobian: quadratic in arc length difference */
00015         TMatrixD jac(5, 5);
00016         jac.UnitMatrix();
00017         jac[1][0] = -bfac * ds * cosl;
00018         jac[3][0] = -0.5 * bfac * ds * ds * cosl;
00019         jac[3][1] = ds;
00020         jac[4][2] = ds;
00021         return jac;
00022 }
00023 
00024 void example1() {
00025         /*
00026          * Create points on initial trajectory, create trajectory from points,
00027          * fit and write trajectory to MP-II binary file,
00028          * get track parameter corrections and covariance matrix at points.
00029          *
00030          * Equidistant measurement layers and thin scatterers, propagation
00031          * with simple jacobian (quadratic in arc length differences).
00032          */
00033 
00034 //MP    MilleBinary mille; // for producing MillePede-II binary file
00035         unsigned int nTry = 10000; //: number of tries
00036         unsigned int nLayer = 10; //: number of detector layers
00037         std::cout << " Gbltst $Rev: 125 $ " << nTry << ", " << nLayer << std::endl;
00038 
00039         TRandom *r = new TRandom3();
00040 
00041         clock_t startTime = clock();
00042 // track direction
00043         double sinLambda = 0.3;
00044         double cosLambda = sqrt(1.0 - sinLambda * sinLambda);
00045         double sinPhi = 0.;
00046         double cosPhi = sqrt(1.0 - sinPhi * sinPhi);
00047 // tDir = (cosLambda * cosPhi, cosLambda * sinPhi, sinLambda)
00048 // U = Z x T / |Z x T|, V = T x U
00049         TMatrixD uvDirT(3, 2);
00050         uvDirT[0][0] = -sinPhi;
00051         uvDirT[1][0] = cosPhi;
00052         uvDirT[2][0] = 0.;
00053         uvDirT[0][1] = -sinLambda * cosPhi;
00054         uvDirT[1][1] = -sinLambda * sinPhi;
00055         uvDirT[2][1] = cosLambda;
00056 // measurement resolution
00057         TVectorD measErr(2);
00058         measErr[0] = 0.001;
00059         measErr[1] = 0.001;
00060         TVectorD measPrec(2); // (independent) precisions
00061         measPrec[0] = 1.0 / (measErr[0] * measErr[0]);
00062         measPrec[1] = 1.0 / (measErr[1] * measErr[1]);
00063         TMatrixDSym measInvCov(2); // inverse covariance matrix
00064         measInvCov.Zero();
00065         measInvCov[0][0] = measPrec[0];
00066         measInvCov[1][1] = measPrec[1];
00067 // scattering error
00068         TVectorD scatErr(2);
00069         scatErr[0] = 0.001;
00070         scatErr[1] = 0.001;
00071         TVectorD scatPrec(2);
00072         scatPrec[0] = 1.0 / (scatErr[0] * scatErr[0]);
00073         scatPrec[1] = 1.0 / (scatErr[1] * scatErr[1]);
00074 // (RMS of) CurvLinear track parameters
00075         TVectorD clPar(5);
00076         TVectorD clErr(5);
00077         clErr[0] = 0.001;
00078         clErr[1] = -0.1;
00079         clErr[2] = 0.2;
00080         clErr[3] = -0.15;
00081         clErr[4] = 0.25;
00082         TMatrixDSym clCov(5), clSeed(5);
00083         unsigned int seedLabel = 0;
00084 // additional parameters
00085         TVectorD addPar(2);
00086         addPar[0] = 0.0025;
00087         addPar[1] = -0.005;
00088 // global labels for MP
00089         /*MP    std::vector<int> globalLabels(2);
00090          globalLabels[0] = 11;
00091          globalLabels[1] = 12; */
00092 
00093         double bfac = 0.2998; // Bz*c for Bz=1
00094         double step = 1.5 / cosLambda; // constant steps in RPhi
00095 
00096         double Chi2Sum = 0.;
00097         int NdfSum = 0;
00098         double LostSum = 0.;
00099 
00100         for (unsigned int iTry = 1; iTry <= nTry; iTry++) {
00101                 // curvilinear track parameters
00102                 for (unsigned int i = 0; i < 5; i++) {
00103                         clPar[i] = clErr[i] * r->Gaus();
00104                 }
00105                 clCov.Zero();
00106                 for (unsigned int i = 0; i < 5; i++) {
00107                         clCov[i][i] = 1.0 * (clErr[i] * clErr[i]);
00108                 }
00109 //              std::cout << " Try " << iTry << ":" << clPar << std::endl;
00110                 TMatrixD addDer(2, 2);
00111                 addDer.Zero();
00112                 addDer[0][0] = 1.;
00113                 addDer[1][1] = 1.;
00114 // arclength
00115                 double s = 0.;
00116                 std::vector<double> sPoint;
00117                 TMatrixD jacPointToPoint(5, 5);
00118                 jacPointToPoint.UnitMatrix();
00119 // create trajectory
00120                 GblTrajectory traj;
00121 
00122                 for (unsigned int iLayer = 0; iLayer < nLayer; iLayer++) {
00123 //                      std::cout << " Layer " << iLayer << ", " << s << std::endl;
00124 //     measurement directions
00125                         double sinStereo = (iLayer % 2 == 0) ? 0. : 0.5;
00126                         double cosStereo = sqrt(1.0 - sinStereo * sinStereo);
00127                         TMatrixD mDir(2, 3);
00128                         mDir.Zero();
00129                         mDir[0][0] = sinStereo;
00130                         mDir[0][1] = cosStereo;
00131                         mDir[1][2] = 1.;
00132 // projection measurement to local (uv) directions
00133                         TMatrixD proM2l = mDir * uvDirT;
00134 // projection local (uv) to measurement directions
00135                         TMatrixD proL2m = proM2l;
00136                         proL2m.Invert();
00137 // point with (independent) measurements (in measurement system)
00138                         GblPoint point(jacPointToPoint);
00139 // measurement - prediction in measurement system with error
00140                         TVectorD meas = proL2m * clPar.GetSub(3, 4);
00141 //MP                    meas += addDer * addPar; // additional parameters
00142                         for (unsigned int i = 0; i < 2; i++) {
00143                                 meas[i] += measErr[i] * r->Gaus();
00144                         }
00145                         point.addMeasurement(proL2m, meas, measPrec);
00146                         /* point with (correlated) measurements (in local system)
00147                          GblPoint point(jacPointToPoint);
00148                          // measurement - prediction in local system with error
00149                          TVectorD meas(2);
00150                          for (unsigned int i = 0; i < 2; i++) {
00151                          meas[i] = measErr[i] * r->Gaus() + addDer(i,0) * 0.0075;
00152                          }
00153                          meas = proM2l * meas + clPar.GetSub(3, 4);
00154                          TMatrixDSym localInvCov = measInvCov;
00155                          localInvCov.SimilarityT(proL2m);
00156                          point.addMeasurement(meas, localInvCov);
00157 
00158                          // additional local parameters? */
00159 //                      point.addLocals(addDer);
00160 //MP                    point.addGlobals(globalLabels, addDer);
00161                         addDer *= -1.; // Der flips sign every measurement
00162 // add point to trajectory
00163                         unsigned int iLabel = traj.addPoint(point);
00164                         sPoint.push_back(s);
00165                         if (iLabel == seedLabel) {
00166                                 clSeed = clCov.Invert();
00167                         }
00168 // propagate to scatterer
00169                         jacPointToPoint = gblSimpleJacobian(step, cosLambda, bfac);
00170                         clPar = jacPointToPoint * clPar;
00171                         clCov = clCov.Similarity(jacPointToPoint);
00172                         s += step;
00173                         if (iLayer < nLayer - 1) {
00174                                 TVectorD scat(2);
00175                                 scat.Zero();
00176                                 // point with scatterer
00177                                 GblPoint point(jacPointToPoint);
00178                                 point.addScatterer(scat, scatPrec);
00179                                 iLabel = traj.addPoint(point);
00180                                 sPoint.push_back(s);
00181                                 if (iLabel == seedLabel) {
00182                                         clSeed = clCov.Invert();
00183                                 }
00184                                 // scatter a little
00185                                 for (unsigned int i = 0; i < 2; i++) {
00186                                         clPar[i + 1] += scatErr[i] * r->Gaus();
00187                                         clCov[i + 1] += scatErr[i] * scatErr[i];
00188                                 }
00189                                 // propagate to next measurement layer
00190                                 clPar = jacPointToPoint * clPar;
00191                                 clCov = clCov.Similarity(jacPointToPoint);
00192                                 s += step;
00193                         }
00194                 }
00195 //
00196                 if (seedLabel) {
00197                         traj.addExternalSeed(seedLabel, clSeed); // external seed
00198                 }
00199 // fit trajectory
00200                 double Chi2;
00201                 int Ndf;
00202                 double lostWeight;
00203                 traj.fit(Chi2, Ndf, lostWeight);
00204 //              std::cout << " Fit: " << Chi2 << ", " << Ndf << ", " << lostWeight << std::endl;
00205                 /*               TVectorD aCorrection(5);
00206                  TMatrixDSym aCovariance(5);
00207                  traj.getResults(3, aCorrection, aCovariance);
00208                  std::cout << " cor " << std::endl;
00209                  aCorrection.Print();
00210                  std::cout << " cov " << std::endl;
00211                  aCovariance.Print(); */
00212 // write to MP binary file
00213 //MP            traj.milleOut(mille);
00214                 Chi2Sum += Chi2;
00215                 NdfSum += Ndf;
00216                 LostSum += lostWeight;
00217 // try Kalman
00218 //              traj.kalmanGainFilter(Chi2, Ndf);
00219         }
00220         clock_t endTime = clock();
00221         double diff = endTime - startTime;
00222         double cps = CLOCKS_PER_SEC;
00223         std::cout << " Time elapsed " << diff / cps << " s" << std::endl;
00224         std::cout << " Chi2/Ndf = " << Chi2Sum / NdfSum << std::endl;
00225 }
00226 
 All Classes Files Functions Variables Typedefs