/* ======================================================================== *\
!
! *
! * This file is part of MARS, the MAGIC Analysis and Reconstruction
! * Software. It is distributed to you in the hope that it can be a useful
! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
! * It is distributed WITHOUT ANY WARRANTY.
! *
! * Permission to use, copy, modify and distribute this software and its
! * documentation for any purpose is hereby granted without fee,
! * provided that the above copyright notice appear in all copies and
! * that both that copyright notice and this permission notice appear
! * in supporting documentation. It is provided "as is" without express
! * or implied warranty.
! *
!
!   Author(s): Javier Rico     04/2004 <mailto:jrico@ifae.es>
!
!   Copyright: MAGIC Software Development, 2000-2004
!
!
\* ======================================================================== */

//////////////////////////////////////////////////////////////////////////////
//
// MSrcRotate
//
// Task to rotate the position of the source as a function of Azimuth and 
// Zenith angles
//
//  Input Containers:
//    MSrcPosCam
//
//  Output Containers:
//    MSrcPosCam
//    MDCA
//
//////////////////////////////////////////////////////////////////////////////

#include <fstream>
#include <math.h>

#include "MParList.h"
#include "MRawRunHeader.h"
#include "MRawEvtHeader.h"
#include "MSrcRotate.h"
#include "MSrcPosCam.h"
#include "MDCA.h"

#include "MObservatory.h"
#include "MAstroSky2Local.h"
#include "MAstro.h"

#include "MLog.h"
#include "MLogManip.h"

ClassImp(MSrcRotate);

using namespace std;

static const TString gsDefName  = "MSrcRotate";
static const TString gsDefTitle = "De-rotate position of the source";

// -------------------------------------------------------------------------
//
// Default constructor. The first (second) argument is the name of a container
// containing the source position in the camera plain, MScrPosCam (MDCA).
// The default is "MSrcPosCam" ("MDCA"). 
//
MSrcRotate::MSrcRotate(const char* srcPos, const char* dca, const char *name, const char *title)
  : fSrcPos(NULL), fDCA(NULL), fRA(0), fDEC(0), fRunNumber(0)
{
    fName  = name  ? name  : gsDefName.Data();
    fTitle = title ? title : gsDefTitle.Data();

    fSrcPosName  = srcPos;
    fDCAName     = dca;
}

// -------------------------------------------------------------------------
//
// Look for/create the needed containers
// Check whether RA and DEC have been initialized
//
Int_t MSrcRotate::PreProcess(MParList *pList)
{
    // look for/create MSrcPosCam
  fSrcPos = (MSrcPosCam*)pList->FindObject(AddSerialNumber(fSrcPosName), "MSrcPosCam");
  if (!fSrcPos)
    {
      *fLog << warn << AddSerialNumber(fSrcPosName) << " [MSrcPosCam] not found... creating default container." << endl;
      fSrcPos = (MSrcPosCam*)pList->FindCreateObj("MSrcPosCam", AddSerialNumber(fSrcPosName));
      if(!fSrcPos)
	return kFALSE;
    }
  fIniSrcPos = *fSrcPos;

  // look for/create MDCA
  fDCA = (MDCA*)pList->FindObject(AddSerialNumber(fDCAName), "MDCA");
  if (!fDCA)
    {
      *fLog << warn << AddSerialNumber(fDCAName) << " [MDCA] not found... creating default container." << endl;
      fDCA = (MDCA*)pList->FindCreateObj("MDCA", AddSerialNumber(fDCAName));
      if(!fDCA)
	return kFALSE;
    }

  // look for MRawRunHeader
  fRunHeader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
  if (!fRunHeader)
    {
      *fLog << err << "MSrcRotate::PreProcess Error: MRawRunHeader not found... aborting" << endl;
      return kFALSE;
    }
  
  // look for MRawEvtHeader
  fEvtHeader = (MRawEvtHeader*)pList->FindObject("MRawEvtHeader");
  if (!fEvtHeader)
    {
      *fLog << err << "MSrcRotate::PreProcess Error: MRawEvtHeader not found... aborting." << endl;
      return kFALSE;
    }
  
  // look for/create the MObservatory
  fObservatory = (MObservatory*)pList->FindCreateObj("MObservatory");
  if(!fObservatory)
    {
      *fLog << err << "MSrcRotate::PreProcess Error: MObservatory not found... aborting." << endl;
      return kFALSE;
    }

  // check RA and DEC
  if(fRA==0. || fDEC ==0)
    *fLog << warn << "MSrcRotate::PreProcess Warning: RA=" << fRA << "or DEC=" << fDEC << " could be not initialized" << endl;    
	
  return kTRUE;
}

