////////////////////////////////////////////////////////////////////////
// $Id: AlgSliceListFromDSlice.cxx,v 1.1.1.1 2005/05/10 16:28:19 minoscvs Exp $
//
// AlgSliceListFromDSlice.cxx
//
////////////////////////////////////////////////////////////////////////

#include <cassert>

#include "CandDSlice/AlgSliceListFromDSlice.h"
#include "CandDSlice/CandDSliceListHandle.h"
#include "CandDSlice/DigitVector.h"

#include "Algorithm/AlgConfig.h"
#include "Algorithm/AlgFactory.h"
#include "Algorithm/AlgHandle.h"
#include "CandData/CandHeader.h"
#include "CandData/CandRecord.h"
#include "CandDigit/CandDigitHandle.h"
#include "CandDigit/CandDigitListHandle.h"
#include "Candidate/CandContext.h"
#include "RecoBase/CandSliceListHandle.h"
#include "RecoBase/CandSliceHandle.h"
#include "RecoBase/CandSlice.h"
#include "RecoBase/CandStripList.h"
#include "MessageService/MsgService.h"
#include "MinosObjectMap/MomNavigator.h"
#include "RawData/RawHeader.h"
#include "RawData/RawRecord.h"
#include "RawData/RawDigitDataBlock.h"
#include "UgliGeometry/UgliGeomHandle.h"
#include "UgliGeometry/UgliStripHandle.h"
#include "Validity/VldContext.h"


ClassImp(AlgSliceListFromDSlice)
CVSID( " $Id: AlgSliceListFromDSlice.cxx,v 1.1.1.1 2005/05/10 16:28:19 minoscvs Exp $ ");


//______________________________________________________________________
AlgSliceListFromDSlice::AlgSliceListFromDSlice()
{
}

//______________________________________________________________________
AlgSliceListFromDSlice::~AlgSliceListFromDSlice()
{
}

//______________________________________________________________________

void AlgSliceListFromDSlice::RunAlg(AlgConfig & algConfig, 
		       CandHandle &candHandle,  // thing to make
		       CandContext &candContext)
{
  ///
  /// Looks at the DSlice list. Takes each DSlice (which is a digit list)
  /// and hands it to the strip maker algorithm to make a strip list, which is in
  /// fact a slice.
  ///

  // Make sure output is the right kind.
  assert(candHandle.InheritsFrom("CandSliceListHandle"));

  // Get config parameters.
  const char* stripListAlgorithm = algConfig.GetCharString("StripListAlgorithm");
  const char* stripListAlgConfig = algConfig.GetCharString("StripListAlgConfig");

  // Set up the strip maker.
  AlgFactory &af = AlgFactory::GetInstance();
  AlgHandle ah = af.GetAlgHandle(stripListAlgorithm,stripListAlgConfig);
  CandContext cxx(this,candContext.GetMom());

  // Get the input data (the list of DSlices)
  const CandDSliceListHandle* dsliceList = 
    dynamic_cast<const CandDSliceListHandle*>(candContext.GetDataIn());
    
  if(!dsliceList) {
    MSG("DSlice",Msg::kError) << "Data input to AlgSliceListFromDSlice was bad." << std::endl;
    return;
  }

  Int_t nslice=0;
  CandDigitListHandleItr dsliceItr(dsliceList->GetDaughterIterator());
  while( CandDigitListHandle* dslice = dsliceItr()) {
    cxx.SetDataIn(dslice);
    CandSliceHandle slice = CandSlice::MakeCandidate(ah,cxx); // Some slight jiggery-pokery here: 
                                                              // I'm running a strip algorithm to make a slice
    slice.SetName(Form("Slice%d",nslice));
    slice.SetTitle(Form("Slice%d from %s",nslice,dslice->GetName()));
    candHandle.AddDaughterLink(slice);
    nslice++;
  }
 
}

//______________________________________________________________________
void AlgSliceListFromDSlice::Trace(const char * /* c */) const
{
}
