diff --git a/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/ASwim.java b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/ASwim.java new file mode 100644 index 0000000000..49c96bcbfd --- /dev/null +++ b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/ASwim.java @@ -0,0 +1,48 @@ +package org.jlab.clas.swimtools; + +import org.jlab.geom.prim.Line3D; +import org.jlab.geom.prim.Point3D; +import org.jlab.geom.prim.Vector3D; + +/** + * + * @author baltzell + */ +public abstract class ASwim extends SwimPars implements ISwim { + + @Override + public double[] SwimToPlaneTiltSecSys(int sector, double z_cm) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public double[] SwimToPlaneTiltSecSysBdlXZPlane(int sector, double z_cm) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public double[] SwimToPlaneBoundary(double d_cm, Vector3D n, int dir) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public double[] SwimToPlaneLab(double z_cm) { + return SwimPlane(new Vector3D(0,0,1), new Point3D(0,0,z_cm), accuracy); + } + + @Override + public double[] SwimToCylinder(double radius) { + return SwimGenCylinder(new Point3D(0,0,-1), new Point3D(0,0,1), radius, accuracy); + } + + @Override + public double[] SwimToZ(double Z, int dir) { + return SwimPlane(new Vector3D(0,0,dir*1), new Point3D(0,0,Z), accuracy); + } + + @Override + public double[] SwimToBeamLine(double xB, double yB) { + return SwimToLine(new Line3D(xB,yB,-1,xB,yB,1)); + } + +} diff --git a/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/AdaptiveSwim.java b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/AdaptiveSwim.java new file mode 100644 index 0000000000..073a88891e --- /dev/null +++ b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/AdaptiveSwim.java @@ -0,0 +1,143 @@ +package org.jlab.clas.swimtools; + +import org.jlab.geom.prim.Line3D; +import org.jlab.geom.prim.Point3D; +import org.jlab.geom.prim.Vector3D; + +import cnuphys.adaptiveSwim.geometry.Line; +import cnuphys.adaptiveSwim.geometry.Point; +import cnuphys.adaptiveSwim.geometry.Vector; +import cnuphys.adaptiveSwim.geometry.Sphere; +import cnuphys.adaptiveSwim.geometry.Cylinder; + +import cnuphys.adaptiveSwim.AdaptiveSwimException; +import cnuphys.adaptiveSwim.AdaptiveSwimResult; +import cnuphys.adaptiveSwim.AdaptiveSwimmer; +import cnuphys.adaptiveSwim.geometry.Plane; + +import cnuphys.swim.SwimTrajectory; + +public class AdaptiveSwim extends ASwim { + + private static double[] convert(AdaptiveSwimResult result, double p) { + + double[] value = null; + + if (result.getStatus() == AdaptiveSwimmer.SWIM_SUCCESS) { + value = new double[8]; + value[0] = result.getUf()[0] * 100; // convert back to cm + value[1] = result.getUf()[1] * 100; // convert back to cm + value[2] = result.getUf()[2] * 100; // convert back to cm + value[3] = result.getUf()[3] * p; // normalized values + value[4] = result.getUf()[4] * p; + value[5] = result.getUf()[5] * p; + value[6] = result.getFinalS() * 100; + value[7] = 0; // Conversion from kG.m to T.cm + } + + return value; + } + + @Override + public double[] SwimRho(double radius, double accuracy) { + + // convert to meters: + radius = radius/100; + + try { + AdaptiveSwimResult result = new AdaptiveSwimResult(false); + PC.AS.swimRho(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, radius, + accuracy/100, _rMax, stepSize, cnuphys.swim.Swimmer.getEps(), result); + return convert(result, _pTot); + } + catch (AdaptiveSwimException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public double[] SwimGenCylinder(Point3D axisPoint1, Point3D axisPoint2, double radius, double accuracy) { + + // convert to meters: + radius = radius/100; + Point a1 = new Point(axisPoint1.x()/100, axisPoint1.y()/100, axisPoint1.z()/100); + Point a2 = new Point(axisPoint2.x()/100, axisPoint2.y()/100, axisPoint2.z()/100); + Cylinder targetCylinder = new Cylinder(new Line(a1,a2), radius); + + try { + AdaptiveSwimResult result = new AdaptiveSwimResult(false); + PC.AS.swimCylinder(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, targetCylinder, + accuracy/100, _rMax, stepSize, cnuphys.swim.Swimmer.getEps(), result); + return convert(result, _pTot); + } + catch (AdaptiveSwimException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public double[] SwimPlane(Vector3D n, Point3D p, double accuracy) { + + // convert to meters: + Vector norm = new Vector(n.asUnit().x(), n.asUnit().y(), n.asUnit().z()); + Point point = new Point(p.x()/100, p.y()/100, p.z()/100); + Plane targetPlane = new Plane(norm, point); + + try { + AdaptiveSwimResult result = new AdaptiveSwimResult(false); + PC.AS.swimPlane(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, targetPlane, + accuracy/100, _rMax, stepSize, cnuphys.swim.Swimmer.getEps(), result); + return convert(result, _pTot); + } + catch (AdaptiveSwimException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public double[] SwimToSphere(double Rad) { + + // convert to meters: + Sphere targetSphere = new Sphere(new Point(0,0,0), Rad/100); + + try { + AdaptiveSwimResult result = new AdaptiveSwimResult(false); + PC.AS.swimSphere(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, targetSphere, + accuracy/100, _rMax, stepSize, cnuphys.swim.Swimmer.getEps(), result); + return convert(result, _pTot); + } + catch (AdaptiveSwimException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public double[] SwimToLine(Line3D l) { + + // convert to meters: + Point a1 = new Point(l.origin().x()/100, l.origin().y()/100, l.origin().z()/100); + Point a2 = new Point(l.end().x()/100, l.end().y()/100, l.end().z()/100); + Line targetLine = new Line(a1, a2); + + try { + AdaptiveSwimResult result = new AdaptiveSwimResult(false); + PC.AS.swimLine(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, targetLine, + accuracy/100, _rMax, stepSize, cnuphys.swim.Swimmer.getEps(), result); + return convert(result, _pTot); + } + catch (AdaptiveSwimException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public double[] SwimToDCA(SwimTrajectory trk2) { + throw new UnsupportedOperationException("Not supported yet."); + } + +} diff --git a/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/ISwim.java b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/ISwim.java new file mode 100644 index 0000000000..b5f469d665 --- /dev/null +++ b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/ISwim.java @@ -0,0 +1,40 @@ +package org.jlab.clas.swimtools; + +import cnuphys.swim.SwimTrajectory; +import org.jlab.geom.prim.Line3D; +import org.jlab.geom.prim.Point3D; +import org.jlab.geom.prim.Vector3D; + +/** + * + * @author baltzell + */ +interface ISwim { + + public double[] SwimToPlaneTiltSecSys(int sector, double z_cm); + + public double[] SwimToPlaneTiltSecSysBdlXZPlane(int sector, double z_cm); + + public double[] SwimToPlaneLab(double z_cm); + + public double[] SwimToCylinder(double Rad); + + public double[] SwimRho(double radius, double accuracy); + + public double[] SwimGenCylinder(Point3D axisPoint1, Point3D axisPoint2, double radius, double accuracy); + + public double[] SwimPlane(Vector3D n, Point3D p, double accuracy); + + public double[] SwimToSphere(double Rad); + + public double[] SwimToPlaneBoundary(double d_cm, Vector3D n, int dir); + + public double[] SwimToBeamLine(double xB, double yB); + + public double[] SwimToLine(Line3D l); + + public double[] SwimToZ(double Z, int dir); + + public double[] SwimToDCA(SwimTrajectory trk2); + +} diff --git a/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/MagFieldsEngine.java b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/MagFieldsEngine.java index ceab50389a..b02d767101 100644 --- a/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/MagFieldsEngine.java +++ b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/MagFieldsEngine.java @@ -1,8 +1,9 @@ package org.jlab.clas.swimtools; +import cnuphys.magfield.MagneticFieldInitializationException; import cnuphys.magfield.MagneticFields; +import java.io.FileNotFoundException; import java.util.Arrays; -import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; @@ -14,7 +15,7 @@ public class MagFieldsEngine extends ReconstructionEngine { - public static Logger LOGGER = Logger.getLogger(MagFieldsEngine.class.getName()); + public static final Logger LOGGER = Logger.getLogger(MagFieldsEngine.class.getName()); private String solShift = null; @@ -22,8 +23,6 @@ public MagFieldsEngine() { super("MagFields", "ziegler", "1.0"); } - AtomicInteger Run = new AtomicInteger(0); - /** * Choose one of YAML or ENV values. * @@ -57,19 +56,19 @@ public boolean initializeMagneticFields() { final String mapDir = CLASResources.getResourcePath("etc")+"/data/magfield"; if (torusMap==null) { - LOGGER.log(Level.SEVERE,"["+this.getName()+"] ERROR: torus field is undefined."); + LOGGER.log(Level.SEVERE, "[{0}] ERROR: torus field is undefined.", this.getName()); return false; } if (solenoidMap==null) { - LOGGER.log(Level.SEVERE,"["+this.getName()+"] ERROR: solenoid is undefined."); + LOGGER.log(Level.SEVERE, "[{0}] ERROR: solenoid is undefined.", this.getName()); return false; } try { MagneticFields.getInstance().initializeMagneticFields(mapDir, torusMap, solenoidMap); } - catch (Exception e) { - e.printStackTrace(); + catch (MagneticFieldInitializationException | FileNotFoundException e) { + LOGGER.log(Level.SEVERE,"Magfields error",e); return false; } @@ -77,77 +76,67 @@ public boolean initializeMagneticFields() { solShift = this.getEngineConfigString("magfieldSolenoidShift"); if (solShift != null) { - LOGGER.log(Level.INFO, "[" + this.getName() - + "] run with solenoid z shift in tracking config chosen based on yaml = " + solShift + " cm"); - Swimmer.set_zShift(Float.valueOf(solShift)); + LOGGER.log(Level.INFO, "[{0}] run with solenoid z shift in tracking config chosen based on yaml = {1} cm", new Object[]{this.getName(), solShift}); + Swimmer.set_zShift(Float.parseFloat(solShift)); } else { solShift = System.getenv("COAT_MAGFIELD_SOLENOIDSHIFT"); if (solShift != null) { - LOGGER.log(Level.INFO, "[" + this.getName() - + "] run with solenoid z shift in tracking config chosen based on env = " + solShift + " cm"); - Swimmer.set_zShift(Float.valueOf(solShift)); + LOGGER.log(Level.INFO, "[{0}] run with solenoid z shift in tracking config chosen based on env = {1} cm", new Object[]{this.getName(), solShift}); + Swimmer.set_zShift(Float.parseFloat(solShift)); } } if (solShift == null) { - LOGGER.log(Level.INFO, "[" + this.getName() + "] run with solenoid z shift based on CCDB CD position"); + LOGGER.log(Level.INFO, "[{0}] run with solenoid z shift based on CCDB CD position", this.getName()); // this.solenoidShift = (float) 0; } // torus: String TorX = this.getEngineConfigString("magfieldTorusXShift"); if (TorX != null) { - LOGGER.log(Level.INFO, "[" + this.getName() - + "] run with torus x shift in tracking config chosen based on yaml = " + TorX + " cm"); - Swimmer.setTorXShift(Float.valueOf(TorX)); + LOGGER.log(Level.INFO, "[{0}] run with torus x shift in tracking config chosen based on yaml = {1} cm", new Object[]{this.getName(), TorX}); + Swimmer.setTorXShift(Float.parseFloat(TorX)); } else { TorX = System.getenv("COAT_MAGFIELD_TORUSXSHIFT"); if (TorX != null) { - LOGGER.log(Level.INFO, "[" + this.getName() - + "] run with torus x shift in tracking config chosen based on env = " + TorX + " cm"); - Swimmer.setTorXShift(Float.valueOf(TorX)); + LOGGER.log(Level.INFO, "[{0}] run with torus x shift in tracking config chosen based on env = {1} cm", new Object[]{this.getName(), TorX}); + Swimmer.setTorXShift(Float.parseFloat(TorX)); } } if (TorX == null) { - LOGGER.log(Level.INFO, "[" + this.getName() + "] run with torus x shift in tracking set to 0 cm"); + LOGGER.log(Level.INFO, "[{0}] run with torus x shift in tracking set to 0 cm", this.getName()); // this.solenoidShift = (float) 0; } String TorY = this.getEngineConfigString("magfieldTorusYShift"); if (TorY != null) { - LOGGER.log(Level.INFO, "[" + this.getName() - + "] run with torus y shift in tracking config chosen based on yaml = " + TorY + " cm"); - Swimmer.setTorYShift(Float.valueOf(TorY)); + LOGGER.log(Level.INFO, "[{0}] run with torus y shift in tracking config chosen based on yaml = {1} cm", new Object[]{this.getName(), TorY}); + Swimmer.setTorYShift(Float.parseFloat(TorY)); } else { TorY = System.getenv("COAT_MAGFIELD_TORUSYSHIFT"); if (TorY != null) { - LOGGER.log(Level.INFO, "[" + this.getName() - + "] run with torus y shift in tracking config chosen based on env = " + TorY + " cm"); - Swimmer.setTorYShift(Float.valueOf(TorY)); + LOGGER.log(Level.INFO, "[{0}] run with torus y shift in tracking config chosen based on env = {1} cm", new Object[]{this.getName(), TorY}); + Swimmer.setTorYShift(Float.parseFloat(TorY)); } } if (TorY == null) { - LOGGER.log(Level.INFO, "[" + this.getName() + "] run with torus y shift in tracking set to 0 cm"); - // this.solenoidShift = (float) 0; + LOGGER.log(Level.INFO, "[{0}] run with torus y shift in tracking set to 0 cm", this.getName()); } String TorZ = this.getEngineConfigString("magfieldTorusZShift"); if (TorZ != null) { - LOGGER.log(Level.INFO, "[" + this.getName() - + "] run with torus z shift in tracking config chosen based on yaml = " + TorZ + " cm"); - Swimmer.setTorZShift(Float.valueOf(TorZ)); + LOGGER.log(Level.INFO, "[{0}] run with torus z shift in tracking config chosen based on yaml = {1} cm", new Object[]{this.getName(), TorZ}); + Swimmer.setTorZShift(Float.parseFloat(TorZ)); } else { TorZ = System.getenv("COAT_MAGFIELD_TORUSZSHIFT"); if (TorZ != null) { - LOGGER.log(Level.INFO, "[" + this.getName() - + "] run with torus z shift in tracking config chosen based on env = " + TorZ + " cm"); - Swimmer.setTorZShift(Float.valueOf(TorZ)); + LOGGER.log(Level.INFO, "[{0}] run with torus z shift in tracking config chosen based on env = {1} cm", new Object[]{this.getName(), TorZ}); + Swimmer.setTorZShift(Float.parseFloat(TorZ)); } } if (TorZ == null) { - LOGGER.log(Level.INFO, "[" + this.getName() + "] run with torus z shift in tracking set to 0 cm"); - // this.solenoidShift = (float) 0; + LOGGER.log(Level.INFO, "[{0}] run with torus z shift in tracking set to 0 cm", this.getName()); } return true; @@ -170,9 +159,10 @@ public boolean processDataEvent(DataEvent event) { if (newRun == 0) return true; - if (solShift == null) { // if no shift is set in the yaml file or environment, read from CCDB - // will read target position and assume that is representative of the shift of - // the whole CD + if (solShift == null) { + // if no shift is set in the yaml file or environment, read from CCDB + // will read target position and assume that is representative of the + // shift of the whole CD IndexedTable targetPosition = this.getConstantsManager().getConstants(newRun, "/geometry/shifts/solenoid"); Swimmer.set_zShift((float) targetPosition.getDoubleValue("z", 0, 0, 0)); } diff --git a/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/ProbeCollection.java b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/ProbeCollection.java index 043ffec40e..f410927af6 100644 --- a/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/ProbeCollection.java +++ b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/ProbeCollection.java @@ -1,8 +1,3 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package org.jlab.clas.swimtools; import cnuphys.magfield.CompositeProbe; diff --git a/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/Stoppers.java b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/Stoppers.java new file mode 100644 index 0000000000..2532e746a8 --- /dev/null +++ b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/Stoppers.java @@ -0,0 +1,324 @@ +package org.jlab.clas.swimtools; + +import cnuphys.rk4.IStopper; +import cnuphys.swim.SwimTrajectory; +import java.util.ArrayList; +import org.jlab.geom.prim.Line3D; +import org.jlab.geom.prim.Point3D; +import org.jlab.geom.prim.Vector3D; + +public class Stoppers { + + public static class CylindricalBoundarySwimStopper implements IStopper { + + private double _finalPathLength = Double.NaN; + private double _Rad; + double max = -1.0; + + /** + * A swim stopper that will stop if the boundary of a plane is crossed + * + * @param Rad the max radial coordinate in meters. + */ + public CylindricalBoundarySwimStopper(double Rad) { + _Rad = Rad; + } + + @Override + public boolean stopIntegration(double t, double[] y) { + double r = Math.sqrt(y[0] * y[0] + y[1] * y[1]) * 100.; + return (r < max || r > _Rad); // stop intergration at closest distance to the cylinder + } + + /** + * Get the final path length in meters + * + * @return the final path length in meters + */ + @Override + public double getFinalT() { + return _finalPathLength; + } + + /** + * Set the final path length in meters + * + * @param finalPathLength + * the final path length in meters + */ + @Override + public void setFinalT(double finalPathLength) { + _finalPathLength = finalPathLength; + } + } + + public static class SphericalBoundarySwimStopper implements IStopper { + + private double _finalPathLength = Double.NaN; + private double _Rad; + + /** + * A swim stopper that will stop if the boundary of a plane is crossed + * + * @param maxR the max radial coordinate in meters. + */ + public SphericalBoundarySwimStopper(double Rad) { + _Rad = Rad; + } + + @Override + public boolean stopIntegration(double t, double[] y) { + double r = Math.sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]) * 100.; + return r > _Rad; + } + + /** + * Get the final path length in meters + * + * @return the final path length in meters + */ + @Override + public double getFinalT() { + return _finalPathLength; + } + + /** + * Set the final path length in meters + * + * @param finalPathLength + * the final path length in meters + */ + @Override + public void setFinalT(double finalPathLength) { + _finalPathLength = finalPathLength; + } + } + + // added for swimming to outer detectors + public static class PlaneBoundarySwimStopper implements IStopper { + + private double _finalPathLength = Double.NaN; + private double _d; + private Vector3D _n; + private double _dist2plane; + private int _dir; + + /** + * A swim stopper that will stop if the boundary of a plane is crossed + * + * @param d + * @param dir + * @param n + */ + public PlaneBoundarySwimStopper(double d, Vector3D n, int dir) { + _d = d; + _n = n; + _dir = dir; + } + + @Override + public boolean stopIntegration(double t, double[] y) { + double dtrk = y[0] * _n.x() + y[1] * _n.y() + y[2] * _n.z(); + //double accuracy = 20e-6; // 20 microns + return _dir < 0 ? dtrk < _d : dtrk > _d; + } + + @Override + public double getFinalT() { + return _finalPathLength; + } + + /** + * Set the final path length in meters + * + * @param finalPathLength the final path length in meters + */ + @Override + public void setFinalT(double finalPathLength) { + _finalPathLength = finalPathLength; + } + } + + public static class LineSwimStopper implements IStopper { + + private double _finalPathLength = Double.NaN; + private Line3D _l; + private Point3D _p; + double min = 999; + public LineSwimStopper(Line3D l) { + _l =l; + _p = new Point3D(); + } + + @Override + public boolean stopIntegration(double t, double[] y) { + _p.set(y[0]* 100.0, y[1]* 100.0, y[2]* 100.0); + double doca = _l.distance(_p).length(); + if(doca min ); + } + + /** + * Get the final path length in meters + * + * @return the final path length in meters + */ + @Override + public double getFinalT() { + return _finalPathLength; + } + + /** + * Set the final path length in meters + * + * @param finalPathLength + * the final path length in meters + */ + @Override + public void setFinalT(double finalPathLength) { + _finalPathLength = finalPathLength; + } + } + + public static class ZSwimStopper implements IStopper { + + private double _finalPathLength = Double.NaN; + private double _Z; + private int _dir; + + public ZSwimStopper(double Z, int dir) { + _Z = Z; + _dir = dir; + } + + @Override + public boolean stopIntegration(double t, double[] y) { + double z = y[2] * 100.; + return _dir>0 ? z>_Z : z<_Z; + } + + /** + * Get the final path length in meters + * + * @return the final path length in meters + */ + @Override + public double getFinalT() { + return _finalPathLength; + } + + /** + * Set the final path length in meters + * + * @param finalPathLength + * the final path length in meters + */ + @Override + public void setFinalT(double finalPathLength) { + _finalPathLength = finalPathLength; + } + } + + public static class DCASwimStopper implements IStopper { + + public DCASwimStopper(SwimTrajectory swimTraj) { + _swimTraj = swimTraj; + for(int i = 0; i < _swimTraj.size()-1; i++) { + polylines.add(new Line3D(_swimTraj.get(i)[0],_swimTraj.get(i)[1],_swimTraj.get(i)[2], + _swimTraj.get(i+1)[0],_swimTraj.get(i+1)[1],_swimTraj.get(i+1)[2])); + } + } + + private ArrayList polylines = new ArrayList<>(); + private SwimTrajectory _swimTraj; + private double _finalPathLength = Double.NaN; + private double _doca = Double.POSITIVE_INFINITY; + + @Override + public boolean stopIntegration(double t, double[] y) { + + Point3D dcaCand = new Point3D(y[0],y[1],y[2]); + double maxDoca = Double.POSITIVE_INFINITY; + + for(Line3D l : polylines) { + if(l.distance(dcaCand).length() min ; + + } + + /** + * Get the final path length in meters + * + * @return the final path length in meters + */ + @Override + public double getFinalT() { + return _finalPathLength; + } + + /** + * Set the final path length in meters + * + * @param finalPathLength + * the final path length in meters + */ + @Override + public void setFinalT(double finalPathLength) { + _finalPathLength = finalPathLength; + } + } + +} \ No newline at end of file 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 66897dc346..cfe18bc05f 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 @@ -1,281 +1,100 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package org.jlab.clas.swimtools; -import cnuphys.adaptiveSwim.AdaptiveSwimException; -import cnuphys.rk4.IStopper; +import org.jlab.geom.prim.Vector3D; +import org.jlab.geom.prim.Point3D; +import org.jlab.geom.prim.Line3D; + import cnuphys.rk4.RungeKuttaException; + import cnuphys.swim.SwimTrajectory; import cnuphys.swim.util.Plane; + import cnuphys.swimZ.SwimZException; import cnuphys.swimZ.SwimZResult; import cnuphys.swimZ.SwimZStateVector; -import org.apache.commons.math3.util.FastMath; -import org.jlab.geom.prim.Vector3D; -import org.jlab.geom.prim.Point3D; + import cnuphys.adaptiveSwim.AdaptiveSwimResult; -import cnuphys.adaptiveSwim.AdaptiveSwimmer; -import cnuphys.adaptiveSwim.geometry.Cylinder; -import cnuphys.adaptiveSwim.geometry.Line; -import cnuphys.adaptiveSwim.geometry.Point; -import cnuphys.adaptiveSwim.geometry.Vector; -import java.util.ArrayList; -import java.util.List; -import org.jlab.geom.prim.Line3D; +import org.jlab.clas.swimtools.Stoppers.BeamLineSwimStopper; + +import org.jlab.clas.swimtools.Stoppers.CylindricalBoundarySwimStopper; +import org.jlab.clas.swimtools.Stoppers.DCASwimStopper; +import org.jlab.clas.swimtools.Stoppers.LineSwimStopper; +import org.jlab.clas.swimtools.Stoppers.SphericalBoundarySwimStopper; +import org.jlab.clas.swimtools.Stoppers.ZSwimStopper; + /** + * Class for swimming to various surfaces. The input and output units are cm and GeV/c * * @author ziegler */ +public class Swim extends ASwim { -public class Swim { - - private double _x0; - private double _y0; - private double _z0; - private double _phi; - private double _theta; - private double _pTot; - private final double _rMax = 5 + 3; // increase to allow swimming to outer - // detectors - private double _maxPathLength = 9; - private boolean SwimUnPhys = false; //Flag to indicate if track is swimmable - private int _charge; - - final double SWIMZMINMOM = 0.75; // GeV/c - final double MINTRKMOM = 0.05; // GeV/c - double accuracy = 20e-6; // 20 microns - public double stepSize = 5.00 * 1.e-4; // 500 microns - public double distanceBetweenSaves= 100*stepSize; - - private ProbeCollection PC; + private SwimTrajectory swimTraj; /** - * Class for swimming to various surfaces. The input and output units are cm and GeV/c - */ - public Swim() { - PC = Swimmer.getProbeCollection(Thread.currentThread()); - if (PC == null) { - PC = new ProbeCollection(); - Swimmer.put(Thread.currentThread(), PC); - } - } - - /** - * Set max swimming path length - * - * @param maxPathLength - */ - public void setMaxPathLength(double _maxPathLength) { - this._maxPathLength = _maxPathLength; - } - - /** - * - * @param direction - * +1 for out -1 for in - * @param x0 - * @param y0 - * @param z0 - * @param thx - * @param thy - * @param p - * @param charge - */ - public void SetSwimParameters(int direction, double x0, double y0, double z0, double thx, double thy, double p, - int charge) { - - // x,y,z in m = swimmer units - _x0 = x0 / 100; - _y0 = y0 / 100; - _z0 = z0 / 100; - this.checkR(_x0, _y0, _z0); - double pz = direction * p / Math.sqrt(thx * thx + thy * thy + 1); - double px = thx * pz; - double py = thy * pz; - _phi = Math.toDegrees(FastMath.atan2(py, px)); - _pTot = Math.sqrt(px * px + py * py + pz * pz); - _theta = Math.toDegrees(Math.acos(pz / _pTot)); - - _charge = direction * charge; - } - - /** - * Sets the parameters used by swimmer based on the input track state vector - * parameters swimming outwards - * - * @param superlayerIdx - * @param layerIdx - * @param x0 - * @param y0 - * @param thx - * @param thy - * @param p - * @param charge - */ - public void SetSwimParameters(int superlayerIdx, int layerIdx, double x0, double y0, double z0, double thx, - double thy, double p, int charge) { - // z at a given DC plane in the tilted coordinate system - // x,y,z in m = swimmer units - _x0 = x0 / 100; - _y0 = y0 / 100; - _z0 = z0 / 100; - this.checkR(_x0, _y0, _z0); - double pz = p / Math.sqrt(thx * thx + thy * thy + 1); - double px = thx * pz; - double py = thy * pz; - _phi = Math.toDegrees(FastMath.atan2(py, px)); - _pTot = Math.sqrt(px * px + py * py + pz * pz); - _theta = Math.toDegrees(Math.acos(pz / _pTot)); - - _charge = charge; - - } - - /** - * Sets the parameters used by swimmer based on the input track parameters - * - * @param x0 - * @param y0 - * @param z0 - * @param px - * @param py - * @param pz - * @param charge + * @return the swimTraj */ - public void SetSwimParameters(double x0, double y0, double z0, double px, double py, double pz, int charge) { - _x0 = x0 / 100; - _y0 = y0 / 100; - _z0 = z0 / 100; - this.checkR(_x0, _y0, _z0); - _phi = Math.toDegrees(FastMath.atan2(py, px)); - _pTot = Math.sqrt(px * px + py * py + pz * pz); - _theta = Math.toDegrees(Math.acos(pz / _pTot)); - - _charge = charge; - + public SwimTrajectory getSwimTraj() { + return swimTraj; } /** - * - * @param xcm - * @param ycm - * @param zcm - * @param phiDeg - * @param thetaDeg - * @param p - * @param charge - * @param maxPathLength + * @param swimTraj the swimTraj to set */ - public void SetSwimParameters(double xcm, double ycm, double zcm, double phiDeg, double thetaDeg, double p, - int charge, double maxPathLength) { - - _maxPathLength = maxPathLength; - _charge = charge; - _phi = phiDeg; - _theta = thetaDeg; - _pTot = p; - _x0 = xcm / 100; - _y0 = ycm / 100; - _z0 = zcm / 100; - this.checkR(_x0, _y0, _z0); + public void setSwimTraj(SwimTrajectory swimTraj) { + this.swimTraj = swimTraj; } - + /** * - * @param xcm - * @param ycm - * @param zcm - * @param phiDeg - * @param thetaDeg - * @param p - * @param charge - * @param maxPathLength - * @param Accuracy - * @param StepSize + * @param sector + * @param z_cm + * @return */ - public void SetSwimParameters(double xcm, double ycm, double zcm, double phiDeg, double thetaDeg, double p, - int charge, double maxPathLength, double Accuracy, double StepSize) { - - _maxPathLength = maxPathLength; - accuracy = Accuracy/100; - stepSize = StepSize/100; - _charge = charge; - _phi = phiDeg; - _theta = thetaDeg; - _pTot = p; - _x0 = xcm / 100; - _y0 = ycm / 100; - _z0 = zcm / 100; - this.checkR(_x0, _y0, _z0); - } - + @Override public double[] SwimToPlaneTiltSecSys(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; + // Fiducial Cut: + if (_pTot < MINTRKMOM || this.SwimUnPhys==true) return null; - SwimTrajectory traj = null; + double[] value = new double[8]; double hdata[] = new double[3]; try { + // Try to use new Z-Swimmer: + SwimZResult szr = null; 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); + 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); + szr = null; } } - if (szr != null) { double bdl = szr.sectorGetBDL(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[0] = last.x; // cm + value[1] = last.y; // cm + value[2] = last.z; // 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, + } + + // Use older swimmer: + else { + final double z = z_cm / 100; // convert to meters + SwimTrajectory 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; - + 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 @@ -285,57 +104,47 @@ public double[] SwimToPlaneTiltSecSys(int sector, double z_cm) { value[5] = lastY[5] * _pTot; value[6] = lastY[6] * 100; value[7] = lastY[7] * 10; - } // use old swimmer + } } catch (Exception e) { - e.printStackTrace(); + e.printStackTrace(); } 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; - } + /** + * + * @param sector + * @param z_cm + * @return + */ + @Override + public double[] SwimToPlaneTiltSecSysBdlXZPlane(int sector, double z_cm) { - // use a SwimZResult instead of a trajectory (dph) - SwimZResult szr = null; + // Fiducial Cut: + if (_pTot < MINTRKMOM || this.SwimUnPhys==true) return null; - SwimTrajectory traj = null; double hdata[] = new double[3]; + double[] value = new double[8]; try { + // Try to use new Z-Swimmer: + SwimZResult szr = null; 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); + 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); + szr = null; } } - 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 @@ -344,19 +153,15 @@ public double[] SwimToPlaneTiltSecSysBdlXZPlane(int sector, double z_cm) { 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, + } + + // Use older swimmer: + else { + double z = z_cm / 100; // convert to meters + SwimTrajectory 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; - + 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 @@ -366,12 +171,11 @@ public double[] SwimToPlaneTiltSecSysBdlXZPlane(int sector, double z_cm) { value[5] = lastY[5] * _pTot; value[6] = lastY[6] * 100; value[7] = lastY[7] * 10; - } // use old swimmer + } } catch (Exception e) { - e.printStackTrace(); + e.printStackTrace(); } return value; - } /** @@ -379,49 +183,33 @@ public double[] SwimToPlaneTiltSecSysBdlXZPlane(int sector, double z_cm) { * @param z_cm * @return state x,y,z,px,py,pz, pathlength, iBdl at the plane surface */ + @Override public double[] SwimToPlaneLab(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; - } - SwimTrajectory traj = null; - double hdata[] = new double[3]; - // use a SwimZResult instead of a trajectory (dph) - SwimZResult szr = null; + // Fiducial Cut: + if (_pTot < MINTRKMOM || this.SwimUnPhys==true) return null; + double hdata[] = new double[3]; + double[] value = new double[8]; + try { + // Try to use new Z-Swimmer: + SwimZResult szr = null; 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.CF_z.adaptiveRK(_charge, _pTot, start, z_cm, stepSizeCM, hdata); + szr = PC.CF_z.adaptiveRK(_charge, _pTot, start, z_cm, stepSizeCM, hdata); } catch (SwimZException e) { - szr = null; - //System.err.println("[WARNING] SwimZ Failed for p = " + _pTot); - + szr = null; } } - if (szr != null) { double bdl = szr.getBDL(PC.CF_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 @@ -430,17 +218,16 @@ public double[] SwimToPlaneLab(double z_cm) { 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.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, z, accuracy, _rMax, _maxPathLength, + } + + // Use older swimmer: + else { + double z = z_cm / 100; // the magfield method uses meters + SwimTrajectory traj = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, z, accuracy, _rMax, _maxPathLength, stepSize, cnuphys.swim.Swimmer.CLAS_Tolerance, hdata); - if(traj==null) - return null; + if (traj==null) return null; traj.computeBDL(PC.CP); - // traj.computeBDL(compositeField); - 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 @@ -452,91 +239,11 @@ public double[] SwimToPlaneLab(double z_cm) { } // old swimmer } catch (RungeKuttaException e) { - e.printStackTrace(); + e.printStackTrace(); } return value; - - } - - private void checkR(double _x0, double _y0, double _z0) { - this.SwimUnPhys=false; - if(Math.sqrt(_x0*_x0 + _y0*_y0)>this._rMax || - Math.sqrt(_x0*_x0 + _y0*_y0 + _z0*_z0)>this._maxPathLength) - this.SwimUnPhys=true; } - /** - * Cylindrical stopper - */ - private class CylindricalBoundarySwimStopper implements IStopper { - - private double _finalPathLength = Double.NaN; - private double _Rad; - //boolean cutOff = false; - // check that the track can get to R. Stops at the track radius - //float[] b = new float[3]; - //double x0 = _x0*100; - //double y0 = _y0*100; - //double z0 = _z0*100; - - double max = -1.0; - /** - * A swim stopper that will stop if the boundary of a plane is crossed - * - * @param maxR - * the max radial coordinate in meters. - */ - private CylindricalBoundarySwimStopper(double Rad) { - // DC reconstruction units are cm. Swim units are m. Hence scale by - // 100 - _Rad = Rad; - // check if the track will reach the surface of the cylinder. - //BfieldLab(x0, y0, z0, b); - //double trkR = _pTot*Math.sin(Math.toRadians(_theta))/Math.abs(b[2]*LIGHTVEL); - //double trkXc = x0 + trkR * Math.sin(Math.toRadians(_phi)); - //if(trkR<(Rad+trkXc) && Math.sqrt(x0*x0+y0*y0)<_Rad) { // check only for swimming inside out. - // cutOff=true; - //} - } - - @Override - public boolean stopIntegration(double t, double[] y) { - - double r = Math.sqrt(y[0] * y[0] + y[1] * y[1]) * 100.; -// if(r>max ) -// max = r; -// else System.out.println(r + " " + max + " " + t); - //if(cutOff) { - return (r < max || r > _Rad); // stop intergration at closest distance to the cylinder - //} - //else { - // return (r > _Rad); - //} - } - - /** - * Get the final path length in meters - * - * @return the final path length in meters - */ - @Override - public double getFinalT() { - return _finalPathLength; - } - - /** - * Set the final path length in meters - * - * @param finalPathLength - * the final path length in meters - */ - @Override - public void setFinalT(double finalPathLength) { - _finalPathLength = finalPathLength; - } - } - //private final double LIGHTVEL = 0.000299792458 ; - /** * * @param Rad @@ -544,21 +251,17 @@ public void setFinalT(double finalPathLength) { */ public double[] SwimToCylinder(double Rad) { + if (this.SwimUnPhys) return null; double[] value = new double[8]; - if(this.SwimUnPhys) - return null; CylindricalBoundarySwimStopper stopper = new CylindricalBoundarySwimStopper(Rad); - SwimTrajectory st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, stopper, _maxPathLength, stepSize, - 0.0005); - if(st==null) - return null; + SwimTrajectory st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, + stopper, _maxPathLength, stepSize, 0.0005); + if (st==null) return null; st.computeBDL(PC.CP); - // st.computeBDL(compositeField); double[] lastY = st.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 @@ -567,39 +270,26 @@ public double[] SwimToCylinder(double Rad) { value[5] = lastY[5] * _pTot; value[6] = lastY[6] * 100; value[7] = lastY[7] * 10; // Conversion from kG.m to T.cm - return value; - } - /** - * - * @param radius in cm - * @return state x,y,z,px,py,pz, pathlength, iBdl at the surface - */ - public double[] SwimRho(double radius) { - return SwimRho(radius, accuracy*100); - } - /** * * @param radius in cm * @param accuracy in cm * @return state x,y,z,px,py,pz, pathlength, iBdl at the surface */ + @Override public double[] SwimRho(double radius, double accuracy) { + if(this.SwimUnPhys) return null; double[] value = null; - // using adaptive stepsize - if(this.SwimUnPhys) - return null; - try { - AdaptiveSwimResult result = new AdaptiveSwimResult(false); - PC.CF.swimRho(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, radius/100, accuracy/100, _rMax, stepSize, cnuphys.swim.Swimmer.CLAS_Tolerance, result); + PC.CF.swimRho(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, + radius/100, accuracy/100, _rMax, stepSize, cnuphys.swim.Swimmer.CLAS_Tolerance, result); if(result.getStatus()==0) { value = new double[8]; @@ -612,24 +302,11 @@ public double[] SwimRho(double radius, double accuracy) { value[6] = result.getFinalS() * 100; value[7] = 0; // Conversion from kG.m to T.cm } - } catch (RungeKuttaException e) { - System.out.println(_charge + " " + _x0 + " " + _y0 + " " + _z0 + " " + _pTot + " " + _theta + " " + _phi); - e.printStackTrace(); + System.out.println(_charge + " " + _x0 + " " + _y0 + " " + _z0 + " " + _pTot + " " + _theta + " " + _phi); + e.printStackTrace(); } return value; - - } - - /** - * - * @param axisPoint1 in cm - * @param axisPoint2 in cm - * @param radius in cm - * @return swam trajectory to the cylinder - */ - public double[] SwimGenCylinder(Point3D axisPoint1, Point3D axisPoint2, double radius) { - return SwimGenCylinder(axisPoint1, axisPoint2, radius, accuracy*100); } /** @@ -640,8 +317,11 @@ public double[] SwimGenCylinder(Point3D axisPoint1, Point3D axisPoint2, double r * @param accuracy in cm * @return swam trajectory to the cylinder */ + @Override public double[] SwimGenCylinder(Point3D axisPoint1, Point3D axisPoint2, double radius, double accuracy) { + if(this.SwimUnPhys) return null; + double[] value = null; double[] p1 = new double[3]; double[] p2 = new double[3]; @@ -652,13 +332,7 @@ public double[] SwimGenCylinder(Point3D axisPoint1, Point3D axisPoint2, double r p2[1] = axisPoint2.y()/100; p2[2] = axisPoint2.z()/100; - Cylinder targCyl = new Cylinder(p1, p2, radius/100); - // using adaptive stepsize - if(this.SwimUnPhys) - return null; - try { - AdaptiveSwimResult result = new AdaptiveSwimResult(false); PC.CF.swimCylinder(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, @@ -675,33 +349,35 @@ public double[] SwimGenCylinder(Point3D axisPoint1, Point3D axisPoint2, double r value[6] = result.getFinalS() * 100; value[7] = 0; // Conversion from kG.m to T.cm } - } catch (RungeKuttaException e) { - System.out.println(_charge + " " + _x0 + " " + _y0 + " " + _z0 + " " + _pTot + " " + _theta + " " + _phi); - e.printStackTrace(); + System.out.println(_charge + " " + _x0 + " " + _y0 + " " + _z0 + " " + _pTot + " " + _theta + " " + _phi); + e.printStackTrace(); } return value; } + /** + * + * @param n + * @param p + * @param accuracy + * @return + */ + @Override public double[] SwimPlane(Vector3D n, Point3D p, double accuracy) { - double[] value = null; + if (this.SwimUnPhys) return null; - - // using adaptive stepsize - if(this.SwimUnPhys) - return null; + double[] value = null; try { - AdaptiveSwimResult result = new AdaptiveSwimResult(false); PC.CF.swimPlane(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, n.x(),n.y(),n.z(),p.x()/100,p.y()/100,p.z()/100, accuracy/100, _rMax, stepSize, cnuphys.swim.Swimmer.CLAS_Tolerance, result); - if(result.getStatus()==0) { value = new double[8]; value[0] = result.getUf()[0] * 100; // convert back to cm @@ -713,86 +389,32 @@ public double[] SwimPlane(Vector3D n, Point3D p, double accuracy) { value[6] = result.getFinalS() * 100; value[7] = 0; // Conversion from kG.m to T.cm } - } catch (RungeKuttaException e) { - System.out.println(_charge + " " + _x0 + " " + _y0 + " " + _z0 + " " + _pTot + " " + _theta + " " + _phi); - e.printStackTrace(); + System.out.println(_charge + " " + _x0 + " " + _y0 + " " + _z0 + " " + _pTot + " " + _theta + " " + _phi); + e.printStackTrace(); } return value; - } - - private class SphericalBoundarySwimStopper implements IStopper { - - private double _finalPathLength = Double.NaN; - - private double _Rad; - - /** - * A swim stopper that will stop if the boundary of a plane is crossed - * - * @param maxR - * the max radial coordinate in meters. - */ - private SphericalBoundarySwimStopper(double Rad) { - // DC reconstruction units are cm. Swim units are m. Hence scale by - // 100 - _Rad = Rad; - } - - @Override - public boolean stopIntegration(double t, double[] y) { - - double r = Math.sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]) * 100.; - - return (r > _Rad); - - } - - /** - * Get the final path length in meters - * - * @return the final path length in meters - */ - @Override - public double getFinalT() { - return _finalPathLength; - } - - /** - * Set the final path length in meters - * - * @param finalPathLength - * the final path length in meters - */ - @Override - public void setFinalT(double finalPathLength) { - _finalPathLength = finalPathLength; - } - } /** * * @param Rad * @return state x,y,z,px,py,pz, pathlength, iBdl at the surface */ + @Override public double[] SwimToSphere(double Rad) { + if (this.SwimUnPhys==true) return null; double[] value = new double[8]; - // using adaptive stepsize - if(this.SwimUnPhys==true) - return null; + SphericalBoundarySwimStopper stopper = new SphericalBoundarySwimStopper(Rad); - SwimTrajectory st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, stopper, _maxPathLength, stepSize, - 0.0005); - if(st==null) - return null; + SwimTrajectory st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, + stopper, _maxPathLength, stepSize, 0.0005); + if (st==null) return null; st.computeBDL(PC.CP); - // st.computeBDL(compositeField); double[] lastY = st.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 @@ -801,66 +423,9 @@ public double[] SwimToSphere(double Rad) { value[5] = lastY[5] * _pTot; value[6] = lastY[6] * 100; value[7] = lastY[7] * 10; // Conversion from kG.m to T.cm - return value; - } - // added for swimming to outer detectors - private class PlaneBoundarySwimStopper implements IStopper { - - private double _finalPathLength = Double.NaN; - private double _d; - private Vector3D _n; - private double _dist2plane; - private int _dir; - - /** - * A swim stopper that will stop if the boundary of a plane is crossed - * - * @param maxR - * the max radial coordinate in meters. - */ - private PlaneBoundarySwimStopper(double d, Vector3D n, int dir) { - // DC reconstruction units are cm. Swim units are m. Hence scale by - // 100 - _d = d; - _n = n; - _dir = dir; - } - - @Override - public boolean stopIntegration(double t, double[] y) { - double dtrk = y[0] * _n.x() + y[1] * _n.y() + y[2] * _n.z(); - - double accuracy = 20e-6; // 20 microns - // System.out.println(" dist "+dtrk*100+ " state "+y[0]*100+", - // "+y[1]*100+" , "+y[2]*100); - if (_dir < 0) { - return dtrk < _d; - } else { - return dtrk > _d; - } - - } - - @Override - public double getFinalT() { - - return _finalPathLength; - } - - /** - * Set the final path length in meters - * - * @param finalPathLength - * the final path length in meters - */ - @Override - public void setFinalT(double finalPathLength) { - _finalPathLength = finalPathLength; - } - } /** * * @param d_cm @@ -868,23 +433,21 @@ public void setFinalT(double finalPathLength) { * @param dir * @return return state x,y,z,px,py,pz, pathlength, iBdl at the plane surface in the lab frame */ + @Override public double[] SwimToPlaneBoundary(double d_cm, Vector3D n, int dir) { + if (this.SwimUnPhys) return null; + double[] value = new double[8]; - if(this.SwimUnPhys) - return null; - double d = d_cm / 100; - double hdata[] = new double[3]; - // using adaptive stepsize + double d = d_cm / 100; // convert to meters - // the new swim to plane in swimmer Plane plane = new Plane(n.x(), n.y(), n.z(), d); - SwimTrajectory st; try { - st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, plane, accuracy, _maxPathLength, stepSize, - cnuphys.swim.Swimmer.CLAS_Tolerance, hdata); + SwimTrajectory st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, + plane, accuracy, _maxPathLength, stepSize, + cnuphys.swim.Swimmer.CLAS_Tolerance, hdata); st.computeBDL(PC.CP); @@ -899,88 +462,27 @@ public double[] SwimToPlaneBoundary(double d_cm, Vector3D n, int dir) { value[6] = lastY[6] * 100; value[7] = lastY[7] * 10; // Conversion from kG.m to T.cm - // System.out.println("\nCOMPARE plane swims DIRECTION = " + - // dir); - // for (int i = 0; i < 8; i++) { - // System.out.print(String.format("%-8.5f ", value[i])); - // } - - } catch (RungeKuttaException e) { - e.printStackTrace(); + e.printStackTrace(); } return value; - } - - - private class BeamLineSwimStopper implements IStopper { - - private double _finalPathLength = Double.NaN; - - private double _xB; - private double _yB; - double min = Double.POSITIVE_INFINITY; - double thetaRad = Math.toRadians(_theta); - double phiRad = Math.toRadians(_phi); - double pz = _pTot * Math.cos(thetaRad); - private BeamLineSwimStopper(double xB, double yB) { - // DC reconstruction units are cm. Swim units are m. Hence scale by - // 100 - _xB = xB; - _yB = yB; - } - - @Override - public boolean stopIntegration(double t, double[] y) { - - double r = Math.sqrt((_xB-y[0]* 100.) * (_xB-y[0]* 100.) + (_yB-y[1]* 100.) * (_yB-y[1]* 100.)); - if(r min ); - - } - - /** - * Get the final path length in meters - * - * @return the final path length in meters - */ - @Override - public double getFinalT() { - return _finalPathLength; - } - - /** - * Set the final path length in meters - * - * @param finalPathLength - * the final path length in meters - */ - @Override - public void setFinalT(double finalPathLength) { - _finalPathLength = finalPathLength; - } - } - + @Override public double[] SwimToBeamLine(double xB, double yB) { + if(this.SwimUnPhys==true) return null; + double[] value = new double[8]; - if(this.SwimUnPhys==true) - return null; BeamLineSwimStopper stopper = new BeamLineSwimStopper(xB, yB); - SwimTrajectory st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, stopper, _maxPathLength, stepSize, - 0.0005); - if(st==null) - return null; + SwimTrajectory st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, + stopper, _maxPathLength, stepSize, 0.0005); + if (st==null) return null; st.computeBDL(PC.CP); - // st.computeBDL(compositeField); double[] lastY = st.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 @@ -989,77 +491,24 @@ public double[] SwimToBeamLine(double xB, double yB) { value[5] = lastY[5] * _pTot; value[6] = lastY[6] * 100; value[7] = lastY[7] * 10; // Conversion from kG.m to T.cm - return value; - - } - - // - - private class LineSwimStopper implements IStopper { - - private double _finalPathLength = Double.NaN; - - private Line3D _l; - private Point3D _p; - double min = 999; - private LineSwimStopper(Line3D l) { - // DC reconstruction units are cm. Swim units are m. Hence scale by - // 100 - _l =l; - _p = new Point3D(); - } - - @Override - public boolean stopIntegration(double t, double[] y) { - _p.set(y[0]* 100.0, y[1]* 100.0, y[2]* 100.0); - double doca = _l.distance(_p).length(); - if(doca min ); - - } - - /** - * Get the final path length in meters - * - * @return the final path length in meters - */ - @Override - public double getFinalT() { - return _finalPathLength; - } - - /** - * Set the final path length in meters - * - * @param finalPathLength - * the final path length in meters - */ - @Override - public void setFinalT(double finalPathLength) { - _finalPathLength = finalPathLength; - } } - + + @Override public double[] SwimToLine(Line3D l) { + + if (this.SwimUnPhys==true) return null; double[] value = new double[8]; - if(this.SwimUnPhys==true) - return null; LineSwimStopper stopper = new LineSwimStopper(l); - SwimTrajectory st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, stopper, _maxPathLength, stepSize, - 0.0005); - if(st==null) - return null; + SwimTrajectory st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, + stopper, _maxPathLength, stepSize, 0.0005); + if (st==null) return null; st.computeBDL(PC.CP); - // st.computeBDL(compositeField); double[] lastY = st.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 @@ -1068,206 +517,25 @@ public double[] SwimToLine(Line3D l) { value[5] = lastY[5] * _pTot; value[6] = lastY[6] * 100; value[7] = lastY[7] * 10; // Conversion from kG.m to T.cm - return value; - - } - - // - - - private void printV(String pfx, double v[]) { - double x = v[0] / 100; - double y = v[1] / 100; - double z = v[2] / 100; - double r = Math.sqrt(x * x + y * y + z * z); - System.out.println(String.format("%s: (%-8.5f, %-8.5f, %-8.5f) R: %-8.5f", pfx, z, y, z, r)); } /** - * - * @param sector - * @param x_cm - * @param y_cm - * @param z_cm - * @param result B field components in T in the tilted sector system - */ - public void Bfield(int sector, double x_cm, double y_cm, double z_cm, float[] result) { - - PC.RCP.field(sector, (float) x_cm, (float) y_cm, (float) z_cm, result); - // rcompositeField.field((float) x_cm, (float) y_cm, (float) z_cm, - // result); - result[0] = result[0] / 10; - result[1] = result[1] / 10; - result[2] = result[2] / 10; - - } - /** - * - * @param x_cm - * @param y_cm - * @param z_cm - * @param result B field components in T in the lab frame - */ - public void BfieldLab(double x_cm, double y_cm, double z_cm, float[] result) { - - PC.CP.field((float) x_cm, (float) y_cm, (float) z_cm, result); - result[0] = result[0] / 10; - result[1] = result[1] / 10; - result[2] = result[2] / 10; - - } - - - - public double[] AdaptiveSwimPlane(double px, double py, double pz, double nx, double ny, double nz, double accuracy) { -// System.out.println("Don't use yet"); - - double[] value = new double[8]; - - Vector norm = new Vector(nx,ny,nz); - Point point = new Point(px/100,py/100,pz/100); - - cnuphys.adaptiveSwim.geometry.Plane targetPlane = new cnuphys.adaptiveSwim.geometry.Plane(norm, point); - - - // using adaptive stepsize - if(this.SwimUnPhys) - return null; - - try { - - AdaptiveSwimResult result = new AdaptiveSwimResult(false); - - PC.AS.swimPlane(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, targetPlane, - accuracy/100, _rMax, stepSize, cnuphys.swim.Swimmer.getEps(), result); - - if(result.getStatus() == AdaptiveSwimmer.SWIM_SUCCESS) { - value[0] = result.getUf()[0] * 100; // convert back to cm - value[1] = result.getUf()[1] * 100; // convert back to cm - value[2] = result.getUf()[2] * 100; // convert back to cm - value[3] = result.getUf()[3] * _pTot; // normalized values - value[4] = result.getUf()[4] * _pTot; - value[5] = result.getUf()[5] * _pTot; - value[6] = result.getFinalS() * 100; - value[7] = 0; // Conversion from kG.m to T.cm - } - else { - return null; - } - - } catch (AdaptiveSwimException e) { - e.printStackTrace(); - } - return value; - - } - - - public double[] AdaptiveSwimCylinder(double a1x, double a1y, double a1z, double a2x, double a2y, double a2z, double radius, double accuracy) { - // System.out.println("Don't use yet"); - double[] value = new double[8]; - - radius = radius/100; - Point a1 = new Point(a1x/100, a1y/100, a1z/100); - Point a2 = new Point(a2x/100, a2y/100, a2z/100); - Line centerLine = new Line(a1, a2); - - cnuphys.adaptiveSwim.geometry.Cylinder targetCylinder = new cnuphys.adaptiveSwim.geometry.Cylinder(centerLine, radius); - - - // using adaptive stepsize - if(this.SwimUnPhys) - return null; - - try { - - AdaptiveSwimResult result = new AdaptiveSwimResult(false); - - PC.AS.swimCylinder(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, targetCylinder, - accuracy/100, _rMax, stepSize, cnuphys.swim.Swimmer.getEps(), result); - - if(result.getStatus() == AdaptiveSwimmer.SWIM_SUCCESS) { - value[0] = result.getUf()[0] * 100; // convert back to cm - value[1] = result.getUf()[1] * 100; // convert back to cm - value[2] = result.getUf()[2] * 100; // convert back to cm - value[3] = result.getUf()[3] * _pTot; // normalized values - value[4] = result.getUf()[4] * _pTot; - value[5] = result.getUf()[5] * _pTot; - value[6] = result.getFinalS() * 100; - value[7] = 0; // Conversion from kG.m to T.cm - } - else { - return null; - } - - } catch (AdaptiveSwimException e) { - e.printStackTrace(); - } - return value; - - } - - public double[] AdaptiveSwimRho(double radius, double accuracy) { - System.out.println("Don't use yet"); - - double[] value = new double[8]; - - radius = radius/100; - // using adaptive stepsize - if(this.SwimUnPhys) - return null; - - try { - - AdaptiveSwimResult result = new AdaptiveSwimResult(false); - - PC.AS.swimRho(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, radius, - accuracy/100, _rMax, stepSize, cnuphys.swim.Swimmer.getEps(), result); - - if(result.getStatus() == AdaptiveSwimmer.SWIM_SUCCESS) { - value[0] = result.getUf()[0] * 100; // convert back to cm - value[1] = result.getUf()[1] * 100; // convert back to cm - value[2] = result.getUf()[2] * 100; // convert back to cm - value[3] = result.getUf()[3] * _pTot; // normalized values - value[4] = result.getUf()[4] * _pTot; - value[5] = result.getUf()[5] * _pTot; - value[6] = result.getFinalS() * 100; - value[7] = 0; // Conversion from kG.m to T.cm - } - else { - return null; - } - - } catch (AdaptiveSwimException e) { - e.printStackTrace(); - } - return value; - - } - -/** * * @param Z + * @param dir * @return state x,y,z,px,py,pz, pathlength, iBdl at the surface */ + @Override public double[] SwimToZ(double Z, int dir) { - double[] value = new double[8]; - //if(this.SwimUnPhys) - // return null; - ZSwimStopper stopper = new ZSwimStopper(Z, dir); - - SwimTrajectory st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, stopper, _maxPathLength, stepSize, - distanceBetweenSaves); - if(st==null) - return null; + SwimTrajectory st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, + stopper, _maxPathLength, stepSize, distanceBetweenSaves); + if (st==null) return null; st.computeBDL(PC.CP); - // st.computeBDL(compositeField); this.setSwimTraj(st); double[] lastY = st.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 @@ -1276,156 +544,28 @@ public double[] SwimToZ(double Z, int dir) { value[5] = lastY[5] * _pTot; value[6] = lastY[6] * 100; value[7] = lastY[7] * 10; // Conversion from kG.m to T.cm - return value; - - } - - private SwimTrajectory swimTraj; - - private class ZSwimStopper implements IStopper { - - private double _finalPathLength = Double.NaN; - - private double _Z; - private int _dir; - - private ZSwimStopper(double Z, int dir) { - // The reconstruction units are cm. Swim units are m. Hence scale by - // 100 - _Z = Z; - _dir = dir; - } - - @Override - public boolean stopIntegration(double t, double[] y) { - - double z = y[2] * 100.; - if(_dir>0) { - return (z > _Z); - } else { - return (z<_Z); - } - } - - /** - * Get the final path length in meters - * - * @return the final path length in meters - */ - @Override - public double getFinalT() { - return _finalPathLength; - } - - /** - * Set the final path length in meters - * - * @param finalPathLength - * the final path length in meters - */ - @Override - public void setFinalT(double finalPathLength) { - _finalPathLength = finalPathLength; - } - } - /** - * @return the swimTraj - */ - public SwimTrajectory getSwimTraj() { - return swimTraj; } - /** - * @param swimTraj the swimTraj to set - */ - public void setSwimTraj(SwimTrajectory swimTraj) { - this.swimTraj = swimTraj; - } - - private class DCASwimStopper implements IStopper { - - public DCASwimStopper(SwimTrajectory swimTraj) { - _swimTraj = swimTraj; - for(int i = 0; i < _swimTraj.size()-1; i++) { - polylines.add(new Line3D(_swimTraj.get(i)[0],_swimTraj.get(i)[1],_swimTraj.get(i)[2], - _swimTraj.get(i+1)[0],_swimTraj.get(i+1)[1],_swimTraj.get(i+1)[2])); - - } - } - - private List polylines = new ArrayList<>(); - private SwimTrajectory _swimTraj; - private double _finalPathLength = Double.NaN; - private double _doca = Double.POSITIVE_INFINITY; - - @Override - public boolean stopIntegration(double t, double[] y) { - - Point3D dcaCand = new Point3D(y[0],y[1],y[2]); - double maxDoca = Double.POSITIVE_INFINITY; - - for(Line3D l : polylines) { - if(l.distance(dcaCand).length()this._rMax || + Math.sqrt(_x0*_x0 + _y0*_y0 + _z0*_z0)>this._maxPathLength) + this.SwimUnPhys=true; + } + +} diff --git a/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/Swimmer.java b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/Swimmer.java index 7915ce6129..c6344fc593 100644 --- a/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/Swimmer.java +++ b/common-tools/swim-tools/src/main/java/org/jlab/clas/swimtools/Swimmer.java @@ -1,16 +1,14 @@ package org.jlab.clas.swimtools; -import cnuphys.magfield.MagneticFields; import java.util.HashMap; -import java.util.logging.Level; import java.util.logging.Logger; +import cnuphys.magfield.MagneticFields; /** * * @author ziegler, heddle */ - public class Swimmer { public static Logger LOGGER = Logger.getLogger(Swimmer.class.getName());