// -------------------------------------------------------------------------
//
// Perform the rotation of the source position 
// 
// FIXME: for the time being, this is computed by assuming constant event rate
//
Int_t MSrcRotate::Process()
{  
  if(fRunHeader->GetRunNumber()!=fRunNumber)
    {
      fRunNumber=fRunHeader->GetRunNumber();

      // save the number of events, initial and final times
      fNEvts    = fRunHeader->GetNumEvents();
      fFirstEvt = fEvtHeader->GetDAQEvtNumber();
      fIniTime  = fRunHeader->GetRunStart();
      fFinTime  = fRunHeader->GetRunEnd();
      fDeltaT   = (fFinTime.GetMjd()-fIniTime.GetMjd())/fNEvts;
      
      if((ULong_t)round(fIniTime.GetMjd())!=(ULong_t)round(fFinTime.GetMjd()))
	{
	  *fLog << err << "MSrcRotate::Process Error: Inial and final MJDs are different ("<<fIniTime.GetMjd()<<"!="<<fFinTime.GetMjd()<<")" << endl;
	  return kFALSE;
	}

#ifdef DEBUG
      cout << endl << "********************"  << endl;
      cout << "Run number: " << fRunHeader->GetRunNumber() << endl;
      cout << "Number of events: " << fNEvts << endl;
      cout << "First event: " << fFirstEvt << endl;
      cout << "Initial MJD date: " << fIniTime.GetMjd() << endl;
      cout << "Final MJD   date: " << fFinTime.GetMjd() << endl;
      cout << "Delta t: " << fDeltaT << endl;
#endif
    }
  

  // Compute the event time
  // FIXME: for the time being, this is computed by assuming constant event rate
  MTime eventTime;
  Double_t newMJD = fIniTime.GetMjd() + (fFinTime.GetMjd()-fIniTime.GetMjd())*(fEvtHeader->GetDAQEvtNumber()-fFirstEvt)/fNEvts;  
  eventTime.SetMjd(newMJD);  
  
  // de-rotate the source position
  Double_t rotationAngle = fObservatory->RotationAngle(fRA,fDEC,eventTime);
  
  Float_t c = TMath::Cos(rotationAngle);
  Float_t s = TMath::Sin(rotationAngle);
  // perform a rotation of -rotationAngle to move the source back to the "initial" position
  Float_t newX = c*fIniSrcPos.GetX()+s*fIniSrcPos.GetY();
  Float_t newY = -s*fIniSrcPos.GetX()+c*fIniSrcPos.GetY();

#ifdef DEBUG
  Double_t rotationAngleComp = fObservatory->RotationAngle(0.1256,2.63);
  cout << "Event " << fEvtHeader->GetDAQEvtNumber() << endl;
  cout << "newMJD=" << newMJD <<", rotationAngle=" << rotationAngle <<", rotationAngleComp=" << rotationAngleComp << ", oldX="<<fIniSrcPos.GetX()<< ", oldY="<<fIniSrcPos.GetY()<< ", newX="<<newX<< ", newY="<<newY << endl,
#endif

  fSrcPos->SetX(newX);
  fSrcPos->SetY(newY);
  fDCA->SetRefPoint(newX,newY);

  return kTRUE;
}
