Class MagneticFields

java.lang.Object
cnuphys.magfield.MagneticFields

public class MagneticFields extends Object
Static support for magnetic fields
Author:
heddle
  • Field Details

    • LOGGER

      public static Logger LOGGER
  • Method Details

    • getVersion

      public String getVersion()
      Get the version of the magfield package
      Returns:
      the version of the magfield package
    • getInstance

      public static MagneticFields getInstance()
      public access to the singleton
      Returns:
      the MagneticFields singleton
    • openNewTorus

      protected void openNewTorus()
      Open a new torus map from the file selector
    • openNewTorus

      protected void openNewTorus(String path) throws FileNotFoundException
      Open a new torus map from a full path
      Parameters:
      path - the path to the torus map
      Throws:
      FileNotFoundException
    • openSolenoid

      protected void openSolenoid(File file, boolean isTransverse) throws FileNotFoundException
      Open a new solenoid map from a file
      Parameters:
      file - the file holding the solenoid map
      Throws:
      FileNotFoundException
    • setSolenoidShift

      public void setSolenoidShift(double shiftZ)
      Shift the solenoid along Z for misplacement. A negative shift moved the solenoid upstream. Kept for backwards compatibility
      Parameters:
      shiftZ - the shift in cm
    • shiftMagneticField

      public void shiftMagneticField(MagneticField field, double shiftX, double shiftY, double shiftZ)
      Shift the magnetic field (i.e., a misalignment)
      Parameters:
      field - the field, either _solenoid or torus
      shiftX - the X shift in cm
      shiftY - the Y shift in cm
      shiftZ - the Z shift in cm A negative shift moved the field upstream.
    • changeFieldsAndMenus

      public boolean changeFieldsAndMenus(double torusScale, double solenoidScale)
      This programatically adjusts everything for new scale factors. This is used when data found in the file
      Parameters:
      torusScale - the torus scale factor
      solenoidScale - the solenoid scale factor
    • getActiveFieldType

      public MagneticFields.FieldType getActiveFieldType()
      Get the field type of the active field
      Returns:
      the field type of the active field
    • isSolenoidOnly

      public boolean isSolenoidOnly()
      Is the active field solenoid only
      Returns:
      true of the active field is solenoid only
    • isTorusOnly

      public boolean isTorusOnly()
      Is the active field torus only
      Returns:
      true of the active field is torus only
    • isCompositeField

      public boolean isCompositeField()
      Is the active field composite
      Returns:
      true of the active field is solenoid and torus composite
    • setActiveField

      public void setActiveField(IMagField field)
      Sets the active field
      Parameters:
      field - the new active field
    • setActiveField

      public void setActiveField(MagneticFields.FieldType ftype)
      Sets the active field
      Parameters:
      ftype - one of the enum values
    • getActiveFieldDescription

      public final String getActiveFieldDescription()
      Get a string description of the active field
      Returns:
      a string description of the active field
    • getActiveField

      public IMagField getActiveField()
      Get the active field
      Returns:
      the active field
    • getIField

      public IMagField getIField(MagneticFields.FieldType ftype)
      Get a specific field map.
      Parameters:
      ftype - the field map to get
      Returns:
      the field map, which might be null.
    • getScaleFactor

      public double getScaleFactor(MagneticFields.FieldType ftype)
      Get the scale factor got the field type.
      Parameters:
      ftype - the field type
      Returns:
      the scale factor got the field type. Composite fields return NaN.
    • getShiftZ

      public double getShiftZ(MagneticFields.FieldType ftype)
      Get the shift Z given the field type.
      Parameters:
      ftype - the field type
      Returns:
      the shift in z (cm) for the field type. Composite fields return NaN.
    • setSolenoid

      public void setSolenoid(Solenoid solenoid)
      In case someone loads a solenoid externally.
      Parameters:
      solenoid -
    • setTorus

      public void setTorus(Torus torus)
      In case someone loads a torus externally.
      Parameters:
      torus -
    • tokens

      public static String[] tokens(String str, String delimiter)
      This method breaks a string into an array of tokens.
      Parameters:
      str - the string to decompose.
      delimiter - the delimiter
      Returns:
      an array of tokens
    • initializeMagneticFieldsFromEnv

      public void initializeMagneticFieldsFromEnv() throws MagneticFieldInitializationException, FileNotFoundException
      Attempts to initialize the magnetic fields using the property or environment variables TORUSMAP and SOLENOIDMAP as full paths to the torus and solenoid
      Throws:
      MagneticFieldInitializationException - if neither environment variable is not found. Will proceed if just one is found.
      FileNotFoundException - if neither file is not found. Will proceed if just one is found.
    • initializeMagneticFields

      public void initializeMagneticFields(String dataDir, String torusName, String solenoidName) throws FileNotFoundException, MagneticFieldInitializationException
      Initialize the magnetic field package
      Parameters:
      dataDir - the common data directory containing the torus and solenoid
      torusName - the base name of the torus map
      solenoidName - the base name of the solenoid map
      Throws:
      FileNotFoundException - if either full path is not null but the corresponding file cannot be found
      MagneticFieldInitializationException - if both full paths are null. Will proceed as long as one path is not null.
    • initializeMagneticFields

      public void initializeMagneticFields(String dataDir, String torusName, String solenoidName, String transverseSolenoidName) throws FileNotFoundException, MagneticFieldInitializationException
      Deprecated.
      Initialize the magnetic field package
      Parameters:
      dataDir - the common data directory containing the torus and solenoid
      torusName - the base name of the torus map
      solenoidName - the base name of the solenoid map
      Throws:
      FileNotFoundException - if either full path is not null but the corresponding file cannot be found
      MagneticFieldInitializationException - if both full paths are null. Will proceed as long as one path is not null.
    • initializeMagneticFieldsFromPath

      public void initializeMagneticFieldsFromPath(String torusPath, String solenoidPath) throws MagneticFieldInitializationException, FileNotFoundException
      Initialize the field from the two full paths. One of them can be null.
      Parameters:
      torusPath - the full path to the torus map. Can be null.
      solenoidPath - the full path to the solenoid map. Can be null.
      Throws:
      MagneticFieldInitializationException - if both paths are null. Will proceed as long as one path is not null.
      FileNotFoundException - if either path is not null but the corresponding file cannot be found
    • initializeMagneticFields

      public void initializeMagneticFields()
      Tries to load the magnetic fields from fieldmaps
    • getMagneticFieldMenu

      public JMenu getMagneticFieldMenu()
      Get the magnetic field menu
      Returns:
      the magnetic field menu
    • getMagneticFieldMenu

      public JMenu getMagneticFieldMenu(boolean incRotatedField, boolean includeTestFields)
      Get the magnetic field menu
      Returns:
      the magnetic field menu
    • fixMenus

      public void fixMenus()
      Make sure the menus are consistent.
    • changedScale

      protected void changedScale(MagneticField field)
    • changedShift

      protected void changedShift(MagneticField field)
    • notifyListeners

      protected void notifyListeners()
      Notify all listeners that a change has occurred in the magnetic fields
    • addMagneticFieldChangeListener

      public void addMagneticFieldChangeListener(MagneticFieldChangeListener magChangeListener)
      Add a magnetic field change listener
      Parameters:
      magChangeListener - the listener to add
    • removeMagneticFieldChangeListener

      public void removeMagneticFieldChangeListener(MagneticFieldChangeListener magChangeListener)
      Remove a MagneticFieldChangeListener.
      Parameters:
      magChangeListener - the MagneticFieldChangeListener to remove.
    • removeMapOverlap

      public void removeMapOverlap()
      Removes the overlap between the solenoid and the torus. It does this by Adding the solenoid field to the torus field cutting of the solenoid in the overlap region, then cut off the solenoid at the min Z of the torus. This is an experimental method and irreversible. In particular rescaling the torus after doing this will cause the solenoid part of the overlap area to be scaled too. Which is nonsense.
    • hasActiveTorus

      public boolean hasActiveTorus()
      Check whether we have an active torus field
      Returns:
      true if we have a torus
    • hasActiveSolenoid

      public boolean hasActiveSolenoid()
      Check whether we have an active solenoid field
      Returns:
      true if we have a solenoid
    • getTorus

      public Torus getTorus()
      Get the torus field
      Returns:
      the torus field
    • getSolenoid

      public Solenoid getSolenoid()
      Get the solenoid field
      Returns:
      the solenoid field
    • getCompositeField

      public CompositeField getCompositeField()
      Get the composite field
      Returns:
      the composite field
    • getRotatedCompositeField

      public RotatedCompositeField getRotatedCompositeField()
      Get the rotated composite field
      Returns:
      the rotated composite field
    • getTorusPath

      public String getTorusPath()
      Get the full torus path
      Returns:
      the full torus path
    • getSolenoidPath

      public String getSolenoidPath()
      Get the full solenoid path
      Returns:
      the full solenoid path
    • getTorusBaseName

      public String getTorusBaseName()
      Get the torus file base name
      Returns:
      the torus file base name
    • sectorToLab

      public static void sectorToLab(int sector, float[] lab, float x, float y, float z)
      Converts the sector 3D coordinates to clas (lab) 3D coordinates
      Parameters:
      sector - the 1-based sector [1..6]
      lab - will hold the lab 3D Cartesian coordinates (modified)
      x - the sector x coordinate
      y - the sector y coordinate
      z - the sector z coordinate
    • labToSector

      public static void labToSector(int sector, float[] sect, float x, float y, float z)
      Converts the clas (lab) 3D coordinates to sector 3D coordinates to
      Parameters:
      sector - the 1-based sector [1..6]
      lab - will hold the lab 3D Cartesian coordinates (modified)
      x - the lab x coordinate
      y - the lab y coordinate
      z - the lab z coordinate
    • getSector

      public static int getSector(double labX, double labY)
      Get the sector [1..6] from the lab x and y coordinates
      Parameters:
      labX - the lab x
      labY - the lab y
      Returns:
      the sector [1..6]
    • getSector

      public static int getSector(double phi)
      Get the sector [1..6] from the phi value
      Parameters:
      phi - the value of phi in degrees
      Returns:
      the sector [1..6]
    • getSolenoidBaseName

      public String getSolenoidBaseName()
      Get the solenoid file base name
      Returns:
      the solenoid file base name
    • fileBaseNames

      public String fileBaseNames()
      Get a description of the torus and solenoid base file names
      Returns:
      the torus and solenoid base file names
    • dateStringLong

      public static String dateStringLong(long longtime)
      Returns the time as a string.
      Parameters:
      longtime - the time in millis.
      Returns:
      a string representation of the current time, down to seconds.
    • isProbeOrCompositeProbe

      public boolean isProbeOrCompositeProbe(IField field)
      Is this a probe or a composite probe?
      Parameters:
      field - the object to test
      Returns:
      t
    • maxFieldMagnitude

      public double maxFieldMagnitude()
      Get the maximum value of the active field in kG
      Returns:
      the maximum value of the active field in kG
    • main

      public static void main(String[] arg)
      For testing and also as an example
      Parameters:
      arg - command line arguments
    • getCurrentConfiguration

      public String getCurrentConfiguration()
    • getCurrentConfigurationMultiLine

      public String getCurrentConfigurationMultiLine()
    • writeHeader

      public static void writeHeader(DataOutputStream dos, int gridCS, int fieldCS, int lenUnit, int angUnit, int fieldUnit, float q1Min, float q1Max, int nq1, float q2Min, float q2Max, int nq2, float q3Min, float q3Max, int nq3)
      Write the binary file header
      Parameters:
      dos - the output stream
      gridCS - grid coordinate system [0: cylindrical, 1:Cartesian]
      fieldCS - field coordinate system [0: cylindrical, 1:Cartesian]
      lenUnit - length units [0:cm, 1:m]
      angUnit - angular units [0:deg, 1:radians]
      fieldUnit - field units [0:kG, 1:T]
      q1Min - first coordinate min
      q1Max - first coordinate max
      nq1 - first coordinate num grid points including ends
      q2Min - second coordinate min
      q2Max - second coordinate max
      nq2 - second coordinate num grid points including ends
      q3Min - third coordinate min
      q3Max - third coordinate max
      nq3 - third coordinate num grid points including ends
    • printCurrentConfiguration

      public void printCurrentConfiguration(PrintStream ps)
      Print a one line version of the magnetic field configuration
      Parameters:
      ps - the print stream