#include <iostream>
using std::cout;
using std::endl;
#include <string>
#include <cassert>

#include "BField/BField.h"
#include "CandFitTrackMS/BFieldMS.h"
#include "Conventions/Detector.h"
#include "Validity/VldContext.h"

BFieldMS::BFieldMS(const VldContext *vldc)
{
  assert (vldc);
  //  cout << "got past assertion " << vldc << endl;
  //  cout << " VLDC " << *vldc << endl;
  bf = 0;
  switch (vldc->GetDetector()) {
  case Detector::kFar:
    bf = new BField(*vldc);
    break;
  case Detector::kNear:
    bf = new BField(*vldc);
    break;
  default:
    cout << "BFieldMS can not handle " << *vldc << endl;
    assert(0);
  }
}

BFieldMS::~BFieldMS()
{
  if (bf) {
    delete bf;
  }
}

TVector3 BFieldMS::GetBField(TVector3 &xyz)
{
  TVector3 bxyz;
  if (!bf) {
    cout << "no bf!" << endl;
    bxyz(0) = 0.;
    bxyz(1) = 0.;
    bxyz(2) = 0.;
  }
  else {
    bxyz = bf->GetBField(xyz);
  }
  return bxyz;
}
