From 32f76baf9998551278c2592874a38552e03a95f7 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Mon, 9 Jan 2023 17:28:16 -0500 Subject: [PATCH 01/69] Refactor and move KF tracking package from dc to common-tools and update dc package to apply new KF package --- .../clas/tracking/kalmanfilter/AMeasVecs.java | 55 +- .../tracking/kalmanfilter/AStateVecs.java | 56 +- .../clas/tracking/kalmanfilter/Surface.java | 50 ++ .../jlab/clas/tracking/kalmanfilter/Type.java | 2 +- .../kalmanfilter/zReference/KFitter.java | 567 ++++++++++++++++++ .../kalmanfilter/zReference/MeasVecs.java | 46 ++ .../kalmanfilter/zReference/StateVecs.java | 310 ++++++++++ .../tracking/utilities/RungeKuttaDoca.java | 363 +++++++++++ reconstruction/dc/pom.xml | 6 + .../rec/dc/track/TrackCandListFinder.java | 344 ++++++++--- .../java/org/jlab/service/dc/DCTBEngine.java | 322 ++++++++-- 11 files changed, 1999 insertions(+), 122 deletions(-) create mode 100644 common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java create mode 100644 common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/MeasVecs.java create mode 100644 common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/StateVecs.java create mode 100644 common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/utilities/RungeKuttaDoca.java diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java index 903fb2415..ac77604b5 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java @@ -22,7 +22,8 @@ public abstract class AMeasVecs { public void setMeasVecs(List measSurfaces) { measurements = new ArrayList<>(); - Collections.sort(measSurfaces); + if(measSurfaces.get(0).type != Type.LINEDOCA) // Measurements from DC has been sorted + Collections.sort(measSurfaces); for(int i = 0; i < measSurfaces.size(); i++) { MeasVec mvec = new MeasVec(); mvec.k = i ; @@ -33,9 +34,57 @@ public void setMeasVecs(List measSurfaces) { mvec.skip = mvec.surface.passive; mvec.hemisphere = measSurfaces.get(i).hemisphere; measurements.add(mvec); + if(measSurfaces.get(i).type == Type.LINEDOCA) { + mvec.region = measSurfaces.get(i).region; + mvec.sector = measSurfaces.get(i).getSector(); + mvec.superlayer = measSurfaces.get(i).getSuperLayer(); + mvec.layer = measSurfaces.get(i).getLayer(); + } } } + // For DC, there could be two hits at a measurement + public double[] dhDoca(int k, StateVec stateVec) { + double value[] = {Double.NaN, Double.NaN}; + + Surface surf = this.measurements.get(stateVec.k).surface; + Point3D point = new Point3D(stateVec.x, stateVec.y, stateVec.z); + double h = hDoca(point, surf.wireLine[0]); + + double signMeas = 1; + double sign = 1; + if(surf.doca[1]!=-99 || !(Math.abs(surf.doca[0])<0.5 && surf.doca[1]==-99 ) ) { //use LR only for double hits or large enough doca for one hit + signMeas = Math.signum(surf.doca[0]); + sign = Math.signum(h); + } else { + signMeas = Math.signum(h); + sign = Math.signum(h); + } + + value[0] = signMeas*Math.abs(surf.doca[0]) - sign*Math.abs(h); + + //USE THE DOUBLE HIT + if(surf.doca[1]!=-99) { + h = hDoca(point, surf.wireLine[1]); + + signMeas = Math.signum(surf.doca[1]); + sign = Math.signum(h); + + value[1]= signMeas*Math.abs(surf.doca[1]) - sign*Math.abs(h); + } + + return value; + } + + // Return a signed doca for DC + public double hDoca(Point3D point, Line3D wireLine) { + + Line3D WL = new Line3D(); + WL.copy(wireLine); + WL.copy(WL.distance(point)); + + return WL.length()*Math.signum(-WL.direction().x()); + } public double dh(int k, StateVec stateVec) { @@ -209,6 +258,10 @@ public class MeasVec implements Comparable { public boolean skip = false; public double hemisphere = 1; + //For DC + public int region = -1; + public int sector = -1; + public int superlayer = -1; @Override public int compareTo(MeasVec arg) { diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java index 43895fca0..fdfd5275a 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java @@ -10,6 +10,8 @@ import org.jlab.clas.tracking.kalmanfilter.helical.KFitter; import org.jlab.clas.tracking.trackrep.Helix; import org.jlab.geom.prim.Point3D; +import org.jlab.jnp.matrix.Matrix; +import org.jlab.jnp.matrix.Matrix5x5; public abstract class AStateVecs { @@ -245,6 +247,45 @@ public class StateVec { public double energyLoss; public double dx; public double path; + + /////////////////////// extra variables for forward tracking /////////////////////// + // tx = px/pz + public double ty; //=py/pz + public double Q; //q/p + public double B; + public double deltaPath; + public Matrix CM = new Matrix();; + private double _PathLength; + + public double getPathLength() { + return _PathLength; + } + + public void setPathLength(double _PathLength) { + this._PathLength = _PathLength; + } + + // KF projector --> get Wire midPoint match + private double hw; + + public double getProjector() { + return hw; + } + + public void setProjector(double h) { + this.hw = h; + } + // KF projector --> get fit doca + private double h; + + public double getProjectorDoca() { + return h; + } + + public void setProjectorDoca(double h) { + this.h = h; + } + /////////////////////// extra variables for forward tracking /////////////////////// public StateVec(int k) { this.k = k; @@ -294,8 +335,16 @@ public final void copy(StateVec s) { this.energyLoss = s.energyLoss; this.dx = s.dx; this.path = s.path; - this.covMat = this.copyMatrix(s.covMat); - this.F = this.copyMatrix(s.F); + if(s.covMat != null) + this.covMat = this.copyMatrix(s.covMat); + if(s.F != null) + this.F = this.copyMatrix(s.F); + this.ty = s.ty; + this.Q = s.Q; + this.B = s.B; + this.deltaPath = s.deltaPath; + if(s.CM != null) + Matrix5x5.copy(s.CM, this.CM); } public void copyCovMat(double[][] c) { @@ -414,8 +463,7 @@ public Helix getHelix(double xref, double yref) { if (Math.abs(phi0) > Math.PI) phi0 -= Math.signum(phi0) * 2 * Math.PI; double tanDip = vec.tanL; double z0 = vec.z0 + vec.dz; - //double omega = turningSign * KFitter.polarity / R; - double omega = Math.signum(vec.kappa) / R; + double omega = -turningSign / R; double d0 = -vec.d_rho; Helix helix = new Helix(d0, phi0, omega, z0, tanDip, turningSign, bfield, xref, yref, units); diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java index a87427082..b061aea5d 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java @@ -7,6 +7,7 @@ import org.jlab.geom.prim.Point3D; import org.jlab.geom.prim.Arc3D; import org.jlab.geom.prim.Cylindrical3D; +import org.jlab.geom.prim.Line3D; import org.jlab.geom.prim.Transformation3D; import org.jlab.geom.prim.Vector3D; @@ -38,6 +39,41 @@ public class Surface implements Comparable { public boolean passive = false; public double hemisphere = 1; + // For DC + public double z; + public double x; + public double[] unc = new double[2]; + public double tilt; + public double[] doca = new double[2]; + public double wireMaxSag; + public Line3D[] wireLine = new Line3D[2]; + public boolean reject = false; + public int region; + public int superlayer; + public int nMeas = 1; + + public void setNMeas(int n) { + nMeas = n; + } + + public int getNMeas() { + return nMeas; + } + + // For DC + public Surface(int region, double z, double x, double tilt, double wireMaxSag, double[] doca, double[] unc, double error, Line3D[] wireLine) { + type = Type.LINEDOCA; + this.region = region; + this.z = z; + this.x = x; + this.tilt = tilt; + this.wireMaxSag = wireMaxSag; + this.doca = doca; + this.unc = unc; + this.error = error; + this.wireLine = wireLine; + } + public Surface(Plane3D plane3d, Point3D refrPoint, Point3D c1, Point3D c2, double accuracy) { type = Type.PLANEWITHPOINT; plane = plane3d; @@ -168,6 +204,20 @@ public int getLayer() { public void setLayer(int layer) { this.layer = layer; } + + /** + * @return the superlayer + */ + public int getSuperLayer() { + return superlayer; + } + + /** + * @param superlayer the superlayer to set + */ + public void setSuperLayer(int superlayer) { + this.superlayer = superlayer; + } /** * @return the sector diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java index 181426667..76bae7238 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java @@ -7,7 +7,7 @@ public enum Type { UDF(-1), PLANEWITHPOINT(0), PLANEWITHLINE(1), PLANEWITHSTRIP(2), CYLINDERWITHPOINT(3), CYLINDERWITHLINE(4), CYLINDERWITHARC(5), - CYLINDERWITHSTRIP(6), LINE(7); + CYLINDERWITHSTRIP(6), LINE(7), LINEDOCA(8); private final int value; Type(int value) { diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java new file mode 100644 index 000000000..a11bcbeb7 --- /dev/null +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -0,0 +1,567 @@ +package org.jlab.clas.tracking.kalmanfilter.zReference; + +import java.util.ArrayList; +import java.util.List; + +import org.jlab.clas.clas.math.FastMath; +import org.jlab.clas.swimtools.Swim; +import org.jlab.clas.tracking.kalmanfilter.AKFitter; +import org.jlab.clas.tracking.kalmanfilter.AMeasVecs; +import org.jlab.clas.tracking.kalmanfilter.AStateVecs; +import org.jlab.clas.tracking.kalmanfilter.Surface; +import org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec; +import org.jlab.clas.tracking.kalmanfilter.zReference.MeasVecs; +import org.jlab.clas.tracking.kalmanfilter.zReference.StateVecs; +import org.jlab.clas.tracking.utilities.RungeKuttaDoca; +import org.jlab.clas.tracking.utilities.MatrixOps.Libr; +import org.jlab.geom.prim.Point3D; +import org.jlab.jnp.matrix.*; + +/** + * + * @author Tongtong Cao + */ + +public class KFitter extends AKFitter { + + private StateVecs sv = new StateVecs(); + private MeasVecs mv = new MeasVecs(); + private StateVec finalSmoothedStateVec = null; + private StateVec finalTransportedStateVec = null; + + public StateVec finalStateVec = null; + public StateVec initialStateVec = null; + public List kfStateVecsAlongTrajectory; + + private int interNum; + + private double chi2kf = 0; + private double KFScale = 4; + + private int svzLength; + + public int ConvStatus = 1; + + private double Z[]; + + private boolean stopIteration = false; + + private boolean TBT =false; + + Matrix first_inverse = new Matrix(); + Matrix addition = new Matrix(); + Matrix result = new Matrix(); + Matrix result_inv = new Matrix(); + Matrix adj = new Matrix(); + + public KFitter(boolean filter, int iterations, int dir, Swim swim, double Z[], Libr mo) { + super(filter, iterations, dir, swim, mo); + this.Z = Z; + } + + public final void init(List measSurfaces, StateVec initSV) { + finalSmoothedStateVec = null; + finalTransportedStateVec = null; + this.NDF0 = -5; + this.NDF = -5; + this.chi2 = Double.POSITIVE_INFINITY; + this.numIter = 0; + this.setFitFailed = false; + mv.setMeasVecs(measSurfaces); + for (int i = 0; i < mv.measurements.size(); i++) { + if (mv.measurements.get(i).skip == false) { + this.NDF+= mv.measurements.get(i).surface.getNMeas(); + } + } + + sv.init(initSV); + sv.Z = Z; + } + + public final void initFromHB(List measSurfaces, StateVec initSV, double beta) { + finalSmoothedStateVec = null; + finalTransportedStateVec = null; + this.NDF0 = -5; + this.NDF = -5; + this.chi2 = Double.POSITIVE_INFINITY; + this.numIter = 0; + this.setFitFailed = false; + mv.setMeasVecs(measSurfaces); + for (int i = 0; i < mv.measurements.size(); i++) { + if (mv.measurements.get(i).skip == false) { + this.NDF+= mv.measurements.get(i).surface.getNMeas(); + } + } + + sv.initFromHB(initSV, beta); + sv.Z = Z; + TBT = true; + } + + public void runFitter() { + this.chi2 = Double.POSITIVE_INFINITY; + double initChi2 = Double.POSITIVE_INFINITY; + // this.NDF = mv.ndf; + this.svzLength = this.mv.measurements.size(); + + int sector = this.mv.measurements.get(0).sector; + + + if(TBT==true) { + this.chi2kf = 0; + // Get the input parameters + for (int k = 0; k < svzLength - 1; k++) { + sv.transport(sector, k, k + 1, this.sv.trackTrajT.get(k), mv, this.getSwimmer(), true); + } + this.calcFinalChisq(sector, true); + this.initialStateVec = sv.trackTrajT.get(svzLength - 1); + this.finalStateVec = sv.trackTrajT.get(svzLength - 1); + initChi2 = this.chi2; + if(Double.isNaN(chi2)) { + this.setFitFailed = true; + return; + } + } + + for (int i = 1; i <= totNumIter; i++) { + interNum = i; + this.chi2kf = 0; + + if (i > 1) { + + for (int k = svzLength - 1; k >0; k--) { + boolean forward = false; + if(k>=2) { + + // Not backward transport and filter states for the last measurement layer + if(k == svzLength - 1) { + if(!sv.transport(sector, k, k - 2, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + else { + if(!sv.transport(sector, k, k - 2, this.sv.trackTrajB.get(k), mv, this.getSwimmer(), forward)){ + this.stopIteration = true; + break; + } + } + + if(!this.filter(k - 2, forward)) { + this.stopIteration = true; + break; + } + + if(!sv.transport(sector, k - 2, k - 1, this.sv.trackTrajB.get(k-2), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + if(!this.filter(k - 1, forward)) { + this.stopIteration = true; + break; + } + } + else { + if(!sv.transport(sector, 1, 0, this.sv.trackTrajB.get(1), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + if(!this.filter(0, forward)) { + this.stopIteration = true; + break; + } + } + } + } + + if(this.stopIteration) break; + + for (int k = 0; k < svzLength - 1; k++) { + boolean forward = true; + + if (interNum == 1 && (k == 0)) { + if(TBT==true) { + this.sv.transported(true).put(0, this.sv.transported(false).get(0)); // For TBT, calcFinalChisq() is called previously. + } + } + + + if(k == 0) { + if(i == 1) { + if(!this.sv.transport(sector, 0, 1, this.sv.trackTrajT.get(0), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + else { + if(!this.sv.transport(sector, 0, 1, this.sv.trackTrajB.get(0), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + } + else { + if(!this.sv.transport(sector, k, k + 1, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + } + + if(!this.filter(k + 1, forward)) { + this.stopIteration = true; + break; + } + } + + if(this.stopIteration) break; + + + if (i > 1) { + if(this.setFitFailed==true) + i = totNumIter; + if (this.setFitFailed==false) { + if(this.finalStateVec!=null) { + if(Math.abs(sv.trackTrajF.get(svzLength - 1).Q-this.finalStateVec.Q)<5.e-4 && + Math.abs(sv.trackTrajF.get(svzLength - 1).x-this.finalStateVec.x)<1.e-4 && + Math.abs(sv.trackTrajF.get(svzLength - 1).y-this.finalStateVec.y)<1.e-4 && + Math.abs(sv.trackTrajF.get(svzLength - 1).tx-this.finalStateVec.tx)<1.e-6 && + Math.abs(sv.trackTrajF.get(svzLength - 1).ty-this.finalStateVec.ty)<1.e-6) { + i = totNumIter; + } + } + this.finalStateVec = sv.trackTrajF.get(svzLength - 1); + + } else { + this.ConvStatus = 1; // Should be 0??? + } + } + + + } + + + if(totNumIter==1) { + if(this.setFitFailed==false && this.stopIteration == false) + this.finalStateVec = sv.trackTrajF.get(svzLength - 1); + } + + + this.calcFinalChisq(sector); + + + if(Double.isNaN(chi2)) + this.setFitFailed = true; + + + if(TBT==true) { + if(chi2>initChi2) { // fit failed + this.finalStateVec = this.initialStateVec; + sv.trackTrajT.put(svzLength - 1,this.initialStateVec); + this.calcFinalChisq(sector, true); + } + } + + } + + private boolean filter(int k, boolean forward) { + StateVec sVec = sv.transported(forward).get(k); + org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec mVec = mv.measurements.get(k); + + if (Double.isNaN(sVec.x) || Double.isNaN(sVec.y) + || Double.isNaN(sVec.tx) || Double.isNaN(sVec.ty) + || Double.isNaN(sVec.Q)) { + this.setFitFailed = true; + return false; + } + if (sVec != null && sVec.CM != null + && k < mv.measurements.size() && mVec.skip == false) { + + + double[] K = new double[5]; + double V = mVec.surface.unc[0] * KFScale; + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[0]); + Matrix CaInv = this.filterCovMat(H, sVec.CM, V); + Matrix cMat = new Matrix(); + if (CaInv != null) { + Matrix5x5.copy(CaInv, cMat); + } else { + return false; + } + + for (int j = 0; j < 5; j++) { + // the gain matrix + K[j] = (H[0] * cMat.get(j, 0) + + H[1] * cMat.get(j, 1)) / V; + } + + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + double h = mv.hDoca(point, mVec.surface.wireLine[0]); + + double signMeas = 1; + double sign = 1; + if (mVec.surface.doca[1] != -99 + || !(Math.abs(mVec.surface.doca[0]) < 0.5 + && mVec.surface.doca[1] == -99)) { // use LR only for double hits && large + // enough docas + signMeas = Math.signum(mVec.surface.doca[0]); + sign = Math.signum(h); + } else { + signMeas = Math.signum(h); + sign = Math.signum(h); + } + + double c2 = ((signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)) + * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)) / V); + + double x_filt = sVec.x + + K[0] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double y_filt = sVec.y + + K[1] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double tx_filt = sVec.tx + + K[2] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double ty_filt = sVec.ty + + K[3] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double Q_filt = sVec.Q + + K[4] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + + // USE THE DOUBLE HIT + if (mVec.surface.doca[1] != -99) { + // now filter using the other Hit + V = mVec.surface.unc[1] * KFScale; + H = mv.H(x_filt, y_filt, mVec.surface.z, + mVec.surface.wireLine[1]); + CaInv = this.filterCovMat(H, cMat, V); + if (CaInv != null) { + for (int i = 0; i < 5; i++) { + Matrix5x5.copy(CaInv, cMat); + } + } else { + return false; + } + for (int j = 0; j < 5; j++) { + // the gain matrix + K[j] = (H[0] * cMat.get(j, 0) + + H[1] * cMat.get(j, 1)) / V; + } + + Point3D point2 = new Point3D(x_filt, y_filt, mVec.surface.z); + + h = mv.hDoca(point2, mVec.surface.wireLine[1]); + + signMeas = Math.signum(mVec.surface.doca[1]); + sign = Math.signum(h); + + x_filt += K[0] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + y_filt += K[1] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + tx_filt += K[2] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + ty_filt += K[3] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + Q_filt += K[4] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + + c2 += ((signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)) + * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)) / V); + } + + chi2kf += c2; + if (filterOn) { + StateVec filteredVec = sv.new StateVec(k); + filteredVec.x = x_filt; + filteredVec.y = y_filt; + filteredVec.tx = tx_filt; + filteredVec.ty = ty_filt; + filteredVec.Q = Q_filt; + filteredVec.z = sVec.z; + filteredVec.B = sVec.B; + filteredVec.deltaPath = sVec.deltaPath; + + filteredVec.CM = cMat; + + sv.filtered(forward).put(k, filteredVec); + } + else { + return false; + } + + return true; + } + else { + return false; + } + } + + public Matrix filterCovMat(double[] H, Matrix Ci, double V) { + + double det = Matrix5x5.inverse(Ci, first_inverse, adj); + if(Math.abs(det)<1.e-30) + return null; + + addition.set( + H[0] * H[0] / V, H[0] * H[1] / V, 0, 0, 0, + H[0] * H[1] / V, H[1] * H[1] / V, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0); + + Matrix5x5.add(first_inverse, addition, result); + double det2 = Matrix5x5.inverse(result, result_inv, adj); + if(Math.abs(det2)<1.e-30) + return null; + + return result_inv; + } + + private void calcFinalChisq(int sector) { + calcFinalChisq(sector, false); + } + + private void calcFinalChisq(int sector, boolean nofilter) { + int k = svzLength - 1; + this.chi2 = 0; + double path = 0; + double[] nRj = new double[3]; + + StateVec sVec; + + // To be changed: to match wit the old package, we make the following codes. Could be changed when other codes for application of calcFinalChisq are changed. + if(nofilter || sv.trackTrajF.size() < svzLength) { + sVec = sv.trackTrajT.get(k); + } + else { + sVec = sv.trackTrajF.get(k); + } + + + kfStateVecsAlongTrajectory = new ArrayList<>(); + if (sVec != null && sVec.CM != null) { + + boolean forward = false; + sv.transport(sector, k, 0, sVec, mv, this.getSwimmer(), forward); + + StateVec svc = sv.transported(forward).get(0); + path += svc.deltaPath; + svc.setPathLength(path); + + double V0 = mv.measurements.get(0).surface.unc[0]; + + Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); + double h0 = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); + + svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); + svc.setProjectorDoca(h0); + kfStateVecsAlongTrajectory.add(svc); + double res = (mv.measurements.get(0).surface.doca[0] - h0); + chi2 += (mv.measurements.get(0).surface.doca[0] - h0) * (mv.measurements.get(0).surface.doca[0] - h0) / V0; + nRj[mv.measurements.get(0).region-1]+=res*res/mv.measurements.get(0).error; + //USE THE DOUBLE HIT + if(mv.measurements.get(0).surface.doca[1]!=-99) { + V0 = mv.measurements.get(0).surface.unc[1]; + h0 = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[1]); + res = (mv.measurements.get(0).surface.doca[1] - h0); + chi2 += (mv.measurements.get(0).surface.doca[1] - h0) * (mv.measurements.get(0).surface.doca[1] - h0) / V0; + nRj[mv.measurements.get(0).region-1]+=res*res/mv.measurements.get(0).error; + svc.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); + svc.setProjectorDoca(h0); + kfStateVecsAlongTrajectory.add(svc); + } + + forward = true; + for (int k1 = 0; k1 < k; k1++) { + if(k1 == 0) { + sv.transport(sector, k1, k1 + 1, svc, mv, this.getSwimmer(), forward); + } + else { + sv.transport(sector, k1, k1 + 1, sv.transported(forward).get(k1), mv, this.getSwimmer(), forward); + } + + double V = mv.measurements.get(k1 + 1).surface.unc[0]; + + point = new Point3D(sv.transported(forward).get(k1+1).x, sv.transported(forward).get(k1+1).y, mv.measurements.get(k1+1).surface.z); + + double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); + svc = sv.transported(forward).get(k1+1); + path += svc.deltaPath; + svc.setPathLength(path); + svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); + svc.setProjectorDoca(h); + kfStateVecsAlongTrajectory.add(svc); + res = (mv.measurements.get(k1 + 1).surface.doca[0] - h); + chi2 += (mv.measurements.get(k1 + 1).surface.doca[0] - h) * (mv.measurements.get(k1 + 1).surface.doca[0] - h) / V; + nRj[mv.measurements.get(k1 + 1).region-1]+=res*res/V; + //USE THE DOUBLE HIT + if(mv.measurements.get(k1 + 1).surface.doca[1]!=-99) { + V = mv.measurements.get(k1 + 1).surface.unc[1]; + h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[1]); + res = (mv.measurements.get(k1 + 1).surface.doca[1] - h); + chi2 += (mv.measurements.get(k1 + 1).surface.doca[1] - h) * (mv.measurements.get(k1 + 1).surface.doca[1] - h) / V; + nRj[mv.measurements.get(k1 + 1).region-1]+=res*res/V; + svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); + svc.setProjectorDoca(h); + kfStateVecsAlongTrajectory.add(svc); + } + } + } + + } + + + public Matrix propagateToVtx(int sector, double Zf) { + return sv.transport(sector, 0, Zf, sv.trackTrajP.get(0), mv, this.getSwimmer()); + } + + + @Override + public void runFitter(AStateVecs sv, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public StateVec filter(int k, StateVec vec, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public StateVec smooth(int k, AStateVecs sv, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public StateVec smooth(StateVec v1, StateVec v2) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public MeasVecs getMeasVecs() { + return mv; + } + + public StateVecs getStateVecs() { + return sv; + } + + public void printlnMeasVecs() { + for (int i = 0; i < mv.measurements.size(); i++) { + org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec measvec = mv.measurements.get(i); + String s = String.format("k=%d region=%d superlayer=%d layer=%d error=%.4f", measvec.k, measvec.region, measvec.superlayer, + measvec.layer, measvec.error); + s += String.format(" Surface: index=%d x=%.4f z=%.4f tilt=%.4f wireMaxSag=%.4f", measvec.surface.getIndex(), + measvec.surface.x, measvec.surface.z, measvec.surface.tilt, measvec.surface.wireMaxSag, measvec.surface.unc[1]); + s += String.format( + " Surface line 0: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", + measvec.surface.doca[0], measvec.surface.unc[0], measvec.surface.wireLine[0].origin().x(), + measvec.surface.wireLine[0].origin().y(), measvec.surface.wireLine[0].origin().z(), + measvec.surface.wireLine[0].end().x(), measvec.surface.wireLine[0].end().y(), + measvec.surface.wireLine[0].end().z()); + if (measvec.surface.wireLine[1] != null) + s += String.format( + " Surface line 1: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", + measvec.surface.doca[1], measvec.surface.unc[1], measvec.surface.wireLine[1].origin().x(), + measvec.surface.wireLine[1].origin().y(), measvec.surface.wireLine[1].origin().z(), + measvec.surface.wireLine[1].end().x(), measvec.surface.wireLine[1].end().y(), + measvec.surface.wireLine[1].end().z()); + + System.out.println(s); + } + } + +} diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/MeasVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/MeasVecs.java new file mode 100644 index 000000000..8415b5139 --- /dev/null +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/MeasVecs.java @@ -0,0 +1,46 @@ +package org.jlab.clas.tracking.kalmanfilter.zReference; + +import java.util.ArrayList; +import java.util.List; + +import org.jlab.clas.swimtools.Swim; +import org.jlab.clas.tracking.kalmanfilter.AMeasVecs; +import org.jlab.clas.tracking.kalmanfilter.AStateVecs; +import org.jlab.clas.tracking.kalmanfilter.Material; +import org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec; +import org.jlab.geom.prim.Line3D; +import org.jlab.geom.prim.Point3D; + +/** + * + * @author tongtong cao + */ +public class MeasVecs extends AMeasVecs { + + public double[] H(double x, double y, double z, Line3D wireLine) { + double[] hMatrix = new double[5]; + double Err = 0.025; + double[][] Result = new double[2][2]; + for(int i = 0; i < 2; i++) { + Point3D point = new Point3D(x + (double)Math.pow(-1, i) * Err, y, z); + Result[i][0] = hDoca(point, wireLine); + } + for(int i = 0; i < 2; i++) { + Point3D point = new Point3D(x, y + (double)Math.pow(-1, i) * Err, z); + Result[i][1] = hDoca(point, wireLine); + } + + hMatrix[0] = (Result[0][0]-Result[1][0])/(2.*Err); + hMatrix[1] = (Result[0][1]-Result[1][1])/(2.*Err); + hMatrix[2] = 0; + hMatrix[3] = 0; + hMatrix[4] = 0; + + return hMatrix; + } + + @Override + public double[] H(AStateVecs.StateVec stateVec, AStateVecs sv, MeasVec mv, Swim swimmer) { + throw new UnsupportedOperationException("Not supported yet."); + } +} diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/StateVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/StateVecs.java new file mode 100644 index 000000000..e1dcf670f --- /dev/null +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/StateVecs.java @@ -0,0 +1,310 @@ +package org.jlab.clas.tracking.kalmanfilter.zReference; + +import org.jlab.clas.pdg.PhysicsConstants; +import org.jlab.clas.swimtools.Swim; +import org.jlab.clas.tracking.kalmanfilter.AMeasVecs; +import org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec; +import org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec; +import org.jlab.clas.tracking.kalmanfilter.AStateVecs; +import org.jlab.clas.tracking.kalmanfilter.Surface; +import org.jlab.clas.tracking.kalmanfilter.Units; +import org.jlab.clas.tracking.trackrep.Helix; +import org.jlab.clas.tracking.utilities.MatrixOps; +import org.jlab.geom.prim.Line3D; +import org.jlab.geom.prim.Point3D; +import org.jlab.geom.prim.Vector3D; +import org.jlab.jnp.matrix.Matrix5x5; +import org.jlab.clas.tracking.utilities.RungeKuttaDoca; +import org.jlab.jnp.matrix.Matrix; + +/** + * + * @author Tongtong Cao + */ +public class StateVecs extends AStateVecs { + + private RungeKuttaDoca rk = new RungeKuttaDoca(); + private final Matrix fMS = new Matrix(); + private final Matrix copyMatrix = new Matrix(); + + final double ARGONRADLEN = 14; // radiation length in Argon is 14 cm + + final double AIRRADLEN = 30400; // radiation length in cm + + public double Z[]; + + private double beta = 1.0; // beta depends on mass hypothesis + + // For hit-based tracking + public void init(StateVec initSV) { + this.initSV = initSV; + this.trackTrajT.clear(); + this.trackTrajF.clear(); + this.trackTrajP.clear(); + this.trackTrajB.clear(); + this.trackTrajS.clear(); + this.trackTrajT.put(0, new StateVec(initSV)); + } + + // For time-based tracking + public void initFromHB(StateVec initSV, double beta) { + this.beta = beta; + this.initSV = initSV; + this.trackTrajT.clear(); + this.trackTrajF.clear(); + this.trackTrajP.clear(); + this.trackTrajB.clear(); + this.trackTrajS.clear(); + this.trackTrajT.put(0, new StateVec(initSV)); + } + + + /** + * + * @param sector + * @param i initial state vector index + * @param Zf + * @param iVec state vector at the initial index + * @param mv measurements + */ + public Matrix transport(int sector, int i, double Zf, StateVec iVec, AMeasVecs mv, Swim swimmer) { // s = signed step-size + + double stepSize = 1.0; + StateVec fVec = new StateVec(0); + fVec.x = iVec.x; + fVec.y = iVec.y; + fVec.z = iVec.z; + fVec.tx = iVec.tx; + fVec.ty = iVec.ty; + fVec.Q = iVec.Q; + fVec.B = iVec.B; + Matrix5x5.copy(iVec.CM, fVec.CM); + + double s = 0; + double zInit = mv.measurements.get(i).surface.z; + double BatMeas = iVec.B; + + double z = zInit; + + while(Math.signum(Zf - zInit) *zMath.signum(Zf - zInit) *Zf) + s=Math.signum(Zf - zInit) *Math.abs(Zf-z); + + + //rk.RK4transport(sector, Q, x, y, z, tx, ty, s, swimmer, cMat, fVec, dPath); + rk.RK4transport(sector, s, swimmer, cMat, fVec); + + // Q process noise matrix estimate + double p = Math.abs(1. / iVec.Q); + + double X0 = this.getX0(z, Z); + double t_ov_X0 = Math.abs(s) / X0;//path length in radiation length units = t/X0 [true path length/ X0] ; Ar radiation length = 14 cm + + double beta = this.beta; + if(beta>1.0 || beta<=0) + beta =1.0; + + double sctRMS = 0; + + if(Math.abs(s)>0) + sctRMS = ((0.0136)/(beta*PhysicsConstants.speedOfLight()*p))*Math.sqrt(t_ov_X0)* + (1 + 0.038 * Math.log(t_ov_X0)); + + + double cov_txtx = (1 + tx * tx) * (1 + tx * tx + ty * ty) * sctRMS * sctRMS; + double cov_tyty = (1 + ty * ty) * (1 + tx * tx + ty * ty) * sctRMS * sctRMS; + double cov_txty = tx * ty * (1 + tx * tx + ty * ty) * sctRMS * sctRMS; + + fMS.set( + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, cov_txtx, cov_txty, 0, + 0, 0, cov_txty, cov_tyty, 0, + 0, 0, 0, 0, 0 + ); + + Matrix5x5.copy(fVec.CM, copyMatrix); + Matrix5x5.add(copyMatrix, fMS, fVec.CM); + + if( Math.abs(fVec.B - BatMeas)<0.0001) + stepSize*=2; + + BatMeas = fVec.B; + } + + return fVec.CM; + + } + + + /** + * + * @param sector + * @param i initial state vector index + * @param f final state vector index + * @param iVec state vector at the initial index + * @param mv measurements + */ + public boolean transport(int sector, int i, int f, StateVec iVec, AMeasVecs mv, Swim swimmer, boolean forward) { // s = signed step-size + if(iVec==null) { + return false; + } + double stepSize = 1.0; + StateVec fVec = new StateVec(f); + fVec.x = iVec.x; + fVec.y = iVec.y; + fVec.z = iVec.z; + fVec.tx = iVec.tx; + fVec.ty = iVec.ty; + fVec.Q = iVec.Q; + fVec.B = iVec.B; + Matrix5x5.copy(iVec.CM, fVec.CM); + + double s = 0; + double zInit = mv.measurements.get(i).surface.z; + double BatMeas = iVec.B; + + double z = zInit; + double zFinal = mv.measurements.get(f).surface.z; + + while(Math.signum(zFinal - zInit) *zMath.signum(zFinal - zInit) *zFinal) + s=Math.signum(zFinal - zInit) *Math.abs(zFinal-z); + + + //rk.RK4transport(sector, Q, x, y, z, tx, ty, s, swimmer, cMat, fVec, dPath); + rk.RK4transport(sector, s, swimmer, cMat, fVec); + + // Q process noise matrix estimate + double p = Math.abs(1. / iVec.Q); + + double X0 = this.getX0(z, Z); + double t_ov_X0 = Math.abs(s) / X0;//path length in radiation length units = t/X0 [true path length/ X0] ; Ar radiation length = 14 cm + + double beta = this.beta; + if(beta>1.0 || beta<=0) + beta =1.0; + + double sctRMS = 0; + + if(Math.abs(s)>0) + sctRMS = ((0.0136)/(beta*PhysicsConstants.speedOfLight()*p))*Math.sqrt(t_ov_X0)* + (1 + 0.038 * Math.log(t_ov_X0)); + + + double cov_txtx = (1 + tx * tx) * (1 + tx * tx + ty * ty) * sctRMS * sctRMS; + double cov_tyty = (1 + ty * ty) * (1 + tx * tx + ty * ty) * sctRMS * sctRMS; + double cov_txty = tx * ty * (1 + tx * tx + ty * ty) * sctRMS * sctRMS; + + fMS.set( + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, cov_txtx, cov_txty, 0, + 0, 0, cov_txty, cov_tyty, 0, + 0, 0, 0, 0, 0 + ); + + Matrix5x5.copy(fVec.CM, copyMatrix); + Matrix5x5.add(copyMatrix, fMS, fVec.CM); + + if( Math.abs(fVec.B - BatMeas)<0.0001) + stepSize*=2; + + BatMeas = fVec.B; + } + + if(forward) + this.trackTrajT.put(f, fVec); + else + this.trackTrajP.put(f, fVec); + + if(Double.isNaN(fVec.x) || Double.isNaN(fVec.y) || Double.isNaN(fVec.tx) || Double.isNaN(fVec.ty) || Double.isNaN(fVec.Q)) + return false; + else + return true; + + } + + public double getX0(double z, double Z[]) { + double X0 = AIRRADLEN; + double tolerance = 0.01; + + for(int i = 1; i =Z[i]-tolerance && z<=Z[i+1]+tolerance) { + return ARGONRADLEN; + } + } + return X0; + } + + @Override + public double[][] Q(StateVec vec, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void corrForEloss(int dir, StateVec vec, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean getStateVecPosAtMeasSite(StateVec sv, AMeasVecs.MeasVec mv, Swim swim) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean setStateVecPosAtMeasSite(StateVec sv, MeasVec mv, Swim swimmer) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void printlnStateVec(StateVec S) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public double[][] F(StateVec iVec, StateVec fVec) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void init(Helix helix, double[][] cov, double xref, double yref, double zref, double mass, Swim swimmer) { + throw new UnsupportedOperationException("Not for forward tracking."); + + } + + @Override + public void init(double x0, double z0, double tx, double tz, Units units, double[][] cov) { + throw new UnsupportedOperationException("Not for forward tracking."); + } +} diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/utilities/RungeKuttaDoca.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/utilities/RungeKuttaDoca.java new file mode 100644 index 000000000..f4c07624d --- /dev/null +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/utilities/RungeKuttaDoca.java @@ -0,0 +1,363 @@ +package org.jlab.clas.tracking.utilities; + +import org.jlab.jnp.matrix.*; +import org.jlab.clas.swimtools.Swim; +import org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec;; + +/** + * Swims a given state vector to a given Z position using Runge Kutta 4 transport. + * @author ziegler + * @author benkel + */ +public class RungeKuttaDoca { + private final float[] _b = new float[3]; + final double v = 0.0029979245; + + public RungeKuttaDoca() {} + + /** Swim to Z position without updating the covariance matrix. */ + public void SwimToZ(int sector, StateVec vec, Swim swim, double z0, float[] bf) { + double stepSize = 1.0; + swim.Bfield(sector, vec.x, vec.y, vec.z, bf); + + vec.B = Math.sqrt(bf[0]*bf[0] + bf[1]*bf[1] + bf[2]*bf[2]); + double s = vec.B; + final double travelSign = Math.signum(z0 - vec.z); + double BatMeas = 0; + + while(travelSign * vec.z < travelSign * z0) { + s = travelSign * stepSize; + if (travelSign*(vec.z+s) > travelSign*z0) s = travelSign*Math.abs(z0-vec.z); + + this.RK4transport(sector, s, swim, vec); + + if (Math.abs(vec.B - BatMeas) < 0.0001) stepSize *= 2; + BatMeas = vec.B; + } + } + + /** + * Transport using Runge Kutta 4 without updating the covariance matrix. Lab system = 1, TSC = 0. + * Internal state array (sN) is defined as {x, y, tx, ty}, since q doesn't change. + */ + private void RK4transport(int sector, double h, Swim swim, StateVec vec) { + // Set initial state. + double qv = vec.Q*v; + double[] s0 = {vec.x, vec.y, vec.tx, vec.ty}; + double[] sNull = {0, 0, 0, 0}; + + // Transport. + double[] s1 = RK4step(sector, vec.z, 0, swim, s0, sNull, qv); + double[] s2 = RK4step(sector, vec.z, 0.5*h, swim, s0, s1, qv); + double[] s3 = RK4step(sector, vec.z, 0.5*h, swim, s0, s2, qv); + double[] s4 = RK4step(sector, vec.z, h, swim, s0, s3, qv); + + // Set final state. + vec.z += h; + vec.x += this.RK4(s1[0], s2[0], s3[0], s4[0], h); + vec.y += this.RK4(s1[1], s2[1], s3[1], s4[1], h); + vec.tx += this.RK4(s1[2], s2[2], s3[2], s4[2], h); + vec.ty += this.RK4(s1[3], s2[3], s3[3], s4[3], h); + + vec.B = Math.sqrt(_b[0]*_b[0] + _b[1]*_b[1] + _b[2]*_b[2]); + vec.deltaPath += Math.sqrt((s0[0]-vec.x)*(s0[0]-vec.x)+(s0[1]-vec.y)*(s0[1]-vec.y)+h*h); + } + + /** Perform a single RK4 step without updating the covariance matrix. */ + private double[] RK4step(int sector, double z0, double h, Swim swim, double[] sInit, + double[] sPrev, double qv) { + swim.Bfield(sector, sInit[0]+h*sPrev[0], sInit[1]+h*sPrev[1], z0+h, _b); + double[] sNext = {0,0,0,0}; + sNext[0] = sInit[2] + h*sPrev[2]; + sNext[1] = sInit[3] + h*sPrev[3]; + double C = C(sNext[0], sNext[1]); + sNext[2] = qv * Ax(C, sNext[0], sNext[1]); + sNext[3] = qv * Ay(C, sNext[0], sNext[1]); + return sNext; + } + + /** Transport using Runge Kutta 4, updating the covariance matrix. */ + public void RK4transport(int sector, double h, Swim swim, Matrix cMat, StateVec vec) { + // Set initial state and Jacobian. + double qv = vec.Q*v; + RK4Vec s0 = new RK4Vec(vec); + RK4Vec sNull = new RK4Vec(); + + // Perform steps. + RK4Vec s1 = RK4step(sector, vec.z, 0, swim, s0, sNull, qv); + RK4Vec s2 = RK4step(sector, vec.z, 0.5*h, swim, s0, s1, qv); + RK4Vec s3 = RK4step(sector, vec.z, 0.5*h, swim, s0, s2, qv); + RK4Vec s4 = RK4step(sector, vec.z, h, swim, s0, s3, qv); + + // Compute and set final state and covariance matrix. + RK4Vec sF = computeFinalState(h, s0, s1, s2, s3, s4); + + vec.x = sF.x; + vec.y = sF.y; + vec.tx = sF.tx; + vec.ty = sF.ty; + vec.z += h; + vec.B = Math.sqrt(_b[0]*_b[0]+_b[1]*_b[1]+_b[2]*_b[2]); + vec.deltaPath += Math.sqrt((s0.x-vec.x)*(s0.x-vec.x) + (s0.y-vec.y)*(s0.y-vec.y) + h*h); + vec.CM.set(computeCovMat(cMat, sF)); + } + + /** Perform one RK4 step, updating the covariance matrix. */ + private RK4Vec RK4step(int sector, double z0, double h, Swim swim, + RK4Vec sInit, RK4Vec sPrev, double qv) { + RK4Vec sNext = new RK4Vec(); + swim.Bfield(sector, sInit.x + h*sPrev.x, sInit.y + h*sPrev.y, z0 + h, _b); + + // State. + sNext.x = sInit.tx + h*sPrev.tx; + sNext.y = sInit.ty + h*sPrev.ty; + + double Csq = Csq(sNext.x, sNext.y); + double C = C(Csq); + double Ax = Ax(C, sNext.x, sNext.y); + double Ay = Ay(C, sNext.x, sNext.y); + + sNext.tx = qv * Ax; + sNext.ty = qv * Ay; + + // Jacobian. + sNext.dxdtx0 = sInit.dtxdtx0 + h*sPrev.dtxdtx0; + sNext.dxdty0 = sInit.dtxdty0 + h*sPrev.dtxdty0; + sNext.dxdq0 = sInit.dtxdq0 + h*sPrev.dtxdq0; + sNext.dydtx0 = sInit.dtydtx0 + h*sPrev.dtydtx0; + sNext.dydty0 = sInit.dtydty0 + h*sPrev.dtydty0; + sNext.dydq0 = sInit.dtydq0 + h*sPrev.dtydq0; + + double dAx_dtx = dAx_dtx(C, Csq, Ax, sNext.x, sNext.y); + double dAx_dty = dAx_dty(C, Csq, Ax, sNext.x, sNext.y); + double dAy_dtx = dAy_dtx(C, Csq, Ay, sNext.x, sNext.y); + double dAy_dty = dAy_dty(C, Csq, Ay, sNext.x, sNext.y); + + sNext.dtxdtx0 = this.dtx_dtx0(qv, dAx_dtx, dAx_dty, sNext.dxdtx0, sNext.dydtx0); + sNext.dtxdty0 = this.dtx_dty0(qv, dAx_dty, sNext.dxdty0, sNext.dydty0); + sNext.dtxdq0 = this.dtx_dq0( qv, Ax, dAx_dtx, dAx_dty, sNext.dxdq0, sNext.dydq0); + sNext.dtydtx0 = this.dty_dtx0(qv, dAy_dtx, dAy_dty, sNext.dxdtx0, sNext.dydtx0); + sNext.dtydty0 = this.dty_dty0(qv, dAy_dty, sNext.dxdty0, sNext.dydty0); + sNext.dtydq0 = this.dty_dq0( qv, Ay, dAy_dtx, dAy_dty, sNext.dxdq0, sNext.dydq0); + + return sNext; + } + + /** Compute the final state for each entry in the internal state matrix. */ + private RK4Vec computeFinalState(double h, RK4Vec s0, RK4Vec s1, RK4Vec s2, RK4Vec s3, + RK4Vec s4) { + RK4Vec sF = new RK4Vec(); + + sF.x = s0.x + this.RK4(s1.x, s2.x, s3.x, s4.x, h); + sF.dxdtx0 = this.RK4(s1.dxdtx0, s2.dxdtx0, s3.dxdtx0, s4.dxdtx0, h); + sF.dxdty0 = this.RK4(s1.dxdty0, s2.dxdty0, s3.dxdty0, s4.dxdty0, h); + sF.dxdq0 = this.RK4(s1.dxdq0, s2.dxdq0, s3.dxdq0, s4.dxdq0, h); + + sF.y = s0.y + this.RK4(s1.y, s2.y, s3.y, s4.y, h); + sF.dydtx0 = this.RK4(s1.dydtx0, s2.dydtx0, s3.dydtx0, s4.dydtx0, h); + sF.dydty0 = this.RK4(s1.dydty0, s2.dydty0, s3.dydty0, s4.dydty0, h); + sF.dydq0 = this.RK4(s1.dydq0, s2.dydq0, s3.dydq0, s4.dydq0, h); + + sF.tx = s0.tx + this.RK4(s1.tx, s2.tx, s3.tx, s4.tx, h); + sF.dtxdtx0 = 1 + this.RK4(s1.dtxdtx0, s2.dtxdtx0, s3.dtxdtx0, s4.dtxdtx0, h); + sF.dtxdty0 = this.RK4(s1.dtxdty0, s2.dtxdty0, s3.dtxdty0, s4.dtxdty0, h); + sF.dtxdq0 = this.RK4(s1.dtxdq0, s2.dtxdq0, s3.dtxdq0, s4.dtxdq0, h); + + sF.ty = s0.ty + this.RK4(s1.ty, s2.ty, s3.ty, s4.ty, h); + sF.dtydtx0 = this.RK4(s1.dtydtx0, s2.dtydtx0, s3.dtydtx0, s4.dtydtx0, h); + sF.dtydty0 = 1 + this.RK4(s1.dtydty0, s2.dtydty0, s3.dtydty0, s4.dtydty0, h); + sF.dtydq0 = this.RK4(s1.dtydq0, s2.dtydq0, s3.dtydq0, s4.dtydq0, h); + + return sF; + } + + /** Compute the final covariance matrix. covMat = FCF^T. */ + private double[][] computeCovMat(Matrix C, RK4Vec sF) { + double[][] cNext = new double[5][5]; + cNext[0][0] = C.get(0,0)+C.get(2,0)*sF.dxdtx0+C.get(3,0)*sF.dxdty0+C.get(4,0)*sF.dxdq0 + + sF.dxdq0 *(C.get(0,4)+C.get(2,4)*sF.dxdtx0+C.get(3,4)*sF.dxdty0+C.get(4,4)*sF.dxdq0) + + sF.dxdtx0*(C.get(0,2)+C.get(2,2)*sF.dxdtx0+C.get(3,2)*sF.dxdty0+C.get(4,2)*sF.dxdq0) + + sF.dxdty0*(C.get(0,3)+C.get(2,3)*sF.dxdtx0+C.get(3,3)*sF.dxdty0+C.get(4,3)*sF.dxdq0); + + cNext[0][1] = C.get(0,1)+C.get(2,1)*sF.dxdtx0+C.get(3,1)*sF.dxdty0+C.get(4,1)*sF.dxdq0 + + sF.dydq0 *(C.get(0,4)+C.get(2,4)*sF.dxdtx0+C.get(3,4)*sF.dxdty0+C.get(4,4)*sF.dxdq0) + + sF.dydtx0*(C.get(0,2)+C.get(2,2)*sF.dxdtx0+C.get(3,2)*sF.dxdty0+C.get(4,2)*sF.dxdq0) + + sF.dydty0*(C.get(0,3)+C.get(2,3)*sF.dxdtx0+C.get(3,3)*sF.dxdty0+C.get(4,3)*sF.dxdq0); + + cNext[0][2] = sF.dtxdq0 *(C.get(0,4)+C.get(2,4)*sF.dxdtx0+C.get(3,4)*sF.dxdty0+C.get(4,4)*sF.dxdq0) + + sF.dtxdtx0*(C.get(0,2)+C.get(2,2)*sF.dxdtx0+C.get(3,2)*sF.dxdty0+C.get(4,2)*sF.dxdq0) + + sF.dtxdty0*(C.get(0,3)+C.get(2,3)*sF.dxdtx0+C.get(3,3)*sF.dxdty0+C.get(4,3)*sF.dxdq0); + + cNext[0][3] = sF.dtydq0 *(C.get(0,4)+C.get(2,4)*sF.dxdtx0+C.get(3,4)*sF.dxdty0+C.get(4,4)*sF.dxdq0) + + sF.dtydtx0*(C.get(0,2)+C.get(2,2)*sF.dxdtx0+C.get(3,2)*sF.dxdty0+C.get(4,2)*sF.dxdq0) + + sF.dtydty0*(C.get(0,3)+C.get(2,3)*sF.dxdtx0+C.get(3,3)*sF.dxdty0+C.get(4,3)*sF.dxdq0); + + cNext[0][4] = C.get(0,4)+C.get(2,4)*sF.dxdtx0+C.get(3,4)*sF.dxdty0+C.get(4,4)*sF.dxdq0; + + cNext[1][0] = C.get(1,0)+C.get(2,0)*sF.dydtx0+C.get(3,0)*sF.dydty0+C.get(4,0)*sF.dydq0 + + sF.dxdq0 *(C.get(1,4)+C.get(2,4)*sF.dydtx0+C.get(3,4)*sF.dydty0+C.get(4,4)*sF.dydq0) + + sF.dxdtx0*(C.get(1,2)+C.get(2,2)*sF.dydtx0+C.get(3,2)*sF.dydty0+C.get(4,2)*sF.dydq0) + + sF.dxdty0*(C.get(1,3)+C.get(2,3)*sF.dydtx0+C.get(3,3)*sF.dydty0+C.get(4,3)*sF.dydq0); + + cNext[1][1] = C.get(1,1)+C.get(2,1)*sF.dydtx0+C.get(3,1)*sF.dydty0+C.get(4,1)*sF.dydq0 + + sF.dydq0 *(C.get(1,4)+C.get(2,4)*sF.dydtx0+C.get(3,4)*sF.dydty0+C.get(4,4)*sF.dydq0) + + sF.dydtx0*(C.get(1,2)+C.get(2,2)*sF.dydtx0+C.get(3,2)*sF.dydty0+C.get(4,2)*sF.dydq0) + + sF.dydty0*(C.get(1,3)+C.get(2,3)*sF.dydtx0+C.get(3,3)*sF.dydty0+C.get(4,3)*sF.dydq0); + + cNext[1][2] = sF.dtxdq0 *(C.get(1,4)+C.get(2,4)*sF.dydtx0+C.get(3,4)*sF.dydty0+C.get(4,4)*sF.dydq0) + + sF.dtxdtx0*(C.get(1,2)+C.get(2,2)*sF.dydtx0+C.get(3,2)*sF.dydty0+C.get(4,2)*sF.dydq0) + + sF.dtxdty0*(C.get(1,3)+C.get(2,3)*sF.dydtx0+C.get(3,3)*sF.dydty0+C.get(4,3)*sF.dydq0); + + cNext[1][3] = sF.dtydq0 *(C.get(1,4)+C.get(2,4)*sF.dydtx0+C.get(3,4)*sF.dydty0+C.get(4,4)*sF.dydq0) + + sF.dtydtx0*(C.get(1,2)+C.get(2,2)*sF.dydtx0+C.get(3,2)*sF.dydty0+C.get(4,2)*sF.dydq0) + + sF.dtydty0*(C.get(1,3)+C.get(2,3)*sF.dydtx0+C.get(3,3)*sF.dydty0+C.get(4,3)*sF.dydq0); + + cNext[1][4] = C.get(1,4)+C.get(2,4)*sF.dydtx0+C.get(3,4)*sF.dydty0+C.get(4,4)*sF.dydq0; + + cNext[2][0] = C.get(2,0)*sF.dtxdtx0+C.get(3,0)*sF.dtxdty0+C.get(4,0)*sF.dtxdq0 + + sF.dxdq0 *(C.get(2,4)*sF.dtxdtx0+C.get(3,4)*sF.dtxdty0+C.get(4,4)*sF.dtxdq0) + + sF.dxdtx0*(C.get(2,2)*sF.dtxdtx0+C.get(3,2)*sF.dtxdty0+C.get(4,2)*sF.dtxdq0) + + sF.dxdty0*(C.get(2,3)*sF.dtxdtx0+C.get(3,3)*sF.dtxdty0+C.get(4,3)*sF.dtxdq0); + + cNext[2][1] = C.get(2,1)*sF.dtxdtx0+C.get(3,1)*sF.dtxdty0+C.get(4,1)*sF.dtxdq0 + + sF.dydq0 *(C.get(2,4)*sF.dtxdtx0+C.get(3,4)*sF.dtxdty0+C.get(4,4)*sF.dtxdq0) + + sF.dydtx0*(C.get(2,2)*sF.dtxdtx0+C.get(3,2)*sF.dtxdty0+C.get(4,2)*sF.dtxdq0) + + sF.dydty0*(C.get(2,3)*sF.dtxdtx0+C.get(3,3)*sF.dtxdty0+C.get(4,3)*sF.dtxdq0); + + cNext[2][2] = sF.dtxdq0 *(C.get(2,4)*sF.dtxdtx0+C.get(3,4)*sF.dtxdty0+C.get(4,4)*sF.dtxdq0) + + sF.dtxdtx0*(C.get(2,2)*sF.dtxdtx0+C.get(3,2)*sF.dtxdty0+C.get(4,2)*sF.dtxdq0) + + sF.dtxdty0*(C.get(2,3)*sF.dtxdtx0+C.get(3,3)*sF.dtxdty0+C.get(4,3)*sF.dtxdq0); + + cNext[2][3] = sF.dtydq0 *(C.get(2,4)*sF.dtxdtx0+C.get(3,4)*sF.dtxdty0+C.get(4,4)*sF.dtxdq0) + + sF.dtydtx0*(C.get(2,2)*sF.dtxdtx0+C.get(3,2)*sF.dtxdty0+C.get(4,2)*sF.dtxdq0) + + sF.dtydty0*(C.get(2,3)*sF.dtxdtx0+C.get(3,3)*sF.dtxdty0+C.get(4,3)*sF.dtxdq0); + + cNext[2][4] = C.get(2,4)*sF.dtxdtx0+C.get(3,4)*sF.dtxdty0+C.get(4,4)*sF.dtxdq0; + + cNext[3][0] = C.get(2,0)*sF.dtydtx0+C.get(3,0)*sF.dtydty0+C.get(4,0)*sF.dtydq0 + + sF.dxdq0 *(C.get(2,4)*sF.dtydtx0+C.get(3,4)*sF.dtydty0+C.get(4,4)*sF.dtydq0) + + sF.dxdtx0*(C.get(2,2)*sF.dtydtx0+C.get(3,2)*sF.dtydty0+C.get(4,2)*sF.dtydq0) + + sF.dxdty0*(C.get(2,3)*sF.dtydtx0+C.get(3,3)*sF.dtydty0+C.get(4,3)*sF.dtydq0); + + cNext[3][1] = C.get(2,1)*sF.dtydtx0+C.get(3,1)*sF.dtydty0+C.get(4,1)*sF.dtydq0 + + sF.dydq0 *(C.get(2,4)*sF.dtydtx0+C.get(3,4)*sF.dtydty0+C.get(4,4)*sF.dtydq0) + + sF.dydtx0*(C.get(2,2)*sF.dtydtx0+C.get(3,2)*sF.dtydty0+C.get(4,2)*sF.dtydq0) + + sF.dydty0*(C.get(2,3)*sF.dtydtx0+C.get(3,3)*sF.dtydty0+C.get(4,3)*sF.dtydq0); + + cNext[3][2] = sF.dtxdq0 *(C.get(2,4)*sF.dtydtx0+C.get(3,4)*sF.dtydty0+C.get(4,4)*sF.dtydq0) + + sF.dtxdtx0*(C.get(2,2)*sF.dtydtx0+C.get(3,2)*sF.dtydty0+C.get(4,2)*sF.dtydq0) + + sF.dtxdty0*(C.get(2,3)*sF.dtydtx0+C.get(3,3)*sF.dtydty0+C.get(4,3)*sF.dtydq0); + + cNext[3][3] = sF.dtydq0 *(C.get(2,4)*sF.dtydtx0+C.get(3,4)*sF.dtydty0+C.get(4,4)*sF.dtydq0) + + sF.dtydtx0*(C.get(2,2)*sF.dtydtx0+C.get(3,2)*sF.dtydty0+C.get(4,2)*sF.dtydq0) + + sF.dtydty0*(C.get(2,3)*sF.dtydtx0+C.get(3,3)*sF.dtydty0+C.get(4,3)*sF.dtydq0); + + cNext[3][4] = C.get(2,4)*sF.dtydtx0+C.get(3,4)*sF.dtydty0+C.get(4,4)*sF.dtydq0; + + cNext[4][0] = C.get(4,0)+C.get(4,2)*sF.dxdtx0+C.get(4,3)*sF.dxdty0+C.get(4,4)*sF.dxdq0; + + cNext[4][1] = C.get(4,1)+C.get(4,2)*sF.dydtx0+C.get(4,3)*sF.dydty0+C.get(4,4)*sF.dydq0; + + cNext[4][2] = C.get(4,2)*sF.dtxdtx0+C.get(4,3)*sF.dtxdty0+C.get(4,4)*sF.dtxdq0; + + cNext[4][3] = C.get(4,2)*sF.dtydtx0+C.get(4,3)*sF.dtydty0+C.get(4,4)*sF.dtydq0; + + cNext[4][4] = C.get(4,4); + + return cNext; + } + + /** Get the final RK4 estimate. */ + private double RK4(double k1, double k2, double k3, double k4, double h) { + return (h/6) * (k1 + 2*k2 + 2*k3 + k4); + } + + // Auxiliary calculations. + private double C(double tx, double ty) { + return Math.sqrt(1 + tx*tx + ty*ty); + } + private double C(double Csq) { + return Math.sqrt(Csq); + } + private double Csq(double tx, double ty) { + return 1 + tx*tx + ty*ty; + } + + private double Ax(double C, double tx, double ty) { + return C * (ty * (tx * _b[0] + _b[2]) - (1 + tx * tx) * _b[1]); + } + private double Ay(double C, double tx, double ty) { + return C * (-tx * (ty * _b[1] + _b[2]) + (1 + ty * ty) * _b[0]); + } + + private double dAx_dtx(double C, double C2, double Ax, double tx, double ty) { + return tx * Ax/C2 + C * (ty*_b[0] - 2*tx*_b[1]); + } + private double dAx_dty(double C, double C2, double Ax, double tx, double ty) { + return ty * Ax/C2 + C * (tx*_b[0] + _b[2]); + } + private double dAy_dtx(double C, double C2, double Ay, double tx, double ty) { + return tx * Ay/C2 + C * (-ty*_b[1] - _b[2]); + } + private double dAy_dty(double C, double C2, double Ay, double tx, double ty) { + return ty * Ay/C2 + C * (-tx*_b[1] + 2*ty*_b[0]); + } + + // Total derivatives. + private double dtx_dtx0(double qv, double dAx_dtx, double dAx_dty, + double dtx_dtx0, double dty_dtx0) { + return qv * (dAx_dtx*dtx_dtx0 + dAx_dty*dty_dtx0); + } + private double dtx_dty0(double qv, double dAx_dty, double dtx_dty0, double dty_dty0) { + return qv * (dAx_dty*dtx_dty0 + dAx_dty*dty_dty0); + } + private double dtx_dq0(double qv, double Ax, double dAx_dtx, double dAx_dty, + double dtx_dq0, double dty_dq0) { + return v*Ax + qv * (dAx_dtx*dtx_dq0 + dAx_dty*dty_dq0); + } + private double dty_dtx0(double qv, double dAy_dtx, double dAy_dty, + double dtx_dtx0, double dty_dtx0) { + return qv * (dAy_dtx*dtx_dtx0 + dAy_dty*dty_dtx0); + } + private double dty_dty0(double qv, double dAy_dty, double dtx_dty0, double dty_dty0) { + return qv * (dAy_dty*dtx_dty0 + dAy_dty*dty_dty0); + } + private double dty_dq0(double qv, double Ay, double dAy_dtx, double dAy_dty, + double dtx_dq0, double dty_dq0) { + return v*Ay + qv * (dAy_dtx*dtx_dq0 + dAy_dty*dty_dq0); + } + + /** State vector and its derivatives used internally. */ + private class RK4Vec { + public double x = 0; + public double dxdtx0 = 0; + public double dxdty0 = 0; + public double dxdq0 = 0; + + public double y = 0; + public double dydtx0 = 0; + public double dydty0 = 0; + public double dydq0 = 0; + + public double tx = 0; + public double dtxdtx0 = 0; + public double dtxdty0 = 0; + public double dtxdq0 = 0; + + public double ty = 0; + public double dtydtx0 = 0; + public double dtydty0 = 0; + public double dtydq0 = 0; + + RK4Vec() {} + RK4Vec(StateVec vec) { + this.x = vec.x; + this.y = vec.y; + this.tx = vec.tx; + this.ty = vec.ty; + + this.dtxdtx0 = 1; + this.dtydty0 = 1; + } + } +} diff --git a/reconstruction/dc/pom.xml b/reconstruction/dc/pom.xml index 726fb9adc..796939362 100644 --- a/reconstruction/dc/pom.xml +++ b/reconstruction/dc/pom.xml @@ -19,6 +19,12 @@ + + org.jlab.clas + clas-tracking + 8.4.0-SNAPSHOT + + org.jlab.clas clas-jcsg diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index ec1fb3f56..c2f66a54c 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -1,6 +1,7 @@ package org.jlab.rec.dc.track; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; @@ -10,14 +11,15 @@ import org.jlab.clas.clas.math.FastMath; import org.jlab.clas.swimtools.Swim; import org.jlab.detector.geant4.v2.DCGeant4Factory; +import org.jlab.geom.prim.Line3D; import org.jlab.geom.prim.Point3D; import org.jlab.geom.prim.Vector3D; +import org.jlab.jnp.matrix.Matrix; import org.jlab.rec.dc.Constants; import org.jlab.rec.dc.cross.Cross; import org.jlab.rec.dc.cross.CrossList; import org.jlab.rec.dc.hit.FittedHit; import org.jlab.rec.dc.segment.Segment; -import org.jlab.rec.dc.track.fit.KFitterDoca; import org.jlab.rec.dc.trajectory.StateVec; import org.jlab.rec.dc.trajectory.Trajectory; import org.jlab.rec.dc.trajectory.TrajectoryFinder; @@ -25,11 +27,17 @@ import trackfitter.fitter.LineFitPars; import trackfitter.fitter.LineFitter; +import org.jlab.clas.tracking.kalmanfilter.Surface; +import org.jlab.clas.tracking.kalmanfilter.zReference.KFitter; +import org.jlab.clas.tracking.kalmanfilter.zReference.StateVecs; +import org.jlab.clas.tracking.utilities.MatrixOps.Libr; +import org.jlab.clas.tracking.utilities.RungeKuttaDoca; + /** * A class with a method implementing an algorithm that finds lists of track * candidates in the DC * - * @author ziegler + * @author ziegler, tongtong */ public class TrackCandListFinder { @@ -352,6 +360,7 @@ public void setTrackPars(Track cand, // swimming to a ref points outside of the last DC region double[] VecAtTarOut = dcSwim.SwimToPlaneTiltSecSys(cand.get(0).get_Sector(), 592); + if (VecAtTarOut == null) { return; } @@ -689,24 +698,6 @@ else if(t1.get_FitChi2()==t2.get_FitChi2() && i>j) trkcands.addAll(selectedTracks); } -// public void removeOverlappingTracks(List trkcands) { -// List selectedTracks = new ArrayList(); -// List list = new ArrayList(); -// int size = trkcands.size(); -// for (int i = 0; i < size; i++) { -// list.clear(); -// this.getOverlapLists(trkcands.get(i), trkcands, list); -// trkcands.removeAll(list); -// size -= list.size(); -// Track selectedTrk = this.FindBestTrack(list); -// if (selectedTrk == null) -// continue; -// //if(this.ListContainsTrack(selectedTracks, selectedTrk)==false) -// selectedTracks.add(selectedTrk); -// } -// //trkcands.removeAll(trkcands); -// trkcands.addAll(selectedTracks); -// } /** * @param selectedTracks the list of selected tracks * @param selectedTrk the selected track @@ -902,48 +893,49 @@ private List findStraightTracks(CrossList crossList, DCGeant4Factory DcDe cand.get(0).get_Dir().x() / cand.get(0).get_Dir().z(), cand.get(0).get_Dir().y() / cand.get(0).get_Dir().z()); cand.set_StateVecAtReg1MiddlePlane(VecAtReg1MiddlePlane); - // initialize the fitter with the candidate track - KFitterDoca kFit = new KFitterDoca(cand, DcDetector, false, dcSwim, 0); - kFit.totNumIter = 1; - - if(LOGGER.getLevel()==Level.FINE) { - startTime = System.currentTimeMillis(); - } - kFit.runFitter(cand.get(0).get_Sector()); - LOGGER.log(Level.FINE, "Kalman fitter = " + (System.currentTimeMillis() - startTime)); - - if (kFit.finalStateVec == null) { + + LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); + + KFitter kFZRef = new KFitter(true, 1, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(cand, DcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(cand, measSurfaces.get(0).z, 0, initSV, dcSwim, new float[3]); + kFZRef.init(measSurfaces, initSV); + + kFZRef.runFitter(); + + if (kFZRef.finalStateVec == null) { continue; } + + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); - // initialize the state vector corresponding to the last measurement site StateVec fn = new StateVec(); - - //LOGGER.log(Level.FINE, " fit failed due to chi2 "+kFit.setFitFailed+" p "+1./Math.abs(kFit.finalStateVec.Q)); - if (!kFit.setFitFailed && kFit.finalStateVec != null) { - // set the state vector at the last measurement site - fn.set(kFit.finalStateVec.x, kFit.finalStateVec.y, kFit.finalStateVec.tx, kFit.finalStateVec.ty); - //set the track parameters if the filter does not fail - fn.setZ(kFit.finalStateVec.z); - cand.setFinalStateVec(fn); - cand.set_P(1. / Math.abs(kFit.finalStateVec.Q)); - cand.set_Q((int) Math.signum(kFit.finalStateVec.Q)); - this.setTrackPars(cand, traj, trjFind, fn, kFit.finalStateVec.z, DcDetector, dcSwim); - if(cand.fit_Successful==true) { - // candidate parameters are set from the state vector - cand.set_FitChi2(kFit.chi2); - cand.set_FitNDF(kFit.NDF); - cand.set_FitConvergenceStatus(kFit.ConvStatus); - cand.set_Id(cands.size() + 1); - cand.set_CovMat(kFit.finalCovMat.covMat); - cand.setStateVecs(kFit.kfStateVecsAlongTrajectory); - // add candidate to list of tracks - cands.add(cand); - } - } - //this.matchHits(traj.getStateVecs(), cand, DcDetector); -// cands.add(cand); + if (!kFZRef.setFitFailed && kFZRef.finalStateVec != null) { + fn.set(kFZRef.finalStateVec.x, kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); + fn.setZ(kFZRef.finalStateVec.z); + cand.setFinalStateVec(fn); + + // set the track parameters + cand.set_P(1. / Math.abs(kFZRef.finalStateVec.Q)); + cand.set_Q((int) Math.signum(kFZRef.finalStateVec.Q)); + this.setTrackPars(cand, traj, trjFind, fn, kFZRef.finalStateVec.z, DcDetector, dcSwim); + + if (cand.fit_Successful == true) { + // candidate parameters + cand.set_FitChi2(kFZRef.chi2); + cand.set_FitNDF(kFZRef.NDF); + cand.set_FitConvergenceStatus(kFZRef.ConvStatus); + cand.set_Id(cands.size() + 1); + cand.set_CovMat(kFZRef.finalStateVec.CM); + cand.setStateVecs(kfStateVecsAlongTrajectory); + + // add candidate to list of tracks + cands.add(cand); + } + } } } } @@ -952,6 +944,8 @@ private List findStraightTracks(CrossList crossList, DCGeant4Factory DcDe private List findCurvedTracks(CrossList crossList, DCGeant4Factory DcDetector, double TORSCALE, Swim dcSwim, boolean donotapplyCuts) { + + if(LOGGER.getLevel()==Level.FINE) { startTime2 = System.currentTimeMillis(); } @@ -1142,42 +1136,49 @@ private List findCurvedTracks(CrossList crossList, DCGeant4Factory DcDete cand.get(0).get_Dir().x() / cand.get(0).get_Dir().z(), cand.get(0).get_Dir().y() / cand.get(0).get_Dir().z()); cand.set_StateVecAtReg1MiddlePlane(VecAtReg1MiddlePlane); - // initialize the fitter with the candidate track - KFitterDoca kFit = null; - StateVec fitStateVec = null; - LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); + StateVec fitStateVec = null; // prefer to initialize the seed with region 2 cross due to higher background in region 1 int crossIdxinList = 1; if (cand.get(1).isPseudoCross) { crossIdxinList = 0; } - kFit = new KFitterDoca(cand, DcDetector, false, dcSwim, crossIdxinList); - kFit.totNumIter = 10; - kFit.runFitter(cand.get(0).get_Sector()); - if (kFit.finalStateVec == null) { + + LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); + + KFitter kFZRef = new KFitter(true, 10, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(cand, DcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(cand, measSurfaces.get(0).z, crossIdxinList, initSV, dcSwim, new float[3]); + kFZRef.init(measSurfaces, initSV); + + kFZRef.runFitter(); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + if (kFZRef.finalStateVec == null) { continue; } else { - if (kFit.chi2 < Constants.MAXCHI2) { + if (kFZRef.chi2 < Constants.MAXCHI2) { - fitStateVec = new StateVec(kFit.finalStateVec.x, - kFit.finalStateVec.y, kFit.finalStateVec.tx, kFit.finalStateVec.ty); - q = (int) Math.signum(kFit.finalStateVec.Q); - p = 1. / Math.abs(kFit.finalStateVec.Q); - fitStateVec.setZ(kFit.finalStateVec.z); + fitStateVec = new StateVec(kFZRef.finalStateVec.x, + kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); + q = (int) Math.signum(kFZRef.finalStateVec.Q); + p = 1. / Math.abs(kFZRef.finalStateVec.Q); + fitStateVec.setZ(kFZRef.finalStateVec.z); //set the track parameters cand.set_P(p); cand.set_Q(q); // candidate parameters - cand.set_FitChi2(kFit.chi2); - cand.set_FitNDF(kFit.NDF); - cand.set_FitConvergenceStatus(kFit.ConvStatus); + cand.set_FitChi2(kFZRef.chi2); + cand.set_FitNDF(kFZRef.NDF); + cand.set_FitConvergenceStatus(kFZRef.ConvStatus); - cand.set_CovMat(kFit.finalCovMat.covMat); - cand.setStateVecs(kFit.kfStateVecsAlongTrajectory); + cand.set_CovMat(kFZRef.finalStateVec.CM); + cand.setStateVecs(kfStateVecsAlongTrajectory); cand.setFinalStateVec(fitStateVec); cand.set_Id(cands.size() + 1); @@ -1187,15 +1188,204 @@ private List findCurvedTracks(CrossList crossList, DCGeant4Factory DcDete DcDetector, dcSwim); // add candidate to list of tracks if (cand.fit_Successful = true) { - cands.add(cand); + cands.add(cand); } } } + + } } } } return cands; } + + public List setKFStateVecsAlongTrajectory(KFitter kFZRef) { + List kfStateVecsAlongTrajectory = new ArrayList<>(); + + for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + sv.setZ(svc.z); + sv.setB(svc.B); + sv.setPathLength(svc.getPathLength()); + sv.setProjector(svc.getProjector()); + sv.setProjectorDoca(svc.getProjectorDoca()); + kfStateVecsAlongTrajectory.add(sv); + } + + return kfStateVecsAlongTrajectory; + } + + public void getInitState(Track trkcand, double z0, int c, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initStateVec, Swim dcSwim, float[] bf) { + + Point3D trkCrs = trkcand.get(c).get_Point(); + Point3D trkCrsD = trkcand.get(c).get_Dir(); + //org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + initStateVec.x = trkCrs.x(); + initStateVec.y = trkCrs.y(); + initStateVec.z = trkCrs.z(); + initStateVec.tx = trkCrsD.x()/trkCrsD.z(); + initStateVec.ty = trkCrsD.y()/trkCrsD.z(); + initStateVec.Q = trkcand.get_Q() / trkcand.get_P(); + + RungeKuttaDoca rk = new RungeKuttaDoca(); + rk.SwimToZ(trkcand.get(0).get_Sector(), initStateVec, dcSwim, z0, bf); + + //LOGGER.log(Level.FINE, (0)+"] init "+this.trackTraj.get(0).x+","+this.trackTraj.get(0).y+","+ + // this.trackTraj.get(0).z+","+this.trackTraj.get(0).tx+","+this.trackTraj.get(0).ty+" "+1/this.trackTraj.get(0).Q); + double err_sl1 = trkcand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitSlopeErr(); + double err_sl2 = trkcand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitSlopeErr(); + double err_it1 = trkcand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitInterceptErr(); + double err_it2 = trkcand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitInterceptErr(); + double wy_over_wx = (FastMath.cos(Math.toRadians(6.)) / FastMath.sin(Math.toRadians(6.))); + + double eux = 0.5 * Math.sqrt(err_sl1 * err_sl1 + err_sl2 * err_sl2); + double euy = 0.5 * wy_over_wx * Math.sqrt(err_sl1 * err_sl1 + err_sl2 * err_sl2); + double z = trkcand.get(0).get_Point().z(); + double ex = 0.5 * Math.sqrt(err_it1 * err_it1 + err_it2 * err_it2 + z * z * (err_sl1 * err_sl1 + err_sl2 * err_sl2)); + double ey = 0.5 * wy_over_wx * Math.sqrt(err_it1 * err_it1 + err_it2 * err_it2 + z * z * (err_sl1 * err_sl1 + err_sl2 * err_sl2)); + double epSq = 0.001 * trkcand.get_P() * trkcand.get_P(); + + + Matrix initCMatrix = new Matrix(); + initCMatrix.set(ex * ex, 0, 0, 0, 0, + 0, ey * ey, 0, 0, 0, + 0, 0, eux * eux, 0, 0, + 0, 0, 0, euy * euy, 0, + 0, 0, 0, 0, epSq + ); + + + initStateVec.CM = initCMatrix; + } + + public List getMeasSurfaces(Track trkcand, DCGeant4Factory DcDetector) { + + List hOTS = new ArrayList<>(); // the list of hits on track + FittedHit hitOnTrk; + // loops over the regions (1 to 3) and the superlayers in a region (1 to 2) and obtains the hits on track + for (int c = 0; c < 3; c++) { + for (int s = 0; s < 2; s++) { + for (int h = 0; h < trkcand.get(c).get(s).size(); h++) { + // if (trkcand.get(c).get(s).get(h).get_Id() == -1 /*|| trkcand.get(c).get(s).get(h).get_Id() == 0*/) { //PASS1 + if (trkcand.get(c).get(s).get(h).get_Id() == -1 || trkcand.get(c).get(s).get(h).get_Id() == 0) { + continue; + } + trkcand.get(c).get(s).get(h).calc_CellSize(DcDetector); + hitOnTrk = trkcand.get(c).get(s).get(h); + int slayr = trkcand.get(c).get(s).get(h).get_Superlayer(); + + double sl1 = trkcand.get(c).get(s).get_fittedCluster().get_clusterLineFitSlope(); + double it1 = trkcand.get(c).get(s).get_fittedCluster().get_clusterLineFitIntercept(); + + double Z = hitOnTrk.get_Z(); + double X = sl1 * Z + it1; + + HitOnTrack hot = new HitOnTrack(slayr, X, Z, + trkcand.get(c).get(s).get(h).get_WireMaxSag(), + trkcand.get(c).get(s).get(h).get_WireLine() + ); + double err_sl1 = trkcand.get(c).get(s).get_fittedCluster().get_clusterLineFitSlopeErr(); + + double err_it1 = trkcand.get(c).get(s).get_fittedCluster().get_clusterLineFitInterceptErr(); + double err_cov1 = trkcand.get(c).get(s).get_fittedCluster().get_clusterLineFitSlIntCov(); + + hot._Unc[0] = Math.sqrt(err_sl1 * err_sl1 * Z * Z + err_it1 * err_it1); + hot._hitError = err_sl1 * err_sl1 * Z * Z + err_it1 * err_it1 + 2 * Z * err_cov1 + trkcand.get(c).get(s).get(h).get_DocaErr()*trkcand.get(c).get(s).get(h).get_DocaErr(); + //if(trkcand.get(c).get(s).get(h).get_Time()/CCDBConstants.getTMAXSUPERLAYER()[trkcand.get(c).get(s).get(h).get_Sector()-1][trkcand.get(c).get(s).get(h).get_Superlayer()-1]<1.1) + // hot._hitError = 100000; //exclude outoftimers from fit + hot.region = trkcand.get(c).get(s).get(h).get_Region(); + hot.sector = trkcand.get(c).get(s).get(h).get_Sector(); + hot.superlayer = trkcand.get(c).get(s).get(h).get_Superlayer(); + hot.layer = trkcand.get(c).get(s).get(h).get_Layer(); + + hot._doca[0] = trkcand.get(c).get(s).get(h).get_ClusFitDoca(); + + double LR = Math.signum(trkcand.get(c).get(s).get(h).get_XWire()-trkcand.get(c).get(s).get(h).get_X()); + + hot._doca[0]*=LR; + hot._hitError = trkcand.get(c).get(s).get(h).get_DocaErr()*trkcand.get(c).get(s).get(h).get_DocaErr(); + //LOGGER.log(Level.FINE, " Z "+Z+" ferr "+(float)(hot._Unc /(hot._hitError/4.))); + hot._Unc[0] = hot._hitError; + hOTS.add(hot); + + } + } + } + Collections.sort(hOTS); // sort the collection in order of increasing Z value (i.e. going downstream from the target) + // identify double hits and take the average position + for (int i = 0; i < hOTS.size(); i++) { + if (i > 0) { + if (Math.abs(hOTS.get(i - 1)._Z - hOTS.get(i)._Z)<0.01) { + hOTS.get(i - 1)._doca[1] = hOTS.get(i)._doca[0]; + hOTS.get(i - 1)._Unc[1] = hOTS.get(i)._Unc[0]; + hOTS.get(i - 1)._wireLine[1] = hOTS.get(i)._wireLine[0]; + hOTS.remove(i); + hOTS.get(i - 1).nMeas = 2; + } + } + } + + List surfaces = new ArrayList<>(hOTS.size()); + for (int i = 0; i < hOTS.size(); i++) { + Surface surf = new Surface(hOTS.get(i).region, hOTS.get(i)._Z, hOTS.get(i)._X, hOTS.get(i)._tilt, hOTS.get(i)._wireMaxSag, + hOTS.get(i)._doca, hOTS.get(i)._Unc, hOTS.get(i)._hitError, hOTS.get(i)._wireLine); + surf.setSector(hOTS.get(i).sector); + surf.setSuperLayer(hOTS.get(i).superlayer); + surf.setLayer(hOTS.get(i).layer); + surf.setNMeas(hOTS.get(i).nMeas); + surfaces.add(i, surf); + } + + return surfaces; + } + + public class HitOnTrack implements Comparable { + + public double _hitError; + public double _X; + public double _Z; + public double[] _Unc = new double[2]; + public double _tilt; + public double[] _doca = new double[2]; + public double _wireMaxSag; + public Line3D[] _wireLine = new Line3D[2]; + public int region; + public int superlayer; + public int sector; + public int layer; + public int nMeas = 1; + + + public HitOnTrack(int superlayer, double X, double Z, double wiremaxsag, Line3D wireLine) { + _X = X; + _Z = Z; + _wireMaxSag = wiremaxsag; + _wireLine[0] = wireLine; + _doca[0] = -99; + _doca[1] = -99; + _Unc[0] = 1; + _Unc[1] = 1; + + //use stereo angle in fit based on wire direction + _tilt = 90-Math.toDegrees(wireLine.direction().asUnit().angle(new Vector3D(1,0,0))); + } + + @Override + public int compareTo(HitOnTrack o) { + if (this._Z == o._Z) { + return 0; + } + if (this._Z > o._Z) { + return 1; + } else { + return -1; + } + } + } + } diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index 97760eb9e..d5b3c275f 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -1,16 +1,20 @@ package org.jlab.service.dc; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Level; import org.jlab.clas.swimtools.Swim; +import org.jlab.detector.geant4.v2.DCGeant4Factory; +import org.jlab.geom.prim.Line3D; import org.jlab.geom.prim.Point3D; import org.jlab.geom.prim.Vector3D; import org.jlab.io.base.DataBank; import org.jlab.io.base.DataEvent; +import org.jlab.jnp.matrix.Matrix; import org.jlab.rec.dc.Constants; import org.jlab.rec.dc.banks.HitReader; import org.jlab.rec.dc.banks.RecoBankWriter; @@ -27,12 +31,17 @@ import org.jlab.rec.dc.timetodistance.TimeToDistanceEstimator; import org.jlab.rec.dc.track.Track; import org.jlab.rec.dc.track.TrackCandListFinder; -import org.jlab.rec.dc.track.fit.KFitterDoca; import org.jlab.rec.dc.trajectory.StateVec; import org.jlab.rec.dc.trajectory.Trajectory; import org.jlab.rec.dc.trajectory.TrajectoryFinder; import org.jlab.utils.groups.IndexedTable; +import org.jlab.clas.tracking.kalmanfilter.Surface; +import org.jlab.clas.tracking.kalmanfilter.zReference.KFitter; +import org.jlab.clas.tracking.kalmanfilter.zReference.StateVecs; +import org.jlab.clas.tracking.utilities.MatrixOps.Libr; +import org.jlab.clas.tracking.utilities.RungeKuttaDoca; + public class DCTBEngine extends DCEngine { private TimeToDistanceEstimator tde = null; @@ -60,7 +69,6 @@ public void setDropBanks() { @Override public boolean processDataEvent(DataEvent event) { - int run = this.getRun(event); if(run==0) return true; @@ -165,9 +173,6 @@ public boolean processDataEvent(DataEvent event) { DataBank trkbank = event.getBank(this.getBanks().getInputTracksBank()); //DataBank trkcovbank = event.getBank("TimeBasedTrkg::TBCovMat"); int trkrows = trkbank.rows(); - //if(trkbank.rows()!=trkcovbank.rows()) { - // return true; // HB tracks not saved correctly - //} Track[] TrackArray = new Track[trkrows]; for (int i = 0; i < trkrows; i++) { Track HBtrk = new Track(); @@ -182,15 +187,6 @@ public boolean processDataEvent(DataEvent event) { trkbank.getFloat("tx", i), trkbank.getFloat("ty", i)); HBFinalSV.setZ(trkbank.getFloat("z", i)); HBtrk.setFinalStateVec(HBFinalSV); -// Matrix initCMatrix = new Matrix(); -// initCMatrix.set(new double[][]{ -// {trkcovbank.getFloat("C11", i), trkcovbank.getFloat("C12", i), trkcovbank.getFloat("C13", i), trkcovbank.getFloat("C14", i), trkcovbank.getFloat("C15", i)}, -// {trkcovbank.getFloat("C21", i), trkcovbank.getFloat("C22", i), trkcovbank.getFloat("C23", i), trkcovbank.getFloat("C24", i), trkcovbank.getFloat("C25", i)}, -// {trkcovbank.getFloat("C31", i), trkcovbank.getFloat("C32", i), trkcovbank.getFloat("C33", i), trkcovbank.getFloat("C34", i), trkcovbank.getFloat("C35", i)}, -// {trkcovbank.getFloat("C41", i), trkcovbank.getFloat("C42", i), trkcovbank.getFloat("C43", i), trkcovbank.getFloat("C44", i), trkcovbank.getFloat("C45", i)}, -// {trkcovbank.getFloat("C51", i), trkcovbank.getFloat("C52", i), trkcovbank.getFloat("C53", i), trkcovbank.getFloat("C54", i), trkcovbank.getFloat("C55", i)} -// }); -// HBtrk.set_CovMat(initCMatrix); TrackArray[HBtrk.get_Id()-1] = HBtrk; TrackArray[HBtrk.get_Id()-1].set_Status(0); } @@ -217,8 +213,9 @@ public boolean processDataEvent(DataEvent event) { beamYoffset += raster_bank.getFloat("y", 0); } TrackCandListFinder trkcandFinder = new TrackCandListFinder("TimeBased"); - TrajectoryFinder trjFind = new TrajectoryFinder(); - for (Track TrackArray1 : TrackArray) { + TrajectoryFinder trjFind = new TrajectoryFinder(); + + for (Track TrackArray1 : TrackArray) { if (TrackArray1 == null || TrackArray1.get_ListOfHBSegments() == null || TrackArray1.get_ListOfHBSegments().size() < 5) { continue; } @@ -228,44 +225,52 @@ public boolean processDataEvent(DataEvent event) { continue; } crosses.addAll(TrackArray1); - //if(TrackArray[i].get_FitChi2()>200) { - // resetTrackParams(TrackArray[i], new DCSwimmer()); - //} - KFitterDoca kFit = new KFitterDoca(TrackArray1, Constants.getInstance().dcDetector, true, dcSwim, 0); - StateVec fn = new StateVec(); - kFit.runFitter(TrackArray1.get(0).get_Sector()); - if (kFit.setFitFailed==false && kFit.finalStateVec!=null) { + + KFitter kFZRef = new KFitter(true, 30, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitter(); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + StateVec fn = new StateVec(); + if (kFZRef.setFitFailed==false && kFZRef.finalStateVec!=null) { // set the state vector at the last measurement site - fn.set(kFit.finalStateVec.x, kFit.finalStateVec.y, kFit.finalStateVec.tx, kFit.finalStateVec.ty); + fn.set(kFZRef.finalStateVec.x, kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); //set the track parameters if the filter does not fail - TrackArray1.set_P(1./Math.abs(kFit.finalStateVec.Q)); - TrackArray1.set_Q((int)Math.signum(kFit.finalStateVec.Q)); - trkcandFinder.setTrackPars(TrackArray1, new Trajectory(), trjFind, fn, kFit.finalStateVec.z, Constants.getInstance().dcDetector, dcSwim, beamXoffset, beamYoffset); + TrackArray1.set_P(1./Math.abs(kFZRef.finalStateVec.Q)); + TrackArray1.set_Q((int)Math.signum(kFZRef.finalStateVec.Q)); + + trkcandFinder.setTrackPars(TrackArray1, new Trajectory(), trjFind, fn, kFZRef.finalStateVec.z, Constants.getInstance().dcDetector, dcSwim, beamXoffset, beamYoffset); // candidate parameters are set from the state vector if (TrackArray1.fit_Successful == false) { continue; - } - TrackArray1.set_FitChi2(kFit.chi2); - TrackArray1.set_FitNDF(kFit.NDF); - TrackArray1.setStateVecs(kFit.kfStateVecsAlongTrajectory); - TrackArray1.set_FitConvergenceStatus(kFit.ConvStatus); - //TrackArray[i].set_Id(TrackArray[i].size()+1); - //TrackArray[i].set_CovMat(kFit.finalCovMat.covMat); + } + + TrackArray1.set_FitChi2(kFZRef.chi2); + TrackArray1.set_FitNDF(kFZRef.NDF); + TrackArray1.setStateVecs(kfStateVecsAlongTrajectory); + TrackArray1.set_FitConvergenceStatus(kFZRef.ConvStatus); if (TrackArray1.get_Vtx0().toVector3D().mag() > 500) { continue; } + // get CovMat at vertex Point3D VTCS = crosses.get(0).getCoordsInSector(TrackArray1.get_Vtx0().x(), TrackArray1.get_Vtx0().y(), TrackArray1.get_Vtx0().z()); - TrackArray1.set_CovMat(kFit.propagateToVtx(crosses.get(0).get_Sector(), VTCS.z())); + TrackArray1.set_CovMat(kFZRef.propagateToVtx(crosses.get(0).get_Sector(), VTCS.z())); + if (TrackArray1.isGood()) { trkcands.add(TrackArray1); } - } - } - + } + + } + if(!trkcands.isEmpty()) { - //trkcandFinder.removeOverlappingTracks(trkcands); // remove overlaps + //trkcandFinder.removeOverlappingTracks(trkcands); // remove overlaps for(Track trk: trkcands) { int trkId = trk.get_Id(); // reset the id @@ -310,8 +315,247 @@ public boolean processDataEvent(DataEvent event) { return true; } + public List setKFStateVecsAlongTrajectory(KFitter kFZRef) { + List kfStateVecsAlongTrajectory = new ArrayList<>(); + + for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + sv.setZ(svc.z); + sv.setB(svc.B); + sv.setPathLength(svc.getPathLength()); + kfStateVecsAlongTrajectory.add(sv); + } + + return kfStateVecsAlongTrajectory; + } + + public void getInitState(Track trkcand, double z0, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initStateVec, KFitter kf, Swim dcSwim, float[] bf) { + if (trkcand != null && trkcand.getFinalStateVec()!=null ) { + initStateVec.x = trkcand.getFinalStateVec().x(); + initStateVec.y = trkcand.getFinalStateVec().y(); + initStateVec.z = trkcand.getFinalStateVec().getZ(); + initStateVec.tx = trkcand.getFinalStateVec().tanThetaX(); + initStateVec.ty = trkcand.getFinalStateVec().tanThetaY(); + initStateVec.Q = ((double) trkcand.get_Q())/trkcand.get_P(); + + RungeKuttaDoca rk = new RungeKuttaDoca(); + rk.SwimToZ(trkcand.get(0).get_Sector(), initStateVec, dcSwim, z0, bf); + + //initCM.covMat = trkcand.get_CovMat(); + //test + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec rinitSV = svs.new StateVec(0); + rinitSV.x = trkcand.getFinalStateVec().x(); + rinitSV.y = trkcand.getFinalStateVec().y(); + rinitSV.z = trkcand.getFinalStateVec().getZ(); + rinitSV.tx = trkcand.getFinalStateVec().tanThetaX(); + rinitSV.ty = trkcand.getFinalStateVec().tanThetaY(); + rinitSV.Q = ((double) trkcand.get_Q())/trkcand.get_P(); + double[] FTF = new double[25]; + double[] F = this.F(trkcand.get(0).get_Sector(), z0, rinitSV, rk, dcSwim, bf); + for(int i = 0; i<5; i++) { + FTF[i*5+i]=F[i]*F[i]; + + } + + //Matrix initCMatrix = new Matrix(FTF); + Matrix initCMatrix = new Matrix(); + initCMatrix.set(FTF); + initStateVec.CM = initCMatrix; + + } else { + kf.setFitFailed = true; + } + + } + + private org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec reset(org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec sv, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec stateVec) { + StateVecs svs = new StateVecs(); + sv = svs.new StateVec(stateVec.k); + sv.x = stateVec.x; + sv.y = stateVec.y; + sv.tx = stateVec.tx; + sv.ty = stateVec.ty; + sv.z = stateVec.z; + sv.Q = stateVec.Q; + + return sv; + } + private void swimToSite(int sector, double z0, + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec SVplus, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec SVminus, RungeKuttaDoca rk, Swim dcSwim, float[] bf) { + + rk.SwimToZ(sector, SVplus, dcSwim, z0, bf); + rk.SwimToZ(sector, SVminus, dcSwim, z0, bf); + } + + double[] F(int sector, double z0, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec stateVec, RungeKuttaDoca rk, Swim dcSwim, float[] bf) { + double[] _F = new double[5]; + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec SVplus = null; + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec SVminus = null; + + SVplus = this.reset(SVplus, stateVec); + SVminus = this.reset(SVminus, stateVec); + + double delt_x = 0.05; + SVplus.x += delt_x/2.; + SVminus.x-= delt_x/2.; + + this.swimToSite(sector, z0, SVplus, SVminus, rk, dcSwim, bf); + + _F[0] = (SVplus.x - SVminus.x)/delt_x; + + SVplus = this.reset(SVplus, stateVec); + SVminus = this.reset(SVminus, stateVec); + + double delt_y = 0.05; + SVplus.y += delt_y/2.; + SVminus.y-= delt_y/2.; + + this.swimToSite(sector, z0, SVplus, SVminus, rk, dcSwim, bf); + + _F[1] = (SVplus.y - SVminus.y)/delt_y; + + SVplus = this.reset(SVplus, stateVec); + SVminus = this.reset(SVminus, stateVec); + + double delt_tx = 0.001; + SVplus.tx += delt_tx/2.; + SVminus.tx-= delt_tx/2.; + + this.swimToSite(sector, z0, SVplus, SVminus, rk, dcSwim, bf); + + _F[2] = (SVplus.tx - SVminus.tx)/delt_tx; + + SVplus = this.reset(SVplus, stateVec); + SVminus = this.reset(SVminus, stateVec); + + double delt_ty = 0.001; + SVplus.ty += delt_ty/2.; + SVminus.ty-= delt_ty/2.; + + this.swimToSite(sector, z0, SVplus, SVminus, rk, dcSwim, bf); + + _F[3] = (SVplus.ty - SVminus.ty)/delt_ty; + + SVplus = this.reset(SVplus, stateVec); + SVminus = this.reset(SVminus, stateVec); + + + _F[4] = 0.01/Math.abs(SVplus.Q); + + return _F; + + } + + public List getMeasSurfaces(Track trk, DCGeant4Factory DcDetector) { + List hOTS = new ArrayList<>(); // the list of hits on track + FittedHit hitOnTrk; + for(int s = 0; s < trk.get_ListOfHBSegments().size(); s++) { + for(int h = 0; h < trk.get_ListOfHBSegments().get(s).size(); h++) { + trk.get_ListOfHBSegments().get(s).get(h).calc_CellSize(DcDetector); + hitOnTrk = trk.get_ListOfHBSegments().get(s).get(h); + int slayr = trk.get_ListOfHBSegments().get(s).get(h).get_Superlayer(); + + double sl1 = trk.get_ListOfHBSegments().get(s).get_fittedCluster().get_clusterLineFitSlope(); + double it1 = trk.get_ListOfHBSegments().get(s).get_fittedCluster().get_clusterLineFitIntercept(); + + double Z = hitOnTrk.get_Z(); + double X = sl1 * Z + it1; + HitOnTrack hot = new HitOnTrack(slayr, X, Z, + hitOnTrk.get_WireMaxSag(), + hitOnTrk.get_WireLine()); + + hot._doca[0] = trk.get_ListOfHBSegments().get(s).get(h).get_Doca(); + + double LR = Math.signum(trk.get_ListOfHBSegments().get(s).get(h).get_XWire()-trk.get_ListOfHBSegments().get(s).get(h).get_X()); + hot._doca[0]*=LR; + hot._hitError = trk.get_ListOfHBSegments().get(s).get(h).get_DocaErr()*trk.get_ListOfHBSegments().get(s).get(h).get_DocaErr(); + //LOGGER.log(Level.FINE, " Z "+Z+" ferr "+(float)(hot._Unc /(hot._hitError/4.))); + hot._Unc[0] = hot._hitError; + hot.region = trk.get_ListOfHBSegments().get(s).get(h).get_Region(); + hot.sector = trk.get_ListOfHBSegments().get(s).get(h).get_Sector(); + hot.superlayer = trk.get_ListOfHBSegments().get(s).get(h).get_Superlayer(); + hot.layer = trk.get_ListOfHBSegments().get(s).get(h).get_Layer(); + hOTS.add(hot); + } + } + + Collections.sort(hOTS); // sort the collection in order of increasing Z value (i.e. going downstream from the target) + // identify double hits and take the average position + for (int i = 0; i < hOTS.size(); i++) { + if (i > 0) { + if (Math.abs(hOTS.get(i - 1)._Z - hOTS.get(i)._Z)<0.01) { + hOTS.get(i - 1)._doca[1] = hOTS.get(i)._doca[0]; + hOTS.get(i - 1)._Unc[1] = hOTS.get(i)._Unc[0]; + hOTS.get(i - 1)._wireLine[1] = hOTS.get(i)._wireLine[0]; + hOTS.remove(i); + hOTS.get(i - 1).nMeas = 2; + } + } + } + + List surfaces = new ArrayList<>(hOTS.size()); + + for (int i = 0; i < hOTS.size(); i++) { + Surface surf = new Surface(hOTS.get(i).region, hOTS.get(i)._Z, hOTS.get(i)._X, hOTS.get(i)._tilt, hOTS.get(i)._wireMaxSag, + hOTS.get(i)._doca, hOTS.get(i)._Unc, hOTS.get(i)._hitError, hOTS.get(i)._wireLine); + surf.setSector(hOTS.get(i).sector); + surf.setSuperLayer(hOTS.get(i).superlayer); + surf.setLayer(hOTS.get(i).layer); + surf.setNMeas(hOTS.get(i).nMeas); + surfaces.add(i, surf); + } + + return surfaces; + } + public class HitOnTrack implements Comparable { + + public double _hitError; + public double _X; + public double _Z; + public double[] _Unc = new double[2]; + public double _tilt; + public double[] _doca = new double[2]; + public double _wireMaxSag; + public Line3D[] _wireLine = new Line3D[2]; + public int region; + public int superlayer; + public int sector; + public int layer; + public int nMeas = 1; + + + public HitOnTrack(int superlayer, double X, double Z, double wiremaxsag, Line3D wireLine) { + _X = X; + _Z = Z; + _wireMaxSag = wiremaxsag; + _wireLine[0] = wireLine; + _doca[0] = -99; + _doca[1] = -99; + _Unc[0] = 1; + _Unc[1] = 1; + //use stereo angle in fit based on wire direction + _tilt = 90-Math.toDegrees(wireLine.direction().asUnit().angle(new Vector3D(1,0,0))); + } + + @Override + public int compareTo(HitOnTrack o) { + if (this._Z == o._Z) { + return 0; + } + if (this._Z > o._Z) { + return 1; + } else { + return -1; + } + } + } + + private int get_Status(Track track) { int miss = 0; From c95ea472dac201f9b26b5941d010cace7a49ebad Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Tue, 17 Jan 2023 14:49:28 -0500 Subject: [PATCH 02/69] small update for KFitter::calcFinalChisq() --- .../org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index a11bcbeb7..2e3c4b586 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -425,7 +425,7 @@ private void calcFinalChisq(int sector, boolean nofilter) { StateVec sVec; // To be changed: to match wit the old package, we make the following codes. Could be changed when other codes for application of calcFinalChisq are changed. - if(nofilter || sv.trackTrajF.size() < svzLength) { + if(nofilter || (sv.trackTrajF.get(k) == null)) { sVec = sv.trackTrajT.get(k); } else { From a1f0dc47f172378d960fbdc3bfa90f432a832c6b Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 3 Feb 2023 12:13:58 -0500 Subject: [PATCH 03/69] adjust code format and add author for source files --- .../clas/tracking/kalmanfilter/AMeasVecs.java | 1 + .../tracking/kalmanfilter/AStateVecs.java | 6 +++ .../clas/tracking/kalmanfilter/Surface.java | 1 + .../jlab/clas/tracking/kalmanfilter/Type.java | 1 + .../tracking/utilities/RungeKuttaDoca.java | 1 + .../rec/dc/track/TrackCandListFinder.java | 48 +++++++++---------- .../java/org/jlab/service/dc/DCTBEngine.java | 28 +++++------ 7 files changed, 48 insertions(+), 38 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java index ac77604b5..524af89bc 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java @@ -14,6 +14,7 @@ /** * * @author ziegler + * @author Tongtong Cao */ public abstract class AMeasVecs { diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java index fdfd5275a..655962173 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java @@ -13,6 +13,12 @@ import org.jlab.jnp.matrix.Matrix; import org.jlab.jnp.matrix.Matrix5x5; +/** + * + * @author ziegler + * @author Tongtong Cao + */ + public abstract class AStateVecs { public Units units; diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java index b061aea5d..68f5d17f4 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java @@ -14,6 +14,7 @@ /** * * @author ziegler + * @author Tongtong Cao */ public class Surface implements Comparable { diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java index 76bae7238..3065ec092 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java @@ -3,6 +3,7 @@ /** * * @author ziegler + * @author Tongtong Cao */ public enum Type { UDF(-1), PLANEWITHPOINT(0), PLANEWITHLINE(1), PLANEWITHSTRIP(2), diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/utilities/RungeKuttaDoca.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/utilities/RungeKuttaDoca.java index f4c07624d..1e6d4dd3d 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/utilities/RungeKuttaDoca.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/utilities/RungeKuttaDoca.java @@ -8,6 +8,7 @@ * Swims a given state vector to a given Z position using Runge Kutta 4 transport. * @author ziegler * @author benkel + * @author Tongtong Cao */ public class RungeKuttaDoca { private final float[] _b = new float[3]; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index c2f66a54c..97e86311f 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -909,33 +909,33 @@ private List findStraightTracks(CrossList crossList, DCGeant4Factory DcDe if (kFZRef.finalStateVec == null) { continue; } - - List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); StateVec fn = new StateVec(); - if (!kFZRef.setFitFailed && kFZRef.finalStateVec != null) { + if (!kFZRef.setFitFailed && kFZRef.finalStateVec != null) { fn.set(kFZRef.finalStateVec.x, kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); - fn.setZ(kFZRef.finalStateVec.z); - cand.setFinalStateVec(fn); - - // set the track parameters - cand.set_P(1. / Math.abs(kFZRef.finalStateVec.Q)); - cand.set_Q((int) Math.signum(kFZRef.finalStateVec.Q)); - this.setTrackPars(cand, traj, trjFind, fn, kFZRef.finalStateVec.z, DcDetector, dcSwim); - - if (cand.fit_Successful == true) { - // candidate parameters - cand.set_FitChi2(kFZRef.chi2); - cand.set_FitNDF(kFZRef.NDF); - cand.set_FitConvergenceStatus(kFZRef.ConvStatus); - cand.set_Id(cands.size() + 1); - cand.set_CovMat(kFZRef.finalStateVec.CM); - cand.setStateVecs(kfStateVecsAlongTrajectory); - - // add candidate to list of tracks - cands.add(cand); - } - } + fn.setZ(kFZRef.finalStateVec.z); + cand.setFinalStateVec(fn); + + // set the track parameters + cand.set_P(1. / Math.abs(kFZRef.finalStateVec.Q)); + cand.set_Q((int) Math.signum(kFZRef.finalStateVec.Q)); + this.setTrackPars(cand, traj, trjFind, fn, kFZRef.finalStateVec.z, DcDetector, dcSwim); + + if (cand.fit_Successful == true) { + // candidate parameters + cand.set_FitChi2(kFZRef.chi2); + cand.set_FitNDF(kFZRef.NDF); + cand.set_FitConvergenceStatus(kFZRef.ConvStatus); + cand.set_Id(cands.size() + 1); + cand.set_CovMat(kFZRef.finalStateVec.CM); + cand.setStateVecs(kfStateVecsAlongTrajectory); + + // add candidate to list of tracks + cands.add(cand); + } + } } } } diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index d5b3c275f..964cad5f1 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -213,9 +213,9 @@ public boolean processDataEvent(DataEvent event) { beamYoffset += raster_bank.getFloat("y", 0); } TrackCandListFinder trkcandFinder = new TrackCandListFinder("TimeBased"); - TrajectoryFinder trjFind = new TrajectoryFinder(); - - for (Track TrackArray1 : TrackArray) { + TrajectoryFinder trjFind = new TrajectoryFinder(); + + for (Track TrackArray1 : TrackArray) { if (TrackArray1 == null || TrackArray1.get_ListOfHBSegments() == null || TrackArray1.get_ListOfHBSegments().size() < 5) { continue; } @@ -225,17 +225,17 @@ public boolean processDataEvent(DataEvent event) { continue; } crosses.addAll(TrackArray1); - - KFitter kFZRef = new KFitter(true, 30, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); - List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); - StateVecs svs = new StateVecs(); - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); - kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); - kFZRef.runFitter(); - List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); - - StateVec fn = new StateVec(); + + KFitter kFZRef = new KFitter(true, 30, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitter(); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + StateVec fn = new StateVec(); if (kFZRef.setFitFailed==false && kFZRef.finalStateVec!=null) { // set the state vector at the last measurement site fn.set(kFZRef.finalStateVec.x, kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); From 0eeb540112d17deaf777e4a65f655a5692cd5ca1 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 3 Feb 2023 12:22:12 -0500 Subject: [PATCH 04/69] further adjust code format --- .../kalmanfilter/zReference/KFitter.java | 911 +++++++++--------- .../kalmanfilter/zReference/MeasVecs.java | 26 +- .../kalmanfilter/zReference/StateVecs.java | 291 +++--- 3 files changed, 613 insertions(+), 615 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 2e3c4b586..ae8421956 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -21,547 +21,536 @@ * * @author Tongtong Cao */ - public class KFitter extends AKFitter { - private StateVecs sv = new StateVecs(); - private MeasVecs mv = new MeasVecs(); - private StateVec finalSmoothedStateVec = null; - private StateVec finalTransportedStateVec = null; - - public StateVec finalStateVec = null; - public StateVec initialStateVec = null; - public List kfStateVecsAlongTrajectory; - - private int interNum; - - private double chi2kf = 0; - private double KFScale = 4; - - private int svzLength; - - public int ConvStatus = 1; - - private double Z[]; - - private boolean stopIteration = false; - - private boolean TBT =false; - - Matrix first_inverse = new Matrix(); - Matrix addition = new Matrix(); - Matrix result = new Matrix(); - Matrix result_inv = new Matrix(); - Matrix adj = new Matrix(); - - public KFitter(boolean filter, int iterations, int dir, Swim swim, double Z[], Libr mo) { - super(filter, iterations, dir, swim, mo); - this.Z = Z; - } - - public final void init(List measSurfaces, StateVec initSV) { - finalSmoothedStateVec = null; - finalTransportedStateVec = null; - this.NDF0 = -5; - this.NDF = -5; - this.chi2 = Double.POSITIVE_INFINITY; - this.numIter = 0; - this.setFitFailed = false; - mv.setMeasVecs(measSurfaces); - for (int i = 0; i < mv.measurements.size(); i++) { - if (mv.measurements.get(i).skip == false) { - this.NDF+= mv.measurements.get(i).surface.getNMeas(); - } - } - - sv.init(initSV); - sv.Z = Z; - } - - public final void initFromHB(List measSurfaces, StateVec initSV, double beta) { - finalSmoothedStateVec = null; - finalTransportedStateVec = null; - this.NDF0 = -5; - this.NDF = -5; - this.chi2 = Double.POSITIVE_INFINITY; - this.numIter = 0; - this.setFitFailed = false; - mv.setMeasVecs(measSurfaces); - for (int i = 0; i < mv.measurements.size(); i++) { - if (mv.measurements.get(i).skip == false) { - this.NDF+= mv.measurements.get(i).surface.getNMeas(); - } - } - - sv.initFromHB(initSV, beta); - sv.Z = Z; - TBT = true; - } - - public void runFitter() { - this.chi2 = Double.POSITIVE_INFINITY; - double initChi2 = Double.POSITIVE_INFINITY; - // this.NDF = mv.ndf; - this.svzLength = this.mv.measurements.size(); - - int sector = this.mv.measurements.get(0).sector; - - - if(TBT==true) { - this.chi2kf = 0; + private StateVecs sv = new StateVecs(); + private MeasVecs mv = new MeasVecs(); + private StateVec finalSmoothedStateVec = null; + private StateVec finalTransportedStateVec = null; + + public StateVec finalStateVec = null; + public StateVec initialStateVec = null; + public List kfStateVecsAlongTrajectory; + + private int iterNum; + + private double chi2kf = 0; + private double KFScale = 4; + + private int svzLength; + + public int ConvStatus = 1; + + private double Z[]; + + private boolean stopIteration = false; + + private boolean TBT = false; + + Matrix first_inverse = new Matrix(); + Matrix addition = new Matrix(); + Matrix result = new Matrix(); + Matrix result_inv = new Matrix(); + Matrix adj = new Matrix(); + + public KFitter(boolean filter, int iterations, int dir, Swim swim, double Z[], Libr mo) { + super(filter, iterations, dir, swim, mo); + this.Z = Z; + } + + public final void init(List measSurfaces, StateVec initSV) { + finalSmoothedStateVec = null; + finalTransportedStateVec = null; + this.NDF0 = -5; + this.NDF = -5; + this.chi2 = Double.POSITIVE_INFINITY; + this.numIter = 0; + this.setFitFailed = false; + mv.setMeasVecs(measSurfaces); + for (int i = 0; i < mv.measurements.size(); i++) { + if (mv.measurements.get(i).skip == false) { + this.NDF += mv.measurements.get(i).surface.getNMeas(); + } + } + + sv.init(initSV); + sv.Z = Z; + } + + public final void initFromHB(List measSurfaces, StateVec initSV, double beta) { + finalSmoothedStateVec = null; + finalTransportedStateVec = null; + this.NDF0 = -5; + this.NDF = -5; + this.chi2 = Double.POSITIVE_INFINITY; + this.numIter = 0; + this.setFitFailed = false; + mv.setMeasVecs(measSurfaces); + for (int i = 0; i < mv.measurements.size(); i++) { + if (mv.measurements.get(i).skip == false) { + this.NDF += mv.measurements.get(i).surface.getNMeas(); + } + } + + sv.initFromHB(initSV, beta); + sv.Z = Z; + TBT = true; + } + + public void runFitter() { + this.chi2 = Double.POSITIVE_INFINITY; + double initChi2 = Double.POSITIVE_INFINITY; + // this.NDF = mv.ndf; + this.svzLength = this.mv.measurements.size(); + + int sector = this.mv.measurements.get(0).sector; + + if (TBT == true) { + this.chi2kf = 0; // Get the input parameters - for (int k = 0; k < svzLength - 1; k++) { - sv.transport(sector, k, k + 1, this.sv.trackTrajT.get(k), mv, this.getSwimmer(), true); + for (int k = 0; k < svzLength - 1; k++) { + sv.transport(sector, k, k + 1, this.sv.trackTrajT.get(k), mv, this.getSwimmer(), true); } this.calcFinalChisq(sector, true); this.initialStateVec = sv.trackTrajT.get(svzLength - 1); this.finalStateVec = sv.trackTrajT.get(svzLength - 1); - initChi2 = this.chi2; - if(Double.isNaN(chi2)) { + initChi2 = this.chi2; + if (Double.isNaN(chi2)) { this.setFitFailed = true; return; } - } - - for (int i = 1; i <= totNumIter; i++) { - interNum = i; - this.chi2kf = 0; - - if (i > 1) { - - for (int k = svzLength - 1; k >0; k--) { - boolean forward = false; - if(k>=2) { - - // Not backward transport and filter states for the last measurement layer - if(k == svzLength - 1) { - if(!sv.transport(sector, k, k - 2, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { - this.stopIteration = true; - break; - } - } - else { - if(!sv.transport(sector, k, k - 2, this.sv.trackTrajB.get(k), mv, this.getSwimmer(), forward)){ - this.stopIteration = true; - break; - } - } - - if(!this.filter(k - 2, forward)) { - this.stopIteration = true; - break; - } - - if(!sv.transport(sector, k - 2, k - 1, this.sv.trackTrajB.get(k-2), mv, this.getSwimmer(), forward)) { - this.stopIteration = true; - break; - } - - if(!this.filter(k - 1, forward)) { - this.stopIteration = true; - break; + } + + for (int i = 1; i <= totNumIter; i++) { + iterNum = i; + this.chi2kf = 0; + + if (i > 1) { + + for (int k = svzLength - 1; k > 0; k--) { + boolean forward = false; + if (k >= 2) { + + // Not backward transport and filter states for the last measurement layer + if (k == svzLength - 1) { + if (!sv.transport(sector, k, k - 2, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } else { + if (!sv.transport(sector, k, k - 2, this.sv.trackTrajB.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + + if (!this.filter(k - 2, forward)) { + this.stopIteration = true; + break; + } + + if (!sv.transport(sector, k - 2, k - 1, this.sv.trackTrajB.get(k - 2), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; } - } - else { - if(!sv.transport(sector, 1, 0, this.sv.trackTrajB.get(1), mv, this.getSwimmer(), forward)) { - this.stopIteration = true; - break; + + if (!this.filter(k - 1, forward)) { + this.stopIteration = true; + break; } - - if(!this.filter(0, forward)) { - this.stopIteration = true; - break; - } + } else { + if (!sv.transport(sector, 1, 0, this.sv.trackTrajB.get(1), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + if (!this.filter(0, forward)) { + this.stopIteration = true; + break; + } + } + } + } + + if (this.stopIteration) { + break; + } + + for (int k = 0; k < svzLength - 1; k++) { + boolean forward = true; + + if (iterNum == 1 && (k == 0)) { + if (TBT == true) { + this.sv.transported(true).put(0, this.sv.transported(false).get(0)); // For TBT, calcFinalChisq() is called previously. + } + } + + if (k == 0) { + if (i == 1) { + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajT.get(0), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } else { + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajB.get(0), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + } else { + if (!this.sv.transport(sector, k, k + 1, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; } - } - } - - if(this.stopIteration) break; - - for (int k = 0; k < svzLength - 1; k++) { - boolean forward = true; - - if (interNum == 1 && (k == 0)) { - if(TBT==true) { - this.sv.transported(true).put(0, this.sv.transported(false).get(0)); // For TBT, calcFinalChisq() is called previously. - } - } - - - if(k == 0) { - if(i == 1) { - if(!this.sv.transport(sector, 0, 1, this.sv.trackTrajT.get(0), mv, this.getSwimmer(), forward)) { - this.stopIteration = true; - break; - } - } - else { - if(!this.sv.transport(sector, 0, 1, this.sv.trackTrajB.get(0), mv, this.getSwimmer(), forward)) { - this.stopIteration = true; - break; - } - } - } - else { - if(!this.sv.transport(sector, k, k + 1, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { - this.stopIteration = true; - break; - } - - } - - if(!this.filter(k + 1, forward)) { - this.stopIteration = true; - break; - } - } - - if(this.stopIteration) break; - - + + } + + if (!this.filter(k + 1, forward)) { + this.stopIteration = true; + break; + } + } + + if (this.stopIteration) { + break; + } + if (i > 1) { - if(this.setFitFailed==true) + if (this.setFitFailed == true) { i = totNumIter; - if (this.setFitFailed==false) { - if(this.finalStateVec!=null) { - if(Math.abs(sv.trackTrajF.get(svzLength - 1).Q-this.finalStateVec.Q)<5.e-4 && - Math.abs(sv.trackTrajF.get(svzLength - 1).x-this.finalStateVec.x)<1.e-4 && - Math.abs(sv.trackTrajF.get(svzLength - 1).y-this.finalStateVec.y)<1.e-4 && - Math.abs(sv.trackTrajF.get(svzLength - 1).tx-this.finalStateVec.tx)<1.e-6 && - Math.abs(sv.trackTrajF.get(svzLength - 1).ty-this.finalStateVec.ty)<1.e-6) { + } + if (this.setFitFailed == false) { + if (this.finalStateVec != null) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 5.e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 1.e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 1.e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 1.e-6 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.e-6) { i = totNumIter; } } this.finalStateVec = sv.trackTrajF.get(svzLength - 1); - + } else { this.ConvStatus = 1; // Should be 0??? } } - - - } - - - if(totNumIter==1) { - if(this.setFitFailed==false && this.stopIteration == false) - this.finalStateVec = sv.trackTrajF.get(svzLength - 1); + + } + + if (totNumIter == 1) { + if (this.setFitFailed == false && this.stopIteration == false) { + this.finalStateVec = sv.trackTrajF.get(svzLength - 1); + } } - - this.calcFinalChisq(sector); - + this.calcFinalChisq(sector); - if(Double.isNaN(chi2)) + if (Double.isNaN(chi2)) { this.setFitFailed = true; - + } - if(TBT==true) { - if(chi2>initChi2) { // fit failed + if (TBT == true) { + if (chi2 > initChi2) { // fit failed this.finalStateVec = this.initialStateVec; - sv.trackTrajT.put(svzLength - 1,this.initialStateVec); + sv.trackTrajT.put(svzLength - 1, this.initialStateVec); this.calcFinalChisq(sector, true); } - } - - } - - private boolean filter(int k, boolean forward) { - StateVec sVec = sv.transported(forward).get(k); - org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec mVec = mv.measurements.get(k); - - if (Double.isNaN(sVec.x) || Double.isNaN(sVec.y) - || Double.isNaN(sVec.tx) || Double.isNaN(sVec.ty) - || Double.isNaN(sVec.Q)) { - this.setFitFailed = true; - return false; - } - if (sVec != null && sVec.CM != null - && k < mv.measurements.size() && mVec.skip == false) { - - - double[] K = new double[5]; - double V = mVec.surface.unc[0] * KFScale; - double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[0]); - Matrix CaInv = this.filterCovMat(H, sVec.CM, V); - Matrix cMat = new Matrix(); - if (CaInv != null) { - Matrix5x5.copy(CaInv, cMat); - } else { - return false; - } - - for (int j = 0; j < 5; j++) { - // the gain matrix - K[j] = (H[0] * cMat.get(j, 0) - + H[1] * cMat.get(j, 1)) / V; - } - - Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); - double h = mv.hDoca(point, mVec.surface.wireLine[0]); - - double signMeas = 1; - double sign = 1; - if (mVec.surface.doca[1] != -99 - || !(Math.abs(mVec.surface.doca[0]) < 0.5 - && mVec.surface.doca[1] == -99)) { // use LR only for double hits && large - // enough docas - signMeas = Math.signum(mVec.surface.doca[0]); - sign = Math.signum(h); - } else { - signMeas = Math.signum(h); - sign = Math.signum(h); - } - - double c2 = ((signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)) - * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)) / V); - - double x_filt = sVec.x - + K[0] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); - double y_filt = sVec.y - + K[1] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); - double tx_filt = sVec.tx - + K[2] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); - double ty_filt = sVec.ty - + K[3] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); - double Q_filt = sVec.Q - + K[4] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); - - // USE THE DOUBLE HIT - if (mVec.surface.doca[1] != -99) { - // now filter using the other Hit - V = mVec.surface.unc[1] * KFScale; - H = mv.H(x_filt, y_filt, mVec.surface.z, - mVec.surface.wireLine[1]); - CaInv = this.filterCovMat(H, cMat, V); - if (CaInv != null) { - for (int i = 0; i < 5; i++) { - Matrix5x5.copy(CaInv, cMat); - } - } else { - return false; - } - for (int j = 0; j < 5; j++) { - // the gain matrix - K[j] = (H[0] * cMat.get(j, 0) - + H[1] * cMat.get(j, 1)) / V; - } - - Point3D point2 = new Point3D(x_filt, y_filt, mVec.surface.z); - - h = mv.hDoca(point2, mVec.surface.wireLine[1]); - - signMeas = Math.signum(mVec.surface.doca[1]); - sign = Math.signum(h); - - x_filt += K[0] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); - y_filt += K[1] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); - tx_filt += K[2] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); - ty_filt += K[3] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); - Q_filt += K[4] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); - - c2 += ((signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)) - * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)) / V); - } - - chi2kf += c2; - if (filterOn) { - StateVec filteredVec = sv.new StateVec(k); - filteredVec.x = x_filt; - filteredVec.y = y_filt; - filteredVec.tx = tx_filt; - filteredVec.ty = ty_filt; - filteredVec.Q = Q_filt; - filteredVec.z = sVec.z; - filteredVec.B = sVec.B; - filteredVec.deltaPath = sVec.deltaPath; - - filteredVec.CM = cMat; - - sv.filtered(forward).put(k, filteredVec); - } - else { - return false; - } - - return true; - } - else { - return false; - } - } - + } + + } + + private boolean filter(int k, boolean forward) { + StateVec sVec = sv.transported(forward).get(k); + org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec mVec = mv.measurements.get(k); + + if (Double.isNaN(sVec.x) || Double.isNaN(sVec.y) + || Double.isNaN(sVec.tx) || Double.isNaN(sVec.ty) + || Double.isNaN(sVec.Q)) { + this.setFitFailed = true; + return false; + } + if (sVec != null && sVec.CM != null + && k < mv.measurements.size() && mVec.skip == false) { + + double[] K = new double[5]; + double V = mVec.surface.unc[0] * KFScale; + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[0]); + Matrix CaInv = this.filterCovMat(H, sVec.CM, V); + Matrix cMat = new Matrix(); + if (CaInv != null) { + Matrix5x5.copy(CaInv, cMat); + } else { + return false; + } + + for (int j = 0; j < 5; j++) { + // the gain matrix + K[j] = (H[0] * cMat.get(j, 0) + + H[1] * cMat.get(j, 1)) / V; + } + + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + double h = mv.hDoca(point, mVec.surface.wireLine[0]); + + double signMeas = 1; + double sign = 1; + if (mVec.surface.doca[1] != -99 + || !(Math.abs(mVec.surface.doca[0]) < 0.5 + && mVec.surface.doca[1] == -99)) { // use LR only for double hits && large + // enough docas + signMeas = Math.signum(mVec.surface.doca[0]); + sign = Math.signum(h); + } else { + signMeas = Math.signum(h); + sign = Math.signum(h); + } + + double c2 = ((signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)) + * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)) / V); + + double x_filt = sVec.x + + K[0] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double y_filt = sVec.y + + K[1] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double tx_filt = sVec.tx + + K[2] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double ty_filt = sVec.ty + + K[3] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double Q_filt = sVec.Q + + K[4] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + + // USE THE DOUBLE HIT + if (mVec.surface.doca[1] != -99) { + // now filter using the other Hit + V = mVec.surface.unc[1] * KFScale; + H = mv.H(x_filt, y_filt, mVec.surface.z, + mVec.surface.wireLine[1]); + CaInv = this.filterCovMat(H, cMat, V); + if (CaInv != null) { + for (int i = 0; i < 5; i++) { + Matrix5x5.copy(CaInv, cMat); + } + } else { + return false; + } + for (int j = 0; j < 5; j++) { + // the gain matrix + K[j] = (H[0] * cMat.get(j, 0) + + H[1] * cMat.get(j, 1)) / V; + } + + Point3D point2 = new Point3D(x_filt, y_filt, mVec.surface.z); + + h = mv.hDoca(point2, mVec.surface.wireLine[1]); + + signMeas = Math.signum(mVec.surface.doca[1]); + sign = Math.signum(h); + + x_filt += K[0] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + y_filt += K[1] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + tx_filt += K[2] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + ty_filt += K[3] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + Q_filt += K[4] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + + c2 += ((signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)) + * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)) / V); + } + + chi2kf += c2; + if (filterOn) { + StateVec filteredVec = sv.new StateVec(k); + filteredVec.x = x_filt; + filteredVec.y = y_filt; + filteredVec.tx = tx_filt; + filteredVec.ty = ty_filt; + filteredVec.Q = Q_filt; + filteredVec.z = sVec.z; + filteredVec.B = sVec.B; + filteredVec.deltaPath = sVec.deltaPath; + + filteredVec.CM = cMat; + + sv.filtered(forward).put(k, filteredVec); + } else { + return false; + } + + return true; + } else { + return false; + } + } + public Matrix filterCovMat(double[] H, Matrix Ci, double V) { - + double det = Matrix5x5.inverse(Ci, first_inverse, adj); - if(Math.abs(det)<1.e-30) - return null; - + if (Math.abs(det) < 1.e-30) { + return null; + } + addition.set( - H[0] * H[0] / V, H[0] * H[1] / V, 0, 0, 0, - H[0] * H[1] / V, H[1] * H[1] / V, 0, 0, 0, - 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0); - + H[0] * H[0] / V, H[0] * H[1] / V, 0, 0, 0, + H[0] * H[1] / V, H[1] * H[1] / V, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0); + Matrix5x5.add(first_inverse, addition, result); double det2 = Matrix5x5.inverse(result, result_inv, adj); - if(Math.abs(det2)<1.e-30) - return null; - + if (Math.abs(det2) < 1.e-30) { + return null; + } + return result_inv; } - - private void calcFinalChisq(int sector) { - calcFinalChisq(sector, false); - } - - private void calcFinalChisq(int sector, boolean nofilter) { + + private void calcFinalChisq(int sector) { + calcFinalChisq(sector, false); + } + + private void calcFinalChisq(int sector, boolean nofilter) { int k = svzLength - 1; this.chi2 = 0; double path = 0; double[] nRj = new double[3]; StateVec sVec; - + // To be changed: to match wit the old package, we make the following codes. Could be changed when other codes for application of calcFinalChisq are changed. - if(nofilter || (sv.trackTrajF.get(k) == null)) { - sVec = sv.trackTrajT.get(k); + if (nofilter || (sv.trackTrajF.get(k) == null)) { + sVec = sv.trackTrajT.get(k); + } else { + sVec = sv.trackTrajF.get(k); } - else { - sVec = sv.trackTrajF.get(k); - } - - + kfStateVecsAlongTrajectory = new ArrayList<>(); if (sVec != null && sVec.CM != null) { - - boolean forward = false; - sv.transport(sector, k, 0, sVec, mv, this.getSwimmer(), forward); - - StateVec svc = sv.transported(forward).get(0); - path += svc.deltaPath; - svc.setPathLength(path); - + + boolean forward = false; + sv.transport(sector, k, 0, sVec, mv, this.getSwimmer(), forward); + + StateVec svc = sv.transported(forward).get(0); + path += svc.deltaPath; + svc.setPathLength(path); + double V0 = mv.measurements.get(0).surface.unc[0]; - - Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); + + Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); double h0 = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); - + svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); svc.setProjectorDoca(h0); - kfStateVecsAlongTrajectory.add(svc); + kfStateVecsAlongTrajectory.add(svc); double res = (mv.measurements.get(0).surface.doca[0] - h0); - chi2 += (mv.measurements.get(0).surface.doca[0] - h0) * (mv.measurements.get(0).surface.doca[0] - h0) / V0; - nRj[mv.measurements.get(0).region-1]+=res*res/mv.measurements.get(0).error; + chi2 += (mv.measurements.get(0).surface.doca[0] - h0) * (mv.measurements.get(0).surface.doca[0] - h0) / V0; + nRj[mv.measurements.get(0).region - 1] += res * res / mv.measurements.get(0).error; //USE THE DOUBLE HIT - if(mv.measurements.get(0).surface.doca[1]!=-99) { + if (mv.measurements.get(0).surface.doca[1] != -99) { V0 = mv.measurements.get(0).surface.unc[1]; h0 = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[1]); res = (mv.measurements.get(0).surface.doca[1] - h0); chi2 += (mv.measurements.get(0).surface.doca[1] - h0) * (mv.measurements.get(0).surface.doca[1] - h0) / V0; - nRj[mv.measurements.get(0).region-1]+=res*res/mv.measurements.get(0).error; + nRj[mv.measurements.get(0).region - 1] += res * res / mv.measurements.get(0).error; svc.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); svc.setProjectorDoca(h0); - kfStateVecsAlongTrajectory.add(svc); + kfStateVecsAlongTrajectory.add(svc); } - + forward = true; for (int k1 = 0; k1 < k; k1++) { - if(k1 == 0) { - sv.transport(sector, k1, k1 + 1, svc, mv, this.getSwimmer(), forward); - } - else { - sv.transport(sector, k1, k1 + 1, sv.transported(forward).get(k1), mv, this.getSwimmer(), forward); - } + if (k1 == 0) { + sv.transport(sector, k1, k1 + 1, svc, mv, this.getSwimmer(), forward); + } else { + sv.transport(sector, k1, k1 + 1, sv.transported(forward).get(k1), mv, this.getSwimmer(), forward); + } double V = mv.measurements.get(k1 + 1).surface.unc[0]; - - point = new Point3D(sv.transported(forward).get(k1+1).x, sv.transported(forward).get(k1+1).y, mv.measurements.get(k1+1).surface.z); - + + point = new Point3D(sv.transported(forward).get(k1 + 1).x, sv.transported(forward).get(k1 + 1).y, mv.measurements.get(k1 + 1).surface.z); + double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); - svc = sv.transported(forward).get(k1+1); + svc = sv.transported(forward).get(k1 + 1); path += svc.deltaPath; svc.setPathLength(path); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); svc.setProjectorDoca(h); - kfStateVecsAlongTrajectory.add(svc); - res = (mv.measurements.get(k1 + 1).surface.doca[0] - h); - chi2 += (mv.measurements.get(k1 + 1).surface.doca[0] - h) * (mv.measurements.get(k1 + 1).surface.doca[0] - h) / V; - nRj[mv.measurements.get(k1 + 1).region-1]+=res*res/V; + kfStateVecsAlongTrajectory.add(svc); + res = (mv.measurements.get(k1 + 1).surface.doca[0] - h); + chi2 += (mv.measurements.get(k1 + 1).surface.doca[0] - h) * (mv.measurements.get(k1 + 1).surface.doca[0] - h) / V; + nRj[mv.measurements.get(k1 + 1).region - 1] += res * res / V; //USE THE DOUBLE HIT - if(mv.measurements.get(k1 + 1).surface.doca[1]!=-99) { + if (mv.measurements.get(k1 + 1).surface.doca[1] != -99) { V = mv.measurements.get(k1 + 1).surface.unc[1]; h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[1]); - res = (mv.measurements.get(k1 + 1).surface.doca[1] - h); - chi2 += (mv.measurements.get(k1 + 1).surface.doca[1] - h) * (mv.measurements.get(k1 + 1).surface.doca[1] - h) / V; - nRj[mv.measurements.get(k1 + 1).region-1]+=res*res/V; + res = (mv.measurements.get(k1 + 1).surface.doca[1] - h); + chi2 += (mv.measurements.get(k1 + 1).surface.doca[1] - h) * (mv.measurements.get(k1 + 1).surface.doca[1] - h) / V; + nRj[mv.measurements.get(k1 + 1).region - 1] += res * res / V; svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); svc.setProjectorDoca(h); - kfStateVecsAlongTrajectory.add(svc); + kfStateVecsAlongTrajectory.add(svc); } - } - } - + } + } + } - - + public Matrix propagateToVtx(int sector, double Zf) { - return sv.transport(sector, 0, Zf, sv.trackTrajP.get(0), mv, this.getSwimmer()); + return sv.transport(sector, 0, Zf, sv.trackTrajP.get(0), mv, this.getSwimmer()); + } + + @Override + public void runFitter(AStateVecs sv, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public StateVec filter(int k, StateVec vec, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public StateVec smooth(int k, AStateVecs sv, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public StateVec smooth(StateVec v1, StateVec v2) { + throw new UnsupportedOperationException("Not supported yet."); } - - - @Override - public void runFitter(AStateVecs sv, AMeasVecs mv) { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public StateVec filter(int k, StateVec vec, AMeasVecs mv) { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public StateVec smooth(int k, AStateVecs sv, AMeasVecs mv) { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public StateVec smooth(StateVec v1, StateVec v2) { - throw new UnsupportedOperationException("Not supported yet."); - } - - public MeasVecs getMeasVecs() { - return mv; - } - - public StateVecs getStateVecs() { - return sv; - } - + + public MeasVecs getMeasVecs() { + return mv; + } + + public StateVecs getStateVecs() { + return sv; + } + public void printlnMeasVecs() { - for (int i = 0; i < mv.measurements.size(); i++) { - org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec measvec = mv.measurements.get(i); - String s = String.format("k=%d region=%d superlayer=%d layer=%d error=%.4f", measvec.k, measvec.region, measvec.superlayer, - measvec.layer, measvec.error); - s += String.format(" Surface: index=%d x=%.4f z=%.4f tilt=%.4f wireMaxSag=%.4f", measvec.surface.getIndex(), - measvec.surface.x, measvec.surface.z, measvec.surface.tilt, measvec.surface.wireMaxSag, measvec.surface.unc[1]); - s += String.format( - " Surface line 0: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", - measvec.surface.doca[0], measvec.surface.unc[0], measvec.surface.wireLine[0].origin().x(), - measvec.surface.wireLine[0].origin().y(), measvec.surface.wireLine[0].origin().z(), - measvec.surface.wireLine[0].end().x(), measvec.surface.wireLine[0].end().y(), - measvec.surface.wireLine[0].end().z()); - if (measvec.surface.wireLine[1] != null) - s += String.format( - " Surface line 1: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", - measvec.surface.doca[1], measvec.surface.unc[1], measvec.surface.wireLine[1].origin().x(), - measvec.surface.wireLine[1].origin().y(), measvec.surface.wireLine[1].origin().z(), - measvec.surface.wireLine[1].end().x(), measvec.surface.wireLine[1].end().y(), - measvec.surface.wireLine[1].end().z()); - - System.out.println(s); - } + for (int i = 0; i < mv.measurements.size(); i++) { + org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec measvec = mv.measurements.get(i); + String s = String.format("k=%d region=%d superlayer=%d layer=%d error=%.4f", measvec.k, measvec.region, measvec.superlayer, + measvec.layer, measvec.error); + s += String.format(" Surface: index=%d x=%.4f z=%.4f tilt=%.4f wireMaxSag=%.4f", measvec.surface.getIndex(), + measvec.surface.x, measvec.surface.z, measvec.surface.tilt, measvec.surface.wireMaxSag, measvec.surface.unc[1]); + s += String.format( + " Surface line 0: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", + measvec.surface.doca[0], measvec.surface.unc[0], measvec.surface.wireLine[0].origin().x(), + measvec.surface.wireLine[0].origin().y(), measvec.surface.wireLine[0].origin().z(), + measvec.surface.wireLine[0].end().x(), measvec.surface.wireLine[0].end().y(), + measvec.surface.wireLine[0].end().z()); + if (measvec.surface.wireLine[1] != null) { + s += String.format( + " Surface line 1: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", + measvec.surface.doca[1], measvec.surface.unc[1], measvec.surface.wireLine[1].origin().x(), + measvec.surface.wireLine[1].origin().y(), measvec.surface.wireLine[1].origin().z(), + measvec.surface.wireLine[1].end().x(), measvec.surface.wireLine[1].end().y(), + measvec.surface.wireLine[1].end().z()); + } + + System.out.println(s); + } } } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/MeasVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/MeasVecs.java index 8415b5139..24e4d87c8 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/MeasVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/MeasVecs.java @@ -16,31 +16,31 @@ * @author tongtong cao */ public class MeasVecs extends AMeasVecs { - + public double[] H(double x, double y, double z, Line3D wireLine) { - double[] hMatrix = new double[5]; + double[] hMatrix = new double[5]; double Err = 0.025; double[][] Result = new double[2][2]; - for(int i = 0; i < 2; i++) { - Point3D point = new Point3D(x + (double)Math.pow(-1, i) * Err, y, z); + for (int i = 0; i < 2; i++) { + Point3D point = new Point3D(x + (double) Math.pow(-1, i) * Err, y, z); Result[i][0] = hDoca(point, wireLine); } - for(int i = 0; i < 2; i++) { - Point3D point = new Point3D(x, y + (double)Math.pow(-1, i) * Err, z); + for (int i = 0; i < 2; i++) { + Point3D point = new Point3D(x, y + (double) Math.pow(-1, i) * Err, z); Result[i][1] = hDoca(point, wireLine); } - - hMatrix[0] = (Result[0][0]-Result[1][0])/(2.*Err); - hMatrix[1] = (Result[0][1]-Result[1][1])/(2.*Err); + + hMatrix[0] = (Result[0][0] - Result[1][0]) / (2. * Err); + hMatrix[1] = (Result[0][1] - Result[1][1]) / (2. * Err); hMatrix[2] = 0; hMatrix[3] = 0; hMatrix[4] = 0; - + return hMatrix; } - + @Override public double[] H(AStateVecs.StateVec stateVec, AStateVecs sv, MeasVec mv, Swim swimmer) { - throw new UnsupportedOperationException("Not supported yet."); - } + throw new UnsupportedOperationException("Not supported yet."); + } } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/StateVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/StateVecs.java index e1dcf670f..b48790705 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/StateVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/StateVecs.java @@ -26,41 +26,40 @@ public class StateVecs extends AStateVecs { private RungeKuttaDoca rk = new RungeKuttaDoca(); private final Matrix fMS = new Matrix(); private final Matrix copyMatrix = new Matrix(); - + final double ARGONRADLEN = 14; // radiation length in Argon is 14 cm - + final double AIRRADLEN = 30400; // radiation length in cm - + public double Z[]; - + private double beta = 1.0; // beta depends on mass hypothesis - + // For hit-based tracking - public void init(StateVec initSV) { - this.initSV = initSV; + public void init(StateVec initSV) { + this.initSV = initSV; this.trackTrajT.clear(); this.trackTrajF.clear(); this.trackTrajP.clear(); this.trackTrajB.clear(); this.trackTrajS.clear(); - this.trackTrajT.put(0, new StateVec(initSV)); - } - - // For time-based tracking - public void initFromHB(StateVec initSV, double beta) { - this.beta = beta; - this.initSV = initSV; + this.trackTrajT.put(0, new StateVec(initSV)); + } + + // For time-based tracking + public void initFromHB(StateVec initSV, double beta) { + this.beta = beta; + this.initSV = initSV; this.trackTrajT.clear(); this.trackTrajF.clear(); this.trackTrajP.clear(); this.trackTrajB.clear(); this.trackTrajS.clear(); - this.trackTrajT.put(0, new StateVec(initSV)); - } - - + this.trackTrajT.put(0, new StateVec(initSV)); + } + /** - * + * * @param sector * @param i initial state vector index * @param Zf @@ -68,7 +67,7 @@ public void initFromHB(StateVec initSV, double beta) { * @param mv measurements */ public Matrix transport(int sector, int i, double Zf, StateVec iVec, AMeasVecs mv, Swim swimmer) { // s = signed step-size - + double stepSize = 1.0; StateVec fVec = new StateVec(0); fVec.x = iVec.x; @@ -79,80 +78,83 @@ public Matrix transport(int sector, int i, double Zf, StateVec iVec, AMeasVecs m fVec.Q = iVec.Q; fVec.B = iVec.B; Matrix5x5.copy(iVec.CM, fVec.CM); - - double s = 0; + + double s = 0; double zInit = mv.measurements.get(i).surface.z; double BatMeas = iVec.B; - - double z = zInit; - - while(Math.signum(Zf - zInit) *zMath.signum(Zf - zInit) *Zf) - s=Math.signum(Zf - zInit) *Math.abs(Zf-z); - - + s = Math.signum(Zf - zInit) * stepSize; + + // LOGGER.log(Level.FINE, " from "+(float)Z[i]+" to "+(float)Z[f]+" at "+(float)z+" By is "+bf[1]+" B is "+Math.sqrt(bf[0]*bf[0]+bf[1]*bf[1]+bf[2]*bf[2])/Bmax+" stepSize is "+s); + if (Math.signum(Zf - zInit) * (z + s) > Math.signum(Zf - zInit) * Zf) { + s = Math.signum(Zf - zInit) * Math.abs(Zf - z); + } + //rk.RK4transport(sector, Q, x, y, z, tx, ty, s, swimmer, cMat, fVec, dPath); - rk.RK4transport(sector, s, swimmer, cMat, fVec); - + rk.RK4transport(sector, s, swimmer, cMat, fVec); + // Q process noise matrix estimate double p = Math.abs(1. / iVec.Q); - double X0 = this.getX0(z, Z); + double X0 = this.getX0(z, Z); double t_ov_X0 = Math.abs(s) / X0;//path length in radiation length units = t/X0 [true path length/ X0] ; Ar radiation length = 14 cm - double beta = this.beta; - if(beta>1.0 || beta<=0) - beta =1.0; + double beta = this.beta; + if (beta > 1.0 || beta <= 0) { + beta = 1.0; + } double sctRMS = 0; - - if(Math.abs(s)>0) - sctRMS = ((0.0136)/(beta*PhysicsConstants.speedOfLight()*p))*Math.sqrt(t_ov_X0)* - (1 + 0.038 * Math.log(t_ov_X0)); - + + if (Math.abs(s) > 0) { + sctRMS = ((0.0136) / (beta * PhysicsConstants.speedOfLight() * p)) * Math.sqrt(t_ov_X0) + * (1 + 0.038 * Math.log(t_ov_X0)); + } double cov_txtx = (1 + tx * tx) * (1 + tx * tx + ty * ty) * sctRMS * sctRMS; double cov_tyty = (1 + ty * ty) * (1 + tx * tx + ty * ty) * sctRMS * sctRMS; double cov_txty = tx * ty * (1 + tx * tx + ty * ty) * sctRMS * sctRMS; - + fMS.set( - 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, - 0, 0, cov_txtx, cov_txty, 0, - 0, 0, cov_txty, cov_tyty, 0, - 0, 0, 0, 0, 0 + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, cov_txtx, cov_txty, 0, + 0, 0, cov_txty, cov_tyty, 0, + 0, 0, 0, 0, 0 ); Matrix5x5.copy(fVec.CM, copyMatrix); - Matrix5x5.add(copyMatrix, fMS, fVec.CM); - - if( Math.abs(fVec.B - BatMeas)<0.0001) - stepSize*=2; - + Matrix5x5.add(copyMatrix, fMS, fVec.CM); + + if (Math.abs(fVec.B - BatMeas) < 0.0001) { + stepSize *= 2; + } + BatMeas = fVec.B; } - + return fVec.CM; - + } - - + /** - * + * * @param sector * @param i initial state vector index * @param f final state vector index @@ -160,7 +162,7 @@ public Matrix transport(int sector, int i, double Zf, StateVec iVec, AMeasVecs m * @param mv measurements */ public boolean transport(int sector, int i, int f, StateVec iVec, AMeasVecs mv, Swim swimmer, boolean forward) { // s = signed step-size - if(iVec==null) { + if (iVec == null) { return false; } double stepSize = 1.0; @@ -173,138 +175,145 @@ public boolean transport(int sector, int i, int f, StateVec iVec, AMeasVecs mv, fVec.Q = iVec.Q; fVec.B = iVec.B; Matrix5x5.copy(iVec.CM, fVec.CM); - - double s = 0; + + double s = 0; double zInit = mv.measurements.get(i).surface.z; double BatMeas = iVec.B; - + double z = zInit; - double zFinal = mv.measurements.get(f).surface.z; - - while(Math.signum(zFinal - zInit) *zMath.signum(zFinal - zInit) *zFinal) - s=Math.signum(zFinal - zInit) *Math.abs(zFinal-z); - - + s = Math.signum(zFinal - zInit) * stepSize; + + // LOGGER.log(Level.FINE, " from "+(float)Z[i]+" to "+(float)Z[f]+" at "+(float)z+" By is "+bf[1]+" B is "+Math.sqrt(bf[0]*bf[0]+bf[1]*bf[1]+bf[2]*bf[2])/Bmax+" stepSize is "+s); + if (Math.signum(zFinal - zInit) * (z + s) > Math.signum(zFinal - zInit) * zFinal) { + s = Math.signum(zFinal - zInit) * Math.abs(zFinal - z); + } + //rk.RK4transport(sector, Q, x, y, z, tx, ty, s, swimmer, cMat, fVec, dPath); rk.RK4transport(sector, s, swimmer, cMat, fVec); - + // Q process noise matrix estimate double p = Math.abs(1. / iVec.Q); - double X0 = this.getX0(z, Z); + double X0 = this.getX0(z, Z); double t_ov_X0 = Math.abs(s) / X0;//path length in radiation length units = t/X0 [true path length/ X0] ; Ar radiation length = 14 cm - double beta = this.beta; - if(beta>1.0 || beta<=0) - beta =1.0; + double beta = this.beta; + if (beta > 1.0 || beta <= 0) { + beta = 1.0; + } double sctRMS = 0; - - if(Math.abs(s)>0) - sctRMS = ((0.0136)/(beta*PhysicsConstants.speedOfLight()*p))*Math.sqrt(t_ov_X0)* - (1 + 0.038 * Math.log(t_ov_X0)); - + + if (Math.abs(s) > 0) { + sctRMS = ((0.0136) / (beta * PhysicsConstants.speedOfLight() * p)) * Math.sqrt(t_ov_X0) + * (1 + 0.038 * Math.log(t_ov_X0)); + } double cov_txtx = (1 + tx * tx) * (1 + tx * tx + ty * ty) * sctRMS * sctRMS; double cov_tyty = (1 + ty * ty) * (1 + tx * tx + ty * ty) * sctRMS * sctRMS; double cov_txty = tx * ty * (1 + tx * tx + ty * ty) * sctRMS * sctRMS; - + fMS.set( - 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, - 0, 0, cov_txtx, cov_txty, 0, - 0, 0, cov_txty, cov_tyty, 0, - 0, 0, 0, 0, 0 + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, cov_txtx, cov_txty, 0, + 0, 0, cov_txty, cov_tyty, 0, + 0, 0, 0, 0, 0 ); Matrix5x5.copy(fVec.CM, copyMatrix); - Matrix5x5.add(copyMatrix, fMS, fVec.CM); - - if( Math.abs(fVec.B - BatMeas)<0.0001) - stepSize*=2; - + Matrix5x5.add(copyMatrix, fMS, fVec.CM); + + if (Math.abs(fVec.B - BatMeas) < 0.0001) { + stepSize *= 2; + } + BatMeas = fVec.B; } - - if(forward) - this.trackTrajT.put(f, fVec); - else - this.trackTrajP.put(f, fVec); - - if(Double.isNaN(fVec.x) || Double.isNaN(fVec.y) || Double.isNaN(fVec.tx) || Double.isNaN(fVec.ty) || Double.isNaN(fVec.Q)) - return false; - else - return true; - + + if (forward) { + this.trackTrajT.put(f, fVec); + } else { + this.trackTrajP.put(f, fVec); + } + + if (Double.isNaN(fVec.x) || Double.isNaN(fVec.y) || Double.isNaN(fVec.tx) || Double.isNaN(fVec.ty) || Double.isNaN(fVec.Q)) { + return false; + } else { + return true; + } + } - + public double getX0(double z, double Z[]) { double X0 = AIRRADLEN; double tolerance = 0.01; - - for(int i = 1; i =Z[i]-tolerance && z<=Z[i+1]+tolerance) { + } + if (z >= Z[i] - tolerance && z <= Z[i + 1] + tolerance) { return ARGONRADLEN; } - } + } return X0; } - + @Override public double[][] Q(StateVec vec, AMeasVecs mv) { - throw new UnsupportedOperationException("Not supported yet."); + throw new UnsupportedOperationException("Not supported yet."); } - + @Override public void corrForEloss(int dir, StateVec vec, AMeasVecs mv) { - throw new UnsupportedOperationException("Not supported yet."); + throw new UnsupportedOperationException("Not supported yet."); } - + @Override public boolean getStateVecPosAtMeasSite(StateVec sv, AMeasVecs.MeasVec mv, Swim swim) { - throw new UnsupportedOperationException("Not supported yet."); + throw new UnsupportedOperationException("Not supported yet."); } - + @Override public boolean setStateVecPosAtMeasSite(StateVec sv, MeasVec mv, Swim swimmer) { - throw new UnsupportedOperationException("Not supported yet."); + throw new UnsupportedOperationException("Not supported yet."); } - + @Override public void printlnStateVec(StateVec S) { - throw new UnsupportedOperationException("Not supported yet."); + throw new UnsupportedOperationException("Not supported yet."); } - + @Override public double[][] F(StateVec iVec, StateVec fVec) { - throw new UnsupportedOperationException("Not supported yet."); - } - + throw new UnsupportedOperationException("Not supported yet."); + } + @Override public void init(Helix helix, double[][] cov, double xref, double yref, double zref, double mass, Swim swimmer) { - throw new UnsupportedOperationException("Not for forward tracking."); - + throw new UnsupportedOperationException("Not for forward tracking."); + } - + @Override public void init(double x0, double z0, double tx, double tz, Units units, double[][] cov) { - throw new UnsupportedOperationException("Not for forward tracking."); - } + throw new UnsupportedOperationException("Not for forward tracking."); + } } From 613ca231b2a299e68ca2e8a9ccf2dcbbb5075a57 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Tue, 7 Feb 2023 11:49:48 -0500 Subject: [PATCH 05/69] futher code format adjustments --- .../rec/dc/track/TrackCandListFinder.java | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index 97e86311f..86706c950 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -893,19 +893,18 @@ private List findStraightTracks(CrossList crossList, DCGeant4Factory DcDe cand.get(0).get_Dir().x() / cand.get(0).get_Dir().z(), cand.get(0).get_Dir().y() / cand.get(0).get_Dir().z()); cand.set_StateVecAtReg1MiddlePlane(VecAtReg1MiddlePlane); - - - LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); - - KFitter kFZRef = new KFitter(true, 1, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); - List measSurfaces = getMeasSurfaces(cand, DcDetector); - StateVecs svs = new StateVecs(); - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(cand, measSurfaces.get(0).z, 0, initSV, dcSwim, new float[3]); - kFZRef.init(measSurfaces, initSV); - + + LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); + + KFitter kFZRef = new KFitter(true, 1, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(cand, DcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(cand, measSurfaces.get(0).z, 0, initSV, dcSwim, new float[3]); + kFZRef.init(measSurfaces, initSV); + kFZRef.runFitter(); - + if (kFZRef.finalStateVec == null) { continue; } @@ -1144,15 +1143,15 @@ private List findCurvedTracks(CrossList crossList, DCGeant4Factory DcDete if (cand.get(1).isPseudoCross) { crossIdxinList = 0; } - - LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); - - KFitter kFZRef = new KFitter(true, 10, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); - List measSurfaces = getMeasSurfaces(cand, DcDetector); - StateVecs svs = new StateVecs(); - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(cand, measSurfaces.get(0).z, crossIdxinList, initSV, dcSwim, new float[3]); - kFZRef.init(measSurfaces, initSV); + + LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); + + KFitter kFZRef = new KFitter(true, 10, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(cand, DcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(cand, measSurfaces.get(0).z, crossIdxinList, initSV, dcSwim, new float[3]); + kFZRef.init(measSurfaces, initSV); kFZRef.runFitter(); List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); From 4fdf52c8ef2c8e3783b9a76755c4e216d94948fa Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Mon, 13 Feb 2023 12:47:30 -0500 Subject: [PATCH 06/69] first version of forward tracking with uRWell --- .../clas/tracking/kalmanfilter/AMeasVecs.java | 9 +- .../clas/tracking/kalmanfilter/Surface.java | 16 + .../jlab/clas/tracking/kalmanfilter/Type.java | 2 +- .../kalmanfilter/zReference/KFitter.java | 2 +- .../zReference/KFitterWithURWell.java | 652 ++++++++ .../kalmanfilter/zReference/MeasVecs.java | 2 +- etc/bankdefs/hipo4/dc.json | 28 +- etc/bankdefs/hipo4/urwell.json | 49 +- reconstruction/dc/pom.xml | 8 +- .../main/java/org/jlab/rec/dc/Constants.java | 4 +- .../java/org/jlab/rec/dc/banks/Banks.java | 4 + .../org/jlab/rec/dc/banks/RecoBankWriter.java | 187 ++- .../java/org/jlab/rec/dc/track/Track.java | 86 + .../track/TrackCandListWithURWellFinder.java | 1447 +++++++++++++++++ .../rec/dc/track/URWellDCCrossesList.java | 47 + .../dc/track/URWellDCCrossesListFinder.java | 30 + .../jlab/rec/dc/trajectory/Trajectory.java | 2 +- .../TrajectoryWithURWellFinder.java | 13 + .../jlab/service/dc/DCHBPostClusterConv.java | 88 - .../service/dc/DCURWellHBPostClusterConv.java | 207 +++ .../org/jlab/service/dc/DCURWellTBEngine.java | 711 ++++++++ .../jlab/rec/urwell/reader/URWellCluster.java | 58 + .../jlab/rec/urwell/reader/URWellCross.java | 139 ++ .../org/jlab/rec/urwell/reader/URWellHit.java | 43 + .../jlab/rec/urwell/reader/URWellReader.java | 127 ++ .../jlab/service/urwell/URWellConstants.java | 5 + 26 files changed, 3866 insertions(+), 100 deletions(-) create mode 100644 common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java create mode 100755 reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java create mode 100644 reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesList.java create mode 100644 reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesListFinder.java create mode 100644 reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrajectoryWithURWellFinder.java create mode 100644 reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java create mode 100755 reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java create mode 100644 reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCluster.java create mode 100644 reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java create mode 100644 reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellHit.java create mode 100644 reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java index 524af89bc..89c29f831 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java @@ -23,7 +23,7 @@ public abstract class AMeasVecs { public void setMeasVecs(List measSurfaces) { measurements = new ArrayList<>(); - if(measSurfaces.get(0).type != Type.LINEDOCA) // Measurements from DC has been sorted + if(measSurfaces.get(0).type != Type.LINEDOCA && measSurfaces.get(0).type != Type.PLANEURWELL) // Measurements from URWell and DC has been sorted Collections.sort(measSurfaces); for(int i = 0; i < measSurfaces.size(); i++) { MeasVec mvec = new MeasVec(); @@ -40,7 +40,12 @@ public void setMeasVecs(List measSurfaces) { mvec.sector = measSurfaces.get(i).getSector(); mvec.superlayer = measSurfaces.get(i).getSuperLayer(); mvec.layer = measSurfaces.get(i).getLayer(); - } + } + else if(measSurfaces.get(i).type == Type.PLANEURWELL){ + mvec.region = measSurfaces.get(i).region; + mvec.layer = measSurfaces.get(i).getLayer(); + mvec.sector = measSurfaces.get(i).getSector(); + } } } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java index 68f5d17f4..f14244123 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java @@ -53,6 +53,11 @@ public class Surface implements Comparable { public int superlayer; public int nMeas = 1; + // For URWell + public double y; + public double x_err; + public double y_err; + public void setNMeas(int n) { nMeas = n; } @@ -61,6 +66,17 @@ public int getNMeas() { return nMeas; } + // For URWell + public Surface(int sector, double x, double y, double z, double x_err, double y_err) { + type = Type.PLANEURWELL; + this.sector = sector; + this.x = x; + this.y = y; + this.z = z; + this.x_err = x_err; + this.y_err = y_err; + } + // For DC public Surface(int region, double z, double x, double tilt, double wireMaxSag, double[] doca, double[] unc, double error, Line3D[] wireLine) { type = Type.LINEDOCA; diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java index 3065ec092..b9de41cf1 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java @@ -8,7 +8,7 @@ public enum Type { UDF(-1), PLANEWITHPOINT(0), PLANEWITHLINE(1), PLANEWITHSTRIP(2), CYLINDERWITHPOINT(3), CYLINDERWITHLINE(4), CYLINDERWITHARC(5), - CYLINDERWITHSTRIP(6), LINE(7), LINEDOCA(8); + CYLINDERWITHSTRIP(6), LINE(7), LINEDOCA(8), PLANEURWELL(9); private final int value; Type(int value) { diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index ae8421956..3f53826b9 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -1,4 +1,4 @@ -package org.jlab.clas.tracking.kalmanfilter.zReference; + package org.jlab.clas.tracking.kalmanfilter.zReference; import java.util.ArrayList; import java.util.List; diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java new file mode 100644 index 000000000..1493db651 --- /dev/null +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -0,0 +1,652 @@ +package org.jlab.clas.tracking.kalmanfilter.zReference; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; +import java.util.logging.Level; + +import org.jlab.clas.clas.math.FastMath; +import org.jlab.clas.swimtools.Swim; +import org.jlab.clas.tracking.kalmanfilter.AKFitter; +import org.jlab.clas.tracking.kalmanfilter.AMeasVecs; +import org.jlab.clas.tracking.kalmanfilter.AStateVecs; +import org.jlab.clas.tracking.kalmanfilter.Surface; +import org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec; +import org.jlab.clas.tracking.kalmanfilter.zReference.MeasVecs; +import org.jlab.clas.tracking.kalmanfilter.zReference.StateVecs; +import org.jlab.clas.tracking.utilities.RungeKuttaDoca; +import org.jlab.clas.tracking.utilities.MatrixOps.Libr; +import org.jlab.geom.prim.Point3D; +import org.jlab.jnp.matrix.*; +import org.jlab.clas.tracking.kalmanfilter.Type; + +/** + * + * @author Tongtong Cao + */ +public class KFitterWithURWell extends AKFitter { + + private static final Logger LOGGER = Logger.getLogger(KFitter.class.getName()); + + private StateVecs sv = new StateVecs(); + private MeasVecs mv = new MeasVecs(); + private StateVec finalSmoothedStateVec = null; + private StateVec finalTransportedStateVec = null; + + public StateVec finalStateVec = null; + public StateVec initialStateVec = null; + public List kfStateVecsAlongTrajectory; + + private int iterNum; + + private double chi2kf = 0; + private double KFScale = 4; + + private int svzLength; + + public int ConvStatus = 1; + + private double Z[]; + + private boolean stopIteration = false; + + private boolean TBT = false; + + Matrix first_inverse = new Matrix(); + Matrix addition = new Matrix(); + Matrix result = new Matrix(); + Matrix result_inv = new Matrix(); + Matrix adj = new Matrix(); + + public KFitterWithURWell(boolean filter, int iterations, int dir, Swim swim, double Z[], Libr mo) { + super(filter, iterations, dir, swim, mo); + this.Z = Z; + } + + public final void init(List measSurfaces, StateVec initSV) { + finalSmoothedStateVec = null; + finalTransportedStateVec = null; + this.NDF0 = -5; + this.NDF = -5; + this.chi2 = Double.POSITIVE_INFINITY; + this.numIter = 0; + this.setFitFailed = false; + mv.setMeasVecs(measSurfaces); + for (int i = 0; i < mv.measurements.size(); i++) { + if (mv.measurements.get(i).skip == false) { + if(mv.measurements.get(i).surface.type == Type.LINEDOCA) + this.NDF += mv.measurements.get(i).surface.getNMeas(); + else if(mv.measurements.get(i).surface.type == Type.PLANEURWELL) + this.NDF += 2; + } + } + + sv.init(initSV); + sv.Z = Z; + } + + public final void initFromHB(List measSurfaces, StateVec initSV, double beta) { + finalSmoothedStateVec = null; + finalTransportedStateVec = null; + this.NDF0 = -5; + this.NDF = -5; + this.chi2 = Double.POSITIVE_INFINITY; + this.numIter = 0; + this.setFitFailed = false; + mv.setMeasVecs(measSurfaces); + for (int i = 0; i < mv.measurements.size(); i++) { + if (mv.measurements.get(i).skip == false) { + if(mv.measurements.get(i).surface.type == Type.LINEDOCA) + this.NDF += mv.measurements.get(i).surface.getNMeas(); + else if(mv.measurements.get(i).surface.type == Type.PLANEURWELL) + this.NDF += 2; + } + } + + sv.initFromHB(initSV, beta); + sv.Z = Z; + TBT = true; + } + + public void runFitter() { + this.chi2 = Double.POSITIVE_INFINITY; + double initChi2 = Double.POSITIVE_INFINITY; + // this.NDF = mv.ndf; + this.svzLength = this.mv.measurements.size(); + + int sector = this.mv.measurements.get(0).sector; + + if (TBT == true) { + this.chi2kf = 0; + // Get the input parameters + for (int k = 0; k < svzLength - 1; k++) { + sv.transport(sector, k, k + 1, this.sv.trackTrajT.get(k), mv, this.getSwimmer(), true); + } + this.calcFinalChisq(sector, true); + this.initialStateVec = sv.trackTrajT.get(svzLength - 1); + this.finalStateVec = sv.trackTrajT.get(svzLength - 1); + initChi2 = this.chi2; + if (Double.isNaN(chi2)) { + this.setFitFailed = true; + return; + } + } + + for (int i = 1; i <= totNumIter; i++) { + iterNum = i; + this.chi2kf = 0; + + if (i > 1) { + + for (int k = svzLength - 1; k > 0; k--) { + boolean forward = false; + if (k >= 2) { + + // Not backward transport and filter states for the last measurement layer + if (k == svzLength - 1) { + if (!sv.transport(sector, k, k - 2, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } else { + if (!sv.transport(sector, k, k - 2, this.sv.trackTrajB.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + + if (!this.filter(k - 2, forward)) { + this.stopIteration = true; + break; + } + + if (!sv.transport(sector, k - 2, k - 1, this.sv.trackTrajB.get(k - 2), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + if (!this.filter(k - 1, forward)) { + this.stopIteration = true; + break; + } + } else { + if (!sv.transport(sector, 1, 0, this.sv.trackTrajB.get(1), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + if (!this.filter(0, forward)) { + this.stopIteration = true; + break; + } + } + } + } + + if (this.stopIteration) { + break; + } + + for (int k = 0; k < svzLength - 1; k++) { + boolean forward = true; + + if (iterNum == 1 && (k == 0)) { + if (TBT == true) { + this.sv.transported(true).put(0, this.sv.transported(false).get(0)); // For TBT, calcFinalChisq() is called previously. + } + } + + if (k == 0) { + if (i == 1) { + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajT.get(0), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } else { + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajB.get(0), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + } else { + if (!this.sv.transport(sector, k, k + 1, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + } + + if (!this.filter(k + 1, forward)) { + this.stopIteration = true; + break; + } + } + + if (this.stopIteration) { + break; + } + + if (i > 1) { + if (this.setFitFailed == true) { + i = totNumIter; + } + if (this.setFitFailed == false) { + if (this.finalStateVec != null) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 5.e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 1.e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 1.e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 1.e-6 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.e-6) { + i = totNumIter; + } + } + this.finalStateVec = sv.trackTrajF.get(svzLength - 1); + + } else { + this.ConvStatus = 1; // Should be 0??? + } + } + + } + + if (totNumIter == 1) { + if (this.setFitFailed == false && this.stopIteration == false) { + this.finalStateVec = sv.trackTrajF.get(svzLength - 1); + } + } + + this.calcFinalChisq(sector); + + if (Double.isNaN(chi2)) { + this.setFitFailed = true; + } + + if (TBT == true) { + if (chi2 > initChi2) { // fit failed + this.finalStateVec = this.initialStateVec; + sv.trackTrajT.put(svzLength - 1, this.initialStateVec); + this.calcFinalChisq(sector, true); + } + } + + } + + private boolean filter(int k, boolean forward) { + StateVec sVec = sv.transported(forward).get(k); + org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec mVec = mv.measurements.get(k); + + if (Double.isNaN(sVec.x) || Double.isNaN(sVec.y) + || Double.isNaN(sVec.tx) || Double.isNaN(sVec.ty) + || Double.isNaN(sVec.Q)) { + this.setFitFailed = true; + return false; + } + if (sVec != null && sVec.CM != null + && k < mv.measurements.size() && mVec.skip == false) { + + if (mVec.surface.type == Type.LINEDOCA) { + + double[] K = new double[5]; + double V = mVec.surface.unc[0] * KFScale; + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[0]); + Matrix CaInv = this.filterCovMat(H, sVec.CM, V); + Matrix cMat = new Matrix(); + if (CaInv != null) { + Matrix5x5.copy(CaInv, cMat); + } else { + return false; + } + + for (int j = 0; j < 5; j++) { + // the gain matrix + K[j] = (H[0] * cMat.get(j, 0) + + H[1] * cMat.get(j, 1)) / V; + } + + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + double h = mv.hDoca(point, mVec.surface.wireLine[0]); + + double signMeas = 1; + double sign = 1; + if (mVec.surface.doca[1] != -99 + || !(Math.abs(mVec.surface.doca[0]) < 0.5 + && mVec.surface.doca[1] == -99)) { // use LR only for double hits && large + // enough docas + signMeas = Math.signum(mVec.surface.doca[0]); + sign = Math.signum(h); + } else { + signMeas = Math.signum(h); + sign = Math.signum(h); + } + + double c2 = ((signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)) + * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)) / V); + + double x_filt = sVec.x + + K[0] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double y_filt = sVec.y + + K[1] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double tx_filt = sVec.tx + + K[2] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double ty_filt = sVec.ty + + K[3] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double Q_filt = sVec.Q + + K[4] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + + // USE THE DOUBLE HIT + if (mVec.surface.doca[1] != -99) { + // now filter using the other Hit + V = mVec.surface.unc[1] * KFScale; + H = mv.H(x_filt, y_filt, mVec.surface.z, + mVec.surface.wireLine[1]); + CaInv = this.filterCovMat(H, cMat, V); + if (CaInv != null) { + for (int i = 0; i < 5; i++) { + Matrix5x5.copy(CaInv, cMat); + } + } else { + return false; + } + for (int j = 0; j < 5; j++) { + // the gain matrix + K[j] = (H[0] * cMat.get(j, 0) + + H[1] * cMat.get(j, 1)) / V; + } + + Point3D point2 = new Point3D(x_filt, y_filt, mVec.surface.z); + + h = mv.hDoca(point2, mVec.surface.wireLine[1]); + + signMeas = Math.signum(mVec.surface.doca[1]); + sign = Math.signum(h); + + x_filt += K[0] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + y_filt += K[1] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + tx_filt += K[2] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + ty_filt += K[3] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + Q_filt += K[4] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + + c2 += ((signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)) + * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)) / V); + } + + chi2kf += c2; + if (filterOn) { + StateVec filteredVec = sv.new StateVec(k); + filteredVec.x = x_filt; + filteredVec.y = y_filt; + filteredVec.tx = tx_filt; + filteredVec.ty = ty_filt; + filteredVec.Q = Q_filt; + filteredVec.z = sVec.z; + filteredVec.B = sVec.B; + filteredVec.deltaPath = sVec.deltaPath; + + filteredVec.CM = cMat; + + sv.filtered(forward).put(k, filteredVec); + } else { + return false; + } + } + + else if (mVec.surface.type == Type.PLANEURWELL) { + + + double V[][] = {{mVec.surface.x_err * mVec.surface.x_err, 0}, {0, mVec.surface.y_err * mVec.surface.y_err}}; + + if(V[0][0] == 0 || V[1][1] == 0 ) + LOGGER.log(Level.SEVERE, "Resolution for URWell is 0."); + + Matrix cMat = this.filterCovMatURWell(sVec.CM, V); + if (cMat == null) return false; + + double[][] K = KURWell(cMat, V); + double [] res = {mVec.surface.x - sVec.x, mVec.surface.y - sVec.y}; + + double filt[] = new double[5]; + for(int i = 0; i < 5; i ++){ + for(int j = 0; j < 2; j++){ + filt[i] += K[i][j]*res[j]; + } + } + + double c2 = res[0]*res[0]/V[0][0] + res[1]/V[1][1]; + + //System.out.println("mVec.surface.x: " + mVec.surface.x + "; sVec.x: " + sVec.x + " " + "mVec.surface.y: " + mVec.surface.y + "; sVec.y: " + sVec.y + "res[0]: " + res[0] + "; res[1]: " + res[1]); + + chi2kf += c2; + if (filterOn) { + StateVec filteredVec = sv.new StateVec(k); + filteredVec.x = sVec.x + filt[0]; + filteredVec.y = sVec.y + filt[1]; + filteredVec.tx = sVec.tx + filt[2]; + filteredVec.ty = sVec.ty + filt[3]; + filteredVec.Q = sVec.Q +filt[4]; + filteredVec.z = sVec.z; + filteredVec.B = sVec.B; + filteredVec.deltaPath = sVec.deltaPath; + + filteredVec.CM = cMat; + + sv.filtered(forward).put(k, filteredVec); + //sv.filtered(forward).put(k, sVec); + + } else { + return false; + } + } + + return true; + + } else { + return false; + } + } + + public Matrix filterCovMatURWell(Matrix Ci, double[][] V) { + double det = Matrix5x5.inverse(Ci, first_inverse, adj); + if (Math.abs(det) < 1.e-30) { + return null; + } + + addition.set( + 1 / V[0][0], 0, 0, 0, 0, + 0, 1 / V[1][1], 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0); + + Matrix5x5.add(first_inverse, addition, result); + double det2 = Matrix5x5.inverse(result, result_inv, adj); + if (Math.abs(det2) < 1.e-30) { + return null; + } + + return result_inv; + } + + public double[][] KURWell(Matrix C, double[][] V) { + double K[][] = new double[5][2]; + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 2; j++) { + K[i][j] = C.get(i, j) / V[j][j]; + } + } + return K; + } + + public Matrix filterCovMat(double[] H, Matrix Ci, double V) { + + double det = Matrix5x5.inverse(Ci, first_inverse, adj); + if (Math.abs(det) < 1.e-30) { + return null; + } + + addition.set( + H[0] * H[0] / V, H[0] * H[1] / V, 0, 0, 0, + H[0] * H[1] / V, H[1] * H[1] / V, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0); + + Matrix5x5.add(first_inverse, addition, result); + double det2 = Matrix5x5.inverse(result, result_inv, adj); + if (Math.abs(det2) < 1.e-30) { + return null; + } + + return result_inv; + } + + private void calcFinalChisq(int sector) { + calcFinalChisq(sector, false); + } + + private void calcFinalChisq(int sector, boolean nofilter) { + int k = svzLength - 1; + this.chi2 = 0; + double path = 0; + double[] nRj = new double[4]; + + StateVec sVec; + + // To be changed: to match wit the old package, we make the following codes. Could be changed when other codes for application of calcFinalChisq are changed. + if (nofilter || (sv.trackTrajF.get(k) == null)) { + sVec = sv.trackTrajT.get(k); + } else { + sVec = sv.trackTrajF.get(k); + } + + kfStateVecsAlongTrajectory = new ArrayList<>(); + if (sVec != null && sVec.CM != null) { + + boolean forward = false; + sv.transport(sector, k, 0, sVec, mv, this.getSwimmer(), forward); + + StateVec svc = sv.transported(forward).get(0); + path += svc.deltaPath; + svc.setPathLength(path); + kfStateVecsAlongTrajectory.add(svc); + + double x_err = mv.measurements.get(0).surface.x_err; + double y_err = mv.measurements.get(0).surface.y_err; + + double x_res = mv.measurements.get(0).surface.x - svc.x; + double y_res = mv.measurements.get(0).surface.y - svc.y; + + //System.out.println(x_res + " " + y_res + " " + x_err + " " + y_err); + + chi2 += (x_res*x_res) / (x_err*x_err); + chi2 += (y_res*y_res) / (y_err*y_err); + + nRj[mv.measurements.get(0).region] += x_res * x_res / (x_err*x_err); + nRj[mv.measurements.get(0).region] += y_res * y_res / (y_err*y_err); + + forward = true; + for (int k1 = 0; k1 < k; k1++) { + if (k1 == 0) { + sv.transport(sector, k1, k1 + 1, svc, mv, this.getSwimmer(), forward); + } else { + sv.transport(sector, k1, k1 + 1, sv.transported(forward).get(k1), mv, this.getSwimmer(), forward); + } + + double V = mv.measurements.get(k1 + 1).surface.unc[0]; + + Point3D point = new Point3D(sv.transported(forward).get(k1 + 1).x, sv.transported(forward).get(k1 + 1).y, mv.measurements.get(k1 + 1).surface.z); + + double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); + svc = sv.transported(forward).get(k1 + 1); + path += svc.deltaPath; + svc.setPathLength(path); + svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); + svc.setProjectorDoca(h); + kfStateVecsAlongTrajectory.add(svc); + double res = (mv.measurements.get(k1 + 1).surface.doca[0] - h); + chi2 += (mv.measurements.get(k1 + 1).surface.doca[0] - h) * (mv.measurements.get(k1 + 1).surface.doca[0] - h) / V; + nRj[mv.measurements.get(k1 + 1).region - 1] += res * res / V; + //USE THE DOUBLE HIT + if (mv.measurements.get(k1 + 1).surface.doca[1] != -99) { + V = mv.measurements.get(k1 + 1).surface.unc[1]; + h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[1]); + res = (mv.measurements.get(k1 + 1).surface.doca[1] - h); + chi2 += (mv.measurements.get(k1 + 1).surface.doca[1] - h) * (mv.measurements.get(k1 + 1).surface.doca[1] - h) / V; + nRj[mv.measurements.get(k1 + 1).region] += res * res / V; + svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); + svc.setProjectorDoca(h); + kfStateVecsAlongTrajectory.add(svc); + } + } + } + //System.out.println("this.chi2:" + this.chi2); + + } + + public Matrix propagateToVtx(int sector, double Zf) { + return sv.transport(sector, 0, Zf, sv.trackTrajP.get(0), mv, this.getSwimmer()); + } + + @Override + public void runFitter(AStateVecs sv, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public StateVec filter(int k, StateVec vec, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public StateVec smooth(int k, AStateVecs sv, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public StateVec smooth(StateVec v1, StateVec v2) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public MeasVecs getMeasVecs() { + return mv; + } + + public StateVecs getStateVecs() { + return sv; + } + + public void printlnMeasVecs() { + for (int i = 0; i < mv.measurements.size(); i++) { + org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec measvec = mv.measurements.get(i); + if(measvec.surface.type == Type.LINEDOCA){ + String s = String.format("k=%d region=%d superlayer=%d layer=%d error=%.4f", measvec.k, measvec.region, measvec.superlayer, + measvec.layer, measvec.error); + s += String.format(" Surface: index=%d x=%.4f z=%.4f tilt=%.4f wireMaxSag=%.4f", measvec.surface.getIndex(), + measvec.surface.x, measvec.surface.z, measvec.surface.tilt, measvec.surface.wireMaxSag, measvec.surface.unc[1]); + s += String.format( + " Surface line 0: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", + measvec.surface.doca[0], measvec.surface.unc[0], measvec.surface.wireLine[0].origin().x(), + measvec.surface.wireLine[0].origin().y(), measvec.surface.wireLine[0].origin().z(), + measvec.surface.wireLine[0].end().x(), measvec.surface.wireLine[0].end().y(), + measvec.surface.wireLine[0].end().z()); + if (measvec.surface.wireLine[1] != null) { + s += String.format( + " Surface line 1: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", + measvec.surface.doca[1], measvec.surface.unc[1], measvec.surface.wireLine[1].origin().x(), + measvec.surface.wireLine[1].origin().y(), measvec.surface.wireLine[1].origin().z(), + measvec.surface.wireLine[1].end().x(), measvec.surface.wireLine[1].end().y(), + measvec.surface.wireLine[1].end().z()); + } + + System.out.println(s); + } + else if(measvec.surface.type == Type.PLANEURWELL){ + String s = String.format("k=%d region=%d superlayer=%d layer=%d", measvec.k, measvec.region, measvec.superlayer, + measvec.layer); + s += String.format(" Surface: index=%d x=%.4f y=%.4f z=%.4f x_err=%.4f y_err=%.4f", measvec.surface.getIndex(), + measvec.surface.x, measvec.surface.y, measvec.surface.z, measvec.surface.x_err, measvec.surface.y_err); + System.out.println(s); + } + } + } + +} diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/MeasVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/MeasVecs.java index 24e4d87c8..8814ca17e 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/MeasVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/MeasVecs.java @@ -16,7 +16,7 @@ * @author tongtong cao */ public class MeasVecs extends AMeasVecs { - + public double[] H(double x, double y, double z, Line3D wireLine) { double[] hMatrix = new double[5]; double Err = 0.025; diff --git a/etc/bankdefs/hipo4/dc.json b/etc/bankdefs/hipo4/dc.json index 7d317f530..a6b33f58a 100644 --- a/etc/bankdefs/hipo4/dc.json +++ b/etc/bankdefs/hipo4/dc.json @@ -211,7 +211,19 @@ {"name":"t1_z", "type":"F", "info":"Upstream Region 1 track z-position in the lab (in cm)"}, {"name":"t1_px", "type":"F", "info":"Upstream Region 1 track unit x-momentum vector in the lab"}, {"name":"t1_py", "type":"F", "info":"Upstream Region 1 track unit y-momentum vector in the lab"}, - {"name":"t1_pz", "type":"F", "info":"Upstream Region 1 track unit z-momentum vector in the lab"}, + {"name":"t1_pz", "type":"F", "info":"Upstream Region 1 track unit z-momentum vector in the lab"}, + {"name":"URWell_x", "type":"F", "info":"URWell track x-position in the lab (in cm)"}, + {"name":"URWell_y", "type":"F", "info":"URWell track y-position in the lab (in cm)"}, + {"name":"URWell_z", "type":"F", "info":"URWell track z-position in the lab (in cm)"}, + {"name":"URWell_px", "type":"F", "info":"URWell track unit x-momentum vector in the lab"}, + {"name":"URWell_py", "type":"F", "info":"URWell track unit y-momentum vector in the lab"}, + {"name":"URWell_pz", "type":"F", "info":"URWell track unit z-momentum vector in the lab"}, + {"name":"URWell_x_local", "type":"F", "info":"URWell track x-position in the tilted sector coordinates (in cm)"}, + {"name":"URWell_y_local", "type":"F", "info":"URWell track y-position in the tilted sector coordinates (in cm)"}, + {"name":"URWell_z_local", "type":"F", "info":"URWell track z-position in the tilted sector coordinates (in cm)"}, + {"name":"URWell_px_local", "type":"F", "info":"URWell track unit x-momentum vector in the tilted sector coordinates"}, + {"name":"URWell_py_local", "type":"F", "info":"URWell track unit y-momentum vector in the tilted sector coordinates"}, + {"name":"URWell_pz_local", "type":"F", "info":"URWell track unit z-momentum vector in the tilted sector coordinates"}, {"name":"Vtx0_x", "type":"F", "info":"Vertex x-position of the swam track to the DOCA to the beamline (in cm)"}, {"name":"Vtx0_y", "type":"F", "info":"Vertex y-position of the swam track to the DOCA to the beamline (in cm)"}, {"name":"Vtx0_z", "type":"F", "info":"Vertex z-position of the swam track to the DOCA to the beamline (in cm)"}, @@ -223,6 +235,7 @@ {"name":"z", "type":"F", "info":"last site stateVec in tilted sector z (in cm)"}, {"name":"tx", "type":"F", "info":"last site stateVec in tilted sector tx (px/pz)"}, {"name":"ty", "type":"F", "info":"last site stateVec in tilted sector ty (py/pz)"}, + {"name":"URWellCross_ID","type":"S", "info":"id of URWell cross on track"}, {"name":"Cross1_ID", "type":"S", "info":"id of first cross on track"}, {"name":"Cross2_ID", "type":"S", "info":"id of second cross on track"}, {"name":"Cross3_ID", "type":"S", "info":"id of third cross on track"}, @@ -445,12 +458,25 @@ {"name":"t1_px", "type":"F", "info":"Upstream Region 1 track unit x-momentum vector in the lab"}, {"name":"t1_py", "type":"F", "info":"Upstream Region 1 track unit y-momentum vector in the lab"}, {"name":"t1_pz", "type":"F", "info":"Upstream Region 1 track unit z-momentum vector in the lab"}, + {"name":"URWell_x", "type":"F", "info":"URWell track x-position in the lab (in cm)"}, + {"name":"URWell_y", "type":"F", "info":"URWell track y-position in the lab (in cm)"}, + {"name":"URWell_z", "type":"F", "info":"URWell track z-position in the lab (in cm)"}, + {"name":"URWell_px", "type":"F", "info":"URWell track unit x-momentum vector in the lab"}, + {"name":"URWell_py", "type":"F", "info":"URWell track unit y-momentum vector in the lab"}, + {"name":"URWell_pz", "type":"F", "info":"URWell track unit z-momentum vector in the lab"}, + {"name":"URWell_x_local", "type":"F", "info":"URWell track x-position in the tilted sector coordinates (in cm)"}, + {"name":"URWell_y_local", "type":"F", "info":"URWell track y-position in the tilted sector coordinates (in cm)"}, + {"name":"URWell_z_local", "type":"F", "info":"URWell track z-position in the tilted sector coordinates (in cm)"}, + {"name":"URWell_px_local", "type":"F", "info":"URWell track unit x-momentum vector in the tilted sector coordinates"}, + {"name":"URWell_py_local", "type":"F", "info":"URWell track unit y-momentum vector in the tilted sector coordinates"}, + {"name":"URWell_pz_local", "type":"F", "info":"URWell track unit z-momentum vector in the tilted sector coordinates"}, {"name":"Vtx0_x", "type":"F", "info":"Vertex x-position of the swam track to the DOCA to the beamline (in cm)"}, {"name":"Vtx0_y", "type":"F", "info":"Vertex y-position of the swam track to the DOCA to the beamline (in cm)"}, {"name":"Vtx0_z", "type":"F", "info":"Vertex z-position of the swam track to the DOCA to the beamline (in cm)"}, {"name":"p0_x", "type":"F", "info":"3-momentum x-coordinate of the swam track to the DOCA to the beamline (in cm)"}, {"name":"p0_y", "type":"F", "info":"3-momentum y-coordinate of the swam track to the DOCA to the beamline (in cm)"}, {"name":"p0_z", "type":"F", "info":"3-momentum z-coordinate of the swam track to the DOCA to the beamline (in cm)"}, + {"name":"URWellCross_ID","type":"S", "info":"id of URWell cross on track"}, {"name":"Cross1_ID", "type":"S", "info":"id of first cross on track"}, {"name":"Cross2_ID", "type":"S", "info":"id of second cross on track"}, {"name":"Cross3_ID", "type":"S", "info":"id of third cross on track"}, diff --git a/etc/bankdefs/hipo4/urwell.json b/etc/bankdefs/hipo4/urwell.json index 9f1da0f4d..051b4ae49 100644 --- a/etc/bankdefs/hipo4/urwell.json +++ b/etc/bankdefs/hipo4/urwell.json @@ -55,5 +55,52 @@ {"name":"cluster2", "type":"S", "info":"id of the cluster in the W layer"}, {"name":"status", "type":"S", "info":"status of the cluster"} ] + }, + { + "name": "HitBasedTrkg::HBURWellCrosses", + "group": 22300, + "item" : 31, + "info": "crosses from URWEL", + "entries": [ + {"name":"id", "type":"S", "info":"id of the cross"}, + {"name":"tid", "type":"S", "info":"track id"}, + {"name":"sector", "type":"B", "info":"sector of URWELL"}, + {"name":"layer", "type":"B", "info":"Layer of URWELL"}, + {"name":"energy", "type":"F", "info":"Energy of the cross (eV)"}, + {"name":"time", "type":"F", "info":"Time of the cross (ns)"}, + {"name":"x", "type":"F", "info":"X in global coordinate (cm)"}, + {"name":"y", "type":"F", "info":"Y in global coordinate (cm)"}, + {"name":"z", "type":"F", "info":"Z in global coordinate (cm)"}, + {"name":"x_local", "type":"F", "info":"X in tilted sector coordinate (cm)"}, + {"name":"y_local", "type":"F", "info":"Y in tilted sector coordinate (cm)"}, + {"name":"z_local", "type":"F", "info":"Z in tilted sector coordinate (cm)"}, + {"name":"cluster1", "type":"S", "info":"id of the cluster in the V layer"}, + {"name":"cluster2", "type":"S", "info":"id of the cluster in the W layer"}, + {"name":"status", "type":"S", "info":"status of the cluster"} + ] + }, + { + "name": "TimeBasedTrkg::TBURWellCrosses", + "group": 22300, + "item" : 32, + "info": "crosses from URWEL", + "entries": [ + {"name":"id", "type":"S", "info":"id of the cross"}, + {"name":"tid", "type":"S", "info":"track id"}, + {"name":"sector", "type":"B", "info":"sector of URWELL"}, + {"name":"layer", "type":"B", "info":"Layer of URWELL"}, + {"name":"energy", "type":"F", "info":"Energy of the cross (eV)"}, + {"name":"time", "type":"F", "info":"Time of the cross (ns)"}, + {"name":"x", "type":"F", "info":"X in global coordinate (cm)"}, + {"name":"y", "type":"F", "info":"Y in global coordinate (cm)"}, + {"name":"z", "type":"F", "info":"Z in global coordinate (cm)"}, + {"name":"x_local", "type":"F", "info":"X in tilted sector coordinate (cm)"}, + {"name":"y_local", "type":"F", "info":"Y in tilted sector coordinate (cm)"}, + {"name":"z_local", "type":"F", "info":"Z in tilted sector coordinate (cm)"}, + {"name":"cluster1", "type":"S", "info":"id of the cluster in the V layer"}, + {"name":"cluster2", "type":"S", "info":"id of the cluster in the W layer"}, + {"name":"status", "type":"S", "info":"status of the cluster"} + ] } -] \ No newline at end of file + +] diff --git a/reconstruction/dc/pom.xml b/reconstruction/dc/pom.xml index 796939362..ec1dc226b 100644 --- a/reconstruction/dc/pom.xml +++ b/reconstruction/dc/pom.xml @@ -18,7 +18,13 @@ - + + + org.jlab.clas12.detector + clas12detector-urwell + 1.0-SNAPSHOT + + org.jlab.clas clas-tracking diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java index 3302774d1..53d2e66df 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java @@ -92,6 +92,8 @@ public static Constants getInstance() { public static final double HTCCRADIUS = 175; public static final double LTCCPLANE = 653.09; + public static final double URWELLLOCALZ = 226.0464; + // other CLAS12 parameters public static final int NSECT = 6; public static final int NSLAY = 6; @@ -184,7 +186,7 @@ public static Constants getInstance() { public static final int MAXCLUSSIZE = 14; - public static final double MAXCHI2 = 10000; + public static final double MAXCHI2 = 1000000; public static double HBTCHI2CUT = 2000; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/Banks.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/Banks.java index 32286eed9..004ffdcd7 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/Banks.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/Banks.java @@ -110,6 +110,10 @@ public String getSegmentsBank() { public String getCrossesBank() { return this.getOutputBank("Crosses"); } + + public String getURWellCrossesBank() { + return this.getOutputBank("URWellCrosses"); + } public String getTracksBank() { return this.getOutputBank("Tracks"); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java index bf741d10d..a677a4f08 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java @@ -12,6 +12,7 @@ import org.jlab.rec.dc.hit.Hit; import org.jlab.rec.dc.segment.Segment; import org.jlab.rec.dc.track.Track; +import org.jlab.rec.urwell.reader.URWellCross; import trackfitter.fitter.utilities.*; @@ -322,6 +323,36 @@ public DataBank fillHBCrossesBank(DataEvent event, List crosslist) { } return bank; } + + /** + * + * @param event the EvioEvent + * @param urCrosses + * @return urwell cross bank + */ + public DataBank fillHBURWellCrossesBank(DataEvent event, List urCrosses) { + String name = bankNames.getURWellCrossesBank(); + DataBank bank = event.createBank(name, urCrosses.size()); + + for (int i = 0; i < urCrosses.size(); i++) { + bank.setShort("id", i, (short) urCrosses.get(i).id()); + bank.setShort("tid", i, (short) urCrosses.get(i).get_tid()); + bank.setByte("sector", i, (byte) urCrosses.get(i).sector()); + bank.setFloat("energy", i, (float) urCrosses.get(i).energy()); + bank.setFloat("time", i, (float) urCrosses.get(i).time()); + bank.setFloat("x", i, (float) urCrosses.get(i).position().x()); + bank.setFloat("y", i, (float) urCrosses.get(i).position().y()); + bank.setFloat("z", i, (float) urCrosses.get(i).position().z()); + bank.setFloat("x_local", i, (float) urCrosses.get(i).local().x()); + bank.setFloat("y_local", i, (float) urCrosses.get(i).local().y()); + bank.setFloat("z_local", i, (float) urCrosses.get(i).local().z()); + bank.setShort("cluster1", i, (short) urCrosses.get(i).cluster1()); + bank.setShort("cluster2", i, (short) urCrosses.get(i).cluster2()); + bank.setShort("status", i, (short) urCrosses.get(i).status()); + } + + return bank; + } public DataBank fillHBTracksBank(DataEvent event, List candlist) { String name = bankNames.getTracksBank(); @@ -356,6 +387,20 @@ public DataBank fillHBTracksBank(DataEvent event, List candlist) { bank.setFloat("t1_py", i, (float) candlist.get(i).get_Region1TrackP().y()); bank.setFloat("t1_pz", i, (float) candlist.get(i).get_Region1TrackP().z()); } + if(candlist.get(i).get_URWellPointGlobal() != null && candlist.get(i).get_URWellPointLocal() != null){ + bank.setFloat("URWell_x", i, (float) candlist.get(i).get_URWellPointGlobal().x()); + bank.setFloat("URWell_y", i, (float) candlist.get(i).get_URWellPointGlobal().y()); + bank.setFloat("URWell_z", i, (float) candlist.get(i).get_URWellPointGlobal().z()); + bank.setFloat("URWell_px", i, (float) candlist.get(i).get_URWellPGlobal().x()); + bank.setFloat("URWell_py", i, (float) candlist.get(i).get_URWellPGlobal().y()); + bank.setFloat("URWell_pz", i, (float) candlist.get(i).get_URWellPGlobal().z()); + bank.setFloat("URWell_x_local", i, (float) candlist.get(i).get_URWellPointLocal().x()); + bank.setFloat("URWell_y_local", i, (float) candlist.get(i).get_URWellPointLocal().y()); + bank.setFloat("URWell_z_local", i, (float) candlist.get(i).get_URWellPointLocal().z()); + bank.setFloat("URWell_px_local", i, (float) candlist.get(i).get_URWellPLocal().x()); + bank.setFloat("URWell_py_local", i, (float) candlist.get(i).get_URWellPLocal().y()); + bank.setFloat("URWell_pz_local", i, (float) candlist.get(i).get_URWellPLocal().z()); + } bank.setFloat("pathlength", i, (float) candlist.get(i).get_TotPathLen()); bank.setFloat("Vtx0_x", i, (float) candlist.get(i).get_Vtx0().x()); bank.setFloat("Vtx0_y", i, (float) candlist.get(i).get_Vtx0().y()); @@ -364,6 +409,10 @@ public DataBank fillHBTracksBank(DataEvent event, List candlist) { bank.setFloat("p0_y", i, (float) candlist.get(i).get_pAtOrig().y()); bank.setFloat("p0_z", i, (float) candlist.get(i).get_pAtOrig().z()); //fill associated IDs + if(candlist.get(i).get_URWellCross() != null) + bank.setShort("URWellCross_ID", i, (short) candlist.get(i).get_URWellCross().id()); + else + bank.setShort("URWellCross_ID", i, (short)-1); for(int r = 0; r < 3; r++) { bank.setShort("Cross"+String.valueOf(r+1)+"_ID", i, (short) -1); @@ -397,7 +446,7 @@ public DataBank fillHBTracksBank(DataEvent event, List candlist) { //bank.show(); return bank; } - + public DataBank fillHBTrajectoryBank(DataEvent event, List candlist) { return this.fillTrajectoryBank(event, candlist); } @@ -690,6 +739,36 @@ private DataBank fillTBCrossesBank(DataEvent event, List crosslist) { return bank; } + /** + * + * @param event the EvioEvent + * @param urCrosses + * @return urwell cross bank + */ + public DataBank fillTBURWellCrossesBank(DataEvent event, List urCrosses) { + String name = bankNames.getURWellCrossesBank(); + DataBank bank = event.createBank(name, urCrosses.size()); + + for (int i = 0; i < urCrosses.size(); i++) { + bank.setShort("id", i, (short) urCrosses.get(i).id()); + bank.setShort("tid", i, (short) urCrosses.get(i).get_tid()); + bank.setByte("sector", i, (byte) urCrosses.get(i).sector()); + bank.setFloat("energy", i, (float) urCrosses.get(i).energy()); + bank.setFloat("time", i, (float) urCrosses.get(i).time()); + bank.setFloat("x", i, (float) urCrosses.get(i).position().x()); + bank.setFloat("y", i, (float) urCrosses.get(i).position().y()); + bank.setFloat("z", i, (float) urCrosses.get(i).position().z()); + bank.setFloat("x_local", i, (float) urCrosses.get(i).local().x()); + bank.setFloat("y_local", i, (float) urCrosses.get(i).local().y()); + bank.setFloat("z_local", i, (float) urCrosses.get(i).local().z()); + bank.setShort("cluster1", i, (short) urCrosses.get(i).cluster1()); + bank.setShort("cluster2", i, (short) urCrosses.get(i).cluster2()); + bank.setShort("status", i, (short) urCrosses.get(i).status()); + } + + return bank; + } + /** * * @param event the EvioEvent @@ -729,6 +808,20 @@ private DataBank fillTBTracksBank(DataEvent event, List candlist) { bank.setFloat("t1_py", i, (float) candlist.get(i).get_Region1TrackP().y()); bank.setFloat("t1_pz", i, (float) candlist.get(i).get_Region1TrackP().z()); } + if(candlist.get(i).get_URWellPointGlobal() != null && candlist.get(i).get_URWellPointLocal() != null){ + bank.setFloat("URWell_x", i, (float) candlist.get(i).get_URWellPointGlobal().x()); + bank.setFloat("URWell_y", i, (float) candlist.get(i).get_URWellPointGlobal().y()); + bank.setFloat("URWell_z", i, (float) candlist.get(i).get_URWellPointGlobal().z()); + bank.setFloat("URWell_px", i, (float) candlist.get(i).get_URWellPGlobal().x()); + bank.setFloat("URWell_py", i, (float) candlist.get(i).get_URWellPGlobal().y()); + bank.setFloat("URWell_pz", i, (float) candlist.get(i).get_URWellPGlobal().z()); + bank.setFloat("URWell_x_local", i, (float) candlist.get(i).get_URWellPointLocal().x()); + bank.setFloat("URWell_y_local", i, (float) candlist.get(i).get_URWellPointLocal().y()); + bank.setFloat("URWell_z_local", i, (float) candlist.get(i).get_URWellPointLocal().z()); + bank.setFloat("URWell_px_local", i, (float) candlist.get(i).get_URWellPLocal().x()); + bank.setFloat("URWell_py_local", i, (float) candlist.get(i).get_URWellPLocal().y()); + bank.setFloat("URWell_pz_local", i, (float) candlist.get(i).get_URWellPLocal().z()); + } bank.setFloat("pathlength", i, (float) candlist.get(i).get_TotPathLen()); bank.setFloat("Vtx0_x", i, (float) candlist.get(i).get_Vtx0().x()); @@ -738,6 +831,10 @@ private DataBank fillTBTracksBank(DataEvent event, List candlist) { bank.setFloat("p0_y", i, (float) candlist.get(i).get_pAtOrig().y()); bank.setFloat("p0_z", i, (float) candlist.get(i).get_pAtOrig().z()); //fill associated IDs + if(candlist.get(i).get_URWellCross() != null) + bank.setShort("URWellCross_ID", i, (short) candlist.get(i).get_URWellCross().id()); + else + bank.setShort("URWellCross_ID", i, (short)-1); for(int r = 0; r < 3; r++) { bank.setShort("Cross"+String.valueOf(r+1)+"_ID", i, (short) -1); @@ -864,9 +961,94 @@ else if (fhits != null && clusters == null) { ); } } + + public void fillAllHBBanks(DataEvent event, List fhits, List clusters, + List segments, List crosses, List urCrosses, + List trkcands) { + + if (event == null) { + return; + } + + if (trkcands != null) { + event.appendBanks(this.fillHBHitsBank(event, fhits), + this.fillHBClustersBank(event, clusters), + this.fillHBSegmentsBank(event, segments), + this.fillHBCrossesBank(event, crosses), + this.fillHBURWellCrossesBank(event, urCrosses), + this.fillHBTracksBank(event, trkcands), + this.fillHBHitsTrkIdBank(event, fhits) + //this.fillTrackCovMatBank(event, trkcands) + ); + + } + else if (crosses != null && trkcands == null) { + event.appendBanks(this.fillHBHitsBank(event, fhits), + this.fillHBClustersBank(event, clusters), + this.fillHBSegmentsBank(event, segments), + this.fillHBCrossesBank(event, crosses) + ); + } + else if (segments != null && crosses == null) { + event.appendBanks(this.fillHBHitsBank(event, fhits), + this.fillHBClustersBank(event, clusters), + this.fillHBSegmentsBank(event, segments) + ); + } + else if (clusters != null && segments == null) { + + event.appendBanks(this.fillHBHitsBank(event, fhits), + this.fillHBClustersBank(event, clusters) + ); + } + else if (fhits != null && clusters == null) { + event.appendBanks(this.fillHBHitsBank(event, fhits) + ); + } + } public void fillAllTBBanks(DataEvent event, List fhits, List clusters, - List segments, List crosses, + List segments, List crosses, + List trkcands) { + + if (event == null) { + return; + } + + if (trkcands != null) { + event.appendBanks(this.fillTBHitsBank(event, fhits), + this.fillTBClustersBank(event, clusters), + this.fillTBSegmentsBank(event, segments), + this.fillTBCrossesBank(event, crosses), + this.fillTBTracksBank(event, trkcands), + this.fillTrajectoryBank(event, trkcands), + this.fillTrackCovMatBank(event, trkcands) + ); + } + if (crosses != null && trkcands == null) { + event.appendBanks(this.fillTBHitsBank(event, fhits), + this.fillTBClustersBank(event, clusters), + this.fillTBSegmentsBank(event, segments), + this.fillTBCrossesBank(event, crosses)); + } + if (segments != null && crosses == null) { + event.appendBanks(this.fillTBHitsBank(event, fhits), + this.fillTBClustersBank(event, clusters), + this.fillTBSegmentsBank(event, segments)); + } + + if (clusters != null && segments == null) { + event.appendBanks(this.fillTBHitsBank(event, fhits), + this.fillTBClustersBank(event, clusters)); + } + + if (fhits != null && clusters == null) { + event.appendBanks(this.fillTBHitsBank(event, fhits)); + } + } + + public void fillAllTBBanks(DataEvent event, List fhits, List clusters, + List segments, List crosses, List urCrosses, List trkcands) { if (event == null) { @@ -878,6 +1060,7 @@ public void fillAllTBBanks(DataEvent event, List fhits, ListConstants.HTCCRADIUS && this._trakOrig.z()>0) isGood=false; return isGood; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java new file mode 100755 index 000000000..4bbcd1753 --- /dev/null +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -0,0 +1,1447 @@ +package org.jlab.rec.dc.track; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.jlab.clas.clas.math.FastMath; +import org.jlab.clas.swimtools.Swim; +import org.jlab.detector.geant4.v2.DCGeant4Factory; +import org.jlab.geom.prim.Line3D; +import org.jlab.geom.prim.Point3D; +import org.jlab.geom.prim.Vector3D; +import org.jlab.jnp.matrix.Matrix; +import org.jlab.rec.dc.Constants; +import org.jlab.rec.dc.cross.Cross; +import org.jlab.rec.dc.cross.CrossList; +import org.jlab.rec.dc.hit.FittedHit; +import org.jlab.rec.dc.segment.Segment; +import org.jlab.rec.dc.trajectory.StateVec; +import org.jlab.rec.dc.trajectory.Trajectory; +import org.jlab.rec.dc.trajectory.TrajectoryFinder; + +import trackfitter.fitter.LineFitPars; +import trackfitter.fitter.LineFitter; + +import org.jlab.clas.tracking.kalmanfilter.Surface; +import org.jlab.clas.tracking.kalmanfilter.zReference.KFitterWithURWell; +import org.jlab.clas.tracking.kalmanfilter.zReference.StateVecs; +import org.jlab.clas.tracking.utilities.MatrixOps.Libr; +import org.jlab.clas.tracking.utilities.RungeKuttaDoca; +import org.jlab.rec.urwell.reader.URWellCross; + +/** + * A class with a method implementing an algorithm that finds lists of track + * candidates in the DC + * + * @author ziegler, tongtong + */ + +public class TrackCandListWithURWellFinder { + + private static final Logger LOGGER = Logger.getLogger(TrackCandListFinder.class.getName()); + + long startTime, startTime2 = 0; + + /** + * the tracking status = HitBased or TimeBased + */ + private String trking; + + /** + * @param stat the tracking status Hit-based or Time-based + */ + public TrackCandListWithURWellFinder(String stat) { + trking = stat; + } + + /** + * @param crossesInTrk the list of crosses on track + * @return the number of superlayers used in the fit + */ + private boolean PassNSuperlayerTracking(List crossesInTrk, Track cand) { + boolean pass = true; + int NbMissingSl = 0; // find the missing superlayers from the pseudo-crosses + for (Cross c : crossesInTrk) { + if (c.isPseudoCross) { + if ((c.get_Segment1().get_Id() == -1) || (c.get_Segment2().get_Id() == -1)) { + NbMissingSl++; + } + if (c.get_Segment1().get_Id() == -1) { + cand.set_MissingSuperlayer(c.get_Segment1().get_Superlayer()); + cand.setSingleSuperlayer(c.get_Segment2()); + } + if (c.get_Segment2().get_Id() == -1) { + cand.set_MissingSuperlayer(c.get_Segment2().get_Superlayer()); + cand.setSingleSuperlayer(c.get_Segment1()); + } + } else { + if ((c.get_Segment1().get_Status() == 1) || (c.get_Segment2().get_Status() == 1)) { + cand.set_Status(1); + } + } + } + // if more superlayers are missing than the required number in the analysis - skip the track + if (NbMissingSl > 6 - Constants.getInstance().NSUPERLAYERTRACKING) { + pass = false; + } + return pass; + } + + private double getHitBasedFitChi2ToCrosses(int sector, double x1, double y1, double z1, + double x2, double y2, double z2, double x3, + double y3, double z3, double p, int q, double x, + double y, double z, double tanThX, double tanThY, Swim dcSwim) { + double pz = p / Math.sqrt(tanThX * tanThX + tanThY * tanThY + 1); + + dcSwim.SetSwimParameters(x, y, z, + -pz * tanThX, -pz * tanThY, -pz, + -q); + double chi2 = 0; // assume err =1 on points + double[] R = dcSwim.SwimToPlaneTiltSecSys(sector, z3); + if (R == null) { + return Double.POSITIVE_INFINITY; + } + + //chi2 += (R[0] - x3) * (R[0] - x3) + (R[1] - y3) * (R[1] - y3); + chi2 += (Math.sqrt(R[0] * R[0] + R[1] * R[1]) - Math.sqrt(x3 * x3 + y3 * y3)) * (Math.sqrt(R[0] * R[0] + R[1] * R[1]) - Math.sqrt(x3 * x3 + y3 * y3)); + dcSwim.SetSwimParameters(R[0], R[1], R[2], + R[3], R[4], R[5], + -q); + R = dcSwim.SwimToPlaneTiltSecSys(sector, z2); + if (R == null) { + return Double.POSITIVE_INFINITY; + } + + dcSwim.SetSwimParameters(R[0], R[1], R[2], + R[3], R[4], R[5], + -q); + chi2 += (R[0] - x2) * (R[0] - x2) + (R[1] - y2) * (R[1] - y2); + dcSwim.SetSwimParameters(R[0], R[1], R[2], + R[3], R[4], R[5], + -q); + R = dcSwim.SwimToPlaneTiltSecSys(sector, z1); + if (R == null) { + return Double.POSITIVE_INFINITY; + } + + chi2 += (R[0] - x1) * (R[0] - x1) + (R[1] - y1) * (R[1] - y1); + + return chi2; + } + + private double[] getTrackInitFit(int sector, double x1, double y1, double z1, + double x2, double y2, double z2, double x3, double y3, double z3, + double ux, double uy, double uz, double thX, double thY, + double theta1, double theta3, + double iBdl, double TORSCALE, Swim dcSwim) { + if (theta1 < -998 || theta3 < -998) { + return new double[]{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; + } + double[] pars = new double[2]; + + double chi2 = 0; // assume err =1 on points + double intBdl = 0; + + double p = calcInitTrkP(ux, uy, uz, thX, thY, + theta1, theta3, + iBdl, TORSCALE); + double p_x = ux * p; + double p_y = uy * p; + double p_z = uz * p; + + int q = calcInitTrkQ(theta1, theta3, TORSCALE); + + dcSwim.SetSwimParameters(x1, y1, z1, p_x, p_y, p_z, q); + double[] R = dcSwim.SwimToPlaneTiltSecSys(sector, z2); + if (R == null) { + return new double[]{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; + } + + chi2 += (R[0] - x2) * (R[0] - x2) + (R[1] - y2) * (R[1] - y2); + intBdl += R[7]; + dcSwim.SetSwimParameters(R[0], R[1], R[2], + R[3], R[4], R[5], + q); + R = dcSwim.SwimToPlaneTiltSecSys(sector, z3); + if (R == null) { + return new double[]{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; + } + + chi2 += (R[0] - x3) * (R[0] - x3) + (R[1] - y3) * (R[1] - y3); + intBdl += R[7]; + + pars[0] = chi2; + pars[1] = intBdl; + + return pars; + } + + private double calcInitTrkP(double ux, double uy, double uz, double thX, double thY, + double theta1, double theta3, + double iBdl, double TORSCALE) { + double deltaTheta = theta3 - theta1; + if (deltaTheta == 0) { + return Double.POSITIVE_INFINITY; + } + + // momentum estimate if Bdl is non zero and the track has curvature + double pxz = Math.abs(Constants.LIGHTVEL * iBdl / deltaTheta); + double py = Math.sqrt((thX * thX + thY * thY + 1) / (thX * thX + 1) - 1) * pxz; + + double p = Math.sqrt(pxz * pxz + py * py); + return p; + } + + private int calcInitTrkQ(double theta1, double theta3, + double TORSCALE) { + double deltaTheta = theta3 - theta1; + + //positive charges bend outward for nominal GEMC field configuration + int q = (int) Math.signum(deltaTheta); + q *= (int) -1 * Math.signum(TORSCALE); // flip the charge according to the field scale + + return q; + } + + /** + * @param crossList the input list of crosses + * @param DcDetector + * @param TORSCALE + * @param dcSwim + * @param donotapplyCuts + * @return a list of track candidates in the DC + */ + public List getTrackCands(URWellDCCrossesList urDCCrossesList, DCGeant4Factory DcDetector, double TORSCALE, Swim dcSwim, + boolean donotapplyCuts) { + + List cands = null; + if (Math.abs(TORSCALE) < 0.001) { + cands = this.findStraightTracks(urDCCrossesList, DcDetector, TORSCALE, dcSwim); + } else { + cands = this.findCurvedTracks(urDCCrossesList, DcDetector, TORSCALE, dcSwim, donotapplyCuts); + } + return cands; + } + + /** + * @param cand straight track candidate + */ + private void getStraightTrack(Track cand) { + + double[] x = new double[3]; + double[] y1 = new double[3]; + double[] y2 = new double[3]; + double[] ex = new double[3]; + double[] ey1 = new double[3]; + double[] ey2 = new double[3]; + + for (int i = 0; i < 3; i++) { + + Point3D X = cand.get(i).getCoordsInLab(cand.get(i).get_Point().x(), + cand.get(i).get_Point().y(), cand.get(i).get_Point().z()); + Point3D eX = cand.get(i).getCoordsInLab(cand.get(i).get_PointErr().x(), + cand.get(i).get_PointErr().y(), cand.get(i).get_PointErr().z()); + + x[i] = X.z(); + ex[i] = eX.z(); + + y1[i] = X.x(); + ey1[i] = eX.x(); + + y2[i] = X.y(); + ey2[i] = eX.y(); + } + + if (x != null) { + + LineFitter linefit = new LineFitter(); + boolean linefitstatusOK1 = linefit.fitStatus(x, y1, ex, ey1, 3); + + LineFitPars FitPars1 = null; + LineFitPars FitPars2 = null; + if (linefitstatusOK1) // Get the results of the fits + { + FitPars1 = linefit.getFit(); + } + + boolean linefitstatusOK2 = linefit.fitStatus(x, y2, ex, ey2, 3); + if (linefitstatusOK2) // Get the results of the fits + { + FitPars2 = linefit.getFit(); + } + + double X0 = -99999; + double Y0 = -99999; + + if (FitPars1 != null && FitPars2 != null) { + + X0 = FitPars1.intercept(); + Y0 = FitPars2.intercept(); + Point3D trkR1X = new Point3D(FitPars1.slope() * x[0] + FitPars1.intercept(), + FitPars2.slope() * x[0] + FitPars2.intercept(), x[0]); + Point3D trkR3X = new Point3D(FitPars1.slope() * x[2] + FitPars1.intercept(), + FitPars2.slope() * x[2] + FitPars2.intercept(), x[2]); + + Vector3D trkDir = new Vector3D(trkR3X.x() - trkR1X.x(), + trkR3X.y() - trkR1X.y(), trkR3X.z() - trkR1X.z()).asUnit(); + trkDir.scale(10); + + Point3D trkVtx = new Point3D(X0, Y0, 0); + + cand.set_P(10); + cand.set_Q(-1); // assume it's a muon + cand.set_pAtOrig(trkDir); + cand.set_Vtx0(trkVtx); + cand.set_PreRegion1CrossPoint(new Point3D(trkR1X.x() - trkDir.x(), + trkR1X.y() - trkDir.y(), trkR1X.z() - trkDir.z())); + cand.set_PostRegion3CrossPoint(new Point3D(trkR3X.x() + trkDir.x(), + trkR3X.y() + trkDir.y(), trkR3X.z() + trkDir.z())); + cand.set_PreRegion1CrossDir(new Point3D(trkDir.x(), trkDir.y(), trkDir.z())); + cand.set_PostRegion3CrossDir(new Point3D(trkDir.x(), trkDir.y(), trkDir.z())); + cand.set_Region1TrackX(trkR1X); + cand.set_Region1TrackP(new Point3D(trkDir.x(), trkDir.y(), trkDir.z())); + + cand.setPathLength(trkR3X.distance(trkVtx)); + } + } + } + + /** + * @param x x coordinate in the lab frame + * @param y y coordinate in the lab frame + * @return the sector in the DC lab frame system corresponding to the (x,y) + * coordinates + */ + private int getSector(double x, double y) { + double phi = Math.toDegrees(FastMath.atan2(y, x)); + double ang = phi + 30; + while (ang < 0) { + ang += 360; + } + int sector = 1 + (int) (ang / 60.); + + if (sector == 7) { + sector = 6; + } + + if ((sector < 1) || (sector > 6)) { + System.err.println("Track sector not found...."); + } + return sector; + } + + /** + * @param cand the track candidate + * @param traj the track trajectory + * @param trjFind the track trajectory utility + * @param stateVec the track state vector at the last measurement site used + * by the Kalman Filter + * @param z the z position in the tilted sector coordinate system at the + * last measurement site + * @param getDcDetector the detector geometry + * @param dcSwim + */ + public void setTrackPars(Track cand, + Trajectory traj, + TrajectoryFinder trjFind, + StateVec stateVec, double z, + DCGeant4Factory getDcDetector, + Swim dcSwim) { + double pz = cand.get_P() / Math.sqrt(stateVec.tanThetaX() * stateVec.tanThetaX() + + stateVec.tanThetaY() * stateVec.tanThetaY() + 1); + + //LOGGER.log(Level.FINE, "Setting track params for ");stateVec.printInfo(); + dcSwim.SetSwimParameters(stateVec.x(), stateVec.y(), z, + pz * stateVec.tanThetaX(), pz * stateVec.tanThetaY(), pz, + cand.get_Q()); + + // swimming to a ref points outside of the last DC region + double[] VecAtTarOut = dcSwim.SwimToPlaneTiltSecSys(cand.get(0).get_Sector(), 592); + + if (VecAtTarOut == null) { + return; + } + + double xOuter = VecAtTarOut[0]; + double yOuter = VecAtTarOut[1]; + double zOuter = VecAtTarOut[2]; + double uxOuter = VecAtTarOut[3] / cand.get_P(); + double uyOuter = VecAtTarOut[4] / cand.get_P(); + double uzOuter = VecAtTarOut[5] / cand.get_P(); + //Cross crossR = new Cross(cand.get(2).getSector(), cand.get(2).get_Region(), -1); + Cross crossR = new Cross(cand.get(cand.size() - 1).get_Sector(), + cand.get(cand.size() - 1).get_Region(), -1); + Point3D xOuterExtp = crossR.getCoordsInLab(xOuter, yOuter, zOuter); + Point3D uOuterExtp = crossR.getCoordsInLab(uxOuter, uyOuter, uzOuter); + + //set the pseudocross at extrapolated position + cand.set_PostRegion3CrossPoint(xOuterExtp); + cand.set_PostRegion3CrossDir(uOuterExtp); + + dcSwim.SetSwimParameters(stateVec.x(), stateVec.y(), z, + -pz * stateVec.tanThetaX(), -pz * stateVec.tanThetaY(), -pz, + -cand.get_Q()); + + //swimming to a ref point upstream of the first DC region + double[] VecAtTarIn = dcSwim.SwimToPlaneTiltSecSys(cand.get(0).get_Sector(), 180); + if (VecAtTarIn == null) { + cand.fit_Successful = false; + return; + } + + if (VecAtTarIn[6] + VecAtTarOut[6] < 200) { + cand.fit_Successful = false; + return; + } + + double xOr = VecAtTarIn[0]; + double yOr = VecAtTarIn[1]; + double zOr = VecAtTarIn[2]; + double pxOr = -VecAtTarIn[3]; + double pyOr = -VecAtTarIn[4]; + double pzOr = -VecAtTarIn[5]; + + Cross C = new Cross(cand.get(cand.size() - 1).get_Sector(), cand.get(cand.size() - 1).get_Region(), -1); + + Point3D trkR1X = C.getCoordsInLab(xOr, yOr, zOr); + Point3D trkR1P = C.getCoordsInLab(pxOr, pyOr, pzOr); + cand.set_Region1TrackX(new Point3D(trkR1X.x(), trkR1X.y(), trkR1X.z())); + cand.set_Region1TrackP(new Point3D(trkR1P.x(), trkR1P.y(), trkR1P.z())); + + double[] VecAtURWell = dcSwim.SwimToPlaneTiltSecSys(cand.get(0).get_Sector(), Constants.URWELLLOCALZ); + Point3D uRWellPointLocal = new Point3D(VecAtURWell[0], VecAtURWell[1], VecAtURWell[2]); + Point3D uRWellPLocal = new Point3D(-VecAtURWell[3], -VecAtURWell[4], -VecAtURWell[5]); + Point3D uRWellPointGlobal = C.getCoordsInLab(VecAtURWell[0], VecAtURWell[1], VecAtURWell[2]); + Point3D uRWellPGlobal = C.getCoordsInLab(-VecAtURWell[3], -VecAtURWell[4], -VecAtURWell[5]); + cand.set_URWellPointLocal(uRWellPointLocal); + cand.set_URWellPLocal(uRWellPLocal); + cand.set_URWellPointGlobal(uRWellPointGlobal); + cand.set_URWellPGlobal(uRWellPGlobal); + + + Point3D R3TrkPoint = C.getCoordsInLab(stateVec.x(), stateVec.y(), z); + Point3D R3TrkMomentum = C.getCoordsInLab(pz * stateVec.tanThetaX(), + pz * stateVec.tanThetaY(), pz); + dcSwim.SetSwimParameters(R3TrkPoint.x(), R3TrkPoint.y(), + R3TrkPoint.z(), + -R3TrkMomentum.x(), + -R3TrkMomentum.y(), + -R3TrkMomentum.z(), + -cand.get_Q()); + // recalc new vertex using plane stopper + //int sector = cand.get(2).getSector(); + int sector = cand.get(cand.size() - 1).get_Sector(); + double theta_n = ((double) (sector - 1)) * Math.toRadians(60.); + double x_n = Math.cos(theta_n); + double y_n = Math.sin(theta_n); + double[] Vt = dcSwim.SwimToPlaneBoundary(0, new Vector3D(x_n, y_n, 0), -1); + + if (Vt == null) { + return; + } + + int status = 99999; + + int LR = 0; + for (Cross crs : cand) { + Segment s1 = crs.get_Segment1(); + Segment s2 = crs.get_Segment2(); + + for (FittedHit h : s1) { + LR += h._lr; + } + for (FittedHit h : s2) { + LR += h._lr; + } + + } + + status = LR; + + double xOrFix = Vt[0]; + double yOrFix = Vt[1]; + double zOrFix = Vt[2]; + double pxOrFix = -Vt[3]; + double pyOrFix = -Vt[4]; + double pzOrFix = -Vt[5]; + double PathInFromR3 = Vt[6]; + + //double totPathLen = VecAtTarlab0[6] + VecAtTarOut[6] + arclen; + double totPathLen = PathInFromR3 + VecAtTarOut[6]; + cand.set_TotPathLen(totPathLen); + + cand.set_Vtx0(new Point3D(xOrFix, yOrFix, zOrFix)); + cand.set_pAtOrig(new Vector3D(pxOrFix, pyOrFix, pzOrFix)); + + double[] VecAtHtccSurf = dcSwim.SwimToSphere(175); + double xInner = VecAtHtccSurf[0]; + double yInner = VecAtHtccSurf[1]; + double zInner = VecAtHtccSurf[2]; + double uxInner = VecAtHtccSurf[3] / cand.get_P(); + double uyInner = VecAtHtccSurf[4] / cand.get_P(); + double uzInner = VecAtHtccSurf[5] / cand.get_P(); + + //set the pseudocross at extrapolated position + cand.set_PreRegion1CrossPoint(new Point3D(xInner, yInner, zInner)); + cand.set_PreRegion1CrossDir(new Point3D(uxInner, uyInner, uzInner)); + + cand.fit_Successful = true; + cand.set_TrackingInfoString(trking); + } + + public void setTrackPars(Track cand, + Trajectory traj, + TrajectoryFinder trjFind, + StateVec stateVec, double z, + DCGeant4Factory getDcDetector, + Swim dcSwim, double xB, double yB) { + + double pz = cand.get_P() / Math.sqrt(stateVec.tanThetaX() * stateVec.tanThetaX() + + stateVec.tanThetaY() * stateVec.tanThetaY() + 1); + + //LOGGER.log(Level.FINE, "Setting track params for ");stateVec.printInfo(); + dcSwim.SetSwimParameters(stateVec.x(), stateVec.y(), z, + pz * stateVec.tanThetaX(), pz * stateVec.tanThetaY(), pz, + cand.get_Q()); + + // swimming to a ref points outside of the last DC region + double[] VecAtTarOut = dcSwim.SwimToPlaneTiltSecSys(cand.get(0).get_Sector(), 592); + if(VecAtTarOut==null) { + cand.fit_Successful = false; + return; + } + double xOuter = VecAtTarOut[0]; + double yOuter = VecAtTarOut[1]; + double zOuter = VecAtTarOut[2]; + double uxOuter = VecAtTarOut[3] / cand.get_P(); + double uyOuter = VecAtTarOut[4] / cand.get_P(); + double uzOuter = VecAtTarOut[5] / cand.get_P(); + //Cross crossR = new Cross(cand.get(2).getSector(), cand.get(2).get_Region(), -1); + Cross crossR = new Cross(cand.get(cand.size() - 1).get_Sector(), + cand.get(cand.size() - 1).get_Region(), -1); + Point3D xOuterExtp = crossR.getCoordsInLab(xOuter, yOuter, zOuter); + Point3D uOuterExtp = crossR.getCoordsInLab(uxOuter, uyOuter, uzOuter); + + //set the pseudocross at extrapolated position + cand.set_PostRegion3CrossPoint(xOuterExtp); + cand.set_PostRegion3CrossDir(uOuterExtp); + + dcSwim.SetSwimParameters(stateVec.x(), stateVec.y(), z, + -pz * stateVec.tanThetaX(), -pz * stateVec.tanThetaY(), -pz, + -cand.get_Q()); + + //swimming to a ref point upstream of the first DC region + double[] VecAtTarIn = dcSwim.SwimToPlaneTiltSecSys(cand.get(0).get_Sector(), 180); + if (VecAtTarIn == null) { + cand.fit_Successful = false; + return; + } + + if (VecAtTarIn[6] + VecAtTarOut[6] < Constants.MINPATH) { + cand.fit_Successful = false; + return; + } + + double xOr = VecAtTarIn[0]; + double yOr = VecAtTarIn[1]; + double zOr = VecAtTarIn[2]; + double pxOr = -VecAtTarIn[3]; + double pyOr = -VecAtTarIn[4]; + double pzOr = -VecAtTarIn[5]; + + Cross C = new Cross(cand.get(cand.size() - 1).get_Sector(), cand.get(cand.size() - 1).get_Region(), -1); + + Point3D trkR1X = C.getCoordsInLab(xOr, yOr, zOr); + Point3D trkR1P = C.getCoordsInLab(pxOr, pyOr, pzOr); + cand.set_Region1TrackX(new Point3D(trkR1X.x(), trkR1X.y(), trkR1X.z())); + cand.set_Region1TrackP(new Point3D(trkR1P.x(), trkR1P.y(), trkR1P.z())); + + double[] VecAtURWell = dcSwim.SwimToPlaneTiltSecSys(cand.get(0).get_Sector(), Constants.URWELLLOCALZ); + Point3D uRWellPointLocal = new Point3D(VecAtURWell[0], VecAtURWell[1], VecAtURWell[2]); + Point3D uRWellPLocal = new Point3D(-VecAtURWell[3], -VecAtURWell[4], -VecAtURWell[5]); + Point3D uRWellPointGlobal = C.getCoordsInLab(VecAtURWell[0], VecAtURWell[1], VecAtURWell[2]); + Point3D uRWellPGlobal = C.getCoordsInLab(-VecAtURWell[3], -VecAtURWell[4], -VecAtURWell[5]); + cand.set_URWellPointLocal(uRWellPointLocal); + cand.set_URWellPLocal(uRWellPLocal); + cand.set_URWellPointGlobal(uRWellPointGlobal); + cand.set_URWellPGlobal(uRWellPGlobal); + + Point3D R3TrkPoint = C.getCoordsInLab(stateVec.x(), stateVec.y(), z); + Point3D R3TrkMomentum = C.getCoordsInLab(pz * stateVec.tanThetaX(), + pz * stateVec.tanThetaY(), pz); + dcSwim.SetSwimParameters(R3TrkPoint.x(), R3TrkPoint.y(), + R3TrkPoint.z(), + -R3TrkMomentum.x(), + -R3TrkMomentum.y(), + -R3TrkMomentum.z(), + -cand.get_Q()); + + double[] Vt = dcSwim.SwimToBeamLine(xB, yB); + if(Vt==null) { + cand.fit_Successful = false; + return; + } + + // recalc new vertex using plane stopper + //int sector = cand.get(2).getSector(); + //double[] Vt = null; + //int sector = cand.get(cand.size() - 1).getSector(); + //double theta_n = ((double) (sector - 1)) * Math.toRadians(60.); + //double x_n = Math.cos(theta_n); + //double y_n = Math.sin(theta_n); + //double d = x_n*xB + y_n*yB; + //Vt = dcSwim.SwimToPlaneBoundary(d, new Vector3D(x_n, y_n, 0), -1); + //if(Vt==null) + // return; + double xOrFix = Vt[0]; + double yOrFix = Vt[1]; + double zOrFix = Vt[2]; + double pxOrFix = -Vt[3]; + double pyOrFix = -Vt[4]; + double pzOrFix = -Vt[5]; + + double PathInFromR3 = Vt[6]; + + double totPathLen = PathInFromR3 + VecAtTarOut[6]; + cand.set_TotPathLen(totPathLen); + + cand.set_Vtx0(new Point3D(xOrFix, yOrFix, zOrFix)); + + cand.set_pAtOrig(new Vector3D(pxOrFix, pyOrFix, pzOrFix)); + + cand.fit_Successful = true; + cand.set_TrackingInfoString(trking); + + dcSwim.SetSwimParameters(xOrFix, yOrFix, zOrFix, + pxOrFix, pyOrFix, pzOrFix, + cand.get_Q()); + double[] VecAtHtccSurf = dcSwim.SwimToSphere(175); + if(VecAtHtccSurf==null) { + cand.fit_Successful = false; + return; + } + double xInner = VecAtHtccSurf[0]; + double yInner = VecAtHtccSurf[1]; + double zInner = VecAtHtccSurf[2]; + double uxInner = VecAtHtccSurf[3] / cand.get_P(); + double uyInner = VecAtHtccSurf[4] / cand.get_P(); + double uzInner = VecAtHtccSurf[5] / cand.get_P(); + + //set the pseudocross at extrapolated position + cand.set_PreRegion1CrossPoint(new Point3D(xInner, yInner, zInner)); + cand.set_PreRegion1CrossDir(new Point3D(uxInner, uyInner, uzInner)); + } + + private Integer getKey(Track trk) { + return trk.get(0).get_Id() * 1000000 + + trk.get(1).get_Id() * 1000 + + trk.get(2).get_Id(); + } + + public void removeOverlappingTracksOld(List trkcands) { + if(Constants.DEBUG) { + LOGGER.log(Level.FINE, "Found "+trkcands.size()+" HB seeds "); + for(int i = 0; i< trkcands.size(); i++) { + LOGGER.log(Level.FINE, "cand "+i); + for(Cross c : trkcands.get(i)) { + LOGGER.log(Level.FINE, c.printInfo()); + } + LOGGER.log(Level.FINE, "------------------------------------------------------------------ "); + } + } + Map selectedTracksMap = new HashMap<>(); + List list = new ArrayList<>(); + int size = trkcands.size(); + for (int i = 0; i < size; i++) { + list.clear(); + this.getOverlapLists(trkcands.get(i), trkcands, list); + Track selectedTrk = this.FindBestTrack(list); + + if (selectedTrk == null) { + continue; + } + selectedTracksMap.put(this.getKey(selectedTrk), selectedTrk); + } + + trkcands.removeAll(trkcands); + selectedTracksMap.entrySet().forEach(entry -> { + trkcands.add(entry.getValue()); + }); + if(Constants.DEBUG) { + LOGGER.log(Level.FINE, "After Overlap Remvr "+trkcands.size()+" HB seeds "); + for(int i = 0; i< trkcands.size(); i++) { + LOGGER.log(Level.FINE, "cand "+i); + for(Cross c : trkcands.get(i)) { + LOGGER.log(Level.FINE, c.printInfo()); + } + LOGGER.log(Level.FINE, "------------------------------------------------------------------ "); + } + } + } + + public void removeOverlappingTracks(List trkcands) { + if(Constants.DEBUG) { + LOGGER.log(Level.FINE, "Found "+trkcands.size()+" HB seeds "); + for(int i = 0; i< trkcands.size(); i++) { + LOGGER.log(Level.FINE, "cand "+i); + for(Cross c : trkcands.get(i)) { + LOGGER.log(Level.FINE, c.printInfo()); + } + LOGGER.log(Level.FINE, "------------------------------------------------------------------ "); + } + } + List badTracks = new ArrayList<>(); + for(Track t : trkcands) if(!t.isGood()) badTracks.add(t); + trkcands.removeAll(badTracks); + + List selectedTracks = new ArrayList<>(); + for (int i = 0; i < trkcands.size(); i++) { + boolean overlap = false; + Track t1 = trkcands.get(i); + for(int j=0; jt2.get_FitChi2()) + overlap=true; + else if(t1.get_FitChi2()==t2.get_FitChi2() && i>j) + overlap=true; + } +// LOGGER.log(Level.FINE, overlap); + } + if(!overlap) selectedTracks.add(t1); + } + trkcands.removeAll(trkcands); + trkcands.addAll(selectedTracks); + } + + /** + * @param selectedTracks the list of selected tracks + * @param selectedTrk the selected track + * @return a boolean indicating if the track is in the list + */ + private boolean ListContainsTrack(List selectedTracks, Track selectedTrk) { + boolean isInList = false; + for (Track trk : selectedTracks) { + if (trk == null) { + continue; + } + if (trk.get_Id() == selectedTrk.get_Id()) { + isInList = true; + } + } + return isInList; + } + + /** + * @param track the track + * @param trkcands the list of candidates + * @param list the list of selected tracks + */ + private void getOverlapLists(Track track, List trkcands, List list) { + for (int i = 0; i < trkcands.size(); i++) { + if ((track.get(0).get_Id() != -1 && track.get(0).get_Id() == trkcands.get(i).get(0).get_Id()) + || (track.get(1).get_Id() != -1 && track.get(1).get_Id() == trkcands.get(i).get(1).get_Id()) + || (track.get(2).get_Id() != -1 && track.get(2).get_Id() == trkcands.get(i).get(2).get_Id())) { + list.add(trkcands.get(i)); + + } + } + } + + /** + * @param trkList the list of tracks + * @return the track with the best chi2 from the list + */ + private Track FindBestTrack(List trkList) { + double bestChi2 = 9999999; + Track bestTrk = null; + + for (int i = 0; i < trkList.size(); i++) { + if (trkList.get(i).get_FitChi2() < bestChi2) { + bestChi2 = trkList.get(i).get_FitChi2(); + bestTrk = trkList.get(i); + } + } + return bestTrk; + } + + public void matchHits(List stateVecAtPlanesList, Track trk, + DCGeant4Factory DcDetector, Swim dcSwim) { + + if (stateVecAtPlanesList == null || trk.get_Vtx0()==null) { + return; + } + + List fhits = new ArrayList<>(); + + dcSwim.SetSwimParameters(trk.get_Vtx0().x(), + trk.get_Vtx0().y(), trk.get_Vtx0().z(), trk.get_pAtOrig().x(), + trk.get_pAtOrig().y(), trk.get_pAtOrig().z(), trk.get_Q()); + double[] ToFirstMeas = dcSwim.SwimToPlaneTiltSecSys(trk.get(0).get_Sector(), + stateVecAtPlanesList.get(0).getZ()); + if (ToFirstMeas == null) { + return; + } + + for (StateVec st : stateVecAtPlanesList) { + if (st == null) { + return; + } + + for (Cross c : trk) { + c.get_Segment1().isOnTrack=true; + c.get_Segment2().isOnTrack=true; + for (FittedHit h1 : c.get_Segment1()) { + if (Math.abs(st.getZ() - h1.get_Z()) < 0.1 && c.get_Segment1().get_Id() > -1 + && (h1.get_XWire() - st.getProjector()) < 0.1) { + + h1.set_AssociatedHBTrackID(trk.get_Id()); + h1.updateHitfromSV(st, DcDetector); + fhits.add(h1); + } + } + for (FittedHit h1 : c.get_Segment2()) { + if (Math.abs(st.getZ() - h1.get_Z()) < 0.1 && c.get_Segment2().get_Id() > -1 + && (h1.get_XWire() - st.getProjector()) < 0.1) { + + h1.set_AssociatedHBTrackID(trk.get_Id()); + h1.updateHitfromSV(st, DcDetector); + fhits.add(h1); + } + } + } + } + trk.setHitsOnTrack(fhits); + } + + public void setHitDoubletsInfo(Segment seg) { + if(seg.isOnTrack) { + for(FittedHit h : seg) { + // for hits with no timing information, check if there is a neighbor hit + // in the same layer and copy time info from there + // currently disabled to avoid fake doublets + if((h.get_AssociatedHBTrackID()==-1 || h.getTFlight()==0) && Constants.getInstance().USEDOUBLETS()) { + for(FittedHit o :seg) { + if(h.get_Id()!=o.get_Id() && + o.get_AssociatedHBTrackID()>0 && + o.getTFlight()>0 && + h.get_Layer()==o.get_Layer() && + Math.abs(h.get_Wire()-o.get_Wire())==1) { + h.set_AssociatedHBTrackID(o.get_AssociatedHBTrackID()); + h.setB(o.getB()); + h.setTProp(o.getTProp()); + h.setTFlight(o.getTFlight()); + h.set_QualityFac(1); + } + } + } + // if time info is still not available, reset the hit trkID + if(h.get_AssociatedHBTrackID()>0 && h.getTFlight()==0) + h.set_AssociatedHBTrackID(-1); + } + } + } + + private double calcCurvSign(Track cand) { + double P0x = cand.get(0).get_Point().z(); + double P1x = cand.get(1).get_Point().z(); + double P2x = cand.get(2).get_Point().z(); + double P0y = cand.get(0).get_Point().x(); + double P1y = cand.get(1).get_Point().x(); + double P2y = cand.get(2).get_Point().x(); + + if (Math.abs(P1x - P0x) < 1.0e-18 || Math.abs(P2x - P1x) < 1.0e-18) { + return 0.0; + } + + // Find the intersection of the lines joining the innermost to middle and middle to outermost point + double ma = (P1y - P0y) / (P1x - P0x); + double mb = (P2y - P1y) / (P2x - P1x); + + if (Math.abs(mb - ma) < 1.0e-18) { + return 0.0; + } + + double xcen = 0.5 * (ma * mb * (P0y - P2y) + mb * (P0x + P1x) - ma * (P1x + P2x)) / (mb - ma); + double ycen = (-1. / mb) * (xcen - 0.5 * (P1x + P2x)) + 0.5 * (P1y + P2y); + + return Math.signum(ycen); + } + + private List findStraightTracks(URWellDCCrossesList urDCCrossesList, DCGeant4Factory DcDetector, double TORSCALE, Swim dcSwim) { + + if(LOGGER.getLevel()==Level.FINE) { + startTime2 = System.currentTimeMillis(); + } + + List cands = new ArrayList<>(); + if (urDCCrossesList.get_URWellDCCrossesList().isEmpty()) { + return cands; + } + + for (int i = 0; i < urDCCrossesList.get_URWellDCCrossesList().size(); i++) { + + List aCrossList = urDCCrossesList.get_URWellDCCrossesList().get(i).get_DCCrosses(); + //initialize + Track cand = new Track(); + TrajectoryFinder trjFind = new TrajectoryFinder(); + + if(LOGGER.getLevel()==Level.FINE) { + startTime = System.currentTimeMillis(); + } + Trajectory traj = trjFind.findTrajectory(aCrossList, DcDetector, dcSwim); + LOGGER.log(Level.FINE, "Trajectory finding = " + (System.currentTimeMillis() - startTime)); + + + if (traj == null) { + continue; + } + + //look for straight tracks + if (aCrossList.size() == 3 && Math.abs(TORSCALE) < 0.001) { + cand.addAll(aCrossList); + cand.setSector(aCrossList.get(0).get_Sector()); + //no field --> fit straight track + this.getStraightTrack(cand); + if (cand.get_pAtOrig() != null) { + cand.set_Id(cands.size() + 1); + // the state vector at the region 1 cross + StateVec VecAtReg1MiddlePlane = new StateVec(cand.get(0).get_Point().x(), + cand.get(0).get_Point().y(), + cand.get(0).get_Dir().x() / cand.get(0).get_Dir().z(), + cand.get(0).get_Dir().y() / cand.get(0).get_Dir().z()); + cand.set_StateVecAtReg1MiddlePlane(VecAtReg1MiddlePlane); + + URWellCross urCross = urDCCrossesList.get_URWellDCCrossesList().get(i).get_URWellCross(); + cand.set_URWellCross(urCross); + + LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); + + KFitterWithURWell kFZRef = new KFitterWithURWell(true, 1, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(cand, DcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(cand, measSurfaces.get(0).z, 0, initSV, dcSwim, new float[3]); + kFZRef.init(measSurfaces, initSV); + + kFZRef.runFitter(); + + if (kFZRef.finalStateVec == null) { + continue; + } + + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + StateVec fn = new StateVec(); + if (!kFZRef.setFitFailed && kFZRef.finalStateVec != null) { + fn.set(kFZRef.finalStateVec.x, kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); + fn.setZ(kFZRef.finalStateVec.z); + cand.setFinalStateVec(fn); + + // set the track parameters + cand.set_P(1. / Math.abs(kFZRef.finalStateVec.Q)); + cand.set_Q((int) Math.signum(kFZRef.finalStateVec.Q)); + this.setTrackPars(cand, traj, trjFind, fn, kFZRef.finalStateVec.z, DcDetector, dcSwim); + + if (cand.fit_Successful == true) { + // candidate parameters + cand.set_FitChi2(kFZRef.chi2); + cand.set_FitNDF(kFZRef.NDF); + cand.set_FitConvergenceStatus(kFZRef.ConvStatus); + cand.set_Id(cands.size() + 1); + cand.set_CovMat(kFZRef.finalStateVec.CM); + cand.setStateVecs(kfStateVecsAlongTrajectory); + + // add candidate to list of tracks + cands.add(cand); + } + } + } + } + } + return cands; + } + + private List findCurvedTracks(URWellDCCrossesList urDCCrossesList, DCGeant4Factory DcDetector, double TORSCALE, Swim dcSwim, + boolean donotapplyCuts) { + + if(LOGGER.getLevel()==Level.FINE) { + startTime2 = System.currentTimeMillis(); + } + + List cands = new ArrayList(); + if (urDCCrossesList.get_URWellDCCrossesList().isEmpty()) { + return cands; + } + for (int i = 0; i < urDCCrossesList.get_URWellDCCrossesList().size(); i++) { + + List aCrossList = urDCCrossesList.get_URWellDCCrossesList().get(i).get_DCCrosses(); + aCrossList.sort(Comparator.comparing(Cross::get_Sector).thenComparing(Cross::get_Region)); + if (aCrossList.size() < 3) { + // return cands; + continue; + } + //initialize + Track cand = new Track(); + TrajectoryFinder trjFind = new TrajectoryFinder(); + + if(LOGGER.getLevel()==Level.FINE) { + startTime = System.currentTimeMillis(); + } + Trajectory traj = trjFind.findTrajectory(aCrossList, DcDetector, dcSwim); + + LOGGER.log(Level.FINE, "Trajectory finding = " + (System.currentTimeMillis() - startTime)); + + + if (traj == null) { + continue; + } + + if (aCrossList.size() == 3 && this.PassNSuperlayerTracking(aCrossList, cand)) { + cand.addAll(aCrossList); + cand.setSector(aCrossList.get(0).get_Sector()); + + // set the candidate trajectory using the parametrization of the track trajectory + // and estimate intefral Bdl along that path + cand.setStateVecs(traj.getStateVecs()); + cand.setIntegralBdl(traj.getIntegralBdl()); + + //require 3 crosses to make a track (allows for 1 pseudo-cross) + if (cand.size() == 3) { + // LOGGER.log(Level.FINE, "---- cand in sector " + aCrossList.get(0).getSector()); + // LOGGER.log(Level.FINE, aCrossList.get(0).printInfo()); + // LOGGER.log(Level.FINE, aCrossList.get(1).printInfo()); + // LOGGER.log(Level.FINE, aCrossList.get(2).printInfo()); + // LOGGER.log(Level.FINE, "---------------"); + double x1 = aCrossList.get(0).get_Point().x(); + double y1 = aCrossList.get(0).get_Point().y(); + double z1 = aCrossList.get(0).get_Point().z(); + double x2 = aCrossList.get(1).get_Point().x(); + double y2 = aCrossList.get(1).get_Point().y(); + double z2 = aCrossList.get(1).get_Point().z(); + double x3 = aCrossList.get(2).get_Point().x(); + double y3 = aCrossList.get(2).get_Point().y(); + double z3 = aCrossList.get(2).get_Point().z(); + double ux = aCrossList.get(0).get_Dir().x(); + double uy = aCrossList.get(0).get_Dir().y(); + double uz = aCrossList.get(0).get_Dir().z(); + double thX = ux / uz; + double thY = uy / uz; + double theta3s2 = Math.atan(cand.get(2).get_Segment2().get_fittedCluster().get_clusterLineFitSlope()); + double theta1s2 = Math.atan(cand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitSlope()); + double theta3s1 = Math.atan(cand.get(2).get_Segment1().get_fittedCluster().get_clusterLineFitSlope()); + double theta1s1 = Math.atan(cand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitSlope()); + + if (cand.get(0).get_Segment2().get_Id() == -1) { + theta1s2 = theta1s1; //do not use + } //theta1s2=-999; //do not use + if (cand.get(0).get_Segment1().get_Id() == -1) { + theta1s1 = theta1s2; + } + //theta1s1=-999; + if (cand.get(2).get_Segment2().get_Id() == -1) { + theta3s2 = theta3s1; + } + //theta3s2=-999; + if (cand.get(2).get_Segment1().get_Id() == -1) { + theta3s1 = theta3s2; + } + //theta3s1=-999; + double theta3 = 0; + double theta1 = 0; + + double chisq = Double.POSITIVE_INFINITY; + double chi2; + double iBdl = traj.getIntegralBdl(); + double[] pars; + + if(LOGGER.getLevel()==Level.FINE) { + startTime = System.currentTimeMillis(); + } + pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, + ux, uy, uz, thX, thY, + theta1s1, theta3s1, + traj.getIntegralBdl(), TORSCALE, dcSwim); + chi2 = pars[0]; + if (chi2 < chisq) { + chisq = chi2; + theta1 = theta1s1; + theta3 = theta3s1; + iBdl = pars[1]; + } + + LOGGER.log(Level.FINE, "TrackInitFit-1 = " + (System.currentTimeMillis() - startTime)); + + if(LOGGER.getLevel()==Level.FINE) { + startTime = System.currentTimeMillis(); + } + pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, + ux, uy, uz, thX, thY, + theta1s1, theta3s2, + traj.getIntegralBdl(), TORSCALE, dcSwim); + chi2 = pars[0]; + if (chi2 < chisq) { + chisq = chi2; + theta1 = theta1s1; + theta3 = theta3s2; + iBdl = pars[1]; + } + + LOGGER.log(Level.FINE, "TrackInitFit-2 = " + (System.currentTimeMillis() - startTime)); + + if(LOGGER.getLevel()==Level.FINE) { + startTime = System.currentTimeMillis(); + } + pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, + ux, uy, uz, thX, thY, + theta1s2, theta3s1, + traj.getIntegralBdl(), TORSCALE, dcSwim); + chi2 = pars[0]; + if (chi2 < chisq) { + chisq = chi2; + theta1 = theta1s2; + theta3 = theta3s1; + iBdl = pars[1]; + } + + LOGGER.log(Level.FINE, "TrackInitFit-3 = " + (System.currentTimeMillis() - startTime)); + + if(LOGGER.getLevel()==Level.FINE) { + startTime = System.currentTimeMillis(); + } + pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, + ux, uy, uz, thX, thY, + theta1s2, theta3s2, + traj.getIntegralBdl(), TORSCALE, dcSwim); + chi2 = pars[0]; + if (chi2 < chisq) { + theta1 = theta1s2; + theta3 = theta3s2; + iBdl = pars[1]; + } + + LOGGER.log(Level.FINE, "TrackInitFit-4 = " + (System.currentTimeMillis() - startTime)); + + if (chi2 > Constants.SEEDCUT && donotapplyCuts == false) { + continue; + } + // compute delta theta using the non-pseudo segments in region 1 and 3 + + // get integral Bdl from the swimmer trajectory + //double iBdl = traj.getIntegralBdl(); + if (iBdl != 0) { + // momentum estimate if Bdl is non zero and the track has curvature + double p = calcInitTrkP(ux, uy, uz, thX, thY, + theta1, theta3, + iBdl, TORSCALE); + if(LOGGER.getLevel()==Level.FINE) { + startTime = System.currentTimeMillis(); + } + int q = this.calcInitTrkQ(theta1, theta3, TORSCALE); + + LOGGER.log(Level.FINE, "calcInitTrkQ = " + (System.currentTimeMillis() - startTime)); + + if (p > 11) { + p = 11; + } + //if(p>Constants.MAXTRKMOM || p< Constants.MINTRKMOM) + // continue; + + cand.set_Q(q); + // momentum correction using the swam trajectory iBdl + cand.set_P(p); + + // the state vector at the region 1 cross + StateVec VecAtReg1MiddlePlane = new StateVec(cand.get(0).get_Point().x(), + cand.get(0).get_Point().y(), + cand.get(0).get_Dir().x() / cand.get(0).get_Dir().z(), + cand.get(0).get_Dir().y() / cand.get(0).get_Dir().z()); + cand.set_StateVecAtReg1MiddlePlane(VecAtReg1MiddlePlane); + + URWellCross urCross = urDCCrossesList.get_URWellDCCrossesList().get(i).get_URWellCross(); + cand.set_URWellCross(urCross); + + StateVec fitStateVec = null; + // prefer to initialize the seed with region 2 cross due to higher background in region 1 + int crossIdxinList = 1; + if (cand.get(1).isPseudoCross) { + crossIdxinList = 0; + } + + LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); + + KFitterWithURWell kFZRef = new KFitterWithURWell(true, 10, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(cand, DcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(cand, measSurfaces.get(0).z, crossIdxinList, initSV, dcSwim, new float[3]); + kFZRef.init(measSurfaces, initSV); + + kFZRef.runFitter(); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + if (kFZRef.finalStateVec == null) { + continue; + } else { + if (kFZRef.chi2 < Constants.MAXCHI2) { + + fitStateVec = new StateVec(kFZRef.finalStateVec.x, + kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); + q = (int) Math.signum(kFZRef.finalStateVec.Q); + p = 1. / Math.abs(kFZRef.finalStateVec.Q); + fitStateVec.setZ(kFZRef.finalStateVec.z); + + //set the track parameters + cand.set_P(p); + cand.set_Q(q); + + // candidate parameters + cand.set_FitChi2(kFZRef.chi2); + cand.set_FitNDF(kFZRef.NDF); + cand.set_FitConvergenceStatus(kFZRef.ConvStatus); + + cand.set_CovMat(kFZRef.finalStateVec.CM); + cand.setStateVecs(kfStateVecsAlongTrajectory); + + cand.setFinalStateVec(fitStateVec); + cand.set_Id(cands.size() + 1); + this.setTrackPars(cand, traj, + trjFind, fitStateVec, + fitStateVec.getZ(), + DcDetector, dcSwim); + // add candidate to list of tracks + if (cand.fit_Successful = true) { + cands.add(cand); + } + } + } + } + } + } + } + return cands; + } + + public List setKFStateVecsAlongTrajectory(KFitterWithURWell kFZRef) { + List kfStateVecsAlongTrajectory = new ArrayList<>(); + + for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + sv.setZ(svc.z); + sv.setB(svc.B); + sv.setPathLength(svc.getPathLength()); + sv.setProjector(svc.getProjector()); + sv.setProjectorDoca(svc.getProjectorDoca()); + kfStateVecsAlongTrajectory.add(sv); + } + + return kfStateVecsAlongTrajectory; + } + + public void getInitState(Track trkcand, double z0, int c, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initStateVec, Swim dcSwim, float[] bf) { + + Point3D trkCrs = trkcand.get(c).get_Point(); + Point3D trkCrsD = trkcand.get(c).get_Dir(); + //org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + initStateVec.x = trkCrs.x(); + initStateVec.y = trkCrs.y(); + initStateVec.z = trkCrs.z(); + initStateVec.tx = trkCrsD.x()/trkCrsD.z(); + initStateVec.ty = trkCrsD.y()/trkCrsD.z(); + initStateVec.Q = trkcand.get_Q() / trkcand.get_P(); + + RungeKuttaDoca rk = new RungeKuttaDoca(); + rk.SwimToZ(trkcand.get(0).get_Sector(), initStateVec, dcSwim, z0, bf); + + //LOGGER.log(Level.FINE, (0)+"] init "+this.trackTraj.get(0).x+","+this.trackTraj.get(0).y+","+ + // this.trackTraj.get(0).z+","+this.trackTraj.get(0).tx+","+this.trackTraj.get(0).ty+" "+1/this.trackTraj.get(0).Q); + double err_sl1 = trkcand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitSlopeErr(); + double err_sl2 = trkcand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitSlopeErr(); + double err_it1 = trkcand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitInterceptErr(); + double err_it2 = trkcand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitInterceptErr(); + double wy_over_wx = (FastMath.cos(Math.toRadians(6.)) / FastMath.sin(Math.toRadians(6.))); + + double eux = 0.5 * Math.sqrt(err_sl1 * err_sl1 + err_sl2 * err_sl2); + double euy = 0.5 * wy_over_wx * Math.sqrt(err_sl1 * err_sl1 + err_sl2 * err_sl2); + double z = trkcand.get(0).get_Point().z(); + double ex = 0.5 * Math.sqrt(err_it1 * err_it1 + err_it2 * err_it2 + z * z * (err_sl1 * err_sl1 + err_sl2 * err_sl2)); + double ey = 0.5 * wy_over_wx * Math.sqrt(err_it1 * err_it1 + err_it2 * err_it2 + z * z * (err_sl1 * err_sl1 + err_sl2 * err_sl2)); + double epSq = 0.001 * trkcand.get_P() * trkcand.get_P(); + + + Matrix initCMatrix = new Matrix(); + initCMatrix.set(ex * ex, 0, 0, 0, 0, + 0, ey * ey, 0, 0, 0, + 0, 0, eux * eux, 0, 0, + 0, 0, 0, euy * euy, 0, + 0, 0, 0, 0, epSq + ); + + + initStateVec.CM = initCMatrix; + } + + public List getMeasSurfaces(Track trkcand, DCGeant4Factory DcDetector) { + + List hOTS = new ArrayList<>(); // the list of hits on track + FittedHit hitOnTrk; + + URWellCross urCross = trkcand.get_URWellCross(); + HitOnTrack urhot = new HitOnTrack(urCross.sector(), urCross.local().x(), urCross.local().y(), urCross.local().z(), 0.02, 0.04); + urhot.isDCHit = false; + hOTS.add(urhot); + + // loops over the regions (1 to 3) and the superlayers in a region (1 to 2) and obtains the hits on track + for (int c = 0; c < 3; c++) { + for (int s = 0; s < 2; s++) { + for (int h = 0; h < trkcand.get(c).get(s).size(); h++) { + // if (trkcand.get(c).get(s).get(h).get_Id() == -1 /*|| trkcand.get(c).get(s).get(h).get_Id() == 0*/) { //PASS1 + if (trkcand.get(c).get(s).get(h).get_Id() == -1 || trkcand.get(c).get(s).get(h).get_Id() == 0) { + continue; + } + trkcand.get(c).get(s).get(h).calc_CellSize(DcDetector); + hitOnTrk = trkcand.get(c).get(s).get(h); + int slayr = trkcand.get(c).get(s).get(h).get_Superlayer(); + + double sl1 = trkcand.get(c).get(s).get_fittedCluster().get_clusterLineFitSlope(); + double it1 = trkcand.get(c).get(s).get_fittedCluster().get_clusterLineFitIntercept(); + + double Z = hitOnTrk.get_Z(); + double X = sl1 * Z + it1; + + HitOnTrack hot = new HitOnTrack(slayr, X, Z, + trkcand.get(c).get(s).get(h).get_WireMaxSag(), + trkcand.get(c).get(s).get(h).get_WireLine() + ); + double err_sl1 = trkcand.get(c).get(s).get_fittedCluster().get_clusterLineFitSlopeErr(); + + double err_it1 = trkcand.get(c).get(s).get_fittedCluster().get_clusterLineFitInterceptErr(); + double err_cov1 = trkcand.get(c).get(s).get_fittedCluster().get_clusterLineFitSlIntCov(); + + hot._Unc[0] = Math.sqrt(err_sl1 * err_sl1 * Z * Z + err_it1 * err_it1); + hot._hitError = err_sl1 * err_sl1 * Z * Z + err_it1 * err_it1 + 2 * Z * err_cov1 + trkcand.get(c).get(s).get(h).get_DocaErr()*trkcand.get(c).get(s).get(h).get_DocaErr(); + //if(trkcand.get(c).get(s).get(h).get_Time()/CCDBConstants.getTMAXSUPERLAYER()[trkcand.get(c).get(s).get(h).get_Sector()-1][trkcand.get(c).get(s).get(h).get_Superlayer()-1]<1.1) + // hot._hitError = 100000; //exclude outoftimers from fit + hot.region = trkcand.get(c).get(s).get(h).get_Region(); + hot.sector = trkcand.get(c).get(s).get(h).get_Sector(); + hot.superlayer = trkcand.get(c).get(s).get(h).get_Superlayer(); + hot.layer = trkcand.get(c).get(s).get(h).get_Layer(); + + hot._doca[0] = trkcand.get(c).get(s).get(h).get_ClusFitDoca(); + + double LR = Math.signum(trkcand.get(c).get(s).get(h).get_XWire()-trkcand.get(c).get(s).get(h).get_X()); + + hot._doca[0]*=LR; + hot._hitError = trkcand.get(c).get(s).get(h).get_DocaErr()*trkcand.get(c).get(s).get(h).get_DocaErr(); + //LOGGER.log(Level.FINE, " Z "+Z+" ferr "+(float)(hot._Unc /(hot._hitError/4.))); + hot._Unc[0] = hot._hitError; + hOTS.add(hot); + + } + } + } + Collections.sort(hOTS); // sort the collection in order of increasing Z value (i.e. going downstream from the target) + // identify double hits and take the average position + for (int i = 0; i < hOTS.size(); i++) { + if (i > 0) { + if (Math.abs(hOTS.get(i - 1)._Z - hOTS.get(i)._Z)<0.01) { + hOTS.get(i - 1)._doca[1] = hOTS.get(i)._doca[0]; + hOTS.get(i - 1)._Unc[1] = hOTS.get(i)._Unc[0]; + hOTS.get(i - 1)._wireLine[1] = hOTS.get(i)._wireLine[0]; + hOTS.remove(i); + hOTS.get(i - 1).nMeas = 2; + } + } + } + + List surfaces = new ArrayList<>(hOTS.size()); + for (int i = 0; i < hOTS.size(); i++) { + if(!hOTS.get(i).isDCHit){ + Surface surf = new Surface(hOTS.get(i).sector, hOTS.get(i)._X, hOTS.get(i)._Y, hOTS.get(i)._Z, hOTS.get(i)._X_err, hOTS.get(i)._Y_err); + surf.region = 0; + surf.setLayer(1); + surfaces.add(i, surf); + } + else{ + Surface surf = new Surface(hOTS.get(i).region, hOTS.get(i)._Z, hOTS.get(i)._X, hOTS.get(i)._tilt, hOTS.get(i)._wireMaxSag, + hOTS.get(i)._doca, hOTS.get(i)._Unc, hOTS.get(i)._hitError, hOTS.get(i)._wireLine); + surf.setSector(hOTS.get(i).sector); + surf.setSuperLayer(hOTS.get(i).superlayer); + surf.setLayer(hOTS.get(i).layer); + surf.setNMeas(hOTS.get(i).nMeas); + surfaces.add(i, surf); + } + } + + return surfaces; + } + + public class HitOnTrack implements Comparable { + + public double _hitError; + public double _X; + public double _Z; + public double[] _Unc = new double[2]; + public double _tilt; + public double[] _doca = new double[2]; + public double _wireMaxSag; + public Line3D[] _wireLine = new Line3D[2]; + public int region; + public int superlayer; + public int sector; + public int layer; + public int nMeas = 1; + + // For URWell + public double _Y; + public double _X_err; + public double _Y_err; + public boolean isDCHit = true; + + public HitOnTrack(int sector, double X, double Y, double Z, double X_err, double Y_err) { + this.sector = sector; + _X = X; + _Y = Y; + _Z = Z; + _X_err = X_err; + _Y_err = Y_err; + } + + public HitOnTrack(int superlayer, double X, double Z, double wiremaxsag, Line3D wireLine) { + _X = X; + _Z = Z; + _wireMaxSag = wiremaxsag; + _wireLine[0] = wireLine; + _doca[0] = -99; + _doca[1] = -99; + _Unc[0] = 1; + _Unc[1] = 1; + + //use stereo angle in fit based on wire direction + _tilt = 90-Math.toDegrees(wireLine.direction().asUnit().angle(new Vector3D(1,0,0))); + } + + @Override + public int compareTo(HitOnTrack o) { + if (this._Z == o._Z) { + return 0; + } + if (this._Z > o._Z) { + return 1; + } else { + return -1; + } + } + } + + +} diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesList.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesList.java new file mode 100644 index 000000000..43be4157f --- /dev/null +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesList.java @@ -0,0 +1,47 @@ +package org.jlab.rec.dc.track; + +import java.util.List; +import java.util.ArrayList; +import org.jlab.rec.dc.cross.CrossList; +import org.jlab.rec.urwell.reader.URWellCross; +import org.jlab.rec.dc.cross.Cross; + +public class URWellDCCrossesList{ + private List urDCCrossesList = new ArrayList(); + + public URWellDCCrossesList(){ + + } + + public List get_URWellDCCrossesList(){ + return urDCCrossesList; + } + + public void set_URWellDCCrossesList(List urDCCrossesList){ + this.urDCCrossesList = urDCCrossesList; + } + + public void add_URWellDCCrosses(URWellCross urCross, List dcCrosses){ + urDCCrossesList.add(new URWellDCCrosses(urCross, dcCrosses)); + } + + public class URWellDCCrosses { + + private URWellCross urCross; + private List dcCrosses = new ArrayList(); + + public URWellDCCrosses(URWellCross urCross, List dcCrosses) { + this.urCross = urCross; + this.dcCrosses = dcCrosses; + } + + URWellCross get_URWellCross(){ + return urCross; + } + + List get_DCCrosses(){ + return dcCrosses; + } + + } +} diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesListFinder.java new file mode 100644 index 000000000..e3c541ad8 --- /dev/null +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesListFinder.java @@ -0,0 +1,30 @@ +package org.jlab.rec.dc.track; + +import java.util.List; +import org.jlab.rec.dc.cross.CrossList; +import org.jlab.rec.urwell.reader.URWellCross; +import org.jlab.rec.dc.cross.Cross; + +public class URWellDCCrossesListFinder { + + public URWellDCCrossesListFinder() { + } + + public URWellDCCrossesList candURWellDCCrossLists(List urCrosses, CrossList crosslist){ + URWellDCCrossesList urDCCrossesList = new URWellDCCrossesList(); + + for(int i = 0; i < urCrosses.size(); i++){ + for(int j = 0; j < crosslist.size(); j++){ + URWellCross urCross = urCrosses.get(i); + List dcCrosses = crosslist.get(j); + + if(urCross.sector() == dcCrosses.get(0).get_Sector() && urCross.sector() == dcCrosses.get(1).get_Sector() && urCross.sector() == dcCrosses.get(2).get_Sector()) + urDCCrossesList.add_URWellDCCrosses(urCross, dcCrosses); + } + } + + return urDCCrossesList; + } + + +} diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java index 36188148c..a475f8cb0 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java @@ -197,7 +197,7 @@ public void calcTrajectory(int trackId, Swim dcSwim, Point3D v, Vector3D p, int double[] htccPars = dcSwim.SwimToSphere(Constants.HTCCRADIUS); if(htccPars==null) return; this.addTrajectoryPoint(htccPars[0], htccPars[1], htccPars[2], htccPars[3], htccPars[4], htccPars[5], htccPars[6], htccPars[7], DetectorType.HTCC, 1); - + //Swim to planes for(int j = 0; j crossSegsNotOnTrack = new ArrayList<>(); - List psegments = new ArrayList<>(); - - for (Cross c : crosses) { - if (!c.get_Segment1().isOnTrack) - crossSegsNotOnTrack.add(c.get_Segment1()); - if (!c.get_Segment2().isOnTrack) - crossSegsNotOnTrack.add(c.get_Segment2()); - } - - RoadFinder rf = new RoadFinder(); - List allRoads = rf.findRoads(segments, Constants.getInstance().dcDetector); - List Segs2Road = new ArrayList<>(); - for (Road r : allRoads) { - Segs2Road.clear(); - int missingSL = -1; - for (int ri = 0; ri < 3; ri++) { - if (r.get(ri).associatedCrossId == -1) { - if (r.get(ri).get_Superlayer() % 2 == 1) { - missingSL = r.get(ri).get_Superlayer() + 1; - } else { - missingSL = r.get(ri).get_Superlayer() - 1; - } - } - } - if(missingSL==-1) - continue; - for (int ri = 0; ri < 3; ri++) { - for (Segment s : crossSegsNotOnTrack) { - if (s.get_Sector() == r.get(ri).get_Sector() && - s.get_Region() == r.get(ri).get_Region() && - s.associatedCrossId == r.get(ri).associatedCrossId && - r.get(ri).associatedCrossId != -1) { - if (s.get_Superlayer() % 2 == missingSL % 2) - Segs2Road.add(s); - } - } - } - if (Segs2Road.size() == 2) { - Segment pSegment = rf.findRoadMissingSegment(Segs2Road, - Constants.getInstance().dcDetector, - r.a); - if (pSegment != null) - psegments.add(pSegment); - } - } - - segments.addAll(psegments); - List pcrosses = crossMake.find_Crosses(segments, Constants.getInstance().dcDetector); - - CrossList pcrosslist = crossLister.candCrossLists(event, pcrosses, - false, - null, - Constants.getInstance().dcDetector, - null, - dcSwim, true); - //pcrosslist.removeDuplicates(crosslist); - - List mistrkcands = trkcandFinder.getTrackCands(pcrosslist, - Constants.getInstance().dcDetector, - Swimmer.getTorScale(), - dcSwim, false); - - // remove overlaps - if (!mistrkcands.isEmpty()) { - trkcandFinder.removeOverlappingTracks(mistrkcands); - for (Track trk : mistrkcands) { - - // reset the id - trk.set_Id(trkId); - trkcandFinder.matchHits(trk.getStateVecs(), - trk, - Constants.getInstance().dcDetector, - dcSwim); - trkId++; - } - } - - trkcands.addAll(mistrkcands); - - LOGGER.log(Level.FINE, "Found after 5STg "+mistrkcands.size()+" HB seeds "); - for(int i = 0; i< trkcands.size(); i++) { - LOGGER.log(Level.FINE, "cand "+i); - for(Cross c : trkcands.get(i)) { - LOGGER.log(Level.FINE, c.printInfo()); - } - LOGGER.log(Level.FINE, "------------------------------------------------------------------ "); - } //gather all the hits for pointer bank creation for (Track trk : trkcands) { diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java new file mode 100644 index 000000000..ea0aeba2b --- /dev/null +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java @@ -0,0 +1,207 @@ +package org.jlab.service.dc; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import org.jlab.clas.swimtools.Swim; +import org.jlab.clas.swimtools.Swimmer; +import org.jlab.io.base.DataEvent; +import org.jlab.rec.dc.Constants; +import org.jlab.rec.dc.banks.HitReader; +import org.jlab.rec.dc.banks.RecoBankWriter; +import org.jlab.rec.dc.cluster.ClusterFinder; +import org.jlab.rec.dc.cluster.ClusterFitter; +import org.jlab.rec.dc.cluster.FittedCluster; +import org.jlab.rec.dc.cross.Cross; +import org.jlab.rec.dc.cross.CrossList; +import org.jlab.rec.dc.cross.CrossListFinder; +import org.jlab.rec.dc.cross.CrossMaker; +import org.jlab.rec.dc.hit.FittedHit; +import org.jlab.rec.dc.segment.Segment; +import org.jlab.rec.dc.segment.SegmentFinder; +import org.jlab.rec.dc.track.Track; +import org.jlab.rec.dc.track.TrackCandListWithURWellFinder; +import org.jlab.rec.dc.track.URWellDCCrossesList; +import org.jlab.rec.dc.trajectory.Road; +import org.jlab.rec.dc.trajectory.RoadFinder; +import org.jlab.rec.urwell.reader.URWellReader; +import org.jlab.rec.urwell.reader.URWellCross; +import org.jlab.rec.dc.track.URWellDCCrossesListFinder; + +/** + * + * @author Tongtong Cao + */ +public class DCURWellHBPostClusterConv extends DCEngine { + public DCURWellHBPostClusterConv() { + super("DCHB"); + this.getBanks().init("HitBasedTrkg", "", "HB"); + } + + @Override + public void setDropBanks() { + super.registerOutputBank(this.getBanks().getHitsBank()); + super.registerOutputBank(this.getBanks().getClustersBank()); + super.registerOutputBank(this.getBanks().getSegmentsBank()); + super.registerOutputBank(this.getBanks().getCrossesBank()); + super.registerOutputBank(this.getBanks().getTracksBank()); + super.registerOutputBank(this.getBanks().getIdsBank()); + } + + @Override + public boolean processDataEvent(DataEvent event) { + + int run = this.getRun(event); + if(run==0) return true; + + /* IO */ + HitReader reader = new HitReader(this.getBanks(), Constants.getInstance().dcDetector); + RecoBankWriter writer = new RecoBankWriter(this.getBanks()); + // get Field + Swim dcSwim = new Swim(); + + List trkcands = null; + List crosses = null; + List clusters = null; + List segments = null; + List fhits = new ArrayList<>(); + + //1) read the hits from the banks + Map> hits = reader.read_Hits(event); + if(hits == null || hits.isEmpty()) + return true; + //2) find the clusters from these hits + ClusterFinder clusFinder = new ClusterFinder(); + ClusterFitter cf = new ClusterFitter(); + clusters = clusFinder.RecomposeClusters(hits, Constants.getInstance().dcDetector, cf); + if (clusters ==null || clusters.isEmpty()) { + return true; + } + + //3) find the segments from the fitted clusters + SegmentFinder segFinder = new SegmentFinder(); + segments = segFinder.get_Segments(clusters, + event, + Constants.getInstance().dcDetector, false); + + /* 15 */ + // need 6 segments to make a trajectory + if (segments.isEmpty()) { + return true; + } + List rmSegs = new ArrayList<>(); + // clean up hit-based segments + double trkDocOverCellSize; + for (Segment se : segments) { + trkDocOverCellSize = 0; + for (FittedHit fh : se.get_fittedCluster()) { + trkDocOverCellSize += fh.get_ClusFitDoca() / fh.get_CellSize(); + } + if (trkDocOverCellSize / se.size() > 1.1) { + rmSegs.add(se); + } + } + segments.removeAll(rmSegs); + if(segments == null || segments.isEmpty()) + return true; + /* 16 */ + CrossMaker crossMake = new CrossMaker(); + crosses = crossMake.find_Crosses(segments, Constants.getInstance().dcDetector); + if (crosses.isEmpty()) { + event.appendBanks( + writer.fillHBSegmentsBank(event, segments)); + return true; + } + /* 17 */ + CrossListFinder crossLister = new CrossListFinder(); + + CrossList crosslist = crossLister.candCrossLists(event, crosses, + false, + null, + Constants.getInstance().dcDetector, + null, + dcSwim, false); + /* 18 */ + + URWellReader uRWellReader = new URWellReader(event, "HB"); + List urCrosses = uRWellReader.getUrwellCrosses(); + + URWellDCCrossesListFinder uRWellDCCrossListLister = new URWellDCCrossesListFinder(); + + URWellDCCrossesList urDCCrossesList = uRWellDCCrossListLister.candURWellDCCrossLists(urCrosses, crosslist); + + //6) find the list of track candidates + TrackCandListWithURWellFinder trkcandFinder = new TrackCandListWithURWellFinder(Constants.HITBASE); + trkcands = trkcandFinder.getTrackCands(urDCCrossesList, + Constants.getInstance().dcDetector, + Swimmer.getTorScale(), + dcSwim, false); + /* 19 */ + // track found + int trkId = 1; + if (!trkcands.isEmpty()) { + // remove overlaps + trkcandFinder.removeOverlappingTracks(trkcands); + for (Track trk : trkcands) { + // reset the id + trk.set_Id(trkId); + trkcandFinder.matchHits(trk.getStateVecs(), + trk, + Constants.getInstance().dcDetector, + dcSwim); + trkId++; + } + } + + //gather all the hits and URWell crosses for pointer bank creation + List urCrossesOnTrks = new ArrayList(); + for (Track trk : trkcands) { + trk.calcTrajectory(trk.getId(), dcSwim, trk.get_Vtx0(), trk.get_pAtOrig(), trk.get_Q()); + for (Cross c : trk) { + c.set_CrossDirIntersSegWires(); + trkcandFinder.setHitDoubletsInfo(c.get_Segment1()); + trkcandFinder.setHitDoubletsInfo(c.get_Segment2()); + for (FittedHit h1 : c.get_Segment1()) { +// h1.setSignalPropagTimeAlongWire(dcDetector); //PASS1, not necessary because hits were already updated in trkcandFinder.matchHits +// h1.setSignalTimeOfFlight(); //PASS1 + if(h1.get_AssociatedHBTrackID()>0) fhits.add(h1); + } + for (FittedHit h2 : c.get_Segment2()) { +// h2.setSignalPropagTimeAlongWire(dcDetector); //PASS1 +// h2.setSignalTimeOfFlight(); //PASS1 + if(h2.get_AssociatedHBTrackID()>0) fhits.add(h2); + } + } + if(trk.get_URWellCross() != null){ + urCrossesOnTrks.add(trk.get_URWellCross()); + trk.get_URWellCross().set_tid(trk.get_Id()); + } + } + + + + + // no candidate found, stop here and save the hits, + // the clusters, the segments, the crosses + if (trkcands.isEmpty()) { + event.appendBanks( + writer.fillHBHitsBank(event, fhits), + writer.fillHBClustersBank(event, clusters), + writer.fillHBSegmentsBank(event, segments), + writer.fillHBCrossesBank(event, crosses), + writer.fillHBURWellCrossesBank(event, urCrossesOnTrks)); + } else { + event.appendBanks( + writer.fillHBHitsBank(event, fhits), + writer.fillHBClustersBank(event, clusters), + writer.fillHBSegmentsBank(event, segments), + writer.fillHBCrossesBank(event, crosses), + writer.fillHBURWellCrossesBank(event, urCrossesOnTrks), + writer.fillHBTracksBank(event, trkcands), + writer.fillHBHitsTrkIdBank(event, fhits), + writer.fillHBTrajectoryBank(event, trkcands)); + } + return true; + } +} diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java new file mode 100755 index 000000000..6970d114f --- /dev/null +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -0,0 +1,711 @@ +package org.jlab.service.dc; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import org.jlab.clas.swimtools.Swim; +import org.jlab.detector.geant4.v2.DCGeant4Factory; +import org.jlab.geom.prim.Line3D; +import org.jlab.geom.prim.Point3D; +import org.jlab.geom.prim.Vector3D; +import org.jlab.io.base.DataBank; +import org.jlab.io.base.DataEvent; +import org.jlab.jnp.matrix.Matrix; +import org.jlab.rec.dc.Constants; +import org.jlab.rec.dc.banks.HitReader; +import org.jlab.rec.dc.banks.RecoBankWriter; +import org.jlab.rec.dc.cluster.ClusterCleanerUtilities; +import org.jlab.rec.dc.cluster.ClusterFinder; +import org.jlab.rec.dc.cluster.ClusterFitter; +import org.jlab.rec.dc.cluster.FittedCluster; +import org.jlab.rec.dc.cross.Cross; +import org.jlab.rec.dc.cross.CrossMaker; +import org.jlab.rec.dc.hit.FittedHit; +import org.jlab.rec.dc.segment.Segment; +import org.jlab.rec.dc.segment.SegmentFinder; +import org.jlab.rec.dc.timetodistance.TableLoader; +import org.jlab.rec.dc.timetodistance.TimeToDistanceEstimator; +import org.jlab.rec.dc.track.Track; +import org.jlab.rec.dc.trajectory.StateVec; +import org.jlab.rec.dc.trajectory.Trajectory; +import org.jlab.rec.dc.trajectory.TrajectoryFinder; +import org.jlab.utils.groups.IndexedTable; + +import org.jlab.clas.tracking.kalmanfilter.Surface; +import org.jlab.clas.tracking.kalmanfilter.zReference.KFitterWithURWell; +import org.jlab.clas.tracking.kalmanfilter.zReference.StateVecs; +import org.jlab.clas.tracking.utilities.MatrixOps.Libr; +import org.jlab.clas.tracking.utilities.RungeKuttaDoca; +import org.jlab.rec.dc.track.TrackCandListWithURWellFinder; +import org.jlab.rec.urwell.reader.URWellCross; +import org.jlab.rec.urwell.reader.URWellReader; + +public class DCURWellTBEngine extends DCEngine { + + private TimeToDistanceEstimator tde = null; + + public DCURWellTBEngine(String trking) { + super(trking); + tde = new TimeToDistanceEstimator(); + } + public DCURWellTBEngine() { + super("DCTB"); + this.getBanks().init("TimeBasedTrkg", "HB", "TB"); + tde = new TimeToDistanceEstimator(); + } + + @Override + public void setDropBanks() { + super.registerOutputBank(this.getBanks().getHitsBank()); + super.registerOutputBank(this.getBanks().getClustersBank()); + super.registerOutputBank(this.getBanks().getSegmentsBank()); + super.registerOutputBank(this.getBanks().getCrossesBank()); + super.registerOutputBank(this.getBanks().getTracksBank()); + super.registerOutputBank(this.getBanks().getCovmatBank()); + super.registerOutputBank(this.getBanks().getTrajBank()); + } + + @Override + public boolean processDataEvent(DataEvent event) { + int run = this.getRun(event); + if(run==0) return true; + + double T_Start = 0; + if(Constants.getInstance().isUSETSTART() == true) { + String recBankName = this.getBanks().getRecEventBank(); + if(event.hasBank(recBankName)==true) { + T_Start = event.getBank(recBankName).getFloat("startTime", 0); + if(T_Start<0) { + return true; // quit if start time not found in data + } + } else { + return true; // no REC HB bank + } + } + // get Field + Swim dcSwim = new Swim(); + + // fill T2D table + if(Constants.getInstance().getT2D()==0) { + TableLoader.Fill(this.getConstantsManager().getConstants(run, Constants.TIME2DIST)); + } else { + TableLoader.Fill(this.getConstantsManager().getConstants(run, Constants.T2DPRESSURE), + this.getConstantsManager().getConstants(run, Constants.T2DPRESSUREREF), + this.getConstantsManager().getConstants(run, Constants.PRESSURE)); + } + ClusterFitter cf = new ClusterFitter(); + ClusterCleanerUtilities ct = new ClusterCleanerUtilities(); + + List fhits = new ArrayList<>(); + List clusters = new ArrayList<>(); + List segments = new ArrayList<>(); + List crosses = new ArrayList<>(); + List trkcands = new ArrayList<>(); + + LOGGER.log(Level.FINE, "TB AI "+ this.getName()); + //instantiate bank writer + RecoBankWriter rbc = new RecoBankWriter(this.getBanks()); + + HitReader hitRead = new HitReader(this.getBanks(), super.getConstantsManager(), Constants.getInstance().dcDetector); //vz; modified reader to read regular or ai hits + hitRead.read_HBHits(event, tde); + //I) get the hits + List hits = hitRead.get_HBHits(); + //II) process the hits + //1) exit if hit list is empty + if(hits.isEmpty() ) { + return true; + } + + //2) find the clusters from these hits + ClusterFinder clusFinder = new ClusterFinder(); + + clusters = clusFinder.FindTimeBasedClusters(event, hits, cf, ct, + this.getConstantsManager().getConstants(run, Constants.TIME2DIST), Constants.getInstance().dcDetector, tde); + for(FittedCluster c : clusters) { + c.set_Id(c.get(0).get_AssociatedClusterID()); + } + if(clusters.isEmpty()) { + rbc.fillAllTBBanks(event, hits, null, null, null, null); + return true; + } + + //3) find the segments from the fitted clusters + SegmentFinder segFinder = new SegmentFinder(); + + List pclusters = segFinder.selectTimeBasedSegments(clusters); + + segments = segFinder.get_Segments(pclusters, event, Constants.getInstance().dcDetector, false); + + if(segments.isEmpty()) { // need 6 segments to make a trajectory + for(FittedCluster c : clusters) { + for(FittedHit hit : c) { + hit.set_AssociatedClusterID(c.get_Id()); + hit.set_AssociatedHBTrackID(c.get(0).get_AssociatedHBTrackID()); + fhits.add(hit); + } + } + rbc.fillAllTBBanks( event, fhits, clusters, null, null, null); + return true; + } + + for(Segment seg : segments) { + for(FittedHit hit : seg.get_fittedCluster()) { + fhits.add(hit); + } + } + + CrossMaker crossMake = new CrossMaker(); + //crosses = crossMake.find_Crosses(segments, dcDetector); + + //if(crosses.isEmpty() ) { + // rbc.fillAllTBBanks(event, rbc, fhits, clusters, segments, null, null); + // return true; + //} + + // + // also need Track bank + if (event.hasBank(this.getBanks().getInputTracksBank()) == false) { + return true; + } + + URWellReader uRWellReader = new URWellReader(event, "TB"); + List urCrosses = uRWellReader.getUrwellCrosses(); + + DataBank trkbank = event.getBank(this.getBanks().getInputTracksBank()); + //DataBank trkcovbank = event.getBank("TimeBasedTrkg::TBCovMat"); + int trkrows = trkbank.rows(); + Track[] TrackArray = new Track[trkrows]; + for (int i = 0; i < trkrows; i++) { + Track HBtrk = new Track(); + HBtrk.set_Id(trkbank.getShort("id", i)); + HBtrk.setSector(trkbank.getByte("sector", i)); + HBtrk.set_Q(trkbank.getByte("q", i)); + HBtrk.set_pAtOrig(new Vector3D(trkbank.getFloat("p0_x", i), trkbank.getFloat("p0_y", i), trkbank.getFloat("p0_z", i))); + HBtrk.set_P(HBtrk.get_pAtOrig().mag()); + HBtrk.set_Vtx0(new Point3D(trkbank.getFloat("Vtx0_x", i), trkbank.getFloat("Vtx0_y", i), trkbank.getFloat("Vtx0_z", i))); + HBtrk.set_FitChi2(trkbank.getFloat("chi2", i)); + StateVec HBFinalSV = new StateVec(trkbank.getFloat("x", i), trkbank.getFloat("y", i), + trkbank.getFloat("tx", i), trkbank.getFloat("ty", i)); + HBFinalSV.setZ(trkbank.getFloat("z", i)); + HBtrk.setFinalStateVec(HBFinalSV); + + int urcross_id = trkbank.getShort("URWellCross_ID", i); + for(int j = 0; j < urCrosses.size(); j++){ + if(urCrosses.get(j).id() == urcross_id){ + HBtrk.set_URWellCross(urCrosses.get(j)); + break; + } + } + + TrackArray[HBtrk.get_Id()-1] = HBtrk; + TrackArray[HBtrk.get_Id()-1].set_Status(0); + + } + if(TrackArray==null) { + return true; // HB tracks not saved correctly + } + for(Segment seg : segments) { + if(seg.get(0).get_AssociatedHBTrackID()>0) { + TrackArray[seg.get(0).get_AssociatedHBTrackID()-1].get_ListOfHBSegments().add(seg); + if(seg.get_Status()==1) + TrackArray[seg.get(0).get_AssociatedHBTrackID()-1].set_Status(1); + } + } + + //6) find the list of track candidates + // read beam offsets from database + double beamXoffset, beamYoffset; + IndexedTable beamOffset = this.getConstantsManager().getConstants(run, Constants.BEAMPOS); + beamXoffset = beamOffset.getDoubleValue("x_offset", 0, 0, 0); + beamYoffset = beamOffset.getDoubleValue("y_offset", 0, 0, 0); + if(event.hasBank("RASTER::position")){ + DataBank raster_bank = event.getBank("RASTER::position"); + beamXoffset += raster_bank.getFloat("x", 0); + beamYoffset += raster_bank.getFloat("y", 0); + } + TrackCandListWithURWellFinder trkcandFinder = new TrackCandListWithURWellFinder("TimeBased"); + TrajectoryFinder trjFind = new TrajectoryFinder(); + + for (Track TrackArray1 : TrackArray) { + if (TrackArray1 == null || TrackArray1.get_ListOfHBSegments() == null || TrackArray1.get_ListOfHBSegments().size() < 5) { + continue; + } + TrackArray1.set_MissingSuperlayer(get_Status(TrackArray1)); + TrackArray1.addAll(crossMake.find_Crosses(TrackArray1.get_ListOfHBSegments(), Constants.getInstance().dcDetector)); + if (TrackArray1.size() < 1) { + continue; + } + crosses.addAll(TrackArray1); + + KFitterWithURWell kFZRef = new KFitterWithURWell(true, 30, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitter(); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + StateVec fn = new StateVec(); + if (kFZRef.setFitFailed==false && kFZRef.finalStateVec!=null) { + // set the state vector at the last measurement site + fn.set(kFZRef.finalStateVec.x, kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); + //set the track parameters if the filter does not fail + TrackArray1.set_P(1./Math.abs(kFZRef.finalStateVec.Q)); + TrackArray1.set_Q((int)Math.signum(kFZRef.finalStateVec.Q)); + + trkcandFinder.setTrackPars(TrackArray1, new Trajectory(), trjFind, fn, kFZRef.finalStateVec.z, Constants.getInstance().dcDetector, dcSwim, beamXoffset, beamYoffset); + // candidate parameters are set from the state vector + if (TrackArray1.fit_Successful == false) { + continue; + } + + TrackArray1.set_FitChi2(kFZRef.chi2); + TrackArray1.set_FitNDF(kFZRef.NDF); + TrackArray1.setStateVecs(kfStateVecsAlongTrajectory); + TrackArray1.set_FitConvergenceStatus(kFZRef.ConvStatus); + if (TrackArray1.get_Vtx0().toVector3D().mag() > 500) { + continue; + } + + // get CovMat at vertex + Point3D VTCS = crosses.get(0).getCoordsInSector(TrackArray1.get_Vtx0().x(), TrackArray1.get_Vtx0().y(), TrackArray1.get_Vtx0().z()); + TrackArray1.set_CovMat(kFZRef.propagateToVtx(crosses.get(0).get_Sector(), VTCS.z())); + + if (TrackArray1.isGood()) { + trkcands.add(TrackArray1); + } + + } + + } + + if(!trkcands.isEmpty()) { + //trkcandFinder.removeOverlappingTracks(trkcands); // remove overlaps + for(Track trk: trkcands) { + int trkId = trk.get_Id(); + // reset the id + //trk.set_Id(trkId); + trkcandFinder.matchHits(trk.getStateVecs(), trk, Constants.getInstance().dcDetector, dcSwim); + trk.calcTrajectory(trkId, dcSwim, trk.get_Vtx0(), trk.get_pAtOrig(), trk.get_Q()); + LOGGER.log(Level.FINE, trk.toString()); + + for(Cross c : trk) { + c.set_CrossDirIntersSegWires(); + c.get_Segment1().isOnTrack=true; + c.get_Segment2().isOnTrack=true; + for (FittedHit h1 : c.get_Segment1()) { + h1.set_AssociatedTBTrackID(trk.get_Id()); + } + for (FittedHit h2 : c.get_Segment2()) { + h2.set_AssociatedTBTrackID(trk.get_Id()); + } + + if (c.get_Segment1().get_Id() == -1) { + trk.set_MissingSuperlayer(c.get_Segment1().get_Superlayer()); + trk.setSingleSuperlayer(c.get_Segment2()); + } + if (c.get_Segment2().get_Id() == -1) { + trk.set_MissingSuperlayer(c.get_Segment2().get_Superlayer()); + trk.setSingleSuperlayer(c.get_Segment1()); + } + } + } + this.ensureTrackUnique(trkcands); + } + + this.ensureUniqueness(fhits, clusters, segments, crosses); + if(trkcands.isEmpty()) { + + rbc.fillAllTBBanks(event, fhits, clusters, segments, crosses, urCrosses, null); // no cand found, stop here and save the hits, the clusters, the segments, the crosses + return true; + } + + rbc.fillAllTBBanks(event, fhits, clusters, segments, crosses, urCrosses, trkcands); + + return true; + } + + public List setKFStateVecsAlongTrajectory(KFitterWithURWell kFZRef) { + List kfStateVecsAlongTrajectory = new ArrayList<>(); + + for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + sv.setZ(svc.z); + sv.setB(svc.B); + sv.setPathLength(svc.getPathLength()); + kfStateVecsAlongTrajectory.add(sv); + } + + return kfStateVecsAlongTrajectory; + } + + public void getInitState(Track trkcand, double z0, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initStateVec, KFitterWithURWell kf, Swim dcSwim, float[] bf) { + if (trkcand != null && trkcand.getFinalStateVec()!=null ) { + initStateVec.x = trkcand.getFinalStateVec().x(); + initStateVec.y = trkcand.getFinalStateVec().y(); + initStateVec.z = trkcand.getFinalStateVec().getZ(); + initStateVec.tx = trkcand.getFinalStateVec().tanThetaX(); + initStateVec.ty = trkcand.getFinalStateVec().tanThetaY(); + initStateVec.Q = ((double) trkcand.get_Q())/trkcand.get_P(); + + RungeKuttaDoca rk = new RungeKuttaDoca(); + rk.SwimToZ(trkcand.get(0).get_Sector(), initStateVec, dcSwim, z0, bf); + + //initCM.covMat = trkcand.get_CovMat(); + //test + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec rinitSV = svs.new StateVec(0); + rinitSV.x = trkcand.getFinalStateVec().x(); + rinitSV.y = trkcand.getFinalStateVec().y(); + rinitSV.z = trkcand.getFinalStateVec().getZ(); + rinitSV.tx = trkcand.getFinalStateVec().tanThetaX(); + rinitSV.ty = trkcand.getFinalStateVec().tanThetaY(); + rinitSV.Q = ((double) trkcand.get_Q())/trkcand.get_P(); + double[] FTF = new double[25]; + double[] F = this.F(trkcand.get(0).get_Sector(), z0, rinitSV, rk, dcSwim, bf); + for(int i = 0; i<5; i++) { + FTF[i*5+i]=F[i]*F[i]; + + } + + //Matrix initCMatrix = new Matrix(FTF); + Matrix initCMatrix = new Matrix(); + initCMatrix.set(FTF); + initStateVec.CM = initCMatrix; + + } else { + kf.setFitFailed = true; + } + + } + + private org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec reset(org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec sv, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec stateVec) { + StateVecs svs = new StateVecs(); + sv = svs.new StateVec(stateVec.k); + sv.x = stateVec.x; + sv.y = stateVec.y; + sv.tx = stateVec.tx; + sv.ty = stateVec.ty; + sv.z = stateVec.z; + sv.Q = stateVec.Q; + + return sv; + } + private void swimToSite(int sector, double z0, + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec SVplus, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec SVminus, RungeKuttaDoca rk, Swim dcSwim, float[] bf) { + + rk.SwimToZ(sector, SVplus, dcSwim, z0, bf); + rk.SwimToZ(sector, SVminus, dcSwim, z0, bf); + } + + double[] F(int sector, double z0, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec stateVec, RungeKuttaDoca rk, Swim dcSwim, float[] bf) { + double[] _F = new double[5]; + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec SVplus = null; + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec SVminus = null; + + SVplus = this.reset(SVplus, stateVec); + SVminus = this.reset(SVminus, stateVec); + + double delt_x = 0.05; + SVplus.x += delt_x/2.; + SVminus.x-= delt_x/2.; + + this.swimToSite(sector, z0, SVplus, SVminus, rk, dcSwim, bf); + + _F[0] = (SVplus.x - SVminus.x)/delt_x; + + SVplus = this.reset(SVplus, stateVec); + SVminus = this.reset(SVminus, stateVec); + + double delt_y = 0.05; + SVplus.y += delt_y/2.; + SVminus.y-= delt_y/2.; + + this.swimToSite(sector, z0, SVplus, SVminus, rk, dcSwim, bf); + + _F[1] = (SVplus.y - SVminus.y)/delt_y; + + SVplus = this.reset(SVplus, stateVec); + SVminus = this.reset(SVminus, stateVec); + + double delt_tx = 0.001; + SVplus.tx += delt_tx/2.; + SVminus.tx-= delt_tx/2.; + + this.swimToSite(sector, z0, SVplus, SVminus, rk, dcSwim, bf); + + _F[2] = (SVplus.tx - SVminus.tx)/delt_tx; + + SVplus = this.reset(SVplus, stateVec); + SVminus = this.reset(SVminus, stateVec); + + double delt_ty = 0.001; + SVplus.ty += delt_ty/2.; + SVminus.ty-= delt_ty/2.; + + this.swimToSite(sector, z0, SVplus, SVminus, rk, dcSwim, bf); + + _F[3] = (SVplus.ty - SVminus.ty)/delt_ty; + + SVplus = this.reset(SVplus, stateVec); + SVminus = this.reset(SVminus, stateVec); + + + _F[4] = 0.01/Math.abs(SVplus.Q); + + return _F; + + } + + public List getMeasSurfaces(Track trk, DCGeant4Factory DcDetector) { + List hOTS = new ArrayList<>(); // the list of hits on track + FittedHit hitOnTrk; + + URWellCross urCross = trk.get_URWellCross(); + HitOnTrack urhot = new HitOnTrack(urCross.sector(), urCross.local().x(), urCross.local().y(), urCross.local().z(), 0.02, 0.04); + urhot.isDCHit = false; + hOTS.add(urhot); + + for(int s = 0; s < trk.get_ListOfHBSegments().size(); s++) { + for(int h = 0; h < trk.get_ListOfHBSegments().get(s).size(); h++) { + trk.get_ListOfHBSegments().get(s).get(h).calc_CellSize(DcDetector); + hitOnTrk = trk.get_ListOfHBSegments().get(s).get(h); + int slayr = trk.get_ListOfHBSegments().get(s).get(h).get_Superlayer(); + + double sl1 = trk.get_ListOfHBSegments().get(s).get_fittedCluster().get_clusterLineFitSlope(); + double it1 = trk.get_ListOfHBSegments().get(s).get_fittedCluster().get_clusterLineFitIntercept(); + + double Z = hitOnTrk.get_Z(); + double X = sl1 * Z + it1; + HitOnTrack hot = new HitOnTrack(slayr, X, Z, + hitOnTrk.get_WireMaxSag(), + hitOnTrk.get_WireLine()); + + hot._doca[0] = trk.get_ListOfHBSegments().get(s).get(h).get_Doca(); + + double LR = Math.signum(trk.get_ListOfHBSegments().get(s).get(h).get_XWire()-trk.get_ListOfHBSegments().get(s).get(h).get_X()); + hot._doca[0]*=LR; + hot._hitError = trk.get_ListOfHBSegments().get(s).get(h).get_DocaErr()*trk.get_ListOfHBSegments().get(s).get(h).get_DocaErr(); + //LOGGER.log(Level.FINE, " Z "+Z+" ferr "+(float)(hot._Unc /(hot._hitError/4.))); + hot._Unc[0] = hot._hitError; + hot.region = trk.get_ListOfHBSegments().get(s).get(h).get_Region(); + hot.sector = trk.get_ListOfHBSegments().get(s).get(h).get_Sector(); + hot.superlayer = trk.get_ListOfHBSegments().get(s).get(h).get_Superlayer(); + hot.layer = trk.get_ListOfHBSegments().get(s).get(h).get_Layer(); + hOTS.add(hot); + } + } + + Collections.sort(hOTS); // sort the collection in order of increasing Z value (i.e. going downstream from the target) + // identify double hits and take the average position + for (int i = 0; i < hOTS.size(); i++) { + if (i > 0) { + if (Math.abs(hOTS.get(i - 1)._Z - hOTS.get(i)._Z)<0.01) { + hOTS.get(i - 1)._doca[1] = hOTS.get(i)._doca[0]; + hOTS.get(i - 1)._Unc[1] = hOTS.get(i)._Unc[0]; + hOTS.get(i - 1)._wireLine[1] = hOTS.get(i)._wireLine[0]; + hOTS.remove(i); + hOTS.get(i - 1).nMeas = 2; + } + } + } + + List surfaces = new ArrayList<>(hOTS.size()); + + for (int i = 0; i < hOTS.size(); i++) { + if(!hOTS.get(i).isDCHit){ + Surface surf = new Surface(hOTS.get(i).sector, hOTS.get(i)._X, hOTS.get(i)._Y, hOTS.get(i)._Z, hOTS.get(i)._X_err, hOTS.get(i)._Y_err); + surf.region = 0; + surf.setLayer(1); + surfaces.add(i, surf); + } + else{ + Surface surf = new Surface(hOTS.get(i).region, hOTS.get(i)._Z, hOTS.get(i)._X, hOTS.get(i)._tilt, hOTS.get(i)._wireMaxSag, + hOTS.get(i)._doca, hOTS.get(i)._Unc, hOTS.get(i)._hitError, hOTS.get(i)._wireLine); + surf.setSector(hOTS.get(i).sector); + surf.setSuperLayer(hOTS.get(i).superlayer); + surf.setLayer(hOTS.get(i).layer); + surf.setNMeas(hOTS.get(i).nMeas); + surfaces.add(i, surf); + } + } + + return surfaces; + } + + public class HitOnTrack implements Comparable { + + public double _hitError; + public double _X; + public double _Z; + public double[] _Unc = new double[2]; + public double _tilt; + public double[] _doca = new double[2]; + public double _wireMaxSag; + public Line3D[] _wireLine = new Line3D[2]; + public int region; + public int superlayer; + public int sector; + public int layer; + public int nMeas = 1; + + // For URWell + public double _Y; + public double _X_err; + public double _Y_err; + public boolean isDCHit = true; + + public HitOnTrack(int sector, double X, double Y, double Z, double X_err, double Y_err) { + this.sector = sector; + _X = X; + _Y = Y; + _Z = Z; + _X_err = X_err; + _Y_err = Y_err; + } + + public HitOnTrack(int superlayer, double X, double Z, double wiremaxsag, Line3D wireLine) { + _X = X; + _Z = Z; + _wireMaxSag = wiremaxsag; + _wireLine[0] = wireLine; + _doca[0] = -99; + _doca[1] = -99; + _Unc[0] = 1; + _Unc[1] = 1; + + //use stereo angle in fit based on wire direction + _tilt = 90-Math.toDegrees(wireLine.direction().asUnit().angle(new Vector3D(1,0,0))); + } + + @Override + public int compareTo(HitOnTrack o) { + if (this._Z == o._Z) { + return 0; + } + if (this._Z > o._Z) { + return 1; + } else { + return -1; + } + } + } + + + private int get_Status(Track track) { + int miss = 0; + + int L[] = new int[6]; + Map SegMap = new HashMap<>(); + for(int l = 0; l trkcands) { + List rmFrmList = new ArrayList<>(); + trkcands.sort(Comparator.comparing(Track::get_Id).thenComparing(Track::get_FitChi2)); + for(int i = 0; i < trkcands.size()-1; i++) { + if(trkcands.get(i).get_Id()==trkcands.get(i+1).get_Id()) { + double chi2_ov_ndf_i=trkcands.get(i).get_FitChi2()/(double)trkcands.get(i).get_FitNDF(); + double chi2_ov_ndf_ip1=trkcands.get(i+1).get_FitChi2()/(double)trkcands.get(i+1).get_FitNDF(); + if(chi2_ov_ndf_i fhits, List clusters, List segments, List crosses) { + + Map hitsMap = new HashMap<>(); + for(FittedHit h : fhits) { + if(hitsMap.get(h.get_Id())==null) { + hitsMap.put(h.get_Id(), h); + } else { + if(h.get_AssociatedHBTrackID()!=-1) + hitsMap.replace(h.get_Id(), h); + } + + } + + fhits.removeAll(fhits); + for (Map.Entry entry : hitsMap.entrySet()) { + fhits.add(entry.getValue()); + } + + Map clsMap = new HashMap<>(); + for(FittedCluster clus : clusters) { + if(clsMap.get(clus.get_Id())==null) { + clsMap.put(clus.get_Id(), clus); + } else { + if(clus.get(0).get_AssociatedHBTrackID()!=-1) + clsMap.replace(clus.get_Id(), clus); + } + + } + + clusters.removeAll(clusters); + for (Map.Entry entry : clsMap.entrySet()) { + clusters.add(entry.getValue()); + } + + Map segMap = new HashMap<>(); + for(Segment seg : segments) { + if(segMap.get(seg.get_Id())==null) { + segMap.put(seg.get_Id(), seg); + } else { + if(seg.get(0).get_AssociatedHBTrackID()!=-1) + segMap.replace(seg.get_Id(), seg); + } + + } + + segments.removeAll(segments); + for (Map.Entry entry : segMap.entrySet()) { + segments.add(entry.getValue()); + } + + Map crsMap = new HashMap<>(); + for(Cross cr : crosses) { + if(crsMap.get(cr.get_Id())==null) { + crsMap.put(cr.get_Id(), cr); + } else { + if(cr.get_Segment1().get(0).get_AssociatedHBTrackID()!=-1 + && cr.get_Segment1().get(0).get_AssociatedHBTrackID()==cr.get_Segment2().get(0).get_AssociatedHBTrackID()) + crsMap.replace(cr.get_Id(), cr); + } + + } + + crosses.removeAll(crosses); + for (Map.Entry entry : crsMap.entrySet()) { + crosses.add(entry.getValue()); + } + + } +} diff --git a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCluster.java b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCluster.java new file mode 100644 index 000000000..067974677 --- /dev/null +++ b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCluster.java @@ -0,0 +1,58 @@ +package org.jlab.rec.urwell.reader; + +import org.jlab.detector.base.DetectorDescriptor; +import org.jlab.detector.base.DetectorType; + +/** + * + * @author Tongtong Cao + */ + +public class URWellCluster { + + private DetectorDescriptor desc = new DetectorDescriptor(DetectorType.URWELL); + private int size = 0; + private double energy = 0; + private double time = 0; + private int crossIndex = -1; + + public URWellCluster(int sector, int layer, int component, int size, double energy, double time) { + this.desc.setSectorLayerComponent(sector, layer, component); + this.size = size; + this.energy = energy; + this.time = time; + } + + public int sector() { + return this.desc.getSector(); + } + + public int layer() { + return this.desc.getLayer(); + } + + public int strip() { + return this.desc.getComponent(); + } + + public int size() { + return size; + } + + public double energy() { + return energy; + } + + public double time() { + return time; + } + + public int getCrossIndex() { + return crossIndex; + } + + public void setCrossIndex(int crossIndex) { + this.crossIndex = crossIndex; + } + +} diff --git a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java new file mode 100644 index 000000000..6d15cdcfe --- /dev/null +++ b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java @@ -0,0 +1,139 @@ +package org.jlab.rec.urwell.reader; + +import java.util.List; +import org.jlab.detector.base.DetectorDescriptor; +import org.jlab.detector.base.DetectorType; +import org.jlab.geom.prim.Point3D; +import org.jlab.service.urwell.URWellConstants; +/** + * + * @author Tongtong Cao + */ + +public class URWellCross { + + private DetectorDescriptor desc = new DetectorDescriptor(DetectorType.URWELL); + private Point3D global; + private Point3D local; + private double energy = 0; + private double time = 0; + private int id = -1; + private int cluster1 = -1; + private int cluster2 = -1; + private int status = -1; + private int tid = -1; // Track id; + + public URWellCross(int id, int sector, double x, double y, double z, double energy, double time, int cluster1, int cluster2, int status) { + this.id = id; + this.desc.setSectorLayerComponent(sector, 0, 0); + this.global = new Point3D(x, y, z); + this.local = new Point3D(x, y, z); + local.rotateZ(Math.toRadians(-60 * (sector - 1))); + local.rotateY(Math.toRadians(-25)); + this.energy = energy; + this.time = time; + this.cluster1 = cluster1; + this.cluster2 = cluster2; + this.status = status; + } + + public URWellCross(int id, int tid, int sector, double x, double y, double z, double x_local, double y_local, double z_local, double energy, double time, int cluster1, int cluster2, int status) { + this.id = id; + this.tid = tid; + this.desc.setSectorLayerComponent(sector, 0, 0); + this.global = new Point3D(x, y, z); + this.local = new Point3D(x_local, y_local, z_local); + this.energy = energy; + this.time = time; + this.cluster1 = cluster1; + this.cluster2 = cluster2; + this.status = status; + } + + /** + * return track id + */ + + public int get_tid(){ + return tid; + } + + /** + * @param tid track id + */ + + public void set_tid(int tid){ + this.tid = tid; + } + + public int id() { + return this.id; + } + + public int sector() { + return this.desc.getSector(); + } + + public Point3D position() { + return this.global; + } + + public Point3D local() { + return this.local; + } + + public double energy() { + return energy; + } + + public double time() { + return time; + } + + public void setClusterIndex1(int cluster) { + this.cluster1 = cluster; + } + + public void setClusterIndex2(int cluster) { + this.cluster2 = cluster; + } + + public int cluster1() { + return this.cluster1; + } + + public int cluster2() { + return this.cluster2; + } + + public int status() { + return this.status; + } + + public URWellCluster getCluster1(List urClusters) { + if (cluster1 > 0 && cluster1 <= urClusters.size()) { + return urClusters.get(cluster1 - 1); + } else { + return null; + } + } + + public URWellCluster getCluster2(List urClusters) { + if (cluster2 > 0 && cluster2 <= urClusters.size()) { + return urClusters.get(cluster2 - 1); + } else { + return null; + } + } + + public boolean isGood(List urClusters) { + if(this.getCluster1(urClusters) != null && this.getCluster2(urClusters)!= null) + return Math.abs(this.getCluster1(urClusters).energy() - this.getCluster2(urClusters).energy()) < URWellConstants.deltaE + && Math.abs(this.getCluster1(urClusters).time() - this.getCluster2(urClusters).time()) < URWellConstants.deltaT; + else return false; + } + + public boolean isInTime() { + return Math.abs(this.time() - URWellConstants.meanT) < 1.2 * URWellConstants.deltaT; + } +} diff --git a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellHit.java b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellHit.java new file mode 100644 index 000000000..33fa88e9f --- /dev/null +++ b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellHit.java @@ -0,0 +1,43 @@ +package org.jlab.rec.urwell.reader; + +import org.jlab.detector.base.DetectorDescriptor; +import org.jlab.detector.base.DetectorType; + +/** + * + * @author Tongtong Cao + */ + +public class URWellHit { + + private DetectorDescriptor desc = new DetectorDescriptor(DetectorType.URWELL); + private double energy = 0; + private double time = 0; + + public URWellHit(int sector, int layer, int component, double energy, double time) { + this.desc.setSectorLayerComponent(sector, layer, component); + this.energy = energy; + this.time = time; + } + + public int sector() { + return this.desc.getSector(); + } + + public int layer() { + return this.desc.getLayer(); + } + + public int strip() { + return this.desc.getComponent(); + } + + public double energy() { + return energy; + } + + public double time() { + return time; + } + +} diff --git a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java new file mode 100644 index 000000000..a5e9cdf8a --- /dev/null +++ b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java @@ -0,0 +1,127 @@ +package org.jlab.rec.urwell.reader; + +import java.util.ArrayList; +import java.util.List; +import org.jlab.io.base.DataBank; +import org.jlab.io.base.DataEvent; + +/** + * + * @author Tongtong Cao + */ +public class URWellReader{ + + private final List urHits = new ArrayList<>(); + private final List urClusters = new ArrayList<>(); + private final List urCrosses = new ArrayList<>(); + + + public URWellReader(DataEvent event) { + + if(event.hasBank("URWELL::hits")) + this.readHits(event.getBank("URWELL::hits")); + if(event.hasBank("URWELL::clusters")) + this.readClusters(event.getBank("URWELL::clusters")); + if(event.hasBank("URWELL::crosses")) + this.readCrosses(event.getBank("URWELL::crosses")); + } + + public URWellReader(DataEvent event, String type) { + if(type == "HB"){ + if(event.hasBank("URWELL::clusters")) + this.readClusters(event.getBank("URWELL::clusters")); + if(event.hasBank("URWELL::crosses")) + this.readCrosses(event.getBank("URWELL::crosses")); + } + else if(type == "TB"){ + if(event.hasBank("HitBasedTrkg::HBURWellCrosses")) + this.readHBCrosses(event.getBank("HitBasedTrkg::HBURWellCrosses")); + } + } + + public List getUrwellHits() { + return urHits; + } + + public List getUrwellClusters() { + return urClusters; + } + + public List getUrwellCrosses() { + return urCrosses; + } + + public final void readHits(DataBank bank) { + + for(int i=0; i Date: Tue, 14 Feb 2023 17:03:06 -0500 Subject: [PATCH 07/69] add pseudo segments for missed track candidates --- .../jlab/service/dc/DCHBPostClusterConv.java | 88 ++++++++++++++++++ .../service/dc/DCURWellHBPostClusterConv.java | 93 ++++++++++++++++++- .../org/jlab/service/dc/DCURWellTBEngine.java | 2 +- 3 files changed, 180 insertions(+), 3 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCHBPostClusterConv.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCHBPostClusterConv.java index b5ea59ad2..c792a149a 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCHBPostClusterConv.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCHBPostClusterConv.java @@ -141,6 +141,94 @@ public boolean processDataEvent(DataEvent event) { trkId++; } } + List crossSegsNotOnTrack = new ArrayList<>(); + List psegments = new ArrayList<>(); + + for (Cross c : crosses) { + if (!c.get_Segment1().isOnTrack) + crossSegsNotOnTrack.add(c.get_Segment1()); + if (!c.get_Segment2().isOnTrack) + crossSegsNotOnTrack.add(c.get_Segment2()); + } + + RoadFinder rf = new RoadFinder(); + List allRoads = rf.findRoads(segments, Constants.getInstance().dcDetector); + List Segs2Road = new ArrayList<>(); + for (Road r : allRoads) { + Segs2Road.clear(); + int missingSL = -1; + for (int ri = 0; ri < 3; ri++) { + if (r.get(ri).associatedCrossId == -1) { + if (r.get(ri).get_Superlayer() % 2 == 1) { + missingSL = r.get(ri).get_Superlayer() + 1; + } else { + missingSL = r.get(ri).get_Superlayer() - 1; + } + } + } + if(missingSL==-1) + continue; + for (int ri = 0; ri < 3; ri++) { + for (Segment s : crossSegsNotOnTrack) { + if (s.get_Sector() == r.get(ri).get_Sector() && + s.get_Region() == r.get(ri).get_Region() && + s.associatedCrossId == r.get(ri).associatedCrossId && + r.get(ri).associatedCrossId != -1) { + if (s.get_Superlayer() % 2 == missingSL % 2) + Segs2Road.add(s); + } + } + } + if (Segs2Road.size() == 2) { + Segment pSegment = rf.findRoadMissingSegment(Segs2Road, + Constants.getInstance().dcDetector, + r.a); + if (pSegment != null) + psegments.add(pSegment); + } + } + + segments.addAll(psegments); + List pcrosses = crossMake.find_Crosses(segments, Constants.getInstance().dcDetector); + + CrossList pcrosslist = crossLister.candCrossLists(event, pcrosses, + false, + null, + Constants.getInstance().dcDetector, + null, + dcSwim, true); + //pcrosslist.removeDuplicates(crosslist); + + List mistrkcands = trkcandFinder.getTrackCands(pcrosslist, + Constants.getInstance().dcDetector, + Swimmer.getTorScale(), + dcSwim, false); + + // remove overlaps + if (!mistrkcands.isEmpty()) { + trkcandFinder.removeOverlappingTracks(mistrkcands); + for (Track trk : mistrkcands) { + + // reset the id + trk.set_Id(trkId); + trkcandFinder.matchHits(trk.getStateVecs(), + trk, + Constants.getInstance().dcDetector, + dcSwim); + trkId++; + } + } + + trkcands.addAll(mistrkcands); + + LOGGER.log(Level.FINE, "Found after 5STg "+mistrkcands.size()+" HB seeds "); + for(int i = 0; i< trkcands.size(); i++) { + LOGGER.log(Level.FINE, "cand "+i); + for(Cross c : trkcands.get(i)) { + LOGGER.log(Level.FINE, c.printInfo()); + } + LOGGER.log(Level.FINE, "------------------------------------------------------------------ "); + } //gather all the hits for pointer bank creation for (Track trk : trkcands) { diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java index ea0aeba2b..3b2bebe21 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java @@ -153,6 +153,96 @@ public boolean processDataEvent(DataEvent event) { trkId++; } } + List crossSegsNotOnTrack = new ArrayList<>(); + List psegments = new ArrayList<>(); + + for (Cross c : crosses) { + if (!c.get_Segment1().isOnTrack) + crossSegsNotOnTrack.add(c.get_Segment1()); + if (!c.get_Segment2().isOnTrack) + crossSegsNotOnTrack.add(c.get_Segment2()); + } + + RoadFinder rf = new RoadFinder(); + List allRoads = rf.findRoads(segments, Constants.getInstance().dcDetector); + List Segs2Road = new ArrayList<>(); + for (Road r : allRoads) { + Segs2Road.clear(); + int missingSL = -1; + for (int ri = 0; ri < 3; ri++) { + if (r.get(ri).associatedCrossId == -1) { + if (r.get(ri).get_Superlayer() % 2 == 1) { + missingSL = r.get(ri).get_Superlayer() + 1; + } else { + missingSL = r.get(ri).get_Superlayer() - 1; + } + } + } + if(missingSL==-1) + continue; + for (int ri = 0; ri < 3; ri++) { + for (Segment s : crossSegsNotOnTrack) { + if (s.get_Sector() == r.get(ri).get_Sector() && + s.get_Region() == r.get(ri).get_Region() && + s.associatedCrossId == r.get(ri).associatedCrossId && + r.get(ri).associatedCrossId != -1) { + if (s.get_Superlayer() % 2 == missingSL % 2) + Segs2Road.add(s); + } + } + } + if (Segs2Road.size() == 2) { + Segment pSegment = rf.findRoadMissingSegment(Segs2Road, + Constants.getInstance().dcDetector, + r.a); + if (pSegment != null) + psegments.add(pSegment); + } + } + + segments.addAll(psegments); + List pcrosses = crossMake.find_Crosses(segments, Constants.getInstance().dcDetector); + + CrossList pcrosslist = crossLister.candCrossLists(event, pcrosses, + false, + null, + Constants.getInstance().dcDetector, + null, + dcSwim, true); + //pcrosslist.removeDuplicates(crosslist); + + URWellDCCrossesList purDCCrossesList = uRWellDCCrossListLister.candURWellDCCrossLists(urCrosses, pcrosslist); + + List mistrkcands = trkcandFinder.getTrackCands(purDCCrossesList, + Constants.getInstance().dcDetector, + Swimmer.getTorScale(), + dcSwim, false); + + // remove overlaps + if (!mistrkcands.isEmpty()) { + trkcandFinder.removeOverlappingTracks(mistrkcands); + for (Track trk : mistrkcands) { + + // reset the id + trk.set_Id(trkId); + trkcandFinder.matchHits(trk.getStateVecs(), + trk, + Constants.getInstance().dcDetector, + dcSwim); + trkId++; + } + } + + trkcands.addAll(mistrkcands); + + LOGGER.log(Level.FINE, "Found after 5STg "+mistrkcands.size()+" HB seeds "); + for(int i = 0; i< trkcands.size(); i++) { + LOGGER.log(Level.FINE, "cand "+i); + for(Cross c : trkcands.get(i)) { + LOGGER.log(Level.FINE, c.printInfo()); + } + LOGGER.log(Level.FINE, "------------------------------------------------------------------ "); + } //gather all the hits and URWell crosses for pointer bank creation List urCrossesOnTrks = new ArrayList(); @@ -189,8 +279,7 @@ public boolean processDataEvent(DataEvent event) { writer.fillHBHitsBank(event, fhits), writer.fillHBClustersBank(event, clusters), writer.fillHBSegmentsBank(event, segments), - writer.fillHBCrossesBank(event, crosses), - writer.fillHBURWellCrossesBank(event, urCrossesOnTrks)); + writer.fillHBCrossesBank(event, crosses)); } else { event.appendBanks( writer.fillHBHitsBank(event, fhits), diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index 6970d114f..f21e51cc0 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -321,7 +321,7 @@ public boolean processDataEvent(DataEvent event) { this.ensureUniqueness(fhits, clusters, segments, crosses); if(trkcands.isEmpty()) { - rbc.fillAllTBBanks(event, fhits, clusters, segments, crosses, urCrosses, null); // no cand found, stop here and save the hits, the clusters, the segments, the crosses + rbc.fillAllTBBanks(event, fhits, clusters, segments, crosses, null); // no cand found, stop here and save the hits, the clusters, the segments, the crosses return true; } From f04469164069f7624cef4e3acfe69930e2071f72 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 15 Feb 2023 13:55:48 -0500 Subject: [PATCH 08/69] add AI services for forward tracking with uRWell --- etc/bankdefs/hipo4/dcnn.json | 26 +++ etc/bankdefs/hipo4/urwell.json | 46 +++++ .../track/TrackCandListWithURWellFinder.java | 2 +- .../service/dc/DCURWellHBPostClusterAI.java | 194 ++++++++++++++++++ .../service/dc/DCURWellHBPostClusterConv.java | 8 +- .../org/jlab/service/dc/DCURWellTBEngine.java | 8 +- .../jlab/service/dc/DCURWellTBEngineAI.java | 14 ++ .../jlab/rec/urwell/reader/URWellReader.java | 8 +- 8 files changed, 297 insertions(+), 9 deletions(-) create mode 100644 reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java create mode 100644 reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngineAI.java diff --git a/etc/bankdefs/hipo4/dcnn.json b/etc/bankdefs/hipo4/dcnn.json index a007771fe..d9f1434c6 100644 --- a/etc/bankdefs/hipo4/dcnn.json +++ b/etc/bankdefs/hipo4/dcnn.json @@ -143,6 +143,18 @@ {"name":"t1_px", "type":"F", "info":"Upstream Region 1 track unit x-momentum vector in the lab"}, {"name":"t1_py", "type":"F", "info":"Upstream Region 1 track unit y-momentum vector in the lab"}, {"name":"t1_pz", "type":"F", "info":"Upstream Region 1 track unit z-momentum vector in the lab"}, + {"name":"URWell_x", "type":"F", "info":"URWell track x-position in the lab (in cm)"}, + {"name":"URWell_y", "type":"F", "info":"URWell track y-position in the lab (in cm)"}, + {"name":"URWell_z", "type":"F", "info":"URWell track z-position in the lab (in cm)"}, + {"name":"URWell_px", "type":"F", "info":"URWell track unit x-momentum vector in the lab"}, + {"name":"URWell_py", "type":"F", "info":"URWell track unit y-momentum vector in the lab"}, + {"name":"URWell_pz", "type":"F", "info":"URWell track unit z-momentum vector in the lab"}, + {"name":"URWell_x_local", "type":"F", "info":"URWell track x-position in the tilted sector coordinates (in cm)"}, + {"name":"URWell_y_local", "type":"F", "info":"URWell track y-position in the tilted sector coordinates (in cm)"}, + {"name":"URWell_z_local", "type":"F", "info":"URWell track z-position in the tilted sector coordinates (in cm)"}, + {"name":"URWell_px_local", "type":"F", "info":"URWell track unit x-momentum vector in the tilted sector coordinates"}, + {"name":"URWell_py_local", "type":"F", "info":"URWell track unit y-momentum vector in the tilted sector coordinates"}, + {"name":"URWell_pz_local", "type":"F", "info":"URWell track unit z-momentum vector in the tilted sector coordinates"}, {"name":"Vtx0_x", "type":"F", "info":"Vertex x-position of the swam track to the DOCA to the beamline (in cm)"}, {"name":"Vtx0_y", "type":"F", "info":"Vertex y-position of the swam track to the DOCA to the beamline (in cm)"}, {"name":"Vtx0_z", "type":"F", "info":"Vertex z-position of the swam track to the DOCA to the beamline (in cm)"}, @@ -154,6 +166,7 @@ {"name":"z", "type":"F", "info":"last site stateVec in tilted sector z (in cm)"}, {"name":"tx", "type":"F", "info":"last site stateVec in tilted sector tx (px/pz)"}, {"name":"ty", "type":"F", "info":"last site stateVec in tilted sector ty (py/pz)"}, + {"name":"URWellCross_ID","type":"S", "info":"id of URWell cross on track"}, {"name":"Cross1_ID", "type":"S", "info":"id of first cross on track"}, {"name":"Cross2_ID", "type":"S", "info":"id of second cross on track"}, {"name":"Cross3_ID", "type":"S", "info":"id of third cross on track"}, @@ -362,12 +375,25 @@ {"name":"t1_px", "type":"F", "info":"Upstream Region 1 track unit x-momentum vector in the lab"}, {"name":"t1_py", "type":"F", "info":"Upstream Region 1 track unit y-momentum vector in the lab"}, {"name":"t1_pz", "type":"F", "info":"Upstream Region 1 track unit z-momentum vector in the lab"}, + {"name":"URWell_x", "type":"F", "info":"URWell track x-position in the lab (in cm)"}, + {"name":"URWell_y", "type":"F", "info":"URWell track y-position in the lab (in cm)"}, + {"name":"URWell_z", "type":"F", "info":"URWell track z-position in the lab (in cm)"}, + {"name":"URWell_px", "type":"F", "info":"URWell track unit x-momentum vector in the lab"}, + {"name":"URWell_py", "type":"F", "info":"URWell track unit y-momentum vector in the lab"}, + {"name":"URWell_pz", "type":"F", "info":"URWell track unit z-momentum vector in the lab"}, + {"name":"URWell_x_local", "type":"F", "info":"URWell track x-position in the tilted sector coordinates (in cm)"}, + {"name":"URWell_y_local", "type":"F", "info":"URWell track y-position in the tilted sector coordinates (in cm)"}, + {"name":"URWell_z_local", "type":"F", "info":"URWell track z-position in the tilted sector coordinates (in cm)"}, + {"name":"URWell_px_local", "type":"F", "info":"URWell track unit x-momentum vector in the tilted sector coordinates"}, + {"name":"URWell_py_local", "type":"F", "info":"URWell track unit y-momentum vector in the tilted sector coordinates"}, + {"name":"URWell_pz_local", "type":"F", "info":"URWell track unit z-momentum vector in the tilted sector coordinates"}, {"name":"Vtx0_x", "type":"F", "info":"Vertex x-position of the swam track to the DOCA to the beamline (in cm)"}, {"name":"Vtx0_y", "type":"F", "info":"Vertex y-position of the swam track to the DOCA to the beamline (in cm)"}, {"name":"Vtx0_z", "type":"F", "info":"Vertex z-position of the swam track to the DOCA to the beamline (in cm)"}, {"name":"p0_x", "type":"F", "info":"3-momentum x-coordinate of the swam track to the DOCA to the beamline (in cm)"}, {"name":"p0_y", "type":"F", "info":"3-momentum y-coordinate of the swam track to the DOCA to the beamline (in cm)"}, {"name":"p0_z", "type":"F", "info":"3-momentum z-coordinate of the swam track to the DOCA to the beamline (in cm)"}, + {"name":"URWellCross_ID","type":"S", "info":"id of URWell cross on track"}, {"name":"Cross1_ID", "type":"S", "info":"id of first cross on track"}, {"name":"Cross2_ID", "type":"S", "info":"id of second cross on track"}, {"name":"Cross3_ID", "type":"S", "info":"id of third cross on track"}, diff --git a/etc/bankdefs/hipo4/urwell.json b/etc/bankdefs/hipo4/urwell.json index 051b4ae49..b9c6c32dd 100644 --- a/etc/bankdefs/hipo4/urwell.json +++ b/etc/bankdefs/hipo4/urwell.json @@ -101,6 +101,52 @@ {"name":"cluster2", "type":"S", "info":"id of the cluster in the W layer"}, {"name":"status", "type":"S", "info":"status of the cluster"} ] + }, + { + "name": "HitBasedTrkg::AIURWellCrosses", + "group": 22300, + "item" : 33, + "info": "crosses from URWEL", + "entries": [ + {"name":"id", "type":"S", "info":"id of the cross"}, + {"name":"tid", "type":"S", "info":"track id"}, + {"name":"sector", "type":"B", "info":"sector of URWELL"}, + {"name":"layer", "type":"B", "info":"Layer of URWELL"}, + {"name":"energy", "type":"F", "info":"Energy of the cross (eV)"}, + {"name":"time", "type":"F", "info":"Time of the cross (ns)"}, + {"name":"x", "type":"F", "info":"X in global coordinate (cm)"}, + {"name":"y", "type":"F", "info":"Y in global coordinate (cm)"}, + {"name":"z", "type":"F", "info":"Z in global coordinate (cm)"}, + {"name":"x_local", "type":"F", "info":"X in tilted sector coordinate (cm)"}, + {"name":"y_local", "type":"F", "info":"Y in tilted sector coordinate (cm)"}, + {"name":"z_local", "type":"F", "info":"Z in tilted sector coordinate (cm)"}, + {"name":"cluster1", "type":"S", "info":"id of the cluster in the V layer"}, + {"name":"cluster2", "type":"S", "info":"id of the cluster in the W layer"}, + {"name":"status", "type":"S", "info":"status of the cluster"} + ] + }, + { + "name": "TimeBasedTrkg::AIURWellCrosses", + "group": 22300, + "item" : 34, + "info": "crosses from URWEL", + "entries": [ + {"name":"id", "type":"S", "info":"id of the cross"}, + {"name":"tid", "type":"S", "info":"track id"}, + {"name":"sector", "type":"B", "info":"sector of URWELL"}, + {"name":"layer", "type":"B", "info":"Layer of URWELL"}, + {"name":"energy", "type":"F", "info":"Energy of the cross (eV)"}, + {"name":"time", "type":"F", "info":"Time of the cross (ns)"}, + {"name":"x", "type":"F", "info":"X in global coordinate (cm)"}, + {"name":"y", "type":"F", "info":"Y in global coordinate (cm)"}, + {"name":"z", "type":"F", "info":"Z in global coordinate (cm)"}, + {"name":"x_local", "type":"F", "info":"X in tilted sector coordinate (cm)"}, + {"name":"y_local", "type":"F", "info":"Y in tilted sector coordinate (cm)"}, + {"name":"z_local", "type":"F", "info":"Z in tilted sector coordinate (cm)"}, + {"name":"cluster1", "type":"S", "info":"id of the cluster in the V layer"}, + {"name":"cluster2", "type":"S", "info":"id of the cluster in the W layer"}, + {"name":"status", "type":"S", "info":"status of the cluster"} + ] } ] diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java index 4bbcd1753..91d33336d 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -38,7 +38,7 @@ * A class with a method implementing an algorithm that finds lists of track * candidates in the DC * - * @author ziegler, tongtong + * @author Tongtong Cao */ public class TrackCandListWithURWellFinder { diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java new file mode 100644 index 000000000..59fd6a60c --- /dev/null +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java @@ -0,0 +1,194 @@ +package org.jlab.service.dc; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.logging.Level; +import org.jlab.clas.swimtools.Swim; +import org.jlab.clas.swimtools.Swimmer; +import org.jlab.io.base.DataEvent; +import org.jlab.rec.dc.Constants; +import org.jlab.rec.dc.banks.HitReader; +import org.jlab.rec.dc.banks.RecoBankWriter; +import org.jlab.rec.dc.cluster.FittedCluster; +import org.jlab.rec.dc.cross.Cross; +import org.jlab.rec.dc.cross.CrossList; +import org.jlab.rec.dc.cross.CrossListFinder; +import org.jlab.rec.dc.hit.FittedHit; +import org.jlab.rec.dc.hit.Hit; +import org.jlab.rec.dc.nn.PatternRec; +import org.jlab.rec.dc.segment.Segment; +import org.jlab.rec.dc.track.Track; +import org.jlab.rec.dc.track.TrackCandListWithURWellFinder; +import org.jlab.rec.dc.track.URWellDCCrossesList; +import org.jlab.rec.dc.track.URWellDCCrossesListFinder; +import org.jlab.rec.urwell.reader.URWellCross; +import org.jlab.rec.urwell.reader.URWellReader; + +/** + * + * @author Tongtong Cao + */ +public class DCURWellHBPostClusterAI extends DCEngine { + + public DCURWellHBPostClusterAI() { + super("DCHAI"); + this.getBanks().init("HitBasedTrkg", "", "AI"); + } + + + @Override + public void setDropBanks() { + super.registerOutputBank(this.getBanks().getHitsBank()); + super.registerOutputBank(this.getBanks().getClustersBank()); + super.registerOutputBank(this.getBanks().getSegmentsBank()); + super.registerOutputBank(this.getBanks().getCrossesBank()); + super.registerOutputBank(this.getBanks().getTracksBank()); + super.registerOutputBank(this.getBanks().getIdsBank()); + } + + @Override + public boolean processDataEvent(DataEvent event) { + + int run = this.getRun(event); + if(run==0) { + LOGGER.log(Level.INFO, "RUN=0: Skipping event"); + return true; + } + + /* IO */ + HitReader reader = new HitReader(this.getBanks(), Constants.getInstance().dcDetector); + reader.initialize(event); + RecoBankWriter writer = new RecoBankWriter(this.getBanks()); + // get Field + Swim dcSwim = new Swim(); + /* 2 */ + + /* 5 */ + LOGGER.log(Level.FINE, "HB AI process event"); + /* 7 */ + /* 8 */ + //AI + List trkcands = null; + List crosses = null; + List clusters = null; + List segments = null; + List fhits = null; + + reader.read_NNHits(event); + + //I) get the lists + List hits = reader.get_DCHits(); + fhits = new ArrayList<>(); + //II) process the hits + //1) exit if hit list is empty + if (hits.isEmpty()) { + return true; + } + PatternRec pr = new PatternRec(); + segments = pr.RecomposeSegments(hits, Constants.getInstance().dcDetector); + Collections.sort(segments); + + if (segments.isEmpty()) { + return true; + } + //crossList + CrossList crosslist = pr.RecomposeCrossList(segments, Constants.getInstance().dcDetector); + crosses = new ArrayList<>(); + + LOGGER.log(Level.FINE, "num cands = "+crosslist.size()); + for (List clist : crosslist) { + crosses.addAll(clist); + for(Cross c : clist) + LOGGER.log(Level.FINE, "Pass Cross"+c.printInfo()); + } + if (crosses.isEmpty()) { + clusters = new ArrayList<>(); + for(Segment seg : segments) { + clusters.add(seg.get_fittedCluster()); + } + event.appendBanks( + writer.fillHBHitsBank(event, fhits), + writer.fillHBClustersBank(event, clusters), + writer.fillHBSegmentsBank(event, segments)); + return true; + } + // update B field + CrossListFinder crossLister = new CrossListFinder(); + for(Cross cr : crosses) { + crossLister.updateBFittedHits(event, cr, null, Constants.getInstance().dcDetector, null, dcSwim); + } + + // Read urwell crosses, and make urwell-dc-crosses combos + URWellReader uRWellReader = new URWellReader(event, "HB"); + List urCrosses = uRWellReader.getUrwellCrosses(); + URWellDCCrossesListFinder uRWellDCCrossListLister = new URWellDCCrossesListFinder(); + URWellDCCrossesList urDCCrossesList = uRWellDCCrossListLister.candURWellDCCrossLists(urCrosses, crosslist); + + //find the list of track candidates + TrackCandListWithURWellFinder trkcandFinder = new TrackCandListWithURWellFinder(Constants.HITBASE); + trkcands = trkcandFinder.getTrackCands(urDCCrossesList, + Constants.getInstance().dcDetector, + Swimmer.getTorScale(), + dcSwim, true); + + // track found + clusters = new ArrayList<>(); + List urCrossesOnTrks = new ArrayList(); + int trkId = 1; + if (trkcands.size() > 0) { + // remove overlaps + trkcandFinder.removeOverlappingTracks(trkcands); + for (Track trk : trkcands) { + // reset the id + trk.set_Id(trkId); + trkcandFinder.matchHits(trk.getStateVecs(), + trk, + Constants.getInstance().dcDetector, + dcSwim); + for (Cross c : trk) { + c.set_CrossDirIntersSegWires(); + clusters.add(c.get_Segment1().get_fittedCluster()); + clusters.add(c.get_Segment2().get_fittedCluster()); + trkcandFinder.setHitDoubletsInfo(c.get_Segment1()); + trkcandFinder.setHitDoubletsInfo(c.get_Segment2()); + for (FittedHit h1 : c.get_Segment1()) { + if(h1.get_AssociatedHBTrackID()>0) fhits.add(h1); + } + for (FittedHit h2 : c.get_Segment2()) { + if(h2.get_AssociatedHBTrackID()>0) fhits.add(h2); + } + } + + if(trk.get_URWellCross() != null){ + urCrossesOnTrks.add(trk.get_URWellCross()); + trk.get_URWellCross().set_tid(trk.get_Id()); + } + trk.calcTrajectory(trk.getId(), dcSwim, trk.get_Vtx0(), trk.get_pAtOrig(), trk.get_Q()); + trkId++; + } + } + + // no candidate found, stop here and save the hits, + // the clusters, the segments, the crosses + if (trkcands.isEmpty()) { + event.appendBanks( + writer.fillHBHitsBank(event, fhits), + writer.fillHBSegmentsBank(event, segments), + writer.fillHBCrossesBank(event, crosses)); + } + else { + event.appendBanks( + writer.fillHBHitsBank(event, fhits), + writer.fillHBClustersBank(event, clusters), + writer.fillHBSegmentsBank(event, segments), + writer.fillHBCrossesBank(event, crosses), + writer.fillHBURWellCrossesBank(event, urCrossesOnTrks), + writer.fillHBTracksBank(event, trkcands), + writer.fillHBHitsTrkIdBank(event, fhits), + writer.fillHBTrajectoryBank(event, trkcands)); + } + return true; + } + +} diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java index 3b2bebe21..636fa7aff 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java @@ -123,12 +123,10 @@ public boolean processDataEvent(DataEvent event) { null, dcSwim, false); /* 18 */ - + // Read urwell crosses, and make urwell-dc-crosses combos URWellReader uRWellReader = new URWellReader(event, "HB"); - List urCrosses = uRWellReader.getUrwellCrosses(); - - URWellDCCrossesListFinder uRWellDCCrossListLister = new URWellDCCrossesListFinder(); - + List urCrosses = uRWellReader.getUrwellCrosses(); + URWellDCCrossesListFinder uRWellDCCrossListLister = new URWellDCCrossesListFinder(); URWellDCCrossesList urDCCrossesList = uRWellDCCrossListLister.candURWellDCCrossLists(urCrosses, crosslist); //6) find the list of track candidates diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index f21e51cc0..04ee94b0f 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -44,6 +44,11 @@ import org.jlab.rec.urwell.reader.URWellCross; import org.jlab.rec.urwell.reader.URWellReader; +/** + * + * @author Tongtong Cao + */ + public class DCURWellTBEngine extends DCEngine { private TimeToDistanceEstimator tde = null; @@ -172,7 +177,8 @@ public boolean processDataEvent(DataEvent event) { return true; } - URWellReader uRWellReader = new URWellReader(event, "TB"); + // Read urwell crosses on HB tracks + URWellReader uRWellReader = new URWellReader(event, this.getBanks().getPrefix()); List urCrosses = uRWellReader.getUrwellCrosses(); DataBank trkbank = event.getBank(this.getBanks().getInputTracksBank()); diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngineAI.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngineAI.java new file mode 100644 index 000000000..79b7d609f --- /dev/null +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngineAI.java @@ -0,0 +1,14 @@ +package org.jlab.service.dc; + +/** + * + * @author Tongtong Cao + */ +public class DCURWellTBEngineAI extends DCURWellTBEngine { + + public DCURWellTBEngineAI() { + super("DCTAI"); + this.getBanks().init("TimeBasedTrkg", "AI", "AI"); + } + +} diff --git a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java index a5e9cdf8a..0e207763e 100644 --- a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java +++ b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java @@ -27,16 +27,20 @@ public URWellReader(DataEvent event) { } public URWellReader(DataEvent event, String type) { - if(type == "HB"){ + if(type == "HB"){ if(event.hasBank("URWELL::clusters")) this.readClusters(event.getBank("URWELL::clusters")); if(event.hasBank("URWELL::crosses")) this.readCrosses(event.getBank("URWELL::crosses")); } - else if(type == "TB"){ + else if(type == "TB"){ if(event.hasBank("HitBasedTrkg::HBURWellCrosses")) this.readHBCrosses(event.getBank("HitBasedTrkg::HBURWellCrosses")); } + else if(type == "AI"){ + if(event.hasBank("HitBasedTrkg::AIURWellCrosses")) + this.readHBCrosses(event.getBank("HitBasedTrkg::AIURWellCrosses")); + } } public List getUrwellHits() { From c9c0af790e10b0040337962d732db0a4af03a4dc Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 24 Feb 2023 12:43:48 -0500 Subject: [PATCH 09/69] change uRWELL resolution setup --- .../dc/src/main/java/org/jlab/rec/dc/Constants.java | 5 +++-- .../org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java index 53d2e66df..4b33e1734 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java @@ -92,8 +92,9 @@ public static Constants getInstance() { public static final double HTCCRADIUS = 175; public static final double LTCCPLANE = 653.09; - public static final double URWELLLOCALZ = 226.0464; - + public static final double URWELLLOCALZ = 226.0464; // cm + public static final double URWELLXRESOLUTION = 0.035; // cm + public static final double URWELLYRESOLUTION = 0.035; // cm // other CLAS12 parameters public static final int NSECT = 6; public static final int NSLAY = 6; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java index 91d33336d..9a0e0ffa4 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -1296,7 +1296,7 @@ public List getMeasSurfaces(Track trkcand, DCGeant4Factory DcDetector) FittedHit hitOnTrk; URWellCross urCross = trkcand.get_URWellCross(); - HitOnTrack urhot = new HitOnTrack(urCross.sector(), urCross.local().x(), urCross.local().y(), urCross.local().z(), 0.02, 0.04); + HitOnTrack urhot = new HitOnTrack(urCross.sector(), urCross.local().x(), urCross.local().y(), urCross.local().z(), Constants.URWELLXRESOLUTION, Constants.URWELLYRESOLUTION); urhot.isDCHit = false; hOTS.add(urhot); From a83e4e3524514dec32f6cc0e077201cade996225 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Mon, 27 Feb 2023 15:34:41 -0500 Subject: [PATCH 10/69] reset uRWell resolution --- .../dc/src/main/java/org/jlab/rec/dc/Constants.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java index 4b33e1734..a881013fc 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java @@ -93,8 +93,8 @@ public static Constants getInstance() { public static final double LTCCPLANE = 653.09; public static final double URWELLLOCALZ = 226.0464; // cm - public static final double URWELLXRESOLUTION = 0.035; // cm - public static final double URWELLYRESOLUTION = 0.035; // cm + public static final double URWELLXRESOLUTION = 0.01; // cm + public static final double URWELLYRESOLUTION = 0.04; // cm // other CLAS12 parameters public static final int NSECT = 6; public static final int NSLAY = 6; From e8c9b2ee0c1ab0d8ca523684d8e4dbec6dee8dc9 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 1 Mar 2023 09:41:19 -0500 Subject: [PATCH 11/69] fix issue for the FastMath package --- .../src/main/java/org/jlab/clas/clas/math/FastMath.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common-tools/clas-math/src/main/java/org/jlab/clas/clas/math/FastMath.java b/common-tools/clas-math/src/main/java/org/jlab/clas/clas/math/FastMath.java index c12983ea0..b058d8be0 100644 --- a/common-tools/clas-math/src/main/java/org/jlab/clas/clas/math/FastMath.java +++ b/common-tools/clas-math/src/main/java/org/jlab/clas/clas/math/FastMath.java @@ -8,7 +8,7 @@ public static enum MathLib { } // controls which algorithms to use - private static MathLib _mathLib = MathLib.SUPERFAST; + private static MathLib _mathLib = MathLib.FAST; /** * Might use standard or fast atan2 From d65561f3a56d423a91170c09b1fa5fcc81d6a0ee Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 1 Mar 2023 13:30:39 -0500 Subject: [PATCH 12/69] fix issue in the FastMath package --- .../src/main/java/org/jlab/clas/clas/math/FastMath.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common-tools/clas-math/src/main/java/org/jlab/clas/clas/math/FastMath.java b/common-tools/clas-math/src/main/java/org/jlab/clas/clas/math/FastMath.java index c12983ea0..b058d8be0 100644 --- a/common-tools/clas-math/src/main/java/org/jlab/clas/clas/math/FastMath.java +++ b/common-tools/clas-math/src/main/java/org/jlab/clas/clas/math/FastMath.java @@ -8,7 +8,7 @@ public static enum MathLib { } // controls which algorithms to use - private static MathLib _mathLib = MathLib.SUPERFAST; + private static MathLib _mathLib = MathLib.FAST; /** * Might use standard or fast atan2 From 3cfc641803590ae8af1c9a46ce7e76c17b0f69ee Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Mon, 6 Mar 2023 16:51:29 -0500 Subject: [PATCH 13/69] cache some sine and cosine calculations in DC reconstruction and forward tracking --- .../main/java/org/jlab/rec/dc/Constants.java | 13 ++++++ .../jlab/rec/dc/cluster/ClusterFitter.java | 3 +- .../java/org/jlab/rec/dc/cross/Cross.java | 27 ++++++------ .../java/org/jlab/rec/dc/hit/FittedHit.java | 2 +- .../java/org/jlab/rec/dc/segment/Segment.java | 12 +++--- .../jlab/rec/dc/segment/SegmentFinder.java | 2 +- .../jlab/rec/dc/track/fit/StateVecsDoca.java | 2 +- .../jlab/rec/dc/trajectory/RoadFinder.java | 9 ++-- .../org/jlab/rec/dc/trajectory/TrackVec.java | 42 ++++++------------- 9 files changed, 54 insertions(+), 58 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java index 3302774d1..d6b8630a3 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java @@ -46,6 +46,19 @@ public static Constants getInstance() { private static boolean ConstantsLoaded = false; public static boolean DEBUG = false; + + // CONSTATNS for TRANSFORMATION + public static final double SIN25 = 0.42261826; + public static final double COS25 = 0.90630779; + public static final double COS30 = 0.86602540; + public static final double SIN6 = 0.10452846; + public static final double COS6 = 0.99452190; + public static final double TAN6 = 0.10510424; + public static final double CTAN6 = 9.51436445; + public static final double[] SINSECTOR60 = {0, 0.86602540, 0.86602540, 0, -0.86602540, -0.86602540}; + public static final double[] COSSECTOR60 = {1, 0.5, -0.5, -1, -0.5, 0.5}; + public static final double[] SINSECTORNEG60 = {0, -0.86602540, -0.86602540, 0, 0.86602540, 0.86602540}; + public static final double[] COSSECTORNEG60 = {1, 0.5, -0.5, -1, -0.5, 0.5}; // PHYSICS CONSTANTS public static final double SPEEDLIGHT = 29.97924580; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/cluster/ClusterFitter.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/cluster/ClusterFitter.java index c9adc3511..3744a3ff6 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/cluster/ClusterFitter.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/cluster/ClusterFitter.java @@ -12,6 +12,7 @@ import org.jlab.rec.dc.hit.FittedHit; import org.jlab.rec.dc.track.fit.basefit.LineFitPars; import org.jlab.rec.dc.track.fit.basefit.LineFitter; +import org.jlab.rec.dc.Constants; public class ClusterFitter { @@ -26,7 +27,7 @@ public class ClusterFitter { private final List y = new ArrayList(); private final List ex = new ArrayList(); private final List ey = new ArrayList(); - private final double stereo = FastMath.cos(Math.toRadians(6.)); + private final double stereo = Constants.COS6; private String CoordinateSystem; // LC= local, TSC = tilted Sector public ClusterFitter() { diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/Cross.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/Cross.java index 6a4751c97..68da99a21 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/Cross.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/Cross.java @@ -10,6 +10,7 @@ import org.jlab.geom.prim.Point3D; import org.jlab.rec.dc.hit.FittedHit; import org.jlab.rec.dc.segment.Segment; +import org.jlab.rec.dc.Constants; /** * The crosses are objects used to find tracks and are characterized by a 3-D @@ -45,10 +46,7 @@ public static long getSerialversionuid() { private Point3D _DirErr; private Segment _seg1; private Segment _seg2; - public boolean isPseudoCross = false; - - private final double cos_tilt = FastMath.cos(Math.toRadians(25.)); - private final double sin_tilt = FastMath.sin(Math.toRadians(25.)); + public boolean isPseudoCross = false; public int recalc; /** @@ -261,7 +259,7 @@ public void set_CrossParams(DCGeant4Factory DcDetector) { //double z = GeometryLoader.dcDetector.getSector(0).getRegionMiddlePlane(this.get_Region()-1).point().z(); double z = DcDetector.getRegionMidpoint(this.get_Region() - 1).z; - double wy_over_wx = (Math.cos(Math.toRadians(6.)) / Math.sin(Math.toRadians(6.))); + double wy_over_wx = Constants.CTAN6; double val_sl1 = this._seg1.get_fittedCluster().get_clusterLineFitSlope(); double val_sl2 = this._seg2.get_fittedCluster().get_clusterLineFitSlope(); double val_it1 = this._seg1.get_fittedCluster().get_clusterLineFitIntercept(); @@ -345,8 +343,8 @@ public String printInfo() { * coordinate system */ public Point3D getCoordsInSector(double X, double Y, double Z) { - double rz = -X * sin_tilt + Z * cos_tilt; - double rx = X * cos_tilt + Z * sin_tilt; + double rz = -X * Constants.SIN25 + Z * Constants.COS25; + double rx = X * Constants.COS25 + Z * Constants.SIN25; return new Point3D(rx, Y, rz); } @@ -361,18 +359,17 @@ public Point3D getCoordsInSector(double X, double Y, double Z) { */ public Point3D getCoordsInLab(double X, double Y, double Z) { Point3D PointInSec = this.getCoordsInSector(X, Y, Z); - double rx = PointInSec.x() * FastMath.cos((this.get_Sector() - 1) * Math.toRadians(60.)) - PointInSec.y() * FastMath.sin((this.get_Sector() - 1) * Math.toRadians(60.)); - double ry = PointInSec.x() * FastMath.sin((this.get_Sector() - 1) * Math.toRadians(60.)) + PointInSec.y() * FastMath.cos((this.get_Sector() - 1) * Math.toRadians(60.)); - + double rx = PointInSec.x() * Constants.COSSECTOR60[this.get_Sector() - 1] - PointInSec.y() * Constants.SINSECTOR60[this.get_Sector() - 1]; + double ry = PointInSec.x() * Constants.SINSECTOR60[this.get_Sector() - 1] + PointInSec.y() * Constants.COSSECTOR60[this.get_Sector() - 1]; return new Point3D(rx, ry, PointInSec.z()); } public Point3D getCoordsInTiltedSector(double X, double Y, double Z) { - double rx = X * FastMath.cos((this.get_Sector() - 1) * Math.toRadians(-60.)) - Y * FastMath.sin((this.get_Sector() - 1) * Math.toRadians(-60.)); - double ry = X * FastMath.sin((this.get_Sector() - 1) * Math.toRadians(-60.)) + Y * FastMath.cos((this.get_Sector() - 1) * Math.toRadians(-60.)); + double rx = X * Constants.COSSECTORNEG60[this.get_Sector() - 1] - Y * Constants.SINSECTORNEG60[this.get_Sector() - 1]; + double ry = X * Constants.SINSECTORNEG60[this.get_Sector() - 1] + Y * Constants.COSSECTORNEG60[this.get_Sector() - 1]; - double rtz = rx * sin_tilt + Z * cos_tilt; - double rtx = rx * cos_tilt - Z * sin_tilt; + double rtz = rx * Constants.SIN25 + Z * Constants.COS25; + double rtx = rx * Constants.COS25 - Z * Constants.SIN25; return new Point3D(rtx, ry, rtz); } @@ -405,7 +402,7 @@ public double[] ReCalcPseudoCross(Cross c2, Cross c3) { */ public void set_CrossDirIntersSegWires() { - double wy_over_wx = (FastMath.cos(Math.toRadians(6.)) / FastMath.sin(Math.toRadians(6.))); + double wy_over_wx = Constants.CTAN6; double val_sl1 = this._seg1.get_fittedCluster().get_clusterLineFitSlope(); double val_sl2 = this._seg2.get_fittedCluster().get_clusterLineFitSlope(); double val_it1 = this._seg1.get_fittedCluster().get_clusterLineFitIntercept(); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java index a13405432..eb5c89fa5 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java @@ -666,7 +666,7 @@ public void calc_GeomCorr(DCGeant4Factory DcDetector, double y) { throw new RuntimeException("invalid region"); } - double MaxSag = Constants.getInstance().getWIREDIST()*A*C*wire*wire*FastMath.cos(Math.toRadians(25.))*FastMath.cos(Math.toRadians(30.)); + double MaxSag = Constants.getInstance().getWIREDIST()*A*C*wire*wire*Constants.COS25*Constants.COS30; double delta_x = MaxSag*(1.-Math.abs(y)/(0.5*wireLen))*(1.-Math.abs(y)/(0.5*wireLen)); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/segment/Segment.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/segment/Segment.java index dcb2fe857..369dc617f 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/segment/Segment.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/segment/Segment.java @@ -287,15 +287,15 @@ public void set_SegmentEndPointsSecCoordSys(DCGeant4Factory DcDetector) { double Z_1 = DcDetector.getWireMidpoint(this.get_Sector() - 1, this.get_Superlayer() - 1, 0, 0).z; double X_1 = this.get_fittedCluster().get_clusterLineFitSlope() * Z_1 + this.get_fittedCluster().get_clusterLineFitIntercept(); - double x1 = FastMath.cos(Math.toRadians(25.)) * X_1 + FastMath.sin(Math.toRadians(25.)) * Z_1; - double z1 = -FastMath.sin(Math.toRadians(25.)) * X_1 + FastMath.cos(Math.toRadians(25.)) * Z_1; + double x1 = Constants.COS25 * X_1 + Constants.SIN25 * Z_1; + double z1 = -Constants.SIN25 * X_1 + Constants.COS25 * Z_1; //double Z_2 = GeometryLoader.dcDetector.getSector(0).getSuperlayer(this.get_Superlayer()-1).getLayer(5).getComponent(0).getMidpoint().z(); double Z_2 = DcDetector.getWireMidpoint(this.get_Sector() - 1, this.get_Superlayer() - 1, 5, 0).z; double X_2 = this.get_fittedCluster().get_clusterLineFitSlope() * Z_2 + this.get_fittedCluster().get_clusterLineFitIntercept(); - double x2 = FastMath.cos(Math.toRadians(25.)) * X_2 + FastMath.sin(Math.toRadians(25.)) * Z_2; - double z2 = -FastMath.sin(Math.toRadians(25.)) * X_2 + FastMath.cos(Math.toRadians(25.)) * Z_2; + double x2 = Constants.COS25 * X_2 + Constants.SIN25 * Z_2; + double z2 = -Constants.SIN25 * X_2 + Constants.COS25 * Z_2; double[] EndPointsArray = new double[4]; EndPointsArray[0] = x1; @@ -350,8 +350,8 @@ public void set_SegmentEndPoints(double[] _SegmentEndPoints) { */ private Plane3D calc_fitPlane(Point3D refPoint, Vector3D refDir) { - double X = Math.pow(-1, (this.get_Superlayer() - 1)) * FastMath.sin(Math.toRadians(6)); - double Y = FastMath.cos(Math.toRadians(6.)); + double X = Math.pow(-1, (this.get_Superlayer() - 1)) * Constants.SIN6; + double Y = Constants.COS6; Vector3D plDir = new Vector3D(X, Y, 0); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/segment/SegmentFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/segment/SegmentFinder.java index 6521979fc..ddb8b3091 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/segment/SegmentFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/segment/SegmentFinder.java @@ -84,7 +84,7 @@ public void get_LayerEfficiencies(Segment seg, DataEvent event, DCGeant4Factory int trjWire = trj.getWireOnTrajectory(seg.get_Sector(), seg.get_Superlayer(), l + 1, trkXMP, DcDetector); //double x = GeometryLoader.dcDetector.getSector(0).getSuperlayer(seg.get_Superlayer()-1).getLayer(l).getComponent(trjWire-1).getMidpoint().x(); double x = DcDetector.getWireMidpoint(seg.get_Sector() - 1, seg.get_Superlayer() - 1, l, trjWire - 1).x; - double cosTkAng = FastMath.cos(Math.toRadians(6.)) * Math.sqrt(1. + seg.get_fittedCluster().get_clusterLineFitSlope() * seg.get_fittedCluster().get_clusterLineFitSlope()); + double cosTkAng = Constants.COS6 * Math.sqrt(1. + seg.get_fittedCluster().get_clusterLineFitSlope() * seg.get_fittedCluster().get_clusterLineFitSlope()); double calc_doca = (x - trkX) * cosTkAng; trkDocas[l] = calc_doca; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/fit/StateVecsDoca.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/fit/StateVecsDoca.java index 7957071ed..25fe85a22 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/fit/StateVecsDoca.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/fit/StateVecsDoca.java @@ -374,7 +374,7 @@ public void init(Track trkcand, double z0, KFitterDoca kf, int c) { double err_sl2 = trkcand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitSlopeErr(); double err_it1 = trkcand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitInterceptErr(); double err_it2 = trkcand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitInterceptErr(); - double wy_over_wx = (FastMath.cos(Math.toRadians(6.)) / FastMath.sin(Math.toRadians(6.))); + double wy_over_wx = Constants.CTAN6; double eux = 0.5 * Math.sqrt(err_sl1 * err_sl1 + err_sl2 * err_sl2); double euy = 0.5 * wy_over_wx * Math.sqrt(err_sl1 * err_sl1 + err_sl2 * err_sl2); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/RoadFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/RoadFinder.java index 42e1d921f..53efdfd09 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/RoadFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/RoadFinder.java @@ -9,6 +9,7 @@ import org.jlab.rec.dc.cluster.FittedCluster; import org.jlab.rec.dc.hit.FittedHit; import org.jlab.rec.dc.segment.Segment; +import org.jlab.rec.dc.Constants; import Jama.Matrix; import org.jlab.detector.geant4.v2.DCGeant4Factory; @@ -168,12 +169,12 @@ private Segment reFit(Segment pseudoSeg, ArrayList segList, DCGeant4Fac int layer = l+1; double z = DcDetector.getWireMidpoint(pseudoSeg.get_Sector()-1, pseudoSeg.get_Superlayer()-1,layer-1,0).z; double trkX = qf.a[0]*z*z+qf.a[1]*z+qf.a[2]; - double delta = (trkX-pseudoSeg.get(l).get_X())/pseudoSeg.get(l).get_CellSize()/FastMath.cos(Math.toRadians(6.)) ; + double delta = (trkX-pseudoSeg.get(l).get_X())/pseudoSeg.get(l).get_CellSize()/Constants.COS6 ; int calcWire = segTrj.getWireOnTrajectory(pseudoSeg.get_Sector(), pseudoSeg.get_Superlayer(), layer, trkX, DcDetector); FittedHit pseudoHit = new FittedHit(segList.get(0).get_Sector(),pseudoSeg.get_Superlayer(), layer, calcWire, 0, 0, -1); - pseudoHit.set_DocaErr(pseudoHit.get_CellSize()/Math.sqrt(12.)/FastMath.cos(Math.toRadians(6.))); + pseudoHit.set_DocaErr(pseudoHit.get_CellSize()/Math.sqrt(12.)/Constants.COS6); pseudoHit.updateHitPosition(DcDetector); pseudoHit.calc_GeomCorr(DcDetector, 0); fpseudoCluster.add(pseudoHit); @@ -209,8 +210,8 @@ private boolean fitRoad(ArrayList segList, DCGeant4Factory DcDetector) X[hitno] = s.get(j).get_X(); //X[hitno] = GeometryLoader.dcDetector.getSector(0).getSuperlayer(s.get(j).get_Superlayer()-1).getLayer(s.get(j).get_Layer()-1).getComponent(s.get(j).get_Wire()-1).getMidpoint().x(); Z[hitno] = s.get(j).get_Z(); - //errX[hitno] = s.get(j).get_DocaErr()/FastMath.cos(Math.toRadians(6.)); - errX[hitno] = s.get(j).get_CellSize()/Math.sqrt(12.)/FastMath.cos(Math.toRadians(6.)); + //errX[hitno] = s.get(j).get_DocaErr()/Constants.COS6; + errX[hitno] = s.get(j).get_CellSize()/Math.sqrt(12.)/Constants.COS6; hitno++; } } diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrackVec.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrackVec.java index 8c65b7c2c..ee102509d 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrackVec.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrackVec.java @@ -1,7 +1,7 @@ package org.jlab.rec.dc.trajectory; import Jama.*; -import org.jlab.clas.clas.math.FastMath; +import org.jlab.rec.dc.Constants; /** * Describes a track pars in the DC. * @author ziegler @@ -228,8 +228,8 @@ public void TransformToTiltSectorFrame() { * @return */ public double[] tilt(double X, double Z, int t) { - double rz = (double)t *X * sin_tilt + Z * cos_tilt; - double rx = X * cos_tilt -(double)t* Z * sin_tilt; + double rz = (double)t *X * Constants.SIN25 + Z * Constants.COS25; + double rx = X * Constants.COS25 -(double)t* Z * Constants.SIN25; return new double[] {rx, rz}; } @@ -243,10 +243,16 @@ public double[] tilt(double X, double Z, int t) { */ public double[] rotateToSec(int sector, double x, double y, int t) { if(sector>0 && sector<7) { - double rx = x * FastMath.cos((sector - 1) * t*rad60) - y * FastMath.sin((sector - 1) * t*rad60); - double ry = x * FastMath.sin((sector - 1) * t*rad60) + y * FastMath.cos((sector - 1) * t*rad60); - - return new double[] {rx, ry}; + if(t == 1){ + double rx = x * Constants.COSSECTOR60[sector - 1] - y * Constants.SINSECTOR60[sector - 1]; + double ry = x * Constants.SINSECTOR60[sector - 1] + y * Constants.COSSECTOR60[sector - 1]; + return new double[] {rx, ry}; + } + else{ + double rx = x * Constants.COSSECTORNEG60[sector - 1] - y * Constants.SINSECTORNEG60[sector - 1]; + double ry = x * Constants.SINSECTORNEG60[sector - 1] + y * Constants.COSSECTORNEG60[sector - 1]; + return new double[] {rx, ry}; + } } else { return null; } @@ -283,26 +289,4 @@ private int calcSector(double x, double y) { } return sector; } - private final double cos_tilt = FastMath.cos(Math.toRadians(25.)); - private final double sin_tilt = FastMath.sin(Math.toRadians(25.)); - - private final double rad60 = Math.toRadians(60.); - /* - public Point3D getCoordsInLab(double X, double Y, double Z) { - Point3D PointInSec = this.getCoordsInSector(X, Y, Z); - double rx = PointInSec.x() * FastMath.cos((this.get_Sector() - 1) * Math.toRadians(60.)) - PointInSec.y() * FastMath.sin((this.get_Sector() - 1) * Math.toRadians(60.)); - double ry = PointInSec.x() * FastMath.sin((this.get_Sector() - 1) * Math.toRadians(60.)) + PointInSec.y() * FastMath.cos((this.get_Sector() - 1) * Math.toRadians(60.)); - - return new Point3D(rx, ry, PointInSec.z()); - } - - public Point3D getCoordsInTiltedSector(double X, double Y, double Z) { - double rx = X * FastMath.cos((this.get_Sector() - 1) * Math.toRadians(-60.)) - Y * FastMath.sin((this.get_Sector() - 1) * Math.toRadians(-60.)); - double ry = X * FastMath.sin((this.get_Sector() - 1) * Math.toRadians(-60.)) + Y * FastMath.cos((this.get_Sector() - 1) * Math.toRadians(-60.)); - - double rtz = rx * sin_tilt + Z * cos_tilt; - double rtx = rx * cos_tilt - Z * sin_tilt; - - return new Point3D(rtx, ry, rtz); - */ } From b7883c814957f5a949375cd1e01febb95f4fbd25 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 8 Mar 2023 13:27:52 -0500 Subject: [PATCH 14/69] use math library to assign values for constant parameters --- .../main/java/org/jlab/rec/dc/Constants.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java index d6b8630a3..f24493180 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java @@ -15,6 +15,7 @@ import org.jlab.geom.base.Detector; import org.jlab.rec.dc.trajectory.TrajectorySurfaces; import org.jlab.utils.groups.IndexedTable; +import org.jlab.clas.clas.math.FastMath; /** * Constants used in the reconstruction @@ -48,16 +49,18 @@ public static Constants getInstance() { public static boolean DEBUG = false; // CONSTATNS for TRANSFORMATION - public static final double SIN25 = 0.42261826; - public static final double COS25 = 0.90630779; - public static final double COS30 = 0.86602540; - public static final double SIN6 = 0.10452846; - public static final double COS6 = 0.99452190; - public static final double TAN6 = 0.10510424; - public static final double CTAN6 = 9.51436445; - public static final double[] SINSECTOR60 = {0, 0.86602540, 0.86602540, 0, -0.86602540, -0.86602540}; + public static final double SIN25 = FastMath.sin(Math.toRadians(25.)); + public static final double COS25 = FastMath.cos(Math.toRadians(25.)); + public static final double COS30 = FastMath.cos(Math.toRadians(30.)); + public static final double SIN6 = FastMath.sin(Math.toRadians(6.)); + public static final double COS6 = FastMath.cos(Math.toRadians(6.)); + public static final double TAN6 = Math.tan(Math.toRadians(6.)); + public static final double CTAN6 = 1/TAN6; + public static final double[] SINSECTOR60 = {0, FastMath.sin(Math.toRadians(60.)), FastMath.sin(Math.toRadians(120.)), 0, + FastMath.sin(Math.toRadians(240.)), FastMath.sin(Math.toRadians(300.))}; public static final double[] COSSECTOR60 = {1, 0.5, -0.5, -1, -0.5, 0.5}; - public static final double[] SINSECTORNEG60 = {0, -0.86602540, -0.86602540, 0, 0.86602540, 0.86602540}; + public static final double[] SINSECTORNEG60 = {0, FastMath.sin(Math.toRadians(-60.)), FastMath.sin(Math.toRadians(-120.)), 0, + FastMath.sin(Math.toRadians(-240.)), FastMath.sin(Math.toRadians(-300.))}; public static final double[] COSSECTORNEG60 = {1, 0.5, -0.5, -1, -0.5, 0.5}; // PHYSICS CONSTANTS From 52c6d81c72f170a165d32084db4308d3fe3f8efd Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 8 Mar 2023 17:12:16 -0500 Subject: [PATCH 15/69] switch to new CCDB --- .../jlab/detector/calib/utils/DatabaseConstantProvider.java | 4 ++-- common-tools/clas-geometry/pom.xml | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/calib/utils/DatabaseConstantProvider.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/calib/utils/DatabaseConstantProvider.java index 7be582175..810e3ace5 100644 --- a/common-tools/clas-detector/src/main/java/org/jlab/detector/calib/utils/DatabaseConstantProvider.java +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/calib/utils/DatabaseConstantProvider.java @@ -12,7 +12,7 @@ import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; -import org.jlab.ccdb.CcdbPackage; +import org.jlab.ccdb.CCDB; import javax.swing.JFrame; import org.jlab.ccdb.Assignment; @@ -137,7 +137,7 @@ private String getEnvironment(){ } private void initialize(String address){ - provider = CcdbPackage.createProvider(address); + provider = CCDB.createProvider(address); LOGGER.log(Level.INFO, "[DB] ---> open connection with : " + address); LOGGER.log(Level.INFO, "[DB] ---> database variation : " + this.variation); diff --git a/common-tools/clas-geometry/pom.xml b/common-tools/clas-geometry/pom.xml index 67c90d725..7e91f2320 100644 --- a/common-tools/clas-geometry/pom.xml +++ b/common-tools/clas-geometry/pom.xml @@ -20,9 +20,9 @@ 11 - org.jlab.ccdb - ccdb - 1.2 + org.jlab.ccdbrcdb + ccdbrcdb + 1.0 From b57b71c9104cda83233ab1f3c2058a03793a96e5 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Mon, 27 Mar 2023 15:55:58 -0400 Subject: [PATCH 16/69] re-design pattern recognition for FD tracking --- .../main/java/cnuphys/swimZ/SwimZResult.java | 52 ++++ .../java/org/jlab/clas/swimtools/Swim.java | 82 ++++++ .../java/org/jlab/rec/dc/cross/Cross.java | 6 +- .../rec/dc/track/TrackCandListFinder.java | 120 ++------- .../track/TrackCandListWithURWellFinder.java | 120 ++------- .../jlab/rec/dc/trajectory/Trajectory.java | 12 +- .../rec/dc/trajectory/TrajectoryFinder.java | 237 ++++++------------ 7 files changed, 263 insertions(+), 366 deletions(-) diff --git a/common-tools/cnuphys/swimmer/src/main/java/cnuphys/swimZ/SwimZResult.java b/common-tools/cnuphys/swimmer/src/main/java/cnuphys/swimZ/SwimZResult.java index cb7754928..3e450f612 100755 --- a/common-tools/cnuphys/swimmer/src/main/java/cnuphys/swimZ/SwimZResult.java +++ b/common-tools/cnuphys/swimmer/src/main/java/cnuphys/swimZ/SwimZResult.java @@ -209,6 +209,58 @@ public double sectorGetBDL(int sector, FieldProbe probe) { return _bdl; } + + /** + * Get the approximate integral |B x dL| + * + * @param sector sector 1..6 + * @param probe the probe use to compute this result trajectory + * @return the approximate integral |B x dL| in kG*cm + */ + public double sectorGetBDLXZPlane(int sector, FieldProbe probe) { + + // only compute if necessary + if (Double.isNaN(_bdl)) { + _bdl = 0; + _pathLength = 0; + + int size = size(); + + SwimZStateVector prev = null; + if (size > 1) { + + double dr[] = new double[3]; + + float b[] = new float[3]; + double bxdl[] = new double[3]; + double bxdlxz[] = new double[3]; + + for (SwimZStateVector next : _trajectory) { + if (prev != null) { + prev.dR(next, dr); + _pathLength += vecmag(dr); + + //get the field at the midpoint + float xmid = (float) ((prev.x + next.x) / 2); + float ymid = (float) ((prev.y + next.y) / 2); + float zmid = (float) ((prev.z + next.z) / 2); + probe.field(sector, xmid, ymid, zmid, b); + + cross(b, dr, bxdl); + bxdlxz[0] = bxdl[0]; + bxdlxz[1] = 0; + bxdlxz[2] = bxdl[2]; + + _bdl += vecmag(bxdlxz); + + } + prev = next; + } + } + } + + return _bdl; + } // usual cross product c = a x b private static void cross(float a[], double b[], double c[]) { diff --git a/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/Swim.java b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/Swim.java index 5fb3fee46..5cec9b2b5 100644 --- a/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/Swim.java +++ b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/Swim.java @@ -279,6 +279,88 @@ public double[] SwimToPlaneTiltSecSys(int sector, double z_cm) { return value; } + + public double[] SwimToPlaneTiltSecSysBdlXZPlane(int sector, double z_cm) { + double z = z_cm / 100; // the magfield method uses meters + double[] value = new double[8]; + + if (_pTot < MINTRKMOM || this.SwimUnPhys==true) // fiducial cut + { + return null; + } + + // use a SwimZResult instead of a trajectory (dph) + SwimZResult szr = null; + + SwimTrajectory traj = null; + double hdata[] = new double[3]; + + try { + + if (_pTot > SWIMZMINMOM) { + + // use the new z swimmer (dph) + // NOTE THE DISTANCE, UNITS FOR swimZ are cm, NOT m like the old + // swimmer (dph) + + double stepSizeCM = stepSize * 100; // convert to cm + + // create the starting SwimZ state vector + SwimZStateVector start = new SwimZStateVector(_x0 * 100, _y0 * 100, _z0 * 100, _pTot, _theta, _phi); + + try { + szr = PC.RCF_z.sectorAdaptiveRK(sector, _charge, _pTot, start, z_cm, stepSizeCM, hdata); + } catch (SwimZException e) { + szr = null; + //System.err.println("[WARNING] Tilted SwimZ Failed for p = " + _pTot); + } + } + + if (szr != null) { + double bdl = szr.sectorGetBDLXZPlane(sector, PC.RCF_z.getProbe()); + double pathLength = szr.getPathLength(); // already in cm + + SwimZStateVector last = szr.last(); + double p3[] = szr.getThreeMomentum(last); + + value[0] = last.x; // xf in cm + value[1] = last.y; // yz in cm + value[2] = last.z; // zf in cm + value[3] = p3[0]; + value[4] = p3[1]; + value[5] = p3[2]; + value[6] = pathLength; + value[7] = bdl / 10; // convert from kg*cm to T*cm + } else { // use old swimmer. Either low momentum or SwimZ failed. + // (dph) + + traj = PC.RCF.sectorSwim(sector, _charge, _x0, _y0, _z0, _pTot, _theta, _phi, z, accuracy, _rMax, + _maxPathLength, stepSize, cnuphys.swim.Swimmer.CLAS_Tolerance, hdata); + + // traj.computeBDL(sector, rprob); + if(traj==null) + return null; + + traj.sectorComputeBDL(sector, PC.RCP); + // traj.computeBDL(rcompositeField); + + double lastY[] = traj.lastElement(); + value[0] = lastY[0] * 100; // convert back to cm + value[1] = lastY[1] * 100; // convert back to cm + value[2] = lastY[2] * 100; // convert back to cm + value[3] = lastY[3] * _pTot; + value[4] = lastY[4] * _pTot; + value[5] = lastY[5] * _pTot; + value[6] = lastY[6] * 100; + value[7] = lastY[7] * 10; + } // use old swimmer + } catch (Exception e) { + e.printStackTrace(); + } + return value; + + } + /** * * @param z_cm diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/Cross.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/Cross.java index 68da99a21..57c78d1f8 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/Cross.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/Cross.java @@ -270,7 +270,7 @@ public void set_CrossParams(DCGeant4Factory DcDetector) { this.set_Point(new Point3D(x, y, z)); - double tanThX = val_sl2; + double tanThX = 0.5 * (val_sl1 + val_sl2); double tanThY = FastMath.atan2(y, z); double uz = 1. / Math.sqrt(1 + tanThX * tanThX + tanThY * tanThY); double ux = uz * tanThX; @@ -293,8 +293,8 @@ public void set_CrossParams(DCGeant4Factory DcDetector) { //double err_x = 0.5*Math.sqrt(err_it1*err_it1+err_it2*err_it2 + z*z*(err_sl1*err_sl1+err_sl2*err_sl2) ); //double err_y = 0.5*wy_over_wx*Math.sqrt(err_it1*err_it1+err_it2*err_it2 +z*z*(err_sl1*err_sl1+err_sl2*err_sl2) ); - double err_x_fix = 0.5 * Math.sqrt(err_it1 * err_it1 + err_it2 * err_it2 + z * z * (err_sl1 * err_sl1 + err_sl2 * err_sl2) + 2 * z * err_cov1 + 2 * z * err_cov2); - double err_y_fix = 0.5 * wy_over_wx * Math.sqrt(err_it1 * err_it1 + err_it2 * err_it2 + z * z * (err_sl1 * err_sl1 + err_sl2 * err_sl2) + 2 * z * err_cov1 + 2 * z * err_cov2); + double err_x_fix = 0.5 * Math.sqrt(err_it1 * err_it1 + err_it2 * err_it2 + z * z * (err_sl1 * err_sl1 + err_sl2 * err_sl2)); + double err_y_fix = 0.5 * wy_over_wx * Math.sqrt(err_it1 * err_it1 + err_it2 * err_it2 + z * z * (err_sl1 * err_sl1 + err_sl2 * err_sl2)); this.set_PointErr(new Point3D(err_x_fix, err_y_fix, 0)); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index 86706c950..e533c28bd 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -136,7 +136,7 @@ private double getHitBasedFitChi2ToCrosses(int sector, double x1, double y1, dou private double[] getTrackInitFit(int sector, double x1, double y1, double z1, double x2, double y2, double z2, double x3, double y3, double z3, double ux, double uy, double uz, double thX, double thY, - double theta1, double theta3, + double theta1, double theta3, double a, double iBdl, double TORSCALE, Swim dcSwim) { if (theta1 < -998 || theta3 < -998) { return new double[]{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; @@ -146,17 +146,15 @@ private double[] getTrackInitFit(int sector, double x1, double y1, double z1, double chi2 = 0; // assume err =1 on points double intBdl = 0; - double p = calcInitTrkP(ux, uy, uz, thX, thY, - theta1, theta3, - iBdl, TORSCALE); + double p = calcInitTrkP(thX, thY, theta1, theta3, iBdl); double p_x = ux * p; double p_y = uy * p; double p_z = uz * p; - int q = calcInitTrkQ(theta1, theta3, TORSCALE); + int q = calcInitTrkQ(a, TORSCALE); dcSwim.SetSwimParameters(x1, y1, z1, p_x, p_y, p_z, q); - double[] R = dcSwim.SwimToPlaneTiltSecSys(sector, z2); + double[] R = dcSwim.SwimToPlaneTiltSecSysBdlXZPlane(sector, z2); if (R == null) { return new double[]{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; } @@ -166,7 +164,7 @@ private double[] getTrackInitFit(int sector, double x1, double y1, double z1, dcSwim.SetSwimParameters(R[0], R[1], R[2], R[3], R[4], R[5], q); - R = dcSwim.SwimToPlaneTiltSecSys(sector, z3); + R = dcSwim.SwimToPlaneTiltSecSysBdlXZPlane(sector, z3); if (R == null) { return new double[]{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; } @@ -180,9 +178,7 @@ private double[] getTrackInitFit(int sector, double x1, double y1, double z1, return pars; } - private double calcInitTrkP(double ux, double uy, double uz, double thX, double thY, - double theta1, double theta3, - double iBdl, double TORSCALE) { + private double calcInitTrkP(double thX, double thY, double theta1, double theta3, double iBdl) { double deltaTheta = theta3 - theta1; if (deltaTheta == 0) { return Double.POSITIVE_INFINITY; @@ -196,12 +192,9 @@ private double calcInitTrkP(double ux, double uy, double uz, double thX, double return p; } - private int calcInitTrkQ(double theta1, double theta3, - double TORSCALE) { - double deltaTheta = theta3 - theta1; - + private int calcInitTrkQ(double a, double TORSCALE) { //positive charges bend outward for nominal GEMC field configuration - int q = (int) Math.signum(deltaTheta); + int q = (int) Math.signum(a); q *= (int) -1 * Math.signum(TORSCALE); // flip the charge according to the field scale return q; @@ -1010,95 +1003,16 @@ private List findCurvedTracks(CrossList crossList, DCGeant4Factory DcDete double theta3s1 = Math.atan(cand.get(2).get_Segment1().get_fittedCluster().get_clusterLineFitSlope()); double theta1s1 = Math.atan(cand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitSlope()); - if (cand.get(0).get_Segment2().get_Id() == -1) { - theta1s2 = theta1s1; //do not use - } //theta1s2=-999; //do not use - if (cand.get(0).get_Segment1().get_Id() == -1) { - theta1s1 = theta1s2; - } - //theta1s1=-999; - if (cand.get(2).get_Segment2().get_Id() == -1) { - theta3s2 = theta3s1; - } - //theta3s2=-999; - if (cand.get(2).get_Segment1().get_Id() == -1) { - theta3s1 = theta3s2; - } - //theta3s1=-999; - double theta3 = 0; - double theta1 = 0; - - double chisq = Double.POSITIVE_INFINITY; - double chi2; - double iBdl = traj.getIntegralBdl(); - double[] pars; - - if(LOGGER.getLevel()==Level.FINE) { - startTime = System.currentTimeMillis(); - } - pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, - ux, uy, uz, thX, thY, - theta1s1, theta3s1, - traj.getIntegralBdl(), TORSCALE, dcSwim); - chi2 = pars[0]; - if (chi2 < chisq) { - chisq = chi2; - theta1 = theta1s1; - theta3 = theta3s1; - iBdl = pars[1]; - } - - LOGGER.log(Level.FINE, "TrackInitFit-1 = " + (System.currentTimeMillis() - startTime)); - - if(LOGGER.getLevel()==Level.FINE) { - startTime = System.currentTimeMillis(); - } - pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, - ux, uy, uz, thX, thY, - theta1s1, theta3s2, - traj.getIntegralBdl(), TORSCALE, dcSwim); - chi2 = pars[0]; - if (chi2 < chisq) { - chisq = chi2; - theta1 = theta1s1; - theta3 = theta3s2; - iBdl = pars[1]; - } - - LOGGER.log(Level.FINE, "TrackInitFit-2 = " + (System.currentTimeMillis() - startTime)); + double theta1 = 0.5 * (theta1s1 + theta1s2); + double theta3 = 0.5 * (theta3s1 + theta3s2); - if(LOGGER.getLevel()==Level.FINE) { - startTime = System.currentTimeMillis(); - } - pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, + double[] pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, ux, uy, uz, thX, thY, - theta1s2, theta3s1, + theta1, theta3, traj.getA(), traj.getIntegralBdl(), TORSCALE, dcSwim); - chi2 = pars[0]; - if (chi2 < chisq) { - chisq = chi2; - theta1 = theta1s2; - theta3 = theta3s1; - iBdl = pars[1]; - } - - LOGGER.log(Level.FINE, "TrackInitFit-3 = " + (System.currentTimeMillis() - startTime)); - - if(LOGGER.getLevel()==Level.FINE) { - startTime = System.currentTimeMillis(); - } - pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, - ux, uy, uz, thX, thY, - theta1s2, theta3s2, - traj.getIntegralBdl(), TORSCALE, dcSwim); - chi2 = pars[0]; - if (chi2 < chisq) { - theta1 = theta1s2; - theta3 = theta3s2; - iBdl = pars[1]; - } - LOGGER.log(Level.FINE, "TrackInitFit-4 = " + (System.currentTimeMillis() - startTime)); + double chi2 = pars[0]; + double iBdl = pars[1]; if (chi2 > Constants.SEEDCUT && donotapplyCuts == false) { continue; @@ -1109,13 +1023,11 @@ private List findCurvedTracks(CrossList crossList, DCGeant4Factory DcDete //double iBdl = traj.getIntegralBdl(); if (iBdl != 0) { // momentum estimate if Bdl is non zero and the track has curvature - double p = calcInitTrkP(ux, uy, uz, thX, thY, - theta1, theta3, - iBdl, TORSCALE); + double p = calcInitTrkP(thX, thY, theta1, theta3, iBdl); if(LOGGER.getLevel()==Level.FINE) { startTime = System.currentTimeMillis(); } - int q = this.calcInitTrkQ(theta1, theta3, TORSCALE); + int q = this.calcInitTrkQ(traj.getA(), TORSCALE); LOGGER.log(Level.FINE, "calcInitTrkQ = " + (System.currentTimeMillis() - startTime)); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java index 9a0e0ffa4..56c84b09e 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -137,7 +137,7 @@ private double getHitBasedFitChi2ToCrosses(int sector, double x1, double y1, dou private double[] getTrackInitFit(int sector, double x1, double y1, double z1, double x2, double y2, double z2, double x3, double y3, double z3, double ux, double uy, double uz, double thX, double thY, - double theta1, double theta3, + double theta1, double theta3, double a, double iBdl, double TORSCALE, Swim dcSwim) { if (theta1 < -998 || theta3 < -998) { return new double[]{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; @@ -147,17 +147,15 @@ private double[] getTrackInitFit(int sector, double x1, double y1, double z1, double chi2 = 0; // assume err =1 on points double intBdl = 0; - double p = calcInitTrkP(ux, uy, uz, thX, thY, - theta1, theta3, - iBdl, TORSCALE); + double p = calcInitTrkP(thX, thY, theta1, theta3, iBdl); double p_x = ux * p; double p_y = uy * p; double p_z = uz * p; - int q = calcInitTrkQ(theta1, theta3, TORSCALE); + int q = calcInitTrkQ(a, TORSCALE); dcSwim.SetSwimParameters(x1, y1, z1, p_x, p_y, p_z, q); - double[] R = dcSwim.SwimToPlaneTiltSecSys(sector, z2); + double[] R = dcSwim.SwimToPlaneTiltSecSysBdlXZPlane(sector, z2); if (R == null) { return new double[]{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; } @@ -167,7 +165,7 @@ private double[] getTrackInitFit(int sector, double x1, double y1, double z1, dcSwim.SetSwimParameters(R[0], R[1], R[2], R[3], R[4], R[5], q); - R = dcSwim.SwimToPlaneTiltSecSys(sector, z3); + R = dcSwim.SwimToPlaneTiltSecSysBdlXZPlane(sector, z3); if (R == null) { return new double[]{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; } @@ -181,9 +179,7 @@ private double[] getTrackInitFit(int sector, double x1, double y1, double z1, return pars; } - private double calcInitTrkP(double ux, double uy, double uz, double thX, double thY, - double theta1, double theta3, - double iBdl, double TORSCALE) { + private double calcInitTrkP(double thX, double thY, double theta1, double theta3, double iBdl) { double deltaTheta = theta3 - theta1; if (deltaTheta == 0) { return Double.POSITIVE_INFINITY; @@ -197,12 +193,9 @@ private double calcInitTrkP(double ux, double uy, double uz, double thX, double return p; } - private int calcInitTrkQ(double theta1, double theta3, - double TORSCALE) { - double deltaTheta = theta3 - theta1; - + private int calcInitTrkQ(double a, double TORSCALE) { //positive charges bend outward for nominal GEMC field configuration - int q = (int) Math.signum(deltaTheta); + int q = (int) Math.signum(a); q *= (int) -1 * Math.signum(TORSCALE); // flip the charge according to the field scale return q; @@ -1039,95 +1032,16 @@ private List findCurvedTracks(URWellDCCrossesList urDCCrossesList, DCGean double theta3s1 = Math.atan(cand.get(2).get_Segment1().get_fittedCluster().get_clusterLineFitSlope()); double theta1s1 = Math.atan(cand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitSlope()); - if (cand.get(0).get_Segment2().get_Id() == -1) { - theta1s2 = theta1s1; //do not use - } //theta1s2=-999; //do not use - if (cand.get(0).get_Segment1().get_Id() == -1) { - theta1s1 = theta1s2; - } - //theta1s1=-999; - if (cand.get(2).get_Segment2().get_Id() == -1) { - theta3s2 = theta3s1; - } - //theta3s2=-999; - if (cand.get(2).get_Segment1().get_Id() == -1) { - theta3s1 = theta3s2; - } - //theta3s1=-999; - double theta3 = 0; - double theta1 = 0; - - double chisq = Double.POSITIVE_INFINITY; - double chi2; - double iBdl = traj.getIntegralBdl(); - double[] pars; - - if(LOGGER.getLevel()==Level.FINE) { - startTime = System.currentTimeMillis(); - } - pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, - ux, uy, uz, thX, thY, - theta1s1, theta3s1, - traj.getIntegralBdl(), TORSCALE, dcSwim); - chi2 = pars[0]; - if (chi2 < chisq) { - chisq = chi2; - theta1 = theta1s1; - theta3 = theta3s1; - iBdl = pars[1]; - } - - LOGGER.log(Level.FINE, "TrackInitFit-1 = " + (System.currentTimeMillis() - startTime)); - - if(LOGGER.getLevel()==Level.FINE) { - startTime = System.currentTimeMillis(); - } - pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, - ux, uy, uz, thX, thY, - theta1s1, theta3s2, - traj.getIntegralBdl(), TORSCALE, dcSwim); - chi2 = pars[0]; - if (chi2 < chisq) { - chisq = chi2; - theta1 = theta1s1; - theta3 = theta3s2; - iBdl = pars[1]; - } - - LOGGER.log(Level.FINE, "TrackInitFit-2 = " + (System.currentTimeMillis() - startTime)); + double theta1 = 0.5 * (theta1s1 + theta1s2); + double theta3 = 0.5 * (theta3s1 + theta3s2); - if(LOGGER.getLevel()==Level.FINE) { - startTime = System.currentTimeMillis(); - } - pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, + double[] pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, ux, uy, uz, thX, thY, - theta1s2, theta3s1, + theta1, theta3, traj.getA(), traj.getIntegralBdl(), TORSCALE, dcSwim); - chi2 = pars[0]; - if (chi2 < chisq) { - chisq = chi2; - theta1 = theta1s2; - theta3 = theta3s1; - iBdl = pars[1]; - } - - LOGGER.log(Level.FINE, "TrackInitFit-3 = " + (System.currentTimeMillis() - startTime)); - - if(LOGGER.getLevel()==Level.FINE) { - startTime = System.currentTimeMillis(); - } - pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, - ux, uy, uz, thX, thY, - theta1s2, theta3s2, - traj.getIntegralBdl(), TORSCALE, dcSwim); - chi2 = pars[0]; - if (chi2 < chisq) { - theta1 = theta1s2; - theta3 = theta3s2; - iBdl = pars[1]; - } - LOGGER.log(Level.FINE, "TrackInitFit-4 = " + (System.currentTimeMillis() - startTime)); + double chi2 = pars[0]; + double iBdl = pars[1]; if (chi2 > Constants.SEEDCUT && donotapplyCuts == false) { continue; @@ -1138,13 +1052,11 @@ private List findCurvedTracks(URWellDCCrossesList urDCCrossesList, DCGean //double iBdl = traj.getIntegralBdl(); if (iBdl != 0) { // momentum estimate if Bdl is non zero and the track has curvature - double p = calcInitTrkP(ux, uy, uz, thX, thY, - theta1, theta3, - iBdl, TORSCALE); + double p = calcInitTrkP(thX, thY, theta1, theta3, iBdl); if(LOGGER.getLevel()==Level.FINE) { startTime = System.currentTimeMillis(); } - int q = this.calcInitTrkQ(theta1, theta3, TORSCALE); + int q = this.calcInitTrkQ(traj.getA(), TORSCALE); LOGGER.log(Level.FINE, "calcInitTrkQ = " + (System.currentTimeMillis() - startTime)); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java index a475f8cb0..3719402cd 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java @@ -31,9 +31,19 @@ public Trajectory() { private double pathLength; private List stateVecs; private List trajStateVecs = new ArrayList<>(); - + private final static double TOLERANCE = 0.1; // trajectory toleerance (cm) + + private double a; // Parameter a for fitting function in xz plane: f(x) = a*z^2 + b*z + c + + public void setA(double a){ + this.a = a; + } + public double getA(){ + return a; + } + public List getStateVecs() { return stateVecs; } diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrajectoryFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrajectoryFinder.java index 870dce6b0..9fa71474d 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrajectoryFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrajectoryFinder.java @@ -8,6 +8,7 @@ import org.jlab.rec.dc.Constants; import org.jlab.rec.dc.cross.Cross; import trackfitter.fitter.LineFitter; +import Jama.Matrix; /** * A driver class to find the trajectory of a track candidate. NOTE THAT THE PATH TO FIELD MAPS IS SET BY THE CLARA_SERVICES ENVIRONMENT VAR. @@ -19,8 +20,6 @@ public class TrajectoryFinder { private LineFitter lineFit; - private double[] tanTheta_x_fitCoeff; - private double[] tanTheta_y_fitCoeff; private double[] x_fitCoeff; private double[] y_fitCoeff; @@ -34,15 +33,14 @@ public class TrajectoryFinder { public double mmStepSizeForIntBdl = 10; private double PathLength; - + /** * * @return the value of the integral of the magnetic field over the path traveled by the particle as estimated from the fits to the crosses. */ int counter =0; + public double TrajChisqProbFitXZ; - public double TrajChisqProbFitYZ; - /** * * @param candCrossList the input list of crosses used in determining a trajectory @@ -58,12 +56,13 @@ public Trajectory findTrajectory(List candCrossList, DCGeant4Factory DcDe traj.addAll(candCrossList); traj.setSector(candCrossList.get(0).get_Sector()); fitTrajectory(traj); - if (this.TrajChisqProbFitXZ getStateVecsAlongTrajectory(double x0, double y0, double z0, double tanTheta_x, double tanTheta_y, double p, int q, DCGeant4Factory DcDetector) { -// //initialize at target -// dcSwim.SetSwimParameters(x0, y0, z0, tanTheta_x, tanTheta_y, p, q); -// //position array -// double[] X = new double[36]; -// double[] Y = new double[36]; -// double[] Z = new double[36]; -// double[] thX = new double[36]; -// double[] thY = new double[36]; -// -// //Z[0] = GeometryLoader.dcDetector.getSector(0).getSuperlayer(0).getLayer(0).getPlane().point().z(); -// Z[0] = DcDetector.getLayerMidpoint(0, 0).z; -// double[] swamPars = dcSwim.SwimToPlane(Z[0]) ; -// X[0] = swamPars[0]; -// Y[0] = swamPars[1]; -// thX[0] = swamPars[3]/swamPars[5]; -// thY[0] = swamPars[4]/swamPars[5]; -// double pathLen = swamPars[6]; -// int planeIdx = 0; -// int lastSupLyrIdx = 0; -// int lastLyrIdx = 0; -// List stateVecAtPlanesList = new ArrayList(36); -// -// stateVecAtPlanesList.add(new StateVec(X[0],Y[0],thX[0], thY[0])); -// stateVecAtPlanesList.get(stateVecAtPlanesList.size()-1).setPathLength(pathLen); -// for(int superlayerIdx =0; superlayerIdx<6; superlayerIdx++) { -// for(int layerIdx =0; layerIdx<6; layerIdx++) { -// if(superlayerIdx ==0 && layerIdx==0) { -// continue; -// } else { -// // move to the next plane and determine the swam track parameters at that plane -// planeIdx++; -// dcSwim.SetSwimParameters(X[planeIdx-1], Y[planeIdx-1], Z[planeIdx-1], thX[planeIdx-1], thY[planeIdx-1], p, q); -// //Z[layerIdx] = GeometryLoader.dcDetector.getSector(0).getSuperlayer(superlayerIdx).getLayer(layerIdx).getPlane().point().z(); -// Z[planeIdx] = DcDetector.getLayerMidpoint(superlayerIdx, layerIdx).z; -// -// swamPars = dcSwim.SwimToPlane(Z[planeIdx]) ; -// X[planeIdx] = swamPars[0]; -// Y[planeIdx] = swamPars[1]; -// thX[planeIdx] = swamPars[3]/swamPars[5]; -// thY[planeIdx] = swamPars[4]/swamPars[5]; -// pathLen+=swamPars[6]; -// StateVec stVec = new StateVec(X[planeIdx],Y[planeIdx],thX[planeIdx], thY[planeIdx]); -// stVec.set_planeIdx(planeIdx); -// stVec.setPathLength(pathLen); -// stateVecAtPlanesList.add(stVec); -// } -// lastSupLyrIdx = superlayerIdx; -// lastLyrIdx = layerIdx; -// } -// } -// // return the list of state vectors at the list of measurement planes -// return stateVecAtPlanesList; -// } -// + /** * * @param DcDetector @@ -205,109 +143,100 @@ public List getStateVecsAlongTrajectory(DCGeant4Factory DcDetector) { * and constraining the quadratic parameters of the function describing the position values of the state vecs. * @param candCrossList list of crosses used in the fit */ - public void fitTrajectory(List candCrossList) { - tanTheta_x_fitCoeff = new double[2]; - tanTheta_y_fitCoeff = new double[2]; + public void fitTrajectory(List candCrossList) { x_fitCoeff = new double[3]; y_fitCoeff = new double[3]; - - + double[] theta_x = new double[3]; double[] theta_x_err = new double[3]; - double[] theta_y = new double[3]; - double[] theta_y_err = new double[3]; double[] x = new double[3]; - double[] x_err = new double[3]; double[] y = new double[3]; - double[] y_err = new double[3]; double[] z = new double[3]; - for (int i =0; i<3; i++) { + double[] x_err = new double[3]; + double[] y_err = new double[3]; + + for (int i = 0; i < 3; i++) { // make sure that the track direction makes sense - if(candCrossList.get(i).get_Dir().z()==0) { + if (candCrossList.get(i).get_Dir().z() == 0) { return; } x[i] = candCrossList.get(i).get_Point().x(); - x_err[i] = candCrossList.get(i).get_PointErr().x(); y[i] = candCrossList.get(i).get_Point().y(); - y_err[i] = candCrossList.get(i).get_PointErr().y(); z[i] = candCrossList.get(i).get_Point().z(); - theta_x[i] = candCrossList.get(i).get_Dir().x()/candCrossList.get(i).get_Dir().z(); - theta_x_err[i] = calcTanErr(candCrossList.get(i).get_Dir().x(),candCrossList.get(i).get_Dir().z(),candCrossList.get(i).get_DirErr().x(),candCrossList.get(i).get_DirErr().z()); - theta_y[i] = candCrossList.get(i).get_Dir().y()/candCrossList.get(i).get_Dir().z(); - theta_y_err[i] = calcTanErr(candCrossList.get(i).get_Dir().y(),candCrossList.get(i).get_Dir().z(),candCrossList.get(i).get_DirErr().y(),candCrossList.get(i).get_DirErr().z()); + x_err[i] = candCrossList.get(i).get_PointErr().x(); + y_err[i] = candCrossList.get(i).get_PointErr().x(); + + theta_x[i] = candCrossList.get(i).get_Dir().x() / candCrossList.get(i).get_Dir().z(); + theta_x_err[i] = calcTanErr(candCrossList.get(i).get_Dir().x(), candCrossList.get(i).get_Dir().z(), candCrossList.get(i).get_DirErr().x(), candCrossList.get(i).get_DirErr().z()); } lineFit = new LineFitter(); boolean linefitstatusOK = lineFit.fitStatus(z, theta_x, new double[3], theta_x_err, 3); - - // tan_thetax = alpha*z + beta; - // x = a*z^2 +b*z +c - if (linefitstatusOK) { - double alpha = lineFit.getFit().slope(); - double beta = lineFit.getFit().intercept(); - - - double a = alpha/2; - double b = beta; - - double sum_inv_xerr = 0; - double sum_X_ov_errX = 0; - for (int i =0; i<3; i++) { - x[i]-=a*z[i]*z[i]+b*z[i]; - sum_inv_xerr += 1./x_err[i]; - sum_X_ov_errX += x[i]/x_err[i]; - } - if(sum_inv_xerr==0) { - return; - } - double c = sum_X_ov_errX/sum_inv_xerr; - - tanTheta_x_fitCoeff[0] = alpha; - tanTheta_x_fitCoeff[1] = beta; - - x_fitCoeff[0] = a; - x_fitCoeff[1] = b; - x_fitCoeff[2] = c; - } TrajChisqProbFitXZ = lineFit.getFit().getProb(); - lineFit = new LineFitter(); - linefitstatusOK = lineFit.fitStatus(z, theta_y, new double[3], theta_y_err, 3); - - // tan_thetay = alpha*z + beta; - // y = a*z^2 +b*z +c - if (linefitstatusOK) { - double alpha = lineFit.getFit().slope(); - double beta = lineFit.getFit().intercept(); - - double a = alpha/2; - double b = beta; - - double sum_inv_yerr = 0; - double sum_Y_ov_errY = 0; - for (int i =0; i<3; i++) { - y[i]-=a*z[i]*z[i]+b*z[i]; - sum_inv_yerr += 1./y_err[i]; - sum_Y_ov_errY += y[i]/y_err[i]; - } - if(sum_inv_yerr==0) { - return; + x_fitCoeff = quadraticLRFit(z, x, x_err); + y_fitCoeff = quadraticLRFit(z, y, y_err); + } + + private double[] quadraticLRFit(double[] x, double[] y, double[] err) { + double[] ret = {0., 0., 0.}; + + Matrix A = new Matrix(3, 3); + Matrix V = new Matrix(3, 1); + double sum1 = 0.0; + double sum2 = 0.0; + double sum3 = 0.0; + double sum4 = 0.0; + double sum5 = 0.0; + double sum6 = 0.0; + double sum7 = 0.0; + double sum8 = 0.0; + for (int i = 0; i < x.length; ++i) { + double y1 = y[i]; + double x1 = x[i]; + double x2 = x1 * x1; + double x3 = x2 * x1; + double x4 = x2 * x2; + double e2 = err[i] * err[i]; + sum1 += x4 / e2; + sum2 += x3 / e2; + sum3 += x2 / e2; + sum4 += x1 / e2; + sum5 += 1.0 / e2; + sum6 += y1 * x2 / e2; + sum7 += y1 * x1 / e2; + sum8 += y1 / e2; + } + A.set(0, 0, sum1); + A.set(0, 1, sum2); + A.set(0, 2, sum3); + A.set(1, 0, sum2); + A.set(1, 1, sum3); + A.set(1, 2, sum4); + A.set(2, 0, sum3); + A.set(2, 1, sum4); + A.set(2, 2, sum5); + V.set(0, 0, sum6); + V.set(1, 0, sum7); + V.set(2, 0, sum8); + Matrix Ainv = A.inverse(); + Matrix X; + try { + X = Ainv.times(V); + for (int i = 0; i < 3; ++i) { + ret[i] = X.get(i, 0); } - double c = sum_Y_ov_errY/sum_inv_yerr; - tanTheta_y_fitCoeff[0] = alpha; - tanTheta_y_fitCoeff[1] = beta; - - y_fitCoeff[0] = a; - y_fitCoeff[1] = b; - y_fitCoeff[2] = c; + } catch (ArithmeticException e) { + // TODO Auto-generated catch block } - TrajChisqProbFitYZ = lineFit.getFit().getProb(); + return (ret); } + /** * * @param num From 17f0c63e8d5dbd4a7131037dcce4483778575333 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 30 Mar 2023 13:03:40 -0400 Subject: [PATCH 17/69] reset covaraince matrix for initial state --- .../main/java/org/jlab/rec/dc/Constants.java | 15 +++++++++ .../rec/dc/track/TrackCandListFinder.java | 27 ++++++---------- .../track/TrackCandListWithURWellFinder.java | 32 ++++++------------- .../java/org/jlab/service/dc/DCTBEngine.java | 31 ++++++++---------- .../org/jlab/service/dc/DCURWellTBEngine.java | 30 +++++++---------- 5 files changed, 59 insertions(+), 76 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java index 91dab0cd9..9149e9ce2 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java @@ -62,6 +62,21 @@ public static Constants getInstance() { public static final double[] SINSECTORNEG60 = {0, FastMath.sin(Math.toRadians(-60.)), FastMath.sin(Math.toRadians(-120.)), 0, FastMath.sin(Math.toRadians(-240.)), FastMath.sin(Math.toRadians(-300.))}; public static final double[] COSSECTORNEG60 = {1, 0.5, -0.5, -1, -0.5, 0.5}; + + // Uncertainties for initial state + public static final double HBINITIALSTATEUNCSCALE = 1.5; + public static final double HBINITIALSTATEXUNC = 7.8; + public static final double HBINITIALSTATEYUNC = 5.7; + public static final double HBINITIALSTATETXUNC = 0.063; + public static final double HBINITIALSTATETYUNC = 0.036; + public static final double HBINITIALSTATEQUNC = 0.13; + + public static final double TBINITIALSTATEUNCSCALE = 1.5; + public static final double TBINITIALSTATEXUNC = 0.11; + public static final double TBINITIALSTATEYUNC = 0.98; + public static final double TBINITIALSTATETXUNC = 0.0024; + public static final double TBINITIALSTATETYUNC = 0.0091; + public static final double TBINITIALSTATEQUNC = 0.0085; // PHYSICS CONSTANTS public static final double SPEEDLIGHT = 29.97924580; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index e533c28bd..cd9df59da 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -1144,29 +1144,20 @@ public void getInitState(Track trkcand, double z0, int c, org.jlab.clas.tracking RungeKuttaDoca rk = new RungeKuttaDoca(); rk.SwimToZ(trkcand.get(0).get_Sector(), initStateVec, dcSwim, z0, bf); - - //LOGGER.log(Level.FINE, (0)+"] init "+this.trackTraj.get(0).x+","+this.trackTraj.get(0).y+","+ - // this.trackTraj.get(0).z+","+this.trackTraj.get(0).tx+","+this.trackTraj.get(0).ty+" "+1/this.trackTraj.get(0).Q); - double err_sl1 = trkcand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitSlopeErr(); - double err_sl2 = trkcand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitSlopeErr(); - double err_it1 = trkcand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitInterceptErr(); - double err_it2 = trkcand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitInterceptErr(); - double wy_over_wx = (FastMath.cos(Math.toRadians(6.)) / FastMath.sin(Math.toRadians(6.))); - - double eux = 0.5 * Math.sqrt(err_sl1 * err_sl1 + err_sl2 * err_sl2); - double euy = 0.5 * wy_over_wx * Math.sqrt(err_sl1 * err_sl1 + err_sl2 * err_sl2); - double z = trkcand.get(0).get_Point().z(); - double ex = 0.5 * Math.sqrt(err_it1 * err_it1 + err_it2 * err_it2 + z * z * (err_sl1 * err_sl1 + err_sl2 * err_sl2)); - double ey = 0.5 * wy_over_wx * Math.sqrt(err_it1 * err_it1 + err_it2 * err_it2 + z * z * (err_sl1 * err_sl1 + err_sl2 * err_sl2)); - double epSq = 0.001 * trkcand.get_P() * trkcand.get_P(); + + double ex = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATEXUNC; + double ey = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATEYUNC; + double etx = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATETXUNC; + double ety = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATETYUNC; + double eQ = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATEQUNC; Matrix initCMatrix = new Matrix(); initCMatrix.set(ex * ex, 0, 0, 0, 0, 0, ey * ey, 0, 0, 0, - 0, 0, eux * eux, 0, 0, - 0, 0, 0, euy * euy, 0, - 0, 0, 0, 0, epSq + 0, 0, etx * etx, 0, 0, + 0, 0, 0, ety * ety, 0, + 0, 0, 0, 0, eQ * eQ ); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java index 56c84b09e..0aa190a4c 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -1174,32 +1174,20 @@ public void getInitState(Track trkcand, double z0, int c, org.jlab.clas.tracking RungeKuttaDoca rk = new RungeKuttaDoca(); rk.SwimToZ(trkcand.get(0).get_Sector(), initStateVec, dcSwim, z0, bf); - //LOGGER.log(Level.FINE, (0)+"] init "+this.trackTraj.get(0).x+","+this.trackTraj.get(0).y+","+ - // this.trackTraj.get(0).z+","+this.trackTraj.get(0).tx+","+this.trackTraj.get(0).ty+" "+1/this.trackTraj.get(0).Q); - double err_sl1 = trkcand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitSlopeErr(); - double err_sl2 = trkcand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitSlopeErr(); - double err_it1 = trkcand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitInterceptErr(); - double err_it2 = trkcand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitInterceptErr(); - double wy_over_wx = (FastMath.cos(Math.toRadians(6.)) / FastMath.sin(Math.toRadians(6.))); - - double eux = 0.5 * Math.sqrt(err_sl1 * err_sl1 + err_sl2 * err_sl2); - double euy = 0.5 * wy_over_wx * Math.sqrt(err_sl1 * err_sl1 + err_sl2 * err_sl2); - double z = trkcand.get(0).get_Point().z(); - double ex = 0.5 * Math.sqrt(err_it1 * err_it1 + err_it2 * err_it2 + z * z * (err_sl1 * err_sl1 + err_sl2 * err_sl2)); - double ey = 0.5 * wy_over_wx * Math.sqrt(err_it1 * err_it1 + err_it2 * err_it2 + z * z * (err_sl1 * err_sl1 + err_sl2 * err_sl2)); - double epSq = 0.001 * trkcand.get_P() * trkcand.get_P(); - + double ex = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATEXUNC; + double ey = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATEYUNC; + double etx = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATETXUNC; + double ety = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATETYUNC; + double eQ = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATEQUNC; Matrix initCMatrix = new Matrix(); initCMatrix.set(ex * ex, 0, 0, 0, 0, 0, ey * ey, 0, 0, 0, - 0, 0, eux * eux, 0, 0, - 0, 0, 0, euy * euy, 0, - 0, 0, 0, 0, epSq - ); - - - initStateVec.CM = initCMatrix; + 0, 0, etx * etx, 0, 0, + 0, 0, 0, ety * ety, 0, + 0, 0, 0, 0, eQ * eQ + ); + initStateVec.CM = initCMatrix; } public List getMeasSurfaces(Track trkcand, DCGeant4Factory DcDetector) { diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index 964cad5f1..4f85f636e 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -342,28 +342,23 @@ public void getInitState(Track trkcand, double z0, org.jlab.clas.tracking.kalman RungeKuttaDoca rk = new RungeKuttaDoca(); rk.SwimToZ(trkcand.get(0).get_Sector(), initStateVec, dcSwim, z0, bf); - - //initCM.covMat = trkcand.get_CovMat(); - //test - StateVecs svs = new StateVecs(); - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec rinitSV = svs.new StateVec(0); - rinitSV.x = trkcand.getFinalStateVec().x(); - rinitSV.y = trkcand.getFinalStateVec().y(); - rinitSV.z = trkcand.getFinalStateVec().getZ(); - rinitSV.tx = trkcand.getFinalStateVec().tanThetaX(); - rinitSV.ty = trkcand.getFinalStateVec().tanThetaY(); - rinitSV.Q = ((double) trkcand.get_Q())/trkcand.get_P(); - double[] FTF = new double[25]; - double[] F = this.F(trkcand.get(0).get_Sector(), z0, rinitSV, rk, dcSwim, bf); - for(int i = 0; i<5; i++) { - FTF[i*5+i]=F[i]*F[i]; - - } + + double ex = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATEXUNC; + double ey = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATEYUNC; + double etx = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATETXUNC; + double ety = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATETYUNC; + double eQ = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATEQUNC; //Matrix initCMatrix = new Matrix(FTF); Matrix initCMatrix = new Matrix(); - initCMatrix.set(FTF); + initCMatrix.set(ex * ex, 0, 0, 0, 0, + 0, ey * ey, 0, 0, 0, + 0, 0, etx * etx, 0, 0, + 0, 0, 0, ety * ety, 0, + 0, 0, 0, 0, eQ * eQ + ); initStateVec.CM = initCMatrix; + } else { kf.setFitFailed = true; diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index 04ee94b0f..ac45a5a35 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -363,27 +363,21 @@ public void getInitState(Track trkcand, double z0, org.jlab.clas.tracking.kalman RungeKuttaDoca rk = new RungeKuttaDoca(); rk.SwimToZ(trkcand.get(0).get_Sector(), initStateVec, dcSwim, z0, bf); - - //initCM.covMat = trkcand.get_CovMat(); - //test - StateVecs svs = new StateVecs(); - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec rinitSV = svs.new StateVec(0); - rinitSV.x = trkcand.getFinalStateVec().x(); - rinitSV.y = trkcand.getFinalStateVec().y(); - rinitSV.z = trkcand.getFinalStateVec().getZ(); - rinitSV.tx = trkcand.getFinalStateVec().tanThetaX(); - rinitSV.ty = trkcand.getFinalStateVec().tanThetaY(); - rinitSV.Q = ((double) trkcand.get_Q())/trkcand.get_P(); - double[] FTF = new double[25]; - double[] F = this.F(trkcand.get(0).get_Sector(), z0, rinitSV, rk, dcSwim, bf); - for(int i = 0; i<5; i++) { - FTF[i*5+i]=F[i]*F[i]; - - } + + double ex = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATEXUNC; + double ey = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATEYUNC; + double etx = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATETXUNC; + double ety = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATETYUNC; + double eQ = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATEQUNC; //Matrix initCMatrix = new Matrix(FTF); Matrix initCMatrix = new Matrix(); - initCMatrix.set(FTF); + initCMatrix.set(ex * ex, 0, 0, 0, 0, + 0, ey * ey, 0, 0, 0, + 0, 0, etx * etx, 0, 0, + 0, 0, 0, ety * ety, 0, + 0, 0, 0, 0, eQ * eQ + ); initStateVec.CM = initCMatrix; } else { From e937fc6ab835e113e8625bef97754c7cf457a669 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 30 Mar 2023 16:09:18 -0400 Subject: [PATCH 18/69] tiny change --- .../main/java/org/jlab/rec/dc/track/TrackCandListFinder.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index cd9df59da..db2c333ca 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -1158,9 +1158,7 @@ public void getInitState(Track trkcand, double z0, int c, org.jlab.clas.tracking 0, 0, etx * etx, 0, 0, 0, 0, 0, ety * ety, 0, 0, 0, 0, 0, eQ * eQ - ); - - + ); initStateVec.CM = initCMatrix; } From 20ba0590df3c4b58c6b1f5cf951e3a996f97703d Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 30 Mar 2023 16:10:00 -0400 Subject: [PATCH 19/69] tiny change --- .../src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java | 1 - .../dc/src/main/java/org/jlab/service/dc/DCTBEngine.java | 1 - 2 files changed, 2 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index db2c333ca..778cfb662 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -1150,7 +1150,6 @@ public void getInitState(Track trkcand, double z0, int c, org.jlab.clas.tracking double etx = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATETXUNC; double ety = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATETYUNC; double eQ = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATEQUNC; - Matrix initCMatrix = new Matrix(); initCMatrix.set(ex * ex, 0, 0, 0, 0, diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index 4f85f636e..5da91962a 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -358,7 +358,6 @@ public void getInitState(Track trkcand, double z0, org.jlab.clas.tracking.kalman 0, 0, 0, 0, eQ * eQ ); initStateVec.CM = initCMatrix; - } else { kf.setFitFailed = true; From d39473d76be09fe4faf7e96d415048d2e0c79865 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Mon, 3 Apr 2023 13:04:44 -0400 Subject: [PATCH 20/69] change conditions for stopping forward tracking iteration --- .../kalmanfilter/zReference/KFitter.java | 22 ++++++++++++++----- .../zReference/KFitterWithURWell.java | 22 ++++++++++++++----- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 3f53826b9..eccca0ec6 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -221,12 +221,22 @@ public void runFitter() { } if (this.setFitFailed == false) { if (this.finalStateVec != null) { - if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 5.e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 1.e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 1.e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 1.e-6 - && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.e-6) { - i = totNumIter; + if (!TBT) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 3.3e-3 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 7.3e-2 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 4.3e-1 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 9.2e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 2.1e-3) { + i = totNumIter; + } + } else { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 5.5e-5 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 5.0e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 2.1e-3 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 8.8e-6 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.4e-5) { + i = totNumIter; + } } } this.finalStateVec = sv.trackTrajF.get(svzLength - 1); diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index 1493db651..ed4cbde49 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -232,12 +232,22 @@ public void runFitter() { } if (this.setFitFailed == false) { if (this.finalStateVec != null) { - if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 5.e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 1.e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 1.e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 1.e-6 - && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.e-6) { - i = totNumIter; + if (!TBT) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 3.3e-3 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 7.3e-2 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 4.3e-1 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 9.2e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 2.1e-3) { + i = totNumIter; + } + } else { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 5.5e-5 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 5.0e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 2.1e-3 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 8.8e-6 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.4e-5) { + i = totNumIter; + } } } this.finalStateVec = sv.trackTrajF.get(svzLength - 1); From 9edbc8ac32cce23ee18e33738f7ddf633100f3e1 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 5 Apr 2023 15:14:16 -0400 Subject: [PATCH 21/69] update overlap of tracks with uRWell crosses --- .../dc/src/main/java/org/jlab/rec/dc/track/Track.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/Track.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/Track.java index f6a563598..c07af931c 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/Track.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/Track.java @@ -487,6 +487,9 @@ public boolean overlaps(Track o) { int co = o.get(i).get_Id(); if(ct!=-1 && ct==co) value = true; } + if(!value && this.get_URWellCross() != null && o.get_URWellCross() != null){ + if(this.get_URWellCross().id() == o.get_URWellCross().id()) value = true; + } return value; } From 886382e986f7c44e980490037552c7fb01337bd2 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 26 May 2023 12:37:08 -0400 Subject: [PATCH 22/69] Version 2 of DC-uRWell tracking --- .../main/java/org/jlab/rec/dc/Constants.java | 70 +- .../org/jlab/rec/dc/banks/RecoBankWriter.java | 4 +- .../jlab/rec/dc/cross/CrossListFinder.java | 2 +- .../{track => cross}/URWellDCCrossesList.java | 12 +- .../dc/cross/URWellDCCrossesListFinder.java | 728 ++++++++++++++++++ .../java/org/jlab/rec/dc/track/Track.java | 32 + .../rec/dc/track/TrackCandListFinder.java | 4 +- .../track/TrackCandListWithURWellFinder.java | 463 ++++++++++- .../dc/track/URWellDCCrossesListFinder.java | 30 - .../jlab/rec/dc/trajectory/Trajectory.java | 18 + .../TrajectoryWithURWellFinder.java | 336 +++++++- .../service/dc/DCURWellHBPostClusterAI.java | 4 +- .../service/dc/DCURWellHBPostClusterConv.java | 168 +++- .../org/jlab/service/dc/DCURWellTBEngine.java | 278 +++++-- 14 files changed, 1980 insertions(+), 169 deletions(-) rename reconstruction/dc/src/main/java/org/jlab/rec/dc/{track => cross}/URWellDCCrossesList.java (83%) create mode 100644 reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/URWellDCCrossesListFinder.java delete mode 100644 reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesListFinder.java diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java index 9149e9ce2..99adc4313 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java @@ -63,7 +63,8 @@ public static Constants getInstance() { FastMath.sin(Math.toRadians(-240.)), FastMath.sin(Math.toRadians(-300.))}; public static final double[] COSSECTORNEG60 = {1, 0.5, -0.5, -1, -0.5, 0.5}; - // Uncertainties for initial state + ////////////// Uncertainties for initial state + ////// DC only public static final double HBINITIALSTATEUNCSCALE = 1.5; public static final double HBINITIALSTATEXUNC = 7.8; public static final double HBINITIALSTATEYUNC = 5.7; @@ -77,6 +78,71 @@ public static Constants getInstance() { public static final double TBINITIALSTATETXUNC = 0.0024; public static final double TBINITIALSTATETYUNC = 0.0091; public static final double TBINITIALSTATEQUNC = 0.0085; + + ////// With uRWell + public static final double HBINITIALSTATEUNCSCALEUR = 2; + public static final double TBINITIALSTATEUNCSCALEUR = 2; + + // R0R1R2R3 + public static final double HBINITIALSTATEXUNCUR = 6.5; + public static final double HBINITIALSTATEYUNCUR = 3.1; + public static final double HBINITIALSTATETXUNCUR = 0.05; + public static final double HBINITIALSTATETYUNCUR = 0.019; + public static final double HBINITIALSTATEQUNCUR = 0.15; + public static final double TBINITIALSTATEXUNCUR = 0.0095; + public static final double TBINITIALSTATEYUNCUR = 0.043; + public static final double TBINITIALSTATETXUNCUR = 0.0014; + public static final double TBINITIALSTATETYUNCUR = 0.0056; + public static final double TBINITIALSTATEQUNCUR = 0.0089; + + // R1R2R3; no R0 + public static final double HBINITIALSTATEXUNCURNOR0 = 6.4; + public static final double HBINITIALSTATEYUNCURNOR0 = 3.1; + public static final double HBINITIALSTATETXUNCURNOR0 = 0.05; + public static final double HBINITIALSTATETYUNCURNOR0 = 0.018; + public static final double HBINITIALSTATEQUNCURNOR0 = 0.15; + public static final double TBINITIALSTATEXUNCURNOR0 = 0.08; + public static final double TBINITIALSTATEYUNCURNOR0 = 0.72; + public static final double TBINITIALSTATETXUNCURNOR0 = 0.0017; + public static final double TBINITIALSTATETYUNCURNOR0 = 0.0068; + public static final double TBINITIALSTATEQUNCURNOR0 = 0.0092; + + // R0R2R3; no R1 + public static final double HBINITIALSTATEXUNCURNOR1 = 7.5; + public static final double HBINITIALSTATEYUNCURNOR1 = 3.1; + public static final double HBINITIALSTATETXUNCURNOR1 = 0.061; + public static final double HBINITIALSTATETYUNCURNOR1 = 0.020; + public static final double HBINITIALSTATEQUNCURNOR1 = 0.2; + public static final double TBINITIALSTATEXUNCURNOR1 = 0.0093; + public static final double TBINITIALSTATEYUNCURNOR1 = 0.042; + public static final double TBINITIALSTATETXUNCURNOR1 = 0.0014; + public static final double TBINITIALSTATETYUNCURNOR1 = 0.0056; + public static final double TBINITIALSTATEQUNCURNOR1 = 0.009; + + // R0R1R3; no R2 + public static final double HBINITIALSTATEXUNCURNOR2 = 10; + public static final double HBINITIALSTATEYUNCURNOR2 = 4.4; + public static final double HBINITIALSTATETXUNCURNOR2 = 0.046; + public static final double HBINITIALSTATETYUNCURNOR2 = 0.021; + public static final double HBINITIALSTATEQUNCURNOR2 = 0.15; + public static final double TBINITIALSTATEXUNCURNOR2 = 0.0088; + public static final double TBINITIALSTATEYUNCURNOR2 = 0.042; + public static final double TBINITIALSTATETXUNCURNOR2 = 0.0053; + public static final double TBINITIALSTATETYUNCURNOR2 = 0.0076; + public static final double TBINITIALSTATEQUNCURNOR2 = 0.038; + + // R0R1R2; no R3 + public static final double HBINITIALSTATEXUNCURNOR3 = 5.4; + public static final double HBINITIALSTATEYUNCURNOR3 = 2.9; + public static final double HBINITIALSTATETXUNCURNOR3 = 0.047; + public static final double HBINITIALSTATETYUNCURNOR3 = 0.019; + public static final double HBINITIALSTATEQUNCURNOR3 = 0.3; + public static final double TBINITIALSTATEXUNCURNOR3 = 0.0085; + public static final double TBINITIALSTATEYUNCURNOR3 = 0.042; + public static final double TBINITIALSTATETXUNCURNOR3 = 0.0047; + public static final double TBINITIALSTATETYUNCURNOR3 = 0.0097; + public static final double TBINITIALSTATEQUNCURNOR3 = 0.071; + // PHYSICS CONSTANTS public static final double SPEEDLIGHT = 29.97924580; @@ -218,7 +284,7 @@ public static Constants getInstance() { public static final int MAXCLUSSIZE = 14; - public static final double MAXCHI2 = 1000000; + public static final double MAXCHI2 = 10000; public static double HBTCHI2CUT = 2000; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java index a677a4f08..440437265 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java @@ -362,7 +362,7 @@ public DataBank fillHBTracksBank(DataEvent event, List candlist) { bank.setShort("id", i, (short) candlist.get(i).get_Id()); bank.setByte("sector", i, (byte) candlist.get(i).getSector()); bank.setByte("q", i, (byte) candlist.get(i).get_Q()); - bank.setShort("status", i, (short) (100+candlist.get(i).get_Status()*10+candlist.get(i).get_MissingSuperlayer())); + bank.setShort("status", i, (short) (1000*candlist.get(i).get_Status_crossCombo() + 100+candlist.get(i).get_Status()*10+candlist.get(i).get_MissingSuperlayer())); if(candlist.get(i).get_PreRegion1CrossPoint()!=null) { bank.setFloat("c1_x", i, (float) candlist.get(i).get_PreRegion1CrossPoint().x()); bank.setFloat("c1_y", i, (float) candlist.get(i).get_PreRegion1CrossPoint().y()); @@ -780,7 +780,7 @@ private DataBank fillTBTracksBank(DataEvent event, List candlist) { DataBank bank = event.createBank(name, candlist.size()); for (int i = 0; i < candlist.size(); i++) { bank.setShort("id", i, (short) candlist.get(i).get_Id()); - bank.setShort("status", i, (short) (100+candlist.get(i).get_Status()*10+candlist.get(i).get_MissingSuperlayer())); + bank.setShort("status", i, (short) (1000*candlist.get(i).get_Status_crossCombo() + 100+candlist.get(i).get_Status()*10+candlist.get(i).get_MissingSuperlayer())); bank.setByte("sector", i, (byte) candlist.get(i).getSector()); bank.setByte("q", i, (byte) candlist.get(i).get_Q()); //bank.setFloat("p", i, (float) candlist.get(i).get_P()); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/CrossListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/CrossListFinder.java index 613ad9cbc..dc28ef9ab 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/CrossListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/CrossListFinder.java @@ -356,7 +356,7 @@ public double[] evaluate(double[] x, double[] y, double[] err, double[] y2, doub double tiltSysZterm = x[i]; double dl = 0.01; - double dQ = 2.*ret[0]*x[i]*dl+ret[0]+ret[1]*dl; + double dQ = 2.*ret[0]*x[i]*dl+ret[1]*dl; double dL = linefit.getFit().slope()*dl; double Len = Math.sqrt(dl*dl + dQ*dQ + dL*dL) ; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesList.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/URWellDCCrossesList.java similarity index 83% rename from reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesList.java rename to reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/URWellDCCrossesList.java index 43be4157f..805ad6e40 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesList.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/URWellDCCrossesList.java @@ -1,4 +1,4 @@ -package org.jlab.rec.dc.track; +package org.jlab.rec.dc.cross; import java.util.List; import java.util.ArrayList; @@ -6,6 +6,12 @@ import org.jlab.rec.urwell.reader.URWellCross; import org.jlab.rec.dc.cross.Cross; +/** + * A class for cross combos with DC crosses and with/without uRWell cross + * + * @author Tongtong Cao + */ + public class URWellDCCrossesList{ private List urDCCrossesList = new ArrayList(); @@ -35,11 +41,11 @@ public URWellDCCrosses(URWellCross urCross, List dcCrosses) { this.dcCrosses = dcCrosses; } - URWellCross get_URWellCross(){ + public URWellCross get_URWellCross(){ return urCross; } - List get_DCCrosses(){ + public List get_DCCrosses(){ return dcCrosses; } diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/URWellDCCrossesListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/URWellDCCrossesListFinder.java new file mode 100644 index 000000000..ecc3f9fa8 --- /dev/null +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/cross/URWellDCCrossesListFinder.java @@ -0,0 +1,728 @@ +package org.jlab.rec.dc.cross; + +import Jama.Matrix; +import java.util.List; +import java.util.ArrayList; +import java.util.List; +import org.jlab.clas.swimtools.Swim; +import org.jlab.detector.geant4.v2.DCGeant4Factory; +import org.jlab.geom.prim.Point3D; +import org.jlab.geom.prim.Vector3D; +import org.jlab.io.base.DataEvent; +import org.jlab.rec.dc.Constants; +import org.jlab.rec.dc.cluster.ClusterFitter; +import org.jlab.rec.dc.hit.FittedHit; +import org.jlab.rec.dc.segment.Segment; +import org.jlab.rec.dc.timetodistance.TimeToDistanceEstimator; +import org.jlab.utils.groups.IndexedTable; +import trackfitter.fitter.LineFitter; +import org.jlab.rec.urwell.reader.URWellCross; + +/** + * A class to build 4-crosses or 3-crosses combos + * + * @author Tongtong Cao + */ + +public class URWellDCCrossesListFinder { + + public URWellDCCrossesListFinder() { + } + + public URWellDCCrossesList candURWellDCCrossLists(List urCrosses, CrossList crosslist) { + URWellDCCrossesList urDCCrossesList = new URWellDCCrossesList(); + + for (int i = 0; i < urCrosses.size(); i++) { + for (int j = 0; j < crosslist.size(); j++) { + URWellCross urCross = urCrosses.get(i); + List dcCrosses = crosslist.get(j); + + if (urCross.sector() == dcCrosses.get(0).get_Sector() && urCross.sector() == dcCrosses.get(1).get_Sector() && urCross.sector() == dcCrosses.get(2).get_Sector()) { + urDCCrossesList.add_URWellDCCrosses(urCross, dcCrosses); + } + } + } + + return urDCCrossesList; + } + + /** + * + * @param dccrosslist the list of crosses in the event + * @return the list of crosses determined to be consistent with belonging to + * a track in the DC + */ + ClusterFitter cf = new ClusterFitter(); + + public URWellDCCrossesList candURWellDCCrossesLists(DataEvent event, + List dccrosslist, List urCrosses, boolean TimeBased, + IndexedTable tab, DCGeant4Factory DcDetector, TimeToDistanceEstimator tde, + Swim swimmer, boolean FOOS, int nReqCrosses) { + + URWellDCCrossesList urDCCrossesList = new URWellDCCrossesList(); + + if (dccrosslist.size() > 0) { + List dccrosslistRg1 = new ArrayList<>(); + List dccrosslistRg2 = new ArrayList<>(); + List dccrosslistRg3 = new ArrayList<>(); + + for (Cross dc : dccrosslist) { + if (dc.get_Region() == 1) { + dccrosslistRg1.add(dc); + } + if (dc.get_Region() == 2) { + dccrosslistRg2.add(dc); + } + if (dc.get_Region() == 3) { + dccrosslistRg3.add(dc); + } + } + if (nReqCrosses == 4) { // need 4 crosses + + TrajectoryParametriz qf1 = new TrajectoryParametriz(nReqCrosses); + double[] X = new double[4]; + double[] Y = new double[4]; + double[] Z = new double[4]; + double[] errX = new double[4]; + double[] errY = new double[4]; + Vector3D traj1 = new Vector3D(0, 0, 0); + Vector3D traj2 = new Vector3D(0, 0, 0); + Vector3D traj3 = new Vector3D(0, 0, 0); + + if (!dccrosslistRg1.isEmpty() && !dccrosslistRg2.isEmpty() && !dccrosslistRg3.isEmpty() && !urCrosses.isEmpty()) { + for (Cross c1 : dccrosslistRg1) { + for (Cross c2 : dccrosslistRg2) { + for (Cross c3 : dccrosslistRg3) { + for (URWellCross urCross : urCrosses) { + if (urCross.sector() != c1.get_Sector() || urCross.sector() != c2.get_Sector() || urCross.sector() != c3.get_Sector()) { + this.clear(X, Y, Z, errX, errY); + continue; + } + if (FOOS == true) { + if (c1.get_Id() != -1 && c2.get_Id() != -1 && c3.get_Id() != -1) { + this.clear(X, Y, Z, errX, errY); + continue; + } + } + + Z[0] = c1.get_Point().z(); + Y[0] = c1.get_Point().y(); + X[0] = c1.get_Point().x(); + errX[0] = c1.get_PointErr().x(); + errY[0] = c1.get_PointErr().y(); + Z[1] = c2.get_Point().z(); + Y[1] = c2.get_Point().y(); + X[1] = c2.get_Point().x(); + errX[1] = c2.get_PointErr().x(); + errY[1] = c2.get_PointErr().y(); + Z[2] = c3.get_Point().z(); + Y[2] = c3.get_Point().y(); + X[2] = c3.get_Point().x(); + errX[2] = c3.get_PointErr().x(); + errY[2] = c3.get_PointErr().y(); + Z[3] = urCross.local().z(); + Y[3] = urCross.local().y(); + X[3] = urCross.local().x(); + errX[3] = Constants.URWELLXRESOLUTION; + errY[3] = Constants.URWELLYRESOLUTION; + // ignore point errors and assume the track vertex is close to the origin + for (int j = 0; j < 6; j++) { + this.clear(qf1.fitResult[j]); + } + qf1.evaluate(Z, X, errX, Y, errY); + + traj1.setXYZ(qf1.fitResult[3][0], qf1.fitResult[4][0], qf1.fitResult[5][0]); + traj2.setXYZ(qf1.fitResult[3][1], qf1.fitResult[4][1], qf1.fitResult[5][1]); + traj3.setXYZ(qf1.fitResult[3][2], qf1.fitResult[4][2], qf1.fitResult[5][2]); + + double cosTh1 = traj1.dot(c1.get_Dir().toVector3D()); + double cosTh2 = traj2.dot(c2.get_Dir().toVector3D()); + double cosTh3 = traj3.dot(c3.get_Dir().toVector3D()); + + // require that the cross direction estimate be in the direction of the trajectory + if (cosTh1 < Constants.TRACKDIRTOCROSSDIRCOSANGLE || cosTh2 < Constants.TRACKDIRTOCROSSDIRCOSANGLE || cosTh3 < Constants.TRACKDIRTOCROSSDIRCOSANGLE) { + continue; + } + + double fitchsq = 0; + + if (c1.isPseudoCross == false) { + fitchsq += ((qf1.fitResult[1][0] - c1.get_Point().y()) / c1.get_PointErr().y()) * ((qf1.fitResult[1][0] - c1.get_Point().y()) / c1.get_PointErr().y()); + } + if (c2.isPseudoCross == false) { + fitchsq += ((qf1.fitResult[1][1] - c2.get_Point().y()) / c2.get_PointErr().y()) * ((qf1.fitResult[1][1] - c2.get_Point().y()) / c2.get_PointErr().y()); + } + if (c3.isPseudoCross == false) { + fitchsq += ((qf1.fitResult[1][2] - c3.get_Point().y()) / c3.get_PointErr().y()) * ((qf1.fitResult[1][2] - c3.get_Point().y()) / c3.get_PointErr().y()); + } + fitchsq += ((qf1.fitResult[1][3] - urCross.local().y()) / Constants.URWELLYRESOLUTION) * ((qf1.fitResult[1][3] - urCross.local().y()) / Constants.URWELLYRESOLUTION); + + // fit the projection with a line -- the track is ~ constant in phi + LineFitter linefit = new LineFitter(); + boolean linefitstatusOK = linefit.fitStatus(X, Y, errX, errY, Z.length); + if (!linefitstatusOK) { + continue; // fit failed + } + + this.updateBFittedHits(event, c1, tab, DcDetector, tde, swimmer); + this.updateBFittedHits(event, c2, tab, DcDetector, tde, swimmer); + this.updateBFittedHits(event, c3, tab, DcDetector, tde, swimmer); + + if (fitchsq < Constants.CROSSLISTSELECTQFMINCHSQ) { + List dcCrosses = new ArrayList(); + dcCrosses.add(c1); + dcCrosses.add(c2); + dcCrosses.add(c3); + urDCCrossesList.add_URWellDCCrosses(urCross, dcCrosses); + } + } + } + } + } + } + } else if (nReqCrosses == 3) { // need 3 crosses + + TrajectoryParametriz qf1 = new TrajectoryParametriz(nReqCrosses); + double[] X = new double[3]; + double[] Y = new double[3]; + double[] Z = new double[3]; + double[] errX = new double[3]; + double[] errY = new double[3]; + Vector3D traj1 = new Vector3D(0, 0, 0); + Vector3D traj2 = new Vector3D(0, 0, 0); + Vector3D traj3 = new Vector3D(0, 0, 0); + + if (!dccrosslistRg1.isEmpty() && !dccrosslistRg2.isEmpty() && !dccrosslistRg3.isEmpty()) { // Cases for R1R2R3 + for (Cross c1 : dccrosslistRg1) { + for (Cross c2 : dccrosslistRg2) { + for (Cross c3 : dccrosslistRg3) { + if (c1.get_Sector() != c2.get_Sector() || c1.get_Sector() != c3.get_Sector()) { + this.clear(X, Y, Z, errX, errY); + continue; + } + if (FOOS == true) { + if (c1.get_Id() != -1 && c2.get_Id() != -1 && c3.get_Id() != -1) { + this.clear(X, Y, Z, errX, errY); + continue; + } + } + + Z[0] = c1.get_Point().z(); + Y[0] = c1.get_Point().y(); + X[0] = c1.get_Point().x(); + errX[0] = c1.get_PointErr().x(); + errY[0] = c1.get_PointErr().y(); + Z[1] = c2.get_Point().z(); + Y[1] = c2.get_Point().y(); + X[1] = c2.get_Point().x(); + errX[1] = c2.get_PointErr().x(); + errY[1] = c2.get_PointErr().y(); + Z[2] = c3.get_Point().z(); + Y[2] = c3.get_Point().y(); + X[2] = c3.get_Point().x(); + errX[2] = c3.get_PointErr().x(); + errY[2] = c3.get_PointErr().y(); + + // ignore point errors and assume the track vertex is close to the origin + for (int j = 0; j < 6; j++) { + this.clear(qf1.fitResult[j]); + } + qf1.evaluate(Z, X, errX, Y, errY); + + traj1.setXYZ(qf1.fitResult[3][0], qf1.fitResult[4][0], qf1.fitResult[5][0]); + traj2.setXYZ(qf1.fitResult[3][1], qf1.fitResult[4][1], qf1.fitResult[5][1]); + traj3.setXYZ(qf1.fitResult[3][2], qf1.fitResult[4][2], qf1.fitResult[5][2]); + + double cosTh1 = traj1.dot(c1.get_Dir().toVector3D()); + double cosTh2 = traj2.dot(c2.get_Dir().toVector3D()); + double cosTh3 = traj3.dot(c3.get_Dir().toVector3D()); + + // require that the cross direction estimate be in the direction of the trajectory + if (cosTh1 < Constants.TRACKDIRTOCROSSDIRCOSANGLE || cosTh2 < Constants.TRACKDIRTOCROSSDIRCOSANGLE || cosTh3 < Constants.TRACKDIRTOCROSSDIRCOSANGLE) { + continue; + } + + double fitchsq = 0; + + if (c1.isPseudoCross == false) { + fitchsq += ((qf1.fitResult[1][0] - c1.get_Point().y()) / c1.get_PointErr().y()) * ((qf1.fitResult[1][0] - c1.get_Point().y()) / c1.get_PointErr().y()); + } + if (c2.isPseudoCross == false) { + fitchsq += ((qf1.fitResult[1][1] - c2.get_Point().y()) / c2.get_PointErr().y()) * ((qf1.fitResult[1][1] - c2.get_Point().y()) / c2.get_PointErr().y()); + } + if (c3.isPseudoCross == false) { + fitchsq += ((qf1.fitResult[1][2] - c3.get_Point().y()) / c3.get_PointErr().y()) * ((qf1.fitResult[1][2] - c3.get_Point().y()) / c3.get_PointErr().y()); + } + + // fit the projection with a line -- the track is ~ constant in phi + LineFitter linefit = new LineFitter(); + boolean linefitstatusOK = linefit.fitStatus(X, Y, errX, errY, Z.length); + if (!linefitstatusOK) { + continue; // fit failed + } + + this.updateBFittedHits(event, c1, tab, DcDetector, tde, swimmer); + this.updateBFittedHits(event, c2, tab, DcDetector, tde, swimmer); + this.updateBFittedHits(event, c3, tab, DcDetector, tde, swimmer); + + if (fitchsq < Constants.CROSSLISTSELECTQFMINCHSQ) { + List dcCrosses = new ArrayList(); + dcCrosses.add(c1); + dcCrosses.add(c2); + dcCrosses.add(c3); + urDCCrossesList.add_URWellDCCrosses(null, dcCrosses); + } + } + } + } + } + else if(!dccrosslistRg2.isEmpty() && !dccrosslistRg3.isEmpty() && !urCrosses.isEmpty()){ // Cases for R2R3R0 + for (Cross c2 : dccrosslistRg2) { + for (Cross c3 : dccrosslistRg3) { + for (URWellCross urCross : urCrosses) { + if (urCross.sector() != c2.get_Sector() || urCross.sector() != c3.get_Sector()) { + this.clear(X, Y, Z, errX, errY); + continue; + } + if (FOOS == true) { + if (c2.get_Id() != -1 && c3.get_Id() != -1) { + this.clear(X, Y, Z, errX, errY); + continue; + } + } + + Z[0] = c2.get_Point().z(); + Y[0] = c2.get_Point().y(); + X[0] = c2.get_Point().x(); + errX[0] = c2.get_PointErr().x(); + errY[0] = c2.get_PointErr().y(); + Z[1] = c3.get_Point().z(); + Y[1] = c3.get_Point().y(); + X[1] = c3.get_Point().x(); + errX[1] = c3.get_PointErr().x(); + errY[1] = c3.get_PointErr().y(); + Z[2] = urCross.local().z(); + Y[2] = urCross.local().y(); + X[2] = urCross.local().x(); + errX[2] = Constants.URWELLXRESOLUTION; + errY[2] = Constants.URWELLYRESOLUTION; + + // ignore point errors and assume the track vertex is close to the origin + for (int j = 0; j < 6; j++) { + this.clear(qf1.fitResult[j]); + } + qf1.evaluate(Z, X, errX, Y, errY); + + traj2.setXYZ(qf1.fitResult[3][0], qf1.fitResult[4][0], qf1.fitResult[5][0]); + traj3.setXYZ(qf1.fitResult[3][1], qf1.fitResult[4][1], qf1.fitResult[5][1]); + + double cosTh2 = traj2.dot(c2.get_Dir().toVector3D()); + double cosTh3 = traj3.dot(c3.get_Dir().toVector3D()); + + // require that the cross direction estimate be in the direction of the trajectory + if (cosTh2 < Constants.TRACKDIRTOCROSSDIRCOSANGLE || cosTh3 < Constants.TRACKDIRTOCROSSDIRCOSANGLE) { + continue; + } + + double fitchsq = 0; + + if (c2.isPseudoCross == false) { + fitchsq += ((qf1.fitResult[1][0] - c2.get_Point().y()) / c2.get_PointErr().y()) * ((qf1.fitResult[1][0] - c2.get_Point().y()) / c2.get_PointErr().y()); + } + if (c3.isPseudoCross == false) { + fitchsq += ((qf1.fitResult[1][1] - c3.get_Point().y()) / c3.get_PointErr().y()) * ((qf1.fitResult[1][1] - c3.get_Point().y()) / c3.get_PointErr().y()); + } + fitchsq += ((qf1.fitResult[1][2] - urCross.local().y()) / Constants.URWELLYRESOLUTION) * ((qf1.fitResult[1][2] - urCross.local().y()) / Constants.URWELLYRESOLUTION); + + // fit the projection with a line -- the track is ~ constant in phi + LineFitter linefit = new LineFitter(); + boolean linefitstatusOK = linefit.fitStatus(X, Y, errX, errY, Z.length); + if (!linefitstatusOK) { + continue; // fit failed + } + + this.updateBFittedHits(event, c2, tab, DcDetector, tde, swimmer); + this.updateBFittedHits(event, c3, tab, DcDetector, tde, swimmer); + + if (fitchsq < Constants.CROSSLISTSELECTQFMINCHSQ) { + List dcCrosses = new ArrayList(); + dcCrosses.add(c2); + dcCrosses.add(c3); + urDCCrossesList.add_URWellDCCrosses(urCross, dcCrosses); + } + } + } + } + } + else if (!dccrosslistRg1.isEmpty() && !dccrosslistRg3.isEmpty() && !urCrosses.isEmpty()) { // Cases for R1R3R0 + for (Cross c1 : dccrosslistRg1) { + for (Cross c3 : dccrosslistRg3) { + for (URWellCross urCross : urCrosses) { + if (urCross.sector() != c1.get_Sector() || urCross.sector() != c3.get_Sector()) { + this.clear(X, Y, Z, errX, errY); + continue; + } + if (FOOS == true) { + if (c1.get_Id() != -1 && c3.get_Id() != -1) { + this.clear(X, Y, Z, errX, errY); + continue; + } + } + + Z[0] = c1.get_Point().z(); + Y[0] = c1.get_Point().y(); + X[0] = c1.get_Point().x(); + errX[0] = c1.get_PointErr().x(); + errY[0] = c1.get_PointErr().y(); + Z[1] = c3.get_Point().z(); + Y[1] = c3.get_Point().y(); + X[1] = c3.get_Point().x(); + errX[1] = c3.get_PointErr().x(); + errY[1] = c3.get_PointErr().y(); + Z[2] = urCross.local().z(); + Y[2] = urCross.local().y(); + X[2] = urCross.local().x(); + errX[2] = Constants.URWELLXRESOLUTION; + errY[2] = Constants.URWELLYRESOLUTION; + + // ignore point errors and assume the track vertex is close to the origin + for (int j = 0; j < 6; j++) { + this.clear(qf1.fitResult[j]); + } + qf1.evaluate(Z, X, errX, Y, errY); + + traj1.setXYZ(qf1.fitResult[3][0], qf1.fitResult[4][0], qf1.fitResult[5][0]); + traj3.setXYZ(qf1.fitResult[3][1], qf1.fitResult[4][1], qf1.fitResult[5][1]); + + double cosTh1 = traj1.dot(c1.get_Dir().toVector3D()); + double cosTh3 = traj3.dot(c3.get_Dir().toVector3D()); + + // require that the cross direction estimate be in the direction of the trajectory + if (cosTh1 < Constants.TRACKDIRTOCROSSDIRCOSANGLE || cosTh3 < Constants.TRACKDIRTOCROSSDIRCOSANGLE) { + continue; + } + + double fitchsq = 0; + + if (c1.isPseudoCross == false) { + fitchsq += ((qf1.fitResult[1][0] - c1.get_Point().y()) / c1.get_PointErr().y()) * ((qf1.fitResult[1][0] - c1.get_Point().y()) / c1.get_PointErr().y()); + } + if (c3.isPseudoCross == false) { + fitchsq += ((qf1.fitResult[1][1] - c3.get_Point().y()) / c3.get_PointErr().y()) * ((qf1.fitResult[1][1] - c3.get_Point().y()) / c3.get_PointErr().y()); + } + fitchsq += ((qf1.fitResult[1][2] - urCross.local().y()) / Constants.URWELLYRESOLUTION) * ((qf1.fitResult[1][2] - urCross.local().y()) / Constants.URWELLYRESOLUTION); + + // fit the projection with a line -- the track is ~ constant in phi + LineFitter linefit = new LineFitter(); + boolean linefitstatusOK = linefit.fitStatus(X, Y, errX, errY, Z.length); + if (!linefitstatusOK) { + continue; // fit failed + } + + this.updateBFittedHits(event, c1, tab, DcDetector, tde, swimmer); + this.updateBFittedHits(event, c3, tab, DcDetector, tde, swimmer); + + if (fitchsq < Constants.CROSSLISTSELECTQFMINCHSQ) { + List dcCrosses = new ArrayList(); + dcCrosses.add(c1); + dcCrosses.add(c3); + urDCCrossesList.add_URWellDCCrosses(urCross, dcCrosses); + } + } + } + } + } + else if (!dccrosslistRg1.isEmpty() && !dccrosslistRg2.isEmpty() && !urCrosses.isEmpty()) { // Cases for R1R2R0 + for (Cross c1 : dccrosslistRg1) { + for (Cross c2 : dccrosslistRg2) { + for (URWellCross urCross : urCrosses) { + if (urCross.sector() != c1.get_Sector() || urCross.sector() != c2.get_Sector()) { + this.clear(X, Y, Z, errX, errY); + continue; + } + if (FOOS == true) { + if (c1.get_Id() != -1 && c2.get_Id() != -1) { + this.clear(X, Y, Z, errX, errY); + continue; + } + } + + Z[0] = c1.get_Point().z(); + Y[0] = c1.get_Point().y(); + X[0] = c1.get_Point().x(); + errX[0] = c1.get_PointErr().x(); + errY[0] = c1.get_PointErr().y(); + Z[1] = c2.get_Point().z(); + Y[1] = c2.get_Point().y(); + X[1] = c2.get_Point().x(); + errX[1] = c2.get_PointErr().x(); + errY[1] = c2.get_PointErr().y(); + Z[2] = urCross.local().z(); + Y[2] = urCross.local().y(); + X[2] = urCross.local().x(); + errX[2] = Constants.URWELLXRESOLUTION; + errY[2] = Constants.URWELLYRESOLUTION; + + // ignore point errors and assume the track vertex is close to the origin + for (int j = 0; j < 6; j++) { + this.clear(qf1.fitResult[j]); + } + qf1.evaluate(Z, X, errX, Y, errY); + + traj1.setXYZ(qf1.fitResult[3][0], qf1.fitResult[4][0], qf1.fitResult[5][0]); + traj2.setXYZ(qf1.fitResult[3][1], qf1.fitResult[4][1], qf1.fitResult[5][1]); + + double cosTh1 = traj1.dot(c1.get_Dir().toVector3D()); + double cosTh2 = traj2.dot(c2.get_Dir().toVector3D()); + + // require that the cross direction estimate be in the direction of the trajectory + if (cosTh1 < Constants.TRACKDIRTOCROSSDIRCOSANGLE || cosTh2 < Constants.TRACKDIRTOCROSSDIRCOSANGLE) { + continue; + } + + double fitchsq = 0; + + if (c1.isPseudoCross == false) { + fitchsq += ((qf1.fitResult[1][0] - c1.get_Point().y()) / c1.get_PointErr().y()) * ((qf1.fitResult[1][0] - c1.get_Point().y()) / c1.get_PointErr().y()); + } + if (c2.isPseudoCross == false) { + fitchsq += ((qf1.fitResult[1][1] - c2.get_Point().y()) / c2.get_PointErr().y()) * ((qf1.fitResult[1][1] - c2.get_Point().y()) / c2.get_PointErr().y()); + } + fitchsq += ((qf1.fitResult[1][2] - urCross.local().y()) / Constants.URWELLYRESOLUTION) * ((qf1.fitResult[1][2] - urCross.local().y()) / Constants.URWELLYRESOLUTION); + + // fit the projection with a line -- the track is ~ constant in phi + LineFitter linefit = new LineFitter(); + boolean linefitstatusOK = linefit.fitStatus(X, Y, errX, errY, Z.length); + if (!linefitstatusOK) { + continue; // fit failed + } + + this.updateBFittedHits(event, c1, tab, DcDetector, tde, swimmer); + this.updateBFittedHits(event, c2, tab, DcDetector, tde, swimmer); + + if (fitchsq < Constants.CROSSLISTSELECTQFMINCHSQ) { + List dcCrosses = new ArrayList(); + dcCrosses.add(c1); + dcCrosses.add(c2); + urDCCrossesList.add_URWellDCCrosses(urCross, dcCrosses); + } + } + } + } + } + + } + } + + return urDCCrossesList; + } + + @SuppressWarnings("unused") + private void RecalculateCrossDir(Cross c1, double slope) { + double val_sl2 = c1.get_Segment2().get_fittedCluster().get_clusterLineFitSlope(); + double tanThX = val_sl2; + double tanThY = slope; + double uz = 1. / Math.sqrt(1 + tanThX * tanThX + tanThY * tanThY); + double ux = uz * tanThX; + double uy = uz * tanThY; + // set the dir + c1.set_Dir(new Point3D(ux, uy, uz)); + } + + @SuppressWarnings("unused") + private void RecalculateCrossDirErr(Cross c1, double slope, double slopeErr) { + // Error calculation + double val_sl2 = c1.get_Segment2().get_fittedCluster().get_clusterLineFitSlope(); + double tanThX = val_sl2; + double tanThY = slope; + double del_tanThX = c1.get_Segment2().get_fittedCluster().get_clusterLineFitSlopeErr(); + double del_tanThY = slopeErr; + double uz = 1. / Math.sqrt(1 + tanThX * tanThX + tanThY * tanThY); + double del_uz = uz * uz * uz * Math.sqrt(tanThX * tanThX * del_tanThX * del_tanThX + tanThY * tanThY * del_tanThY * del_tanThY); + double del_ux = Math.sqrt(tanThX * tanThX * del_uz * del_uz + uz * uz * del_tanThX * del_tanThX); + double del_uy = Math.sqrt(tanThY * tanThY * del_uz * del_uz + uz * uz * del_tanThY * del_tanThY); + + double err_xDir = del_ux; + double err_yDir = del_uy; + double err_zDir = del_uz; + + Point3D estimDirErr = new Point3D(err_xDir, err_yDir, err_zDir); + + c1.set_DirErr(estimDirErr); + } + + private void recalcParsSegment(DataEvent event, Segment _Segment1, IndexedTable tab, DCGeant4Factory DcDetector, TimeToDistanceEstimator tde) { + //refit + double trkAngle = _Segment1.get_fittedCluster().get_clusterLineFitSlope(); + // update the hits + for (FittedHit fhit : _Segment1.get_fittedCluster()) { + fhit.updateHitPositionWithTime(event, trkAngle, fhit.getB(), tab, DcDetector, tde); + } + + cf.SetFitArray(_Segment1.get_fittedCluster(), "TSC"); + cf.Fit(_Segment1.get_fittedCluster(), true); + trkAngle = _Segment1.get_fittedCluster().get_clusterLineFitSlope(); + + for (FittedHit fhit : _Segment1.get_fittedCluster()) { + fhit.updateHitPositionWithTime(event, trkAngle, fhit.getB(), tab, DcDetector, tde); + } + cf.SetFitArray(_Segment1.get_fittedCluster(), "TSC"); + cf.Fit(_Segment1.get_fittedCluster(), true); + cf.SetResidualDerivedParams(_Segment1.get_fittedCluster(), true, false, DcDetector); //calcTimeResidual=false, resetLRAmbig=false + + cf.SetFitArray(_Segment1.get_fittedCluster(), "TSC"); + cf.Fit(_Segment1.get_fittedCluster(), false); + + cf.SetSegmentLineParameters(_Segment1.get_fittedCluster().get(0).get_Z(), _Segment1.get_fittedCluster()); + + } + + public List> get_CrossesInSectors(List crosses) { + List> CrossesBySectors = new ArrayList<>(); + + for (int s = 0; s < 6; s++) { + CrossesBySectors.add(s, new ArrayList<>()); + } + for (Cross cross : crosses) { + //if(cross.isPseudoCross==false) + CrossesBySectors.get(cross.get_Sector() - 1).add(cross); + } + return CrossesBySectors; + } + + /** + * + * @param event + * @param c cross + * @param tab table of constants + * @param DcDetector detector geometry + * @param tde time-to-distance utility Updates the B-field information of + * the hits in the cross segments + * @param swimmer + */ + public void updateBFittedHits(DataEvent event, Cross c, IndexedTable tab, DCGeant4Factory DcDetector, TimeToDistanceEstimator tde, Swim swimmer) { + for (int i = 0; i < c.get_Segment1().size(); i++) { + Point3D ref = c.get_Segment1().get(i).getCrossDirIntersWire(); + float[] result = new float[3]; + swimmer.Bfield(c.get_Sector(), ref.x(), ref.y(), ref.z(), result); + c.get_Segment1().get(i).setB(Math.sqrt(result[0] * result[0] + result[1] * result[1] + result[2] * result[2])); + + } + for (int i = 0; i < c.get_Segment2().size(); i++) { + Point3D ref = c.get_Segment2().get(i).getCrossDirIntersWire(); + float[] result = new float[3]; + swimmer.Bfield(c.get_Sector(), ref.x(), ref.y(), ref.z(), result); + c.get_Segment2().get(i).setB(Math.sqrt(result[0] * result[0] + result[1] * result[1] + result[2] * result[2])); + } + if (tde != null) { + this.recalcParsSegment(event, c.get_Segment1(), tab, DcDetector, tde); + this.recalcParsSegment(event, c.get_Segment2(), tab, DcDetector, tde); + } + //remake cross + c.set_CrossParams(DcDetector); + } + + private void clear(double[]... X) { + for (double[] x : X) { + for (int i = 0; i < x.length; i++) { + x[i] = 0; + } + } + } + + private class TrajectoryParametriz { + + private int nReqCrosses = 3; + + private double[][] fitResult = {{0., 0., 0., 0.}, + {0., 0., 0., 0.}, + {0., 0., 0., 0.}, + {0., 0., 0., 0.}, + {0., 0., 0., 0.}, + {0., 0., 0., 0.}}; + + public TrajectoryParametriz(int nReqCrosses) { + this.nReqCrosses = nReqCrosses; + } + + public double[] evaluate(double[] x, double[] y, double[] err, double[] y2, double[] err2) { + LineFitter linefit = new LineFitter(); + linefit.fitStatus(x, y2, err, err, x.length); + double[] ret = {0., 0., 0.}; + Matrix A = new Matrix(3, 3); + Matrix V = new Matrix(3, 1); + double sum1 = 0.0; + double sum2 = 0.0; + double sum3 = 0.0; + double sum4 = 0.0; + double sum5 = 0.0; + double sum6 = 0.0; + double sum7 = 0.0; + double sum8 = 0.0; + for (int i = 0; i < x.length; ++i) { + double y1 = y[i]; + double x1 = x[i]; + double x2 = x1 * x1; + double x3 = x2 * x1; + double x4 = x2 * x2; + double e2 = err[i] * err[i]; + sum1 += x4 / e2; + sum2 += x3 / e2; + sum3 += x2 / e2; + sum4 += x1 / e2; + sum5 += 1.0 / e2; + sum6 += y1 * x2 / e2; + sum7 += y1 * x1 / e2; + sum8 += y1 / e2; + } + A.set(0, 0, sum1); + A.set(0, 1, sum2); + A.set(0, 2, sum3); + A.set(1, 0, sum2); + A.set(1, 1, sum3); + A.set(1, 2, sum4); + A.set(2, 0, sum3); + A.set(2, 1, sum4); + A.set(2, 2, sum5); + V.set(0, 0, sum6); + V.set(1, 0, sum7); + V.set(2, 0, sum8); + Matrix Ainv = A.inverse(); + Matrix X; + try { + X = Ainv.times(V); + for (int i = 0; i < 3; ++i) { + ret[i] = X.get(i, 0); + } + for (int i = 0; i < x.length; i++) { + + double tiltSysXterm = ret[0] * x[i] * x[i] + ret[1] * x[i] + ret[2]; + double tiltSysYterm = linefit.getFit().slope() * x[i] + linefit.getFit().intercept(); + double tiltSysZterm = x[i]; + + double dl = 0.01; + double dQ = 2. * ret[0] * x[i] * dl + ret[1] * dl; + double dL = linefit.getFit().slope() * dl; + double Len = Math.sqrt(dl * dl + dQ * dQ + dL * dL); + + double tiltSysdirXterm = dQ / Len; + double tiltSysdirYterm = dL / Len; + double tiltSysdirZterm = dl / Len; + + fitResult[0][i] = tiltSysXterm; + fitResult[1][i] = tiltSysYterm; + fitResult[2][i] = tiltSysZterm; + fitResult[3][i] = tiltSysdirXterm; + fitResult[4][i] = tiltSysdirYterm; + fitResult[5][i] = tiltSysdirZterm; + + //double n = ret[0]*x[i]*x[i]+ret[1]*x[i]+ret[2] - y[i]; + } + } catch (ArithmeticException e) { + // TODO Auto-generated catch block + } + return (ret); + } + } + +} diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/Track.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/Track.java index c07af931c..7c5f8cdff 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/Track.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/Track.java @@ -191,6 +191,24 @@ public void set_Status(int _Status) { this._Status = _Status; } + /** + * Status for different cross combos: + * 1: combos from R0R1R2R3 + * 2: combos from R1R2R3 + * 3: combos from R0R2R3 + * 4: combos from R0R1R3 + * 5: combos from R0R1R2 + */ + private int _Status_crossCombo=0; + + public int get_Status_crossCombo() { + return _Status_crossCombo; + } + + public void set_Status_crossCombo(int _Status) { + this._Status_crossCombo = _Status; + } + /** * * @return id of the track @@ -487,6 +505,20 @@ public boolean overlaps(Track o) { int co = o.get(i).get_Id(); if(ct!=-1 && ct==co) value = true; } + return value; + } + + public boolean overlapsWithURWell(Track o) { + boolean value = false; + for(int i=0; i trkcands) { // LOGGER.log(Level.FINE, "Checking overlaps for tracks "); // t1.printInfo();t2.printInfo(); if(i!=j && t1.overlaps(t2)) { - if(t1.get_FitChi2()>t2.get_FitChi2()) + if(t1.get_FitChi2()/t1.get_FitNDF()>t2.get_FitChi2()/t2.get_FitNDF()) overlap=true; - else if(t1.get_FitChi2()==t2.get_FitChi2() && i>j) + else if(t1.get_FitChi2()/t1.get_FitNDF()==t2.get_FitChi2()/t2.get_FitNDF() && i>j) overlap=true; } // LOGGER.log(Level.FINE, overlap); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java index 0aa190a4c..5e746d5b6 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -22,17 +22,19 @@ import org.jlab.rec.dc.segment.Segment; import org.jlab.rec.dc.trajectory.StateVec; import org.jlab.rec.dc.trajectory.Trajectory; -import org.jlab.rec.dc.trajectory.TrajectoryFinder; +import org.jlab.rec.dc.trajectory.TrajectoryWithURWellFinder; import trackfitter.fitter.LineFitPars; import trackfitter.fitter.LineFitter; import org.jlab.clas.tracking.kalmanfilter.Surface; +import org.jlab.clas.tracking.kalmanfilter.zReference.KFitter; import org.jlab.clas.tracking.kalmanfilter.zReference.KFitterWithURWell; import org.jlab.clas.tracking.kalmanfilter.zReference.StateVecs; import org.jlab.clas.tracking.utilities.MatrixOps.Libr; import org.jlab.clas.tracking.utilities.RungeKuttaDoca; import org.jlab.rec.urwell.reader.URWellCross; +import org.jlab.rec.dc.cross.URWellDCCrossesList; /** * A class with a method implementing an algorithm that finds lists of track @@ -43,7 +45,7 @@ public class TrackCandListWithURWellFinder { - private static final Logger LOGGER = Logger.getLogger(TrackCandListFinder.class.getName()); + private static final Logger LOGGER = Logger.getLogger(TrackCandListWithURWellFinder.class.getName()); long startTime, startTime2 = 0; @@ -133,7 +135,7 @@ private double getHitBasedFitChi2ToCrosses(int sector, double x1, double y1, dou return chi2; } - + private double[] getTrackInitFit(int sector, double x1, double y1, double z1, double x2, double y2, double z2, double x3, double y3, double z3, double ux, double uy, double uz, double thX, double thY, @@ -178,9 +180,44 @@ private double[] getTrackInitFit(int sector, double x1, double y1, double z1, return pars; } + + private double[] getTrackInitFit(int sector, double x_first, double y_first, double z_first, + double x_last, double y_last, double z_last, + double ux, double uy, double uz, double thX, double thY, + double theta_first, double theta_last, double a, + double iBdl, double TORSCALE, Swim dcSwim) { + if (theta_first < -998 || theta_last < -998) { + return new double[]{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; + } + double[] pars = new double[2]; + + double chi2 = 0; // assume err =1 on points + double intBdl = 0; + + double p = calcInitTrkP(thX, thY, theta_first, theta_last, iBdl); + double p_x = ux * p; + double p_y = uy * p; + double p_z = uz * p; + + int q = calcInitTrkQ(a, TORSCALE); + + dcSwim.SetSwimParameters(x_first, y_first, z_first, p_x, p_y, p_z, q); + double[] R = dcSwim.SwimToPlaneTiltSecSysBdlXZPlane(sector, z_last); + if (R == null) { + return new double[]{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; + } + + chi2 += (R[0] - x_last) * (R[0] - x_last) + (R[1] - y_last) * (R[1] - y_last); + intBdl += R[7]; - private double calcInitTrkP(double thX, double thY, double theta1, double theta3, double iBdl) { - double deltaTheta = theta3 - theta1; + pars[0] = chi2; + pars[1] = intBdl; + + return pars; + } + + private double calcInitTrkP(double thX, double thY, double theta_first, double theta_last, double iBdl) { + double deltaTheta = theta_last - theta_first; if (deltaTheta == 0) { return Double.POSITIVE_INFINITY; } @@ -220,6 +257,26 @@ public List getTrackCands(URWellDCCrossesList urDCCrossesList, DCGeant4Fa } return cands; } + + /** + * @param crossList the input list of crosses + * @param DcDetector + * @param TORSCALE + * @param dcSwim + * @param donotapplyCuts + * @return a list of track candidates in the DC + */ + public List getTrackCands3URDCCrosses(URWellDCCrossesList urDCCrossesList, DCGeant4Factory DcDetector, double TORSCALE, Swim dcSwim, + boolean donotapplyCuts) { + + List cands = null; + if (Math.abs(TORSCALE) < 0.001) { + return null; + } else { + cands = this.findCurvedTracks3URDCCrosses(urDCCrossesList, DcDetector, TORSCALE, dcSwim, donotapplyCuts); + } + return cands; + } /** * @param cand straight track candidate @@ -341,7 +398,7 @@ private int getSector(double x, double y) { */ public void setTrackPars(Track cand, Trajectory traj, - TrajectoryFinder trjFind, + TrajectoryWithURWellFinder trjFind, StateVec stateVec, double z, DCGeant4Factory getDcDetector, Swim dcSwim) { @@ -489,7 +546,7 @@ public void setTrackPars(Track cand, public void setTrackPars(Track cand, Trajectory traj, - TrajectoryFinder trjFind, + TrajectoryWithURWellFinder trjFind, StateVec stateVec, double z, DCGeant4Factory getDcDetector, Swim dcSwim, double xB, double yB) { @@ -700,10 +757,10 @@ public void removeOverlappingTracks(List trkcands) { Track t2 = trkcands.get(j); // LOGGER.log(Level.FINE, "Checking overlaps for tracks "); // t1.printInfo();t2.printInfo(); - if(i!=j && t1.overlaps(t2)) { - if(t1.get_FitChi2()>t2.get_FitChi2()) + if(i!=j && t1.overlapsWithURWell(t2)) { + if(t1.get_FitChi2()/t1.get_FitNDF()>t2.get_FitChi2()/t2.get_FitNDF()) overlap=true; - else if(t1.get_FitChi2()==t2.get_FitChi2() && i>j) + else if(t1.get_FitChi2()/t1.get_FitNDF()==t2.get_FitChi2()/t2.get_FitNDF() && i>j) overlap=true; } // LOGGER.log(Level.FINE, overlap); @@ -713,7 +770,7 @@ else if(t1.get_FitChi2()==t2.get_FitChi2() && i>j) trkcands.removeAll(trkcands); trkcands.addAll(selectedTracks); } - + /** * @param selectedTracks the list of selected tracks * @param selectedTrk the selected track @@ -882,14 +939,15 @@ private List findStraightTracks(URWellDCCrossesList urDCCrossesList, DCGe for (int i = 0; i < urDCCrossesList.get_URWellDCCrossesList().size(); i++) { List aCrossList = urDCCrossesList.get_URWellDCCrossesList().get(i).get_DCCrosses(); + URWellCross urCross = urDCCrossesList.get_URWellDCCrossesList().get(i).get_URWellCross(); //initialize Track cand = new Track(); - TrajectoryFinder trjFind = new TrajectoryFinder(); + TrajectoryWithURWellFinder trjFind = new TrajectoryWithURWellFinder(); if(LOGGER.getLevel()==Level.FINE) { startTime = System.currentTimeMillis(); } - Trajectory traj = trjFind.findTrajectory(aCrossList, DcDetector, dcSwim); + Trajectory traj = trjFind.findTrajectory(aCrossList, urCross, DcDetector, dcSwim); LOGGER.log(Level.FINE, "Trajectory finding = " + (System.currentTimeMillis() - startTime)); @@ -912,7 +970,6 @@ private List findStraightTracks(URWellDCCrossesList urDCCrossesList, DCGe cand.get(0).get_Dir().y() / cand.get(0).get_Dir().z()); cand.set_StateVecAtReg1MiddlePlane(VecAtReg1MiddlePlane); - URWellCross urCross = urDCCrossesList.get_URWellDCCrossesList().get(i).get_URWellCross(); cand.set_URWellCross(urCross); LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); @@ -981,14 +1038,17 @@ private List findCurvedTracks(URWellDCCrossesList urDCCrossesList, DCGean // return cands; continue; } + + URWellCross urCross = urDCCrossesList.get_URWellDCCrossesList().get(i).get_URWellCross(); + //initialize Track cand = new Track(); - TrajectoryFinder trjFind = new TrajectoryFinder(); + TrajectoryWithURWellFinder trjFind = new TrajectoryWithURWellFinder(); if(LOGGER.getLevel()==Level.FINE) { startTime = System.currentTimeMillis(); } - Trajectory traj = trjFind.findTrajectory(aCrossList, DcDetector, dcSwim); + Trajectory traj = trjFind.findTrajectory(aCrossList, urCross, DcDetector, dcSwim); LOGGER.log(Level.FINE, "Trajectory finding = " + (System.currentTimeMillis() - startTime)); @@ -1042,7 +1102,7 @@ private List findCurvedTracks(URWellDCCrossesList urDCCrossesList, DCGean double chi2 = pars[0]; double iBdl = pars[1]; - + if (chi2 > Constants.SEEDCUT && donotapplyCuts == false) { continue; } @@ -1076,8 +1136,7 @@ private List findCurvedTracks(URWellDCCrossesList urDCCrossesList, DCGean cand.get(0).get_Dir().x() / cand.get(0).get_Dir().z(), cand.get(0).get_Dir().y() / cand.get(0).get_Dir().z()); cand.set_StateVecAtReg1MiddlePlane(VecAtReg1MiddlePlane); - - URWellCross urCross = urDCCrossesList.get_URWellDCCrossesList().get(i).get_URWellCross(); + cand.set_URWellCross(urCross); StateVec fitStateVec = null; @@ -1123,7 +1182,8 @@ private List findCurvedTracks(URWellDCCrossesList urDCCrossesList, DCGean cand.setStateVecs(kfStateVecsAlongTrajectory); cand.setFinalStateVec(fitStateVec); - cand.set_Id(cands.size() + 1); + cand.set_Id(cands.size() + 1); + cand.set_Status_crossCombo(1); this.setTrackPars(cand, traj, trjFind, fitStateVec, fitStateVec.getZ(), @@ -1141,6 +1201,314 @@ private List findCurvedTracks(URWellDCCrossesList urDCCrossesList, DCGean return cands; } + + private List findCurvedTracks3URDCCrosses(URWellDCCrossesList urDCCrossesList, DCGeant4Factory DcDetector, double TORSCALE, Swim dcSwim, + boolean donotapplyCuts) { + + if(LOGGER.getLevel()==Level.FINE) { + startTime2 = System.currentTimeMillis(); + } + + List cands = new ArrayList(); + if (urDCCrossesList.get_URWellDCCrossesList().isEmpty()) { + return cands; + } + for (int i = 0; i < urDCCrossesList.get_URWellDCCrossesList().size(); i++) { + + List aCrossList = urDCCrossesList.get_URWellDCCrossesList().get(i).get_DCCrosses(); + aCrossList.sort(Comparator.comparing(Cross::get_Sector).thenComparing(Cross::get_Region)); + if (aCrossList.size() < 2) { + // return cands; + continue; + } + + URWellCross urCross = urDCCrossesList.get_URWellDCCrossesList().get(i).get_URWellCross(); + + //initialize + Track cand = new Track(); + TrajectoryWithURWellFinder trjFind = new TrajectoryWithURWellFinder(); + + if(LOGGER.getLevel()==Level.FINE) { + startTime = System.currentTimeMillis(); + } + Trajectory traj = trjFind.findTrajectory(aCrossList, urCross, DcDetector, dcSwim); + + LOGGER.log(Level.FINE, "Trajectory finding = " + (System.currentTimeMillis() - startTime)); + + + if (traj == null) { + continue; + } + + cand.addAll(aCrossList); + cand.setSector(aCrossList.get(0).get_Sector()); + + // set the candidate trajectory using the parametrization of the track trajectory + // and estimate intefral Bdl along that path + cand.setStateVecs(traj.getStateVecs()); + cand.setIntegralBdl(traj.getIntegralBdl()); + + //require 3 crosses to make a track (allows for 1 pseudo-cross) + if (urCross == null && aCrossList.size() == 3) { + // LOGGER.log(Level.FINE, "---- cand in sector " + aCrossList.get(0).getSector()); + // LOGGER.log(Level.FINE, aCrossList.get(0).printInfo()); + // LOGGER.log(Level.FINE, aCrossList.get(1).printInfo()); + // LOGGER.log(Level.FINE, aCrossList.get(2).printInfo()); + // LOGGER.log(Level.FINE, "---------------"); + double x1 = aCrossList.get(0).get_Point().x(); + double y1 = aCrossList.get(0).get_Point().y(); + double z1 = aCrossList.get(0).get_Point().z(); + double x2 = aCrossList.get(1).get_Point().x(); + double y2 = aCrossList.get(1).get_Point().y(); + double z2 = aCrossList.get(1).get_Point().z(); + double x3 = aCrossList.get(2).get_Point().x(); + double y3 = aCrossList.get(2).get_Point().y(); + double z3 = aCrossList.get(2).get_Point().z(); + double ux = aCrossList.get(0).get_Dir().x(); + double uy = aCrossList.get(0).get_Dir().y(); + double uz = aCrossList.get(0).get_Dir().z(); + double thX = ux / uz; + double thY = uy / uz; + double theta3s2 = Math.atan(cand.get(2).get_Segment2().get_fittedCluster().get_clusterLineFitSlope()); + double theta1s2 = Math.atan(cand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitSlope()); + double theta3s1 = Math.atan(cand.get(2).get_Segment1().get_fittedCluster().get_clusterLineFitSlope()); + double theta1s1 = Math.atan(cand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitSlope()); + + double theta1 = 0.5 * (theta1s1 + theta1s2); + double theta3 = 0.5 * (theta3s1 + theta3s2); + + double[] pars = getTrackInitFit(cand.get(0).get_Sector(), x1, y1, z1, x2, y2, z2, x3, y3, z3, + ux, uy, uz, thX, thY, + theta1, theta3, traj.getA(), + traj.getIntegralBdl(), TORSCALE, dcSwim); + + double chi2 = pars[0]; + double iBdl = pars[1]; + + if (chi2 > Constants.SEEDCUT && donotapplyCuts == false) { + continue; + } + // compute delta theta using the non-pseudo segments in region 1 and 3 + + // get integral Bdl from the swimmer trajectory + //double iBdl = traj.getIntegralBdl(); + if (iBdl != 0) { + // momentum estimate if Bdl is non zero and the track has curvature + double p = calcInitTrkP(thX, thY, theta1, theta3, iBdl); + if(LOGGER.getLevel()==Level.FINE) { + startTime = System.currentTimeMillis(); + } + int q = this.calcInitTrkQ(traj.getA(), TORSCALE); + + LOGGER.log(Level.FINE, "calcInitTrkQ = " + (System.currentTimeMillis() - startTime)); + + if (p > 11) { + p = 11; + } + //if(p>Constants.MAXTRKMOM || p< Constants.MINTRKMOM) + // continue; + + cand.set_Q(q); + // momentum correction using the swam trajectory iBdl + cand.set_P(p); + + StateVec fitStateVec = null; + // prefer to initialize the seed with region 2 cross due to higher background in region 1 + int crossIdxinList = 1; + + LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); + + KFitter kFZRef = new KFitter(true, 10, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(cand, DcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(cand, measSurfaces.get(0).z, crossIdxinList, initSV, dcSwim, new float[3]); + kFZRef.init(measSurfaces, initSV); + + kFZRef.runFitter(); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + if (kFZRef.finalStateVec == null) { + continue; + } else { + if (kFZRef.chi2 < Constants.MAXCHI2) { + + fitStateVec = new StateVec(kFZRef.finalStateVec.x, + kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); + q = (int) Math.signum(kFZRef.finalStateVec.Q); + p = 1. / Math.abs(kFZRef.finalStateVec.Q); + fitStateVec.setZ(kFZRef.finalStateVec.z); + + //set the track parameters + cand.set_P(p); + cand.set_Q(q); + + // candidate parameters + cand.set_FitChi2(kFZRef.chi2); + cand.set_FitNDF(kFZRef.NDF); + cand.set_FitConvergenceStatus(kFZRef.ConvStatus); + + cand.set_CovMat(kFZRef.finalStateVec.CM); + cand.setStateVecs(kfStateVecsAlongTrajectory); + + cand.setFinalStateVec(fitStateVec); + cand.set_Id(cands.size() + 1); + cand.set_Status_crossCombo(2); + this.setTrackPars(cand, traj, + trjFind, fitStateVec, + fitStateVec.getZ(), + DcDetector, dcSwim); + // add candidate to list of tracks + if (cand.fit_Successful = true) { + cands.add(cand); + } + } + } + } + } + if (urCross != null && aCrossList.size() == 2) { + double x_first = aCrossList.get(0).get_Point().x(); + double y_first = aCrossList.get(0).get_Point().y(); + double z_first = aCrossList.get(0).get_Point().z(); + double x_last = aCrossList.get(1).get_Point().x(); + double y_last = aCrossList.get(1).get_Point().y(); + double z_last = aCrossList.get(1).get_Point().z(); + double ux = aCrossList.get(0).get_Dir().x(); + double uy = aCrossList.get(0).get_Dir().y(); + double uz = aCrossList.get(0).get_Dir().z(); + double thX = ux / uz; + double thY = uy / uz; + double thetaLasts2 = Math.atan(cand.get(1).get_Segment2().get_fittedCluster().get_clusterLineFitSlope()); + double thetaFirsts2 = Math.atan(cand.get(0).get_Segment2().get_fittedCluster().get_clusterLineFitSlope()); + double thetaLasts1 = Math.atan(cand.get(1).get_Segment1().get_fittedCluster().get_clusterLineFitSlope()); + double thetaFirsts1 = Math.atan(cand.get(0).get_Segment1().get_fittedCluster().get_clusterLineFitSlope()); + + double theta_first = 0.5 * (thetaFirsts1 + thetaFirsts2); + double theta_last = 0.5 * (thetaLasts1 + thetaLasts2); + + double[] pars = getTrackInitFit(cand.get(0).get_Sector(), x_first, y_first, z_first, x_last, y_last, z_last, + ux, uy, uz, thX, thY, + theta_first, theta_last, traj.getA(), + traj.getIntegralBdl(), TORSCALE, dcSwim); + + double chi2 = pars[0]; + double iBdl = pars[1]; + + if (chi2 > Constants.SEEDCUT && donotapplyCuts == false) { + continue; + } + // compute delta theta using the non-pseudo segments in region 1 and 3 + + // get integral Bdl from the swimmer trajectory + //double iBdl = traj.getIntegralBdl(); + if (iBdl != 0) { + // momentum estimate if Bdl is non zero and the track has curvature + double p = calcInitTrkP(thX, thY, theta_first, theta_last, iBdl); + if(LOGGER.getLevel()==Level.FINE) { + startTime = System.currentTimeMillis(); + } + int q = this.calcInitTrkQ(traj.getA(), TORSCALE); + + LOGGER.log(Level.FINE, "calcInitTrkQ = " + (System.currentTimeMillis() - startTime)); + + if (p > 11) { + p = 11; + } + //if(p>Constants.MAXTRKMOM || p< Constants.MINTRKMOM) + // continue; + + cand.set_Q(q); + // momentum correction using the swam trajectory iBdl + cand.set_P(p); + + cand.set_URWellCross(urCross); + + StateVec fitStateVec = null; + // prefer to initialize the seed with region 2; + // if no region 2, then region 3 + int crossIdxinList = 1; // the latter DC cross + if(cand.get(0).get_Region() == 2) crossIdxinList = 0; + + LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); + + KFitterWithURWell kFZRef = new KFitterWithURWell(true, 10, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(cand, DcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(cand, measSurfaces.get(0).z, crossIdxinList, initSV, dcSwim, new float[3]); + kFZRef.init(measSurfaces, initSV); + + kFZRef.runFitter(); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + if (kFZRef.finalStateVec == null) { + continue; + } else { + if (kFZRef.chi2 < Constants.MAXCHI2) { + + fitStateVec = new StateVec(kFZRef.finalStateVec.x, + kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); + q = (int) Math.signum(kFZRef.finalStateVec.Q); + p = 1. / Math.abs(kFZRef.finalStateVec.Q); + fitStateVec.setZ(kFZRef.finalStateVec.z); + + //set the track parameters + cand.set_P(p); + cand.set_Q(q); + + // candidate parameters + cand.set_FitChi2(kFZRef.chi2); + cand.set_FitNDF(kFZRef.NDF); + cand.set_FitConvergenceStatus(kFZRef.ConvStatus); + + cand.set_CovMat(kFZRef.finalStateVec.CM); + cand.setStateVecs(kfStateVecsAlongTrajectory); + + cand.setFinalStateVec(fitStateVec); + cand.set_Id(cands.size() + 1); + if(cand.get(0).get_Region() == 2 && cand.get(1).get_Region() == 3) + cand.set_Status_crossCombo(3); + else if(cand.get(0).get_Region() == 1 && cand.get(1).get_Region() == 3) + cand.set_Status_crossCombo(4); + else if(cand.get(0).get_Region() == 1 && cand.get(1).get_Region() == 2) + cand.set_Status_crossCombo(5); + this.setTrackPars(cand, traj, + trjFind, fitStateVec, + fitStateVec.getZ(), + DcDetector, dcSwim); + // add candidate to list of tracks + if (cand.fit_Successful = true) { + cands.add(cand); + } + } + } + } + } + + + + } + return cands; + } + + public List setKFStateVecsAlongTrajectory(KFitter kFZRef) { + List kfStateVecsAlongTrajectory = new ArrayList<>(); + + for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + sv.setZ(svc.z); + sv.setB(svc.B); + sv.setPathLength(svc.getPathLength()); + sv.setProjector(svc.getProjector()); + sv.setProjectorDoca(svc.getProjectorDoca()); + kfStateVecsAlongTrajectory.add(sv); + } + + return kfStateVecsAlongTrajectory; + } + public List setKFStateVecsAlongTrajectory(KFitterWithURWell kFZRef) { List kfStateVecsAlongTrajectory = new ArrayList<>(); @@ -1174,11 +1542,44 @@ public void getInitState(Track trkcand, double z0, int c, org.jlab.clas.tracking RungeKuttaDoca rk = new RungeKuttaDoca(); rk.SwimToZ(trkcand.get(0).get_Sector(), initStateVec, dcSwim, z0, bf); - double ex = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATEXUNC; - double ey = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATEYUNC; - double etx = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATETXUNC; - double ety = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATETYUNC; - double eQ = Constants.HBINITIALSTATEUNCSCALE * Constants.HBINITIALSTATEQUNC; + double ex = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEXUNCUR; + double ey = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEYUNCUR; + double etx = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATETXUNCUR; + double ety = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATETYUNCUR; + double eQ = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEQUNCUR; + + if(trkcand.get_URWellCross() == null){ + ex = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEXUNCURNOR0; + ey = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEYUNCURNOR0; + etx = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATETXUNCURNOR0; + ety = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATETYUNCURNOR0; + eQ = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEQUNCURNOR0; + } + else{ + if(trkcand.size() == 2){ + if(trkcand.get(0).get_Region() == 2 && trkcand.get(1).get_Region() == 3){ + ex = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEXUNCURNOR1; + ey = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEYUNCURNOR1; + etx = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATETXUNCURNOR1; + ety = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATETYUNCURNOR1; + eQ = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEQUNCURNOR1; + } + else if(trkcand.get(0).get_Region() == 1 && trkcand.get(1).get_Region() == 3){ + ex = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEXUNCURNOR2; + ey = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEYUNCURNOR2; + etx = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATETXUNCURNOR2; + ety = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATETYUNCURNOR2; + eQ = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEQUNCURNOR2; + } + else if(trkcand.get(0).get_Region() == 1 && trkcand.get(1).get_Region() == 2){ + ex = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEXUNCURNOR3; + ey = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEYUNCURNOR3; + etx = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATETXUNCURNOR3; + ety = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATETYUNCURNOR3; + eQ = Constants.HBINITIALSTATEUNCSCALEUR * Constants.HBINITIALSTATEQUNCURNOR3; + } + } + } Matrix initCMatrix = new Matrix(); initCMatrix.set(ex * ex, 0, 0, 0, 0, @@ -1196,12 +1597,14 @@ public List getMeasSurfaces(Track trkcand, DCGeant4Factory DcDetector) FittedHit hitOnTrk; URWellCross urCross = trkcand.get_URWellCross(); - HitOnTrack urhot = new HitOnTrack(urCross.sector(), urCross.local().x(), urCross.local().y(), urCross.local().z(), Constants.URWELLXRESOLUTION, Constants.URWELLYRESOLUTION); - urhot.isDCHit = false; - hOTS.add(urhot); + if(urCross != null){ + HitOnTrack urhot = new HitOnTrack(urCross.sector(), urCross.local().x(), urCross.local().y(), urCross.local().z(), Constants.URWELLXRESOLUTION, Constants.URWELLYRESOLUTION); + urhot.isDCHit = false; + hOTS.add(urhot); + } - // loops over the regions (1 to 3) and the superlayers in a region (1 to 2) and obtains the hits on track - for (int c = 0; c < 3; c++) { + // loops over the regions and the superlayers in a region (1 to 2) and obtains the hits on track + for (int c = 0; c < trkcand.size(); c++) { for (int s = 0; s < 2; s++) { for (int h = 0; h < trkcand.get(c).get(s).size(); h++) { // if (trkcand.get(c).get(s).get(h).get_Id() == -1 /*|| trkcand.get(c).get(s).get(h).get_Id() == 0*/) { //PASS1 diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesListFinder.java deleted file mode 100644 index e3c541ad8..000000000 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/URWellDCCrossesListFinder.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.jlab.rec.dc.track; - -import java.util.List; -import org.jlab.rec.dc.cross.CrossList; -import org.jlab.rec.urwell.reader.URWellCross; -import org.jlab.rec.dc.cross.Cross; - -public class URWellDCCrossesListFinder { - - public URWellDCCrossesListFinder() { - } - - public URWellDCCrossesList candURWellDCCrossLists(List urCrosses, CrossList crosslist){ - URWellDCCrossesList urDCCrossesList = new URWellDCCrossesList(); - - for(int i = 0; i < urCrosses.size(); i++){ - for(int j = 0; j < crosslist.size(); j++){ - URWellCross urCross = urCrosses.get(i); - List dcCrosses = crosslist.get(j); - - if(urCross.sector() == dcCrosses.get(0).get_Sector() && urCross.sector() == dcCrosses.get(1).get_Sector() && urCross.sector() == dcCrosses.get(2).get_Sector()) - urDCCrossesList.add_URWellDCCrosses(urCross, dcCrosses); - } - } - - return urDCCrossesList; - } - - -} diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java index 3719402cd..efb6c1bdd 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java @@ -9,6 +9,7 @@ import org.jlab.geom.prim.Vector3D; import org.jlab.rec.dc.Constants; import org.jlab.rec.dc.cross.Cross; +import org.jlab.rec.urwell.reader.URWellCross; /** @@ -34,6 +35,23 @@ public Trajectory() { private final static double TOLERANCE = 0.1; // trajectory toleerance (cm) + private URWellCross urCross; + + /** + * + * @return URWell cross of track + */ + public URWellCross get_URWellCross() { + return urCross; + } + /** + * + * @param urCross URWell cross + */ + public void set_URWellCross(URWellCross urCross) { + this.urCross = urCross; + } + private double a; // Parameter a for fitting function in xz plane: f(x) = a*z^2 + b*z + c public void setA(double a){ diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrajectoryWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrajectoryWithURWellFinder.java index 669bd4244..5d9e3f9db 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrajectoryWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/TrajectoryWithURWellFinder.java @@ -1,13 +1,341 @@ package org.jlab.rec.dc.trajectory; +import java.util.ArrayList; +import java.util.List; +import org.jlab.clas.swimtools.Swim; +import org.jlab.detector.geant4.v2.DCGeant4Factory; +import org.jlab.geom.prim.Vector3D; +import org.jlab.rec.dc.Constants; +import org.jlab.rec.dc.cross.Cross; +import trackfitter.fitter.LineFitter; +import Jama.Matrix; +import org.jlab.rec.urwell.reader.URWellCross; /** * A driver class to find the trajectory of a track candidate. NOTE THAT THE PATH TO FIELD MAPS IS SET BY THE CLARA_SERVICES ENVIRONMENT VAR. * - * @author ziegler + * @author Tongtong Cao * */ -public class TrajectoryWithURWellFinder extends TrajectoryFinder{ + +public class TrajectoryWithURWellFinder { + + private LineFitter lineFit; + + private double[] x_fitCoeff; + private double[] y_fitCoeff; + + /** + * Field instantiated using the torus and the solenoid + */ + + /** + * Step size used in integral Bdl Riemann integration + */ + public double mmStepSizeForIntBdl = 10; + + private double PathLength; + + /** + * + * @return the value of the integral of the magnetic field over the path traveled by the particle as estimated from the fits to the crosses. + */ + int counter =0; + + public double TrajChisqProbFitXZ; + /** + * + * @param candCrossList the input list of crosses used in determining a trajectory + * @param DcDetector + * @param dcSwim + * @return a trajectory object + */ + public Trajectory findTrajectory(List candCrossList, URWellCross urCross, DCGeant4Factory DcDetector, Swim dcSwim) { + Trajectory traj = new Trajectory(); + if (candCrossList.isEmpty()) { + return traj; + } + + traj.addAll(candCrossList); + traj.set_URWellCross(urCross); + traj.setSector(candCrossList.get(0).get_Sector()); + fitTrajectory(candCrossList, urCross); + if (this.TrajChisqProbFitXZ getStateVecsAlongTrajectory(DCGeant4Factory DcDetector) { + List stateVecAtPlanesList = new ArrayList<>(36); + for(int superlayerIdx =0; superlayerIdx<6; superlayerIdx++) { + for(int layerIdx =0; layerIdx<6; layerIdx++) { + double z = DcDetector.getLayerMidpoint(superlayerIdx, layerIdx).z; + double x = x_fitCoeff[0]*z*z+x_fitCoeff[1]*z+x_fitCoeff[2]; + double y = y_fitCoeff[0]*z*z+y_fitCoeff[1]*z+y_fitCoeff[2]; + double tanTheta_x = x_fitCoeff[0]*z+x_fitCoeff[1]; + double tanTheta_y = y_fitCoeff[0]*z+y_fitCoeff[1]; + + StateVec stateVec = new StateVec(x,y,tanTheta_x, tanTheta_y); + stateVecAtPlanesList.add(stateVec); + } + } + return stateVecAtPlanesList; + } + + + /** + * The parametric form of the trajectory determined from fitting the tangent values of the state vecs linearly, + * and constraining the quadratic parameters of the function describing the position values of the state vecs. + * @param candCrossList list of crosses used in the fit + */ + public void fitTrajectory(List candCrossList, URWellCross urCross) { + x_fitCoeff = new double[3]; + y_fitCoeff = new double[3]; + + if(candCrossList.size() == 3){ + double[] theta_x = new double[3]; + double[] theta_x_err = new double[3]; + + double[] z = new double[3]; + + for (int i = 0; i < 3; i++) { + // make sure that the track direction makes sense + if (candCrossList.get(i).get_Dir().z() == 0) { + return; + } + + theta_x[i] = candCrossList.get(i).get_Dir().x() / candCrossList.get(i).get_Dir().z(); + theta_x_err[i] = calcTanErr(candCrossList.get(i).get_Dir().x(), candCrossList.get(i).get_Dir().z(), candCrossList.get(i).get_DirErr().x(), candCrossList.get(i).get_DirErr().z()); + } + + lineFit = new LineFitter(); + boolean linefitstatusOK = lineFit.fitStatus(z, theta_x, new double[3], theta_x_err, 3); + TrajChisqProbFitXZ = lineFit.getFit().getProb(); + } + else{ + TrajChisqProbFitXZ = Double.POSITIVE_INFINITY; + } + + if(urCross == null && candCrossList.size() == 3){ + double[] x = new double[3]; + double[] y = new double[3]; + double[] z = new double[3]; + + double[] x_err = new double[3]; + double[] y_err = new double[3]; + + for (int i = 0; i < 3; i++) { + // make sure that the track direction makes sense + if (candCrossList.get(i).get_Dir().z() == 0) { + return; + } + + x[i] = candCrossList.get(i).get_Point().x(); + y[i] = candCrossList.get(i).get_Point().y(); + z[i] = candCrossList.get(i).get_Point().z(); + + x_err[i] = candCrossList.get(i).get_PointErr().x(); + y_err[i] = candCrossList.get(i).get_PointErr().x(); + + } + + x_fitCoeff = quadraticLRFit(z, x, x_err); + y_fitCoeff = quadraticLRFit(z, y, y_err); + } + else if(urCross != null && candCrossList.size() == 2){ + double[] x = new double[3]; + double[] y = new double[3]; + double[] z = new double[3]; + + double[] x_err = new double[3]; + double[] y_err = new double[3]; + + for (int i = 0; i < 2; i++) { + // make sure that the track direction makes sense + if (candCrossList.get(i).get_Dir().z() == 0) { + return; + } + + x[i] = candCrossList.get(i).get_Point().x(); + y[i] = candCrossList.get(i).get_Point().y(); + z[i] = candCrossList.get(i).get_Point().z(); + + x_err[i] = candCrossList.get(i).get_PointErr().x(); + y_err[i] = candCrossList.get(i).get_PointErr().x(); + } + + x[2] = urCross.local().x(); + y[2] = urCross.local().y(); + z[2] = urCross.local().z(); + + x_err[2] = Constants.URWELLXRESOLUTION; + y_err[2] = Constants.URWELLYRESOLUTION; + + x_fitCoeff = quadraticLRFit(z, x, x_err); + y_fitCoeff = quadraticLRFit(z, y, y_err); + } + else if(urCross != null && candCrossList.size() == 3){ + double[] x = new double[4]; + double[] y = new double[4]; + double[] z = new double[4]; + + double[] x_err = new double[4]; + double[] y_err = new double[4]; + + for (int i = 0; i < 3; i++) { + // make sure that the track direction makes sense + if (candCrossList.get(i).get_Dir().z() == 0) { + return; + } + + x[i] = candCrossList.get(i).get_Point().x(); + y[i] = candCrossList.get(i).get_Point().y(); + z[i] = candCrossList.get(i).get_Point().z(); + + x_err[i] = candCrossList.get(i).get_PointErr().x(); + y_err[i] = candCrossList.get(i).get_PointErr().x(); + } + + x[3] = urCross.local().x(); + y[3] = urCross.local().y(); + z[3] = urCross.local().z(); + + x_err[3] = Constants.URWELLXRESOLUTION; + y_err[3] = Constants.URWELLYRESOLUTION; + + x_fitCoeff = quadraticLRFit(z, x, x_err); + y_fitCoeff = quadraticLRFit(z, y, y_err); + } + } - public TrajectoryWithURWellFinder() { - } + private double[] quadraticLRFit(double[] x, double[] y, double[] err) { + double[] ret = {0., 0., 0.}; + + Matrix A = new Matrix(3, 3); + Matrix V = new Matrix(3, 1); + double sum1 = 0.0; + double sum2 = 0.0; + double sum3 = 0.0; + double sum4 = 0.0; + double sum5 = 0.0; + double sum6 = 0.0; + double sum7 = 0.0; + double sum8 = 0.0; + for (int i = 0; i < x.length; ++i) { + double y1 = y[i]; + double x1 = x[i]; + double x2 = x1 * x1; + double x3 = x2 * x1; + double x4 = x2 * x2; + double e2 = err[i] * err[i]; + sum1 += x4 / e2; + sum2 += x3 / e2; + sum3 += x2 / e2; + sum4 += x1 / e2; + sum5 += 1.0 / e2; + sum6 += y1 * x2 / e2; + sum7 += y1 * x1 / e2; + sum8 += y1 / e2; + } + A.set(0, 0, sum1); + A.set(0, 1, sum2); + A.set(0, 2, sum3); + A.set(1, 0, sum2); + A.set(1, 1, sum3); + A.set(1, 2, sum4); + A.set(2, 0, sum3); + A.set(2, 1, sum4); + A.set(2, 2, sum5); + V.set(0, 0, sum6); + V.set(1, 0, sum7); + V.set(2, 0, sum8); + Matrix Ainv = A.inverse(); + Matrix X; + try { + X = Ainv.times(V); + for (int i = 0; i < 3; ++i) { + ret[i] = X.get(i, 0); + } + + } catch (ArithmeticException e) { + // TODO Auto-generated catch block + } + return (ret); + } + + /** + * + * @param num + * @param denom + * @param numEr + * @param denomEr + * @return the error on the tangent num/denom. + */ + private double calcTanErr(double num, double denom, double numEr, double denomEr) { + double d1 = num/(denom*denom); + double e1 = denomEr; + double d2 = 1/denom; + double e2 = numEr; + + return Math.sqrt(d1*d1+e1*e1 + d2*d2*e2*e2); + } + } diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java index 59fd6a60c..40f850731 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java @@ -20,8 +20,8 @@ import org.jlab.rec.dc.segment.Segment; import org.jlab.rec.dc.track.Track; import org.jlab.rec.dc.track.TrackCandListWithURWellFinder; -import org.jlab.rec.dc.track.URWellDCCrossesList; -import org.jlab.rec.dc.track.URWellDCCrossesListFinder; +import org.jlab.rec.dc.cross.URWellDCCrossesList; +import org.jlab.rec.dc.cross.URWellDCCrossesListFinder; import org.jlab.rec.urwell.reader.URWellCross; import org.jlab.rec.urwell.reader.URWellReader; diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java index 636fa7aff..53300e3d3 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterConv.java @@ -14,20 +14,18 @@ import org.jlab.rec.dc.cluster.ClusterFitter; import org.jlab.rec.dc.cluster.FittedCluster; import org.jlab.rec.dc.cross.Cross; -import org.jlab.rec.dc.cross.CrossList; -import org.jlab.rec.dc.cross.CrossListFinder; import org.jlab.rec.dc.cross.CrossMaker; import org.jlab.rec.dc.hit.FittedHit; import org.jlab.rec.dc.segment.Segment; import org.jlab.rec.dc.segment.SegmentFinder; import org.jlab.rec.dc.track.Track; import org.jlab.rec.dc.track.TrackCandListWithURWellFinder; -import org.jlab.rec.dc.track.URWellDCCrossesList; +import org.jlab.rec.dc.cross.URWellDCCrossesList; import org.jlab.rec.dc.trajectory.Road; import org.jlab.rec.dc.trajectory.RoadFinder; import org.jlab.rec.urwell.reader.URWellReader; import org.jlab.rec.urwell.reader.URWellCross; -import org.jlab.rec.dc.track.URWellDCCrossesListFinder; +import org.jlab.rec.dc.cross.URWellDCCrossesListFinder; /** * @@ -114,29 +112,28 @@ public boolean processDataEvent(DataEvent event) { return true; } /* 17 */ - CrossListFinder crossLister = new CrossListFinder(); - - CrossList crosslist = crossLister.candCrossLists(event, crosses, + // Read urwell crosses + URWellReader uRWellReader = new URWellReader(event, "HB"); + List urCrosses = uRWellReader.getUrwellCrosses(); + URWellDCCrossesListFinder uRWellDCCrossListLister = new URWellDCCrossesListFinder(); + // Build 4-crosses combos, where 1 cross from uRWell, and other 3 crosses from DCs + URWellDCCrossesList urDC4CrossesList = uRWellDCCrossListLister.candURWellDCCrossesLists(event, crosses, urCrosses, false, null, Constants.getInstance().dcDetector, null, - dcSwim, false); - /* 18 */ - // Read urwell crosses, and make urwell-dc-crosses combos - URWellReader uRWellReader = new URWellReader(event, "HB"); - List urCrosses = uRWellReader.getUrwellCrosses(); - URWellDCCrossesListFinder uRWellDCCrossListLister = new URWellDCCrossesListFinder(); - URWellDCCrossesList urDCCrossesList = uRWellDCCrossListLister.candURWellDCCrossLists(urCrosses, crosslist); - - //6) find the list of track candidates + dcSwim, false, 4 + ); + /* 18 */ + //6) Build tracks for 4-crosses combos TrackCandListWithURWellFinder trkcandFinder = new TrackCandListWithURWellFinder(Constants.HITBASE); - trkcands = trkcandFinder.getTrackCands(urDCCrossesList, + trkcands = trkcandFinder.getTrackCands(urDC4CrossesList, Constants.getInstance().dcDetector, Swimmer.getTorScale(), dcSwim, false); /* 19 */ // track found + List urCrossesOnTrks = new ArrayList(); int trkId = 1; if (!trkcands.isEmpty()) { // remove overlaps @@ -148,9 +145,19 @@ public boolean processDataEvent(DataEvent event) { trk, Constants.getInstance().dcDetector, dcSwim); + if(trk.get_URWellCross() != null){ + urCrossesOnTrks.add(trk.get_URWellCross()); + trk.get_URWellCross().set_tid(trk.get_Id()); + } trkId++; } } + + /** Build pseudo-segments based on five real segments at the same sector + * Firstly, build roads by 3 segments at odd/even superlayers + * For each road, check if one segment has no associated cross, while other two segments have associated crosses, which are not on tracks + * If yes, build a pseudo-segment, and then a pseudo-cross with the real segment without associated cross + */ List crossSegsNotOnTrack = new ArrayList<>(); List psegments = new ArrayList<>(); @@ -200,23 +207,25 @@ public boolean processDataEvent(DataEvent event) { segments.addAll(psegments); List pcrosses = crossMake.find_Crosses(segments, Constants.getInstance().dcDetector); - - CrossList pcrosslist = crossLister.candCrossLists(event, pcrosses, + + // Remove uRWell crosses on tracks + urCrosses.removeAll(urCrossesOnTrks); + // Build pseudo 4-crosses combos, where there is pseudo dc cross + URWellDCCrossesList purDC4CrossesList = uRWellDCCrossListLister.candURWellDCCrossesLists(event, pcrosses, urCrosses, false, null, Constants.getInstance().dcDetector, null, - dcSwim, true); - //pcrosslist.removeDuplicates(crosslist); - - URWellDCCrossesList purDCCrossesList = uRWellDCCrossListLister.candURWellDCCrossLists(urCrosses, pcrosslist); - - List mistrkcands = trkcandFinder.getTrackCands(purDCCrossesList, + dcSwim, false, 4 + ); + //Build tracks for pseudo 4-crosses combos + List mistrkcands = trkcandFinder.getTrackCands(purDC4CrossesList, Constants.getInstance().dcDetector, Swimmer.getTorScale(), dcSwim, false); // remove overlaps + List urCrossesOnTrksWithPSegment = new ArrayList(); if (!mistrkcands.isEmpty()) { trkcandFinder.removeOverlappingTracks(mistrkcands); for (Track trk : mistrkcands) { @@ -227,12 +236,106 @@ public boolean processDataEvent(DataEvent event) { trk, Constants.getInstance().dcDetector, dcSwim); + if(trk.get_URWellCross() != null){ + urCrossesOnTrksWithPSegment.add(trk.get_URWellCross()); + urCrossesOnTrks.add(trk.get_URWellCross()); + trk.get_URWellCross().set_tid(trk.get_Id()); + } trkId++; } } trkcands.addAll(mistrkcands); - + + ////// Tracking for 3-URDCCrosses combos + // Add real dc crosses into crossesOnTrack list + List dcCrossesOnTrack = new ArrayList(); + for(Cross dcCross : crosses){ + Segment seg1 = dcCross.get_Segment1(); + Segment seg2 = dcCross.get_Segment2(); + if(seg1.isOnTrack==true && seg2.isOnTrack==true && seg1.associatedCrossId==seg2.associatedCrossId && dcCross.get_Id() != -1){ + dcCrossesOnTrack.add(dcCross); + } + } + + // Remove real dc crosses on tracks + crosses.removeAll(dcCrossesOnTrack); + // Further remove uRWell crosses on tracks with pseudo-segment + urCrosses.removeAll(urCrossesOnTrksWithPSegment); + // Build 3-crosses combos from any of 3 regions + URWellDCCrossesList urDC3CrossesList = uRWellDCCrossListLister.candURWellDCCrossesLists(event, crosses, urCrosses, + false, + null, + Constants.getInstance().dcDetector, + null, + dcSwim, false, 3 + ); + // Build tracks for 3-crosses combos + List trkcands3URDCCrosses = trkcandFinder.getTrackCands3URDCCrosses(urDC3CrossesList, + Constants.getInstance().dcDetector, + Swimmer.getTorScale(), + dcSwim, false); + if (!trkcands3URDCCrosses.isEmpty()) { + trkcandFinder.removeOverlappingTracks(trkcands3URDCCrosses); + for (Track trk : trkcands3URDCCrosses) { + + // reset the id + trk.set_Id(trkId); + trkcandFinder.matchHits(trk.getStateVecs(), + trk, + Constants.getInstance().dcDetector, + dcSwim); + + if(trk.get_URWellCross() != null){ + urCrossesOnTrks.add(trk.get_URWellCross()); + trk.get_URWellCross().set_tid(trk.get_Id()); + } + trkId++; + } + } + + trkcands.addAll(trkcands3URDCCrosses); + + ////// Tracking for pseudo 3-DCcrosses combos + // Remove dc crosses on tracks from pcrosses + List pcrossesOnTrack = new ArrayList(); + for(Cross dcCross : pcrosses){ + Segment seg1 = dcCross.get_Segment1(); + Segment seg2 = dcCross.get_Segment2(); + if(seg1.isOnTrack==true && seg2.isOnTrack==true && seg1.associatedCrossId==seg2.associatedCrossId){ + pcrossesOnTrack.add(dcCross); + } + } + pcrosses.removeAll(pcrossesOnTrack); + // Build pseudo 3-DCcrosses combos + URWellDCCrossesList pdc3CrossesList = uRWellDCCrossListLister.candURWellDCCrossesLists(event, pcrosses, new ArrayList(), + false, + null, + Constants.getInstance().dcDetector, + null, + dcSwim, false, 3 + ); + // Build tracks for pseudo 3-DCcrosses combos + List trkcands3pDCCrosses = trkcandFinder.getTrackCands3URDCCrosses(pdc3CrossesList, + Constants.getInstance().dcDetector, + Swimmer.getTorScale(), + dcSwim, false); + if (!trkcands3pDCCrosses.isEmpty()) { + trkcandFinder.removeOverlappingTracks(trkcands3pDCCrosses); + for (Track trk : trkcands3pDCCrosses) { + + // reset the id + trk.set_Id(trkId); + trkcandFinder.matchHits(trk.getStateVecs(), + trk, + Constants.getInstance().dcDetector, + dcSwim); + trkId++; + } + } + + trkcands.addAll(trkcands3pDCCrosses); + LOGGER.log(Level.FINE, "Found after 5STg "+mistrkcands.size()+" HB seeds "); for(int i = 0; i< trkcands.size(); i++) { LOGGER.log(Level.FINE, "cand "+i); @@ -242,8 +345,7 @@ public boolean processDataEvent(DataEvent event) { LOGGER.log(Level.FINE, "------------------------------------------------------------------ "); } - //gather all the hits and URWell crosses for pointer bank creation - List urCrossesOnTrks = new ArrayList(); + //gather all the hits and URWell crosses for pointer bank creation for (Track trk : trkcands) { trk.calcTrajectory(trk.getId(), dcSwim, trk.get_Vtx0(), trk.get_pAtOrig(), trk.get_Q()); for (Cross c : trk) { @@ -261,17 +363,11 @@ public boolean processDataEvent(DataEvent event) { if(h2.get_AssociatedHBTrackID()>0) fhits.add(h2); } } - if(trk.get_URWellCross() != null){ - urCrossesOnTrks.add(trk.get_URWellCross()); - trk.get_URWellCross().set_tid(trk.get_Id()); - } } - - - - + // no candidate found, stop here and save the hits, // the clusters, the segments, the crosses + crosses.addAll(dcCrossesOnTrack); if (trkcands.isEmpty()) { event.appendBanks( writer.fillHBHitsBank(event, fhits), diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index ac45a5a35..ac963db47 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -32,11 +32,12 @@ import org.jlab.rec.dc.track.Track; import org.jlab.rec.dc.trajectory.StateVec; import org.jlab.rec.dc.trajectory.Trajectory; -import org.jlab.rec.dc.trajectory.TrajectoryFinder; +import org.jlab.rec.dc.trajectory.TrajectoryWithURWellFinder; import org.jlab.utils.groups.IndexedTable; import org.jlab.clas.tracking.kalmanfilter.Surface; import org.jlab.clas.tracking.kalmanfilter.zReference.KFitterWithURWell; +import org.jlab.clas.tracking.kalmanfilter.zReference.KFitter; import org.jlab.clas.tracking.kalmanfilter.zReference.StateVecs; import org.jlab.clas.tracking.utilities.MatrixOps.Libr; import org.jlab.clas.tracking.utilities.RungeKuttaDoca; @@ -200,16 +201,17 @@ public boolean processDataEvent(DataEvent event) { HBtrk.setFinalStateVec(HBFinalSV); int urcross_id = trkbank.getShort("URWellCross_ID", i); - for(int j = 0; j < urCrosses.size(); j++){ - if(urCrosses.get(j).id() == urcross_id){ - HBtrk.set_URWellCross(urCrosses.get(j)); - break; + if(urcross_id > 0){ + for(int j = 0; j < urCrosses.size(); j++){ + if(urCrosses.get(j).id() == urcross_id){ + HBtrk.set_URWellCross(urCrosses.get(j)); + break; + } } } - TrackArray[HBtrk.get_Id()-1] = HBtrk; TrackArray[HBtrk.get_Id()-1].set_Status(0); - + TrackArray[HBtrk.get_Id()-1].set_Status_crossCombo(trkbank.getShort("status", i)/1000); } if(TrackArray==null) { return true; // HB tracks not saved correctly @@ -221,7 +223,7 @@ public boolean processDataEvent(DataEvent event) { TrackArray[seg.get(0).get_AssociatedHBTrackID()-1].set_Status(1); } } - + //6) find the list of track candidates // read beam offsets from database double beamXoffset, beamYoffset; @@ -234,10 +236,10 @@ public boolean processDataEvent(DataEvent event) { beamYoffset += raster_bank.getFloat("y", 0); } TrackCandListWithURWellFinder trkcandFinder = new TrackCandListWithURWellFinder("TimeBased"); - TrajectoryFinder trjFind = new TrajectoryFinder(); + TrajectoryWithURWellFinder trjFind = new TrajectoryWithURWellFinder(); for (Track TrackArray1 : TrackArray) { - if (TrackArray1 == null || TrackArray1.get_ListOfHBSegments() == null || TrackArray1.get_ListOfHBSegments().size() < 5) { + if (TrackArray1 == null || TrackArray1.get_ListOfHBSegments() == null || TrackArray1.get_ListOfHBSegments().size() < 3) { continue; } TrackArray1.set_MissingSuperlayer(get_Status(TrackArray1)); @@ -245,49 +247,90 @@ public boolean processDataEvent(DataEvent event) { if (TrackArray1.size() < 1) { continue; } + crosses.addAll(TrackArray1); + if(TrackArray1.get_URWellCross() != null){ + KFitterWithURWell kFZRef = new KFitterWithURWell(true, 30, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitter(); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + StateVec fn = new StateVec(); + if (kFZRef.setFitFailed==false && kFZRef.finalStateVec!=null) { + // set the state vector at the last measurement site + fn.set(kFZRef.finalStateVec.x, kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); + //set the track parameters if the filter does not fail + TrackArray1.set_P(1./Math.abs(kFZRef.finalStateVec.Q)); + TrackArray1.set_Q((int)Math.signum(kFZRef.finalStateVec.Q)); + + trkcandFinder.setTrackPars(TrackArray1, new Trajectory(), trjFind, fn, kFZRef.finalStateVec.z, Constants.getInstance().dcDetector, dcSwim, beamXoffset, beamYoffset); + // candidate parameters are set from the state vector + if (TrackArray1.fit_Successful == false) { + continue; + } + + TrackArray1.set_FitChi2(kFZRef.chi2); + TrackArray1.set_FitNDF(kFZRef.NDF); + TrackArray1.setStateVecs(kfStateVecsAlongTrajectory); + TrackArray1.set_FitConvergenceStatus(kFZRef.ConvStatus); + if (TrackArray1.get_Vtx0().toVector3D().mag() > 500) { + continue; + } - KFitterWithURWell kFZRef = new KFitterWithURWell(true, 30, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); - List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); - StateVecs svs = new StateVecs(); - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); - kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); - kFZRef.runFitter(); - List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); - - StateVec fn = new StateVec(); - if (kFZRef.setFitFailed==false && kFZRef.finalStateVec!=null) { - // set the state vector at the last measurement site - fn.set(kFZRef.finalStateVec.x, kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); - //set the track parameters if the filter does not fail - TrackArray1.set_P(1./Math.abs(kFZRef.finalStateVec.Q)); - TrackArray1.set_Q((int)Math.signum(kFZRef.finalStateVec.Q)); - - trkcandFinder.setTrackPars(TrackArray1, new Trajectory(), trjFind, fn, kFZRef.finalStateVec.z, Constants.getInstance().dcDetector, dcSwim, beamXoffset, beamYoffset); - // candidate parameters are set from the state vector - if (TrackArray1.fit_Successful == false) { - continue; - } - - TrackArray1.set_FitChi2(kFZRef.chi2); - TrackArray1.set_FitNDF(kFZRef.NDF); - TrackArray1.setStateVecs(kfStateVecsAlongTrajectory); - TrackArray1.set_FitConvergenceStatus(kFZRef.ConvStatus); - if (TrackArray1.get_Vtx0().toVector3D().mag() > 500) { - continue; - } - - // get CovMat at vertex - Point3D VTCS = crosses.get(0).getCoordsInSector(TrackArray1.get_Vtx0().x(), TrackArray1.get_Vtx0().y(), TrackArray1.get_Vtx0().z()); - TrackArray1.set_CovMat(kFZRef.propagateToVtx(crosses.get(0).get_Sector(), VTCS.z())); - - if (TrackArray1.isGood()) { - trkcands.add(TrackArray1); - } + // get CovMat at vertex + Point3D VTCS = crosses.get(0).getCoordsInSector(TrackArray1.get_Vtx0().x(), TrackArray1.get_Vtx0().y(), TrackArray1.get_Vtx0().z()); + TrackArray1.set_CovMat(kFZRef.propagateToVtx(crosses.get(0).get_Sector(), VTCS.z())); + if (TrackArray1.isGood()) { + trkcands.add(TrackArray1); + } + } } - + else{ + KFitter kFZRef = new KFitter(true, 30, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitter(); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + StateVec fn = new StateVec(); + if (kFZRef.setFitFailed==false && kFZRef.finalStateVec!=null) { + // set the state vector at the last measurement site + fn.set(kFZRef.finalStateVec.x, kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); + //set the track parameters if the filter does not fail + TrackArray1.set_P(1./Math.abs(kFZRef.finalStateVec.Q)); + TrackArray1.set_Q((int)Math.signum(kFZRef.finalStateVec.Q)); + + trkcandFinder.setTrackPars(TrackArray1, new Trajectory(), trjFind, fn, kFZRef.finalStateVec.z, Constants.getInstance().dcDetector, dcSwim, beamXoffset, beamYoffset); + // candidate parameters are set from the state vector + if (TrackArray1.fit_Successful == false) { + continue; + } + + TrackArray1.set_FitChi2(kFZRef.chi2); + TrackArray1.set_FitNDF(kFZRef.NDF); + TrackArray1.setStateVecs(kfStateVecsAlongTrajectory); + TrackArray1.set_FitConvergenceStatus(kFZRef.ConvStatus); + if (TrackArray1.get_Vtx0().toVector3D().mag() > 500) { + continue; + } + + // get CovMat at vertex + Point3D VTCS = crosses.get(0).getCoordsInSector(TrackArray1.get_Vtx0().x(), TrackArray1.get_Vtx0().y(), TrackArray1.get_Vtx0().z()); + TrackArray1.set_CovMat(kFZRef.propagateToVtx(crosses.get(0).get_Sector(), VTCS.z())); + + if (TrackArray1.isGood()) { + trkcands.add(TrackArray1); + } + } + } } if(!trkcands.isEmpty()) { @@ -352,6 +395,22 @@ public List setKFStateVecsAlongTrajectory(K return kfStateVecsAlongTrajectory; } + public List setKFStateVecsAlongTrajectory(KFitter kFZRef) { + List kfStateVecsAlongTrajectory = new ArrayList<>(); + + for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + sv.setZ(svc.z); + sv.setB(svc.B); + sv.setPathLength(svc.getPathLength()); + kfStateVecsAlongTrajectory.add(sv); + } + + return kfStateVecsAlongTrajectory; + } + public void getInitState(Track trkcand, double z0, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initStateVec, KFitterWithURWell kf, Swim dcSwim, float[] bf) { if (trkcand != null && trkcand.getFinalStateVec()!=null ) { initStateVec.x = trkcand.getFinalStateVec().x(); @@ -364,11 +423,105 @@ public void getInitState(Track trkcand, double z0, org.jlab.clas.tracking.kalman RungeKuttaDoca rk = new RungeKuttaDoca(); rk.SwimToZ(trkcand.get(0).get_Sector(), initStateVec, dcSwim, z0, bf); - double ex = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATEXUNC; - double ey = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATEYUNC; - double etx = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATETXUNC; - double ety = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATETYUNC; - double eQ = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATEQUNC; + double ex = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEXUNCUR; + double ey = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEYUNCUR; + double etx = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETXUNCUR; + double ety = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETYUNCUR; + double eQ = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEQUNCUR; + + if (trkcand.get_URWellCross() == null) { + ex = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEXUNCURNOR0; + ey = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEYUNCURNOR0; + etx = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETXUNCURNOR0; + ety = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETYUNCURNOR0; + eQ = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEQUNCURNOR0; + } else { + if (trkcand.size() == 2) { + if (trkcand.get(0).get_Region() == 2 && trkcand.get(1).get_Region() == 3) { + ex = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEXUNCURNOR1; + ey = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEYUNCURNOR1; + etx = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETXUNCURNOR1; + ety = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETYUNCURNOR1; + eQ = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEQUNCURNOR1; + } else if (trkcand.get(0).get_Region() == 1 && trkcand.get(1).get_Region() == 3) { + ex = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEXUNCURNOR2; + ey = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEYUNCURNOR2; + etx = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETXUNCURNOR2; + ety = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETYUNCURNOR2; + eQ = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEQUNCURNOR2; + } else if (trkcand.get(0).get_Region() == 1 && trkcand.get(1).get_Region() == 2) { + ex = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEXUNCURNOR3; + ey = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEYUNCURNOR3; + etx = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETXUNCURNOR3; + ety = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETYUNCURNOR3; + eQ = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEQUNCURNOR3; + } + } + } + + //Matrix initCMatrix = new Matrix(FTF); + Matrix initCMatrix = new Matrix(); + initCMatrix.set(ex * ex, 0, 0, 0, 0, + 0, ey * ey, 0, 0, 0, + 0, 0, etx * etx, 0, 0, + 0, 0, 0, ety * ety, 0, + 0, 0, 0, 0, eQ * eQ + ); + initStateVec.CM = initCMatrix; + + } else { + kf.setFitFailed = true; + } + + } + + public void getInitState(Track trkcand, double z0, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initStateVec, KFitter kf, Swim dcSwim, float[] bf) { + if (trkcand != null && trkcand.getFinalStateVec()!=null ) { + initStateVec.x = trkcand.getFinalStateVec().x(); + initStateVec.y = trkcand.getFinalStateVec().y(); + initStateVec.z = trkcand.getFinalStateVec().getZ(); + initStateVec.tx = trkcand.getFinalStateVec().tanThetaX(); + initStateVec.ty = trkcand.getFinalStateVec().tanThetaY(); + initStateVec.Q = ((double) trkcand.get_Q())/trkcand.get_P(); + + RungeKuttaDoca rk = new RungeKuttaDoca(); + rk.SwimToZ(trkcand.get(0).get_Sector(), initStateVec, dcSwim, z0, bf); + + double ex = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEXUNCUR; + double ey = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEYUNCUR; + double etx = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETXUNCUR; + double ety = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETYUNCUR; + double eQ = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEQUNCUR; + + if (trkcand.get_URWellCross() == null) { + ex = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEXUNCURNOR0; + ey = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEYUNCURNOR0; + etx = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETXUNCURNOR0; + ety = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETYUNCURNOR0; + eQ = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEQUNCURNOR0; + } else { + if (trkcand.size() == 2) { + if (trkcand.get(0).get_Region() == 2 && trkcand.get(1).get_Region() == 3) { + ex = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEXUNCURNOR1; + ey = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEYUNCURNOR1; + etx = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETXUNCURNOR1; + ety = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETYUNCURNOR1; + eQ = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEQUNCURNOR1; + } else if (trkcand.get(0).get_Region() == 1 && trkcand.get(1).get_Region() == 3) { + ex = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEXUNCURNOR2; + ey = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEYUNCURNOR2; + etx = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETXUNCURNOR2; + ety = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETYUNCURNOR2; + eQ = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEQUNCURNOR2; + } else if (trkcand.get(0).get_Region() == 1 && trkcand.get(1).get_Region() == 2) { + ex = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEXUNCURNOR3; + ey = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEYUNCURNOR3; + etx = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETXUNCURNOR3; + ety = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATETYUNCURNOR3; + eQ = Constants.TBINITIALSTATEUNCSCALEUR * Constants.TBINITIALSTATEQUNCURNOR3; + } + } + } //Matrix initCMatrix = new Matrix(FTF); Matrix initCMatrix = new Matrix(); @@ -469,9 +622,11 @@ public List getMeasSurfaces(Track trk, DCGeant4Factory DcDetector) { FittedHit hitOnTrk; URWellCross urCross = trk.get_URWellCross(); - HitOnTrack urhot = new HitOnTrack(urCross.sector(), urCross.local().x(), urCross.local().y(), urCross.local().z(), 0.02, 0.04); - urhot.isDCHit = false; - hOTS.add(urhot); + if(urCross != null){ + HitOnTrack urhot = new HitOnTrack(urCross.sector(), urCross.local().x(), urCross.local().y(), urCross.local().z(), 0.02, 0.04); + urhot.isDCHit = false; + hOTS.add(urhot); + } for(int s = 0; s < trk.get_ListOfHBSegments().size(); s++) { for(int h = 0; h < trk.get_ListOfHBSegments().get(s).size(); h++) { @@ -608,6 +763,15 @@ private int get_Status(Track track) { L[track.get_ListOfHBSegments().get(l).get_Superlayer()-1]++; SegMap.put(track.get_ListOfHBSegments().get(l).get_Superlayer(), track.get_ListOfHBSegments().get(l)); } + + // In case that a cross is missed + for(int l = 0; l < 3; l++){ + if(L[2*l] == 0 && L[2*l+1] == 0){ + L[2*l] = -1; + L[2*l+1] = -1; + } + } + for(int l = 0; l<6; l++) { if(L[l]==0){ miss=l+1; From 582118af9739ec9a34f22e7db0474473b7270333 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 19 Jul 2023 13:46:59 -0400 Subject: [PATCH 23/69] fix issue that some information for states in trajectory is missed --- .../track/TrackCandListWithURWellFinder.java | 5 ++--- .../org/jlab/service/dc/DCURWellTBEngine.java | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java index cae104887..462fda90e 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -1491,9 +1491,8 @@ public List setKFStateVecsAlongTrajectory(K List kfStateVecsAlongTrajectory = new ArrayList<>(); for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); - - org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); sv.setZ(svc.z); sv.setB(svc.B); sv.setPathLength(svc.getPathLength()); diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index c5fa85b49..5857feef0 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -380,12 +380,13 @@ public List setKFStateVecsAlongTrajectory(K List kfStateVecsAlongTrajectory = new ArrayList<>(); for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); - - org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); sv.setZ(svc.z); sv.setB(svc.B); - sv.setPathLength(svc.getPathLength()); + sv.setPathLength(svc.getPathLength()); + sv.setProjector(svc.getProjector()); + sv.setProjectorDoca(svc.getProjectorDoca()); kfStateVecsAlongTrajectory.add(sv); } @@ -396,12 +397,13 @@ public List setKFStateVecsAlongTrajectory(K List kfStateVecsAlongTrajectory = new ArrayList<>(); for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); - - org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); sv.setZ(svc.z); sv.setB(svc.B); - sv.setPathLength(svc.getPathLength()); + sv.setPathLength(svc.getPathLength()); + sv.setProjector(svc.getProjector()); + sv.setProjectorDoca(svc.getProjectorDoca()); kfStateVecsAlongTrajectory.add(sv); } From 4692bd0fea92496acb17daeafa0194123a6c2556 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 19 Jul 2023 13:58:30 -0400 Subject: [PATCH 24/69] tiny change --- .../org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java index 462fda90e..6126ecaef 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -1508,9 +1508,8 @@ public List setKFStateVecsAlongTrajectory(K List kfStateVecsAlongTrajectory = new ArrayList<>(); for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); - - org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); sv.setZ(svc.z); sv.setB(svc.B); sv.setPathLength(svc.getPathLength()); From b707d0963a46d00eaafed30ad965221652617197 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 19 Jul 2023 14:24:14 -0400 Subject: [PATCH 25/69] update pom file --- reconstruction/dc/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reconstruction/dc/pom.xml b/reconstruction/dc/pom.xml index 2991d7a8e..af4f3d58a 100644 --- a/reconstruction/dc/pom.xml +++ b/reconstruction/dc/pom.xml @@ -28,7 +28,7 @@ org.jlab.clas clas-tracking - 8.4.0-SNAPSHOT + 10.0.2-SNAPSHOT From 0484aa52489e385d01fe92fed89572ae48df05ea Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Tue, 8 Aug 2023 11:54:35 -0400 Subject: [PATCH 26/69] change uRWell resolution setup in TB tracking --- .../dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index 5857feef0..cec6a4c72 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -622,7 +622,7 @@ public List getMeasSurfaces(Track trk, DCGeant4Factory DcDetector) { URWellCross urCross = trk.get_URWellCross(); if(urCross != null){ - HitOnTrack urhot = new HitOnTrack(urCross.sector(), urCross.local().x(), urCross.local().y(), urCross.local().z(), 0.02, 0.04); + HitOnTrack urhot = new HitOnTrack(urCross.sector(), urCross.local().x(), urCross.local().y(), urCross.local().z(), Constants.URWELLXRESOLUTION, Constants.URWELLYRESOLUTION); urhot.isDCHit = false; hOTS.add(urhot); } From 39dd88790a016da8f73126ae6763b4d2e0e1a7c5 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 18 Aug 2023 16:20:38 -0400 Subject: [PATCH 27/69] fix an issue for writting KF fit doca --- .../java/org/jlab/rec/dc/track/TrackCandListFinder.java | 5 ++--- .../dc/src/main/java/org/jlab/service/dc/DCTBEngine.java | 9 +++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index 419d488db..437a052f2 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -1117,9 +1117,8 @@ public List setKFStateVecsAlongTrajectory(K List kfStateVecsAlongTrajectory = new ArrayList<>(); for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); - - org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); sv.setZ(svc.z); sv.setB(svc.B); sv.setPathLength(svc.getPathLength()); diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index 6f7d445d3..094ec6c05 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -319,12 +319,13 @@ public List setKFStateVecsAlongTrajectory(K List kfStateVecsAlongTrajectory = new ArrayList<>(); for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); - - org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); sv.setZ(svc.z); sv.setB(svc.B); - sv.setPathLength(svc.getPathLength()); + sv.setPathLength(svc.getPathLength()); + sv.setProjector(svc.getProjector()); + sv.setProjectorDoca(svc.getProjectorDoca()); kfStateVecsAlongTrajectory.add(sv); } From 670e0d1712ca8c174c1479827e79b304e38563ad Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 15 Sep 2023 16:20:48 -0400 Subject: [PATCH 28/69] blow up initial covariance matrix starting from the second iteration --- .../kalmanfilter/zReference/KFitter.java | 37 +++++++++++++------ .../zReference/KFitterWithURWell.java | 37 +++++++++++++------ .../main/java/org/jlab/rec/dc/Constants.java | 8 ++-- 3 files changed, 56 insertions(+), 26 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index eccca0ec6..7fd526336 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -23,6 +23,8 @@ */ public class KFitter extends AKFitter { + private static final double initialCMBlowupFactor = 70; + private StateVecs sv = new StateVecs(); private MeasVecs mv = new MeasVecs(); private StateVec finalSmoothedStateVec = null; @@ -192,6 +194,19 @@ public void runFitter() { break; } } else { + double c00 = this.sv.trackTrajB.get(0).CM.get(0, 0); + double c11 = this.sv.trackTrajB.get(0).CM.get(1, 1); + double c22 = this.sv.trackTrajB.get(0).CM.get(2, 2); + double c33 = this.sv.trackTrajB.get(0).CM.get(3, 3); + double c44 = this.sv.trackTrajB.get(0).CM.get(4, 4); + Matrix newCM = new Matrix(); + newCM.set(c00*initialCMBlowupFactor, 0, 0, 0, 0, + 0, c11*initialCMBlowupFactor, 0, 0, 0, + 0, 0, c22*initialCMBlowupFactor, 0, 0, + 0, 0, 0, c33*initialCMBlowupFactor, 0, + 0, 0, 0, 0, c44*initialCMBlowupFactor); + this.sv.trackTrajB.get(0).CM = newCM; + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajB.get(0), mv, this.getSwimmer(), forward)) { this.stopIteration = true; break; @@ -222,19 +237,19 @@ public void runFitter() { if (this.setFitFailed == false) { if (this.finalStateVec != null) { if (!TBT) { - if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 3.3e-3 - && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 7.3e-2 - && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 4.3e-1 - && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 9.2e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 2.1e-3) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 1.6e-3 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 1.2e-2 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 1.4e-1 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 2.5e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.0e-3) { i = totNumIter; } } else { - if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 5.5e-5 - && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 5.0e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 2.1e-3 - && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 8.8e-6 - && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.4e-5) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 1.1e-5 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 5.5e-5 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 8.0e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 2.1e-6 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 3.5e-6) { i = totNumIter; } } @@ -506,7 +521,7 @@ private void calcFinalChisq(int sector, boolean nofilter) { } public Matrix propagateToVtx(int sector, double Zf) { - return sv.transport(sector, 0, Zf, sv.trackTrajP.get(0), mv, this.getSwimmer()); + return sv.transport(sector, finalStateVec.k, Zf, finalStateVec, mv, this.getSwimmer()); } @Override diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index ed4cbde49..a630aceb5 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -27,6 +27,8 @@ public class KFitterWithURWell extends AKFitter { private static final Logger LOGGER = Logger.getLogger(KFitter.class.getName()); + + private static final double initialCMBlowupFactor = 70; private StateVecs sv = new StateVecs(); private MeasVecs mv = new MeasVecs(); @@ -203,6 +205,19 @@ public void runFitter() { break; } } else { + double c00 = this.sv.trackTrajB.get(0).CM.get(0, 0); + double c11 = this.sv.trackTrajB.get(0).CM.get(1, 1); + double c22 = this.sv.trackTrajB.get(0).CM.get(2, 2); + double c33 = this.sv.trackTrajB.get(0).CM.get(3, 3); + double c44 = this.sv.trackTrajB.get(0).CM.get(4, 4); + Matrix newCM = new Matrix(); + newCM.set(c00*initialCMBlowupFactor, 0, 0, 0, 0, + 0, c11*initialCMBlowupFactor, 0, 0, 0, + 0, 0, c22*initialCMBlowupFactor, 0, 0, + 0, 0, 0, c33*initialCMBlowupFactor, 0, + 0, 0, 0, 0, c44*initialCMBlowupFactor); + this.sv.trackTrajB.get(0).CM = newCM; + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajB.get(0), mv, this.getSwimmer(), forward)) { this.stopIteration = true; break; @@ -233,19 +248,19 @@ public void runFitter() { if (this.setFitFailed == false) { if (this.finalStateVec != null) { if (!TBT) { - if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 3.3e-3 - && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 7.3e-2 - && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 4.3e-1 - && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 9.2e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 2.1e-3) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 1.6e-3 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 1.2e-2 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 1.4e-1 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 2.5e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.0e-3) { i = totNumIter; } } else { - if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 5.5e-5 - && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 5.0e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 2.1e-3 - && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 8.8e-6 - && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.4e-5) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 1.1e-5 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 5.5e-5 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 8.0e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 2.1e-6 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 3.5e-6) { i = totNumIter; } } @@ -593,7 +608,7 @@ private void calcFinalChisq(int sector, boolean nofilter) { } public Matrix propagateToVtx(int sector, double Zf) { - return sv.transport(sector, 0, Zf, sv.trackTrajP.get(0), mv, this.getSwimmer()); + return sv.transport(sector, finalStateVec.k, Zf, finalStateVec, mv, this.getSwimmer()); } @Override diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java index 24cd7b2bd..538f37502 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java @@ -76,11 +76,11 @@ public static Constants getInstance() { public static final double HBINITIALSTATEQUNC = 0.13; public static final double TBINITIALSTATEUNCSCALE = 1.5; - public static final double TBINITIALSTATEXUNC = 0.11; - public static final double TBINITIALSTATEYUNC = 0.98; - public static final double TBINITIALSTATETXUNC = 0.0024; + public static final double TBINITIALSTATEXUNC = 0.14; + public static final double TBINITIALSTATEYUNC = 1.03; + public static final double TBINITIALSTATETXUNC = 0.0025; public static final double TBINITIALSTATETYUNC = 0.0091; - public static final double TBINITIALSTATEQUNC = 0.0085; + public static final double TBINITIALSTATEQUNC = 0.0084; ////// With uRWell public static final double HBINITIALSTATEUNCSCALEUR = 2; From 9fea9d9a5d098db677021115a62c07e72ec26cb0 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 12 Oct 2023 16:03:49 -0400 Subject: [PATCH 29/69] change limit for inverse calculation of covariance matrix --- .../clas/tracking/kalmanfilter/zReference/KFitter.java | 4 ++-- .../kalmanfilter/zReference/KFitterWithURWell.java | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 7fd526336..7e227dff1 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -410,7 +410,7 @@ private boolean filter(int k, boolean forward) { public Matrix filterCovMat(double[] H, Matrix Ci, double V) { double det = Matrix5x5.inverse(Ci, first_inverse, adj); - if (Math.abs(det) < 1.e-30) { + if (Math.abs(det) < 1.e-60) { return null; } @@ -423,7 +423,7 @@ public Matrix filterCovMat(double[] H, Matrix Ci, double V) { Matrix5x5.add(first_inverse, addition, result); double det2 = Matrix5x5.inverse(result, result_inv, adj); - if (Math.abs(det2) < 1.e-30) { + if (Math.abs(det2) < 1.e-60) { return null; } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index a630aceb5..7aabebf19 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -471,7 +471,7 @@ else if (mVec.surface.type == Type.PLANEURWELL) { public Matrix filterCovMatURWell(Matrix Ci, double[][] V) { double det = Matrix5x5.inverse(Ci, first_inverse, adj); - if (Math.abs(det) < 1.e-30) { + if (Math.abs(det) < 1.e-60) { return null; } @@ -484,7 +484,7 @@ public Matrix filterCovMatURWell(Matrix Ci, double[][] V) { Matrix5x5.add(first_inverse, addition, result); double det2 = Matrix5x5.inverse(result, result_inv, adj); - if (Math.abs(det2) < 1.e-30) { + if (Math.abs(det2) < 1.e-60) { return null; } @@ -504,7 +504,7 @@ public double[][] KURWell(Matrix C, double[][] V) { public Matrix filterCovMat(double[] H, Matrix Ci, double V) { double det = Matrix5x5.inverse(Ci, first_inverse, adj); - if (Math.abs(det) < 1.e-30) { + if (Math.abs(det) < 1.e-60) { return null; } @@ -517,7 +517,7 @@ public Matrix filterCovMat(double[] H, Matrix Ci, double V) { Matrix5x5.add(first_inverse, addition, result); double det2 = Matrix5x5.inverse(result, result_inv, adj); - if (Math.abs(det2) < 1.e-30) { + if (Math.abs(det2) < 1.e-60) { return null; } From b9b7bfb958f5faa061da862a12455c5bed85c054 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 12 Oct 2023 16:32:45 -0400 Subject: [PATCH 30/69] reset limit since CI test failed --- .../clas/tracking/kalmanfilter/zReference/KFitter.java | 4 ++-- .../kalmanfilter/zReference/KFitterWithURWell.java | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 7e227dff1..94f7f43f1 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -410,7 +410,7 @@ private boolean filter(int k, boolean forward) { public Matrix filterCovMat(double[] H, Matrix Ci, double V) { double det = Matrix5x5.inverse(Ci, first_inverse, adj); - if (Math.abs(det) < 1.e-60) { + if (Math.abs(det) < 1.e-40) { return null; } @@ -423,7 +423,7 @@ public Matrix filterCovMat(double[] H, Matrix Ci, double V) { Matrix5x5.add(first_inverse, addition, result); double det2 = Matrix5x5.inverse(result, result_inv, adj); - if (Math.abs(det2) < 1.e-60) { + if (Math.abs(det2) < 1.e-40) { return null; } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index 7aabebf19..620b21d8a 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -471,7 +471,7 @@ else if (mVec.surface.type == Type.PLANEURWELL) { public Matrix filterCovMatURWell(Matrix Ci, double[][] V) { double det = Matrix5x5.inverse(Ci, first_inverse, adj); - if (Math.abs(det) < 1.e-60) { + if (Math.abs(det) < 1.e-40) { return null; } @@ -484,7 +484,7 @@ public Matrix filterCovMatURWell(Matrix Ci, double[][] V) { Matrix5x5.add(first_inverse, addition, result); double det2 = Matrix5x5.inverse(result, result_inv, adj); - if (Math.abs(det2) < 1.e-60) { + if (Math.abs(det2) < 1.e-40) { return null; } @@ -504,7 +504,7 @@ public double[][] KURWell(Matrix C, double[][] V) { public Matrix filterCovMat(double[] H, Matrix Ci, double V) { double det = Matrix5x5.inverse(Ci, first_inverse, adj); - if (Math.abs(det) < 1.e-60) { + if (Math.abs(det) < 1.e-40) { return null; } @@ -517,7 +517,7 @@ public Matrix filterCovMat(double[] H, Matrix Ci, double V) { Matrix5x5.add(first_inverse, addition, result); double det2 = Matrix5x5.inverse(result, result_inv, adj); - if (Math.abs(det2) < 1.e-60) { + if (Math.abs(det2) < 1.e-40) { return null; } From 50530915bd046e66298dea16c0798cff5f1b38d5 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 12 Oct 2023 16:51:00 -0400 Subject: [PATCH 31/69] modify reconstruction/dc/pom.xml for version of tracking package --- reconstruction/dc/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reconstruction/dc/pom.xml b/reconstruction/dc/pom.xml index 866e9eb58..fb4365de4 100644 --- a/reconstruction/dc/pom.xml +++ b/reconstruction/dc/pom.xml @@ -28,7 +28,7 @@ org.jlab.clas clas-tracking - 10.0.2-SNAPSHOT + 10.0.4-SNAPSHOT From a0d8fb6e61481b79f63d020413dca5606b5044bd Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 12 Oct 2023 17:37:53 -0400 Subject: [PATCH 32/69] Issue for CI test was fixed. Reset limit to proper value. --- .../clas/tracking/kalmanfilter/zReference/KFitter.java | 4 ++-- .../kalmanfilter/zReference/KFitterWithURWell.java | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 94f7f43f1..7e227dff1 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -410,7 +410,7 @@ private boolean filter(int k, boolean forward) { public Matrix filterCovMat(double[] H, Matrix Ci, double V) { double det = Matrix5x5.inverse(Ci, first_inverse, adj); - if (Math.abs(det) < 1.e-40) { + if (Math.abs(det) < 1.e-60) { return null; } @@ -423,7 +423,7 @@ public Matrix filterCovMat(double[] H, Matrix Ci, double V) { Matrix5x5.add(first_inverse, addition, result); double det2 = Matrix5x5.inverse(result, result_inv, adj); - if (Math.abs(det2) < 1.e-40) { + if (Math.abs(det2) < 1.e-60) { return null; } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index 620b21d8a..7aabebf19 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -471,7 +471,7 @@ else if (mVec.surface.type == Type.PLANEURWELL) { public Matrix filterCovMatURWell(Matrix Ci, double[][] V) { double det = Matrix5x5.inverse(Ci, first_inverse, adj); - if (Math.abs(det) < 1.e-40) { + if (Math.abs(det) < 1.e-60) { return null; } @@ -484,7 +484,7 @@ public Matrix filterCovMatURWell(Matrix Ci, double[][] V) { Matrix5x5.add(first_inverse, addition, result); double det2 = Matrix5x5.inverse(result, result_inv, adj); - if (Math.abs(det2) < 1.e-40) { + if (Math.abs(det2) < 1.e-60) { return null; } @@ -504,7 +504,7 @@ public double[][] KURWell(Matrix C, double[][] V) { public Matrix filterCovMat(double[] H, Matrix Ci, double V) { double det = Matrix5x5.inverse(Ci, first_inverse, adj); - if (Math.abs(det) < 1.e-40) { + if (Math.abs(det) < 1.e-60) { return null; } @@ -517,7 +517,7 @@ public Matrix filterCovMat(double[] H, Matrix Ci, double V) { Matrix5x5.add(first_inverse, addition, result); double det2 = Matrix5x5.inverse(result, result_inv, adj); - if (Math.abs(det2) < 1.e-40) { + if (Math.abs(det2) < 1.e-60) { return null; } From 55fd5ddd9dd261e17eebbf30eb3892148a141e25 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 19 Oct 2023 15:31:57 -0400 Subject: [PATCH 33/69] apply DAF into DC-only tracking --- .../tracking/kalmanfilter/AStateVecs.java | 21 + .../kalmanfilter/zReference/DAFilter.java | 216 +++++++++ .../kalmanfilter/zReference/KFitter.java | 454 ++++++++++++++++-- 3 files changed, 653 insertions(+), 38 deletions(-) create mode 100644 common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java index 62eb8b100..19d3987ee 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java @@ -262,6 +262,25 @@ public class StateVec { public double deltaPath; public Matrix CM = new Matrix();; private double _PathLength; + + /////////////////////// For DAF /////////////////////// + private double weightDAF_single = 1; + private double[] weightDAF_double = {0.5, 0.5}; + + public double getWeightDAF_singleHit(){ + return weightDAF_single; + } + public void setWeightDAF_singleHit(double weight){ + this.weightDAF_single = weight; + } + + public double[] getWeightDAF_doubleHits(){ + return weightDAF_double; + } + public void setWeightDAF_doubleHits(double[] weight){ + this.weightDAF_double = weight; + } + public double getPathLength() { return _PathLength; @@ -351,6 +370,8 @@ public final void copy(StateVec s) { this.deltaPath = s.deltaPath; if(s.CM != null) Matrix5x5.copy(s.CM, this.CM); + this.weightDAF_single = s.weightDAF_single; + this.weightDAF_double = s.weightDAF_double; } public void copyCovMat(double[][] c) { diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java new file mode 100644 index 000000000..579aff92d --- /dev/null +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java @@ -0,0 +1,216 @@ +package org.jlab.clas.tracking.kalmanfilter.zReference; +import org.jlab.clas.clas.math.FastMath; +import org.jlab.geom.prim.Point3D; +import org.jlab.geom.prim.Line3D; + +/** + * Calculate effective Doca and variance by DAF + * @author Tongtong + */ +public class DAFilter { + + private static final double dafChi2Cut = 8; + + // For double hits + private double[] docas_double; + private double[] vars_double; + private double[] weights_double; + private Line3D[] wireLines_double; + private Line3D verticalLine; + private double halfWireDistance; + + // For single hit + private double doca_single; + private double var_single; + private double weight_single; + + // Effective doca + private double effectiveDoca; + private double effectiveVar; + private int indexReferenceWire = 0; + + public DAFilter(double[] docas, double[] vars, double[] weights, Line3D[] wireLines) { + this.docas_double = docas; + this.vars_double = vars; + this.weights_double = weights; + this.wireLines_double = wireLines; + this.verticalLine = wireLines[0].distance(wireLines[1]); + this.halfWireDistance = this.verticalLine.length()/2.; + } + + public DAFilter(double doca, double var, double weight) { + this.doca_single = doca; + this.var_single = var; + this.weight_single = weight; + } + + public void calc_effectiveDoca_doubleHits(){ + if((wireLines_double[0] == wireLines_double[1]) || ((docas_double[0] == docas_double[1]) && !(docas_double[0] == 0 && docas_double[1] == 0))) { + //System.out.println((docas[0] != 0) + " " + docas[0]); + effectiveVar = vars_double[0]/weights_double[0]; + effectiveDoca = docas_double[0]; + indexReferenceWire = 0; + return; + } + + // Calculate distance between doca point to middle line with sign + double[] toMids = {0, 0}; + + if(docas_double[0] < 0){ + toMids[0] = Math.abs(docas_double[0]) - halfWireDistance; + } + else if(docas_double[0] > 0){ + toMids[0] = halfWireDistance - docas_double[0]; + } + else{ + if(verticalLine.direction().x() > 0) toMids[0] = -halfWireDistance; + else toMids[0] = halfWireDistance; + } + + if(docas_double[1] < 0){ + toMids[1] = Math.abs(docas_double[1]) - halfWireDistance; + } + else if(docas_double[1] > 0){ + toMids[1] = halfWireDistance - docas_double[1]; + } + else{ + if(verticalLine.direction().x() > 0) toMids[1] = halfWireDistance; + else toMids[1] = -halfWireDistance; + } + + // Calculate weighted averge distance to middle line + double sumWeightedVarRec = 0; + double SumWeightedVarRecToMid = 0; + for(int i = 0; i < 2; i++){ + sumWeightedVarRec += weights_double[i] / vars_double[i]; + SumWeightedVarRecToMid += weights_double[i] / vars_double[i] * toMids[i]; + } + effectiveVar = 1/sumWeightedVarRec; + double effectiveToMid = effectiveVar * SumWeightedVarRecToMid; + + // Calculate effective doca with reference line, which correponding doca has higher weight + double docaLargerWeight = 0; + if(weights_double[0] >= weights_double[1]){ + indexReferenceWire = 0; + docaLargerWeight = docas_double[0]; + } + else{ + indexReferenceWire = 1; + docaLargerWeight = docas_double[1]; + } + + if (docaLargerWeight > 0) { + effectiveDoca = halfWireDistance - effectiveToMid; + } else { + if (effectiveToMid < 0) { + effectiveDoca = Math.abs(effectiveToMid) - halfWireDistance; + } else { + effectiveDoca = -effectiveToMid - halfWireDistance; + } + } + + /* + if((docas_double[0] == 0) || (docas_double[1] == 0)){ + System.out.println("Weights: " + weights_double[0] + " " + weights_double[1]); + System.out.println("docas : " + docas_double[0] + " " + docas_double[1]); + System.out.println("wireLines_double[0]" + wireLines_double[0]); + System.out.println("wireLines_double[1]" + wireLines_double[1]); + System.out.println("verticalLine: " + verticalLine.direction()); + System.out.println("toMids : " + toMids[0] + " " + toMids[1]); + System.out.println("effectiveToMid : " + effectiveToMid); + System.out.println("effectiveDoca : " + effectiveDoca); + System.out.println("halfWireDistance : " + halfWireDistance); + System.out.println("vars : " + vars_double[0] + " " + vars_double[1]); + System.out.println("effectiveVar : " + effectiveVar); + + System.out.println(); + System.out.println(); + + } +*/ + + } + + public void calc_effectiveDoca_singleHit(){ + effectiveVar = var_single/weight_single; + effectiveDoca = doca_single; + + /* + System.out.println("weight: " + weight_single); + System.out.println("var: " + var_single + " " + effectiveVar); + System.out.println("effectiveDoca: " + effectiveDoca); + */ + } + + public double get_EffectiveDoca(){ + return effectiveDoca; + } + + public double get_EffectiveVar(){ + return effectiveVar; + } + + public int get_IndexReferenceWire(){ + return indexReferenceWire; + } + + public double calc_updatedWeight_singleHit(double residual, double annealingFactor){ + double factor = 1/Math.sqrt(2 * Math.PI * annealingFactor * var_single); + + double Chi2 = residual * residual/var_single; + double Phi = factor * Math.exp(-0.5 / annealingFactor * Chi2); + double Lambda = factor * Math.exp(-0.5 / annealingFactor * dafChi2Cut); + double sum = Phi + Lambda; + + double updatedWeight = Phi/sum; + + /* + System.out.println("Residual : " + " " + residual); + System.out.println("variance : " + " " + var_single); + System.out.println("annealingFactor: " + " " + annealingFactor); + System.out.println("Chi2 : " + " " + Chi2); + System.out.println("Phi : " + " " + Phi); + System.out.println("Lambda : " + " " + Lambda); + System.out.println("updatedWeight: " + " " + updatedWeight); + System.out.println(); + System.out.println(); + */ + + return updatedWeight; + } + + public double[] calc_updatedWeights_doubleHits(double[] residuals, double annealingFactor){ + double factor = 1/Math.sqrt(2 * Math.PI * annealingFactor * vars_double[0] * vars_double[1]); + + double[] Chi2 = new double[2]; + double[] Phi = new double[2]; + double[] Lambda = new double[2]; + double sum = 0; + for(int i = 0; i < 2; i++){ + Chi2[i] = residuals[i] * residuals[i]/vars_double[i]; + Phi[i] = factor * Math.exp(-0.5 / annealingFactor * Chi2[i]); + Lambda[i] = factor * Math.exp(-0.5 / annealingFactor * dafChi2Cut); + sum += (Phi[i] + Lambda[i]); + } + + double[] updatedWeights = {0.5, 0.5}; + for(int i = 0; i < 2; i++){ + updatedWeights[i] = Phi[i]/sum; + } + + /* + System.out.println("Residuals : " + " " + residuals[0] + " " + residuals[1]); + System.out.println("variances : " + " " + vars[0] + " " + vars[1]); + System.out.println("annealingFactor: " + " " + annealingFactor); + System.out.println("Chi2 : " + " " + Chi2[0] + " " + Chi2[1]); + System.out.println("Phi : " + " " + Phi[0] + " " + Phi[1]); + System.out.println("Lambda : " + " " + Lambda[0] + " " + Lambda[1]); + System.out.println("updatedWeights: " + " " + updatedWeights[0] + " " + updatedWeights[1]); + System.out.println(); + System.out.println(); + */ + return updatedWeights; + } + +} + diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 7e227dff1..93bc4f07a 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -1,4 +1,4 @@ - package org.jlab.clas.tracking.kalmanfilter.zReference; +package org.jlab.clas.tracking.kalmanfilter.zReference; import java.util.ArrayList; import java.util.List; @@ -22,6 +22,10 @@ * @author Tongtong Cao */ public class KFitter extends AKFitter { + + private static final double[] dafAnnealingFactorsTB={64, 16, 4, 1}; + private int dafAnnealingFactorsIndex = 0; + private double dafAnnealingFactor = 1; private static final double initialCMBlowupFactor = 70; @@ -63,7 +67,6 @@ public KFitter(boolean filter, int iterations, int dir, Swim swim, double Z[], L public final void init(List measSurfaces, StateVec initSV) { finalSmoothedStateVec = null; finalTransportedStateVec = null; - this.NDF0 = -5; this.NDF = -5; this.chi2 = Double.POSITIVE_INFINITY; this.numIter = 0; @@ -71,7 +74,8 @@ public final void init(List measSurfaces, StateVec initSV) { mv.setMeasVecs(measSurfaces); for (int i = 0; i < mv.measurements.size(); i++) { if (mv.measurements.get(i).skip == false) { - this.NDF += mv.measurements.get(i).surface.getNMeas(); + if(TBT) this.NDF++; + else this.NDF += mv.measurements.get(i).surface.getNMeas(); } } @@ -82,7 +86,6 @@ public final void init(List measSurfaces, StateVec initSV) { public final void initFromHB(List measSurfaces, StateVec initSV, double beta) { finalSmoothedStateVec = null; finalTransportedStateVec = null; - this.NDF0 = -5; this.NDF = -5; this.chi2 = Double.POSITIVE_INFINITY; this.numIter = 0; @@ -90,10 +93,10 @@ public final void initFromHB(List measSurfaces, StateVec initSV, double mv.setMeasVecs(measSurfaces); for (int i = 0; i < mv.measurements.size(); i++) { if (mv.measurements.get(i).skip == false) { - this.NDF += mv.measurements.get(i).surface.getNMeas(); + if(TBT) this.NDF++; + else this.NDF += mv.measurements.get(i).surface.getNMeas(); } } - sv.initFromHB(initSV, beta); sv.Z = Z; TBT = true; @@ -102,7 +105,6 @@ public final void initFromHB(List measSurfaces, StateVec initSV, double public void runFitter() { this.chi2 = Double.POSITIVE_INFINITY; double initChi2 = Double.POSITIVE_INFINITY; - // this.NDF = mv.ndf; this.svzLength = this.mv.measurements.size(); int sector = this.mv.measurements.get(0).sector; @@ -113,7 +115,7 @@ public void runFitter() { for (int k = 0; k < svzLength - 1; k++) { sv.transport(sector, k, k + 1, this.sv.trackTrajT.get(k), mv, this.getSwimmer(), true); } - this.calcFinalChisq(sector, true); + this.calcFinalChisqDAF(sector, true); this.initialStateVec = sv.trackTrajT.get(svzLength - 1); this.finalStateVec = sv.trackTrajT.get(svzLength - 1); initChi2 = this.chi2; @@ -128,6 +130,13 @@ public void runFitter() { this.chi2kf = 0; if (i > 1) { + if (dafAnnealingFactorsIndex < dafAnnealingFactorsTB.length) { + dafAnnealingFactor = dafAnnealingFactorsTB[dafAnnealingFactorsIndex]; + dafAnnealingFactorsIndex++; + } else { + dafAnnealingFactor = 1; + dafAnnealingFactorsIndex++; + } for (int k = svzLength - 1; k > 0; k--) { boolean forward = false; @@ -135,6 +144,8 @@ public void runFitter() { // Not backward transport and filter states for the last measurement layer if (k == svzLength - 1) { + this.sv.transported(forward).put(k, this.sv.trackTrajF.get(k)); + this.sv.filtered(forward).put(k, this.sv.trackTrajF.get(k)); if (!sv.transport(sector, k, k - 2, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { this.stopIteration = true; break; @@ -145,30 +156,53 @@ public void runFitter() { break; } } - - if (!this.filter(k - 2, forward)) { - this.stopIteration = true; - break; + if(TBT){ + if (!this.filter(k - 2, forward, dafAnnealingFactor)) { + this.stopIteration = true; + break; + } } - + else{ + if (!this.filter(k - 2, forward)) { + this.stopIteration = true; + break; + } + } + if (!sv.transport(sector, k - 2, k - 1, this.sv.trackTrajB.get(k - 2), mv, this.getSwimmer(), forward)) { this.stopIteration = true; break; } - - if (!this.filter(k - 1, forward)) { - this.stopIteration = true; - break; + + if(TBT){ + if (!this.filter(k - 1, forward, dafAnnealingFactor)) { + this.stopIteration = true; + break; + } + } + else{ + if (!this.filter(k - 1, forward)) { + this.stopIteration = true; + break; + } } } else { if (!sv.transport(sector, 1, 0, this.sv.trackTrajB.get(1), mv, this.getSwimmer(), forward)) { this.stopIteration = true; break; } - - if (!this.filter(0, forward)) { - this.stopIteration = true; - break; + + if(TBT){ + if (!this.filter(0, forward, dafAnnealingFactor)) { + this.stopIteration = true; + break; + } + } + else{ + if (!this.filter(0, forward)) { + this.stopIteration = true; + break; + } } } } @@ -178,6 +212,14 @@ public void runFitter() { break; } + if (dafAnnealingFactorsIndex < dafAnnealingFactorsTB.length) { + dafAnnealingFactor = dafAnnealingFactorsTB[dafAnnealingFactorsIndex]; + dafAnnealingFactorsIndex++; + } else { + dafAnnealingFactor = 1; + dafAnnealingFactorsIndex++; + } + for (int k = 0; k < svzLength - 1; k++) { boolean forward = true; @@ -189,6 +231,7 @@ public void runFitter() { if (k == 0) { if (i == 1) { + sv.filtered(forward).put(0, this.sv.trackTrajT.get(0)); if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajT.get(0), mv, this.getSwimmer(), forward)) { this.stopIteration = true; break; @@ -207,6 +250,9 @@ public void runFitter() { 0, 0, 0, 0, c44*initialCMBlowupFactor); this.sv.trackTrajB.get(0).CM = newCM; + this.sv.transported(forward).put(0, this.sv.trackTrajB.get(0)); + this.sv.filtered(forward).put(0, this.sv.trackTrajB.get(0)); + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajB.get(0), mv, this.getSwimmer(), forward)) { this.stopIteration = true; break; @@ -219,10 +265,17 @@ public void runFitter() { } } - - if (!this.filter(k + 1, forward)) { - this.stopIteration = true; - break; + if(TBT){ + if (!this.filter(k + 1, forward, dafAnnealingFactor)) { + this.stopIteration = true; + break; + } + } + else{ + if (!this.filter(k + 1, forward)) { + this.stopIteration = true; + break; + } } } @@ -269,20 +322,200 @@ public void runFitter() { } } - this.calcFinalChisq(sector); + if(TBT) this.calcFinalChisqDAF(sector); + else this.calcFinalChisq(sector); if (Double.isNaN(chi2)) { this.setFitFailed = true; } - + if (TBT == true) { - if (chi2 > initChi2) { // fit failed + if (chi2 > initChi2) { // fit failed this.finalStateVec = this.initialStateVec; sv.trackTrajT.put(svzLength - 1, this.initialStateVec); - this.calcFinalChisq(sector, true); + this.calcFinalChisqDAF(sector, true); } } + + } + + private boolean filter(int k, boolean forward, double annealingFactor) { + StateVec sVec = sv.transported(forward).get(k); + org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec mVec = mv.measurements.get(k); + if (Double.isNaN(sVec.x) || Double.isNaN(sVec.y) + || Double.isNaN(sVec.tx) || Double.isNaN(sVec.ty) + || Double.isNaN(sVec.Q)) { + this.setFitFailed = true; + return false; + } + if (sVec != null && sVec.CM != null + && k < mv.measurements.size() && mVec.skip == false) { + + double signMeas = 1; + double sign = 1; + + double c2 = 0; + double x_filt = 0; + double y_filt = 0; + double tx_filt = 0; + double ty_filt = 0; + double Q_filt = 0; + Matrix cMat = new Matrix(); + + double updatedWeights_singleHit = 1; + double[] updatedWeights_doubleHits = {0.5, 0.5}; + + if (mVec.surface.doca[1] == -99) { + StateVec sVecPreviousFiltered = sv.filtered(!forward).get(k); + double daf_weight = 1; + if (sVecPreviousFiltered != null) { + daf_weight = sVecPreviousFiltered.getWeightDAF_singleHit(); + } + double var = mVec.surface.unc[0] * KFScale; + DAFilter daf = new DAFilter(mVec.surface.doca[0], var, daf_weight); + daf.calc_effectiveDoca_singleHit(); + + double effectiveDoca = daf.get_EffectiveDoca(); + double effectiveVar = daf.get_EffectiveVar(); + + + double[] K = new double[5]; + double V = effectiveVar; + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[0]); + Matrix CaInv = this.filterCovMat(H, sVec.CM, V); + if (CaInv != null) { + Matrix5x5.copy(CaInv, cMat); + } else { + return false; + } + + for (int j = 0; j < 5; j++) { + // the gain matrix + K[j] = (H[0] * cMat.get(j, 0) + + H[1] * cMat.get(j, 1)) / V; + } + + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + double h = mv.hDoca(point, mVec.surface.wireLine[0]); + + + if (Math.abs(effectiveDoca) < 0.5) { + signMeas = Math.signum(h); + sign = Math.signum(h); + } else { + signMeas = Math.signum(effectiveDoca); + sign = Math.signum(h); + } + + c2 = ((signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)) + * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)) / V); + + x_filt = sVec.x + + K[0] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + y_filt = sVec.y + + K[1] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + tx_filt = sVec.tx + + K[2] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + ty_filt = sVec.ty + + K[3] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + Q_filt = sVec.Q + + K[4] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.z); + double h0 = mv.hDoca(pointFiltered, mVec.surface.wireLine[0]); + if (Math.abs(effectiveDoca) < 0.5) { + signMeas = Math.signum(h0); + sign = Math.signum(h0); + } else { + signMeas = Math.signum(effectiveDoca); + sign = Math.signum(h0); + } + double residual = signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h0); + updatedWeights_singleHit = daf.calc_updatedWeight_singleHit(residual, annealingFactor); + } + else{ + StateVec sVecPreviousFiltered = sv.filtered(!forward).get(k); + double[] daf_weights = {0.5, 0.5}; + if (sVecPreviousFiltered != null) { + daf_weights = sVecPreviousFiltered.getWeightDAF_doubleHits(); + } + double[] vars = {mVec.surface.unc[0] * KFScale, mVec.surface.unc[1] * KFScale}; + DAFilter daf = new DAFilter(mVec.surface.doca, vars, daf_weights, mVec.surface.wireLine); + daf.calc_effectiveDoca_doubleHits(); + + double effectiveDoca = daf.get_EffectiveDoca(); + double effectiveVar = daf.get_EffectiveVar(); + int indexReferenceWire = daf.get_IndexReferenceWire(); + + double[] K = new double[5]; + double V = effectiveVar; + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[indexReferenceWire]); + Matrix CaInv = this.filterCovMat(H, sVec.CM, V); + if (CaInv != null) { + Matrix5x5.copy(CaInv, cMat); + } else { + return false; + } + + for (int j = 0; j < 5; j++) { + // the gain matrix + K[j] = (H[0] * cMat.get(j, 0) + + H[1] * cMat.get(j, 1)) / V; + } + + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + double h = mv.hDoca(point, mVec.surface.wireLine[indexReferenceWire]); + + signMeas = Math.signum(effectiveDoca); + sign = Math.signum(h); + + c2 = ((signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)) + * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)) / V); + + x_filt = sVec.x + + K[0] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + y_filt = sVec.y + + K[1] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + tx_filt = sVec.tx + + K[2] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + ty_filt = sVec.ty + + K[3] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + Q_filt = sVec.Q + + K[4] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.z); + double h0 = mv.hDoca(pointFiltered, mVec.surface.wireLine[0]); + double h1 = mv.hDoca(pointFiltered, mVec.surface.wireLine[1]); + double[] residuals = {mVec.surface.doca[0] - h0, mVec.surface.doca[1] - h1}; + updatedWeights_doubleHits = daf.calc_updatedWeights_doubleHits(residuals, annealingFactor); + } + + chi2kf += c2; + if (filterOn) { + StateVec filteredVec = sv.new StateVec(k); + filteredVec.x = x_filt; + filteredVec.y = y_filt; + filteredVec.tx = tx_filt; + filteredVec.ty = ty_filt; + filteredVec.Q = Q_filt; + filteredVec.z = sVec.z; + filteredVec.B = sVec.B; + filteredVec.deltaPath = sVec.deltaPath; + + filteredVec.CM = cMat; + filteredVec.setWeightDAF_singleHit(updatedWeights_singleHit); + filteredVec.setWeightDAF_doubleHits(updatedWeights_doubleHits); + + sv.filtered(forward).put(k, filteredVec); + } else { + return false; + } + + return true; + } else { + return false; + } } private boolean filter(int k, boolean forward) { @@ -433,7 +666,7 @@ public Matrix filterCovMat(double[] H, Matrix Ci, double V) { private void calcFinalChisq(int sector) { calcFinalChisq(sector, false); } - + private void calcFinalChisq(int sector, boolean nofilter) { int k = svzLength - 1; this.chi2 = 0; @@ -477,9 +710,11 @@ private void calcFinalChisq(int sector, boolean nofilter) { res = (mv.measurements.get(0).surface.doca[1] - h0); chi2 += (mv.measurements.get(0).surface.doca[1] - h0) * (mv.measurements.get(0).surface.doca[1] - h0) / V0; nRj[mv.measurements.get(0).region - 1] += res * res / mv.measurements.get(0).error; - svc.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); - svc.setProjectorDoca(h0); - kfStateVecsAlongTrajectory.add(svc); + + StateVec svc2 = sv.new StateVec(svc); + svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); + svc2.setProjectorDoca(h0); + kfStateVecsAlongTrajectory.add(svc2); } forward = true; @@ -505,21 +740,164 @@ private void calcFinalChisq(int sector, boolean nofilter) { chi2 += (mv.measurements.get(k1 + 1).surface.doca[0] - h) * (mv.measurements.get(k1 + 1).surface.doca[0] - h) / V; nRj[mv.measurements.get(k1 + 1).region - 1] += res * res / V; //USE THE DOUBLE HIT - if (mv.measurements.get(k1 + 1).surface.doca[1] != -99) { - V = mv.measurements.get(k1 + 1).surface.unc[1]; + if (mv.measurements.get(k1 + 1).surface.doca[1] != -99) { + V = mv.measurements.get(k1 + 1).surface.unc[1]; h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[1]); res = (mv.measurements.get(k1 + 1).surface.doca[1] - h); chi2 += (mv.measurements.get(k1 + 1).surface.doca[1] - h) * (mv.measurements.get(k1 + 1).surface.doca[1] - h) / V; nRj[mv.measurements.get(k1 + 1).region - 1] += res * res / V; - svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); - svc.setProjectorDoca(h); - kfStateVecsAlongTrajectory.add(svc); + + StateVec svc2 = sv.new StateVec(svc); + svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); + svc2.setProjectorDoca(h); + kfStateVecsAlongTrajectory.add(svc2); } } } } + private void calcFinalChisqDAF(int sector) { + calcFinalChisqDAF(sector, false); + } + + private void calcFinalChisqDAF(int sector, boolean nofilter) { + int k = svzLength - 1; + this.chi2 = 0; + double path = 0; + + StateVec sVec; + + // To be changed: to match wit the old package, we make the following codes. Could be changed when other codes for application of calcFinalChisq are changed. + if (nofilter || (sv.trackTrajF.get(k) == null)) { + sVec = sv.trackTrajT.get(k); + } else { + sVec = sv.trackTrajF.get(k); + } + + kfStateVecsAlongTrajectory = new ArrayList<>(); + if (sVec != null && sVec.CM != null) { + + boolean forward = false; + sv.transport(sector, k, 0, sVec, mv, this.getSwimmer(), forward); + + StateVec svc = sv.transported(forward).get(0); + path += svc.deltaPath; + svc.setPathLength(path); + + Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); + if(mv.measurements.get(0).surface.doca[1] == -99) { + StateVec sVecPreviousFiltered = sv.filtered(true).get(0); + double daf_weight = 1; + if (sVecPreviousFiltered != null) { + daf_weight = sVecPreviousFiltered.getWeightDAF_singleHit(); + } + double V0 = mv.measurements.get(0).surface.unc[0]; + DAFilter daf = new DAFilter(mv.measurements.get(0).surface.doca[0], V0, daf_weight); + daf.calc_effectiveDoca_singleHit(); + + double effectiveDoca = daf.get_EffectiveDoca(); + double effectiveVar = daf.get_EffectiveVar(); + + double h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); + double res = (effectiveDoca - h); + chi2 += res*res / effectiveVar; + + svc.setProjectorDoca(h); + svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); + kfStateVecsAlongTrajectory.add(svc); + } + else{ + StateVec sVecPreviousFiltered = sv.filtered(true).get(0); + double[] daf_weights = {0.5, 0.5}; + if (sVecPreviousFiltered != null) { + daf_weights = sVecPreviousFiltered.getWeightDAF_doubleHits(); + } + double[] vars = {mv.measurements.get(0).surface.unc[0], mv.measurements.get(0).surface.unc[1]}; + DAFilter daf = new DAFilter(mv.measurements.get(0).surface.doca, vars, daf_weights, mv.measurements.get(0).surface.wireLine); + daf.calc_effectiveDoca_doubleHits(); + + double effectiveDoca = daf.get_EffectiveDoca(); + double effectiveVar = daf.get_EffectiveVar(); + int indexReferenceWire = daf.get_IndexReferenceWire(); + + double h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[indexReferenceWire]); + double res = (effectiveDoca - h); + chi2 += res*res / effectiveVar; + + svc.setProjectorDoca(h); + svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); + kfStateVecsAlongTrajectory.add(svc); + + StateVec svc2 = sv.new StateVec(svc); + svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); + kfStateVecsAlongTrajectory.add(svc2); + } + + forward = true; + for (int k1 = 0; k1 < k; k1++) { + if (k1 == 0) { + sv.transport(sector, k1, k1 + 1, svc, mv, this.getSwimmer(), forward); + } else { + sv.transport(sector, k1, k1 + 1, sv.transported(forward).get(k1), mv, this.getSwimmer(), forward); + } + + svc = sv.transported(forward).get(k1 + 1); + path += svc.deltaPath; + svc.setPathLength(path); + + point = new Point3D(sv.transported(forward).get(k1 + 1).x, sv.transported(forward).get(k1 + 1).y, mv.measurements.get(k1 + 1).surface.z); + if(mv.measurements.get(k1 + 1).surface.doca[1] == -99) { + StateVec sVecPreviousFiltered = sv.filtered(true).get(k1 + 1); + double daf_weight = 1; + if (sVecPreviousFiltered != null) { + daf_weight = sVecPreviousFiltered.getWeightDAF_singleHit(); + } + double V0 = mv.measurements.get(k1 + 1).surface.unc[0]; + DAFilter daf = new DAFilter(mv.measurements.get(k1 + 1).surface.doca[0], V0, daf_weight); + daf.calc_effectiveDoca_singleHit(); + + double effectiveDoca = daf.get_EffectiveDoca(); + double effectiveVar = daf.get_EffectiveVar(); + + double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); + double res = (effectiveDoca - h); + chi2 += res*res / effectiveVar; + + svc.setProjectorDoca(h); + svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); + kfStateVecsAlongTrajectory.add(svc); + } + else{ + StateVec sVecPreviousFiltered = sv.filtered(true).get(k1 + 1); + double[] daf_weights = {0.5, 0.5}; + if (sVecPreviousFiltered != null) { + daf_weights = sVecPreviousFiltered.getWeightDAF_doubleHits(); + } + double[] vars = {mv.measurements.get(k1 + 1).surface.unc[0], mv.measurements.get(k1 + 1).surface.unc[1]}; + DAFilter daf = new DAFilter(mv.measurements.get(k1 + 1).surface.doca, vars, daf_weights, mv.measurements.get(k1 + 1).surface.wireLine); + daf.calc_effectiveDoca_doubleHits(); + + double effectiveDoca = daf.get_EffectiveDoca(); + double effectiveVar = daf.get_EffectiveVar(); + int indexReferenceWire = daf.get_IndexReferenceWire(); + + double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[indexReferenceWire]); + double res = (effectiveDoca - h); + chi2 += res*res / effectiveVar; + + svc.setProjectorDoca(h); + svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); + kfStateVecsAlongTrajectory.add(svc); + + StateVec svc2 = sv.new StateVec(svc); + svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); + kfStateVecsAlongTrajectory.add(svc2); + } + } + } + } + public Matrix propagateToVtx(int sector, double Zf) { return sv.transport(sector, finalStateVec.k, Zf, finalStateVec, mv, this.getSwimmer()); } From ee56b5e98d96655a0f64a5b8db5e5dd23c0f52d0 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Tue, 7 Nov 2023 11:58:02 -0500 Subject: [PATCH 34/69] redefine NDF and remove codes for fail tracking judge --- .../tracking/kalmanfilter/zReference/KFitter.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 93bc4f07a..2812f890a 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -73,9 +73,8 @@ public final void init(List measSurfaces, StateVec initSV) { this.setFitFailed = false; mv.setMeasVecs(measSurfaces); for (int i = 0; i < mv.measurements.size(); i++) { - if (mv.measurements.get(i).skip == false) { - if(TBT) this.NDF++; - else this.NDF += mv.measurements.get(i).surface.getNMeas(); + if (mv.measurements.get(i).skip == false) { + this.NDF += mv.measurements.get(i).surface.getNMeas(); } } @@ -93,8 +92,7 @@ public final void initFromHB(List measSurfaces, StateVec initSV, double mv.setMeasVecs(measSurfaces); for (int i = 0; i < mv.measurements.size(); i++) { if (mv.measurements.get(i).skip == false) { - if(TBT) this.NDF++; - else this.NDF += mv.measurements.get(i).surface.getNMeas(); + this.NDF++; } } sv.initFromHB(initSV, beta); @@ -328,7 +326,8 @@ public void runFitter() { if (Double.isNaN(chi2)) { this.setFitFailed = true; } - + +/* if (TBT == true) { if (chi2 > initChi2) { // fit failed this.finalStateVec = this.initialStateVec; @@ -336,6 +335,7 @@ public void runFitter() { this.calcFinalChisqDAF(sector, true); } } +*/ } From 723bced0bcfe724df9b9071a9b66f1b1f2b0bfd3 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 29 Nov 2023 16:40:24 -0500 Subject: [PATCH 35/69] update projected doca for double hits --- .../clas/tracking/kalmanfilter/zReference/KFitter.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 2812f890a..33ed812cc 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -825,11 +825,14 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { double res = (effectiveDoca - h); chi2 += res*res / effectiveVar; - svc.setProjectorDoca(h); + if(indexReferenceWire == 0) svc.setProjectorDoca(h); + else svc.setProjectorDoca(-999); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); + if(indexReferenceWire == 1) svc2.setProjectorDoca(h); + else svc2.setProjectorDoca(-999); svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); kfStateVecsAlongTrajectory.add(svc2); } @@ -886,11 +889,14 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { double res = (effectiveDoca - h); chi2 += res*res / effectiveVar; - svc.setProjectorDoca(h); + if(indexReferenceWire == 0) svc.setProjectorDoca(h); + else svc.setProjectorDoca(-999); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); + if(indexReferenceWire == 1) svc2.setProjectorDoca(h); + else svc2.setProjectorDoca(-999); svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); kfStateVecsAlongTrajectory.add(svc2); } From 352c964f432b08710f1cd65e732a824047a37aca Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 1 Dec 2023 14:14:57 -0500 Subject: [PATCH 36/69] fix conflict for definition of LR ambiguity between DC hits and measurements as input of KF tracking --- .../java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java | 2 +- .../main/java/org/jlab/rec/dc/track/TrackCandListFinder.java | 2 +- .../org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java | 2 +- .../src/main/java/org/jlab/rec/dc/track/fit/MeasVecsDoca.java | 2 +- .../dc/src/main/java/org/jlab/service/dc/DCTBEngine.java | 2 +- .../dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java index 89c29f831..e53527404 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java @@ -89,7 +89,7 @@ public double hDoca(Point3D point, Line3D wireLine) { WL.copy(wireLine); WL.copy(WL.distance(point)); - return WL.length()*Math.signum(-WL.direction().x()); + return WL.length()*Math.signum(WL.direction().x()); } public double dh(int k, StateVec stateVec) { diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index 437a052f2..df07a9142 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -1205,7 +1205,7 @@ public List getMeasSurfaces(Track trkcand, DCGeant4Factory DcDetector) double LR = Math.signum(trkcand.get(c).get(s).get(h).get_XWire()-trkcand.get(c).get(s).get(h).get_X()); - hot._doca[0]*=LR; + hot._doca[0]*=-LR; hot._hitError = trkcand.get(c).get(s).get(h).get_DocaErr()*trkcand.get(c).get(s).get(h).get_DocaErr(); //LOGGER.log(Level.FINE, " Z "+Z+" ferr "+(float)(hot._Unc /(hot._hitError/4.))); hot._Unc[0] = hot._hitError; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java index 6126ecaef..8ec0f2d33 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -1637,7 +1637,7 @@ public List getMeasSurfaces(Track trkcand, DCGeant4Factory DcDetector) double LR = Math.signum(trkcand.get(c).get(s).get(h).get_XWire()-trkcand.get(c).get(s).get(h).get_X()); - hot._doca[0]*=LR; + hot._doca[0]*=-LR; hot._hitError = trkcand.get(c).get(s).get(h).get_DocaErr()*trkcand.get(c).get(s).get(h).get_DocaErr(); //LOGGER.log(Level.FINE, " Z "+Z+" ferr "+(float)(hot._Unc /(hot._hitError/4.))); hot._Unc[0] = hot._hitError; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/fit/MeasVecsDoca.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/fit/MeasVecsDoca.java index 1e36fdaa3..6dba453a0 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/fit/MeasVecsDoca.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/fit/MeasVecsDoca.java @@ -48,7 +48,7 @@ public double h(double[] stateV, double Z, Line3D wireLine) { //LOGGER.log(Level.FINE, Math.signum(-WL.direction().x())+ // wireLine.origin().toString()+WL.toString()+" "+stateV[0]+" ,"+stateV[1]); - return WL.length()*Math.signum(-WL.direction().x()); + return WL.length()*Math.signum(WL.direction().x()); } public void setMeasVecs(Track trkcand, DCGeant4Factory DcDetector) { diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index 094ec6c05..ba2c5fb98 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -465,7 +465,7 @@ public List getMeasSurfaces(Track trk, DCGeant4Factory DcDetector) { hot._doca[0] = trk.get_ListOfHBSegments().get(s).get(h).get_Doca(); double LR = Math.signum(trk.get_ListOfHBSegments().get(s).get(h).get_XWire()-trk.get_ListOfHBSegments().get(s).get(h).get_X()); - hot._doca[0]*=LR; + hot._doca[0]*=-LR; hot._hitError = trk.get_ListOfHBSegments().get(s).get(h).get_DocaErr()*trk.get_ListOfHBSegments().get(s).get(h).get_DocaErr(); //LOGGER.log(Level.FINE, " Z "+Z+" ferr "+(float)(hot._Unc /(hot._hitError/4.))); hot._Unc[0] = hot._hitError; diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index cec6a4c72..7924b9af9 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -645,7 +645,7 @@ public List getMeasSurfaces(Track trk, DCGeant4Factory DcDetector) { hot._doca[0] = trk.get_ListOfHBSegments().get(s).get(h).get_Doca(); double LR = Math.signum(trk.get_ListOfHBSegments().get(s).get(h).get_XWire()-trk.get_ListOfHBSegments().get(s).get(h).get_X()); - hot._doca[0]*=LR; + hot._doca[0]*=-LR; hot._hitError = trk.get_ListOfHBSegments().get(s).get(h).get_DocaErr()*trk.get_ListOfHBSegments().get(s).get(h).get_DocaErr(); //LOGGER.log(Level.FINE, " Z "+Z+" ferr "+(float)(hot._Unc /(hot._hitError/4.))); hot._Unc[0] = hot._hitError; From f64c764796cefd1ad6d86ccdcef6b3344443e237 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 1 Dec 2023 16:53:42 -0500 Subject: [PATCH 37/69] apply DAF into DC-uRWell tracking --- .../kalmanfilter/zReference/DAFilter.java | 56 +- .../zReference/KFitterWithURWell.java | 499 +++++++++++++++++- 2 files changed, 473 insertions(+), 82 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java index 579aff92d..bf21e8663 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java @@ -46,7 +46,6 @@ public DAFilter(double doca, double var, double weight) { public void calc_effectiveDoca_doubleHits(){ if((wireLines_double[0] == wireLines_double[1]) || ((docas_double[0] == docas_double[1]) && !(docas_double[0] == 0 && docas_double[1] == 0))) { - //System.out.println((docas[0] != 0) + " " + docas[0]); effectiveVar = vars_double[0]/weights_double[0]; effectiveDoca = docas_double[0]; indexReferenceWire = 0; @@ -107,39 +106,12 @@ else if(docas_double[1] > 0){ } else { effectiveDoca = -effectiveToMid - halfWireDistance; } - } - - /* - if((docas_double[0] == 0) || (docas_double[1] == 0)){ - System.out.println("Weights: " + weights_double[0] + " " + weights_double[1]); - System.out.println("docas : " + docas_double[0] + " " + docas_double[1]); - System.out.println("wireLines_double[0]" + wireLines_double[0]); - System.out.println("wireLines_double[1]" + wireLines_double[1]); - System.out.println("verticalLine: " + verticalLine.direction()); - System.out.println("toMids : " + toMids[0] + " " + toMids[1]); - System.out.println("effectiveToMid : " + effectiveToMid); - System.out.println("effectiveDoca : " + effectiveDoca); - System.out.println("halfWireDistance : " + halfWireDistance); - System.out.println("vars : " + vars_double[0] + " " + vars_double[1]); - System.out.println("effectiveVar : " + effectiveVar); - - System.out.println(); - System.out.println(); - - } -*/ - + } } public void calc_effectiveDoca_singleHit(){ effectiveVar = var_single/weight_single; effectiveDoca = doca_single; - - /* - System.out.println("weight: " + weight_single); - System.out.println("var: " + var_single + " " + effectiveVar); - System.out.println("effectiveDoca: " + effectiveDoca); - */ } public double get_EffectiveDoca(){ @@ -161,20 +133,7 @@ public double calc_updatedWeight_singleHit(double residual, double annealingFact double Phi = factor * Math.exp(-0.5 / annealingFactor * Chi2); double Lambda = factor * Math.exp(-0.5 / annealingFactor * dafChi2Cut); double sum = Phi + Lambda; - - double updatedWeight = Phi/sum; - - /* - System.out.println("Residual : " + " " + residual); - System.out.println("variance : " + " " + var_single); - System.out.println("annealingFactor: " + " " + annealingFactor); - System.out.println("Chi2 : " + " " + Chi2); - System.out.println("Phi : " + " " + Phi); - System.out.println("Lambda : " + " " + Lambda); - System.out.println("updatedWeight: " + " " + updatedWeight); - System.out.println(); - System.out.println(); - */ + double updatedWeight = Phi/sum; return updatedWeight; } @@ -198,17 +157,6 @@ public double[] calc_updatedWeights_doubleHits(double[] residuals, double anneal updatedWeights[i] = Phi[i]/sum; } - /* - System.out.println("Residuals : " + " " + residuals[0] + " " + residuals[1]); - System.out.println("variances : " + " " + vars[0] + " " + vars[1]); - System.out.println("annealingFactor: " + " " + annealingFactor); - System.out.println("Chi2 : " + " " + Chi2[0] + " " + Chi2[1]); - System.out.println("Phi : " + " " + Phi[0] + " " + Phi[1]); - System.out.println("Lambda : " + " " + Lambda[0] + " " + Lambda[1]); - System.out.println("updatedWeights: " + " " + updatedWeights[0] + " " + updatedWeights[1]); - System.out.println(); - System.out.println(); - */ return updatedWeights; } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index 7aabebf19..88153aba0 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -26,6 +26,10 @@ */ public class KFitterWithURWell extends AKFitter { + private static final double[] dafAnnealingFactorsTB={64, 16, 4, 1}; + private int dafAnnealingFactorsIndex = 0; + private double dafAnnealingFactor = 1; + private static final Logger LOGGER = Logger.getLogger(KFitter.class.getName()); private static final double initialCMBlowupFactor = 70; @@ -68,7 +72,6 @@ public KFitterWithURWell(boolean filter, int iterations, int dir, Swim swim, dou public final void init(List measSurfaces, StateVec initSV) { finalSmoothedStateVec = null; finalTransportedStateVec = null; - this.NDF0 = -5; this.NDF = -5; this.chi2 = Double.POSITIVE_INFINITY; this.numIter = 0; @@ -90,7 +93,6 @@ else if(mv.measurements.get(i).surface.type == Type.PLANEURWELL) public final void initFromHB(List measSurfaces, StateVec initSV, double beta) { finalSmoothedStateVec = null; finalTransportedStateVec = null; - this.NDF0 = -5; this.NDF = -5; this.chi2 = Double.POSITIVE_INFINITY; this.numIter = 0; @@ -99,7 +101,7 @@ public final void initFromHB(List measSurfaces, StateVec initSV, double for (int i = 0; i < mv.measurements.size(); i++) { if (mv.measurements.get(i).skip == false) { if(mv.measurements.get(i).surface.type == Type.LINEDOCA) - this.NDF += mv.measurements.get(i).surface.getNMeas(); + this.NDF++; else if(mv.measurements.get(i).surface.type == Type.PLANEURWELL) this.NDF += 2; } @@ -124,7 +126,7 @@ public void runFitter() { for (int k = 0; k < svzLength - 1; k++) { sv.transport(sector, k, k + 1, this.sv.trackTrajT.get(k), mv, this.getSwimmer(), true); } - this.calcFinalChisq(sector, true); + this.calcFinalChisqDAF(sector, true); this.initialStateVec = sv.trackTrajT.get(svzLength - 1); this.finalStateVec = sv.trackTrajT.get(svzLength - 1); initChi2 = this.chi2; @@ -139,13 +141,22 @@ public void runFitter() { this.chi2kf = 0; if (i > 1) { - + if (dafAnnealingFactorsIndex < dafAnnealingFactorsTB.length) { + dafAnnealingFactor = dafAnnealingFactorsTB[dafAnnealingFactorsIndex]; + dafAnnealingFactorsIndex++; + } else { + dafAnnealingFactor = 1; + dafAnnealingFactorsIndex++; + } + for (int k = svzLength - 1; k > 0; k--) { boolean forward = false; if (k >= 2) { // Not backward transport and filter states for the last measurement layer if (k == svzLength - 1) { + this.sv.transported(forward).put(k, this.sv.trackTrajF.get(k)); + this.sv.filtered(forward).put(k, this.sv.trackTrajF.get(k)); if (!sv.transport(sector, k, k - 2, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { this.stopIteration = true; break; @@ -157,9 +168,17 @@ public void runFitter() { } } - if (!this.filter(k - 2, forward)) { - this.stopIteration = true; - break; + if(TBT){ + if (!this.filter(k - 2, forward, dafAnnealingFactor)) { + this.stopIteration = true; + break; + } + } + else{ + if (!this.filter(k - 2, forward)) { + this.stopIteration = true; + break; + } } if (!sv.transport(sector, k - 2, k - 1, this.sv.trackTrajB.get(k - 2), mv, this.getSwimmer(), forward)) { @@ -167,9 +186,17 @@ public void runFitter() { break; } - if (!this.filter(k - 1, forward)) { - this.stopIteration = true; - break; + if(TBT){ + if (!this.filter(k - 1, forward, dafAnnealingFactor)) { + this.stopIteration = true; + break; + } + } + else{ + if (!this.filter(k - 1, forward)) { + this.stopIteration = true; + break; + } } } else { if (!sv.transport(sector, 1, 0, this.sv.trackTrajB.get(1), mv, this.getSwimmer(), forward)) { @@ -177,9 +204,17 @@ public void runFitter() { break; } - if (!this.filter(0, forward)) { - this.stopIteration = true; - break; + if(TBT){ + if (!this.filter(0, forward, dafAnnealingFactor)) { + this.stopIteration = true; + break; + } + } + else{ + if (!this.filter(0, forward)) { + this.stopIteration = true; + break; + } } } } @@ -188,6 +223,14 @@ public void runFitter() { if (this.stopIteration) { break; } + + if (dafAnnealingFactorsIndex < dafAnnealingFactorsTB.length) { + dafAnnealingFactor = dafAnnealingFactorsTB[dafAnnealingFactorsIndex]; + dafAnnealingFactorsIndex++; + } else { + dafAnnealingFactor = 1; + dafAnnealingFactorsIndex++; + } for (int k = 0; k < svzLength - 1; k++) { boolean forward = true; @@ -200,6 +243,7 @@ public void runFitter() { if (k == 0) { if (i == 1) { + sv.filtered(forward).put(0, this.sv.trackTrajT.get(0)); if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajT.get(0), mv, this.getSwimmer(), forward)) { this.stopIteration = true; break; @@ -218,6 +262,9 @@ public void runFitter() { 0, 0, 0, 0, c44*initialCMBlowupFactor); this.sv.trackTrajB.get(0).CM = newCM; + this.sv.transported(forward).put(0, this.sv.trackTrajB.get(0)); + this.sv.filtered(forward).put(0, this.sv.trackTrajB.get(0)); + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajB.get(0), mv, this.getSwimmer(), forward)) { this.stopIteration = true; break; @@ -230,10 +277,17 @@ public void runFitter() { } } - - if (!this.filter(k + 1, forward)) { - this.stopIteration = true; - break; + if(TBT){ + if (!this.filter(k + 1, forward, dafAnnealingFactor)) { + this.stopIteration = true; + break; + } + } + else{ + if (!this.filter(k + 1, forward)) { + this.stopIteration = true; + break; + } } } @@ -280,21 +334,253 @@ public void runFitter() { } } - this.calcFinalChisq(sector); + if(TBT) this.calcFinalChisqDAF(sector); + else this.calcFinalChisq(sector); if (Double.isNaN(chi2)) { this.setFitFailed = true; } +/* if (TBT == true) { - if (chi2 > initChi2) { // fit failed + if (chi2 > initChi2) { // fit failed this.finalStateVec = this.initialStateVec; sv.trackTrajT.put(svzLength - 1, this.initialStateVec); - this.calcFinalChisq(sector, true); + this.calcFinalChisqDAF(sector, true); } } +*/ } + + private boolean filter(int k, boolean forward, double annealingFactor) { + StateVec sVec = sv.transported(forward).get(k); + org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec mVec = mv.measurements.get(k); + + if (Double.isNaN(sVec.x) || Double.isNaN(sVec.y) + || Double.isNaN(sVec.tx) || Double.isNaN(sVec.ty) + || Double.isNaN(sVec.Q)) { + this.setFitFailed = true; + return false; + } + if (sVec != null && sVec.CM != null + && k < mv.measurements.size() && mVec.skip == false) { + + if (mVec.surface.type == Type.LINEDOCA) { + + double signMeas = 1; + double sign = 1; + + double c2 = 0; + double x_filt = 0; + double y_filt = 0; + double tx_filt = 0; + double ty_filt = 0; + double Q_filt = 0; + Matrix cMat = new Matrix(); + + double updatedWeights_singleHit = 1; + double[] updatedWeights_doubleHits = {0.5, 0.5}; + + if (mVec.surface.doca[1] == -99) { + StateVec sVecPreviousFiltered = sv.filtered(!forward).get(k); + double daf_weight = 1; + if (sVecPreviousFiltered != null) { + daf_weight = sVecPreviousFiltered.getWeightDAF_singleHit(); + } + double var = mVec.surface.unc[0] * KFScale; + DAFilter daf = new DAFilter(mVec.surface.doca[0], var, daf_weight); + daf.calc_effectiveDoca_singleHit(); + + double effectiveDoca = daf.get_EffectiveDoca(); + double effectiveVar = daf.get_EffectiveVar(); + + + double[] K = new double[5]; + double V = effectiveVar; + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[0]); + Matrix CaInv = this.filterCovMat(H, sVec.CM, V); + if (CaInv != null) { + Matrix5x5.copy(CaInv, cMat); + } else { + return false; + } + + for (int j = 0; j < 5; j++) { + // the gain matrix + K[j] = (H[0] * cMat.get(j, 0) + + H[1] * cMat.get(j, 1)) / V; + } + + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + double h = mv.hDoca(point, mVec.surface.wireLine[0]); + + + if (Math.abs(effectiveDoca) < 0.5) { + signMeas = Math.signum(h); + sign = Math.signum(h); + } else { + signMeas = Math.signum(effectiveDoca); + sign = Math.signum(h); + } + + c2 = ((signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)) + * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)) / V); + + x_filt = sVec.x + + K[0] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + y_filt = sVec.y + + K[1] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + tx_filt = sVec.tx + + K[2] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + ty_filt = sVec.ty + + K[3] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + Q_filt = sVec.Q + + K[4] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.z); + double h0 = mv.hDoca(pointFiltered, mVec.surface.wireLine[0]); + if (Math.abs(effectiveDoca) < 0.5) { + signMeas = Math.signum(h0); + sign = Math.signum(h0); + } else { + signMeas = Math.signum(effectiveDoca); + sign = Math.signum(h0); + } + double residual = signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h0); + updatedWeights_singleHit = daf.calc_updatedWeight_singleHit(residual, annealingFactor); + } + else{ + StateVec sVecPreviousFiltered = sv.filtered(!forward).get(k); + double[] daf_weights = {0.5, 0.5}; + if (sVecPreviousFiltered != null) { + daf_weights = sVecPreviousFiltered.getWeightDAF_doubleHits(); + } + double[] vars = {mVec.surface.unc[0] * KFScale, mVec.surface.unc[1] * KFScale}; + DAFilter daf = new DAFilter(mVec.surface.doca, vars, daf_weights, mVec.surface.wireLine); + daf.calc_effectiveDoca_doubleHits(); + + double effectiveDoca = daf.get_EffectiveDoca(); + double effectiveVar = daf.get_EffectiveVar(); + int indexReferenceWire = daf.get_IndexReferenceWire(); + + double[] K = new double[5]; + double V = effectiveVar; + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[indexReferenceWire]); + Matrix CaInv = this.filterCovMat(H, sVec.CM, V); + if (CaInv != null) { + Matrix5x5.copy(CaInv, cMat); + } else { + return false; + } + + for (int j = 0; j < 5; j++) { + // the gain matrix + K[j] = (H[0] * cMat.get(j, 0) + + H[1] * cMat.get(j, 1)) / V; + } + + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + double h = mv.hDoca(point, mVec.surface.wireLine[indexReferenceWire]); + + signMeas = Math.signum(effectiveDoca); + sign = Math.signum(h); + + c2 = ((signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)) + * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)) / V); + + x_filt = sVec.x + + K[0] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + y_filt = sVec.y + + K[1] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + tx_filt = sVec.tx + + K[2] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + ty_filt = sVec.ty + + K[3] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + Q_filt = sVec.Q + + K[4] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.z); + double h0 = mv.hDoca(pointFiltered, mVec.surface.wireLine[0]); + double h1 = mv.hDoca(pointFiltered, mVec.surface.wireLine[1]); + double[] residuals = {mVec.surface.doca[0] - h0, mVec.surface.doca[1] - h1}; + updatedWeights_doubleHits = daf.calc_updatedWeights_doubleHits(residuals, annealingFactor); + } + + chi2kf += c2; + if (filterOn) { + StateVec filteredVec = sv.new StateVec(k); + filteredVec.x = x_filt; + filteredVec.y = y_filt; + filteredVec.tx = tx_filt; + filteredVec.ty = ty_filt; + filteredVec.Q = Q_filt; + filteredVec.z = sVec.z; + filteredVec.B = sVec.B; + filteredVec.deltaPath = sVec.deltaPath; + + filteredVec.CM = cMat; + filteredVec.setWeightDAF_singleHit(updatedWeights_singleHit); + filteredVec.setWeightDAF_doubleHits(updatedWeights_doubleHits); + + sv.filtered(forward).put(k, filteredVec); + } else { + return false; + } + } + + else if (mVec.surface.type == Type.PLANEURWELL) { + + + double V[][] = {{mVec.surface.x_err * mVec.surface.x_err, 0}, {0, mVec.surface.y_err * mVec.surface.y_err}}; + + if(V[0][0] == 0 || V[1][1] == 0 ) + LOGGER.log(Level.SEVERE, "Resolution for URWell is 0."); + + Matrix cMat = this.filterCovMatURWell(sVec.CM, V); + if (cMat == null) return false; + + double[][] K = KURWell(cMat, V); + double [] res = {mVec.surface.x - sVec.x, mVec.surface.y - sVec.y}; + + double filt[] = new double[5]; + for(int i = 0; i < 5; i ++){ + for(int j = 0; j < 2; j++){ + filt[i] += K[i][j]*res[j]; + } + } + + double c2 = res[0]*res[0]/V[0][0] + res[1]/V[1][1]; + + //System.out.println("mVec.surface.x: " + mVec.surface.x + "; sVec.x: " + sVec.x + " " + "mVec.surface.y: " + mVec.surface.y + "; sVec.y: " + sVec.y + "res[0]: " + res[0] + "; res[1]: " + res[1]); + + chi2kf += c2; + if (filterOn) { + StateVec filteredVec = sv.new StateVec(k); + filteredVec.x = sVec.x + filt[0]; + filteredVec.y = sVec.y + filt[1]; + filteredVec.tx = sVec.tx + filt[2]; + filteredVec.ty = sVec.ty + filt[3]; + filteredVec.Q = sVec.Q +filt[4]; + filteredVec.z = sVec.z; + filteredVec.B = sVec.B; + filteredVec.deltaPath = sVec.deltaPath; + + filteredVec.CM = cMat; + + sv.filtered(forward).put(k, filteredVec); + //sv.filtered(forward).put(k, sVec); + + } else { + return false; + } + } + + return true; + } else { + return false; + } + } private boolean filter(int k, boolean forward) { StateVec sVec = sv.transported(forward).get(k); @@ -558,9 +844,7 @@ private void calcFinalChisq(int sector, boolean nofilter) { double y_err = mv.measurements.get(0).surface.y_err; double x_res = mv.measurements.get(0).surface.x - svc.x; - double y_res = mv.measurements.get(0).surface.y - svc.y; - - //System.out.println(x_res + " " + y_res + " " + x_err + " " + y_err); + double y_res = mv.measurements.get(0).surface.y - svc.y; chi2 += (x_res*x_res) / (x_err*x_err); chi2 += (y_res*y_res) / (y_err*y_err); @@ -597,14 +881,173 @@ private void calcFinalChisq(int sector, boolean nofilter) { res = (mv.measurements.get(k1 + 1).surface.doca[1] - h); chi2 += (mv.measurements.get(k1 + 1).surface.doca[1] - h) * (mv.measurements.get(k1 + 1).surface.doca[1] - h) / V; nRj[mv.measurements.get(k1 + 1).region] += res * res / V; - svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); - svc.setProjectorDoca(h); - kfStateVecsAlongTrajectory.add(svc); + + StateVec svc2 = sv.new StateVec(svc); + svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); + svc2.setProjectorDoca(h); + kfStateVecsAlongTrajectory.add(svc2); } } } - //System.out.println("this.chi2:" + this.chi2); + } + + private void calcFinalChisqDAF(int sector) { + calcFinalChisqDAF(sector, false); + } + + private void calcFinalChisqDAF(int sector, boolean nofilter) { + int k = svzLength - 1; + this.chi2 = 0; + double path = 0; + + StateVec sVec; + + // To be changed: to match wit the old package, we make the following codes. Could be changed when other codes for application of calcFinalChisq are changed. + if (nofilter || (sv.trackTrajF.get(k) == null)) { + sVec = sv.trackTrajT.get(k); + } else { + sVec = sv.trackTrajF.get(k); + } + + kfStateVecsAlongTrajectory = new ArrayList<>(); + if (sVec != null && sVec.CM != null) { + + boolean forward = false; + sv.transport(sector, k, 0, sVec, mv, this.getSwimmer(), forward); + + StateVec svc = sv.transported(forward).get(0); + path += svc.deltaPath; + svc.setPathLength(path); + + if(mv.measurements.get(0).surface.type == Type.PLANEURWELL){ + double x_err = mv.measurements.get(0).surface.x_err; + double y_err = mv.measurements.get(0).surface.y_err; + + double x_res = mv.measurements.get(0).surface.x - svc.x; + double y_res = mv.measurements.get(0).surface.y - svc.y; + + chi2 += (x_res*x_res) / (x_err*x_err); + chi2 += (y_res*y_res) / (y_err*y_err); + } + else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ + Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); + if(mv.measurements.get(0).surface.doca[1] == -99) { + StateVec sVecPreviousFiltered = sv.filtered(true).get(0); + double daf_weight = 1; + if (sVecPreviousFiltered != null) { + daf_weight = sVecPreviousFiltered.getWeightDAF_singleHit(); + } + double V0 = mv.measurements.get(0).surface.unc[0]; + DAFilter daf = new DAFilter(mv.measurements.get(0).surface.doca[0], V0, daf_weight); + daf.calc_effectiveDoca_singleHit(); + + double effectiveDoca = daf.get_EffectiveDoca(); + double effectiveVar = daf.get_EffectiveVar(); + + double h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); + double res = (effectiveDoca - h); + chi2 += res*res / effectiveVar; + + svc.setProjectorDoca(h); + svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); + kfStateVecsAlongTrajectory.add(svc); + } + else{ + StateVec sVecPreviousFiltered = sv.filtered(true).get(0); + double[] daf_weights = {0.5, 0.5}; + if (sVecPreviousFiltered != null) { + daf_weights = sVecPreviousFiltered.getWeightDAF_doubleHits(); + } + double[] vars = {mv.measurements.get(0).surface.unc[0], mv.measurements.get(0).surface.unc[1]}; + DAFilter daf = new DAFilter(mv.measurements.get(0).surface.doca, vars, daf_weights, mv.measurements.get(0).surface.wireLine); + daf.calc_effectiveDoca_doubleHits(); + double effectiveDoca = daf.get_EffectiveDoca(); + double effectiveVar = daf.get_EffectiveVar(); + int indexReferenceWire = daf.get_IndexReferenceWire(); + + double h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[indexReferenceWire]); + double res = (effectiveDoca - h); + chi2 += res*res / effectiveVar; + + if(indexReferenceWire == 0) svc.setProjectorDoca(h); + else svc.setProjectorDoca(-999); + svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); + kfStateVecsAlongTrajectory.add(svc); + + StateVec svc2 = sv.new StateVec(svc); + if(indexReferenceWire == 1) svc2.setProjectorDoca(h); + else svc2.setProjectorDoca(-999); + svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); + kfStateVecsAlongTrajectory.add(svc2); + } + } + + forward = true; + for (int k1 = 0; k1 < k; k1++) { + if (k1 == 0) { + sv.transport(sector, k1, k1 + 1, svc, mv, this.getSwimmer(), forward); + } else { + sv.transport(sector, k1, k1 + 1, sv.transported(forward).get(k1), mv, this.getSwimmer(), forward); + } + + svc = sv.transported(forward).get(k1 + 1); + path += svc.deltaPath; + svc.setPathLength(path); + + Point3D point = new Point3D(sv.transported(forward).get(k1 + 1).x, sv.transported(forward).get(k1 + 1).y, mv.measurements.get(k1 + 1).surface.z); + if(mv.measurements.get(k1 + 1).surface.doca[1] == -99) { + StateVec sVecPreviousFiltered = sv.filtered(true).get(k1 + 1); + double daf_weight = 1; + if (sVecPreviousFiltered != null) { + daf_weight = sVecPreviousFiltered.getWeightDAF_singleHit(); + } + double V0 = mv.measurements.get(k1 + 1).surface.unc[0]; + DAFilter daf = new DAFilter(mv.measurements.get(k1 + 1).surface.doca[0], V0, daf_weight); + daf.calc_effectiveDoca_singleHit(); + + double effectiveDoca = daf.get_EffectiveDoca(); + double effectiveVar = daf.get_EffectiveVar(); + + double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); + double res = (effectiveDoca - h); + chi2 += res*res / effectiveVar; + + svc.setProjectorDoca(h); + svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); + kfStateVecsAlongTrajectory.add(svc); + } + else{ + StateVec sVecPreviousFiltered = sv.filtered(true).get(k1 + 1); + double[] daf_weights = {0.5, 0.5}; + if (sVecPreviousFiltered != null) { + daf_weights = sVecPreviousFiltered.getWeightDAF_doubleHits(); + } + double[] vars = {mv.measurements.get(k1 + 1).surface.unc[0], mv.measurements.get(k1 + 1).surface.unc[1]}; + DAFilter daf = new DAFilter(mv.measurements.get(k1 + 1).surface.doca, vars, daf_weights, mv.measurements.get(k1 + 1).surface.wireLine); + daf.calc_effectiveDoca_doubleHits(); + + double effectiveDoca = daf.get_EffectiveDoca(); + double effectiveVar = daf.get_EffectiveVar(); + int indexReferenceWire = daf.get_IndexReferenceWire(); + + double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[indexReferenceWire]); + double res = (effectiveDoca - h); + chi2 += res*res / effectiveVar; + + if(indexReferenceWire == 0) svc.setProjectorDoca(h); + else svc.setProjectorDoca(-999); + svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); + kfStateVecsAlongTrajectory.add(svc); + + StateVec svc2 = sv.new StateVec(svc); + if(indexReferenceWire == 1) svc2.setProjectorDoca(h); + else svc2.setProjectorDoca(-999); + svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); + kfStateVecsAlongTrajectory.add(svc2); + } + } + } } public Matrix propagateToVtx(int sector, double Zf) { From a45a392df2b0b9fe92044bf646d303b24f6ac40f Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 1 Dec 2023 17:07:27 -0500 Subject: [PATCH 38/69] update calcFinalChisq() in org.jlab.clas.tracking.kalmanfilter.zReference.KFitterWithURWell --- .../zReference/KFitterWithURWell.java | 54 +++++++++++++------ 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index 7aabebf19..b76424ee9 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -548,25 +548,49 @@ private void calcFinalChisq(int sector, boolean nofilter) { boolean forward = false; sv.transport(sector, k, 0, sVec, mv, this.getSwimmer(), forward); - StateVec svc = sv.transported(forward).get(0); path += svc.deltaPath; svc.setPathLength(path); - kfStateVecsAlongTrajectory.add(svc); + + if (mv.measurements.get(0).surface.type == Type.PLANEURWELL) { + kfStateVecsAlongTrajectory.add(svc); + + double x_err = mv.measurements.get(0).surface.x_err; + double y_err = mv.measurements.get(0).surface.y_err; - double x_err = mv.measurements.get(0).surface.x_err; - double y_err = mv.measurements.get(0).surface.y_err; + double x_res = mv.measurements.get(0).surface.x - svc.x; + double y_res = mv.measurements.get(0).surface.y - svc.y; - double x_res = mv.measurements.get(0).surface.x - svc.x; - double y_res = mv.measurements.get(0).surface.y - svc.y; - - //System.out.println(x_res + " " + y_res + " " + x_err + " " + y_err); - - chi2 += (x_res*x_res) / (x_err*x_err); - chi2 += (y_res*y_res) / (y_err*y_err); - - nRj[mv.measurements.get(0).region] += x_res * x_res / (x_err*x_err); - nRj[mv.measurements.get(0).region] += y_res * y_res / (y_err*y_err); + chi2 += (x_res*x_res) / (x_err*x_err); + chi2 += (y_res*y_res) / (y_err*y_err); + + nRj[mv.measurements.get(0).region] += x_res * x_res / (x_err*x_err); + nRj[mv.measurements.get(0).region] += y_res * y_res / (y_err*y_err); + } + else if(mv.measurements.get(0).surface.type == Type.LINEDOCA) { + double V0 = mv.measurements.get(0).surface.unc[0]; + + Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); + double h0 = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); + + svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); + svc.setProjectorDoca(h0); + kfStateVecsAlongTrajectory.add(svc); + double res = (mv.measurements.get(0).surface.doca[0] - h0); + chi2 += (mv.measurements.get(0).surface.doca[0] - h0) * (mv.measurements.get(0).surface.doca[0] - h0) / V0; + nRj[mv.measurements.get(0).region - 1] += res * res / mv.measurements.get(0).error; + //USE THE DOUBLE HIT + if (mv.measurements.get(0).surface.doca[1] != -99) { + V0 = mv.measurements.get(0).surface.unc[1]; + h0 = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[1]); + res = (mv.measurements.get(0).surface.doca[1] - h0); + chi2 += (mv.measurements.get(0).surface.doca[1] - h0) * (mv.measurements.get(0).surface.doca[1] - h0) / V0; + nRj[mv.measurements.get(0).region - 1] += res * res / mv.measurements.get(0).error; + svc.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); + svc.setProjectorDoca(h0); + kfStateVecsAlongTrajectory.add(svc); + } + } forward = true; for (int k1 = 0; k1 < k; k1++) { @@ -603,8 +627,6 @@ private void calcFinalChisq(int sector, boolean nofilter) { } } } - //System.out.println("this.chi2:" + this.chi2); - } public Matrix propagateToVtx(int sector, double Zf) { From 25f77c3284f0792d1c1369c618dc2c7ad0a234f7 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Tue, 5 Dec 2023 10:42:33 -0500 Subject: [PATCH 39/69] modify fitResidual in TB hit banks --- .../dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java index eb5c89fa5..bf56a5bcb 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java @@ -1043,7 +1043,7 @@ public void updateHitfromSV(StateVec st, DCGeant4Factory DcDetector) { // this.set_AssociatedHBTrackID(trk.get_Id()); // this.set_AssociatedClusterID(this.get_AssociatedClusterID()); this.setAssociatedStateVec(st); - this.set_TrkResid(this.get_Doca() * Math.signum(st.getProjectorDoca()) - st.getProjectorDoca()); + this.set_TrkResid(this.get_Doca() * this.get_LeftRightAmb() - st.getProjectorDoca()); this.setB(st.getB()); this.setSignalPropagTimeAlongWire(st.x(), st.y(), DcDetector); this.setSignalTimeOfFlight(); From 4651800e60c6de5b9453412f9fd77bb098b505c4 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Tue, 5 Dec 2023 17:02:45 -0500 Subject: [PATCH 40/69] update trajectory projected doca --- .../kalmanfilter/zReference/KFitter.java | 16 ++++++++-------- .../zReference/KFitterWithURWell.java | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 33ed812cc..aa2aae78a 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -825,14 +825,14 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { double res = (effectiveDoca - h); chi2 += res*res / effectiveVar; - if(indexReferenceWire == 0) svc.setProjectorDoca(h); - else svc.setProjectorDoca(-999); + h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); + svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); - if(indexReferenceWire == 1) svc2.setProjectorDoca(h); - else svc2.setProjectorDoca(-999); + h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[1]); + svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); kfStateVecsAlongTrajectory.add(svc2); } @@ -889,14 +889,14 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { double res = (effectiveDoca - h); chi2 += res*res / effectiveVar; - if(indexReferenceWire == 0) svc.setProjectorDoca(h); - else svc.setProjectorDoca(-999); + h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); + svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); - if(indexReferenceWire == 1) svc2.setProjectorDoca(h); - else svc2.setProjectorDoca(-999); + h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[1]); + svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); kfStateVecsAlongTrajectory.add(svc2); } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index 0022b49f3..840482f9b 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -998,14 +998,14 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ double res = (effectiveDoca - h); chi2 += res*res / effectiveVar; - if(indexReferenceWire == 0) svc.setProjectorDoca(h); - else svc.setProjectorDoca(-999); + h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); + svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); - if(indexReferenceWire == 1) svc2.setProjectorDoca(h); - else svc2.setProjectorDoca(-999); + h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[1]); + svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); kfStateVecsAlongTrajectory.add(svc2); } @@ -1063,14 +1063,14 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ double res = (effectiveDoca - h); chi2 += res*res / effectiveVar; - if(indexReferenceWire == 0) svc.setProjectorDoca(h); - else svc.setProjectorDoca(-999); + h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); + svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); - if(indexReferenceWire == 1) svc2.setProjectorDoca(h); - else svc2.setProjectorDoca(-999); + h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[1]); + svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); kfStateVecsAlongTrajectory.add(svc2); } From 93044b15d940ba4c40f3f48f0b1743def900666e Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 8 Dec 2023 00:57:57 -0500 Subject: [PATCH 41/69] add KF package for straight tracking --- .../zReference/KFitterStraight.java | 561 ++++++++++++++++++ .../rec/dc/track/TrackCandListFinder.java | 20 +- .../java/org/jlab/service/dc/DCTBEngine.java | 169 ++++-- 3 files changed, 712 insertions(+), 38 deletions(-) create mode 100644 common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterStraight.java diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterStraight.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterStraight.java new file mode 100644 index 000000000..5f47479ca --- /dev/null +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterStraight.java @@ -0,0 +1,561 @@ +package org.jlab.clas.tracking.kalmanfilter.zReference; + +import java.util.ArrayList; +import java.util.List; + +import org.jlab.clas.clas.math.FastMath; +import org.jlab.clas.swimtools.Swim; +import org.jlab.clas.tracking.kalmanfilter.AKFitter; +import org.jlab.clas.tracking.kalmanfilter.AMeasVecs; +import org.jlab.clas.tracking.kalmanfilter.AStateVecs; +import org.jlab.clas.tracking.kalmanfilter.Surface; +import org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec; +import org.jlab.clas.tracking.kalmanfilter.zReference.MeasVecs; +import org.jlab.clas.tracking.kalmanfilter.zReference.StateVecs; +import org.jlab.clas.tracking.utilities.RungeKuttaDoca; +import org.jlab.clas.tracking.utilities.MatrixOps.Libr; +import org.jlab.geom.prim.Point3D; +import org.jlab.jnp.matrix.*; + +/** + * + * @author Tongtong Cao + */ +public class KFitterStraight extends AKFitter { + + private StateVecs sv = new StateVecs(); + private MeasVecs mv = new MeasVecs(); + private StateVec finalSmoothedStateVec = null; + private StateVec finalTransportedStateVec = null; + + public StateVec finalStateVec = null; + public StateVec initialStateVec = null; + public List kfStateVecsAlongTrajectory; + + private int interNum; + + private double chi2kf = 0; + private double KFScale = 4; + + private int svzLength; + + public int ConvStatus = 1; + + private double Z[]; + + private boolean stopIteration = false; + + private boolean TBT = false; + + Matrix first_inverse = new Matrix(); + Matrix addition = new Matrix(); + Matrix result = new Matrix(); + Matrix result_inv = new Matrix(); + Matrix adj = new Matrix(); + + public KFitterStraight(boolean filter, int iterations, int dir, Swim swim, double Z[], Libr mo) { + super(filter, iterations, dir, swim, mo); + this.Z = Z; + } + + public final void init(List measSurfaces, StateVec initSV) { + finalSmoothedStateVec = null; + finalTransportedStateVec = null; + this.NDF = -5; + this.chi2 = Double.POSITIVE_INFINITY; + this.numIter = 0; + this.setFitFailed = false; + mv.setMeasVecs(measSurfaces); + for (int i = 0; i < mv.measurements.size(); i++) { + if (mv.measurements.get(i).skip == false) { + this.NDF += mv.measurements.get(i).surface.getNMeas(); + } + } + + sv.init(initSV); + sv.Z = Z; + } + + public final void initFromHB(List measSurfaces, StateVec initSV, double beta) { + finalSmoothedStateVec = null; + finalTransportedStateVec = null; + this.NDF = -5; + this.chi2 = Double.POSITIVE_INFINITY; + this.numIter = 0; + this.setFitFailed = false; + mv.setMeasVecs(measSurfaces); + for (int i = 0; i < mv.measurements.size(); i++) { + if (mv.measurements.get(i).skip == false) { + this.NDF += mv.measurements.get(i).surface.getNMeas(); + } + } + + sv.initFromHB(initSV, beta); + sv.Z = Z; + TBT = true; + } + + public void runFitter() { + this.chi2 = Double.POSITIVE_INFINITY; + double initChi2 = Double.POSITIVE_INFINITY; + // this.NDF = mv.ndf; + this.svzLength = this.mv.measurements.size(); + + int sector = this.mv.measurements.get(0).sector; + + if (TBT == true) { + this.chi2kf = 0; + // Get the input parameters + for (int k = 0; k < svzLength - 1; k++) { + sv.transport(sector, k, k + 1, this.sv.trackTrajT.get(k), mv, this.getSwimmer(), true); + } + this.calcFinalChisq(sector, true); + this.initialStateVec = sv.trackTrajT.get(svzLength - 1); + this.finalStateVec = sv.trackTrajT.get(svzLength - 1); + initChi2 = this.chi2; + if (Double.isNaN(chi2)) { + this.setFitFailed = true; + return; + } + } + + for (int i = 1; i <= totNumIter; i++) { + interNum = i; + this.chi2kf = 0; + + if (i > 1) { + + for (int k = svzLength - 1; k > 0; k--) { + boolean forward = false; + if (k >= 2) { + + // Not backward transport and filter states for the last measurement layer + if (k == svzLength - 1) { + if (!sv.transport(sector, k, k - 2, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } else { + if (!sv.transport(sector, k, k - 2, this.sv.trackTrajB.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + + if (!this.filter(k - 2, forward)) { + this.stopIteration = true; + break; + } + + if (!sv.transport(sector, k - 2, k - 1, this.sv.trackTrajB.get(k - 2), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + if (!this.filter(k - 1, forward)) { + this.stopIteration = true; + break; + } + } else { + if (!sv.transport(sector, 1, 0, this.sv.trackTrajB.get(1), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + if (!this.filter(0, forward)) { + this.stopIteration = true; + break; + } + } + } + } + + if (this.stopIteration) { + break; + } + + for (int k = 0; k < svzLength - 1; k++) { + boolean forward = true; + + if (interNum == 1 && (k == 0)) { + if (TBT == true) { + this.sv.transported(true).put(0, this.sv.transported(false).get(0)); // For TBT, calcFinalChisq() is called previously. + } + } + + if (k == 0) { + if (i == 1) { + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajT.get(0), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } else { + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajB.get(0), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + } else { + if (!this.sv.transport(sector, k, k + 1, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + } + + if (!this.filter(k + 1, forward)) { + this.stopIteration = true; + break; + } + } + + if (this.stopIteration) { + break; + } + + if (i > 1) { + if (this.setFitFailed == true) { + i = totNumIter; + } + if (this.setFitFailed == false) { + if (this.finalStateVec != null) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 5.e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 1.e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 1.e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 1.e-6 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.e-6) { + i = totNumIter; + } + } + this.finalStateVec = sv.trackTrajF.get(svzLength - 1); + + } else { + this.ConvStatus = 1; // Should be 0??? + } + } + } + + if (totNumIter == 1) { + if (this.setFitFailed == false && this.stopIteration == false) { + this.finalStateVec = sv.trackTrajF.get(svzLength - 1); + } + } + + this.calcFinalChisq(sector); + + if (Double.isNaN(chi2)) { + this.setFitFailed = true; + } + + if (TBT == true) { + if (chi2 > initChi2) { // fit failed + this.finalStateVec = this.initialStateVec; + sv.trackTrajT.put(svzLength - 1, this.initialStateVec); + this.calcFinalChisq(sector, true); + } + } + + } + + private boolean filter(int k, boolean forward) { + StateVec sVec = sv.transported(forward).get(k); + org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec mVec = mv.measurements.get(k); + + if (Double.isNaN(sVec.x) || Double.isNaN(sVec.y) + || Double.isNaN(sVec.tx) || Double.isNaN(sVec.ty) + || Double.isNaN(sVec.Q)) { + this.setFitFailed = true; + return false; + } + if (sVec != null && sVec.CM != null + && k < mv.measurements.size() && mVec.skip == false) { + + + double[] K = new double[5]; + double V = mVec.surface.unc[0] * KFScale; + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[0]); + Matrix CaInv = this.filterCovMat(H, sVec.CM, V); + Matrix cMat = new Matrix(); + if (CaInv != null) { + Matrix5x5.copy(CaInv, cMat); + } else { + return false; + } + + for (int j = 0; j < 5; j++) { + // the gain matrix + K[j] = (H[0] * cMat.get(j, 0) + + H[1] * cMat.get(j, 1)) / V; + } + + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + double h = mv.hDoca(point, mVec.surface.wireLine[0]); + + double signMeas = 1; + double sign = 1; + if (mVec.surface.doca[1] != -99 + || !(Math.abs(mVec.surface.doca[0]) < 0.5 + && mVec.surface.doca[1] == -99)) { // use LR only for double hits && large + // enough docas + signMeas = Math.signum(mVec.surface.doca[0]); + sign = Math.signum(h); + } else { + signMeas = Math.signum(h); + sign = Math.signum(h); + } + + double c2 = ((signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)) + * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)) / V); + + double x_filt = sVec.x + + K[0] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double y_filt = sVec.y + + K[1] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double tx_filt = sVec.tx + + K[2] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double ty_filt = sVec.ty + + K[3] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + double Q_filt = sVec.Q + + K[4] * (signMeas * Math.abs(mVec.surface.doca[0]) - sign * Math.abs(h)); + + // USE THE DOUBLE HIT + if (mVec.surface.doca[1] != -99) { + // now filter using the other Hit + V = mVec.surface.unc[1] * KFScale; + H = mv.H(x_filt, y_filt, mVec.surface.z, + mVec.surface.wireLine[1]); + CaInv = this.filterCovMat(H, cMat, V); + if (CaInv != null) { + for (int i = 0; i < 5; i++) { + Matrix5x5.copy(CaInv, cMat); + } + } else { + return false; + } + for (int j = 0; j < 5; j++) { + // the gain matrix + K[j] = (H[0] * cMat.get(j, 0) + + H[1] * cMat.get(j, 1)) / V; + } + + Point3D point2 = new Point3D(x_filt, y_filt, mVec.surface.z); + + h = mv.hDoca(point2, mVec.surface.wireLine[1]); + + signMeas = Math.signum(mVec.surface.doca[1]); + sign = Math.signum(h); + + x_filt += K[0] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + y_filt += K[1] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + tx_filt += K[2] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + ty_filt += K[3] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + Q_filt += K[4] * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)); + + c2 += ((signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)) + * (signMeas * Math.abs(mVec.surface.doca[1]) - sign * Math.abs(h)) / V); + } + + chi2kf += c2; + if (filterOn) { + StateVec filteredVec = sv.new StateVec(k); + filteredVec.x = x_filt; + filteredVec.y = y_filt; + filteredVec.tx = tx_filt; + filteredVec.ty = ty_filt; + filteredVec.Q = Q_filt; + filteredVec.z = sVec.z; + filteredVec.B = sVec.B; + filteredVec.deltaPath = sVec.deltaPath; + + filteredVec.CM = cMat; + + sv.filtered(forward).put(k, filteredVec); + } + else { + return false; + } + + return true; + } + else { + return false; + } + } + + public Matrix filterCovMat(double[] H, Matrix Ci, double V) { + + double det = Matrix5x5.inverse(Ci, first_inverse, adj); + if(Math.abs(det)<1.e-60) + return null; + + addition.set( + H[0] * H[0] / V, H[0] * H[1] / V, 0, 0, 0, + H[0] * H[1] / V, H[1] * H[1] / V, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0); + + Matrix5x5.add(first_inverse, addition, result); + double det2 = Matrix5x5.inverse(result, result_inv, adj); + if(Math.abs(det2)<1.e-60) + return null; + + return result_inv; + } + + private void calcFinalChisq(int sector) { + calcFinalChisq(sector, false); + } + + private void calcFinalChisq(int sector, boolean nofilter) { + int k = svzLength - 1; + this.chi2 = 0; + double path = 0; + double[] nRj = new double[3]; + + StateVec sVec; + + // To be changed: to match wit the old package, we make the following codes. Could be changed when other codes for application of calcFinalChisq are changed. + if(nofilter || (sv.trackTrajF.get(k) == null)) { + sVec = sv.trackTrajT.get(k); + } + else { + sVec = sv.trackTrajF.get(k); + } + + + kfStateVecsAlongTrajectory = new ArrayList<>(); + if (sVec != null && sVec.CM != null) { + + boolean forward = false; + sv.transport(sector, k, 0, sVec, mv, this.getSwimmer(), forward); + + StateVec svc = sv.transported(forward).get(0); + path += svc.deltaPath; + svc.setPathLength(path); + + double V0 = mv.measurements.get(0).surface.unc[0]; + + Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); + double h0 = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); + + svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); + svc.setProjectorDoca(h0); + kfStateVecsAlongTrajectory.add(svc); + double res = (mv.measurements.get(0).surface.doca[0] - h0); + chi2 += (mv.measurements.get(0).surface.doca[0] - h0) * (mv.measurements.get(0).surface.doca[0] - h0) / V0; + nRj[mv.measurements.get(0).region-1]+=res*res/mv.measurements.get(0).error; + //USE THE DOUBLE HIT + if(mv.measurements.get(0).surface.doca[1]!=-99) { + V0 = mv.measurements.get(0).surface.unc[1]; + h0 = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[1]); + res = (mv.measurements.get(0).surface.doca[1] - h0); + chi2 += (mv.measurements.get(0).surface.doca[1] - h0) * (mv.measurements.get(0).surface.doca[1] - h0) / V0; + nRj[mv.measurements.get(0).region-1]+=res*res/mv.measurements.get(0).error; + + StateVec svc2 = sv.new StateVec(svc); + svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); + svc2.setProjectorDoca(h0); + kfStateVecsAlongTrajectory.add(svc2); + } + + forward = true; + for (int k1 = 0; k1 < k; k1++) { + if(k1 == 0) { + sv.transport(sector, k1, k1 + 1, svc, mv, this.getSwimmer(), forward); + } + else { + sv.transport(sector, k1, k1 + 1, sv.transported(forward).get(k1), mv, this.getSwimmer(), forward); + } + + double V = mv.measurements.get(k1 + 1).surface.unc[0]; + + point = new Point3D(sv.transported(forward).get(k1+1).x, sv.transported(forward).get(k1+1).y, mv.measurements.get(k1+1).surface.z); + + double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); + svc = sv.transported(forward).get(k1+1); + path += svc.deltaPath; + svc.setPathLength(path); + svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); + svc.setProjectorDoca(h); + kfStateVecsAlongTrajectory.add(svc); + res = (mv.measurements.get(k1 + 1).surface.doca[0] - h); + chi2 += (mv.measurements.get(k1 + 1).surface.doca[0] - h) * (mv.measurements.get(k1 + 1).surface.doca[0] - h) / V; + nRj[mv.measurements.get(k1 + 1).region-1]+=res*res/V; + //USE THE DOUBLE HIT + if(mv.measurements.get(k1 + 1).surface.doca[1]!=-99) { + V = mv.measurements.get(k1 + 1).surface.unc[1]; + h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[1]); + res = (mv.measurements.get(k1 + 1).surface.doca[1] - h); + chi2 += (mv.measurements.get(k1 + 1).surface.doca[1] - h) * (mv.measurements.get(k1 + 1).surface.doca[1] - h) / V; + nRj[mv.measurements.get(k1 + 1).region-1]+=res*res/V; + + StateVec svc2 = sv.new StateVec(svc); + svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); + svc2.setProjectorDoca(h); + kfStateVecsAlongTrajectory.add(svc2); + } + } + } + + } + + + public Matrix propagateToVtx(int sector, double Zf) { + return sv.transport(sector, finalStateVec.k, Zf, finalStateVec, mv, this.getSwimmer()); + } + + @Override + public void runFitter(AStateVecs sv, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public StateVec filter(int k, StateVec vec, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public StateVec smooth(int k, AStateVecs sv, AMeasVecs mv) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public StateVec smooth(StateVec v1, StateVec v2) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public MeasVecs getMeasVecs() { + return mv; + } + + public StateVecs getStateVecs() { + return sv; + } + + public void printlnMeasVecs() { + for (int i = 0; i < mv.measurements.size(); i++) { + org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec measvec = mv.measurements.get(i); + String s = String.format("k=%d region=%d superlayer=%d layer=%d error=%.4f", measvec.k, measvec.region, measvec.superlayer, + measvec.layer, measvec.error); + s += String.format(" Surface: index=%d x=%.4f z=%.4f tilt=%.4f wireMaxSag=%.4f", measvec.surface.getIndex(), + measvec.surface.x, measvec.surface.z, measvec.surface.tilt, measvec.surface.wireMaxSag, measvec.surface.unc[1]); + s += String.format( + " Surface line 0: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", + measvec.surface.doca[0], measvec.surface.unc[0], measvec.surface.wireLine[0].origin().x(), + measvec.surface.wireLine[0].origin().y(), measvec.surface.wireLine[0].origin().z(), + measvec.surface.wireLine[0].end().x(), measvec.surface.wireLine[0].end().y(), + measvec.surface.wireLine[0].end().z()); + if (measvec.surface.wireLine[1] != null) + s += String.format( + " Surface line 1: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", + measvec.surface.doca[1], measvec.surface.unc[1], measvec.surface.wireLine[1].origin().x(), + measvec.surface.wireLine[1].origin().y(), measvec.surface.wireLine[1].origin().z(), + measvec.surface.wireLine[1].end().x(), measvec.surface.wireLine[1].end().y(), + measvec.surface.wireLine[1].end().z()); + + System.out.println(s); + } + } + +} diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index df07a9142..0aaae61d0 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -29,6 +29,7 @@ import org.jlab.clas.tracking.kalmanfilter.Surface; import org.jlab.clas.tracking.kalmanfilter.zReference.KFitter; +import org.jlab.clas.tracking.kalmanfilter.zReference.KFitterStraight; import org.jlab.clas.tracking.kalmanfilter.zReference.StateVecs; import org.jlab.clas.tracking.utilities.MatrixOps.Libr; import org.jlab.clas.tracking.utilities.RungeKuttaDoca; @@ -890,7 +891,7 @@ private List findStraightTracks(CrossList crossList, DCGeant4Factory DcDe LOGGER.log(Level.FINE, "Kalman fitter - 2 = " + (System.currentTimeMillis() - startTime)); - KFitter kFZRef = new KFitter(true, 1, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + KFitterStraight kFZRef = new KFitterStraight(true, 1, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); List measSurfaces = getMeasSurfaces(cand, DcDetector); StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); @@ -1129,6 +1130,23 @@ public List setKFStateVecsAlongTrajectory(K return kfStateVecsAlongTrajectory; } + + public List setKFStateVecsAlongTrajectory(KFitterStraight kFZRef) { + List kfStateVecsAlongTrajectory = new ArrayList<>(); + + for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + sv.setZ(svc.z); + sv.setB(svc.B); + sv.setPathLength(svc.getPathLength()); + sv.setProjector(svc.getProjector()); + sv.setProjectorDoca(svc.getProjectorDoca()); + kfStateVecsAlongTrajectory.add(sv); + } + + return kfStateVecsAlongTrajectory; + } public void getInitState(Track trkcand, double z0, int c, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initStateVec, Swim dcSwim, float[] bf) { diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index ba2c5fb98..e6bca3175 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -8,6 +8,7 @@ import java.util.Map; import java.util.logging.Level; import org.jlab.clas.swimtools.Swim; +import org.jlab.clas.swimtools.Swimmer; import org.jlab.detector.geant4.v2.DCGeant4Factory; import org.jlab.geom.prim.Line3D; import org.jlab.geom.prim.Point3D; @@ -38,6 +39,7 @@ import org.jlab.clas.tracking.kalmanfilter.Surface; import org.jlab.clas.tracking.kalmanfilter.zReference.KFitter; +import org.jlab.clas.tracking.kalmanfilter.zReference.KFitterStraight; import org.jlab.clas.tracking.kalmanfilter.zReference.StateVecs; import org.jlab.clas.tracking.utilities.MatrixOps.Libr; import org.jlab.clas.tracking.utilities.RungeKuttaDoca; @@ -225,46 +227,88 @@ public boolean processDataEvent(DataEvent event) { continue; } crosses.addAll(TrackArray1); + if(Swimmer.getTorScale() < 0.001){ + KFitterStraight kFZRef = new KFitterStraight(true, 30, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitter(); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + StateVec fn = new StateVec(); + if (kFZRef.setFitFailed==false && kFZRef.finalStateVec!=null) { + // set the state vector at the last measurement site + fn.set(kFZRef.finalStateVec.x, kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); + //set the track parameters if the filter does not fail + TrackArray1.set_P(1./Math.abs(kFZRef.finalStateVec.Q)); + TrackArray1.set_Q((int)Math.signum(kFZRef.finalStateVec.Q)); + + trkcandFinder.setTrackPars(TrackArray1, new Trajectory(), trjFind, fn, kFZRef.finalStateVec.z, Constants.getInstance().dcDetector, dcSwim, beamXoffset, beamYoffset); + // candidate parameters are set from the state vector + if (TrackArray1.fit_Successful == false) { + continue; + } + + TrackArray1.set_FitChi2(kFZRef.chi2); + TrackArray1.set_FitNDF(kFZRef.NDF); + TrackArray1.setStateVecs(kfStateVecsAlongTrajectory); + TrackArray1.set_FitConvergenceStatus(kFZRef.ConvStatus); + if (TrackArray1.get_Vtx0().toVector3D().mag() > 500) { + continue; + } - KFitter kFZRef = new KFitter(true, 30, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); - List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); - StateVecs svs = new StateVecs(); - org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); - kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); - kFZRef.runFitter(); - List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); - - StateVec fn = new StateVec(); - if (kFZRef.setFitFailed==false && kFZRef.finalStateVec!=null) { - // set the state vector at the last measurement site - fn.set(kFZRef.finalStateVec.x, kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); - //set the track parameters if the filter does not fail - TrackArray1.set_P(1./Math.abs(kFZRef.finalStateVec.Q)); - TrackArray1.set_Q((int)Math.signum(kFZRef.finalStateVec.Q)); - - trkcandFinder.setTrackPars(TrackArray1, new Trajectory(), trjFind, fn, kFZRef.finalStateVec.z, Constants.getInstance().dcDetector, dcSwim, beamXoffset, beamYoffset); - // candidate parameters are set from the state vector - if (TrackArray1.fit_Successful == false) { - continue; + // get CovMat at vertex + Point3D VTCS = crosses.get(0).getCoordsInSector(TrackArray1.get_Vtx0().x(), TrackArray1.get_Vtx0().y(), TrackArray1.get_Vtx0().z()); + TrackArray1.set_CovMat(kFZRef.propagateToVtx(crosses.get(0).get_Sector(), VTCS.z())); + + if (TrackArray1.isGood()) { + trkcands.add(TrackArray1); + } } - - TrackArray1.set_FitChi2(kFZRef.chi2); - TrackArray1.set_FitNDF(kFZRef.NDF); - TrackArray1.setStateVecs(kfStateVecsAlongTrajectory); - TrackArray1.set_FitConvergenceStatus(kFZRef.ConvStatus); - if (TrackArray1.get_Vtx0().toVector3D().mag() > 500) { - continue; - } - - // get CovMat at vertex - Point3D VTCS = crosses.get(0).getCoordsInSector(TrackArray1.get_Vtx0().x(), TrackArray1.get_Vtx0().y(), TrackArray1.get_Vtx0().z()); - TrackArray1.set_CovMat(kFZRef.propagateToVtx(crosses.get(0).get_Sector(), VTCS.z())); - - if (TrackArray1.isGood()) { - trkcands.add(TrackArray1); - } + } + else{ + KFitter kFZRef = new KFitter(true, 30, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); + List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); + StateVecs svs = new StateVecs(); + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); + getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitter(); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + + StateVec fn = new StateVec(); + if (kFZRef.setFitFailed==false && kFZRef.finalStateVec!=null) { + // set the state vector at the last measurement site + fn.set(kFZRef.finalStateVec.x, kFZRef.finalStateVec.y, kFZRef.finalStateVec.tx, kFZRef.finalStateVec.ty); + //set the track parameters if the filter does not fail + TrackArray1.set_P(1./Math.abs(kFZRef.finalStateVec.Q)); + TrackArray1.set_Q((int)Math.signum(kFZRef.finalStateVec.Q)); + + trkcandFinder.setTrackPars(TrackArray1, new Trajectory(), trjFind, fn, kFZRef.finalStateVec.z, Constants.getInstance().dcDetector, dcSwim, beamXoffset, beamYoffset); + // candidate parameters are set from the state vector + if (TrackArray1.fit_Successful == false) { + continue; + } + + TrackArray1.set_FitChi2(kFZRef.chi2); + TrackArray1.set_FitNDF(kFZRef.NDF); + TrackArray1.setStateVecs(kfStateVecsAlongTrajectory); + TrackArray1.set_FitConvergenceStatus(kFZRef.ConvStatus); + if (TrackArray1.get_Vtx0().toVector3D().mag() > 500) { + continue; + } + + // get CovMat at vertex + Point3D VTCS = crosses.get(0).getCoordsInSector(TrackArray1.get_Vtx0().x(), TrackArray1.get_Vtx0().y(), TrackArray1.get_Vtx0().z()); + TrackArray1.set_CovMat(kFZRef.propagateToVtx(crosses.get(0).get_Sector(), VTCS.z())); + if (TrackArray1.isGood()) { + trkcands.add(TrackArray1); + } + + } } } @@ -332,6 +376,23 @@ public List setKFStateVecsAlongTrajectory(K return kfStateVecsAlongTrajectory; } + public List setKFStateVecsAlongTrajectory(KFitterStraight kFZRef) { + List kfStateVecsAlongTrajectory = new ArrayList<>(); + + for(int i = 0; i < kFZRef.kfStateVecsAlongTrajectory.size(); i++) { + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecsAlongTrajectory.get(i); + org.jlab.rec.dc.trajectory.StateVec sv = new org.jlab.rec.dc.trajectory.StateVec(svc.x, svc.y, svc.tx, svc.ty); + sv.setZ(svc.z); + sv.setB(svc.B); + sv.setPathLength(svc.getPathLength()); + sv.setProjector(svc.getProjector()); + sv.setProjectorDoca(svc.getProjectorDoca()); + kfStateVecsAlongTrajectory.add(sv); + } + + return kfStateVecsAlongTrajectory; + } + public void getInitState(Track trkcand, double z0, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initStateVec, KFitter kf, Swim dcSwim, float[] bf) { if (trkcand != null && trkcand.getFinalStateVec()!=null ) { initStateVec.x = trkcand.getFinalStateVec().x(); @@ -366,6 +427,40 @@ public void getInitState(Track trkcand, double z0, org.jlab.clas.tracking.kalman } + public void getInitState(Track trkcand, double z0, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initStateVec, KFitterStraight kf, Swim dcSwim, float[] bf) { + if (trkcand != null && trkcand.getFinalStateVec()!=null ) { + initStateVec.x = trkcand.getFinalStateVec().x(); + initStateVec.y = trkcand.getFinalStateVec().y(); + initStateVec.z = trkcand.getFinalStateVec().getZ(); + initStateVec.tx = trkcand.getFinalStateVec().tanThetaX(); + initStateVec.ty = trkcand.getFinalStateVec().tanThetaY(); + initStateVec.Q = ((double) trkcand.get_Q())/trkcand.get_P(); + + RungeKuttaDoca rk = new RungeKuttaDoca(); + rk.SwimToZ(trkcand.get(0).get_Sector(), initStateVec, dcSwim, z0, bf); + + double ex = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATEXUNC; + double ey = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATEYUNC; + double etx = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATETXUNC; + double ety = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATETYUNC; + double eQ = Constants.TBINITIALSTATEUNCSCALE * Constants.TBINITIALSTATEQUNC; + + //Matrix initCMatrix = new Matrix(FTF); + Matrix initCMatrix = new Matrix(); + initCMatrix.set(ex * ex, 0, 0, 0, 0, + 0, ey * ey, 0, 0, 0, + 0, 0, etx * etx, 0, 0, + 0, 0, 0, ety * ety, 0, + 0, 0, 0, 0, eQ * eQ + ); + initStateVec.CM = initCMatrix; + + } else { + kf.setFitFailed = true; + } + + } + private org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec reset(org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec sv, org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec stateVec) { StateVecs svs = new StateVecs(); sv = svs.new StateVec(stateVec.k); From f9e18b63b8243114ae83b51726f9e6875324f4ad Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Tue, 19 Dec 2023 08:06:19 -0500 Subject: [PATCH 42/69] update filter_DAF function --- .../kalmanfilter/zReference/KFitter.java | 56 ++++++------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index aa2aae78a..b2f854c8f 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -350,11 +350,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { return false; } if (sVec != null && sVec.CM != null - && k < mv.measurements.size() && mVec.skip == false) { - - double signMeas = 1; - double sign = 1; - + && k < mv.measurements.size() && mVec.skip == false) { double c2 = 0; double x_filt = 0; double y_filt = 0; @@ -398,40 +394,24 @@ private boolean filter(int k, boolean forward, double annealingFactor) { Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); double h = mv.hDoca(point, mVec.surface.wireLine[0]); - - - if (Math.abs(effectiveDoca) < 0.5) { - signMeas = Math.signum(h); - sign = Math.signum(h); - } else { - signMeas = Math.signum(effectiveDoca); - sign = Math.signum(h); - } - - c2 = ((signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)) - * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)) / V); + + c2 = (effectiveDoca - h) * (effectiveDoca - h) / V; x_filt = sVec.x - + K[0] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + K[0] * (effectiveDoca - h); y_filt = sVec.y - + K[1] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + K[1] * (effectiveDoca - h); tx_filt = sVec.tx - + K[2] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + K[2] * (effectiveDoca - h); ty_filt = sVec.ty - + K[3] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + K[3] * (effectiveDoca - h); Q_filt = sVec.Q - + K[4] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + K[4] * (effectiveDoca - h); Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.z); double h0 = mv.hDoca(pointFiltered, mVec.surface.wireLine[0]); - if (Math.abs(effectiveDoca) < 0.5) { - signMeas = Math.signum(h0); - sign = Math.signum(h0); - } else { - signMeas = Math.signum(effectiveDoca); - sign = Math.signum(h0); - } - double residual = signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h0); + + double residual = effectiveDoca - h0; updatedWeights_singleHit = daf.calc_updatedWeight_singleHit(residual, annealingFactor); } else{ @@ -467,22 +447,18 @@ private boolean filter(int k, boolean forward, double annealingFactor) { Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); double h = mv.hDoca(point, mVec.surface.wireLine[indexReferenceWire]); - signMeas = Math.signum(effectiveDoca); - sign = Math.signum(h); - - c2 = ((signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)) - * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)) / V); + c2 = (effectiveDoca - h) * (effectiveDoca - h) / V; x_filt = sVec.x - + K[0] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + K[0] * (effectiveDoca - h); y_filt = sVec.y - + K[1] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + K[1] * (effectiveDoca - h); tx_filt = sVec.tx - + K[2] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + K[2] * (effectiveDoca - h); ty_filt = sVec.ty - + K[3] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + K[3] * (effectiveDoca - h); Q_filt = sVec.Q - + K[4] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); + + K[4] * (effectiveDoca - h); Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.z); double h0 = mv.hDoca(pointFiltered, mVec.surface.wireLine[0]); From a2419f4807e0057afbd5d2a45b3689b017847f7d Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Tue, 19 Dec 2023 10:50:20 -0500 Subject: [PATCH 43/69] fix issue to judge if zero field in DCTBEngine --- .../dc/src/main/java/org/jlab/service/dc/DCTBEngine.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index e6bca3175..c18a5a28f 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -227,7 +227,7 @@ public boolean processDataEvent(DataEvent event) { continue; } crosses.addAll(TrackArray1); - if(Swimmer.getTorScale() < 0.001){ + if(Math.abs(Swimmer.getTorScale()) < 0.001){ KFitterStraight kFZRef = new KFitterStraight(true, 30, 1, dcSwim, Constants.getInstance().Z, Libr.JNP); List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); StateVecs svs = new StateVecs(); From 52eff359562c52f239129d917aa70dcaab42efe9 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Tue, 19 Dec 2023 11:06:50 -0500 Subject: [PATCH 44/69] update pom file for DC reconstruction for version of clas-tracking --- reconstruction/dc/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reconstruction/dc/pom.xml b/reconstruction/dc/pom.xml index 826954397..68719854b 100644 --- a/reconstruction/dc/pom.xml +++ b/reconstruction/dc/pom.xml @@ -28,7 +28,7 @@ org.jlab.clas clas-tracking - 10.0.4-SNAPSHOT + 10.0.5-SNAPSHOT From 28c6e8607b43702e0a3fc235d3cd21f26fefa696 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 20 Dec 2023 13:56:03 -0500 Subject: [PATCH 45/69] add DAF weight into TB hit banks --- .../tracking/kalmanfilter/AStateVecs.java | 15 ++++++++++ .../kalmanfilter/zReference/KFitter.java | 14 +++++++++- etc/bankdefs/hipo4/dc.json | 2 ++ etc/bankdefs/hipo4/dcnn.json | 2 ++ .../org/jlab/rec/dc/banks/RecoBankWriter.java | 2 ++ .../java/org/jlab/rec/dc/hit/FittedHit.java | 28 +++++++++++++++++++ .../org/jlab/rec/dc/trajectory/StateVec.java | 20 +++++++++++++ .../java/org/jlab/service/dc/DCTBEngine.java | 2 ++ 8 files changed, 84 insertions(+), 1 deletion(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java index 19d3987ee..272732019 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java @@ -281,7 +281,22 @@ public void setWeightDAF_doubleHits(double[] weight){ this.weightDAF_double = weight; } + private double finalDAFWeight = -999; + public double getFinalDAFWeight(){ + return finalDAFWeight; + } + public void setFinalDAFWeight(double weight){ + this.finalDAFWeight = weight; + } + private int SorDHit = -1; // 1 : single hit; 0: double hit for DC + public int getSorDHit(){ + return SorDHit; + } + public void setSorDHit(int SorDHit){ + this.SorDHit = SorDHit; + } + public double getPathLength() { return _PathLength; } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index b2f854c8f..d8f9e39fc 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -781,6 +781,8 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); + svc.setFinalDAFWeight(daf_weight); + svc.setSorDHit(1); kfStateVecsAlongTrajectory.add(svc); } else{ @@ -804,12 +806,16 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); + svc.setFinalDAFWeight(daf_weights[0]); + svc.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[1]); svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); + svc2.setFinalDAFWeight(daf_weights[1]); + svc2.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc2); } @@ -845,6 +851,8 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); + svc.setFinalDAFWeight(daf_weight); + svc.setSorDHit(1); kfStateVecsAlongTrajectory.add(svc); } else{ @@ -868,12 +876,16 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); - kfStateVecsAlongTrajectory.add(svc); + svc.setFinalDAFWeight(daf_weights[0]); + svc.setSorDHit(0); + kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[1]); svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); + svc2.setFinalDAFWeight(daf_weights[1]); + svc2.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc2); } } diff --git a/etc/bankdefs/hipo4/dc.json b/etc/bankdefs/hipo4/dc.json index d3663e56b..1ad5d863e 100644 --- a/etc/bankdefs/hipo4/dc.json +++ b/etc/bankdefs/hipo4/dc.json @@ -361,6 +361,8 @@ {"name":"trkDoca", "type":"F", "info":"track doca of the hit (in cm)"}, {"name":"timeResidual", "type":"F", "info":"time residual of the hit (in cm)"}, {"name":"fitResidual", "type":"F", "info":"fit residual of the hit (in cm, from KF)"}, + {"name":"DAFWeight", "type":"F", "info":"Weight by DAF"}, + {"name":"SorD", "type":"B", "info":"1: single hit; 0: double hit"}, {"name":"LR", "type":"B", "info":"Left/Right ambiguity of the hit"}, {"name":"X", "type":"F", "info":"wire x-coordinate in tilted-sector"}, {"name":"Z", "type":"F", "info":"wire z-coordinate in tilted-sector"}, diff --git a/etc/bankdefs/hipo4/dcnn.json b/etc/bankdefs/hipo4/dcnn.json index ab8b04f80..b56b34b99 100644 --- a/etc/bankdefs/hipo4/dcnn.json +++ b/etc/bankdefs/hipo4/dcnn.json @@ -238,6 +238,8 @@ {"name":"timeResidual", "type":"F", "info":"time residual of the hit (in cm)"}, {"name":"fitResidual", "type":"F", "info":"fit residual of the hit (in cm, from KF)"}, {"name":"dDoca", "type":"F", "info":"delta Doca correction (cm)"}, + {"name":"DAFWeight", "type":"F", "info":"Weight by DAF"}, + {"name":"SorD", "type":"B", "info":"1: single hit; 0: double hit"}, {"name":"LR", "type":"B", "info":"Left/Right ambiguity of the hit"}, {"name":"X", "type":"F", "info":"wire x-coordinate in tilted-sector"}, {"name":"Z", "type":"F", "info":"wire z-coordinate in tilted-sector"}, diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java index 4e05488fe..b8bdf0fb2 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java @@ -545,6 +545,8 @@ private DataBank fillTBHitsBank(DataEvent event, List hitlist) { bank.setShort("clusterID", i, (short) hitlist.get(i).get_AssociatedClusterID()); bank.setByte("trkID", i, (byte) hitlist.get(i).get_AssociatedTBTrackID()); bank.setFloat("timeResidual", i, (float) hitlist.get(i).get_TimeResidual()); + bank.setFloat("DAFWeight", i, (float) hitlist.get(i).getDAFWeight()); + bank.setByte("SorD", i, (byte) hitlist.get(i).getSorDHit()); bank.setInt("TDC",i,hitlist.get(i).get_TDC()); bank.setByte("jitter",i, (byte) hitlist.get(i).getJitter()); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java index bf56a5bcb..0e20b78c1 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java @@ -62,6 +62,10 @@ public class FittedHit extends Hit implements Comparable { private double _tProp; private double _tStart; // The event start time private double _Time; //Time = TDC - tFlight - tProp - T0 - TStart + + private double _DAFWeight = -999; // Weight by DAF + private int _SorDHit = -1; // 1 : single hit; 0: double hit for DC + /** * identifying outoftimehits; */ @@ -1037,6 +1041,28 @@ public double get_Beta0to1() { return beta; } + /** + * @return the _DAFWeight + */ + public double getDAFWeight() { + return _DAFWeight; + } + + /** + * @param weight the _DAFWeight to set + */ + public void setDAFWeight(double weight) { + this._DAFWeight = weight; + } + + public int getSorDHit() { + return _SorDHit; + } + + public void setSorDHit(int SorDHit) { + this._SorDHit = SorDHit; + } + public void updateHitfromSV(StateVec st, DCGeant4Factory DcDetector) { // this.set_Id(this.get_Id()); // this.set_TDC(this.get_TDC()); @@ -1044,6 +1070,8 @@ public void updateHitfromSV(StateVec st, DCGeant4Factory DcDetector) { // this.set_AssociatedClusterID(this.get_AssociatedClusterID()); this.setAssociatedStateVec(st); this.set_TrkResid(this.get_Doca() * this.get_LeftRightAmb() - st.getProjectorDoca()); + this.setDAFWeight(st.getDAFWeight()); + this.setSorDHit(st.getSorDHit()); this.setB(st.getB()); this.setSignalPropagTimeAlongWire(st.x(), st.y(), DcDetector); this.setSignalTimeOfFlight(); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/StateVec.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/StateVec.java index 1bc2f7fa1..373641fdc 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/StateVec.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/StateVec.java @@ -33,6 +33,26 @@ public double getPathLength() { public void setPathLength(double _PathLength) { this._PathLength = _PathLength; } + + private double _DAFWeight = -999; + + public double getDAFWeight() { + return _DAFWeight; + } + + public void setDAFWeight(double weight) { + this._DAFWeight = weight; + } + + private int _SorDHit = -1; // 1 : single hit; 0: double hit for DC + + public int getSorDHit() { + return _SorDHit; + } + + public void setSorDHit(int SorDHit) { + this._SorDHit = SorDHit; + } /** * Sets the. diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index c18a5a28f..c8fb39e93 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -370,6 +370,8 @@ public List setKFStateVecsAlongTrajectory(K sv.setPathLength(svc.getPathLength()); sv.setProjector(svc.getProjector()); sv.setProjectorDoca(svc.getProjectorDoca()); + sv.setDAFWeight(svc.getFinalDAFWeight()); + sv.setSorDHit(svc.getSorDHit()); kfStateVecsAlongTrajectory.add(sv); } From 4c4fe700d819b05a0e3a723a6a319c5f28371c30 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 3 Jan 2024 10:27:04 -0500 Subject: [PATCH 46/69] fix a bug in function matchHits() --- .../main/java/org/jlab/rec/dc/track/TrackCandListFinder.java | 4 ++-- .../org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index 0aaae61d0..736fad67b 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -772,7 +772,7 @@ public void matchHits(List stateVecAtPlanesList, Track trk, c.get_Segment2().isOnTrack=true; for (FittedHit h1 : c.get_Segment1()) { if (Math.abs(st.getZ() - h1.get_Z()) < 0.1 && c.get_Segment1().get_Id() > -1 - && (h1.get_XWire() - st.getProjector()) < 0.1) { + && Math.abs(h1.get_XWire() - st.getProjector()) < 0.1) { h1.set_AssociatedHBTrackID(trk.get_Id()); h1.updateHitfromSV(st, DcDetector); @@ -781,7 +781,7 @@ public void matchHits(List stateVecAtPlanesList, Track trk, } for (FittedHit h1 : c.get_Segment2()) { if (Math.abs(st.getZ() - h1.get_Z()) < 0.1 && c.get_Segment2().get_Id() > -1 - && (h1.get_XWire() - st.getProjector()) < 0.1) { + && Math.abs(h1.get_XWire() - st.getProjector()) < 0.1) { h1.set_AssociatedHBTrackID(trk.get_Id()); h1.updateHitfromSV(st, DcDetector); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java index 8ec0f2d33..9a6b39f0e 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -846,7 +846,7 @@ public void matchHits(List stateVecAtPlanesList, Track trk, c.get_Segment2().isOnTrack=true; for (FittedHit h1 : c.get_Segment1()) { if (Math.abs(st.getZ() - h1.get_Z()) < 0.1 && c.get_Segment1().get_Id() > -1 - && (h1.get_XWire() - st.getProjector()) < 0.1) { + && Math.abs(h1.get_XWire() - st.getProjector()) < 0.1) { h1.set_AssociatedHBTrackID(trk.get_Id()); h1.updateHitfromSV(st, DcDetector); @@ -855,7 +855,7 @@ public void matchHits(List stateVecAtPlanesList, Track trk, } for (FittedHit h1 : c.get_Segment2()) { if (Math.abs(st.getZ() - h1.get_Z()) < 0.1 && c.get_Segment2().get_Id() > -1 - && (h1.get_XWire() - st.getProjector()) < 0.1) { + && Math.abs(h1.get_XWire() - st.getProjector()) < 0.1) { h1.set_AssociatedHBTrackID(trk.get_Id()); h1.updateHitfromSV(st, DcDetector); From 9daeb40f21fec9798918a7d6d129b29646f81447 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 31 Jan 2024 14:59:38 -0500 Subject: [PATCH 47/69] add ndfDAF for NDF weighted by DAF --- .../kalmanfilter/zReference/KFitter.java | 16 ++++++++++++++-- .../zReference/KFitterWithURWell.java | 19 ++++++++++++++++--- etc/bankdefs/hipo4/dc.json | 6 ++++-- etc/bankdefs/hipo4/dcnn.json | 6 ++++-- .../org/jlab/rec/dc/banks/RecoBankWriter.java | 2 ++ .../java/org/jlab/rec/dc/track/Track.java | 17 +++++++++++++++++ .../java/org/jlab/service/dc/DCTBEngine.java | 1 + .../org/jlab/service/dc/DCURWellTBEngine.java | 2 ++ 8 files changed, 60 insertions(+), 9 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index aa2aae78a..603ab3faa 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -27,6 +27,8 @@ public class KFitter extends AKFitter { private int dafAnnealingFactorsIndex = 0; private double dafAnnealingFactor = 1; + private double ndfDAF = -5; + private static final double initialCMBlowupFactor = 70; private StateVecs sv = new StateVecs(); @@ -762,6 +764,8 @@ private void calcFinalChisqDAF(int sector) { } private void calcFinalChisqDAF(int sector, boolean nofilter) { + ndfDAF = -5; + int k = svzLength - 1; this.chi2 = 0; double path = 0; @@ -801,7 +805,8 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { double h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); double res = (effectiveDoca - h); - chi2 += res*res / effectiveVar; + chi2 += res*res / effectiveVar; + ndfDAF += daf_weight; svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); @@ -824,6 +829,7 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { double h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[indexReferenceWire]); double res = (effectiveDoca - h); chi2 += res*res / effectiveVar; + ndfDAF += (daf_weights[0] + daf_weights[1]); h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); svc.setProjectorDoca(h); @@ -866,7 +872,8 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); double res = (effectiveDoca - h); chi2 += res*res / effectiveVar; - + ndfDAF += daf_weight; + svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); kfStateVecsAlongTrajectory.add(svc); @@ -888,6 +895,7 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[indexReferenceWire]); double res = (effectiveDoca - h); chi2 += res*res / effectiveVar; + ndfDAF += (daf_weights[0] + daf_weights[1]); h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); svc.setProjectorDoca(h); @@ -935,6 +943,10 @@ public MeasVecs getMeasVecs() { public StateVecs getStateVecs() { return sv; } + + public double getNDFDAF(){ + return ndfDAF; + } public void printlnMeasVecs() { for (int i = 0; i < mv.measurements.size(); i++) { diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index 840482f9b..a283c8691 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -30,6 +30,8 @@ public class KFitterWithURWell extends AKFitter { private int dafAnnealingFactorsIndex = 0; private double dafAnnealingFactor = 1; + private double ndfDAF = -5; + private static final Logger LOGGER = Logger.getLogger(KFitter.class.getName()); private static final double initialCMBlowupFactor = 70; @@ -924,6 +926,8 @@ private void calcFinalChisqDAF(int sector) { } private void calcFinalChisqDAF(int sector, boolean nofilter) { + ndfDAF = -5; + int k = svzLength - 1; this.chi2 = 0; double path = 0; @@ -956,6 +960,7 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { chi2 += (x_res*x_res) / (x_err*x_err); chi2 += (y_res*y_res) / (y_err*y_err); + ndfDAF += 2; } else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); @@ -974,7 +979,8 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ double h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); double res = (effectiveDoca - h); - chi2 += res*res / effectiveVar; + chi2 += res*res / effectiveVar; + ndfDAF += daf_weight; svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); @@ -997,7 +1003,8 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ double h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[indexReferenceWire]); double res = (effectiveDoca - h); chi2 += res*res / effectiveVar; - + ndfDAF += (daf_weights[0] + daf_weights[1]); + h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); @@ -1040,6 +1047,7 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); double res = (effectiveDoca - h); chi2 += res*res / effectiveVar; + ndfDAF += daf_weight; svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); @@ -1062,7 +1070,8 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[indexReferenceWire]); double res = (effectiveDoca - h); chi2 += res*res / effectiveVar; - + ndfDAF += (daf_weights[0] + daf_weights[1]); + h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); @@ -1109,6 +1118,10 @@ public MeasVecs getMeasVecs() { public StateVecs getStateVecs() { return sv; } + + public double getNDFDAF(){ + return ndfDAF; + } public void printlnMeasVecs() { for (int i = 0; i < mv.measurements.size(); i++) { diff --git a/etc/bankdefs/hipo4/dc.json b/etc/bankdefs/hipo4/dc.json index d3663e56b..06a33d38b 100644 --- a/etc/bankdefs/hipo4/dc.json +++ b/etc/bankdefs/hipo4/dc.json @@ -304,7 +304,8 @@ {"name":"q", "type":"B", "info":"charge of the track"}, {"name":"pathlength", "type":"F", "info":"pathlength of the track"}, {"name":"chi2", "type":"F", "info":"fit chi2 of the track"}, - {"name":"ndf", "type":"S", "info":"fit ndf of the track"} + {"name":"ndf", "type":"S", "info":"fit ndf of the track"}, + {"name":"ndfDAF", "type":"F", "info":"fit ndf of the track weighted by DAF"} ] }, { @@ -546,7 +547,8 @@ {"name":"q", "type":"B", "info":"charge of the track"}, {"name":"pathlength", "type":"F", "info":"pathlength of the track"}, {"name":"chi2", "type":"F", "info":"fit chi2 of the track"}, - {"name":"ndf", "type":"S", "info":"fit ndf of the track"} + {"name":"ndf", "type":"S", "info":"fit ndf of the track"}, + {"name":"ndfDAF", "type":"F", "info":"fit ndf of the track weighted by DAF"} ] }, { diff --git a/etc/bankdefs/hipo4/dcnn.json b/etc/bankdefs/hipo4/dcnn.json index ab8b04f80..a9a319777 100644 --- a/etc/bankdefs/hipo4/dcnn.json +++ b/etc/bankdefs/hipo4/dcnn.json @@ -180,7 +180,8 @@ {"name":"q", "type":"B", "info":"charge of the track"}, {"name":"pathlength", "type":"F", "info":"pathlength of the track"}, {"name":"chi2", "type":"F", "info":"fit chi2 of the track"}, - {"name":"ndf", "type":"S", "info":"fit ndf of the track"} + {"name":"ndf", "type":"S", "info":"fit ndf of the track"}, + {"name":"ndfDAF", "type":"F", "info":"fit ndf of the track weighted by DAF"} ] }, { @@ -408,7 +409,8 @@ {"name":"q", "type":"B", "info":"charge of the track"}, {"name":"pathlength", "type":"F", "info":"pathlength of the track"}, {"name":"chi2", "type":"F", "info":"fit chi2 of the track"}, - {"name":"ndf", "type":"S", "info":"fit ndf of the track"} + {"name":"ndf", "type":"S", "info":"fit ndf of the track"}, + {"name":"ndfDAF", "type":"F", "info":"fit ndf of the track weighted by DAF"} ] }, { diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java index 4e05488fe..ebd4ba9ca 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java @@ -438,6 +438,7 @@ public DataBank fillHBTracksBank(DataEvent event, List candlist) { bank.setFloat("chi2", i, (float) candlist.get(i).get_FitChi2()); bank.setShort("ndf", i, (short) candlist.get(i).get_FitNDF()); + bank.setFloat("ndfDAF", i, (float) candlist.get(i).get_NDFDAF()); bank.setFloat("x", i, (float) candlist.get(i).getFinalStateVec().x()); bank.setFloat("y", i, (float) candlist.get(i).getFinalStateVec().y()); bank.setFloat("z", i, (float) candlist.get(i).getFinalStateVec().getZ()); @@ -862,6 +863,7 @@ private DataBank fillTBTracksBank(DataEvent event, List candlist) { } bank.setFloat("chi2", i, (float) candlist.get(i).get_FitChi2()); bank.setShort("ndf", i, (short) candlist.get(i).get_FitNDF()); + bank.setFloat("ndfDAF", i, (float) candlist.get(i).get_NDFDAF()); } return bank; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/Track.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/Track.java index 6b0425fe9..ec14ba630 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/Track.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/Track.java @@ -60,6 +60,7 @@ public void setFinalStateVec(StateVec finalStateVec) { private Vector3D _pAtOrig_TiltedCS; private String _trking; private int _FitNDF; + private double _NDFDAF; private double _fitChisq; public boolean fit_Successful; private int _missingSuperlayer; @@ -435,6 +436,22 @@ public int get_FitNDF() { public void set_FitNDF(int _FitNDF) { this._FitNDF = _FitNDF; } + + /** + * + * @return Kalman fit NDF weighted DAF + */ + public double get_NDFDAF() { + return _NDFDAF; + } + /** + * + * @param _NDFDAF Kalman fit NDF weighted by DAF + */ + public void set_NDFDAF(double _NDFDAF) { + this._NDFDAF = _NDFDAF; + } + /** * * @return Kalman fit covariance matrix diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index c18a5a28f..017da7116 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -294,6 +294,7 @@ public boolean processDataEvent(DataEvent event) { TrackArray1.set_FitChi2(kFZRef.chi2); TrackArray1.set_FitNDF(kFZRef.NDF); + TrackArray1.set_NDFDAF(kFZRef.getNDFDAF()); TrackArray1.setStateVecs(kfStateVecsAlongTrajectory); TrackArray1.set_FitConvergenceStatus(kFZRef.ConvStatus); if (TrackArray1.get_Vtx0().toVector3D().mag() > 500) { diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index 7924b9af9..bd2e6e6c4 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -272,6 +272,7 @@ public boolean processDataEvent(DataEvent event) { TrackArray1.set_FitChi2(kFZRef.chi2); TrackArray1.set_FitNDF(kFZRef.NDF); + TrackArray1.set_NDFDAF(kFZRef.getNDFDAF()); TrackArray1.setStateVecs(kfStateVecsAlongTrajectory); TrackArray1.set_FitConvergenceStatus(kFZRef.ConvStatus); if (TrackArray1.get_Vtx0().toVector3D().mag() > 500) { @@ -313,6 +314,7 @@ public boolean processDataEvent(DataEvent event) { TrackArray1.set_FitChi2(kFZRef.chi2); TrackArray1.set_FitNDF(kFZRef.NDF); + TrackArray1.set_NDFDAF(kFZRef.getNDFDAF()); TrackArray1.setStateVecs(kfStateVecsAlongTrajectory); TrackArray1.set_FitConvergenceStatus(kFZRef.ConvStatus); if (TrackArray1.get_Vtx0().toVector3D().mag() > 500) { From 0a0df89fcb138332a7ee8aee9e177772fa3e2cbe Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 31 Jan 2024 15:20:40 -0500 Subject: [PATCH 48/69] store final weight for DAF in DC-uRWell tracking --- .../kalmanfilter/zReference/KFitterWithURWell.java | 14 +++++++++++++- .../java/org/jlab/service/dc/DCURWellTBEngine.java | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index a283c8691..9b6ace5e8 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -984,6 +984,8 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); + svc.setFinalDAFWeight(daf_weight); + svc.setSorDHit(1); kfStateVecsAlongTrajectory.add(svc); } else{ @@ -1007,13 +1009,17 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); svc.setProjectorDoca(h); - svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); + svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); + svc.setFinalDAFWeight(daf_weights[0]); + svc.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); h = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[1]); svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); + svc2.setFinalDAFWeight(daf_weights[1]); + svc2.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc2); } } @@ -1051,6 +1057,8 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); + svc.setFinalDAFWeight(daf_weight); + svc.setSorDHit(1); kfStateVecsAlongTrajectory.add(svc); } else{ @@ -1075,12 +1083,16 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); + svc.setFinalDAFWeight(daf_weights[0]); + svc.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[1]); svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); + svc2.setFinalDAFWeight(daf_weights[1]); + svc2.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc2); } } diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index bd2e6e6c4..ee06e0093 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -389,6 +389,8 @@ public List setKFStateVecsAlongTrajectory(K sv.setPathLength(svc.getPathLength()); sv.setProjector(svc.getProjector()); sv.setProjectorDoca(svc.getProjectorDoca()); + sv.setDAFWeight(svc.getFinalDAFWeight()); + sv.setSorDHit(svc.getSorDHit()); kfStateVecsAlongTrajectory.add(sv); } @@ -406,6 +408,8 @@ public List setKFStateVecsAlongTrajectory(K sv.setPathLength(svc.getPathLength()); sv.setProjector(svc.getProjector()); sv.setProjectorDoca(svc.getProjectorDoca()); + sv.setDAFWeight(svc.getFinalDAFWeight()); + sv.setSorDHit(svc.getSorDHit()); kfStateVecsAlongTrajectory.add(sv); } From 469bf9518aa5f62922b0116bfa3ca6133cfb28dc Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 8 Feb 2024 22:14:23 -0500 Subject: [PATCH 49/69] update pom file in DC package for version of clas-tracking --- reconstruction/dc/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reconstruction/dc/pom.xml b/reconstruction/dc/pom.xml index 1a71b9261..dc0d5eb69 100644 --- a/reconstruction/dc/pom.xml +++ b/reconstruction/dc/pom.xml @@ -28,7 +28,7 @@ org.jlab.clas clas-tracking - 10.0.5-SNAPSHOT + 10.0.6-SNAPSHOT From d4921c8151eab468a28221a42c4ce13849b29283 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 6 Mar 2024 16:59:12 -0500 Subject: [PATCH 50/69] set configuration for option of DAF application --- .../kalmanfilter/zReference/KFitter.java | 207 ++++++++++++++++- .../zReference/KFitterWithURWell.java | 208 +++++++++++++++++- .../java/org/jlab/service/dc/DCEngine.java | 5 + .../java/org/jlab/service/dc/DCTBEngine.java | 13 +- .../org/jlab/service/dc/DCURWellTBEngine.java | 20 +- 5 files changed, 435 insertions(+), 18 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index b7f7eb5a9..38de06769 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -101,6 +101,26 @@ public final void initFromHB(List measSurfaces, StateVec initSV, double sv.Z = Z; TBT = true; } + + public final void initFromHBNoDAF(List measSurfaces, StateVec initSV, double beta) { + finalSmoothedStateVec = null; + finalTransportedStateVec = null; + this.NDF0 = -5; + this.NDF = -5; + this.chi2 = Double.POSITIVE_INFINITY; + this.numIter = 0; + this.setFitFailed = false; + mv.setMeasVecs(measSurfaces); + for (int i = 0; i < mv.measurements.size(); i++) { + if (mv.measurements.get(i).skip == false) { + this.NDF += mv.measurements.get(i).surface.getNMeas(); + } + } + + sv.initFromHB(initSV, beta); + sv.Z = Z; + TBT = true; + } public void runFitter() { this.chi2 = Double.POSITIVE_INFINITY; @@ -327,18 +347,193 @@ public void runFitter() { if (Double.isNaN(chi2)) { this.setFitFailed = true; + } + } + + public void runFitterNoDAF() { + this.chi2 = Double.POSITIVE_INFINITY; + double initChi2 = Double.POSITIVE_INFINITY; + // this.NDF = mv.ndf; + this.svzLength = this.mv.measurements.size(); + + int sector = this.mv.measurements.get(0).sector; + + if (TBT == true) { + this.chi2kf = 0; + // Get the input parameters + for (int k = 0; k < svzLength - 1; k++) { + sv.transport(sector, k, k + 1, this.sv.trackTrajT.get(k), mv, this.getSwimmer(), true); + } + this.calcFinalChisq(sector, true); + this.initialStateVec = sv.trackTrajT.get(svzLength - 1); + this.finalStateVec = sv.trackTrajT.get(svzLength - 1); + initChi2 = this.chi2; + if (Double.isNaN(chi2)) { + this.setFitFailed = true; + return; + } } - -/* + + for (int i = 1; i <= totNumIter; i++) { + iterNum = i; + this.chi2kf = 0; + + if (i > 1) { + + for (int k = svzLength - 1; k > 0; k--) { + boolean forward = false; + if (k >= 2) { + + // Not backward transport and filter states for the last measurement layer + if (k == svzLength - 1) { + if (!sv.transport(sector, k, k - 2, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } else { + if (!sv.transport(sector, k, k - 2, this.sv.trackTrajB.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + + if (!this.filter(k - 2, forward)) { + this.stopIteration = true; + break; + } + + if (!sv.transport(sector, k - 2, k - 1, this.sv.trackTrajB.get(k - 2), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + if (!this.filter(k - 1, forward)) { + this.stopIteration = true; + break; + } + } else { + if (!sv.transport(sector, 1, 0, this.sv.trackTrajB.get(1), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + if (!this.filter(0, forward)) { + this.stopIteration = true; + break; + } + } + } + } + + if (this.stopIteration) { + break; + } + + for (int k = 0; k < svzLength - 1; k++) { + boolean forward = true; + + if (iterNum == 1 && (k == 0)) { + if (TBT == true) { + this.sv.transported(true).put(0, this.sv.transported(false).get(0)); // For TBT, calcFinalChisq() is called previously. + } + } + + if (k == 0) { + if (i == 1) { + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajT.get(0), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } else { + double c00 = this.sv.trackTrajB.get(0).CM.get(0, 0); + double c11 = this.sv.trackTrajB.get(0).CM.get(1, 1); + double c22 = this.sv.trackTrajB.get(0).CM.get(2, 2); + double c33 = this.sv.trackTrajB.get(0).CM.get(3, 3); + double c44 = this.sv.trackTrajB.get(0).CM.get(4, 4); + Matrix newCM = new Matrix(); + newCM.set(c00*initialCMBlowupFactor, 0, 0, 0, 0, + 0, c11*initialCMBlowupFactor, 0, 0, 0, + 0, 0, c22*initialCMBlowupFactor, 0, 0, + 0, 0, 0, c33*initialCMBlowupFactor, 0, + 0, 0, 0, 0, c44*initialCMBlowupFactor); + this.sv.trackTrajB.get(0).CM = newCM; + + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajB.get(0), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + } else { + if (!this.sv.transport(sector, k, k + 1, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + } + + if (!this.filter(k + 1, forward)) { + this.stopIteration = true; + break; + } + } + + if (this.stopIteration) { + break; + } + + if (i > 1) { + if (this.setFitFailed == true) { + i = totNumIter; + } + if (this.setFitFailed == false) { + if (this.finalStateVec != null) { + if (!TBT) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 1.6e-3 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 1.2e-2 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 1.4e-1 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 2.5e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.0e-3) { + i = totNumIter; + } + } else { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 1.1e-5 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 5.5e-5 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 8.0e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 2.1e-6 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 3.5e-6) { + i = totNumIter; + } + } + } + this.finalStateVec = sv.trackTrajF.get(svzLength - 1); + + } else { + this.ConvStatus = 1; // Should be 0??? + } + } + + } + + if (totNumIter == 1) { + if (this.setFitFailed == false && this.stopIteration == false) { + this.finalStateVec = sv.trackTrajF.get(svzLength - 1); + } + } + + this.calcFinalChisq(sector); + + if (Double.isNaN(chi2)) { + this.setFitFailed = true; + } + if (TBT == true) { - if (chi2 > initChi2) { // fit failed + if (chi2 > initChi2) { // fit failed this.finalStateVec = this.initialStateVec; sv.trackTrajT.put(svzLength - 1, this.initialStateVec); - this.calcFinalChisqDAF(sector, true); + this.calcFinalChisq(sector, true); } } -*/ - + } private boolean filter(int k, boolean forward, double annealingFactor) { diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index 9b6ace5e8..ca1203719 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -113,6 +113,29 @@ else if(mv.measurements.get(i).surface.type == Type.PLANEURWELL) sv.Z = Z; TBT = true; } + + public final void initFromHBNoDAF(List measSurfaces, StateVec initSV, double beta) { + finalSmoothedStateVec = null; + finalTransportedStateVec = null; + this.NDF0 = -5; + this.NDF = -5; + this.chi2 = Double.POSITIVE_INFINITY; + this.numIter = 0; + this.setFitFailed = false; + mv.setMeasVecs(measSurfaces); + for (int i = 0; i < mv.measurements.size(); i++) { + if (mv.measurements.get(i).skip == false) { + if(mv.measurements.get(i).surface.type == Type.LINEDOCA) + this.NDF += mv.measurements.get(i).surface.getNMeas(); + else if(mv.measurements.get(i).surface.type == Type.PLANEURWELL) + this.NDF += 2; + } + } + + sv.initFromHB(initSV, beta); + sv.Z = Z; + TBT = true; + } public void runFitter() { this.chi2 = Double.POSITIVE_INFINITY; @@ -342,18 +365,193 @@ public void runFitter() { if (Double.isNaN(chi2)) { this.setFitFailed = true; } + } + + public void runFitterNoDAF() { + this.chi2 = Double.POSITIVE_INFINITY; + double initChi2 = Double.POSITIVE_INFINITY; + // this.NDF = mv.ndf; + this.svzLength = this.mv.measurements.size(); + + int sector = this.mv.measurements.get(0).sector; + + if (TBT == true) { + this.chi2kf = 0; + // Get the input parameters + for (int k = 0; k < svzLength - 1; k++) { + sv.transport(sector, k, k + 1, this.sv.trackTrajT.get(k), mv, this.getSwimmer(), true); + } + this.calcFinalChisq(sector, true); + this.initialStateVec = sv.trackTrajT.get(svzLength - 1); + this.finalStateVec = sv.trackTrajT.get(svzLength - 1); + initChi2 = this.chi2; + if (Double.isNaN(chi2)) { + this.setFitFailed = true; + return; + } + } + + for (int i = 1; i <= totNumIter; i++) { + iterNum = i; + this.chi2kf = 0; + + if (i > 1) { + + for (int k = svzLength - 1; k > 0; k--) { + boolean forward = false; + if (k >= 2) { + + // Not backward transport and filter states for the last measurement layer + if (k == svzLength - 1) { + if (!sv.transport(sector, k, k - 2, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } else { + if (!sv.transport(sector, k, k - 2, this.sv.trackTrajB.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + + if (!this.filter(k - 2, forward)) { + this.stopIteration = true; + break; + } + + if (!sv.transport(sector, k - 2, k - 1, this.sv.trackTrajB.get(k - 2), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + if (!this.filter(k - 1, forward)) { + this.stopIteration = true; + break; + } + } else { + if (!sv.transport(sector, 1, 0, this.sv.trackTrajB.get(1), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + if (!this.filter(0, forward)) { + this.stopIteration = true; + break; + } + } + } + } + + if (this.stopIteration) { + break; + } + + for (int k = 0; k < svzLength - 1; k++) { + boolean forward = true; + + if (iterNum == 1 && (k == 0)) { + if (TBT == true) { + this.sv.transported(true).put(0, this.sv.transported(false).get(0)); // For TBT, calcFinalChisq() is called previously. + } + } + + if (k == 0) { + if (i == 1) { + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajT.get(0), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } else { + double c00 = this.sv.trackTrajB.get(0).CM.get(0, 0); + double c11 = this.sv.trackTrajB.get(0).CM.get(1, 1); + double c22 = this.sv.trackTrajB.get(0).CM.get(2, 2); + double c33 = this.sv.trackTrajB.get(0).CM.get(3, 3); + double c44 = this.sv.trackTrajB.get(0).CM.get(4, 4); + Matrix newCM = new Matrix(); + newCM.set(c00*initialCMBlowupFactor, 0, 0, 0, 0, + 0, c11*initialCMBlowupFactor, 0, 0, 0, + 0, 0, c22*initialCMBlowupFactor, 0, 0, + 0, 0, 0, c33*initialCMBlowupFactor, 0, + 0, 0, 0, 0, c44*initialCMBlowupFactor); + this.sv.trackTrajB.get(0).CM = newCM; + + if (!this.sv.transport(sector, 0, 1, this.sv.trackTrajB.get(0), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + } + } else { + if (!this.sv.transport(sector, k, k + 1, this.sv.trackTrajF.get(k), mv, this.getSwimmer(), forward)) { + this.stopIteration = true; + break; + } + + } + + if (!this.filter(k + 1, forward)) { + this.stopIteration = true; + break; + } + } + + if (this.stopIteration) { + break; + } + + if (i > 1) { + if (this.setFitFailed == true) { + i = totNumIter; + } + if (this.setFitFailed == false) { + if (this.finalStateVec != null) { + if (!TBT) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 1.6e-3 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 1.2e-2 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 1.4e-1 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 2.5e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.0e-3) { + i = totNumIter; + } + } else { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 1.1e-5 + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 5.5e-5 + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 8.0e-4 + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 2.1e-6 + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 3.5e-6) { + i = totNumIter; + } + } + } + this.finalStateVec = sv.trackTrajF.get(svzLength - 1); + + } else { + this.ConvStatus = 1; // Should be 0??? + } + } + + } + + if (totNumIter == 1) { + if (this.setFitFailed == false && this.stopIteration == false) { + this.finalStateVec = sv.trackTrajF.get(svzLength - 1); + } + } + + this.calcFinalChisq(sector); + + if (Double.isNaN(chi2)) { + this.setFitFailed = true; + } -/* if (TBT == true) { - if (chi2 > initChi2) { // fit failed + if (chi2 > initChi2) { // fit failed this.finalStateVec = this.initialStateVec; sv.trackTrajT.put(svzLength - 1, this.initialStateVec); - this.calcFinalChisqDAF(sector, true); + this.calcFinalChisq(sector, true); } } -*/ - } + } private boolean filter(int k, boolean forward, double annealingFactor) { StateVec sVec = sv.transported(forward).get(k); diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java index c7dde27cb..107602fd0 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java @@ -31,6 +31,7 @@ public class DCEngine extends ReconstructionEngine { private String inBankPrefix = null; private String outBankPrefix = null; private double[][] shifts = new double[Constants.NREG][6]; + protected boolean useDAF = true; public static final Logger LOGGER = Logger.getLogger(ReconstructionEngine.class.getName()); @@ -99,6 +100,10 @@ else if(this.getEngineConfigString("dcT2DFunc").equalsIgnoreCase("Polynomial")) outBankPrefix = this.getEngineConfigString("outputBankPrefix"); } + //Set if use DAF + if(this.getEngineConfigString("useDAF")!=null) + useDAF=Boolean.valueOf(this.getEngineConfigString("useDAF")); + // Set geometry shifts for alignment code if(this.getEngineConfigString("alignmentShifts")!=null) { String[] alignmentShift = this.getEngineConfigString("alignmentShifts").split(","); diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index 11d4947b7..434a82859 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -274,8 +274,15 @@ public boolean processDataEvent(DataEvent event) { StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); - kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); - kFZRef.runFitter(); + if(useDAF){ + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitter(); + } + else{ + kFZRef.initFromHBNoDAF(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitterNoDAF(); + } + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); StateVec fn = new StateVec(); @@ -359,7 +366,7 @@ public boolean processDataEvent(DataEvent event) { return true; } - + public List setKFStateVecsAlongTrajectory(KFitter kFZRef) { List kfStateVecsAlongTrajectory = new ArrayList<>(); diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index ee06e0093..f31ff2231 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -252,8 +252,14 @@ public boolean processDataEvent(DataEvent event) { StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); - kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); - kFZRef.runFitter(); + if(useDAF){ + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitter(); + } + else{ + kFZRef.initFromHBNoDAF(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitterNoDAF(); + } List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); StateVec fn = new StateVec(); @@ -294,8 +300,14 @@ public boolean processDataEvent(DataEvent event) { StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); - kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); - kFZRef.runFitter(); + if(useDAF){ + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitter(); + } + else{ + kFZRef.initFromHBNoDAF(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.runFitterNoDAF(); + } List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); StateVec fn = new StateVec(); From 5a47f524aaf6e4623a43f862df2be3638dbb6dc1 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 6 Mar 2024 23:38:36 -0500 Subject: [PATCH 51/69] set configuration for option of DAF parameters --- .../kalmanfilter/zReference/DAFilter.java | 2 +- .../kalmanfilter/zReference/KFitter.java | 30 ++++++++++++++----- .../zReference/KFitterWithURWell.java | 28 +++++++++++++---- .../java/org/jlab/service/dc/DCEngine.java | 8 +++++ .../java/org/jlab/service/dc/DCTBEngine.java | 2 +- .../org/jlab/service/dc/DCURWellTBEngine.java | 4 +-- 6 files changed, 57 insertions(+), 17 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java index bf21e8663..c2084cfa2 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java @@ -9,7 +9,7 @@ */ public class DAFilter { - private static final double dafChi2Cut = 8; + protected static double dafChi2Cut = 8; // For double hits private double[] docas_double; diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 38de06769..172267b58 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -22,8 +22,8 @@ * @author Tongtong Cao */ public class KFitter extends AKFitter { - - private static final double[] dafAnnealingFactorsTB={64, 16, 4, 1}; + + private ArrayList dafAnnealingFactorsTB = new ArrayList<>(); private int dafAnnealingFactorsIndex = 0; private double dafAnnealingFactor = 1; @@ -84,7 +84,23 @@ public final void init(List measSurfaces, StateVec initSV) { sv.Z = Z; } - public final void initFromHB(List measSurfaces, StateVec initSV, double beta) { + public final void initFromHB(List measSurfaces, StateVec initSV, double beta, String strDAFChi2Cut, String strDAFAnnealingFactorsTB) { + if(strDAFChi2Cut != null){ + DAFilter.dafChi2Cut = Double.valueOf(strDAFChi2Cut); + } + + if(strDAFAnnealingFactorsTB != null){ + String strs[] = strDAFAnnealingFactorsTB.split(","); + for(int i = 0; i < strs.length; i++) + dafAnnealingFactorsTB.add(Double.valueOf(strs[i])); + } + else{ + dafAnnealingFactorsTB.add(64.); + dafAnnealingFactorsTB.add(16.); + dafAnnealingFactorsTB.add(4.); + dafAnnealingFactorsTB.add(1.); + } + finalSmoothedStateVec = null; finalTransportedStateVec = null; this.NDF = -5; @@ -150,8 +166,8 @@ public void runFitter() { this.chi2kf = 0; if (i > 1) { - if (dafAnnealingFactorsIndex < dafAnnealingFactorsTB.length) { - dafAnnealingFactor = dafAnnealingFactorsTB[dafAnnealingFactorsIndex]; + if (dafAnnealingFactorsIndex < dafAnnealingFactorsTB.size()) { + dafAnnealingFactor = dafAnnealingFactorsTB.get(dafAnnealingFactorsIndex); dafAnnealingFactorsIndex++; } else { dafAnnealingFactor = 1; @@ -232,8 +248,8 @@ public void runFitter() { break; } - if (dafAnnealingFactorsIndex < dafAnnealingFactorsTB.length) { - dafAnnealingFactor = dafAnnealingFactorsTB[dafAnnealingFactorsIndex]; + if (dafAnnealingFactorsIndex < dafAnnealingFactorsTB.size()) { + dafAnnealingFactor = dafAnnealingFactorsTB.get(dafAnnealingFactorsIndex); dafAnnealingFactorsIndex++; } else { dafAnnealingFactor = 1; diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index ca1203719..25fca39f8 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -26,7 +26,7 @@ */ public class KFitterWithURWell extends AKFitter { - private static final double[] dafAnnealingFactorsTB={64, 16, 4, 1}; + private ArrayList dafAnnealingFactorsTB = new ArrayList<>(); private int dafAnnealingFactorsIndex = 0; private double dafAnnealingFactor = 1; @@ -92,7 +92,23 @@ else if(mv.measurements.get(i).surface.type == Type.PLANEURWELL) sv.Z = Z; } - public final void initFromHB(List measSurfaces, StateVec initSV, double beta) { + public final void initFromHB(List measSurfaces, StateVec initSV, double beta, String strDAFChi2Cut, String strDAFAnnealingFactorsTB) { + if(strDAFChi2Cut != null){ + DAFilter.dafChi2Cut = Double.valueOf(strDAFChi2Cut); + } + + if(strDAFAnnealingFactorsTB != null){ + String strs[] = strDAFAnnealingFactorsTB.split(","); + for(int i = 0; i < strs.length; i++) + dafAnnealingFactorsTB.add(Double.valueOf(strs[i])); + } + else{ + dafAnnealingFactorsTB.add(64.); + dafAnnealingFactorsTB.add(16.); + dafAnnealingFactorsTB.add(4.); + dafAnnealingFactorsTB.add(1.); + } + finalSmoothedStateVec = null; finalTransportedStateVec = null; this.NDF = -5; @@ -166,8 +182,8 @@ public void runFitter() { this.chi2kf = 0; if (i > 1) { - if (dafAnnealingFactorsIndex < dafAnnealingFactorsTB.length) { - dafAnnealingFactor = dafAnnealingFactorsTB[dafAnnealingFactorsIndex]; + if (dafAnnealingFactorsIndex < dafAnnealingFactorsTB.size()) { + dafAnnealingFactor = dafAnnealingFactorsTB.get(dafAnnealingFactorsIndex); dafAnnealingFactorsIndex++; } else { dafAnnealingFactor = 1; @@ -249,8 +265,8 @@ public void runFitter() { break; } - if (dafAnnealingFactorsIndex < dafAnnealingFactorsTB.length) { - dafAnnealingFactor = dafAnnealingFactorsTB[dafAnnealingFactorsIndex]; + if (dafAnnealingFactorsIndex < dafAnnealingFactorsTB.size()) { + dafAnnealingFactor = dafAnnealingFactorsTB.get(dafAnnealingFactorsIndex); dafAnnealingFactorsIndex++; } else { dafAnnealingFactor = 1; diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java index 107602fd0..7b15f082e 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java @@ -32,6 +32,8 @@ public class DCEngine extends ReconstructionEngine { private String outBankPrefix = null; private double[][] shifts = new double[Constants.NREG][6]; protected boolean useDAF = true; + protected String dafChi2Cut = null; + protected String dafAnnealingFactorsTB = null; public static final Logger LOGGER = Logger.getLogger(ReconstructionEngine.class.getName()); @@ -104,6 +106,12 @@ else if(this.getEngineConfigString("dcT2DFunc").equalsIgnoreCase("Polynomial")) if(this.getEngineConfigString("useDAF")!=null) useDAF=Boolean.valueOf(this.getEngineConfigString("useDAF")); + if(this.getEngineConfigString("dafChi2Cut")!=null) + dafChi2Cut=this.getEngineConfigString("dafChi2Cut"); + + if(this.getEngineConfigString("dafAnnealingFactorsTB")!=null) + dafAnnealingFactorsTB=this.getEngineConfigString("dafAnnealingFactorsTB"); + // Set geometry shifts for alignment code if(this.getEngineConfigString("alignmentShifts")!=null) { String[] alignmentShift = this.getEngineConfigString("alignmentShifts").split(","); diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index 434a82859..487a13800 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -275,7 +275,7 @@ public boolean processDataEvent(DataEvent event) { org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); if(useDAF){ - kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta(), dafChi2Cut, dafAnnealingFactorsTB); kFZRef.runFitter(); } else{ diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index f31ff2231..1a21fed46 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -253,7 +253,7 @@ public boolean processDataEvent(DataEvent event) { org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); if(useDAF){ - kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta(), dafChi2Cut, dafAnnealingFactorsTB); kFZRef.runFitter(); } else{ @@ -301,7 +301,7 @@ public boolean processDataEvent(DataEvent event) { org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); if(useDAF){ - kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta(), dafChi2Cut, dafAnnealingFactorsTB); kFZRef.runFitter(); } else{ From a742314191b36720f668eb929dbeddc869792fab Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 7 Mar 2024 14:14:37 -0500 Subject: [PATCH 52/69] update dc pom file for tracking package version --- reconstruction/dc/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reconstruction/dc/pom.xml b/reconstruction/dc/pom.xml index ec5e72454..4b8bee783 100644 --- a/reconstruction/dc/pom.xml +++ b/reconstruction/dc/pom.xml @@ -28,7 +28,7 @@ org.jlab.clas clas-tracking - 10.0.7-SNAPSHOT + 10.0.8-SNAPSHOT From d1a2c290c6d0d507bbc36ee556ad0047a8df9dfe Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 7 Mar 2024 14:35:01 -0500 Subject: [PATCH 53/69] loose limit for Vtx0_y in DC reconstruction test --- .../test/java/org/jlab/service/dc/DCReconstructionTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reconstruction/dc/src/test/java/org/jlab/service/dc/DCReconstructionTest.java b/reconstruction/dc/src/test/java/org/jlab/service/dc/DCReconstructionTest.java index 645a24221..0954c9cd3 100644 --- a/reconstruction/dc/src/test/java/org/jlab/service/dc/DCReconstructionTest.java +++ b/reconstruction/dc/src/test/java/org/jlab/service/dc/DCReconstructionTest.java @@ -85,8 +85,8 @@ public void testDCReconstruction() { assertEquals(testEvent.getBank("TimeBasedTrkg::TBTracks").getFloat("Vtx0_x", 0) < 0.2, true); assertEquals(testEvent.getBank("TimeBasedTrkg::TBTracks").getFloat("Vtx0_x", 0) > -0.2, true); - assertEquals(testEvent.getBank("TimeBasedTrkg::TBTracks").getFloat("Vtx0_y", 0) < 0.18, true); - assertEquals(testEvent.getBank("TimeBasedTrkg::TBTracks").getFloat("Vtx0_y", 0) > -0.228, true); + assertEquals(testEvent.getBank("TimeBasedTrkg::TBTracks").getFloat("Vtx0_y", 0) < 0.5, true); + assertEquals(testEvent.getBank("TimeBasedTrkg::TBTracks").getFloat("Vtx0_y", 0) > -0.5, true); assertEquals(testEvent.getBank("TimeBasedTrkg::TBTracks").getFloat("Vtx0_z", 0) < 0.885, true); assertEquals(testEvent.getBank("TimeBasedTrkg::TBTracks").getFloat("Vtx0_z", 0) > -0.0753, true); From 1bcc0e6e01e56203d925a942d97911a9ab01a755 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 13 Mar 2024 10:18:41 -0400 Subject: [PATCH 54/69] update DC reconstruction pom file for version of tracking --- reconstruction/dc/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reconstruction/dc/pom.xml b/reconstruction/dc/pom.xml index 4456dd1ac..2ae1c6e2f 100644 --- a/reconstruction/dc/pom.xml +++ b/reconstruction/dc/pom.xml @@ -28,7 +28,7 @@ org.jlab.clas clas-tracking - 10.0.7-SNAPSHOT + 10.0.9-SNAPSHOT From 540aa85738267a186fd7d9b98a5c61a9c9edc343 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Wed, 20 Mar 2024 16:04:20 -0400 Subject: [PATCH 55/69] update configuration setup for options of DAF implementation and DAF parameters --- .../kalmanfilter/zReference/DAFilter.java | 10 +++-- .../kalmanfilter/zReference/KFitter.java | 41 ++++++++++--------- .../zReference/KFitterWithURWell.java | 37 +++++++++-------- .../java/org/jlab/service/dc/DCEngine.java | 21 ++++++++-- .../java/org/jlab/service/dc/DCTBEngine.java | 12 ++---- .../org/jlab/service/dc/DCURWellTBEngine.java | 22 +++------- 6 files changed, 73 insertions(+), 70 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java index c2084cfa2..52a4fd21a 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java @@ -9,7 +9,7 @@ */ public class DAFilter { - protected static double dafChi2Cut = 8; + private static double dafChi2Cut = 8; // For double hits private double[] docas_double; @@ -42,7 +42,11 @@ public DAFilter(double doca, double var, double weight) { this.doca_single = doca; this.var_single = var; this.weight_single = weight; - } + } + + public static void setDafChi2Cut(double chi2Cut){ + dafChi2Cut = chi2Cut; + } public void calc_effectiveDoca_doubleHits(){ if((wireLines_double[0] == wireLines_double[1]) || ((docas_double[0] == docas_double[1]) && !(docas_double[0] == 0 && docas_double[1] == 0))) { @@ -134,7 +138,7 @@ public double calc_updatedWeight_singleHit(double residual, double annealingFact double Lambda = factor * Math.exp(-0.5 / annealingFactor * dafChi2Cut); double sum = Phi + Lambda; double updatedWeight = Phi/sum; - + return updatedWeight; } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 172267b58..9b25f08ac 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -1,7 +1,8 @@ package org.jlab.clas.tracking.kalmanfilter.zReference; import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; +import java.util.List; import org.jlab.clas.clas.math.FastMath; import org.jlab.clas.swimtools.Swim; @@ -23,7 +24,7 @@ */ public class KFitter extends AKFitter { - private ArrayList dafAnnealingFactorsTB = new ArrayList<>(); + private static List dafAnnealingFactorsTB = new ArrayList<>(Arrays.asList(64., 16., 4., 1.)); private int dafAnnealingFactorsIndex = 0; private double dafAnnealingFactor = 1; @@ -65,6 +66,12 @@ public KFitter(boolean filter, int iterations, int dir, Swim swim, double Z[], L super(filter, iterations, dir, swim, mo); this.Z = Z; } + + public static void setDafAnnealingFactorsTB(String strDAFAnnealingFactorsTB){ + String strs[] = strDAFAnnealingFactorsTB.split(","); + for(int i = 0; i < strs.length; i++) + dafAnnealingFactorsTB.add(Double.valueOf(strs[i])); + } public final void init(List measSurfaces, StateVec initSV) { finalSmoothedStateVec = null; @@ -83,24 +90,13 @@ public final void init(List measSurfaces, StateVec initSV) { sv.init(initSV); sv.Z = Z; } + + public final void initFromHB(List measSurfaces, StateVec initSV, double beta, boolean useDAF) { + if(useDAF) initFromHB(measSurfaces, initSV, beta); + else initFromHBNoDAF(measSurfaces, initSV, beta); + } - public final void initFromHB(List measSurfaces, StateVec initSV, double beta, String strDAFChi2Cut, String strDAFAnnealingFactorsTB) { - if(strDAFChi2Cut != null){ - DAFilter.dafChi2Cut = Double.valueOf(strDAFChi2Cut); - } - - if(strDAFAnnealingFactorsTB != null){ - String strs[] = strDAFAnnealingFactorsTB.split(","); - for(int i = 0; i < strs.length; i++) - dafAnnealingFactorsTB.add(Double.valueOf(strs[i])); - } - else{ - dafAnnealingFactorsTB.add(64.); - dafAnnealingFactorsTB.add(16.); - dafAnnealingFactorsTB.add(4.); - dafAnnealingFactorsTB.add(1.); - } - + public final void initFromHB(List measSurfaces, StateVec initSV, double beta) { finalSmoothedStateVec = null; finalTransportedStateVec = null; this.NDF = -5; @@ -137,6 +133,11 @@ public final void initFromHBNoDAF(List measSurfaces, StateVec initSV, d sv.Z = Z; TBT = true; } + + public void runFitter(boolean useDAF) { + if(useDAF) runFitter(); + else runFitterNoDAF(); + } public void runFitter() { this.chi2 = Double.POSITIVE_INFINITY; @@ -255,7 +256,7 @@ public void runFitter() { dafAnnealingFactor = 1; dafAnnealingFactorsIndex++; } - + for (int k = 0; k < svzLength - 1; k++) { boolean forward = true; diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index 25fca39f8..dc60c6fb4 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -1,6 +1,7 @@ package org.jlab.clas.tracking.kalmanfilter.zReference; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.logging.Logger; import java.util.logging.Level; @@ -26,7 +27,7 @@ */ public class KFitterWithURWell extends AKFitter { - private ArrayList dafAnnealingFactorsTB = new ArrayList<>(); + private static List dafAnnealingFactorsTB = new ArrayList<>(Arrays.asList(64., 16., 4., 1.)); private int dafAnnealingFactorsIndex = 0; private double dafAnnealingFactor = 1; @@ -70,6 +71,12 @@ public KFitterWithURWell(boolean filter, int iterations, int dir, Swim swim, dou super(filter, iterations, dir, swim, mo); this.Z = Z; } + + public static void setDafAnnealingFactorsTB(String strDAFAnnealingFactorsTB){ + String strs[] = strDAFAnnealingFactorsTB.split(","); + for(int i = 0; i < strs.length; i++) + dafAnnealingFactorsTB.add(Double.valueOf(strs[i])); + } public final void init(List measSurfaces, StateVec initSV) { finalSmoothedStateVec = null; @@ -91,24 +98,13 @@ else if(mv.measurements.get(i).surface.type == Type.PLANEURWELL) sv.init(initSV); sv.Z = Z; } + + public final void initFromHB(List measSurfaces, StateVec initSV, double beta, boolean useDAF) { + if(useDAF) initFromHB(measSurfaces, initSV, beta); + else initFromHBNoDAF(measSurfaces, initSV, beta); + } - public final void initFromHB(List measSurfaces, StateVec initSV, double beta, String strDAFChi2Cut, String strDAFAnnealingFactorsTB) { - if(strDAFChi2Cut != null){ - DAFilter.dafChi2Cut = Double.valueOf(strDAFChi2Cut); - } - - if(strDAFAnnealingFactorsTB != null){ - String strs[] = strDAFAnnealingFactorsTB.split(","); - for(int i = 0; i < strs.length; i++) - dafAnnealingFactorsTB.add(Double.valueOf(strs[i])); - } - else{ - dafAnnealingFactorsTB.add(64.); - dafAnnealingFactorsTB.add(16.); - dafAnnealingFactorsTB.add(4.); - dafAnnealingFactorsTB.add(1.); - } - + public final void initFromHB(List measSurfaces, StateVec initSV, double beta) { finalSmoothedStateVec = null; finalTransportedStateVec = null; this.NDF = -5; @@ -152,6 +148,11 @@ else if(mv.measurements.get(i).surface.type == Type.PLANEURWELL) sv.Z = Z; TBT = true; } + + public void runFitter(boolean useDAF) { + if(useDAF) runFitter(); + else runFitterNoDAF(); + } public void runFitter() { this.chi2 = Double.POSITIVE_INFINITY; diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java index 7b15f082e..7073ac009 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java @@ -10,6 +10,9 @@ import org.jlab.io.base.DataEvent; import org.jlab.rec.dc.Constants; import org.jlab.rec.dc.banks.Banks; +import org.jlab.clas.tracking.kalmanfilter.zReference.KFitter; +import org.jlab.clas.tracking.kalmanfilter.zReference.KFitterWithURWell; +import org.jlab.clas.tracking.kalmanfilter.zReference.DAFilter; public class DCEngine extends ReconstructionEngine { @@ -32,8 +35,9 @@ public class DCEngine extends ReconstructionEngine { private String outBankPrefix = null; private double[][] shifts = new double[Constants.NREG][6]; protected boolean useDAF = true; - protected String dafChi2Cut = null; - protected String dafAnnealingFactorsTB = null; + private String dafChi2Cut = null; + private String dafAnnealingFactorsTB = null; + private String dafAnnealingFactorsTBWithURWell = null; public static final Logger LOGGER = Logger.getLogger(ReconstructionEngine.class.getName()); @@ -106,11 +110,20 @@ else if(this.getEngineConfigString("dcT2DFunc").equalsIgnoreCase("Polynomial")) if(this.getEngineConfigString("useDAF")!=null) useDAF=Boolean.valueOf(this.getEngineConfigString("useDAF")); - if(this.getEngineConfigString("dafChi2Cut")!=null) + if(this.getEngineConfigString("dafChi2Cut")!=null) { dafChi2Cut=this.getEngineConfigString("dafChi2Cut"); + DAFilter.setDafChi2Cut(Double.valueOf(dafChi2Cut)); + } - if(this.getEngineConfigString("dafAnnealingFactorsTB")!=null) + if(this.getEngineConfigString("dafAnnealingFactorsTB")!=null){ dafAnnealingFactorsTB=this.getEngineConfigString("dafAnnealingFactorsTB"); + KFitter.setDafAnnealingFactorsTB(dafAnnealingFactorsTB); + } + + if(this.getEngineConfigString("dafAnnealingFactorsTBWithURWell")!=null){ + dafAnnealingFactorsTBWithURWell=this.getEngineConfigString("dafAnnealingFactorsTBWithURWell"); + KFitterWithURWell.setDafAnnealingFactorsTB(dafAnnealingFactorsTBWithURWell); + } // Set geometry shifts for alignment code if(this.getEngineConfigString("alignmentShifts")!=null) { diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index 487a13800..26af04bf0 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -274,15 +274,9 @@ public boolean processDataEvent(DataEvent event) { StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); - if(useDAF){ - kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta(), dafChi2Cut, dafAnnealingFactorsTB); - kFZRef.runFitter(); - } - else{ - kFZRef.initFromHBNoDAF(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); - kFZRef.runFitterNoDAF(); - } - + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta(), useDAF); + kFZRef.runFitter(useDAF); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); StateVec fn = new StateVec(); diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index 1a21fed46..35de9c608 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -252,14 +252,9 @@ public boolean processDataEvent(DataEvent event) { StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); - if(useDAF){ - kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta(), dafChi2Cut, dafAnnealingFactorsTB); - kFZRef.runFitter(); - } - else{ - kFZRef.initFromHBNoDAF(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); - kFZRef.runFitterNoDAF(); - } + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta(), useDAF); + kFZRef.runFitter(useDAF); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); StateVec fn = new StateVec(); @@ -300,14 +295,9 @@ public boolean processDataEvent(DataEvent event) { StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); - if(useDAF){ - kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta(), dafChi2Cut, dafAnnealingFactorsTB); - kFZRef.runFitter(); - } - else{ - kFZRef.initFromHBNoDAF(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); - kFZRef.runFitterNoDAF(); - } + kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta(), useDAF); + kFZRef.runFitter(useDAF); + List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); StateVec fn = new StateVec(); From 2d9b8bb0b87435442ba9c9a47e768ffb60b4ea6e Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Mon, 25 Mar 2024 12:28:38 -0400 Subject: [PATCH 56/69] update for configuration of DAF annealing factor --- .../org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java | 1 + .../clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java | 1 + 2 files changed, 2 insertions(+) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 9b25f08ac..160422aa7 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -68,6 +68,7 @@ public KFitter(boolean filter, int iterations, int dir, Swim swim, double Z[], L } public static void setDafAnnealingFactorsTB(String strDAFAnnealingFactorsTB){ + dafAnnealingFactorsTB.clear(); String strs[] = strDAFAnnealingFactorsTB.split(","); for(int i = 0; i < strs.length; i++) dafAnnealingFactorsTB.add(Double.valueOf(strs[i])); diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index dc60c6fb4..1d5240de9 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -73,6 +73,7 @@ public KFitterWithURWell(boolean filter, int iterations, int dir, Swim swim, dou } public static void setDafAnnealingFactorsTB(String strDAFAnnealingFactorsTB){ + dafAnnealingFactorsTB.clear(); String strs[] = strDAFAnnealingFactorsTB.split(","); for(int i = 0; i < strs.length; i++) dafAnnealingFactorsTB.add(Double.valueOf(strs[i])); From 84f7422eeb2bfffa884a6193ad16e51e4bde927d Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Tue, 26 Mar 2024 17:18:16 -0400 Subject: [PATCH 57/69] update DC reconstruction pom file for version of tracking --- reconstruction/dc/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reconstruction/dc/pom.xml b/reconstruction/dc/pom.xml index 1347b9fbe..5a9f38b25 100644 --- a/reconstruction/dc/pom.xml +++ b/reconstruction/dc/pom.xml @@ -28,7 +28,7 @@ org.jlab.clas clas-tracking - 10.0.9-SNAPSHOT + 10.0.11-SNAPSHOT From 07cbf206b619aa8f4b768c28b8c44a7c82f8f3cd Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Tue, 26 Mar 2024 21:41:01 -0400 Subject: [PATCH 58/69] set lower limit for DAF weight to avoid overflow --- .../jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java index 52a4fd21a..2f5fae412 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java @@ -138,7 +138,8 @@ public double calc_updatedWeight_singleHit(double residual, double annealingFact double Lambda = factor * Math.exp(-0.5 / annealingFactor * dafChi2Cut); double sum = Phi + Lambda; double updatedWeight = Phi/sum; - + + if(updatedWeight < 1.e-100) updatedWeight = 1.e-100; return updatedWeight; } @@ -159,6 +160,7 @@ public double[] calc_updatedWeights_doubleHits(double[] residuals, double anneal double[] updatedWeights = {0.5, 0.5}; for(int i = 0; i < 2; i++){ updatedWeights[i] = Phi[i]/sum; + if(updatedWeights[i] < 1.e-100) updatedWeights[i] = 1.e-100; } return updatedWeights; From d4d09096bcd7549a2a9e7e74ab09ee8c1185a8af Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 28 Mar 2024 16:44:52 -0400 Subject: [PATCH 59/69] fine updates --- .../clas/tracking/kalmanfilter/AMeasVecs.java | 8 +- .../tracking/kalmanfilter/AStateVecs.java | 12 +-- .../clas/tracking/kalmanfilter/Surface.java | 47 ++++---- .../jlab/clas/tracking/kalmanfilter/Type.java | 2 +- .../kalmanfilter/zReference/Constants.java | 32 ++++++ .../kalmanfilter/zReference/KFitter.java | 61 +++++------ .../zReference/KFitterStraight.java | 28 +++-- .../zReference/KFitterWithURWell.java | 101 ++++++++---------- .../kalmanfilter/zReference/StateVecs.java | 27 ++--- .../tracking/utilities/RungeKuttaDoca.java | 3 +- .../main/java/cnuphys/swimZ/SwimZResult.java | 2 +- etc/bankdefs/hipo4/dc.json | 3 +- .../main/java/org/jlab/rec/dc/Constants.java | 20 ++-- .../org/jlab/rec/dc/banks/RecoBankWriter.java | 1 - .../java/org/jlab/rec/dc/hit/FittedHit.java | 11 +- .../rec/dc/track/TrackCandListFinder.java | 10 +- .../track/TrackCandListWithURWellFinder.java | 11 +- .../org/jlab/rec/dc/trajectory/StateVec.java | 12 +-- .../jlab/rec/dc/trajectory/Trajectory.java | 3 +- .../java/org/jlab/service/dc/DCTBEngine.java | 8 +- .../org/jlab/service/dc/DCURWellTBEngine.java | 9 +- 21 files changed, 200 insertions(+), 211 deletions(-) create mode 100644 common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/Constants.java diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java index e53527404..be2a42ba8 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AMeasVecs.java @@ -23,7 +23,7 @@ public abstract class AMeasVecs { public void setMeasVecs(List measSurfaces) { measurements = new ArrayList<>(); - if(measSurfaces.get(0).type != Type.LINEDOCA && measSurfaces.get(0).type != Type.PLANEURWELL) // Measurements from URWell and DC has been sorted + if(measSurfaces.get(0).type != Type.LINEDOCA && measSurfaces.get(0).type != Type.PLANEWITHPOINT) // Measurements from URWell and DC has been sorted Collections.sort(measSurfaces); for(int i = 0; i < measSurfaces.size(); i++) { MeasVec mvec = new MeasVec(); @@ -41,7 +41,7 @@ public void setMeasVecs(List measSurfaces) { mvec.superlayer = measSurfaces.get(i).getSuperLayer(); mvec.layer = measSurfaces.get(i).getLayer(); } - else if(measSurfaces.get(i).type == Type.PLANEURWELL){ + else if(measSurfaces.get(i).type == Type.PLANEWITHPOINT){ mvec.region = measSurfaces.get(i).region; mvec.layer = measSurfaces.get(i).getLayer(); mvec.sector = measSurfaces.get(i).getSector(); @@ -108,7 +108,7 @@ public double dh(int k, StateVec stateVec) { } if( this.measurements.get(stateVec.k).surface.type == Type.PLANEWITHPOINT || this.measurements.get(stateVec.k).surface.type == Type.CYLINDERWITHPOINT) { - Point3D p = new Point3D(this.measurements.get(stateVec.k).surface.refPoint); + Point3D p = new Point3D(this.measurements.get(stateVec.k).surface.measPoint); value = p.distance(stateVec.x, stateVec.y, stateVec.z); } if( this.measurements.get(stateVec.k).surface.type == Type.CYLINDERWITHARC) { @@ -188,7 +188,7 @@ public double h(int k, StateVec stateVec) { } if( this.measurements.get(stateVec.k).surface.type == Type.PLANEWITHPOINT || this.measurements.get(stateVec.k).surface.type == Type.CYLINDERWITHPOINT) { - Point3D p = new Point3D(this.measurements.get(stateVec.k).surface.refPoint); + Point3D p = new Point3D(this.measurements.get(stateVec.k).surface.measPoint); p.setZ(0); value = p.distance(stateVec.x, stateVec.y, 0); } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java index 272732019..fdd22507a 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java @@ -281,6 +281,7 @@ public void setWeightDAF_doubleHits(double[] weight){ this.weightDAF_double = weight; } + // Todo: move variables finalDAFWeight, _PathLength, hw, and h from AStateVecs to AMeasVecs private double finalDAFWeight = -999; public double getFinalDAFWeight(){ return finalDAFWeight; @@ -288,14 +289,6 @@ public double getFinalDAFWeight(){ public void setFinalDAFWeight(double weight){ this.finalDAFWeight = weight; } - - private int SorDHit = -1; // 1 : single hit; 0: double hit for DC - public int getSorDHit(){ - return SorDHit; - } - public void setSorDHit(int SorDHit){ - this.SorDHit = SorDHit; - } public double getPathLength() { return _PathLength; @@ -505,7 +498,8 @@ public Helix getHelix(double xref, double yref) { if (Math.abs(phi0) > Math.PI) phi0 -= Math.signum(phi0) * 2 * Math.PI; double tanDip = vec.tanL; double z0 = vec.z0 + vec.dz; - double omega = -turningSign / R; + //double omega = turningSign * KFitter.polarity / R; + double omega = Math.signum(vec.kappa) / R; double d0 = -vec.d_rho; Helix helix = new Helix(d0, phi0, omega, z0, tanDip, turningSign, bfield, xref, yref, units); diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java index a190d7123..59348bd03 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Surface.java @@ -20,7 +20,9 @@ public class Surface implements Comparable { public Type type; public Plane3D plane; - public Point3D refPoint; + public Point3D measPoint; + public Point3D measPoint_err; + // Todo: make a line to replace two end points public Point3D lineEndPoint1; public Point3D lineEndPoint2; public Point3D finitePlaneCorner1; @@ -41,24 +43,14 @@ public class Surface implements Comparable { public boolean passive = false; public double hemisphere = 1; - // For DC - public double z; - public double x; public double[] unc = new double[2]; - public double tilt; public double[] doca = new double[2]; - public double wireMaxSag; public Line3D[] wireLine = new Line3D[2]; - public boolean reject = false; public int region; public int superlayer; public int nMeas = 1; - // For URWell - public double y; - public double x_err; - public double y_err; - + public void setNMeas(int n) { nMeas = n; } @@ -69,33 +61,38 @@ public int getNMeas() { // For URWell public Surface(int sector, double x, double y, double z, double x_err, double y_err) { - type = Type.PLANEURWELL; + type = Type.PLANEWITHPOINT; this.sector = sector; - this.x = x; - this.y = y; - this.z = z; - this.x_err = x_err; - this.y_err = y_err; + Point3D point = new Point3D(x, y, z); + measPoint = point; + Point3D point_err = new Point3D(x_err, y_err, 0); + measPoint_err = point_err; + Material material_air = new Material("air", 0, 0, 0, 30400, 0, Units.CM); + Material material_argon = new Material("argon", 0, 0, 0, 14, 0, Units.CM); + materials.add(material_air); + materials.add(material_argon); } // For DC - public Surface(int region, double z, double x, double tilt, double wireMaxSag, double[] doca, double[] unc, double error, Line3D[] wireLine) { + public Surface(int region, double[] doca, double[] unc, double error, Line3D[] wireLine) { type = Type.LINEDOCA; this.region = region; - this.z = z; - this.x = x; - this.tilt = tilt; - this.wireMaxSag = wireMaxSag; this.doca = doca; this.unc = unc; this.error = error; this.wireLine = wireLine; + Point3D point = new Point3D(0, 0, wireLine[0].origin().z()); + this.measPoint = point; + Material material_air = new Material("air", 0, 0, 0, 30400, 0, Units.CM); + Material material_argon = new Material("argon", 0, 0, 0, 14, 0, Units.CM); + materials.add(material_air); + materials.add(material_argon); } public Surface(Plane3D plane3d, Point3D refrPoint, Point3D c1, Point3D c2, double accuracy) { type = Type.PLANEWITHPOINT; plane = plane3d; - refPoint = refrPoint; + measPoint = refrPoint; finitePlaneCorner1 = c1; finitePlaneCorner2 = c2; swimAccuracy = accuracy; @@ -134,7 +131,7 @@ public Surface(Cylindrical3D cylinder3d, Strip strp, double accuracy) { public Surface(Cylindrical3D cylinder3d, Point3D refrPoint, double accuracy) { type = Type.CYLINDERWITHPOINT; cylinder = cylinder3d; - refPoint = refrPoint; + measPoint = refrPoint; swimAccuracy = accuracy; } public Surface(Cylindrical3D cylinder3d, Point3D endPoint1, Point3D endPoint2, double accuracy) { diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java index b9de41cf1..3065ec092 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/Type.java @@ -8,7 +8,7 @@ public enum Type { UDF(-1), PLANEWITHPOINT(0), PLANEWITHLINE(1), PLANEWITHSTRIP(2), CYLINDERWITHPOINT(3), CYLINDERWITHLINE(4), CYLINDERWITHARC(5), - CYLINDERWITHSTRIP(6), LINE(7), LINEDOCA(8), PLANEURWELL(9); + CYLINDERWITHSTRIP(6), LINE(7), LINEDOCA(8); private final int value; Type(int value) { diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/Constants.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/Constants.java new file mode 100644 index 000000000..7eb9d2cfb --- /dev/null +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/Constants.java @@ -0,0 +1,32 @@ +package org.jlab.clas.tracking.kalmanfilter.zReference; + +import java.util.logging.Logger; + +/** + * Constants used in forward tracking + * + * author: Tongtong + */ +public class Constants { + + // private constructor for a singleton + private Constants() { + } + + + public static Logger LOGGER = Logger.getLogger(Constants.class.getName()); + + // CONSTATNS for TRANSFORMATION + public static final double ITERSTOPXHB = 1.2e-2; + public static final double ITERSTOPYHB = 1.4e-1; + public static final double ITERSTOPTXHB = 2.5e-4; + public static final double ITERSTOPTYHB = 1.0e-3; + public static final double ITERSTOPQHB = 1.6e-3; + + public static final double ITERSTOPXTB = 5.5e-5; + public static final double ITERSTOPYTB = 8.0e-4; + public static final double ITERSTOPTXTB = 2.1e-6; + public static final double ITERSTOPTYTB = 3.5e-6; + public static final double ITERSTOPQTB = 1.1e-5; + +} \ No newline at end of file diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index 160422aa7..e7702b070 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -328,19 +328,19 @@ public void runFitter() { if (this.setFitFailed == false) { if (this.finalStateVec != null) { if (!TBT) { - if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 1.6e-3 - && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 1.2e-2 - && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 1.4e-1 - && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 2.5e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.0e-3) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < Constants.ITERSTOPQHB + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < Constants.ITERSTOPXHB + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < Constants.ITERSTOPYHB + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < Constants.ITERSTOPTXHB + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < Constants.ITERSTOPYHB) { i = totNumIter; } } else { - if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 1.1e-5 - && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 5.5e-5 - && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 8.0e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 2.1e-6 - && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 3.5e-6) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < Constants.ITERSTOPQTB + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < Constants.ITERSTOPXTB + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < Constants.ITERSTOPYTB + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < Constants.ITERSTOPTXTB + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < Constants.ITERSTOPTYTB) { i = totNumIter; } } @@ -593,7 +593,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { double[] K = new double[5]; double V = effectiveVar; - double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[0]); + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.measPoint.z(), mVec.surface.wireLine[0]); Matrix CaInv = this.filterCovMat(H, sVec.CM, V); if (CaInv != null) { Matrix5x5.copy(CaInv, cMat); @@ -607,7 +607,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { + H[1] * cMat.get(j, 1)) / V; } - Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.measPoint.z()); double h = mv.hDoca(point, mVec.surface.wireLine[0]); c2 = (effectiveDoca - h) * (effectiveDoca - h) / V; @@ -623,7 +623,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { Q_filt = sVec.Q + K[4] * (effectiveDoca - h); - Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.z); + Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.measPoint.z()); double h0 = mv.hDoca(pointFiltered, mVec.surface.wireLine[0]); double residual = effectiveDoca - h0; @@ -645,7 +645,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { double[] K = new double[5]; double V = effectiveVar; - double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[indexReferenceWire]); + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.measPoint.z(), mVec.surface.wireLine[indexReferenceWire]); Matrix CaInv = this.filterCovMat(H, sVec.CM, V); if (CaInv != null) { Matrix5x5.copy(CaInv, cMat); @@ -659,7 +659,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { + H[1] * cMat.get(j, 1)) / V; } - Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.measPoint.z()); double h = mv.hDoca(point, mVec.surface.wireLine[indexReferenceWire]); c2 = (effectiveDoca - h) * (effectiveDoca - h) / V; @@ -675,7 +675,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { Q_filt = sVec.Q + K[4] * (effectiveDoca - h); - Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.z); + Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.measPoint.z()); double h0 = mv.hDoca(pointFiltered, mVec.surface.wireLine[0]); double h1 = mv.hDoca(pointFiltered, mVec.surface.wireLine[1]); double[] residuals = {mVec.surface.doca[0] - h0, mVec.surface.doca[1] - h1}; @@ -724,7 +724,7 @@ private boolean filter(int k, boolean forward) { double[] K = new double[5]; double V = mVec.surface.unc[0] * KFScale; - double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[0]); + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.measPoint.z(), mVec.surface.wireLine[0]); Matrix CaInv = this.filterCovMat(H, sVec.CM, V); Matrix cMat = new Matrix(); if (CaInv != null) { @@ -739,7 +739,7 @@ private boolean filter(int k, boolean forward) { + H[1] * cMat.get(j, 1)) / V; } - Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.measPoint.z()); double h = mv.hDoca(point, mVec.surface.wireLine[0]); double signMeas = 1; @@ -773,7 +773,7 @@ private boolean filter(int k, boolean forward) { if (mVec.surface.doca[1] != -99) { // now filter using the other Hit V = mVec.surface.unc[1] * KFScale; - H = mv.H(x_filt, y_filt, mVec.surface.z, + H = mv.H(x_filt, y_filt, mVec.surface.measPoint.z(), mVec.surface.wireLine[1]); CaInv = this.filterCovMat(H, cMat, V); if (CaInv != null) { @@ -789,7 +789,7 @@ private boolean filter(int k, boolean forward) { + H[1] * cMat.get(j, 1)) / V; } - Point3D point2 = new Point3D(x_filt, y_filt, mVec.surface.z); + Point3D point2 = new Point3D(x_filt, y_filt, mVec.surface.measPoint.z()); h = mv.hDoca(point2, mVec.surface.wireLine[1]); @@ -885,7 +885,7 @@ private void calcFinalChisq(int sector, boolean nofilter) { double V0 = mv.measurements.get(0).surface.unc[0]; - Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); + Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.measPoint.z()); double h0 = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); @@ -918,7 +918,7 @@ private void calcFinalChisq(int sector, boolean nofilter) { double V = mv.measurements.get(k1 + 1).surface.unc[0]; - point = new Point3D(sv.transported(forward).get(k1 + 1).x, sv.transported(forward).get(k1 + 1).y, mv.measurements.get(k1 + 1).surface.z); + point = new Point3D(sv.transported(forward).get(k1 + 1).x, sv.transported(forward).get(k1 + 1).y, mv.measurements.get(k1 + 1).surface.measPoint.z()); double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); svc = sv.transported(forward).get(k1 + 1); @@ -978,7 +978,7 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { path += svc.deltaPath; svc.setPathLength(path); - Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); + Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.measPoint.z()); if(mv.measurements.get(0).surface.doca[1] == -99) { StateVec sVecPreviousFiltered = sv.filtered(true).get(0); double daf_weight = 1; @@ -1000,7 +1000,6 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weight); - svc.setSorDHit(1); kfStateVecsAlongTrajectory.add(svc); } else{ @@ -1026,7 +1025,6 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weights[0]); - svc.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); @@ -1034,7 +1032,6 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); svc2.setFinalDAFWeight(daf_weights[1]); - svc2.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc2); } @@ -1050,7 +1047,7 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { path += svc.deltaPath; svc.setPathLength(path); - point = new Point3D(sv.transported(forward).get(k1 + 1).x, sv.transported(forward).get(k1 + 1).y, mv.measurements.get(k1 + 1).surface.z); + point = new Point3D(sv.transported(forward).get(k1 + 1).x, sv.transported(forward).get(k1 + 1).y, mv.measurements.get(k1 + 1).surface.measPoint.z()); if(mv.measurements.get(k1 + 1).surface.doca[1] == -99) { StateVec sVecPreviousFiltered = sv.filtered(true).get(k1 + 1); double daf_weight = 1; @@ -1072,7 +1069,6 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weight); - svc.setSorDHit(1); kfStateVecsAlongTrajectory.add(svc); } else{ @@ -1098,7 +1094,6 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weights[0]); - svc.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); @@ -1106,7 +1101,6 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); svc2.setFinalDAFWeight(daf_weights[1]); - svc2.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc2); } } @@ -1117,11 +1111,13 @@ public Matrix propagateToVtx(int sector, double Zf) { return sv.transport(sector, finalStateVec.k, Zf, finalStateVec, mv, this.getSwimmer()); } + //Todo: apply the common funciton to replace current function above @Override public void runFitter(AStateVecs sv, AMeasVecs mv) { throw new UnsupportedOperationException("Not supported yet."); } + //Todo: apply the common funciton to replace current function above @Override public StateVec filter(int k, StateVec vec, AMeasVecs mv) { throw new UnsupportedOperationException("Not supported yet."); @@ -1154,12 +1150,11 @@ public void printlnMeasVecs() { org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec measvec = mv.measurements.get(i); String s = String.format("k=%d region=%d superlayer=%d layer=%d error=%.4f", measvec.k, measvec.region, measvec.superlayer, measvec.layer, measvec.error); - s += String.format(" Surface: index=%d x=%.4f z=%.4f tilt=%.4f wireMaxSag=%.4f", measvec.surface.getIndex(), - measvec.surface.x, measvec.surface.z, measvec.surface.tilt, measvec.surface.wireMaxSag, measvec.surface.unc[1]); + s += String.format(" Surface: index=%d ", measvec.surface.getIndex()); s += String.format( " Surface line 0: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", measvec.surface.doca[0], measvec.surface.unc[0], measvec.surface.wireLine[0].origin().x(), - measvec.surface.wireLine[0].origin().y(), measvec.surface.wireLine[0].origin().z(), + measvec.surface.wireLine[0].origin().y(), measvec.surface.measPoint.z(), measvec.surface.wireLine[0].end().x(), measvec.surface.wireLine[0].end().y(), measvec.surface.wireLine[0].end().z()); if (measvec.surface.wireLine[1] != null) { diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterStraight.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterStraight.java index 5f47479ca..232bcaa7d 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterStraight.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterStraight.java @@ -219,11 +219,11 @@ public void runFitter() { } if (this.setFitFailed == false) { if (this.finalStateVec != null) { - if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 5.e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 1.e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 1.e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 1.e-6 - && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.e-6) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < Constants.ITERSTOPQTB + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < Constants.ITERSTOPXTB + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < Constants.ITERSTOPYTB + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < Constants.ITERSTOPTXTB + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < Constants.ITERSTOPTYTB) { i = totNumIter; } } @@ -273,7 +273,7 @@ private boolean filter(int k, boolean forward) { double[] K = new double[5]; double V = mVec.surface.unc[0] * KFScale; - double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[0]); + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.measPoint.z(), mVec.surface.wireLine[0]); Matrix CaInv = this.filterCovMat(H, sVec.CM, V); Matrix cMat = new Matrix(); if (CaInv != null) { @@ -288,7 +288,7 @@ private boolean filter(int k, boolean forward) { + H[1] * cMat.get(j, 1)) / V; } - Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.measPoint.z()); double h = mv.hDoca(point, mVec.surface.wireLine[0]); double signMeas = 1; @@ -322,8 +322,7 @@ private boolean filter(int k, boolean forward) { if (mVec.surface.doca[1] != -99) { // now filter using the other Hit V = mVec.surface.unc[1] * KFScale; - H = mv.H(x_filt, y_filt, mVec.surface.z, - mVec.surface.wireLine[1]); + H = mv.H(x_filt, y_filt, mVec.surface.measPoint.z(), mVec.surface.wireLine[1]); CaInv = this.filterCovMat(H, cMat, V); if (CaInv != null) { for (int i = 0; i < 5; i++) { @@ -338,7 +337,7 @@ private boolean filter(int k, boolean forward) { + H[1] * cMat.get(j, 1)) / V; } - Point3D point2 = new Point3D(x_filt, y_filt, mVec.surface.z); + Point3D point2 = new Point3D(x_filt, y_filt, mVec.surface.measPoint.z()); h = mv.hDoca(point2, mVec.surface.wireLine[1]); @@ -436,7 +435,7 @@ private void calcFinalChisq(int sector, boolean nofilter) { double V0 = mv.measurements.get(0).surface.unc[0]; - Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); + Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.measPoint.z()); double h0 = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); @@ -470,7 +469,7 @@ private void calcFinalChisq(int sector, boolean nofilter) { double V = mv.measurements.get(k1 + 1).surface.unc[0]; - point = new Point3D(sv.transported(forward).get(k1+1).x, sv.transported(forward).get(k1+1).y, mv.measurements.get(k1+1).surface.z); + point = new Point3D(sv.transported(forward).get(k1+1).x, sv.transported(forward).get(k1+1).y, mv.measurements.get(k1+1).surface.measPoint.z()); double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); svc = sv.transported(forward).get(k1+1); @@ -538,12 +537,11 @@ public void printlnMeasVecs() { org.jlab.clas.tracking.kalmanfilter.AMeasVecs.MeasVec measvec = mv.measurements.get(i); String s = String.format("k=%d region=%d superlayer=%d layer=%d error=%.4f", measvec.k, measvec.region, measvec.superlayer, measvec.layer, measvec.error); - s += String.format(" Surface: index=%d x=%.4f z=%.4f tilt=%.4f wireMaxSag=%.4f", measvec.surface.getIndex(), - measvec.surface.x, measvec.surface.z, measvec.surface.tilt, measvec.surface.wireMaxSag, measvec.surface.unc[1]); + s += String.format(" Surface: index=%d", measvec.surface.getIndex()); s += String.format( " Surface line 0: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", measvec.surface.doca[0], measvec.surface.unc[0], measvec.surface.wireLine[0].origin().x(), - measvec.surface.wireLine[0].origin().y(), measvec.surface.wireLine[0].origin().z(), + measvec.surface.wireLine[0].origin().y(), measvec.surface.measPoint.z(), measvec.surface.wireLine[0].end().x(), measvec.surface.wireLine[0].end().y(), measvec.surface.wireLine[0].end().z()); if (measvec.surface.wireLine[1] != null) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index 1d5240de9..36eafe9be 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -91,7 +91,7 @@ public final void init(List measSurfaces, StateVec initSV) { if (mv.measurements.get(i).skip == false) { if(mv.measurements.get(i).surface.type == Type.LINEDOCA) this.NDF += mv.measurements.get(i).surface.getNMeas(); - else if(mv.measurements.get(i).surface.type == Type.PLANEURWELL) + else if(mv.measurements.get(i).surface.type == Type.PLANEWITHPOINT) this.NDF += 2; } } @@ -117,7 +117,7 @@ public final void initFromHB(List measSurfaces, StateVec initSV, double if (mv.measurements.get(i).skip == false) { if(mv.measurements.get(i).surface.type == Type.LINEDOCA) this.NDF++; - else if(mv.measurements.get(i).surface.type == Type.PLANEURWELL) + else if(mv.measurements.get(i).surface.type == Type.PLANEWITHPOINT) this.NDF += 2; } } @@ -140,7 +140,7 @@ public final void initFromHBNoDAF(List measSurfaces, StateVec initSV, d if (mv.measurements.get(i).skip == false) { if(mv.measurements.get(i).surface.type == Type.LINEDOCA) this.NDF += mv.measurements.get(i).surface.getNMeas(); - else if(mv.measurements.get(i).surface.type == Type.PLANEURWELL) + else if(mv.measurements.get(i).surface.type == Type.PLANEWITHPOINT) this.NDF += 2; } } @@ -345,19 +345,19 @@ public void runFitter() { if (this.setFitFailed == false) { if (this.finalStateVec != null) { if (!TBT) { - if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 1.6e-3 - && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 1.2e-2 - && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 1.4e-1 - && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 2.5e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 1.0e-3) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < Constants.ITERSTOPQHB + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < Constants.ITERSTOPXHB + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < Constants.ITERSTOPYHB + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < Constants.ITERSTOPTXHB + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < Constants.ITERSTOPYHB) { i = totNumIter; } } else { - if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < 1.1e-5 - && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < 5.5e-5 - && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < 8.0e-4 - && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < 2.1e-6 - && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < 3.5e-6) { + if (Math.abs(sv.trackTrajF.get(svzLength - 1).Q - this.finalStateVec.Q) < Constants.ITERSTOPQTB + && Math.abs(sv.trackTrajF.get(svzLength - 1).x - this.finalStateVec.x) < Constants.ITERSTOPXTB + && Math.abs(sv.trackTrajF.get(svzLength - 1).y - this.finalStateVec.y) < Constants.ITERSTOPYTB + && Math.abs(sv.trackTrajF.get(svzLength - 1).tx - this.finalStateVec.tx) < Constants.ITERSTOPTXTB + && Math.abs(sv.trackTrajF.get(svzLength - 1).ty - this.finalStateVec.ty) < Constants.ITERSTOPTYTB) { i = totNumIter; } } @@ -616,7 +616,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { double[] K = new double[5]; double V = effectiveVar; - double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[0]); + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.measPoint.z(), mVec.surface.wireLine[0]); Matrix CaInv = this.filterCovMat(H, sVec.CM, V); if (CaInv != null) { Matrix5x5.copy(CaInv, cMat); @@ -630,7 +630,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { + H[1] * cMat.get(j, 1)) / V; } - Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.measPoint.z()); double h = mv.hDoca(point, mVec.surface.wireLine[0]); @@ -656,7 +656,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { Q_filt = sVec.Q + K[4] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); - Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.z); + Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.measPoint.z()); double h0 = mv.hDoca(pointFiltered, mVec.surface.wireLine[0]); if (Math.abs(effectiveDoca) < 0.5) { signMeas = Math.signum(h0); @@ -684,7 +684,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { double[] K = new double[5]; double V = effectiveVar; - double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[indexReferenceWire]); + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.measPoint.z(), mVec.surface.wireLine[indexReferenceWire]); Matrix CaInv = this.filterCovMat(H, sVec.CM, V); if (CaInv != null) { Matrix5x5.copy(CaInv, cMat); @@ -698,7 +698,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { + H[1] * cMat.get(j, 1)) / V; } - Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.measPoint.z()); double h = mv.hDoca(point, mVec.surface.wireLine[indexReferenceWire]); signMeas = Math.signum(effectiveDoca); @@ -718,7 +718,7 @@ private boolean filter(int k, boolean forward, double annealingFactor) { Q_filt = sVec.Q + K[4] * (signMeas * Math.abs(effectiveDoca) - sign * Math.abs(h)); - Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.z); + Point3D pointFiltered = new Point3D(x_filt, y_filt, mVec.surface.measPoint.z()); double h0 = mv.hDoca(pointFiltered, mVec.surface.wireLine[0]); double h1 = mv.hDoca(pointFiltered, mVec.surface.wireLine[1]); double[] residuals = {mVec.surface.doca[0] - h0, mVec.surface.doca[1] - h1}; @@ -747,10 +747,10 @@ private boolean filter(int k, boolean forward, double annealingFactor) { } } - else if (mVec.surface.type == Type.PLANEURWELL) { + else if (mVec.surface.type == Type.PLANEWITHPOINT) { - double V[][] = {{mVec.surface.x_err * mVec.surface.x_err, 0}, {0, mVec.surface.y_err * mVec.surface.y_err}}; + double V[][] = {{mVec.surface.measPoint_err.x() * mVec.surface.measPoint_err.x(), 0}, {0, mVec.surface.measPoint_err.y() * mVec.surface.measPoint_err.y()}}; if(V[0][0] == 0 || V[1][1] == 0 ) LOGGER.log(Level.SEVERE, "Resolution for URWell is 0."); @@ -759,7 +759,7 @@ else if (mVec.surface.type == Type.PLANEURWELL) { if (cMat == null) return false; double[][] K = KURWell(cMat, V); - double [] res = {mVec.surface.x - sVec.x, mVec.surface.y - sVec.y}; + double [] res = {mVec.surface.measPoint.x() - sVec.x, mVec.surface.measPoint.y() - sVec.y}; double filt[] = new double[5]; for(int i = 0; i < 5; i ++){ @@ -817,7 +817,7 @@ private boolean filter(int k, boolean forward) { double[] K = new double[5]; double V = mVec.surface.unc[0] * KFScale; - double[] H = mv.H(sVec.x, sVec.y, mVec.surface.z, mVec.surface.wireLine[0]); + double[] H = mv.H(sVec.x, sVec.y, mVec.surface.measPoint.z(), mVec.surface.wireLine[0]); Matrix CaInv = this.filterCovMat(H, sVec.CM, V); Matrix cMat = new Matrix(); if (CaInv != null) { @@ -832,7 +832,7 @@ private boolean filter(int k, boolean forward) { + H[1] * cMat.get(j, 1)) / V; } - Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.z); + Point3D point = new Point3D(sVec.x, sVec.y, mVec.surface.measPoint.z()); double h = mv.hDoca(point, mVec.surface.wireLine[0]); double signMeas = 1; @@ -866,7 +866,7 @@ private boolean filter(int k, boolean forward) { if (mVec.surface.doca[1] != -99) { // now filter using the other Hit V = mVec.surface.unc[1] * KFScale; - H = mv.H(x_filt, y_filt, mVec.surface.z, + H = mv.H(x_filt, y_filt, mVec.surface.measPoint.z(), mVec.surface.wireLine[1]); CaInv = this.filterCovMat(H, cMat, V); if (CaInv != null) { @@ -882,7 +882,7 @@ private boolean filter(int k, boolean forward) { + H[1] * cMat.get(j, 1)) / V; } - Point3D point2 = new Point3D(x_filt, y_filt, mVec.surface.z); + Point3D point2 = new Point3D(x_filt, y_filt, mVec.surface.measPoint.z()); h = mv.hDoca(point2, mVec.surface.wireLine[1]); @@ -919,10 +919,10 @@ private boolean filter(int k, boolean forward) { } } - else if (mVec.surface.type == Type.PLANEURWELL) { + else if (mVec.surface.type == Type.PLANEWITHPOINT) { - double V[][] = {{mVec.surface.x_err * mVec.surface.x_err, 0}, {0, mVec.surface.y_err * mVec.surface.y_err}}; + double V[][] = {{mVec.surface.measPoint_err.x() * mVec.surface.measPoint_err.x(), 0}, {0, mVec.surface.measPoint_err.y() * mVec.surface.measPoint_err.y()}}; if(V[0][0] == 0 || V[1][1] == 0 ) LOGGER.log(Level.SEVERE, "Resolution for URWell is 0."); @@ -931,7 +931,7 @@ else if (mVec.surface.type == Type.PLANEURWELL) { if (cMat == null) return false; double[][] K = KURWell(cMat, V); - double [] res = {mVec.surface.x - sVec.x, mVec.surface.y - sVec.y}; + double [] res = {mVec.surface.measPoint.x() - sVec.x, mVec.surface.measPoint.y() - sVec.y}; double filt[] = new double[5]; for(int i = 0; i < 5; i ++){ @@ -1056,14 +1056,14 @@ private void calcFinalChisq(int sector, boolean nofilter) { path += svc.deltaPath; svc.setPathLength(path); - if (mv.measurements.get(0).surface.type == Type.PLANEURWELL) { + if (mv.measurements.get(0).surface.type == Type.PLANEWITHPOINT) { kfStateVecsAlongTrajectory.add(svc); - double x_err = mv.measurements.get(0).surface.x_err; - double y_err = mv.measurements.get(0).surface.y_err; + double x_err = mv.measurements.get(0).surface.measPoint_err.x(); + double y_err = mv.measurements.get(0).surface.measPoint_err.y(); - double x_res = mv.measurements.get(0).surface.x - svc.x; - double y_res = mv.measurements.get(0).surface.y - svc.y; + double x_res = mv.measurements.get(0).surface.measPoint.x() - svc.x; + double y_res = mv.measurements.get(0).surface.measPoint.y() - svc.y; chi2 += (x_res*x_res) / (x_err*x_err); chi2 += (y_res*y_res) / (y_err*y_err); @@ -1074,7 +1074,7 @@ private void calcFinalChisq(int sector, boolean nofilter) { else if(mv.measurements.get(0).surface.type == Type.LINEDOCA) { double V0 = mv.measurements.get(0).surface.unc[0]; - Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); + Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.measPoint.z()); double h0 = mv.hDoca(point, mv.measurements.get(0).surface.wireLine[0]); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); @@ -1108,7 +1108,7 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA) { double V = mv.measurements.get(k1 + 1).surface.unc[0]; - Point3D point = new Point3D(sv.transported(forward).get(k1 + 1).x, sv.transported(forward).get(k1 + 1).y, mv.measurements.get(k1 + 1).surface.z); + Point3D point = new Point3D(sv.transported(forward).get(k1 + 1).x, sv.transported(forward).get(k1 + 1).y, mv.measurements.get(k1 + 1).surface.measPoint.z()); double h = mv.hDoca(point, mv.measurements.get(k1 + 1).surface.wireLine[0]); svc = sv.transported(forward).get(k1 + 1); @@ -1167,19 +1167,19 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { path += svc.deltaPath; svc.setPathLength(path); - if(mv.measurements.get(0).surface.type == Type.PLANEURWELL){ - double x_err = mv.measurements.get(0).surface.x_err; - double y_err = mv.measurements.get(0).surface.y_err; + if(mv.measurements.get(0).surface.type == Type.PLANEWITHPOINT){ + double x_err = mv.measurements.get(0).surface.measPoint_err.x(); + double y_err = mv.measurements.get(0).surface.measPoint_err.y(); - double x_res = mv.measurements.get(0).surface.x - svc.x; - double y_res = mv.measurements.get(0).surface.y - svc.y; + double x_res = mv.measurements.get(0).surface.measPoint.x() - svc.x; + double y_res = mv.measurements.get(0).surface.measPoint.y() - svc.y; chi2 += (x_res*x_res) / (x_err*x_err); chi2 += (y_res*y_res) / (y_err*y_err); ndfDAF += 2; } else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ - Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.z); + Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.measPoint.z()); if(mv.measurements.get(0).surface.doca[1] == -99) { StateVec sVecPreviousFiltered = sv.filtered(true).get(0); double daf_weight = 1; @@ -1201,7 +1201,6 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weight); - svc.setSorDHit(1); kfStateVecsAlongTrajectory.add(svc); } else{ @@ -1227,7 +1226,6 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weights[0]); - svc.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); @@ -1235,7 +1233,6 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); svc2.setFinalDAFWeight(daf_weights[1]); - svc2.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc2); } } @@ -1252,7 +1249,7 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ path += svc.deltaPath; svc.setPathLength(path); - Point3D point = new Point3D(sv.transported(forward).get(k1 + 1).x, sv.transported(forward).get(k1 + 1).y, mv.measurements.get(k1 + 1).surface.z); + Point3D point = new Point3D(sv.transported(forward).get(k1 + 1).x, sv.transported(forward).get(k1 + 1).y, mv.measurements.get(k1 + 1).surface.measPoint.z()); if(mv.measurements.get(k1 + 1).surface.doca[1] == -99) { StateVec sVecPreviousFiltered = sv.filtered(true).get(k1 + 1); double daf_weight = 1; @@ -1274,7 +1271,6 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weight); - svc.setSorDHit(1); kfStateVecsAlongTrajectory.add(svc); } else{ @@ -1300,7 +1296,6 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weights[0]); - svc.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); @@ -1308,7 +1303,6 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); svc2.setFinalDAFWeight(daf_weights[1]); - svc2.setSorDHit(0); kfStateVecsAlongTrajectory.add(svc2); } } @@ -1357,12 +1351,11 @@ public void printlnMeasVecs() { if(measvec.surface.type == Type.LINEDOCA){ String s = String.format("k=%d region=%d superlayer=%d layer=%d error=%.4f", measvec.k, measvec.region, measvec.superlayer, measvec.layer, measvec.error); - s += String.format(" Surface: index=%d x=%.4f z=%.4f tilt=%.4f wireMaxSag=%.4f", measvec.surface.getIndex(), - measvec.surface.x, measvec.surface.z, measvec.surface.tilt, measvec.surface.wireMaxSag, measvec.surface.unc[1]); + s += String.format(" Surface: index=%d", measvec.surface.getIndex()); s += String.format( " Surface line 0: doca=%.4f unc=%.4f origin_x =%.4f, origin_y =%.4f, origin_z =%.4f, end_x=%.4f, end_y=%.4f, end_z=%.4f", measvec.surface.doca[0], measvec.surface.unc[0], measvec.surface.wireLine[0].origin().x(), - measvec.surface.wireLine[0].origin().y(), measvec.surface.wireLine[0].origin().z(), + measvec.surface.wireLine[0].origin().y(), measvec.surface.measPoint.z(), measvec.surface.wireLine[0].end().x(), measvec.surface.wireLine[0].end().y(), measvec.surface.wireLine[0].end().z()); if (measvec.surface.wireLine[1] != null) { @@ -1376,11 +1369,11 @@ public void printlnMeasVecs() { System.out.println(s); } - else if(measvec.surface.type == Type.PLANEURWELL){ + else if(measvec.surface.type == Type.PLANEWITHPOINT){ String s = String.format("k=%d region=%d superlayer=%d layer=%d", measvec.k, measvec.region, measvec.superlayer, measvec.layer); s += String.format(" Surface: index=%d x=%.4f y=%.4f z=%.4f x_err=%.4f y_err=%.4f", measvec.surface.getIndex(), - measvec.surface.x, measvec.surface.y, measvec.surface.z, measvec.surface.x_err, measvec.surface.y_err); + measvec.surface.measPoint.x(), measvec.surface.measPoint.y(), measvec.surface.measPoint.z(), measvec.surface.measPoint_err.x(), measvec.surface.measPoint_err.y()); System.out.println(s); } } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/StateVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/StateVecs.java index b48790705..f69dd0bfc 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/StateVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/StateVecs.java @@ -27,10 +27,6 @@ public class StateVecs extends AStateVecs { private final Matrix fMS = new Matrix(); private final Matrix copyMatrix = new Matrix(); - final double ARGONRADLEN = 14; // radiation length in Argon is 14 cm - - final double AIRRADLEN = 30400; // radiation length in cm - public double Z[]; private double beta = 1.0; // beta depends on mass hypothesis @@ -80,7 +76,7 @@ public Matrix transport(int sector, int i, double Zf, StateVec iVec, AMeasVecs m Matrix5x5.copy(iVec.CM, fVec.CM); double s = 0; - double zInit = mv.measurements.get(i).surface.z; + double zInit = mv.measurements.get(i).surface.measPoint.z(); double BatMeas = iVec.B; double z = zInit; @@ -112,7 +108,7 @@ public Matrix transport(int sector, int i, double Zf, StateVec iVec, AMeasVecs m // Q process noise matrix estimate double p = Math.abs(1. / iVec.Q); - double X0 = this.getX0(z, Z); + double X0 = this.getX0(mv.measurements.get(i).surface, z, Z); double t_ov_X0 = Math.abs(s) / X0;//path length in radiation length units = t/X0 [true path length/ X0] ; Ar radiation length = 14 cm double beta = this.beta; @@ -122,6 +118,9 @@ public Matrix transport(int sector, int i, double Zf, StateVec iVec, AMeasVecs m double sctRMS = 0; + ////// Todo: Modify multi-scattering or remove it; After update, some parameters, like iteration termintion chonditions, may need to be updated. + // Speed of light should be 1 + // From one measurement site to another, F and Q should be calculated separaetely with multiple steps; and then C' = FTCF + Q if (Math.abs(s) > 0) { sctRMS = ((0.0136) / (beta * PhysicsConstants.speedOfLight() * p)) * Math.sqrt(t_ov_X0) * (1 + 0.038 * Math.log(t_ov_X0)); @@ -177,11 +176,11 @@ public boolean transport(int sector, int i, int f, StateVec iVec, AMeasVecs mv, Matrix5x5.copy(iVec.CM, fVec.CM); double s = 0; - double zInit = mv.measurements.get(i).surface.z; + double zInit = mv.measurements.get(i).surface.measPoint.z(); double BatMeas = iVec.B; double z = zInit; - double zFinal = mv.measurements.get(f).surface.z; + double zFinal = mv.measurements.get(f).surface.measPoint.z(); while (Math.signum(zFinal - zInit) * z < Math.signum(zFinal - zInit) * zFinal) { z = fVec.z; @@ -210,7 +209,7 @@ public boolean transport(int sector, int i, int f, StateVec iVec, AMeasVecs mv, // Q process noise matrix estimate double p = Math.abs(1. / iVec.Q); - double X0 = this.getX0(z, Z); + double X0 = this.getX0(mv.measurements.get(i).surface, z, Z); double t_ov_X0 = Math.abs(s) / X0;//path length in radiation length units = t/X0 [true path length/ X0] ; Ar radiation length = 14 cm double beta = this.beta; @@ -220,6 +219,9 @@ public boolean transport(int sector, int i, int f, StateVec iVec, AMeasVecs mv, double sctRMS = 0; + ////// Todo: Modify multi-scattering or remove it; After update, some parameters, like iteration termintion chonditions, may need to be updated. + // Speed of light should be 1 + // From one measurement site to another, F and Q should be calculated separaetely with multiple steps; and then C' = FTCF + Q if (Math.abs(s) > 0) { sctRMS = ((0.0136) / (beta * PhysicsConstants.speedOfLight() * p)) * Math.sqrt(t_ov_X0) * (1 + 0.038 * Math.log(t_ov_X0)); @@ -261,8 +263,9 @@ public boolean transport(int sector, int i, int f, StateVec iVec, AMeasVecs mv, } - public double getX0(double z, double Z[]) { - double X0 = AIRRADLEN; + //Todo: update it when updating multi-scattering + public double getX0(Surface surface, double z, double Z[]) { + double X0 = surface.getMaterials().get(0).getX0(); double tolerance = 0.01; for (int i = 1; i < Z.length; i++) { @@ -270,7 +273,7 @@ public double getX0(double z, double Z[]) { continue; } if (z >= Z[i] - tolerance && z <= Z[i + 1] + tolerance) { - return ARGONRADLEN; + return surface.getMaterials().get(1).getX0(); } } return X0; diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/utilities/RungeKuttaDoca.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/utilities/RungeKuttaDoca.java index 1e6d4dd3d..47129534d 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/utilities/RungeKuttaDoca.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/utilities/RungeKuttaDoca.java @@ -1,5 +1,6 @@ package org.jlab.clas.tracking.utilities; +import org.jlab.clas.pdg.PhysicsConstants; import org.jlab.jnp.matrix.*; import org.jlab.clas.swimtools.Swim; import org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec;; @@ -12,7 +13,7 @@ */ public class RungeKuttaDoca { private final float[] _b = new float[3]; - final double v = 0.0029979245; + final double v = PhysicsConstants.speedOfLight() * 0.0001; public RungeKuttaDoca() {} diff --git a/common-tools/cnuphys/swimmer/src/main/java/cnuphys/swimZ/SwimZResult.java b/common-tools/cnuphys/swimmer/src/main/java/cnuphys/swimZ/SwimZResult.java index 3e450f612..38fcff515 100644 --- a/common-tools/cnuphys/swimmer/src/main/java/cnuphys/swimZ/SwimZResult.java +++ b/common-tools/cnuphys/swimmer/src/main/java/cnuphys/swimZ/SwimZResult.java @@ -211,7 +211,7 @@ public double sectorGetBDL(int sector, FieldProbe probe) { } /** - * Get the approximate integral |B x dL| + * Get the approximate integral |B x dL| in the middle plane * * @param sector sector 1..6 * @param probe the probe use to compute this result trajectory diff --git a/etc/bankdefs/hipo4/dc.json b/etc/bankdefs/hipo4/dc.json index 2a9534f19..eced81dfb 100644 --- a/etc/bankdefs/hipo4/dc.json +++ b/etc/bankdefs/hipo4/dc.json @@ -362,8 +362,7 @@ {"name":"trkDoca", "type":"F", "info":"track doca of the hit (in cm)"}, {"name":"timeResidual", "type":"F", "info":"time residual of the hit (in cm)"}, {"name":"fitResidual", "type":"F", "info":"fit residual of the hit (in cm, from KF)"}, - {"name":"DAFWeight", "type":"F", "info":"Weight by DAF"}, - {"name":"SorD", "type":"B", "info":"1: single hit; 0: double hit"}, + {"name":"DAFWeight", "type":"F", "info":"Weight by DAF; The first bit of status points out a hit is belong to singlet or doublet"}, {"name":"LR", "type":"B", "info":"Left/Right ambiguity of the hit"}, {"name":"X", "type":"F", "info":"wire x-coordinate in tilted-sector"}, {"name":"Z", "type":"F", "info":"wire z-coordinate in tilted-sector"}, diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java index 8e2a8a376..888f05c82 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/Constants.java @@ -17,7 +17,6 @@ import org.jlab.geom.base.Detector; import org.jlab.rec.dc.trajectory.TrajectorySurfaces; import org.jlab.utils.groups.IndexedTable; -import org.jlab.clas.clas.math.FastMath; import org.jlab.detector.banks.RawBank.OrderType; /** @@ -52,18 +51,18 @@ public static Constants getInstance() { public static boolean DEBUG = false; // CONSTATNS for TRANSFORMATION - public static final double SIN25 = FastMath.sin(Math.toRadians(25.)); - public static final double COS25 = FastMath.cos(Math.toRadians(25.)); - public static final double COS30 = FastMath.cos(Math.toRadians(30.)); - public static final double SIN6 = FastMath.sin(Math.toRadians(6.)); - public static final double COS6 = FastMath.cos(Math.toRadians(6.)); + public static final double SIN25 = Math.sin(Math.toRadians(25.)); + public static final double COS25 = Math.cos(Math.toRadians(25.)); + public static final double COS30 = Math.cos(Math.toRadians(30.)); + public static final double SIN6 = Math.sin(Math.toRadians(6.)); + public static final double COS6 = Math.cos(Math.toRadians(6.)); public static final double TAN6 = Math.tan(Math.toRadians(6.)); public static final double CTAN6 = 1/TAN6; - public static final double[] SINSECTOR60 = {0, FastMath.sin(Math.toRadians(60.)), FastMath.sin(Math.toRadians(120.)), 0, - FastMath.sin(Math.toRadians(240.)), FastMath.sin(Math.toRadians(300.))}; + public static final double[] SINSECTOR60 = {0, Math.sin(Math.toRadians(60.)), Math.sin(Math.toRadians(120.)), 0, + Math.sin(Math.toRadians(240.)), Math.sin(Math.toRadians(300.))}; public static final double[] COSSECTOR60 = {1, 0.5, -0.5, -1, -0.5, 0.5}; - public static final double[] SINSECTORNEG60 = {0, FastMath.sin(Math.toRadians(-60.)), FastMath.sin(Math.toRadians(-120.)), 0, - FastMath.sin(Math.toRadians(-240.)), FastMath.sin(Math.toRadians(-300.))}; + public static final double[] SINSECTORNEG60 = {0, Math.sin(Math.toRadians(-60.)), Math.sin(Math.toRadians(-120.)), 0, + Math.sin(Math.toRadians(-240.)), Math.sin(Math.toRadians(-300.))}; public static final double[] COSSECTORNEG60 = {1, 0.5, -0.5, -1, -0.5, 0.5}; ////////////// Uncertainties for initial state @@ -192,6 +191,7 @@ public static Constants getInstance() { public static final double HTCCRADIUS = 175; public static final double LTCCPLANE = 653.09; + //Todo: Should come from uRWell geometry package public static final double URWELLLOCALZ = 226.0464; // cm public static final double URWELLXRESOLUTION = 0.01; // cm public static final double URWELLYRESOLUTION = 0.04; // cm diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java index 1cbe11119..c6f858054 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java @@ -548,7 +548,6 @@ private DataBank fillTBHitsBank(DataEvent event, List hitlist) { bank.setByte("trkID", i, (byte) hitlist.get(i).get_AssociatedTBTrackID()); bank.setFloat("timeResidual", i, (float) hitlist.get(i).get_TimeResidual()); bank.setFloat("DAFWeight", i, (float) hitlist.get(i).getDAFWeight()); - bank.setByte("SorD", i, (byte) hitlist.get(i).getSorDHit()); bank.setInt("TDC",i,hitlist.get(i).get_TDC()); bank.setByte("jitter",i, (byte) hitlist.get(i).getJitter()); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java index 0e20b78c1..c56fe8407 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java @@ -1053,15 +1053,7 @@ public double getDAFWeight() { */ public void setDAFWeight(double weight) { this._DAFWeight = weight; - } - - public int getSorDHit() { - return _SorDHit; - } - - public void setSorDHit(int SorDHit) { - this._SorDHit = SorDHit; - } + } public void updateHitfromSV(StateVec st, DCGeant4Factory DcDetector) { // this.set_Id(this.get_Id()); @@ -1071,7 +1063,6 @@ public void updateHitfromSV(StateVec st, DCGeant4Factory DcDetector) { this.setAssociatedStateVec(st); this.set_TrkResid(this.get_Doca() * this.get_LeftRightAmb() - st.getProjectorDoca()); this.setDAFWeight(st.getDAFWeight()); - this.setSorDHit(st.getSorDHit()); this.setB(st.getB()); this.setSignalPropagTimeAlongWire(st.x(), st.y(), DcDetector); this.setSignalTimeOfFlight(); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index 736fad67b..3be4b241f 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -135,6 +135,9 @@ private double getHitBasedFitChi2ToCrosses(int sector, double x1, double y1, dou return chi2; } + /* + ** a: parameter a for fitting function f(x) = a*z^2 + b*z + c in xz plane + */ private double[] getTrackInitFit(int sector, double x1, double y1, double z1, double x2, double y2, double z2, double x3, double y3, double z3, double ux, double uy, double uz, double thX, double thY, @@ -895,7 +898,7 @@ private List findStraightTracks(CrossList crossList, DCGeant4Factory DcDe List measSurfaces = getMeasSurfaces(cand, DcDetector); StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(cand, measSurfaces.get(0).z, 0, initSV, dcSwim, new float[3]); + getInitState(cand, measSurfaces.get(0).measPoint.z(), 0, initSV, dcSwim, new float[3]); kFZRef.init(measSurfaces, initSV); kFZRef.runFitter(); @@ -1064,7 +1067,7 @@ private List findCurvedTracks(CrossList crossList, DCGeant4Factory DcDete List measSurfaces = getMeasSurfaces(cand, DcDetector); StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(cand, measSurfaces.get(0).z, crossIdxinList, initSV, dcSwim, new float[3]); + getInitState(cand, measSurfaces.get(0).measPoint.z(), crossIdxinList, initSV, dcSwim, new float[3]); kFZRef.init(measSurfaces, initSV); kFZRef.runFitter(); @@ -1248,8 +1251,7 @@ public List getMeasSurfaces(Track trkcand, DCGeant4Factory DcDetector) List surfaces = new ArrayList<>(hOTS.size()); for (int i = 0; i < hOTS.size(); i++) { - Surface surf = new Surface(hOTS.get(i).region, hOTS.get(i)._Z, hOTS.get(i)._X, hOTS.get(i)._tilt, hOTS.get(i)._wireMaxSag, - hOTS.get(i)._doca, hOTS.get(i)._Unc, hOTS.get(i)._hitError, hOTS.get(i)._wireLine); + Surface surf = new Surface(hOTS.get(i).region, hOTS.get(i)._doca, hOTS.get(i)._Unc, hOTS.get(i)._hitError, hOTS.get(i)._wireLine); surf.setSector(hOTS.get(i).sector); surf.setSuperLayer(hOTS.get(i).superlayer); surf.setLayer(hOTS.get(i).layer); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java index 9a6b39f0e..5b880adb7 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -974,7 +974,7 @@ private List findStraightTracks(URWellDCCrossesList urDCCrossesList, DCGe List measSurfaces = getMeasSurfaces(cand, DcDetector); StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(cand, measSurfaces.get(0).z, 0, initSV, dcSwim, new float[3]); + getInitState(cand, measSurfaces.get(0).measPoint.z(), 0, initSV, dcSwim, new float[3]); kFZRef.init(measSurfaces, initSV); kFZRef.runFitter(); @@ -1148,7 +1148,7 @@ private List findCurvedTracks(URWellDCCrossesList urDCCrossesList, DCGean List measSurfaces = getMeasSurfaces(cand, DcDetector); StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(cand, measSurfaces.get(0).z, crossIdxinList, initSV, dcSwim, new float[3]); + getInitState(cand, measSurfaces.get(0).measPoint.z(), crossIdxinList, initSV, dcSwim, new float[3]); kFZRef.init(measSurfaces, initSV); kFZRef.runFitter(); @@ -1318,7 +1318,7 @@ private List findCurvedTracks3URDCCrosses(URWellDCCrossesList urDCCrosses List measSurfaces = getMeasSurfaces(cand, DcDetector); StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(cand, measSurfaces.get(0).z, crossIdxinList, initSV, dcSwim, new float[3]); + getInitState(cand, measSurfaces.get(0).measPoint.z(), crossIdxinList, initSV, dcSwim, new float[3]); kFZRef.init(measSurfaces, initSV); kFZRef.runFitter(); @@ -1431,7 +1431,7 @@ private List findCurvedTracks3URDCCrosses(URWellDCCrossesList urDCCrosses List measSurfaces = getMeasSurfaces(cand, DcDetector); StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(cand, measSurfaces.get(0).z, crossIdxinList, initSV, dcSwim, new float[3]); + getInitState(cand, measSurfaces.get(0).measPoint.z(), crossIdxinList, initSV, dcSwim, new float[3]); kFZRef.init(measSurfaces, initSV); kFZRef.runFitter(); @@ -1669,8 +1669,7 @@ public List getMeasSurfaces(Track trkcand, DCGeant4Factory DcDetector) surfaces.add(i, surf); } else{ - Surface surf = new Surface(hOTS.get(i).region, hOTS.get(i)._Z, hOTS.get(i)._X, hOTS.get(i)._tilt, hOTS.get(i)._wireMaxSag, - hOTS.get(i)._doca, hOTS.get(i)._Unc, hOTS.get(i)._hitError, hOTS.get(i)._wireLine); + Surface surf = new Surface(hOTS.get(i).region, hOTS.get(i)._doca, hOTS.get(i)._Unc, hOTS.get(i)._hitError, hOTS.get(i)._wireLine); surf.setSector(hOTS.get(i).sector); surf.setSuperLayer(hOTS.get(i).superlayer); surf.setLayer(hOTS.get(i).layer); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/StateVec.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/StateVec.java index 373641fdc..145c7e6f5 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/StateVec.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/StateVec.java @@ -42,18 +42,8 @@ public double getDAFWeight() { public void setDAFWeight(double weight) { this._DAFWeight = weight; - } - - private int _SorDHit = -1; // 1 : single hit; 0: double hit for DC + } - public int getSorDHit() { - return _SorDHit; - } - - public void setSorDHit(int SorDHit) { - this._SorDHit = SorDHit; - } - /** * Sets the. * diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java index 25d51c7d6..08e52c838 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/Trajectory.java @@ -57,7 +57,8 @@ public void set_URWellCross(URWellCross urCross) { public void setA(double a){ this.a = a; } - + + // Return parameter a for fitting function f(x) = a*z^2 + b*z + c in xz plane public double getA(){ return a; } diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index 26af04bf0..3f2aa2b10 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -232,7 +232,7 @@ public boolean processDataEvent(DataEvent event) { List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); + getInitState(TrackArray1, measSurfaces.get(0).measPoint.z(), initSV, kFZRef, dcSwim, new float[3]); kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta()); kFZRef.runFitter(); List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); @@ -273,7 +273,7 @@ public boolean processDataEvent(DataEvent event) { List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); + getInitState(TrackArray1, measSurfaces.get(0).measPoint.z(), initSV, kFZRef, dcSwim, new float[3]); kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta(), useDAF); kFZRef.runFitter(useDAF); @@ -373,7 +373,6 @@ public List setKFStateVecsAlongTrajectory(K sv.setProjector(svc.getProjector()); sv.setProjectorDoca(svc.getProjectorDoca()); sv.setDAFWeight(svc.getFinalDAFWeight()); - sv.setSorDHit(svc.getSorDHit()); kfStateVecsAlongTrajectory.add(sv); } @@ -593,8 +592,7 @@ public List getMeasSurfaces(Track trk, DCGeant4Factory DcDetector) { List surfaces = new ArrayList<>(hOTS.size()); for (int i = 0; i < hOTS.size(); i++) { - Surface surf = new Surface(hOTS.get(i).region, hOTS.get(i)._Z, hOTS.get(i)._X, hOTS.get(i)._tilt, hOTS.get(i)._wireMaxSag, - hOTS.get(i)._doca, hOTS.get(i)._Unc, hOTS.get(i)._hitError, hOTS.get(i)._wireLine); + Surface surf = new Surface(hOTS.get(i).region, hOTS.get(i)._doca, hOTS.get(i)._Unc, hOTS.get(i)._hitError, hOTS.get(i)._wireLine); surf.setSector(hOTS.get(i).sector); surf.setSuperLayer(hOTS.get(i).superlayer); surf.setLayer(hOTS.get(i).layer); diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index 35de9c608..7bc4e5c79 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -251,7 +251,7 @@ public boolean processDataEvent(DataEvent event) { List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); + getInitState(TrackArray1, measSurfaces.get(0).measPoint.z(), initSV, kFZRef, dcSwim, new float[3]); kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta(), useDAF); kFZRef.runFitter(useDAF); @@ -294,7 +294,7 @@ public boolean processDataEvent(DataEvent event) { List measSurfaces = getMeasSurfaces(TrackArray1, Constants.getInstance().dcDetector); StateVecs svs = new StateVecs(); org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec initSV = svs.new StateVec(0); - getInitState(TrackArray1, measSurfaces.get(0).z, initSV, kFZRef, dcSwim, new float[3]); + getInitState(TrackArray1, measSurfaces.get(0).measPoint.z(), initSV, kFZRef, dcSwim, new float[3]); kFZRef.initFromHB(measSurfaces, initSV, TrackArray1.get(0).get(0).get(0).get_Beta(), useDAF); kFZRef.runFitter(useDAF); @@ -392,7 +392,6 @@ public List setKFStateVecsAlongTrajectory(K sv.setProjector(svc.getProjector()); sv.setProjectorDoca(svc.getProjectorDoca()); sv.setDAFWeight(svc.getFinalDAFWeight()); - sv.setSorDHit(svc.getSorDHit()); kfStateVecsAlongTrajectory.add(sv); } @@ -411,7 +410,6 @@ public List setKFStateVecsAlongTrajectory(K sv.setProjector(svc.getProjector()); sv.setProjectorDoca(svc.getProjectorDoca()); sv.setDAFWeight(svc.getFinalDAFWeight()); - sv.setSorDHit(svc.getSorDHit()); kfStateVecsAlongTrajectory.add(sv); } @@ -689,8 +687,7 @@ public List getMeasSurfaces(Track trk, DCGeant4Factory DcDetector) { surfaces.add(i, surf); } else{ - Surface surf = new Surface(hOTS.get(i).region, hOTS.get(i)._Z, hOTS.get(i)._X, hOTS.get(i)._tilt, hOTS.get(i)._wireMaxSag, - hOTS.get(i)._doca, hOTS.get(i)._Unc, hOTS.get(i)._hitError, hOTS.get(i)._wireLine); + Surface surf = new Surface(hOTS.get(i).region, hOTS.get(i)._doca, hOTS.get(i)._Unc, hOTS.get(i)._hitError, hOTS.get(i)._wireLine); surf.setSector(hOTS.get(i).sector); surf.setSuperLayer(hOTS.get(i).superlayer); surf.setLayer(hOTS.get(i).layer); From 6f35a35a330302d741688e6c5b9ba37baa58bbf1 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 28 Mar 2024 16:59:41 -0400 Subject: [PATCH 60/69] tiny change --- etc/bankdefs/hipo4/dcnn.json | 1 - 1 file changed, 1 deletion(-) diff --git a/etc/bankdefs/hipo4/dcnn.json b/etc/bankdefs/hipo4/dcnn.json index d5b0e7cf3..ab2ed722c 100644 --- a/etc/bankdefs/hipo4/dcnn.json +++ b/etc/bankdefs/hipo4/dcnn.json @@ -240,7 +240,6 @@ {"name":"fitResidual", "type":"F", "info":"fit residual of the hit (in cm, from KF)"}, {"name":"dDoca", "type":"F", "info":"delta Doca correction (cm)"}, {"name":"DAFWeight", "type":"F", "info":"Weight by DAF"}, - {"name":"SorD", "type":"B", "info":"1: single hit; 0: double hit"}, {"name":"LR", "type":"B", "info":"Left/Right ambiguity of the hit"}, {"name":"X", "type":"F", "info":"wire x-coordinate in tilted-sector"}, {"name":"Z", "type":"F", "info":"wire z-coordinate in tilted-sector"}, From 5239f5270287f2bed922e359920d674026aed643 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 29 Mar 2024 18:07:17 -0400 Subject: [PATCH 61/69] Update TB hit status, whose first bit indicates that a hit is belong to single or double --- .../jlab/clas/tracking/kalmanfilter/AStateVecs.java | 9 +++++++++ .../tracking/kalmanfilter/zReference/KFitter.java | 6 ++++++ .../kalmanfilter/zReference/KFitterWithURWell.java | 6 ++++++ etc/bankdefs/hipo4/dc.json | 2 +- etc/bankdefs/hipo4/dcnn.json | 2 +- .../src/main/java/org/jlab/rec/dc/hit/FittedHit.java | 7 ++++--- .../org/jlab/rec/dc/track/TrackCandListFinder.java | 2 +- .../rec/dc/track/TrackCandListWithURWellFinder.java | 2 +- .../java/org/jlab/rec/dc/trajectory/StateVec.java | 12 +++++++++++- .../main/java/org/jlab/service/dc/DCTBEngine.java | 1 + .../java/org/jlab/service/dc/DCURWellTBEngine.java | 2 ++ 11 files changed, 43 insertions(+), 8 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java index fdd22507a..086010c9b 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/AStateVecs.java @@ -289,6 +289,15 @@ public double getFinalDAFWeight(){ public void setFinalDAFWeight(double weight){ this.finalDAFWeight = weight; } + + boolean isDoubleHit = false; + + public boolean getIsDoubleHit(){ + return isDoubleHit; + } + public void setIsDoubleHit(boolean isDoubleHit){ + this.isDoubleHit = isDoubleHit; + } public double getPathLength() { return _PathLength; diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java index e7702b070..c723fb2a3 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitter.java @@ -1000,6 +1000,7 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weight); + svc.setIsDoubleHit(false); kfStateVecsAlongTrajectory.add(svc); } else{ @@ -1025,6 +1026,7 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weights[0]); + svc.setIsDoubleHit(true); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); @@ -1032,6 +1034,7 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); svc2.setFinalDAFWeight(daf_weights[1]); + svc2.setIsDoubleHit(true); kfStateVecsAlongTrajectory.add(svc2); } @@ -1069,6 +1072,7 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weight); + svc.setIsDoubleHit(false); kfStateVecsAlongTrajectory.add(svc); } else{ @@ -1094,6 +1098,7 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weights[0]); + svc.setIsDoubleHit(true); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); @@ -1101,6 +1106,7 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); svc2.setFinalDAFWeight(daf_weights[1]); + svc2.setIsDoubleHit(true); kfStateVecsAlongTrajectory.add(svc2); } } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index 36eafe9be..cdd96df95 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -1201,6 +1201,7 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weight); + svc.setIsDoubleHit(false); kfStateVecsAlongTrajectory.add(svc); } else{ @@ -1226,6 +1227,7 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(0).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weights[0]); + svc.setIsDoubleHit(true); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); @@ -1233,6 +1235,7 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(0).surface.wireLine[1].origin().x()); svc2.setFinalDAFWeight(daf_weights[1]); + svc2.setIsDoubleHit(true); kfStateVecsAlongTrajectory.add(svc2); } } @@ -1271,6 +1274,7 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weight); + svc.setIsDoubleHit(false); kfStateVecsAlongTrajectory.add(svc); } else{ @@ -1296,6 +1300,7 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc.setProjectorDoca(h); svc.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[0].origin().x()); svc.setFinalDAFWeight(daf_weights[0]); + svc.setIsDoubleHit(true); kfStateVecsAlongTrajectory.add(svc); StateVec svc2 = sv.new StateVec(svc); @@ -1303,6 +1308,7 @@ else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ svc2.setProjectorDoca(h); svc2.setProjector(mv.measurements.get(k1 + 1).surface.wireLine[1].origin().x()); svc2.setFinalDAFWeight(daf_weights[1]); + svc2.setIsDoubleHit(true); kfStateVecsAlongTrajectory.add(svc2); } } diff --git a/etc/bankdefs/hipo4/dc.json b/etc/bankdefs/hipo4/dc.json index eced81dfb..ea0e6cfaf 100644 --- a/etc/bankdefs/hipo4/dc.json +++ b/etc/bankdefs/hipo4/dc.json @@ -362,7 +362,7 @@ {"name":"trkDoca", "type":"F", "info":"track doca of the hit (in cm)"}, {"name":"timeResidual", "type":"F", "info":"time residual of the hit (in cm)"}, {"name":"fitResidual", "type":"F", "info":"fit residual of the hit (in cm, from KF)"}, - {"name":"DAFWeight", "type":"F", "info":"Weight by DAF; The first bit of status points out a hit is belong to singlet or doublet"}, + {"name":"DAFWeight", "type":"F", "info":"Weight by DAF; the 1st bit of status indicates that a hit is belong to single or double"}, {"name":"LR", "type":"B", "info":"Left/Right ambiguity of the hit"}, {"name":"X", "type":"F", "info":"wire x-coordinate in tilted-sector"}, {"name":"Z", "type":"F", "info":"wire z-coordinate in tilted-sector"}, diff --git a/etc/bankdefs/hipo4/dcnn.json b/etc/bankdefs/hipo4/dcnn.json index ab2ed722c..79512c86a 100644 --- a/etc/bankdefs/hipo4/dcnn.json +++ b/etc/bankdefs/hipo4/dcnn.json @@ -239,7 +239,7 @@ {"name":"timeResidual", "type":"F", "info":"time residual of the hit (in cm)"}, {"name":"fitResidual", "type":"F", "info":"fit residual of the hit (in cm, from KF)"}, {"name":"dDoca", "type":"F", "info":"delta Doca correction (cm)"}, - {"name":"DAFWeight", "type":"F", "info":"Weight by DAF"}, + {"name":"DAFWeight", "type":"F", "info":"Weight by DAF; the 1st bit of status indicates that a hit is belong to single or double"}, {"name":"LR", "type":"B", "info":"Left/Right ambiguity of the hit"}, {"name":"X", "type":"F", "info":"wire x-coordinate in tilted-sector"}, {"name":"Z", "type":"F", "info":"wire z-coordinate in tilted-sector"}, diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java index c56fe8407..741a0ed90 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/hit/FittedHit.java @@ -64,7 +64,6 @@ public class FittedHit extends Hit implements Comparable { private double _Time; //Time = TDC - tFlight - tProp - T0 - TStart private double _DAFWeight = -999; // Weight by DAF - private int _SorDHit = -1; // 1 : single hit; 0: double hit for DC /** * identifying outoftimehits; @@ -1042,7 +1041,7 @@ public double get_Beta0to1() { } /** - * @return the _DAFWeight + * @return _DAFWeight */ public double getDAFWeight() { return _DAFWeight; @@ -1053,7 +1052,7 @@ public double getDAFWeight() { */ public void setDAFWeight(double weight) { this._DAFWeight = weight; - } + } public void updateHitfromSV(StateVec st, DCGeant4Factory DcDetector) { // this.set_Id(this.get_Id()); @@ -1063,6 +1062,8 @@ public void updateHitfromSV(StateVec st, DCGeant4Factory DcDetector) { this.setAssociatedStateVec(st); this.set_TrkResid(this.get_Doca() * this.get_LeftRightAmb() - st.getProjectorDoca()); this.setDAFWeight(st.getDAFWeight()); + //Set the first bit of TB hit status to indicate that a hit is belong to single or double + if(st.getIsDoubleHit() == 1) this.set_QualityFac(1); this.setB(st.getB()); this.setSignalPropagTimeAlongWire(st.x(), st.y(), DcDetector); this.setSignalTimeOfFlight(); diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java index 3be4b241f..6d0256005 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListFinder.java @@ -813,7 +813,7 @@ public void setHitDoubletsInfo(Segment seg) { h.setB(o.getB()); h.setTProp(o.getTProp()); h.setTFlight(o.getTFlight()); - h.set_QualityFac(1); + //h.set_QualityFac(1); } } } diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java index 5b880adb7..12e7e6198 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -884,7 +884,7 @@ public void setHitDoubletsInfo(Segment seg) { h.setB(o.getB()); h.setTProp(o.getTProp()); h.setTFlight(o.getTFlight()); - h.set_QualityFac(1); + //h.set_QualityFac(1); } } } diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/StateVec.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/StateVec.java index 145c7e6f5..6d8c5b296 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/StateVec.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/trajectory/StateVec.java @@ -42,7 +42,17 @@ public double getDAFWeight() { public void setDAFWeight(double weight) { this._DAFWeight = weight; - } + } + + private int _isDoubleHit = -1; + + public int getIsDoubleHit() { + return _isDoubleHit; + } + + public void setIsDoubleHit(int _isDoubleHit) { + this._isDoubleHit = _isDoubleHit; + } /** * Sets the. diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java index 3f2aa2b10..2bd9d1e9f 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCTBEngine.java @@ -373,6 +373,7 @@ public List setKFStateVecsAlongTrajectory(K sv.setProjector(svc.getProjector()); sv.setProjectorDoca(svc.getProjectorDoca()); sv.setDAFWeight(svc.getFinalDAFWeight()); + sv.setIsDoubleHit(svc.getIsDoubleHit()?1:0); kfStateVecsAlongTrajectory.add(sv); } diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index 7bc4e5c79..2f10066a4 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -392,6 +392,7 @@ public List setKFStateVecsAlongTrajectory(K sv.setProjector(svc.getProjector()); sv.setProjectorDoca(svc.getProjectorDoca()); sv.setDAFWeight(svc.getFinalDAFWeight()); + sv.setIsDoubleHit(svc.getIsDoubleHit()?1:0); kfStateVecsAlongTrajectory.add(sv); } @@ -410,6 +411,7 @@ public List setKFStateVecsAlongTrajectory(K sv.setProjector(svc.getProjector()); sv.setProjectorDoca(svc.getProjectorDoca()); sv.setDAFWeight(svc.getFinalDAFWeight()); + sv.setIsDoubleHit(svc.getIsDoubleHit()?1:0); kfStateVecsAlongTrajectory.add(sv); } From 78b1557e0061803f3085a91700aed4b5708c2333 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Mon, 1 Apr 2024 17:01:02 -0400 Subject: [PATCH 62/69] update for NDF --- etc/bankdefs/hipo4/dc.json | 7 +++---- etc/bankdefs/hipo4/dcnn.json | 7 +++---- .../java/org/jlab/rec/dc/banks/RecoBankWriter.java | 14 +++++++++++--- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/etc/bankdefs/hipo4/dc.json b/etc/bankdefs/hipo4/dc.json index ea0e6cfaf..f790d3d73 100644 --- a/etc/bankdefs/hipo4/dc.json +++ b/etc/bankdefs/hipo4/dc.json @@ -304,8 +304,7 @@ {"name":"q", "type":"B", "info":"charge of the track"}, {"name":"pathlength", "type":"F", "info":"pathlength of the track"}, {"name":"chi2", "type":"F", "info":"fit chi2 of the track"}, - {"name":"ndf", "type":"S", "info":"fit ndf of the track"}, - {"name":"ndfDAF", "type":"F", "info":"fit ndf of the track weighted by DAF"} + {"name":"ndf", "type":"S", "info":"fit ndf of the track"} ] }, { @@ -548,8 +547,8 @@ {"name":"q", "type":"B", "info":"charge of the track"}, {"name":"pathlength", "type":"F", "info":"pathlength of the track"}, {"name":"chi2", "type":"F", "info":"fit chi2 of the track"}, - {"name":"ndf", "type":"S", "info":"fit ndf of the track"}, - {"name":"ndfDAF", "type":"F", "info":"fit ndf of the track weighted by DAF"} + {"name":"ndf", "type":"S", "info":"fit ndf weightd by DAF for the track"}, + {"name":"ndf0", "type":"S", "info":"traditional ndf for the track"} ] }, { diff --git a/etc/bankdefs/hipo4/dcnn.json b/etc/bankdefs/hipo4/dcnn.json index 79512c86a..19a141f06 100644 --- a/etc/bankdefs/hipo4/dcnn.json +++ b/etc/bankdefs/hipo4/dcnn.json @@ -180,8 +180,7 @@ {"name":"q", "type":"B", "info":"charge of the track"}, {"name":"pathlength", "type":"F", "info":"pathlength of the track"}, {"name":"chi2", "type":"F", "info":"fit chi2 of the track"}, - {"name":"ndf", "type":"S", "info":"fit ndf of the track"}, - {"name":"ndfDAF", "type":"F", "info":"fit ndf of the track weighted by DAF"} + {"name":"ndf", "type":"S", "info":"fit ndf of the track"} ] }, { @@ -410,8 +409,8 @@ {"name":"q", "type":"B", "info":"charge of the track"}, {"name":"pathlength", "type":"F", "info":"pathlength of the track"}, {"name":"chi2", "type":"F", "info":"fit chi2 of the track"}, - {"name":"ndf", "type":"S", "info":"fit ndf of the track"}, - {"name":"ndfDAF", "type":"F", "info":"fit ndf of the track weighted by DAF"} + {"name":"ndf", "type":"S", "info":"fit ndf weightd by DAF for the track"}, + {"name":"ndf0", "type":"S", "info":"traditional ndf for the track"} ] }, { diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java index c6f858054..310d5fc29 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java @@ -439,7 +439,6 @@ public DataBank fillHBTracksBank(DataEvent event, List candlist) { bank.setFloat("chi2", i, (float) candlist.get(i).get_FitChi2()); bank.setShort("ndf", i, (short) candlist.get(i).get_FitNDF()); - bank.setFloat("ndfDAF", i, (float) candlist.get(i).get_NDFDAF()); bank.setFloat("x", i, (float) candlist.get(i).getFinalStateVec().x()); bank.setFloat("y", i, (float) candlist.get(i).getFinalStateVec().y()); bank.setFloat("z", i, (float) candlist.get(i).getFinalStateVec().getZ()); @@ -865,8 +864,17 @@ private DataBank fillTBTracksBank(DataEvent event, List candlist) { i, (short) candlist.get(i).getSingleSuperlayer().get_fittedCluster().get_Id()); } bank.setFloat("chi2", i, (float) candlist.get(i).get_FitChi2()); - bank.setShort("ndf", i, (short) candlist.get(i).get_FitNDF()); - bank.setFloat("ndfDAF", i, (float) candlist.get(i).get_NDFDAF()); + // To not interrupt current type of ndf, ndf weighted by DAF is converted from float to interger + int ndfDAF = 999; + if(candlist.get(i).get_NDFDAF() > 0){ + ndfDAF = (int) Math.ceil(candlist.get(i).get_NDFDAF()); + } + else if (candlist.get(i).get_NDFDAF() < 0){ + ndfDAF = (int) Math.floor(candlist.get(i).get_NDFDAF()); + } + bank.setShort("ndf", i, (short) ndfDAF); + // ndf0 is for traditional ndf for the track; # of hits can be obtained through it + bank.setShort("ndf0", i, (short) candlist.get(i).get_FitNDF()); } return bank; From b25c04a35a90708be12b246dec3100426c20976f Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 4 Apr 2024 11:59:35 -0400 Subject: [PATCH 63/69] apply DAF for uRWell measurements --- .../kalmanfilter/zReference/DAFilter.java | 37 ++++++++++++++- .../zReference/KFitterWithURWell.java | 46 +++++++++++++------ 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java index 2f5fae412..f9c34a098 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java @@ -27,7 +27,13 @@ public class DAFilter { // Effective doca private double effectiveDoca; private double effectiveVar; - private int indexReferenceWire = 0; + private int indexReferenceWire = 0; + + // For uRWell + private double[] xyVars_uRWell; + private double weight_uRWell; + private double[] effectiveXYVars_uRWell; + public DAFilter(double[] docas, double[] vars, double[] weights, Line3D[] wireLines) { this.docas_double = docas; @@ -44,6 +50,11 @@ public DAFilter(double doca, double var, double weight) { this.weight_single = weight; } + public DAFilter(double[] xyVars, double weight) { + this.xyVars_uRWell = xyVars; + this.weight_uRWell = weight; + } + public static void setDafChi2Cut(double chi2Cut){ dafChi2Cut = chi2Cut; } @@ -118,6 +129,12 @@ public void calc_effectiveDoca_singleHit(){ effectiveDoca = doca_single; } + public void calc_effectiveMeasVars_uRWell(){ + effectiveXYVars_uRWell = new double[]{10., 10.}; + for(int i = 0; i < 2; i++) + effectiveXYVars_uRWell[i] = xyVars_uRWell[i]/weight_uRWell; + } + public double get_EffectiveDoca(){ return effectiveDoca; } @@ -126,6 +143,10 @@ public double get_EffectiveVar(){ return effectiveVar; } + public double[] get_EffectiveXYVars_uRWell(){ + return effectiveXYVars_uRWell; + } + public int get_IndexReferenceWire(){ return indexReferenceWire; } @@ -166,5 +187,19 @@ public double[] calc_updatedWeights_doubleHits(double[] residuals, double anneal return updatedWeights; } + public double calc_updatedWeight_uRWell(double[] residuals, double annealingFactor){ + double factor = 1/(2 * Math.PI) / Math.sqrt( annealingFactor * xyVars_uRWell[0] * xyVars_uRWell[1]); + + double Chi2 = residuals[0] * residuals[0]/xyVars_uRWell[0] + residuals[1] * residuals[1]/xyVars_uRWell[1]; + double Phi = factor * Math.exp(-0.5 / annealingFactor * Chi2); + double Lambda = factor * Math.exp(-0.5 / annealingFactor * dafChi2Cut); + double sum = Phi + Lambda; + double updatedWeight = Phi/sum; + + if(updatedWeight < 1.e-100) updatedWeight = 1.e-100; + return updatedWeight; + + } + } diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index cdd96df95..9f092a3e0 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -747,10 +747,20 @@ private boolean filter(int k, boolean forward, double annealingFactor) { } } - else if (mVec.surface.type == Type.PLANEWITHPOINT) { + else if (mVec.surface.type == Type.PLANEWITHPOINT) { + StateVec sVecPreviousFiltered = sv.filtered(!forward).get(k); + double daf_weight = 1; + if (sVecPreviousFiltered != null) { + daf_weight = sVecPreviousFiltered.getWeightDAF_singleHit(); + } + double [] measXYVars = {mVec.surface.measPoint_err.x() * mVec.surface.measPoint_err.x(), mVec.surface.measPoint_err.y() * mVec.surface.measPoint_err.y()}; + DAFilter daf = new DAFilter(measXYVars, daf_weight); + daf.calc_effectiveMeasVars_uRWell(); - double V[][] = {{mVec.surface.measPoint_err.x() * mVec.surface.measPoint_err.x(), 0}, {0, mVec.surface.measPoint_err.y() * mVec.surface.measPoint_err.y()}}; + double[] effectiveVar = daf.get_EffectiveXYVars_uRWell(); + + double V[][] = {{effectiveVar[0], 0}, {0, effectiveVar[1]}}; if(V[0][0] == 0 || V[1][1] == 0 ) LOGGER.log(Level.SEVERE, "Resolution for URWell is 0."); @@ -768,9 +778,7 @@ else if (mVec.surface.type == Type.PLANEWITHPOINT) { } } - double c2 = res[0]*res[0]/V[0][0] + res[1]/V[1][1]; - - //System.out.println("mVec.surface.x: " + mVec.surface.x + "; sVec.x: " + sVec.x + " " + "mVec.surface.y: " + mVec.surface.y + "; sVec.y: " + sVec.y + "res[0]: " + res[0] + "; res[1]: " + res[1]); + double c2 = res[0]*res[0]/V[0][0] + res[1]/V[1][1]; chi2kf += c2; if (filterOn) { @@ -783,12 +791,13 @@ else if (mVec.surface.type == Type.PLANEWITHPOINT) { filteredVec.z = sVec.z; filteredVec.B = sVec.B; filteredVec.deltaPath = sVec.deltaPath; - filteredVec.CM = cMat; - - sv.filtered(forward).put(k, filteredVec); - //sv.filtered(forward).put(k, sVec); + double [] residuals = {mVec.surface.measPoint.x() - filteredVec.x, mVec.surface.measPoint.y() - filteredVec.y}; + double updatedWeight_uRWell= daf.calc_updatedWeight_uRWell(residuals, annealingFactor); + filteredVec.setWeightDAF_singleHit(updatedWeight_uRWell); + + sv.filtered(forward).put(k, filteredVec); } else { return false; } @@ -1168,15 +1177,24 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { svc.setPathLength(path); if(mv.measurements.get(0).surface.type == Type.PLANEWITHPOINT){ - double x_err = mv.measurements.get(0).surface.measPoint_err.x(); - double y_err = mv.measurements.get(0).surface.measPoint_err.y(); + StateVec sVecPreviousFiltered = sv.filtered(true).get(0); + double daf_weight = 1; + if (sVecPreviousFiltered != null) { + daf_weight = sVecPreviousFiltered.getWeightDAF_singleHit(); + } + + double [] measXYVars = {mv.measurements.get(0).surface.measPoint_err.x() * mv.measurements.get(0).surface.measPoint_err.x(), + mv.measurements.get(0).surface.measPoint_err.y() * mv.measurements.get(0).surface.measPoint_err.y()}; + DAFilter daf = new DAFilter(measXYVars, daf_weight); + daf.calc_effectiveMeasVars_uRWell(); + double[] effectiveVar = daf.get_EffectiveXYVars_uRWell(); double x_res = mv.measurements.get(0).surface.measPoint.x() - svc.x; double y_res = mv.measurements.get(0).surface.measPoint.y() - svc.y; - chi2 += (x_res*x_res) / (x_err*x_err); - chi2 += (y_res*y_res) / (y_err*y_err); - ndfDAF += 2; + chi2 += (x_res*x_res) / effectiveVar[0]; + chi2 += (y_res*y_res) / effectiveVar[1]; + ndfDAF += 2*daf_weight; } else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.measPoint.z()); From 55d68b067759c3225f58bb38bebf99f447b0cd1a Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 5 Apr 2024 09:52:23 -0400 Subject: [PATCH 64/69] add state inforamtion from tracking into banks for uRWell --- .../kalmanfilter/zReference/DAFilter.java | 7 +- .../zReference/KFitterWithURWell.java | 12 +- etc/bankdefs/hipo4/urwell.json | 18 +++ .../org/jlab/rec/dc/banks/RecoBankWriter.java | 19 ++- .../track/TrackCandListWithURWellFinder.java | 22 +++- .../java/org/jlab/service/dc/DCEngine.java | 6 + .../org/jlab/service/dc/DCURWellTBEngine.java | 15 +++ .../jlab/rec/urwell/reader/URWellCross.java | 10 ++ .../rec/urwell/reader/URWellStateVec.java | 120 ++++++++++++++++++ 9 files changed, 218 insertions(+), 11 deletions(-) create mode 100644 reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellStateVec.java diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java index f9c34a098..151645172 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/DAFilter.java @@ -10,6 +10,7 @@ public class DAFilter { private static double dafChi2Cut = 8; + private static double dafChi2CutURWell = 100; // For double hits private double[] docas_double; @@ -59,6 +60,10 @@ public static void setDafChi2Cut(double chi2Cut){ dafChi2Cut = chi2Cut; } + public static void setDafChi2CutURWell(double chi2Cut){ + dafChi2CutURWell = chi2Cut; + } + public void calc_effectiveDoca_doubleHits(){ if((wireLines_double[0] == wireLines_double[1]) || ((docas_double[0] == docas_double[1]) && !(docas_double[0] == 0 && docas_double[1] == 0))) { effectiveVar = vars_double[0]/weights_double[0]; @@ -192,7 +197,7 @@ public double calc_updatedWeight_uRWell(double[] residuals, double annealingFact double Chi2 = residuals[0] * residuals[0]/xyVars_uRWell[0] + residuals[1] * residuals[1]/xyVars_uRWell[1]; double Phi = factor * Math.exp(-0.5 / annealingFactor * Chi2); - double Lambda = factor * Math.exp(-0.5 / annealingFactor * dafChi2Cut); + double Lambda = factor * Math.exp(-0.5 / annealingFactor * dafChi2CutURWell); double sum = Phi + Lambda; double updatedWeight = Phi/sum; diff --git a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java index 9f092a3e0..12e866308 100644 --- a/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java +++ b/common-tools/clas-tracking/src/main/java/org/jlab/clas/tracking/kalmanfilter/zReference/KFitterWithURWell.java @@ -45,6 +45,7 @@ public class KFitterWithURWell extends AKFitter { public StateVec finalStateVec = null; public StateVec initialStateVec = null; public List kfStateVecsAlongTrajectory; + public StateVec kfStateVecURWell = null; private int iterNum; @@ -1065,9 +1066,7 @@ private void calcFinalChisq(int sector, boolean nofilter) { path += svc.deltaPath; svc.setPathLength(path); - if (mv.measurements.get(0).surface.type == Type.PLANEWITHPOINT) { - kfStateVecsAlongTrajectory.add(svc); - + if (mv.measurements.get(0).surface.type == Type.PLANEWITHPOINT) { double x_err = mv.measurements.get(0).surface.measPoint_err.x(); double y_err = mv.measurements.get(0).surface.measPoint_err.y(); @@ -1078,7 +1077,9 @@ private void calcFinalChisq(int sector, boolean nofilter) { chi2 += (y_res*y_res) / (y_err*y_err); nRj[mv.measurements.get(0).region] += x_res * x_res / (x_err*x_err); - nRj[mv.measurements.get(0).region] += y_res * y_res / (y_err*y_err); + nRj[mv.measurements.get(0).region] += y_res * y_res / (y_err*y_err); + + kfStateVecURWell = svc; } else if(mv.measurements.get(0).surface.type == Type.LINEDOCA) { double V0 = mv.measurements.get(0).surface.unc[0]; @@ -1195,6 +1196,9 @@ private void calcFinalChisqDAF(int sector, boolean nofilter) { chi2 += (x_res*x_res) / effectiveVar[0]; chi2 += (y_res*y_res) / effectiveVar[1]; ndfDAF += 2*daf_weight; + + svc.setFinalDAFWeight(daf_weight); + kfStateVecURWell = svc; } else if(mv.measurements.get(0).surface.type == Type.LINEDOCA){ Point3D point = new Point3D(svc.x, svc.y, mv.measurements.get(0).surface.measPoint.z()); diff --git a/etc/bankdefs/hipo4/urwell.json b/etc/bankdefs/hipo4/urwell.json index aa5f29b4c..62619626b 100644 --- a/etc/bankdefs/hipo4/urwell.json +++ b/etc/bankdefs/hipo4/urwell.json @@ -75,6 +75,10 @@ {"name":"x_local", "type":"F", "info":"X in tilted sector coordinate (cm)"}, {"name":"y_local", "type":"F", "info":"Y in tilted sector coordinate (cm)"}, {"name":"z_local", "type":"F", "info":"Z in tilted sector coordinate (cm)"}, + {"name":"x_state", "type":"F", "info":"X in state vector (cm)"}, + {"name":"y_state", "type":"F", "info":"Y in state vector (cm)"}, + {"name":"B", "type":"F", "info":"magnetic field (T)"}, + {"name":"pathLength", "type":"F", "info":"pathLength (cm)"}, {"name":"cluster1", "type":"S", "info":"id of the cluster in the V layer"}, {"name":"cluster2", "type":"S", "info":"id of the cluster in the W layer"}, {"name":"status", "type":"S", "info":"status of the cluster"} @@ -99,6 +103,11 @@ {"name":"x_local", "type":"F", "info":"X in tilted sector coordinate (cm)"}, {"name":"y_local", "type":"F", "info":"Y in tilted sector coordinate (cm)"}, {"name":"z_local", "type":"F", "info":"Z in tilted sector coordinate (cm)"}, + {"name":"x_state", "type":"F", "info":"X in state vector (cm)"}, + {"name":"y_state", "type":"F", "info":"Y in state vector (cm)"}, + {"name":"B", "type":"F", "info":"magnetic field (T)"}, + {"name":"pathLength", "type":"F", "info":"pathLength (cm)"}, + {"name":"DAFWeight", "type":"F", "info":"Weight by DAF"}, {"name":"cluster1", "type":"S", "info":"id of the cluster in the V layer"}, {"name":"cluster2", "type":"S", "info":"id of the cluster in the W layer"}, {"name":"status", "type":"S", "info":"status of the cluster"} @@ -123,6 +132,10 @@ {"name":"x_local", "type":"F", "info":"X in tilted sector coordinate (cm)"}, {"name":"y_local", "type":"F", "info":"Y in tilted sector coordinate (cm)"}, {"name":"z_local", "type":"F", "info":"Z in tilted sector coordinate (cm)"}, + {"name":"x_state", "type":"F", "info":"X in state vector (cm)"}, + {"name":"y_state", "type":"F", "info":"Y in state vector (cm)"}, + {"name":"B", "type":"F", "info":"magnetic field (T)"}, + {"name":"pathLength", "type":"F", "info":"pathLength (cm)"}, {"name":"cluster1", "type":"S", "info":"id of the cluster in the V layer"}, {"name":"cluster2", "type":"S", "info":"id of the cluster in the W layer"}, {"name":"status", "type":"S", "info":"status of the cluster"} @@ -147,6 +160,11 @@ {"name":"x_local", "type":"F", "info":"X in tilted sector coordinate (cm)"}, {"name":"y_local", "type":"F", "info":"Y in tilted sector coordinate (cm)"}, {"name":"z_local", "type":"F", "info":"Z in tilted sector coordinate (cm)"}, + {"name":"x_state", "type":"F", "info":"X in state vector (cm)"}, + {"name":"y_state", "type":"F", "info":"Y in state vector (cm)"}, + {"name":"B", "type":"F", "info":"magnetic field (T)"}, + {"name":"pathLength", "type":"F", "info":"pathLength (cm)"}, + {"name":"DAFWeight", "type":"F", "info":"Weight by DAF"}, {"name":"cluster1", "type":"S", "info":"id of the cluster in the V layer"}, {"name":"cluster2", "type":"S", "info":"id of the cluster in the W layer"}, {"name":"status", "type":"S", "info":"status of the cluster"} diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java index 310d5fc29..672c32230 100644 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/banks/RecoBankWriter.java @@ -346,10 +346,16 @@ public DataBank fillHBURWellCrossesBank(DataEvent event, List urCro bank.setFloat("z", i, (float) urCrosses.get(i).position().z()); bank.setFloat("x_local", i, (float) urCrosses.get(i).local().x()); bank.setFloat("y_local", i, (float) urCrosses.get(i).local().y()); - bank.setFloat("z_local", i, (float) urCrosses.get(i).local().z()); + bank.setFloat("z_local", i, (float) urCrosses.get(i).local().z()); bank.setShort("cluster1", i, (short) urCrosses.get(i).cluster1()); bank.setShort("cluster2", i, (short) urCrosses.get(i).cluster2()); - bank.setShort("status", i, (short) urCrosses.get(i).status()); + bank.setShort("status", i, (short) urCrosses.get(i).status()); + if(urCrosses.get(i).getURWellStateVec() != null){ + bank.setFloat("x_state", i, (float) urCrosses.get(i).getURWellStateVec().x()); + bank.setFloat("y_state", i, (float) urCrosses.get(i).getURWellStateVec().y()); + bank.setFloat("B", i, (float) urCrosses.get(i).getURWellStateVec().getB()); + bank.setFloat("pathLength", i, (float) urCrosses.get(i).getURWellStateVec().getPathLength()); + } } return bank; @@ -768,7 +774,14 @@ public DataBank fillTBURWellCrossesBank(DataEvent event, List urCro bank.setFloat("z_local", i, (float) urCrosses.get(i).local().z()); bank.setShort("cluster1", i, (short) urCrosses.get(i).cluster1()); bank.setShort("cluster2", i, (short) urCrosses.get(i).cluster2()); - bank.setShort("status", i, (short) urCrosses.get(i).status()); + bank.setShort("status", i, (short) urCrosses.get(i).status()); + if(urCrosses.get(i).getURWellStateVec() != null){ + bank.setFloat("x_state", i, (float) urCrosses.get(i).getURWellStateVec().x()); + bank.setFloat("y_state", i, (float) urCrosses.get(i).getURWellStateVec().y()); + bank.setFloat("B", i, (float) urCrosses.get(i).getURWellStateVec().getB()); + bank.setFloat("pathLength", i, (float) urCrosses.get(i).getURWellStateVec().getPathLength()); + bank.setFloat("DAFWeight", i, (float) urCrosses.get(i).getURWellStateVec().getDAFWeight()); + } } return bank; diff --git a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java index 12e7e6198..f476c8464 100755 --- a/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java +++ b/reconstruction/dc/src/main/java/org/jlab/rec/dc/track/TrackCandListWithURWellFinder.java @@ -34,6 +34,7 @@ import org.jlab.clas.tracking.utilities.MatrixOps.Libr; import org.jlab.clas.tracking.utilities.RungeKuttaDoca; import org.jlab.rec.urwell.reader.URWellCross; +import org.jlab.rec.urwell.reader.URWellStateVec; import org.jlab.rec.dc.cross.URWellDCCrossesList; /** @@ -1153,6 +1154,7 @@ private List findCurvedTracks(URWellDCCrossesList urDCCrossesList, DCGean kFZRef.runFitter(); List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + URWellStateVec kfStateVecURWell = setKFStateVecURWell(kFZRef); if (kFZRef.finalStateVec == null) { continue; @@ -1175,7 +1177,8 @@ private List findCurvedTracks(URWellDCCrossesList urDCCrossesList, DCGean cand.set_FitConvergenceStatus(kFZRef.ConvStatus); cand.set_CovMat(kFZRef.finalStateVec.CM); - cand.setStateVecs(kfStateVecsAlongTrajectory); + cand.setStateVecs(kfStateVecsAlongTrajectory); + cand.get_URWellCross().setURWellStateVec(kfStateVecURWell); cand.setFinalStateVec(fitStateVec); cand.set_Id(cands.size() + 1); @@ -1436,6 +1439,7 @@ private List findCurvedTracks3URDCCrosses(URWellDCCrossesList urDCCrosses kFZRef.runFitter(); List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + URWellStateVec kfStateVecURWell = setKFStateVecURWell(kFZRef); if (kFZRef.finalStateVec == null) { continue; @@ -1458,7 +1462,8 @@ private List findCurvedTracks3URDCCrosses(URWellDCCrossesList urDCCrosses cand.set_FitConvergenceStatus(kFZRef.ConvStatus); cand.set_CovMat(kFZRef.finalStateVec.CM); - cand.setStateVecs(kfStateVecsAlongTrajectory); + cand.setStateVecs(kfStateVecsAlongTrajectory); + cand.get_URWellCross().setURWellStateVec(kfStateVecURWell); cand.setFinalStateVec(fitStateVec); cand.set_Id(cands.size() + 1); @@ -1503,7 +1508,18 @@ public List setKFStateVecsAlongTrajectory(K return kfStateVecsAlongTrajectory; } - + + public URWellStateVec setKFStateVecURWell(KFitterWithURWell kFZRef) { + if (kFZRef.kfStateVecURWell != null) { + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecURWell; + URWellStateVec sv = new URWellStateVec(svc.x, svc.y, svc.z, svc.tx, svc.ty, svc.Q, svc.B, svc.getPathLength()); + + return sv; + } else { + return null; + } + } + public List setKFStateVecsAlongTrajectory(KFitterWithURWell kFZRef) { List kfStateVecsAlongTrajectory = new ArrayList<>(); diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java index 7073ac009..430d2731b 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCEngine.java @@ -36,6 +36,7 @@ public class DCEngine extends ReconstructionEngine { private double[][] shifts = new double[Constants.NREG][6]; protected boolean useDAF = true; private String dafChi2Cut = null; + private String dafChi2CutURWell = null; private String dafAnnealingFactorsTB = null; private String dafAnnealingFactorsTBWithURWell = null; @@ -115,6 +116,11 @@ else if(this.getEngineConfigString("dcT2DFunc").equalsIgnoreCase("Polynomial")) DAFilter.setDafChi2Cut(Double.valueOf(dafChi2Cut)); } + if(this.getEngineConfigString("dafChi2CutURWell")!=null) { + dafChi2CutURWell=this.getEngineConfigString("dafChi2CutURWell"); + DAFilter.setDafChi2CutURWell(Double.valueOf(dafChi2CutURWell)); + } + if(this.getEngineConfigString("dafAnnealingFactorsTB")!=null){ dafAnnealingFactorsTB=this.getEngineConfigString("dafAnnealingFactorsTB"); KFitter.setDafAnnealingFactorsTB(dafAnnealingFactorsTB); diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java index 2f10066a4..a8e68e71b 100755 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellTBEngine.java @@ -44,6 +44,7 @@ import org.jlab.rec.dc.track.TrackCandListWithURWellFinder; import org.jlab.rec.urwell.reader.URWellCross; import org.jlab.rec.urwell.reader.URWellReader; +import org.jlab.rec.urwell.reader.URWellStateVec; /** * @@ -256,6 +257,7 @@ public boolean processDataEvent(DataEvent event) { kFZRef.runFitter(useDAF); List kfStateVecsAlongTrajectory = setKFStateVecsAlongTrajectory(kFZRef); + URWellStateVec kfStateVecURWell = setKFStateVecURWell(kFZRef); StateVec fn = new StateVec(); if (kFZRef.setFitFailed==false && kFZRef.finalStateVec!=null) { @@ -275,6 +277,7 @@ public boolean processDataEvent(DataEvent event) { TrackArray1.set_FitNDF(kFZRef.NDF); TrackArray1.set_NDFDAF(kFZRef.getNDFDAF()); TrackArray1.setStateVecs(kfStateVecsAlongTrajectory); + TrackArray1.get_URWellCross().setURWellStateVec(kfStateVecURWell); TrackArray1.set_FitConvergenceStatus(kFZRef.ConvStatus); if (TrackArray1.get_Vtx0().toVector3D().mag() > 500) { continue; @@ -399,6 +402,18 @@ public List setKFStateVecsAlongTrajectory(K return kfStateVecsAlongTrajectory; } + public URWellStateVec setKFStateVecURWell(KFitterWithURWell kFZRef) { + if (kFZRef.kfStateVecURWell != null) { + org.jlab.clas.tracking.kalmanfilter.AStateVecs.StateVec svc = kFZRef.kfStateVecURWell; + URWellStateVec sv = new URWellStateVec(svc.x, svc.y, svc.z, svc.tx, svc.ty, svc.Q, svc.B, svc.getPathLength()); + sv.setDAFWeight(svc.getFinalDAFWeight()); + + return sv; + } else { + return null; + } + } + public List setKFStateVecsAlongTrajectory(KFitter kFZRef) { List kfStateVecsAlongTrajectory = new ArrayList<>(); diff --git a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java index 4b6e0c5f5..4a5658fa5 100644 --- a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java +++ b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java @@ -52,6 +52,16 @@ public URWellCross(int id, int tid, int sector, int region, double x, double y, this.cluster2 = cluster2; this.status = status; } + + URWellStateVec stateVec = null; + + public void setURWellStateVec(URWellStateVec svc){ + stateVec = svc; + } + + public URWellStateVec getURWellStateVec(){ + return stateVec; + } /** * return track id diff --git a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellStateVec.java b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellStateVec.java new file mode 100644 index 000000000..146f7dc92 --- /dev/null +++ b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellStateVec.java @@ -0,0 +1,120 @@ +package org.jlab.rec.urwell.reader; + +/** + * @author Tongtong + * + */ + +public class URWellStateVec { + private double _x; + private double _y; + private double _z; + private double _tx; + private double _ty; + private double _Q; + private double _B; + private double _PathLength; + private double _DAFWeight = -999; + + /** + * Instantiates a new vec. + */ + public URWellStateVec(double x, double y, double z, double tx, double ty, double Q, double B, double pathLength) { + _x = x; + _y = y; + _z = z; + _tx = tx; + _ty = ty; + _Q = Q; + _B = B; + _PathLength = pathLength; + } + + public double getB() { + return _B; + } + + public double setB(double B) { + return this._B = B; + } + + public double getPathLength() { + return _PathLength; + } + + public void setPathLength(double _PathLength) { + this._PathLength = _PathLength; + } + + + public double getDAFWeight() { + return _DAFWeight; + } + + public void setDAFWeight(double weight) { + this._DAFWeight = weight; + } + + + + /** + * Description of x(). + * + * @return the x component + */ + public double x() { + return _x; + } + + /** + * Description of y(). + * + * @return the y component + */ + + public double y() { + return _y; + } + + /** + * Description of z(). + * + * @return z + */ + public double z() { + return _z; + } + + /** + * Description of tanThetaX(). + * + * @return the tanThetaX component + */ + public double tx() { + return _tx; + } + + /** + * Description of tanThetaY(). + * + * @return the tanThetaY component + */ + public double ty() { + return _ty; + } + + /** + * Description of Q(). + * + * @return the Q component + */ + public double Q() { + return _Q; + } + + + public void printInfo() { + System.out.println("StateVec [ "+this.x()+", "+this.y()+", "+this.tx()+", "+this.ty()+", "+this.Q()+" ] "); + } + +} From b430b279181d92793fa6db80cf5525903f68301e Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 5 Apr 2024 14:16:48 -0400 Subject: [PATCH 65/69] update org.jlab.service.dc.DCURWellHBPostClusterAI --- .../service/dc/DCURWellHBPostClusterAI.java | 97 +++++++++++++++---- 1 file changed, 76 insertions(+), 21 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java index 40f850731..ab8af0868 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java @@ -123,11 +123,17 @@ public boolean processDataEvent(DataEvent event) { URWellReader uRWellReader = new URWellReader(event, "HB"); List urCrosses = uRWellReader.getUrwellCrosses(); URWellDCCrossesListFinder uRWellDCCrossListLister = new URWellDCCrossesListFinder(); - URWellDCCrossesList urDCCrossesList = uRWellDCCrossListLister.candURWellDCCrossLists(urCrosses, crosslist); + URWellDCCrossesList urDC4CrossesList = uRWellDCCrossListLister.candURWellDCCrossesLists(event, crosses, urCrosses, + false, + null, + Constants.getInstance().dcDetector, + null, + dcSwim, false, 4 + ); - //find the list of track candidates + //Build tracks for 4-crosses combos TrackCandListWithURWellFinder trkcandFinder = new TrackCandListWithURWellFinder(Constants.HITBASE); - trkcands = trkcandFinder.getTrackCands(urDCCrossesList, + trkcands = trkcandFinder.getTrackCands(urDC4CrossesList, Constants.getInstance().dcDetector, Swimmer.getTorScale(), dcSwim, true); @@ -145,32 +151,81 @@ public boolean processDataEvent(DataEvent event) { trkcandFinder.matchHits(trk.getStateVecs(), trk, Constants.getInstance().dcDetector, - dcSwim); - for (Cross c : trk) { - c.set_CrossDirIntersSegWires(); - clusters.add(c.get_Segment1().get_fittedCluster()); - clusters.add(c.get_Segment2().get_fittedCluster()); - trkcandFinder.setHitDoubletsInfo(c.get_Segment1()); - trkcandFinder.setHitDoubletsInfo(c.get_Segment2()); - for (FittedHit h1 : c.get_Segment1()) { - if(h1.get_AssociatedHBTrackID()>0) fhits.add(h1); - } - for (FittedHit h2 : c.get_Segment2()) { - if(h2.get_AssociatedHBTrackID()>0) fhits.add(h2); - } + dcSwim); + if(trk.get_URWellCross() != null){ + urCrossesOnTrks.add(trk.get_URWellCross()); + trk.get_URWellCross().set_tid(trk.get_Id()); } + trkId++; + } + } + + ////// Tracking for 3-URDCCrosses combos + // Add real dc crosses into crossesOnTrack list + List dcCrossesOnTrack = new ArrayList(); + for(Cross dcCross : crosses){ + Segment seg1 = dcCross.get_Segment1(); + Segment seg2 = dcCross.get_Segment2(); + if(seg1.isOnTrack==true && seg2.isOnTrack==true && seg1.associatedCrossId==seg2.associatedCrossId){ + dcCrossesOnTrack.add(dcCross); + } + } + + // Remove real dc crosses on tracks + crosses.removeAll(dcCrossesOnTrack); + // Further remove uRWell crosses on tracks with pseudo-segment + urCrosses.removeAll(urCrossesOnTrks); + // Build 3-crosses combos from any of 3 regions + URWellDCCrossesList urDC3CrossesList = uRWellDCCrossListLister.candURWellDCCrossesLists(event, crosses, urCrosses, + false, + null, + Constants.getInstance().dcDetector, + null, + dcSwim, false, 3 + ); + // Build tracks for 3-crosses combos + List trkcands3URDCCrosses = trkcandFinder.getTrackCands3URDCCrosses(urDC3CrossesList, + Constants.getInstance().dcDetector, + Swimmer.getTorScale(), + dcSwim, false); + if (!trkcands3URDCCrosses.isEmpty()) { + trkcandFinder.removeOverlappingTracks(trkcands3URDCCrosses); + for (Track trk : trkcands3URDCCrosses) { - if(trk.get_URWellCross() != null){ - urCrossesOnTrks.add(trk.get_URWellCross()); - trk.get_URWellCross().set_tid(trk.get_Id()); + // reset the id + trk.set_Id(trkId); + trkcandFinder.matchHits(trk.getStateVecs(), + trk, + Constants.getInstance().dcDetector, + dcSwim); + if(trk.get_URWellCross() != null){ + urCrossesOnTrks.add(trk.get_URWellCross()); + trk.get_URWellCross().set_tid(trk.get_Id()); } - trk.calcTrajectory(trk.getId(), dcSwim, trk.get_Vtx0(), trk.get_pAtOrig(), trk.get_Q()); trkId++; } } - // no candidate found, stop here and save the hits, + trkcands.addAll(trkcands3URDCCrosses); + + //gather all the hits and URWell crosses for pointer bank creation + for (Track trk : trkcands) { + trk.calcTrajectory(trk.getId(), dcSwim, trk.get_Vtx0(), trk.get_pAtOrig(), trk.get_Q()); + for (Cross c : trk) { + c.set_CrossDirIntersSegWires(); + trkcandFinder.setHitDoubletsInfo(c.get_Segment1()); + trkcandFinder.setHitDoubletsInfo(c.get_Segment2()); + for (FittedHit h1 : c.get_Segment1()) { + if(h1.get_AssociatedHBTrackID()>0) fhits.add(h1); + } + for (FittedHit h2 : c.get_Segment2()) { + if(h2.get_AssociatedHBTrackID()>0) fhits.add(h2); + } + } + } + // no candidate found, stop here and save the hits, // the clusters, the segments, the crosses + crosses.addAll(dcCrossesOnTrack); if (trkcands.isEmpty()) { event.appendBanks( writer.fillHBHitsBank(event, fhits), From 3f28d40c19fc3e824c0d2b2a6e3cf4482ae88fd8 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Mon, 8 Apr 2024 10:55:26 -0400 Subject: [PATCH 66/69] Update org.jlab.service.dc.DCURWellHBPostClusterAI to take clusters only from conventional reconstruction into account --- .../service/dc/DCURWellHBPostClusterAI.java | 139 +++++++++++++++++- 1 file changed, 133 insertions(+), 6 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java index ab8af0868..788446521 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.logging.Level; import org.jlab.clas.swimtools.Swim; import org.jlab.clas.swimtools.Swimmer; @@ -10,10 +11,13 @@ import org.jlab.rec.dc.Constants; import org.jlab.rec.dc.banks.HitReader; import org.jlab.rec.dc.banks.RecoBankWriter; +import org.jlab.rec.dc.cluster.ClusterFinder; +import org.jlab.rec.dc.cluster.ClusterFitter; import org.jlab.rec.dc.cluster.FittedCluster; import org.jlab.rec.dc.cross.Cross; import org.jlab.rec.dc.cross.CrossList; import org.jlab.rec.dc.cross.CrossListFinder; +import org.jlab.rec.dc.cross.CrossMaker; import org.jlab.rec.dc.hit.FittedHit; import org.jlab.rec.dc.hit.Hit; import org.jlab.rec.dc.nn.PatternRec; @@ -22,6 +26,7 @@ import org.jlab.rec.dc.track.TrackCandListWithURWellFinder; import org.jlab.rec.dc.cross.URWellDCCrossesList; import org.jlab.rec.dc.cross.URWellDCCrossesListFinder; +import org.jlab.rec.dc.segment.SegmentFinder; import org.jlab.rec.urwell.reader.URWellCross; import org.jlab.rec.urwell.reader.URWellReader; @@ -160,8 +165,8 @@ public boolean processDataEvent(DataEvent event) { } } - ////// Tracking for 3-URDCCrosses combos - // Add real dc crosses into crossesOnTrack list + ////// Tracking for 3-DCCrosses combos, which AI predicted + // Remove real dc crosses on tracks List dcCrossesOnTrack = new ArrayList(); for(Cross dcCross : crosses){ Segment seg1 = dcCross.get_Segment1(); @@ -170,11 +175,95 @@ public boolean processDataEvent(DataEvent event) { dcCrossesOnTrack.add(dcCross); } } + crosses.removeAll(dcCrossesOnTrack); + // Build 3-crosses combos from any of 3 regions + URWellDCCrossesList dc3CrossesList = uRWellDCCrossListLister.candURWellDCCrossesLists(event, crosses, new ArrayList(), + false, + null, + Constants.getInstance().dcDetector, + null, + dcSwim, false, 3 + ); + // Build tracks for 3-DCcrosses combos + List trkcands3DCCrosses = trkcandFinder.getTrackCands3URDCCrosses(dc3CrossesList, + Constants.getInstance().dcDetector, + Swimmer.getTorScale(), + dcSwim, false); + if (!trkcands3DCCrosses.isEmpty()) { + trkcandFinder.removeOverlappingTracks(trkcands3DCCrosses); + for (Track trk : trkcands3DCCrosses) { + + // reset the id + trk.set_Id(trkId); + trkcandFinder.matchHits(trk.getStateVecs(), + trk, + Constants.getInstance().dcDetector, + dcSwim); + trkId++; + } + } + + trkcands.addAll(trkcands3DCCrosses); + ////// Tracking for 3-DCURCrosses combos + //// Find crosses only from conventional reconstruction (not exist in AI-assisted reconstruction) + // Find segments from conventional reconstruction + List clustersConv = null; + List segmentsConv = null; + Map> hitsConv = reader.read_Hits(event); + if(hitsConv != null && !hitsConv.isEmpty()){ + //find the segments from these hits + ClusterFinder clusFinder = new ClusterFinder(); + ClusterFitter cf = new ClusterFitter(); + clustersConv = clusFinder.RecomposeClusters(hitsConv, Constants.getInstance().dcDetector, cf); + if (clustersConv !=null && !clustersConv.isEmpty()) { + //find the segments from the fitted clusters + SegmentFinder segFinder = new SegmentFinder(); + segmentsConv = segFinder.get_Segments(clustersConv, + event, + Constants.getInstance().dcDetector, false); + + if (!segmentsConv.isEmpty()) { + List rmSegs = new ArrayList<>(); + // clean up hit-based segments + double trkDocOverCellSize; + for (Segment se : segmentsConv) { + trkDocOverCellSize = 0; + for (FittedHit fh : se.get_fittedCluster()) { + trkDocOverCellSize += fh.get_ClusFitDoca() / fh.get_CellSize(); + } + if (trkDocOverCellSize / se.size() > 1.1) { + rmSegs.add(se); + } + } + segmentsConv.removeAll(rmSegs); + } + } + } + // find segments and clusters exist in convential reconstruction, but does not exit in AI-assisted reconstruction + List segmentsConvOnly = get_segments_convOnly(segments, segmentsConv); + List clustersConvOnly = get_clusters_convOnly(clustersConv, segmentsConvOnly); + // Find crosses exist in convential reconstruction, but does not exit in AI-assisted reconstruction + CrossMaker crossMake = new CrossMaker(); + List crossesConvOnly = crossMake.find_Crosses(segmentsConvOnly, Constants.getInstance().dcDetector); + + //// Collect remaining crosses, which are not on tracks // Remove real dc crosses on tracks - crosses.removeAll(dcCrossesOnTrack); - // Further remove uRWell crosses on tracks with pseudo-segment + // Add real dc crosses into crossesOnTrack list from previous stage + List dcCrossesOnTrack2 = new ArrayList(); + for(Cross dcCross : crosses){ + Segment seg1 = dcCross.get_Segment1(); + Segment seg2 = dcCross.get_Segment2(); + if(seg1.isOnTrack==true && seg2.isOnTrack==true && seg1.associatedCrossId==seg2.associatedCrossId){ + dcCrossesOnTrack2.add(dcCross); + } + } + crosses.removeAll(dcCrossesOnTrack2); + // Add real dc crosses by segments from conventional only + crosses.addAll(crossesConvOnly); + // Remove uRWell crosses on tracks urCrosses.removeAll(urCrossesOnTrks); + // Build 3-crosses combos from any of 3 regions URWellDCCrossesList urDC3CrossesList = uRWellDCCrossListLister.candURWellDCCrossesLists(event, crosses, urCrosses, false, @@ -197,7 +286,8 @@ public boolean processDataEvent(DataEvent event) { trkcandFinder.matchHits(trk.getStateVecs(), trk, Constants.getInstance().dcDetector, - dcSwim); + dcSwim); + if(trk.get_URWellCross() != null){ urCrossesOnTrks.add(trk.get_URWellCross()); trk.get_URWellCross().set_tid(trk.get_Id()); @@ -208,7 +298,7 @@ public boolean processDataEvent(DataEvent event) { trkcands.addAll(trkcands3URDCCrosses); - //gather all the hits and URWell crosses for pointer bank creation + //gather all the hits and URWell crosses for pointer bank creation for (Track trk : trkcands) { trk.calcTrajectory(trk.getId(), dcSwim, trk.get_Vtx0(), trk.get_pAtOrig(), trk.get_Q()); for (Cross c : trk) { @@ -226,6 +316,9 @@ public boolean processDataEvent(DataEvent event) { // no candidate found, stop here and save the hits, // the clusters, the segments, the crosses crosses.addAll(dcCrossesOnTrack); + crosses.addAll(dcCrossesOnTrack2); + segments.addAll(segmentsConvOnly); + clusters.addAll(clustersConvOnly); if (trkcands.isEmpty()) { event.appendBanks( writer.fillHBHitsBank(event, fhits), @@ -246,4 +339,38 @@ public boolean processDataEvent(DataEvent event) { return true; } + + public List get_segments_convOnly(List segmentsAI, List segmentsConv){ + List segmentsShare = new ArrayList<>(); + for(Segment segmentConv : segmentsConv){ + for(Segment segmentAI: segmentsAI){ + if(segmentConv.get_Id() == segmentAI.get_Id()){ + segmentsShare.add(segmentConv); + break; + } + } + } + + List segmentsConvOnly = new ArrayList<>(); + segmentsConvOnly.addAll(segmentsConv); + segmentsConvOnly.removeAll(segmentsShare); + + return segmentsConvOnly; + } + + + public List get_clusters_convOnly(List clustersConv, List segmentsConvOnly){ + List clustersConvOnly = new ArrayList<>(); + for(Segment seg : segmentsConvOnly){ + for(FittedCluster cl: clustersConv){ + if(cl.get_Id() == seg.get_Id()){ + clustersConvOnly.add(cl); + break; + } + } + } + + return clustersConvOnly; + } + } From 052e1860d2eeef8d6f4bc3633e73188c0f4dd28c Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Tue, 9 Apr 2024 16:33:13 -0400 Subject: [PATCH 67/69] fix HB cluster bank issue in org.jlab.service.dc.DCURWellHBPostClusterAI --- .../service/dc/DCURWellHBPostClusterAI.java | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java index 788446521..4ad8971f3 100644 --- a/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java +++ b/reconstruction/dc/src/main/java/org/jlab/service/dc/DCURWellHBPostClusterAI.java @@ -242,7 +242,7 @@ public boolean processDataEvent(DataEvent event) { } // find segments and clusters exist in convential reconstruction, but does not exit in AI-assisted reconstruction List segmentsConvOnly = get_segments_convOnly(segments, segmentsConv); - List clustersConvOnly = get_clusters_convOnly(clustersConv, segmentsConvOnly); + segments.addAll(segmentsConvOnly); // Find crosses exist in convential reconstruction, but does not exit in AI-assisted reconstruction CrossMaker crossMake = new CrossMaker(); List crossesConvOnly = crossMake.find_Crosses(segmentsConvOnly, Constants.getInstance().dcDetector); @@ -317,8 +317,11 @@ public boolean processDataEvent(DataEvent event) { // the clusters, the segments, the crosses crosses.addAll(dcCrossesOnTrack); crosses.addAll(dcCrossesOnTrack2); - segments.addAll(segmentsConvOnly); - clusters.addAll(clustersConvOnly); + for (Cross c : crosses) { + c.set_CrossDirIntersSegWires(); + clusters.add(c.get_Segment1().get_fittedCluster()); + clusters.add(c.get_Segment2().get_fittedCluster()); + } if (trkcands.isEmpty()) { event.appendBanks( writer.fillHBHitsBank(event, fhits), @@ -356,21 +359,5 @@ public List get_segments_convOnly(List segmentsAI, List get_clusters_convOnly(List clustersConv, List segmentsConvOnly){ - List clustersConvOnly = new ArrayList<>(); - for(Segment seg : segmentsConvOnly){ - for(FittedCluster cl: clustersConv){ - if(cl.get_Id() == seg.get_Id()){ - clustersConvOnly.add(cl); - break; - } - } - } - - return clustersConvOnly; - } - + } } From 6b1823eead6e8f34133056a707d255cc9d7e5ab4 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Thu, 25 Apr 2024 10:30:02 -0400 Subject: [PATCH 68/69] update status of uRWell cross --- .../java/org/jlab/rec/urwell/reader/URWellCross.java | 11 ----------- .../java/org/jlab/rec/urwell/reader/URWellReader.java | 2 +- .../java/org/jlab/service/urwell/URWellCross.java | 7 +++++++ 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java index 4a5658fa5..1b8164b4b 100644 --- a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java +++ b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java @@ -142,15 +142,4 @@ public URWellCluster getCluster2(List urClusters) { return null; } } - - public boolean isGood(List urClusters) { - if(this.getCluster1(urClusters) != null && this.getCluster2(urClusters)!= null) - return Math.abs(this.getCluster1(urClusters).energy() - this.getCluster2(urClusters).energy()) < URWellConstants.deltaE - && Math.abs(this.getCluster1(urClusters).time() - this.getCluster2(urClusters).time()) < URWellConstants.deltaT; - else return false; - } - - public boolean isInTime() { - return Math.abs(this.time() - URWellConstants.meanT) < 1.2 * URWellConstants.deltaT; - } } diff --git a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java index cc0bd5bb4..13b6fd15c 100644 --- a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java +++ b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java @@ -102,7 +102,7 @@ public final void readCrosses(DataBank bank) { cross.setClusterIndex2(cluster2); if(cluster1<=urClusters.size()) urClusters.get(cluster1-1).setCrossIndex(i); if(cluster2<=urClusters.size()) urClusters.get(cluster2-1).setCrossIndex(i); - if(cross.isGood(urClusters) && cross.isInTime()) + if(status == 0) urCrosses.add(cross); } } diff --git a/reconstruction/urwell/src/main/java/org/jlab/service/urwell/URWellCross.java b/reconstruction/urwell/src/main/java/org/jlab/service/urwell/URWellCross.java index 90fa1d2c4..53407d21c 100644 --- a/reconstruction/urwell/src/main/java/org/jlab/service/urwell/URWellCross.java +++ b/reconstruction/urwell/src/main/java/org/jlab/service/urwell/URWellCross.java @@ -42,6 +42,13 @@ public URWellCross(URWellCluster c1, URWellCluster c2) { this.time = (c1.getTime() + c2.getTime())/2; this.cluster1 = c1.getId(); this.cluster2 = c2.getId(); + this.status = 0; + if(Math.abs(c1.getEnergy()-c2.getEnergy())>URWellConstants.deltaE) + this.status += 1; + if(Math.abs(c1.getTime()-c2.getTime())>URWellConstants.deltaT) + this.status += 2; + if(Math.abs(this.time - URWellConstants.meanT)>1.2*URWellConstants.deltaT) + this.status += 4; } } From 8d86beb2a0b20dca83311184789bdbf779951551 Mon Sep 17 00:00:00 2001 From: tongtongcao Date: Fri, 26 Apr 2024 10:43:14 -0400 Subject: [PATCH 69/69] update uRWell reader for uRWell clusters --- .../jlab/rec/urwell/reader/URWellCluster.java | 12 ++++++--- .../jlab/rec/urwell/reader/URWellCross.java | 26 +++++++++++++------ .../jlab/rec/urwell/reader/URWellReader.java | 5 ++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCluster.java b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCluster.java index 067974677..e7620c434 100644 --- a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCluster.java +++ b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCluster.java @@ -9,20 +9,26 @@ */ public class URWellCluster { - + + private int id = -1; private DetectorDescriptor desc = new DetectorDescriptor(DetectorType.URWELL); private int size = 0; private double energy = 0; private double time = 0; private int crossIndex = -1; - public URWellCluster(int sector, int layer, int component, int size, double energy, double time) { + public URWellCluster(int id, int sector, int layer, int component, int size, double energy, double time) { + this.id = id; this.desc.setSectorLayerComponent(sector, layer, component); this.size = size; this.energy = energy; this.time = time; } - + + public int id() { + return this.id; + } + public int sector() { return this.desc.getSector(); } diff --git a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java index 1b8164b4b..bc1b7f116 100644 --- a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java +++ b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellCross.java @@ -128,18 +128,28 @@ public int status() { } public URWellCluster getCluster1(List urClusters) { - if (cluster1 > 0 && cluster1 <= urClusters.size()) { - return urClusters.get(cluster1 - 1); - } else { - return null; + URWellCluster cluster = null; + + for (URWellCluster cl : urClusters) { + if (cl.id() == cluster1) { + cluster = cl; + break; + } } + + return cluster; } public URWellCluster getCluster2(List urClusters) { - if (cluster2 > 0 && cluster2 <= urClusters.size()) { - return urClusters.get(cluster2 - 1); - } else { - return null; + URWellCluster cluster = null; + + for (URWellCluster cl : urClusters) { + if (cl.id() == cluster2) { + cluster = cl; + break; + } } + + return cluster; } } diff --git a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java index 13b6fd15c..272732969 100644 --- a/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java +++ b/reconstruction/urwell/src/main/java/org/jlab/rec/urwell/reader/URWellReader.java @@ -28,8 +28,6 @@ public URWellReader(DataEvent event) { public URWellReader(DataEvent event, String type) { if(type == "HB"){ - if(event.hasBank("URWELL::clusters")) - this.readClusters(event.getBank("URWELL::clusters")); if(event.hasBank("URWELL::crosses")) this.readCrosses(event.getBank("URWELL::crosses")); } @@ -71,13 +69,14 @@ public final void readHits(DataBank bank) { public final void readClusters(DataBank bank) { for(int i=0; i