////////////////////////////////////////////////////////////////////////
// $Id: AlgDigiPair.cxx,v 1.23 2002/12/15 16:08:06 rhatcher Exp $
//
// AlgDigiPair
//
// Begin_Html<img src="../../pedestrians.gif" align=center>
// <a href="../source_warning.html">Warning for beginners</a>.<br> 
//
// An Algorithm class that fills a CandDigiPair using CandDigits that
// correspond to opposite ends of the same strip.
//
// Author:  P.S. Miyagawa 8/2000
//
// Also see <a href="../../root_crib/index.html">The ROOT Crib</a> and 
// <a href="../ALG_Classes.html"> Algorithm Classes</a> (part of
// <a href="../index.html">The MINOS Class User Guide</a>)End_Html
////////////////////////////////////////////////////////////////////////

#include <cassert>

#include "Candidate/CandContext.h"
#include "CandData/CandHeader.h"
#include "CandData/CandRecord.h"
#include "CandDigit/CandDigitHandle.h"
#include "MessageService/MsgService.h"
#include "UgliGeometry/UgliGeomHandle.h"
#include "BubbleSpeak/AlgDigiPair.h"
#include "BubbleSpeak/CandDigiPairHandle.h"

ClassImp(AlgDigiPair)

//......................................................................

CVSID("$Id: AlgDigiPair.cxx,v 1.23 2002/12/15 16:08:06 rhatcher Exp $");

//......................................................................

AlgDigiPair::AlgDigiPair()
{
//
//  Purpose:    Default constructor.
//
//  Arguments:  n/a
//
//  Return:     n/a
//
}

//......................................................................

AlgDigiPair::~AlgDigiPair()
{
//
//  Purpose:    Default destructor to delete all static members.
//
//  Arguments:  n/a
//
//  Return:     n/a
//
}

//......................................................................

void AlgDigiPair::RunAlg(AlgConfig & /* ac */, CandHandle &ch, CandContext &cx)
{
//
//  Purpose:  Fills the CandDigiPair using the CandDigit(s) supplied.
//
//  Argument:
//    ac        in    AlgConfig (not used).
//    ch        in    Handle to the CandDigiPair to fill.
//    cx        in    CandContext containing a TObjArray of
//                    CandDigitHandles.
//
//  Return:   n/a
//

   MSG("BubAlg", Msg::kVerbose)
      << "Starting AlgDigiPair::RunAlg()" << endl;

   assert( ch.InheritsFrom("CandDigiPairHandle") );
   CandDigiPairHandle &chh = dynamic_cast<CandDigiPairHandle &>(ch);

   assert(cx.GetDataIn());

// Check for correct input.
   assert(cx.GetDataIn()->InheritsFrom("TObjArray"));
   const TObjArray *tary =
                    dynamic_cast<const TObjArray*>(cx.GetDataIn());

// Loop over all CandDigitHandles.
   TIter cdhItr(tary);
   while (CandDigitHandle *cdh =
             dynamic_cast<CandDigitHandle *>(cdhItr())) {
      chh.AddDaughterLink(*cdh);
   }

// Set best StripEndId.
   cdhItr.Reset();
   const CandDigitHandle *cdh =
      dynamic_cast<const CandDigitHandle *>(cdhItr());
   assert(cdh);
   chh.SetBestStripEndId(cdh->GetPlexSEIdAltL().GetBestSEId());

// Set TPos and ZPos.
   const CandRecord *candrec = cx.GetCandRecord();
   assert(candrec);
   const VldContext *vld = candrec->GetVldContext();
   assert(vld);
   UgliGeomHandle ugh(*vld);
   UgliStripHandle ush = ugh.GetStripHandle(chh.GetStripEndId());
   if (ush.IsValid()) {
      chh.SetTPos(ush.GetTPos());
      chh.SetZPos(ush.GetScintPlnHandle().GetZ0());
   }
}

//......................................................................

void AlgDigiPair::Trace(const char *c) const
{
//
//  Purpose:  Trace the AlgDigiPair.
//
//  Arguments:
//    c          in    String tag for the trace.
//
//  Return:   n/a
//

  MSG("BubCand", Msg::kDebug)
     << "**********Begin AlgDigiPair::Trace(\"" << c << "\")" << endl
     << "**********End AlgDigiPair::Trace(\"" << c << "\")" << endl;
}
