//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
// ------------------------------------------------------------
//      GEANT 4 class header file
//
//      History:
//      17 August 2004  P. Gumplinger, T. MacPhail
// ------------------------------------------------------------
//
#include "G4DecayWithSpin.hh"

#include "G4Step.hh"
#include "G4Track.hh"
#include "G4DecayTable.hh"
#include "G4MuonDecayChannelWithSpin.hh"

#include "G4Vector3D.hh"

#include "G4TransportationManager.hh"
#include "G4PropagatorInField.hh"
#include "G4FieldManager.hh"
#include "G4Field.hh"

#include "G4Transform3D.hh"

//cks
#include "musrErrorMessage.hh"
#include "G4RunManager.hh"
//#include "musrRootOutput.hh"
//csk

G4DecayWithSpin::G4DecayWithSpin(const G4String& processName):G4Decay(processName){}
//cks  G4DecayWithSpin::G4DecayWithSpin(const G4String& processName):G4Decay(processName){SetVerboseLevel(3);}

G4DecayWithSpin::~G4DecayWithSpin(){}

G4VParticleChange* G4DecayWithSpin::DecayIt(const G4Track& aTrack, const G4Step& aStep)
{
  // debugging:  G4RunManager* fRunManager = G4RunManager::GetRunManager();
  // debugging:  G4int eventNr=fRunManager->GetCurrentEvent()->GetEventID();
  // debugging:  if ((eventNr>447715)&&(eventNr<45005)) SetVerboseLevel(3);
  // debugging:  if (eventNr>=45005) SetVerboseLevel(0);

// get particle
  const G4DynamicParticle* aParticle = aTrack.GetDynamicParticle();
  G4ParticleDefinition* aParticleDef = aParticle->GetDefinition();

// get parent_polarization
  G4ThreeVector parent_polarization = aParticle->GetPolarization();

  if(parent_polarization == G4ThreeVector(0,0,0))
  {
    // Generate random polarization direction

    G4double cost = 1. - 2.*G4UniformRand();
    G4double sint = std::sqrt((1.-cost)*(1.+cost));

    G4double phi = twopi*G4UniformRand();
    G4double sinp = std::sin(phi);
    G4double cosp = std::cos(phi);

    G4double px = sint*cosp;
    G4double py = sint*sinp;
    G4double pz = cost;

    parent_polarization.setX(px);
    parent_polarization.setY(py);
    parent_polarization.setZ(pz);

  }else{

    G4FieldManager* fieldMgr = aStep.GetTrack()->GetVolume()->
                                     GetLogicalVolume()->GetFieldManager();

    if (!fieldMgr) {
       G4TransportationManager *transportMgr =
                         G4TransportationManager::GetTransportationManager();
       G4PropagatorInField* fFieldPropagator = 
                                        transportMgr->GetPropagatorInField();
       if (fFieldPropagator) fieldMgr = 
                                  fFieldPropagator->GetCurrentFieldManager();
    }

    const G4Field* field = NULL;
    if(fieldMgr)field = fieldMgr->GetDetectorField();

    //    if (field && !(fieldMgr->DoesFieldChangeEnergy())) {
    if (field) {
       G4double point[4];
       point[0] = (aStep.GetPostStepPoint()->GetPosition())[0];
       point[1] = (aStep.GetPostStepPoint()->GetPosition())[1];
       point[2] = (aStep.GetPostStepPoint()->GetPosition())[2];
       point[3] = aTrack.GetGlobalTime();

       G4double fieldValue[6];
       field -> GetFieldValue(point,fieldValue);

       G4ThreeVector B(fieldValue[0],fieldValue[1],fieldValue[2]);

       if ((B.mag2())>0) {         // Call the spin precession only for non-zero mag. field
	 parent_polarization = Spin_Precession(aStep,B,fRemainderLifeTime);
	 //	 G4cout<<"G4DecayWithSpin: parent_polarization="<<parent_polarization<<",  B="<<B[0]/tesla<<","<<B[1]/tesla<<","<<B[2]/tesla
	 //	       <<",  fRemainderLifeTime="<<fRemainderLifeTime<<G4endl;
       }

    }
  }

// decay table
  G4DecayTable *decaytable = aParticleDef->GetDecayTable();

  if (decaytable) {
     G4MuonDecayChannelWithSpin *decaychannel;
     decaychannel = (G4MuonDecayChannelWithSpin*)decaytable->SelectADecayChannel();
     if (decaychannel) decaychannel->SetPolarization(parent_polarization);
  }

  G4ParticleChangeForDecay* pParticleChangeForDecay;

  pParticleChangeForDecay = (G4ParticleChangeForDecay*)G4Decay::DecayIt(aTrack,aStep);

  pParticleChangeForDecay->ProposePolarization(parent_polarization);

  //  pParticleChangeForDecay->DumpInfo();
  
  return pParticleChangeForDecay;

}

