#ifndef DIGITIZER_H
#define DIGITIZER_H
#include <iostream>
#include <fstream>


#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "Riostream.h"
#include "TFile.h"
#include "TDirectory.h"
#include "TTree.h"
#include "TLeafI.h"
#include "TH1.h"
#include "TH2.h"
#include "TMath.h"
#include "TSQLServer.h"
//
#include "CRC.h"

class Digitizer{


 protected:

    // in and output data members

    TTree* fhBookTree;
    ofstream fOutputfile;


    static const int fPSCUbuffer = 8;
    static const int fTRIGGERbuffer = 153; //(bytes)
    static const int fTOFbuffer = 276;     //(bytes)
    static const int fACbuffer = 128;
    static const int fNDbuffer = 4;
    static const int fPADbuffer = 64;  // changed from 32!
    static const int fCALObuffer = 10000;
    static const float fCALOGeV2MIPratio = 0.0001059994;

    static const int fDummybuffer = 64;

    //  dimensions of vectors and buffer, parameter for tracker
    static const int fTRACKbuffer = 50000; // to be optimized
    static const int fNviews = 12;
    static const int fNstrips_view = 3072;
    static const int fNladder=3;
    static const int fNstrips_ladder = 1024;
    static const int fCutzop= 4;
    static const int fCutclu= 7;
    static const int fNclst = 2;
    
    
    static const float fAvePedex = 1200.; // to be adjusted
    static const float fSigmaPedex = 1.;  // to be adjusted
    static const float fAveSigmax = 4.3;  // to be adjusted
    static const float fSigmaSigmax =0.1;  // to be adjusted
    static const float fAvePedey = 2800.; // to be adjusted
    static const float fSigmaPedey = 1.;  // to be adjusted
    static const float fAveSigmay = 8.;  // to be adjusted
    static const float fSigmaSigmay =0.1;  // to be adjusted
    static const float fSigmaCommon = 25.; // to be adjusted
    static const float fMipCor = 1.; // to be adjusted
  
  
    //  end dimensions of vectors and buffer, parameter for tracker

    UShort_t fDataPSCU[fPSCUbuffer];
    UInt_t fCounter;
    UInt_t fOBT;

    UShort_t fDataPadding[fPADbuffer];
    UInt_t fPadding;

    UChar_t fDataTrigger[fTRIGGERbuffer];

    UChar_t fDataTof[fTOFbuffer];

    UShort_t fDataAC[fACbuffer];
    UShort_t fDataND[fACbuffer];
    UShort_t fDataDummy[fDummybuffer];

    UShort_t fDataCALO[fCALObuffer];
    UShort_t fSecCalo[4];
    UShort_t fSecCALOLength[4];
    UShort_t fCALOlength;
    UShort_t fModCalo;
    Float_t fCalomip[2][22][96]; // ADC to MIP conversion values
    Float_t fcalped[4][11][96]; // pedestals
    Float_t fcalgood[4][11][96]; // used or not in the common noise calculation
    Float_t fcalthr[4][11][6]; // thresholds
    Float_t fcalrms[4][11][96]; // rms
    Float_t fcalbase[4][11][6]; // baselines (common noise) during calibration
    Float_t fcalvar[4][11][6]; // variance
    Float_t fcstwerr[4]; // status word
    Float_t fcperror[4]; // unpacking error flag
    UInt_t fGivenCaloCalib;

    char* fFilename;
    UInt_t fLen;


    //  vectors, buffer, and variables for tracker
    UShort_t fDataTrack[fTRACKbuffer];
    UInt_t   fTracklength; //  Actual length of buffer 
    Float_t  fPedeTrack[fNviews][fNstrips_view];
    Float_t  fSigmaTrack[fNviews][fNstrips_view];
    // end vectors, buffer, and variables for tracker 



