#include <RAT/Log.hh>
#include <RAT/DB.hh>
#include <RAT/DU/PanelInfo.hh>
using namespace RAT;
using namespace RAT::DU;

#include <vector>
using namespace std;

ClassImp( RAT::DU::PanelInfo )

void
PanelInfo::BeginOfRun()
{
  DBLinkPtr pmtDB = DB::Get()->GetLink( "PANELINFO" );
  fPanels = pmtDB->GetIArray("panel_number");
  vector<int> types = pmtDB->GetIArray("panel_type");
  vector<double> pmtx = pmtDB->GetDArray("x");
  vector<double> pmty = pmtDB->GetDArray("y");
  vector<double> pmtz = pmtDB->GetDArray("z");
  vector<double> dirx = pmtDB->GetDArray("u");
  vector<double> diry = pmtDB->GetDArray("v");
  vector<double> dirz = pmtDB->GetDArray("w");
  Log::Assert( types.size() == fPanels.size() && types.size() == pmtx.size(), "PanelInfo::BeginOfRun: Miss-sized Panel database arrays." );

  fTypes.resize(types.size());
  fPositions.resize(types.size());
  fDirections.resize(types.size());

  for( size_t id = 0; id < types.size(); id++ )
    {
      fTypes[id] = static_cast<EPanelType>( types[id] );
      fPositions[id] = TVector3( pmtx[id], pmty[id], pmtz[id] );
      fDirections[id] = TVector3( dirx[id], diry[id], dirz[id] ).Unit();
    }
  debug << "PanelInfo::BeginOfRun: Loaded " << fTypes.size() << " Panels.\n";
}