#include "SimMuon/CSCDigitizer/src/CSCDriftSim.h"
#include <cmath>

// the following functions were all generated by f2c.
// vin tried to optimize them...
//     with no sizeble effect....

double CSCDriftSim::avgDrift() const {
  /* Initialized data */

  static const double coeff[6] = {-.08618265, -.0020471805, -7.7771331e-4, -.0058433565, -5.0241491e-4, .0010223353};

  /* ! Parameterization of Lorentz drift in CSCs */
  /* ***********************************************************************
   * DOC MC_ALL_BDFT                                                       *
   *                                                                       *
   * DOC  Function    : Parameterization of the Lorentz drift of electrons *
   * DOC                in the muon endcap CSCs.                           *
   *                                                                       *
   * DOC  References  : None                                               *
   *                                                                       *
   * DOC  Arguments   : BZ  - z componant of the local magnetic            *
   *                         field in kgauss                               *
   * DOC                ZCELL - distance from the anode wire in the wire   *
   *                           plane coordinate normalized to one          *
   * DOC  Errors      : None                                               *
   *                                                                       *
   * DOC  Returns     : Drift distance along the anode wire                *
   *                                                                       *
   * DOC  Created     : 15-OCT-1996   Author : Jeff Rowe                   *
   * ***********************************************************************
   */

  const double x10 = 1.;
  const double x11 = bz / 40.;
  const double x12 = x11 * 2. * x11 - x10;
  const double x13 = x11 * 2. * x12 - x11;
  const double x14 = x11 * 2. * x13 - x12;
  const double x15 = x11 * 2. * x14 - x13;
  const double x16 = x11 * 2. * x15 - x14;
  const double x17 = x11 * 2. * x16 - x15;
  const double x18 = x11 * 2. * x17 - x16;
  const double x19 = x11 * 2. * x18 - x17;
  const double x20 = 1.;
  const double x21 = zcell;
  const double x22 = x21 * 2. * x21 - x20;
  const double x23 = x21 * 2. * x22 - x21;
  const double x24 = x21 * 2. * x23 - x22;
  const double x25 = x21 * 2. * x24 - x23;
  const double x26 = x21 * 2. * x25 - x24;
  const double x27 = x21 * 2. * x26 - x25;

  // not sure why we need a minus sign for avgDrift, but we think the
  // parametrization has a wrong sign.  For the +z endcap, bz should
  // be negative in local coord, so it should drift to the right
  // if drifting up (neg zcell), and to the left if it drifts down.  It doesn't.
  // the first-order term here is -0.08 * bz/40 * zcell

  return -1 * (coeff[0] * x11 * x21 + coeff[1] * x17 * x21 + coeff[2] * x21 + coeff[3] * x11 * x27 + coeff[4] * x22 +
               coeff[5] * x19);
}

double CSCDriftSim::driftSigma() const {
  // Initialized data

  static const double coeff[3] = {.01069525, .010364504, .0021662697};

  /* ! Parameterization of Lorentz drift error in CSCs */
  /*************************************************************************
   * DOC MC_ALL_BSIG                                                       *
   *                                                                       *
   * DOC  Function    : Parameterization of the dispersion in Lorentz Drift*
   * DOC                in the muon endcap CSCs.                           *
   *                                                                       *
   * DOC  References  : None                                               *
   *                                                                       *
   * DOC  Arguments   : BZ  - z componant of the local magnetic            *
   * DOC                ZCELL - distance from the anode wire in the wire   *
   *                           plane coordinate normalized to 1            *
   * DOC  Errors      : None                                               *
   *                                                                       *
   * DOC  Returns     : Dispersion in Lorentz drift along anode wire       *
   *                                                                       *
   * DOC  Created     : 15-OCT-1996   Author : Jeff Rowe                   *
   * ***********************************************************************
   */

  const double x11 = bz / 40.;
  const double x20 = 1.;
  const double x21 = zcell;
  const double x22 = x21 * 2. * x21 - x20;

  return coeff[0] + coeff[1] * x11 * x21 + coeff[2] * x22;
}
