////////////////////////////////////////////////////////////////////////
// $Id: AlgSuperSlice.cxx,v 1.5 2004/08/19 18:49:43 musser Exp $
//
// AlgSuperSlice
//
// Begin_Html<img src="../../pedestrians.gif" align=center>
// <a href="../source_warning.html">Warning for beginners</a>.<br> 
//
// An Algorithm class that fills a CandSlice using CandStrips that are
// in the same time slice.
//
// Author:  P.S. Miyagawa 3/2001
//
// 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 "MessageService/MsgService.h"
#include "RecoBase/CandSliceHandle.h"
#include "RecoBase/CandStripHandle.h"
#include "RecoBase/CandStripListHandle.h"
#include "BubbleSpeak/AlgSuperSlice.h"

ClassImp(AlgSuperSlice)

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

CVSID("$Id: AlgSuperSlice.cxx,v 1.5 2004/08/19 18:49:43 musser Exp $");

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

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

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

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

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

void AlgSuperSlice::RunAlg(AlgConfig & /* ac */, CandHandle &ch,
                           CandContext &cx)
{
//
//  Purpose:  Fills the CandSlice using the CandStrips supplied.
//
//  Argument:
//    ac        in    AlgConfig (not used).
//    ch        in    Handle to the CandSlice to fill.
//    cx        in    CandContext containing either CandStripListHandle
//                    or TObjArray of CandStripHandles.
//
//  Return:   n/a
//

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

   assert(ch.InheritsFrom("CandSliceHandle"));
   CandSliceHandle &csh = dynamic_cast<CandSliceHandle &>(ch);

   assert(cx.GetDataIn());

// Check for input type and create iterator.
   TIter *chhItr = 0;
   if (cx.GetDataIn()->InheritsFrom("TObjArray")) {
      const TObjArray *tary =
                       dynamic_cast<const TObjArray*>(cx.GetDataIn());
      chhItr = new TIter(tary);
   }
   else if (cx.GetDataIn()->InheritsFrom("CandStripListHandle")) {
      const CandStripListHandle *chlh =
            dynamic_cast<const CandStripListHandle *>(cx.GetDataIn());
      chhItr = new TIter(chlh->GetDaughterIterator());
   }
   else assert(0);

// Loop over all CandStripHandles.
   Double_t begtime = 1e7;
   Double_t endtime = -1e7;
   while (CandStripHandle *chh =
             dynamic_cast<CandStripHandle *>((*chhItr)())) {
      csh.AddDaughterLink(*chh);
      Double_t chhtime = chh->GetCorrBegTime();
      if (chhtime < begtime) begtime = chhtime;
      if (chhtime > endtime) endtime = chhtime;
   }

// Set begin and end times of CandSlice.
//   csh.SetCorrBegTime(begtime);
//   csh.SetCorrEndTime(endtime);

// Clean-up.
   delete chhItr;
   chhItr = 0;
}

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

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

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