    //
    // db variables
    //
    TSQLServer *fDbc;
    // Declaration of leave types
    Int_t           Irun;
    Int_t           Ievnt;
    Int_t           Ipa;
    Float_t         X0;
    Float_t         Y0;
    Float_t         Z0;
    Float_t         Theta;
    Float_t         Phi;
    Float_t         P0;
    Int_t           Nthtof;
    UChar_t         Ipltof[100];   //[Nthtof]
    UChar_t         Ipaddle[100];   //[Nthtof]
    UChar_t         Ipartof[100];   //[Nthtof]
    Float_t         Xintof[100];   //[Nthtof]
    Float_t         Yintof[100];   //[Nthtof]
    Float_t         Zintof[100];   //[Nthtof]
    Float_t         Xouttof[100];   //[Nthtof]
    Float_t         Youttof[100];   //[Nthtof]
    Float_t         Zouttof[100];   //[Nthtof]
    Float_t         Ereltof[100];   //[Nthtof]
    Float_t         Timetof[100];   //[Nthtof]
    Float_t         Pathtof[100];   //[Nthtof]
    Float_t         P0tof[100];   //[Nthtof]
    Int_t           Nthcat;
    UChar_t         Iparcat[50];   //[Nthcat]
    UChar_t         Icat[50];   //[Nthcat]
    Float_t         Xincat[50];   //[Nthcat]
    Float_t         Yincat[50];   //[Nthcat]
    Float_t         Zincat[50];   //[Nthcat]
    Float_t         Xoutcat[50];   //[Nthcat]
    Float_t         Youtcat[50];   //[Nthcat]
    Float_t         Zoutcat[50];   //[Nthcat]
    Float_t         Erelcat[50];   //[Nthcat]
    Float_t         Timecat[50];   //[Nthcat]
    Float_t         Pathcat[50];   //[Nthcat]
    Float_t         P0cat[50];   //[Nthcat]
    Int_t           Nthcas;
    UChar_t         Iparcas[50];   //[Nthcas]
    UChar_t         Icas[50];   //[Nthcas]
    Float_t         Xincas[50];   //[Nthcas]
    Float_t         Yincas[50];   //[Nthcas]
    Float_t         Zincas[50];   //[Nthcas]
    Float_t         Xoutcas[50];   //[Nthcas]
    Float_t         Youtcas[50];   //[Nthcas]
    Float_t         Zoutcas[50];   //[Nthcas]
    Float_t         Erelcas[50];   //[Nthcas]
    Float_t         Timecas[50];   //[Nthcas]
    Float_t         Pathcas[50];   //[Nthcas]
    Float_t         P0cas[50];   //[Nthcas]
    Int_t           Nthspe;
    UChar_t         Iparspe[100];   //[Nthspe]
    UChar_t         Itrpb[100];   //[Nthspe]
    UChar_t         Itrsl[100];   //[Nthspe]
    UChar_t         Itspa[100];   //[Nthspe]
    Float_t         Xinspe[100];   //[Nthspe]
    Float_t         Yinspe[100];   //[Nthspe]
    Float_t         Zinspe[100];   //[Nthspe]
    Float_t         Xoutspe[100];   //[Nthspe]
    Float_t         Youtspe[100];   //[Nthspe]
    Float_t         Zoutspe[100];   //[Nthspe]
    Float_t         Xavspe[100];   //[Nthspe]
    Float_t         Yavspe[100];   //[Nthspe]
    Float_t         Zavspe[100];   //[Nthspe]
    Float_t         Erelspe[100];   //[Nthspe]
    Float_t         Pathspe[100];   //[Nthspe]
    Float_t         P0spe[100];   //[Nthspe]
    UChar_t         Nxmult[100];   //[Nthspe]
    UChar_t         Nymult[100];   //[Nthspe]
    Int_t           Nstrpx;
    UChar_t         Npstripx[1000];   //[Nstrpx]
    UChar_t         Ntstripx[1000];   //[Nstrpx]
    UShort_t        Istripx[1000];   //[Nstrpx]
    //UInt_t        Istripx[1000];   //[Nstrpx]
    Float_t         Qstripx[1000];   //[Nstrpx]
    Float_t         Xstripx[1000];   //[Nstrpx]
    Int_t           Nstrpy;
    UChar_t         Npstripy[1000];   //[Nstrpy]
    UChar_t         Ntstripy[1000];   //[Nstrpy]
    UShort_t        Istripy[1000];   //[Nstrpy]
    //UInt_t        Istripy[1000];   //[Nstrpy]
    Float_t         Qstripy[1000];   //[Nstrpy]
    Float_t         Ystripy[1000];   //[Nstrpy]
    Int_t           Nthcali;
    UChar_t         Icaplane[4224];   //[Nthcali]
    UChar_t         Icastrip[4224];   //[Nthcali]
    Int_t           Icamod[4224];   //[Nthcali]
    Float_t         Enestrip[4224];   //[Nthcali]
    Int_t           Nthcal;
    UChar_t         Icapl[1000];   //[Nthcal]
    UChar_t         Icasi[1000];   //[Nthcal]
    UChar_t         Icast[1000];   //[Nthcal]
    Float_t         Xincal[1000];   //[Nthcal]
    Float_t         Yincal[1000];   //[Nthcal]
    Float_t         Zincal[1000];   //[Nthcal]
    Float_t         Erelcal[1000];   //[Nthcal]
    Int_t           Nthnd;
    UChar_t         Itubend[200];   //[Nthnd]
    UChar_t         Iparnd[200];   //[Nthnd]
    Float_t         Xinnd[200];   //[Nthnd]
    Float_t         Yinnd[200];   //[Nthnd]
    Float_t         Zinnd[200];   //[Nthnd]
    Float_t         Xoutnd[200];   //[Nthnd]
    Float_t         Youtnd[200];   //[Nthnd]
    Float_t         Zoutnd[200];   //[Nthnd]
    Float_t         Erelnd[200];   //[Nthnd]
    Float_t         Timend[200];   //[Nthnd]
    Float_t         Pathnd[200];   //[Nthnd]
    Float_t         P0nd[200];   //[Nthnd]
    Int_t           Nthcard;
    UChar_t         Iparcard[100];   //[Nthcard]
    UChar_t         Icard[100];   //[Nthcard]
    Float_t         Xincard[100];   //[Nthcard]
    Float_t         Yincard[100];   //[Nthcard]
    Float_t         Zincard[100];   //[Nthcard]
    Float_t         Xoutcard[100];   //[Nthcard]
    Float_t         Youtcard[100];   //[Nthcard]
    Float_t         Zoutcard[100];   //[Nthcard]
    Float_t         Erelcard[100];   //[Nthcard]
    Float_t         Timecard[100];   //[Nthcard]
    Float_t         Pathcard[100];   //[Nthcard]
    Float_t         P0card[100];   //[Nthcard]

