// $Id: UgliDbiStrip.h,v 1.11 2005/08/26 19:00:02 rhatcher Exp $

#ifndef UGLIDBISTRIP_H
#define UGLIDBISTRIP_H

////////////////////////////////////////////////////////////////////////
// UgliDbiStrip
//
// Package: UgliGeometry
//
// Concept:
//
//
// R. Hatcher 2001-08-08
////////////////////////////////////////////////////////////////////////

#include "DatabaseInterface/DbiTableRow.h"
#include "LeakChecker/Lea.h"

#include "UgliGeometry/Ugli.h"
#include "Plex/PlexStripEndId.h"

class DbiValidityRec;

#include <iosfwd>

class UgliDbiStrip;
std::ostream& operator<<(std::ostream& os, const UgliDbiStrip& uds);

class UgliDbiStrip : public DbiTableRow
{

public:

// Constructors and destructors.

   UgliDbiStrip()
      : fStripId(), fTPosRelMdl(0), fLPosRelMdl(0),
        fZRotRelMdlRad(0), fTCurvForm(-1), fZCurvForm(-1) 
      { LEA_CTOR;
        for (int i=0; i<4; i++) {
           fTCurvParm[i] = 0;
           fZCurvParm[i] = 0;
        }
      };

   UgliDbiStrip(PlexStripEndId seid,
                Float_t tpos, Float_t lpos, Float_t zrot, 
                Short_t tcurvForm = -1, const Float_t* tcurvParm = 0,
                Short_t zcurvForm = -1, const Float_t* zcurvParm = 0)
      : fStripId(seid),
        fTPosRelMdl(tpos), fLPosRelMdl(lpos), fZRotRelMdlRad(zrot),
        fTCurvForm(tcurvForm), fZCurvForm(zcurvForm)
      { LEA_CTOR;
        fStripId.SetEnd(StripEnd::kWhole);
        fStripId.SetSubPart(StripEnd::kWhole);
        for (int i=0; i<4; i++) {
           fTCurvParm[i] = ((tcurvParm) ? tcurvParm[i] : 0);
           fZCurvParm[i] = ((zcurvParm) ? zcurvParm[i] : 0);
        }
      };

   virtual ~UgliDbiStrip(){ LEA_DTOR; };

// State testing member functions

   virtual Int_t GetAggregateNo() const { return fStripId.GetPlane(); }
          UInt_t GetIndex(UInt_t defIndex) const;
   static UInt_t HashToIndex(PlexStripEndId seid);

   Detector::Detector_t            GetDetector() const;
   PlaneView::PlaneView_t          GetView() const;
   PlaneCoverage::PlaneCoverage_t  GetCoverage() const;

   Int_t          GetPlane() const { return fStripId.GetPlane(); }
   Int_t          GetStrip() const { return fStripId.GetStrip(); }

   PlexStripEndId GetStripEndId() const { return fStripId; }
   Float_t        GetTPosRelMdl() const { return fTPosRelMdl; }
   Float_t        GetLPosRelMdl() const { return fLPosRelMdl; }
   Float_t        GetZRotRelMdlRad() const { return fZRotRelMdlRad; }
   Float_t        GetZRotRelMdlDeg() const { return fZRotRelMdlRad * Ugli::rad2deg; }
   Short_t        GetTCurvForm() const { return fTCurvForm; }
   Short_t        GetZCurvForm() const { return fZCurvForm; }
   const Float_t* GetTCurvParm() const { return fTCurvParm; }
   const Float_t* GetZCurvParm() const { return fZCurvParm; }

   virtual DbiTableRow* CreateTableRow() const { return new UgliDbiStrip; }

// I/O  member functions

   virtual void          Fill(DbiResultSet& rs,
                              const DbiValidityRec* vrec);
   virtual void          Store(DbiOutRowStream& ors,
                               const DbiValidityRec* vrec) const;

   virtual std::ostream& FormatToOStream(std::ostream& os, 
                                         Option_t *option="",
                                         const DbiValidityRec* vrec=0) const;
   virtual void          Print(Option_t *option="") const;

   static const char*    GetTableDescr();
   static void           SetDefensiveUnpkg(Bool_t defensive) 
      { fgDefensiveUnpkg = defensive; }
   static Bool_t         GetDefensiveUnpkg() { return fgDefensiveUnpkg; }

private:  

// Constructors and destructors.

   UgliDbiStrip(const UgliDbiStrip& from)
     : DbiTableRow(from) { LEA_CTOR; *this = from; }

// Data members

   PlexStripEndId fStripId;      // detector, plane (+view+cover), strip
   Float_t        fTPosRelMdl;    // view (tpos) placement of strip in module
   Float_t        fLPosRelMdl;    // lateral placement of strip on module
   Float_t        fZRotRelMdlRad; // rot about z-axis rel to module (alignd=0)
   Short_t        fTCurvForm;     // param scheme of curvature info in tpos dir
   Short_t        fZCurvForm;     // param scheme of curvature info in z dir
   Float_t        fTCurvParm[4];  // param values of tpos curvature
   Float_t        fZCurvParm[4];  // param values of z curvature

// class-wide static

   static Bool_t fgDefensiveUnpkg;

ClassDef(UgliDbiStrip,0)

};

inline Detector::Detector_t UgliDbiStrip::GetDetector() const
{ return fStripId.GetDetector(); }

inline PlaneView::PlaneView_t UgliDbiStrip::GetView() const
{ return fStripId.GetPlaneView(); }

inline PlaneCoverage::PlaneCoverage_t  UgliDbiStrip::GetCoverage() const
{ return fStripId.GetPlaneCoverage(); }

inline UInt_t UgliDbiStrip::GetIndex(UInt_t /* defIndex */) const 
{ return HashToIndex(fStripId); }

#endif  // UGLIDBISTRIP_H
