////////////////////////////////////////////////////////////////////////
// $Id: CandFitTrackMS.cxx,v 1.3 2002/08/08 20:51:00 bringley Exp $
//
// This is a concrete Event Candidate (Reconstruction) object.
// CandFitTrackMS is descended from CandFitTrack.
//
// Each concrete CandBase must define a Dup function.
// CandBase must grant friendship to class CandRefer.
//
// Tom Bringley
// ttb2@duke.edu
// 6/13/2001
////////////////////////////////////////////////////////////////////////

#include "Algorithm/AlgHandle.h"
#include "Algorithm/AlgConfig.h"
#include "CandFitTrackMS/CandFitTrackMS.h"
#include "CandFitTrackMS/CandFitTrackMSHandle.h"
#include "MessageService/MsgService.h"

ClassImp(CandFitTrackMS)

//______________________________________________________________________
CVSID("$Id: CandFitTrackMS.cxx,v 1.3 2002/08/08 20:51:00 bringley Exp $");

//______________________________________________________________________
CandFitTrackMS::CandFitTrackMS()
{
  MSG("Cand", Msg::kDebug)
                        << "Begin CandFitTrackMS::CandFitTrackMS() ctor: " << endl
                                           << "UidInt = " << GetUidInt()
                           << ", ArchUidInt " << GetArchUidInt() << endl
                             << "No. of links = " << GetNLinks() << endl
                          << "End CandFitTrackMS::CandFitTrackMS() ctor." << endl;
  Init();
}

//______________________________________________________________________
CandFitTrackMS::CandFitTrackMS(AlgHandle &ah) :
  CandFitTrack(ah)      // Should be the next class up on inheritance chain
{

// The sole purpose of this constructor is to transmit the AlgHandle
// up the inheritance chain to CandBase without having to invoke the
// full constructor of an intermediate Candidate type which the highest
// level Candidate might inherit from.  One only wants to create the
// LocalHandle and invoke the RunAlg() method in the lowest level class.

  Init();

}

//______________________________________________________________________
CandFitTrackMS::CandFitTrackMS(AlgHandle &ah, CandHandle &ch, CandContext &cx) :
  CandFitTrack(ah)      // Should be the next class up on inheritance chain
{
  CreateLocalHandle();
  MSG("Cand", Msg::kDebug)
             << "Begin CandFitTrackMS::CandFitTrackMS(AlgHandle &, CandHandle &, "
                                      << "CandContext &) ctor: " << endl
                                           << "UidInt = " << GetUidInt()
                           << ", ArchUidInt " << GetArchUidInt() << endl
                             << "No. of links = " << GetNLinks() << endl
               << "End CandFitTrackMS::CandFitTrackMS(AlgHandle &, CandHandle &, "
                                      << "CandContext &) ctor." << endl;

  Init();

  {                                         // Start of scope.
    CandFitTrackMSHandle csh(this);              // csh will go out of scope
    ch = csh;                               // after setting ch.
  }
  ah.RunAlg(ch, cx);
}

//______________________________________________________________________
CandFitTrackMS::CandFitTrackMS(const CandFitTrackMS &rhs) :
  CandFitTrack(rhs)     // Should be the next class up on inheritance chain
{
  Init();
  MSG("Cand", Msg::kDebug)
    << "Begin CandFitTrackMS::CandFitTrackMS(const CandFitTrackMS &rhs) ctor: " << endl
                                           << "UidInt = " << GetUidInt()
                           << ", ArchUidInt " << GetArchUidInt() << endl
                             << "No. of links = " << GetNLinks() << endl
      << "End CandFitTrackMS::CandFitTrackMS(const CandFitTrackMS &rhs) ctor." << endl;
}

//______________________________________________________________________
CandFitTrackMS::~CandFitTrackMS()
{
  MSG("Cand", Msg::kDebug)
                       << "Begin CandFitTrackMS::~CandFitTrackMS() dtor: " << endl
                                           << "UidInt = " << GetUidInt()
                           << ", ArchUidInt " << GetArchUidInt() << endl
                             << "No. of links = " << GetNLinks() << endl
                         << "End CandFitTrackMS::~CandFitTrackMS() dtor." << endl;

}

//______________________________________________________________________
void CandFitTrackMS::CreateLocalHandle()
{
  SetLocalHandle(new CandFitTrackMSHandle(this));
}

//______________________________________________________________________
CandFitTrackMS *CandFitTrackMS::Dup() const
{

// Base copy ctor dups owned pointers, but defers copying Daughter List.
// Daughter List copy is made in the derived class Dup() function.
// This is because base class copy constructor hasn't yet created
// fLocalHandle with a CandHandle* of the full derived type.
  CandFitTrackMS *cb = new CandFitTrackMS(*this);       // Copy-ctor dups ptrs
  cb->CreateLocalHandle();   // Initializes fLocalHandle after copy-ctor
  TIter iterdau = GetDaughterIterator();
  CandHandle *dau;
  while ((dau=(CandHandle *) iterdau())) cb->AddDaughterLink(*dau);
  return cb;
}

//______________________________________________________________________
CandFitTrackMSHandle CandFitTrackMS::MakeCandidate(AlgHandle &ah, CandContext &cx)
{
  CandFitTrackMSHandle csh;
  new CandFitTrackMS(ah, csh, cx);          // csh owns the new CandFitTrackMS
  return csh;
}

//______________________________________________________________________
void CandFitTrackMS::Init()
{
  fMomentumL = 0;
  fMomentumBF = 0;
  fMomentumMS = 0;
  fMomentumBoth = 0;
  fMomentumAlt = 0;

  fChi2L = 0;
  fChi2BF = 0;
  fChi2MS = 0;
  fChi2Both = 0;
  fChi2Alt = 0;
  
  fFlag = 0;
  fIter = 0;

  fEMChargeD = 0;
}
