#include "DQLightInjection.h"
#include "RawChip.h"

#include "RawData/RawDigit.h"
#include "MessageService/MsgService.h"
 
//
// $Log $
//

ClassImp(DQLightInjection)

CVSID("$Id: DQLightInjection.cxx,v 1.2 2006/11/15 11:02:02 blake Exp $");

DQLightInjection::DQLightInjection() :
  fTime(-1),
  fCalibPoint(-1), 
  fCalibType(-1),
  fPulserBox(-1), 
  fPulserLed(-1),
  fPulseHeight(-1), 
  fPulseWidth(-1),
  fRawReadout(0), 
  fTpmtHitList(0)
{
  fRawReadout = new RawReadout();
  
  fTpmtHitList = new TObjArray(); 
}
 
DQLightInjection::DQLightInjection(const DQLightInjection& rhs) :
  TObject(rhs),
  fTime(rhs.fTime),
  fCalibPoint(rhs.fCalibPoint), 
  fCalibType(rhs.fCalibType),
  fPulserBox(rhs.fPulserBox), 
  fPulserLed(rhs.fPulserLed),
  fPulseHeight(rhs.fPulseHeight), 
  fPulseWidth(rhs.fPulseWidth),
  fRawReadout(rhs.fRawReadout),
  fTpmtHitList(rhs.fTpmtHitList)
{
  
}
   
DQLightInjection::~DQLightInjection()
{
  delete fRawReadout;

  fTpmtHitList->Delete();
  delete fTpmtHitList;
}

void DQLightInjection::Process(RawLIAdcSummaryBlock* rdb)
{
  MSG("DataQuality",Msg::kVerbose) << " DQLightInjection::Process(RawLIAdcSummaryBlock) " << endl;

  fTime=rdb->GetVldContext().GetTimeStamp().GetSec();
  fCalibPoint=rdb->GetCalibPoint();
  fCalibType=rdb->GetCalibType();
  fPulserBox=rdb->GetPulserBox();
  fPulserLed=rdb->GetLed();
  fPulseHeight=rdb->GetPulseHeight();
  fPulseWidth=rdb->GetPulseWidth();

  MSG("DataQuality",Msg::kVerbose) << "   Time=" << fTime << endl;
  MSG("DataQuality",Msg::kVerbose) << "   CalibPoint=" << fCalibPoint << endl;
  MSG("DataQuality",Msg::kVerbose) << "   CalibType=" << fCalibType << endl;
  MSG("DataQuality",Msg::kVerbose) << "   PulserBox=" << fPulserBox << endl;
  MSG("DataQuality",Msg::kVerbose) << "   PulserLed=" << fPulserLed << endl;
  MSG("DataQuality",Msg::kVerbose) << "   PulseHeight=" << fPulseHeight << endl;
  MSG("DataQuality",Msg::kVerbose) << "   PulseWidth=" << fPulseWidth << endl;
  
}

void DQLightInjection::Process(RawLiTpmtDigitsBlock* rdb)
{
  MSG("DataQuality",Msg::kVerbose) << " DQLightInjection::Process(RawLiTpmtDigitsBlock) " << endl;

  Int_t n;
  Int_t time;
  Int_t adc;
  Int_t pulserbox;
  Int_t maxadc = -1;
  Int_t maxpulserbox = -1;

  VldContext vldc = (VldContext)(rdb->GetVldContext()); 
  time=vldc.GetTimeStamp().GetSec();

  if(time!=fTime){
    fTime=time;
    fCalibPoint=-1;
    fCalibType=-1;
    fPulserBox=-1;
    fPulserLed=-1;
    fPulseHeight=-1;
    fPulseWidth=-1;
  }

  // reset the list of TPMT hits
  fTpmtHitList->Delete();
  
  // iterate over raw TPMT hits
  // (pulser box is given by hit with largest adc)
  for(n=0;n<rdb->GetNumberOfDigits();n++){
    RawDigit* rd = (RawDigit*)(rdb->At(n));
    if( rd ){
      RawChannelId rawch = (RawChannelId)(rd->GetChannel());   

      if( vldc.GetDetector()==Detector::kNear
       && rawch.GetElecType()==ElecType::kQIE ){
        adc = rd->GetADC();
        pulserbox = fRawReadout->GetPulserBox(rawch);
        if( (maxpulserbox<0)
	 || (pulserbox>=0 && adc>maxadc) ){
          maxadc=adc; maxpulserbox=pulserbox; }
        RawChip* tpmt = new RawChip(rawch,rd->GetADC(),rd->GetTDC(),RawReadout::kTpmt,RawChip::kGood,0);
        fTpmtHitList->Add(tpmt);
      }

      if( vldc.GetDetector()==Detector::kFar
       && rawch.GetElecType()==ElecType::kVA ){
        adc = rd->GetADC();
        pulserbox = fRawReadout->GetPulserBox(rawch);
        if( (maxpulserbox<0)
	 || (pulserbox>=0 && adc>maxadc) ){
	  maxadc=adc; maxpulserbox=pulserbox; }
        RawChip* tpmt = new RawChip(rawch,rd->GetADC(),rd->GetTDC(),RawReadout::kTpmt,RawChip::kGood,0);
        fTpmtHitList->Add(tpmt);
      }

    }
  }

  if(maxpulserbox>-1 && maxpulserbox!=fPulserBox){
    MSG("DataQuality",Msg::kVerbose) << " Pulser Box: " << maxpulserbox << " Adc: " << maxadc << endl;
    MSG("DataQuality",Msg::kVerbose) << " Overwritten Pulser Box: " << fPulserBox << " -> " << maxpulserbox << endl;
    fPulserBox=maxpulserbox;
  }

  MSG("DataQuality",Msg::kVerbose) << " Found " << 1+this->GetLast() << " Tpmt Hits" << endl;

}

Int_t DQLightInjection::GetLast() const
{
  return fTpmtHitList->GetLast();
}
  
TObject* DQLightInjection::At(Int_t i) const
{
  return (TObject*)(fTpmtHitList->At(i));
}