    // List of branches
    TBranch        *b_Irun;   //!
    TBranch        *b_Ievnt;   //!
    TBranch        *b_Ipa;   //!
    TBranch        *b_X0;   //!
    TBranch        *b_Y0;   //!
    TBranch        *b_Z0;   //!
    TBranch        *b_Theta;   //!
    TBranch        *b_Phi;   //!
    TBranch        *b_P0;   //!
    TBranch        *b_Nthtof;   //!
    TBranch        *b_Ipltof;   //!
    TBranch        *b_Ipaddle;   //!
    TBranch        *b_Ipartof;   //!
    TBranch        *b_Xintof;   //!
    TBranch        *b_Yintof;   //!
    TBranch        *b_Zintof;   //!
    TBranch        *b_Xouttof;   //!
    TBranch        *b_Youttof;   //!
    TBranch        *b_Zouttof;   //!
    TBranch        *b_Ereltof;   //!
    TBranch        *b_Timetof;   //!
    TBranch        *b_Pathtof;   //!
    TBranch        *b_P0tof;   //!
    TBranch        *b_Nthcat;   //!
    TBranch        *b_Iparcat;   //!
    TBranch        *b_Icat;   //!
    TBranch        *b_Xincat;   //!
    TBranch        *b_Yincat;   //!
    TBranch        *b_Zincat;   //!
    TBranch        *b_Xoutcat;   //!
    TBranch        *b_Youtcat;   //!
    TBranch        *b_Zoutcat;   //!
    TBranch        *b_Erelcat;   //!
    TBranch        *b_Timecat;   //!
    TBranch        *b_Pathcat;   //!
    TBranch        *b_P0cat;   //!
    TBranch        *b_Nthcas;   //!
    TBranch        *b_Iparcas;   //!
    TBranch        *b_Icas;   //!
    TBranch        *b_Xincas;   //!
    TBranch        *b_Yincas;   //!
    TBranch        *b_Zincas;   //!
    TBranch        *b_Xoutcas;   //!
    TBranch        *b_Youtcas;   //!
    TBranch        *b_Zoutcas;   //!
    TBranch        *b_Erelcas;   //!
    TBranch        *b_Timecas;   //!
    TBranch        *b_Pathcas;   //!
    TBranch        *b_P0cas;   //!
    TBranch        *b_Nthspe;   //!
    TBranch        *b_Iparspe;   //!
    TBranch        *b_Itrpb;   //!
    TBranch        *b_Itrsl;   //!
    TBranch        *b_Itspa;   //!
    TBranch        *b_Xinspe;   //!
    TBranch        *b_Yinspe;   //!
    TBranch        *b_Zinspe;   //!
    TBranch        *b_Xoutspe;   //!
    TBranch        *b_Youtspe;   //!
    TBranch        *b_Zoutspe;   //!
    TBranch        *b_Xavspe;   //!
    TBranch        *b_Yavspe;   //!
    TBranch        *b_Zavspe;   //!
    TBranch        *b_Erelspe;   //!
    TBranch        *b_Pathspe;   //!
    TBranch        *b_P0spe;   //!
    TBranch        *b_Nxmult;   //!
    TBranch        *b_Nymult;   //!
    TBranch        *b_Nstrpx;   //!
    TBranch        *b_Npstripx;   //!
    TBranch        *b_Ntstripx;   //!
    TBranch        *b_Istripx;   //!
    TBranch        *b_Qstripx;   //!
    TBranch        *b_Xstripx;   //!
    TBranch        *b_Nstrpy;   //!
    TBranch        *b_Npstripy;   //!
    TBranch        *b_Ntstripy;   //!
    TBranch        *b_Istripy;   //!
    TBranch        *b_Qstripy;   //!
    TBranch        *b_Ystripy;   //!
    TBranch        *b_Nthcali;   //!
    TBranch        *b_Icaplane;   //!
    TBranch        *b_Icastrip;   //!
    TBranch        *b_Icamod;   //!
    TBranch        *b_Enestrip;   //!
    TBranch        *b_Nthcal;   //!
    TBranch        *b_Icapl;   //!
    TBranch        *b_Icasi;   //!
    TBranch        *b_Icast;   //!
    TBranch        *b_Xincal;   //!
    TBranch        *b_Yincal;   //!
    TBranch        *b_Zincal;   //!
    TBranch        *b_Erelcal;   //!
    TBranch        *b_Nthnd;   //!
    TBranch        *b_Itubend;   //!
    TBranch        *b_Iparnd;   //!
    TBranch        *b_Xinnd;   //!
    TBranch        *b_Yinnd;   //!
    TBranch        *b_Zinnd;   //!
    TBranch        *b_Xoutnd;   //!
    TBranch        *b_Youtnd;   //!
    TBranch        *b_Zoutnd;   //!
    TBranch        *b_Erelnd;   //!
    TBranch        *b_Timend;   //!
    TBranch        *b_Pathnd;   //!
    TBranch        *b_P0nd;   //!
    TBranch        *b_Nthcard;   //!
    TBranch        *b_Iparcard;   //!
    TBranch        *b_Icard;   //!
    TBranch        *b_Xincard;   //!
    TBranch        *b_Yincard;   //!
    TBranch        *b_Zincard;   //!
    TBranch        *b_Xoutcard;   //!
    TBranch        *b_Youtcard;   //!
    TBranch        *b_Zoutcard;   //!
    TBranch        *b_Erelcard;   //!
    TBranch        *b_Timecard;   //!
    TBranch        *b_Pathcard;   //!
    TBranch        *b_P0card;   //!
    //

