///////////////////////////////////////////////////////////////////////
// $Id: CalTimeCalibration.cxx,v 1.12 2005/03/11 16:17:48 west Exp $
//
// CalTimeCalibration
//
// Package: Dbi (Database Interface).

// Begin_Html<img src="../../pedestrians.gif" align=center>
// <a href="../source_warning.html">Warning for beginners</a>.<br>
// Also see <a href="../../root_crib/index.html">The ROOT Crib</a> and
// <a href="../index.html">The MINOS Class User Guide</a>End_Html
//
//rjn@hep.ucl.ac.uk
///////////////////////////////////////////////////////////////////////

#include "Calibrator/CalTimeCalibration.h"
#include "MessageService/MsgService.h"
#include "DatabaseInterface/DbiOutRowStream.h"
#include "DatabaseInterface/DbiResultSet.h"
#include "DatabaseInterface/DbiValidityRec.h"

ClassImp(CalTimeCalibration)

//   Definition of static data members
//   *********************************

CVSID("$Id: CalTimeCalibration.cxx,v 1.12 2005/03/11 16:17:48 west Exp $\n  \
      CVSID_DBIRESULTPTR ");

//  Instantiate associated Result Pointer class.
//  *******************************************

#include "DatabaseInterface/DbiResultPtr.tpl"
template class  DbiResultPtr<CalTimeCalibration>;

#include "DatabaseInterface/DbiWriter.tpl"
template class  DbiWriter<CalTimeCalibration>;

// Definition of member functions (alphabetical order)
// ***************************************************


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

void CalTimeCalibration::Fill(DbiResultSet& rs, 
                              const DbiValidityRec* /* vrec */) {

//
//
//  Purpose:  Fill object from Result Set
//
//  Arguments: 
//    rs           in    Result Set used to fill object
//    vrec         in    Associated validity record (or 0 if filling
//                                                    DbiValidityRec)
//  Return:    
//
//  Contact:   N. West
//
//  Specification:-
//  =============
//
//  o Fill object from current row of Result Set.

//  Program Notes:-
//  =============

//  This method demonstrates both the "dumb" fill method (just
//  load the data as it comes) and the smart method (check column
//  name and load according to column order). 

   if ( rs.TableName() == "CALTIMECALIBRATION" ) {
      // Dumb method.
     rs >> fSEIDkey >> fStripEndId >> fScale >> fOffset >> fSlewC1 >> fSlewC2 >> fSlewC3 >> fSlewC4;
   }
   else {
      
      // Smart method
      Int_t numCol = rs.NumCols();
      //  The first column (SeqNo) has already been processed.
      for (Int_t curCol = rs.HasRowCounter() ? 3 : 2; curCol <= numCol; ++curCol) {
	 string colName = rs.CurColName();
	 if (      colName == "STRIPENDID" ) rs >> fStripEndId;
	 else if ( colName == "SEIDKEY" )       rs >> fSEIDkey;
	 else if ( colName == "SCALE" )       rs >> fScale;
	 else if ( colName == "OFFSET" )    rs >> fOffset;
	 else if ( colName == "SLEWCONST1" )    rs >> fSlewC1;
	 else if ( colName == "SLEWCONST2" )    rs >> fSlewC2;
	 else if ( colName == "SLEWCONST3" )    rs >> fSlewC3;
	 else if ( colName == "SLEWCONST4" )    rs >> fSlewC4;

	 else {
	    MSG("Dbi",Msg::kDebug) << "Ignoring column " << curCol 
				   << "(" << colName << ")"
				   << "; not part of CalTimeCalibration" 
				   << endl;
	 rs.IncrementCurCol();
	 }
      }
   }
   
   
}


//.....................................................................
void CalTimeCalibration::Store(DbiOutRowStream& ors,
                               const DbiValidityRec* /* vrec */) const {
   //
//
//  Purpose:  Stream object to output row stream
//
//  Arguments: 
//    ors          in     Output row stream.
//    vrec         in    Associated validity record (or 0 if filling
//                                                    DbiValidityRec)
//
//  Return:    
//
//  Contact:   N. West
//
//  Specification:-
//  =============
//
//  o  Stream object to output row stream.

//  Program Notes:-
//  =============

//  None.

  ors << fSEIDkey  << fStripEndId << fScale << fOffset <<fSlewC1 <<fSlewC2 <<fSlewC3 <<fSlewC4 ;
    
}

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


Double_t CalTimeCalibration::GetTime(const Double_t rawtime) const {
//
//
//  Purpose: To apply sume dummy calibration to the linear charge  
//
//  Arguments: 
//    xxxxxxxxx    in    yyyyyy
//
//  Return:    
//
//  Contact:   R.Nichol
//
//  Specification:-
//  =============
//
//  o 

//  Program Notes:-
//  =============

//  None.
   MSG("Calib",Msg::kVerbose) << "StripEndId " << fStripEndId 
			      << " raw time " << rawtime
			      << " Scale " << fScale
			      << " Offse t" << fOffset
			      << " Time " 
			      << rawtime/fScale - fOffset << "\n";
   return (rawtime/(Double_t)fScale - (Double_t)fOffset);
}


























