//************************************************************************//
//									  //
//  Copyright 2013 Bertram Kopf (bertram@ep1.rub.de)			  //
//  	      	   Julian Pychy (julian@ep1.rub.de)			  //
//          	   - Ruhr-Universität Bochum 				  //
//									  //
//  This file is part of Pawian.					  //
//									  //
//  Pawian is free software: you can redistribute it and/or modify	  //
//  it under the terms of the GNU General Public License as published by  //
//  the Free Software Foundation, either version 3 of the License, or 	  //
//  (at your option) any later version.	 	      	  	   	  //
//									  //
//  Pawian is distributed in the hope that it will be useful,		  //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of	  //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	  //
//  GNU General Public License for more details.	      		  //
//									  //
//  You should have received a copy of the GNU General Public License     //
//  along with Pawian.  If not, see <http://www.gnu.org/licenses/>.	  //
//									  //
//************************************************************************//

// LSOmegaTo3PiDecAmps class definition file. -*- C++ -*-
// Copyright 2012 Bertram Kopf

#include <getopt.h>
#include <fstream>
#include <string>
#include <mutex>

#include "PwaUtils/LSOmegaTo3PiDecAmps.hh"
#include "qft++/relativistic-quantum-mechanics/Utils.hh"
#include "ErrLogger/ErrLogger.hh"
#include "PwaUtils/DataUtils.hh"
//#include "PwaUtils/OmegaTo3PiLSDecay.hh"
//#include "PwaUtils/XdecAmpRegistry.hh"
#include "Particle/Particle.hh"


LSOmegaTo3PiDecAmps::LSOmegaTo3PiDecAmps(std::shared_ptr<OmegaTo3PiLSDecay> theDec, ChannelID channelID) :
  LSDecAmps( (std::shared_ptr<AbsDecay>) theDec, channelID)
  ,_lambdaDecKey(theDec->lambdaDecKey())
{
  _LSs=theDec->LSAmps();
  _factorMag=1.;
  if(_LSs.size()>0) _factorMag=1./sqrt(_LSs.size());
}

LSOmegaTo3PiDecAmps::~LSOmegaTo3PiDecAmps()
{
}

void LSOmegaTo3PiDecAmps::updateFitParams(std::shared_ptr<AbsPawianParameters> fitPar){

  std::vector< std::shared_ptr<const LScomb> >::const_iterator itLS;
  for(itLS=_LSs.begin(); itLS!=_LSs.end(); ++itLS){
    std::string magName=(*itLS)->name()+_key+"Mag";
    std::string phiName=(*itLS)->name()+_key+"Phi";
    _currentParamMagExpi[*itLS]=std::polar(fabs(fitPar->Value(magName)), fitPar->Value(phiName));
  }

   _absDyn->updateFitParams(fitPar);

   // no daughter decays possible for omega->3pi
   //  if(!_daughter1IsStable) _decAmpDaughter1->updateFitParams(fitPar);
   //  if(!_daughter2IsStable) _decAmpDaughter2->updateFitParams(fitPar);
}

complex<double> LSOmegaTo3PiDecAmps::XdecPartAmp(const Spin& lamX, Spin& lamDec, short fixDaughterNr, EvtData* theData, Spin& lamFs,AbsXdecAmp* grandmaAmp){

  complex<double> result(0.,0.);
  Id1StringType IdLamOmega=FunctionUtils::spin1Index(lamX);

  std::vector< std::shared_ptr<const LScomb> >::iterator it;
  for (it=_LSs.begin(); it!=_LSs.end(); ++it){
    if( fabs(lamX) > _JPCPtr->J ) continue;
     result+=sqrt(2*(*it)->L+1)*_currentParamMagExpi.at(*it)*conj(theData->WignerDIdId1.at(_decay->wigDWigDRefId()).at(IdLamOmega));
  }
  result*=sqrt(theData->DoubleId.at(_decay->wigDWigDRefId()));
  return result;
}




complex<double> LSOmegaTo3PiDecAmps::XdecAmp(const Spin& lamX, EvtData* theData, AbsXdecAmp* grandmaAmp){

  complex<double> result(0.,0.);

  //  int evtNo=theData->evtNo;
  //  short currentSpinIndex=FunctionUtils::spin1IdIndex(_projIdThreadMap.at(std::this_thread::get_id()),lamX);
  short currentSpinIndex=FunctionUtils::spin1IdIndex(_projId, lamX);
 
  if (!_recalculate){
    //    result=_cachedAmpMap.at(evtNo).at(_absDyn->grandMaKey(grandmaAmp)).at(currentSpinIndex);
    //    result=_cachedAmpIdMap.at(theData->evtNo).at(_absDyn->grandMaId(grandmaAmp)).at(currentSpinIndex);
    return _cachedAmpIdMap.at(theData->evtNo).at(_absDyn->grandMaId(grandmaAmp)).at(currentSpinIndex);
  }

  Id1StringType IdLamOmega=FunctionUtils::spin1Index(lamX);
 
  std::vector< std::shared_ptr<const LScomb> >::iterator it;
  for (it=_LSs.begin(); it!=_LSs.end(); ++it){
    if( fabs(lamX) > _JPCPtr->J ) continue;

    complex<double> amp = _currentParamMagExpi.at(*it)*sqrt(2*(*it)->L+1)
      *conj(theData->WignerDIdId1.at(_decay->wigDWigDRefId()).at(IdLamOmega));

    if (_absDyn->isLdependent()) amp*=_absDyn->eval(theData, grandmaAmp, (*it)->L);
    result+=amp;
  }
  result*=sqrt(theData->DoubleId.at(_decay->wigDWigDRefId()));
  if (!_absDyn->isLdependent()) result*=_absDyn->eval(theData, grandmaAmp);
  if ( _cacheAmps){
     _cachedAmpIdMap[theData->evtNo][_absDyn->grandMaId(grandmaAmp)][currentSpinIndex]=result;
  }

  return result;
}


void LSOmegaTo3PiDecAmps::print(std::ostream& os) const{
  return; //dummy
}


