//////////////////////////////////////////////////////////////////////////////
// AlgCalDetSI
//
// A class to fill CandCalDetSI 
// CalDetSI = "CalDet Specific Information"
//
// Tricia Vahle & Mike Kordosky
//
// Began: January 8, 2002
//
// About this class:
// This class was originally designed to run on caldet data
// taken without a beam trigger (the 2001 run).  Most (99%) 
// of that data was crap due to asynchronous dead time, 
// poor beam focusing, etc. This class was used to skim the raw
// data before any calibration or reconstruction and extract
// some information on the event PID, dead chips, etc. That info
// was made into a candidate: CandCalDetSI.
//
// The Cutter class read CandCalDetSI objects and made very simple
// selections to extract the good events.
//
// With the advent of the beam trigger (2002) much of the original
// motivation for this class was no longer relevant, because
// 99% of that data was not crap.  However, CalDetSI objects
// are still used to extract caldet specific information from the data.
// 
// Modified: many times since (this introduction modified 15 Feb, 2005)
//
//////////////////////////////////////////////////////////////////////////////

#ifndef ALGCALDETSI_H
#define ALGCALDETSI_H
#include <set>
#include <vector>
#include "Algorithm/AlgBase.h"
#include "RawData/RawChannelId.h"

class RawDigitDataBlock;
class RawDeadChipBlock;
class RawOvershootBlock;
class RawTOFBlock;
class RawSnarlHeaderBlock;
class RawDaqSnarlHeader;
class RawVarcErrorInTfBlock;
class RawChannelId;

class AlgCalDetSI : public AlgBase
{

 public:
  AlgCalDetSI();                //constructor
  virtual ~AlgCalDetSI();       //destructor

  //get data and use CandCalDetSIHandle methods 
  //to set CandCalDetSI member variables
  virtual void RunAlg(AlgConfig &ac, CandHandle &ch, CandContext &cx);
  

 private:
  Int_t fRunNumber;  //the run number
  static Int_t fLastSnarlTick;    //timestamp of last snarl
  static Int_t fLastSnarlSec;     //timeframe of last snarl
  static Int_t fTimeFrame;        //current timeframe
  RawDigitDataBlock *fRawDDB;
  RawDeadChipBlock *fRawDCB;
  RawOvershootBlock *fRawOSB;
  RawTOFBlock *fRawTOFB;
  RawSnarlHeaderBlock *fRawSDB;// MAK: 12 April, 2005 : not present in mc
  const RawDaqSnarlHeader* fRawDSH; // added due to above
  RawVarcErrorInTfBlock *fRawVErrB;

  static RawChannelId kCerenkovChannel1;
  static RawChannelId kCerenkovChannel2;
  static RawChannelId kCerenkovChannel3;
  static RawChannelId kTriggerPMTChannel;
  static RawChannelId kTofADCChannel0;
  static RawChannelId kTofADCChannel1;
  static RawChannelId kTofADCChannel2;
  static RawChannelId kTofTimeStampChannel;
  static std::vector<RawChannelId> kTriggerORs; // VARC LEMO inputs
  static UInt_t kGoodTriggerORBits; // VARC LEMO inputs
  static const Int_t kNTIMEBLOCKS;
  static std::set<Int_t> fVarcErrSet;//a set to hold timeblocks with varc errors 
  
  void SetDataPointers(CandContext &cx);  //set the above pointers
  void FillVarcErrs();                    //fill the set of varc errors
  Bool_t IsCosmic(const RawChannelId&) const;
  
ClassDef(AlgCalDetSI, 4)     //macro to rootify

};

inline Bool_t AlgCalDetSI::IsCosmic(const RawChannelId& id) const
{
  // returns kTRUE if the channel corresponds to a cosmic counter
  // returns kFALSE otherwise

  Bool_t rvalue = kFALSE;
  Int_t varc = id.GetVarcId();
  Int_t vmm = id.GetVmm();
  Int_t vaadc = id.GetVaAdcSel();
  //if we find a Cosmic Counter hit, event fails cut, return false
  if((varc==0||varc==1)&&vmm==5&&vaadc==0){
    rvalue = kTRUE;
  }
  return rvalue;
}

#endif // ALGCALDETSI_H





