///////////////////////////////////////////////////////////////////////
// $Id: CalMapperFits.cxx,v 1.15 2007/01/15 19:52:01 rhatcher Exp $
//
// CalMapperFits
//
// 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/CalMapperFits.h"
#include "MessageService/MsgService.h"
#include "DatabaseInterface/DbiOutRowStream.h"
#include "DatabaseInterface/DbiResultSet.h"
#include "DatabaseInterface/DbiValidityRec.h"
#include <cmath>

ClassImp(CalMapperFits)

CVSID("$Id: CalMapperFits.cxx,v 1.15 2007/01/15 19:52:01 rhatcher Exp $\n  \
      CVSID_DBIRESULTPTR ");

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

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

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

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

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


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

void CalMapperFits::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() == "CALMAPPERFITS" ) {
      // Dumb method.
     rs  >> fSEIDkey >> fStripEndId >> fTestID >> fStripNum >> fStripEnd >> fNorm >> fNPoints >> fC1 >> fC1Err >> fLambda1 >> fLambda1Err >> fC2 >> fC2Err >> fLambda2 >> fLambda2Err;
   }
   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 == "TESTID" )       rs >> fTestID;
	 else if( colName == "STRIPNUM" )       rs >> fStripNum;
	 else if( colName == "STRIPEND" )       rs >> fStripEnd;
	 else if( colName == "NORM" )       rs >> fNorm;
	 else if( colName == "NPOINTS" )       rs >> fNPoints;
	 else if( colName == "C1" )       rs >> fC1;
	 else if( colName == "C1ERR" )       rs >> fC1Err;
	 else if( colName == "LAMBDA1" )       rs >> fLambda1;
	 else if( colName == "LAMBDA1ERR" )       rs >> fLambda1Err;
	 else if( colName ==  "C2" )       rs >> fC2;
	 else if( colName ==  "C2ERR" )       rs >> fC2Err;
	 else if( colName ==  "LAMBDA2" )       rs >> fLambda2;
	 else if( colName ==  "LAMBDA2ERR" )       rs >> fLambda2Err;
	 else {
	    MSG("Dbi",Msg::kDebug) << "Ignoring column " << curCol 
				   << "(" << colName << ")"
				   << "; not part of CalMapperFits" 
				   << endl;
	 rs.IncrementCurCol();
	 }
      }
   }

}


//.....................................................................
void CalMapperFits::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 << fTestID << fStripNum << fStripEnd << fNorm << fNPoints << fC1 << fC1Err << fLambda1 << fLambda1Err << fC2 << fC2Err << fLambda2 << fLambda2Err;
    
}

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

Float_t CalMapperFits::AttenCorrected(const Float_t rawcharge, const Float_t Y) const {
//
//
//  Purpose: To apply sume dummy calibration to the linear charge  
//
//  Arguments: 
//    xxxxxxxxx    in    yyyyyy
//
//  Return:    
//
//  Contact:   R.Nichol
//
//  Specification:-
//  =============
//
//  o 

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

//  None.

  Float_t Corr = fC1 * exp(-Y/fLambda1) + fC2 * exp(-Y/fLambda2);

   MSG("Calib",Msg::kVerbose) << "StripEndId " << fStripEndId 
				   << " raw charge " << rawcharge
				   << " C1  " << fC1
				   << " C2  " << fC2
				   << " Lambda1  " << fLambda1
				   << " Lambda2  " << fLambda2
                                   << " Y " << Y
                                   << " Corr " << Corr 
				   << " Atten Corrected " 
				   << rawcharge/Corr << "\n";
   return (rawcharge / Corr);
}

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