 public: 

    Digitizer(TTree *tree, char* &file_raw); // constructor

    void Close();
 
    void SetTree(TTree *tree) {fhBookTree = tree;};

  
    void Loop();  // does the looping over events
 


    void DigitizePSCU(UInt_t, UChar_t);  // digitizes the CPU header
    void AddPadding();
    void DigitizeTRIGGER();  // digitizes the trigger
    Int_t DigitizeTOF();  // digitizes the tof
    void DigitizeAC();  // digitizes the anti-counter
    void DigitizeND();  // digitizes the ND
    void DigitizeCALO();  // digitizes the calorimeter
    void DigitizeCALOCALIB();  // digitizes the calorimeter calibration
    void DigitizeCALORAW();  // digitizes the calorimeter
    void DigitizeCALOCOMPRESS();  // digitizes the calorimeter
    void DigitizeCALOFULL();  // digitizes the calorimeter

    Float_t GetCALOen(Int_t sec, Int_t plane, Int_t strip); // retrieve calorimeter strip energy from the simulation
    void ClearCaloCalib(Int_t s); // clear calorimeter calibration variables for section s
    Int_t CaloLoadCalib(Int_t s,TString fcalname, UInt_t calibno); // load calorimeter calibration variables for section s from file fcalname entry calibno
    void CaloLoadCalib();

    void DigitizeDummy(); // digitizes the dummy detector

    // ToF methods:
    UChar_t EvaluateCrcTof(UChar_t*);
    void Paddle2Pmt(Int_t, Int_t, Int_t*, Int_t*);
    //void Paddle2Pmt(Int_t, Int_t, Int_t &pmtleft, Int_t &pmtright);
    UChar_t Bin2GrayTof(UChar_t, UChar_t);

    UShort_t* GetDataAC(){return(fDataAC);}; // retrieve AC data (not used right now)

    UShort_t* GetDataCALO(){return(fDataCALO);}; // retrieve AC data (not used right now)

    //  methods for Tracker
    void DigitizeTrack(); // digitizes the Tracker data
    void DigitizeTrackCalib(Int_t ii); // digitizes the Tracker calibration (x view or y view)
    void WriteTrackCalib();  // writes Tracker calibration to file
    void ClearTrackCalib(); // clear tracker calibration 
    void LoadTrackCalib(); // load in memmory tracker calibration 
    void CompressTrackData(Float_t AdcTrack[fNviews][fNstrips_view]); // Compress and Digitize data of one Ladder in turn
    void LoadMipCor(); // Load the strip calibration in memory 
    Float_t SaturationTrack(Float_t ADC); // Saturation simulation
    //  end methods for Tracker

    void WriteData(); // writes data to file
    void ReadData();

    //

};

#endif /* DIGITIZER_H */