G4ThreeVector G4DecayWithSpin::Spin_Precession( const G4Step& aStep,
                                        G4ThreeVector B, G4double deltatime )
{
  G4double Bnorm = std::sqrt(sqr(B[0])  + sqr(B[1]) +sqr(B[2]) );

  G4double q = aStep.GetTrack()->GetDefinition()->GetPDGCharge();
  G4double a = 1.165922e-3;
  G4double s_omega = 8.5062e+7*rad/(s*kilogauss);

  G4double omega = -(q*s_omega)*(1.+a) * Bnorm;

  G4double rotationangle = deltatime * omega;

  G4Transform3D SpinRotation = G4Rotate3D(rotationangle,B.unit());

  G4Vector3D Spin = aStep.GetTrack() -> GetPolarization();
  //cks  2008.10.09.  Check that the polarisatin is normalised do 1
  G4double spinMagnitude = Spin.mag();
  if ((spinMagnitude>1.1)||(spinMagnitude<0.9)) {
    G4RunManager* fRunManager = G4RunManager::GetRunManager();
    G4int eventNr=fRunManager->GetCurrentEvent()->GetEventID();
    musrErrorMessage::GetInstance()->musrError(SERIOUS,
          "G4DecayWithSpin.cc::Spin_Precession:  spin magnitude is large than 1.1 or smaller than 0.9!  Strange!!!",true);
    G4cout<<"G4DecayWithSpin.cc::Spin_Precession: "
  	  <<"Event nr.:"<<eventNr
  	  <<",   spin = ("<<Spin.x()<<","<<Spin.y()<<","<<Spin.z()<<")."<<G4endl;
    G4cout.flush();
  }
  Spin = Spin.unit();
  //csk 

  G4Vector3D newSpin = SpinRotation * Spin;

  //cks  2008.10.09.  Check that the polarisatin is normalised do 1
  G4double newSpinMagnitude = newSpin.mag();
  if ((newSpinMagnitude>1.1)||(newSpinMagnitude<0.9)) {
    G4RunManager* fRunManager = G4RunManager::GetRunManager();
    G4int eventNr=fRunManager->GetCurrentEvent()->GetEventID();
    musrErrorMessage::GetInstance()->musrError(SERIOUS,
	      "G4DecayWithSpin.cc::Spin_Precession:  NEW spin magnitude is large than 1.1 or smaller than 0.9!  Strange!!!",true);
    G4cout<<"G4DecayWithSpin.cc::Spin_Precession: "
  	  <<"Event nr.:"<<eventNr
  	  <<",   NEW spin = ("<<newSpin.x()<<","<<newSpin.y()<<","<<newSpin.z()<<")."<<G4endl;
    G4cout.flush();
  }
  newSpin = newSpin.unit();
  //csk 


#ifdef G4VERBOSE
  if (GetVerboseLevel()>2) {
    G4double normspin = std::sqrt(Spin*Spin);
    G4double normnewspin = std::sqrt(newSpin*newSpin);
    //G4double cosalpha = Spin*newSpin/normspin/normnewspin;
    //G4double alpha = std::acos(cosalpha);

    G4cout << "AT REST::: PARAMETERS " << G4endl;
    G4cout << "Initial spin  : " << Spin  << G4endl;
    G4cout << "Delta time    : " << deltatime  << G4endl;
    G4cout << "Rotation angle: " << rotationangle/rad  << G4endl;
    G4cout << "New spin      : " << newSpin  << G4endl;
    G4cout << "Checked norms : " << normspin <<" " << normnewspin << G4endl;
  }
#endif

  return newSpin;

}
