#ifndef HitOutputManager_h
#define HitOutputManager_h 1

#include "G4ThreeVector.hh"

#include <fstream>
#include <string>

class TFile;
class TTree;
class TH1D;
class TH2D;

class HitOutputManager
{
  public:
    static HitOutputManager& Instance();

    void Open();
    void Close();
    void SetIncludeElectrons(bool includeElectrons);
    bool GetIncludeElectrons() const;
    bool ShouldRecordParticle(const std::string& particleType) const;
    void RecordHit(int eventId,
                   const std::string& particleType,
                   double kineticEnergy,
                   double energyDeposit,
                   double wireDeltaESinTheta,
                   const std::string& detectorType,
                   int detectorId,
                   int anodeId,
                   int cathodeId,
                   double anodeEnergy,
                   double cathodeEnergy,
                   double wireDeltaE,
                   const std::string& volumeName,
                   const G4ThreeVector& position,
                   const G4ThreeVector& vertexPosition);

  private:
    HitOutputManager();
    ~HitOutputManager();

    HitOutputManager(const HitOutputManager&) = delete;
    HitOutputManager& operator=(const HitOutputManager&) = delete;

    void ResetBuffers();

    TFile* fRootFile;
    TTree* fTree;
    std::ofstream fTextFile;
    int fEventId;
    int fDetectorId;
    int fAnodeId;
    int fCathodeId;
    double fKineticEnergy;
    double fEnergyDeposit;
    double fWireDeltaESinTheta;
    double fAnodeEnergy;
    double fCathodeEnergy;
    double fWireDeltaE;
    double fX;
    double fY;
    double fZ;
    double fVertexX;
    double fVertexY;
    double fVertexZ;
    char fParticleType[64];
    char fDetectorType[32];
    char fVolumeName[32];
    TH1D* fKineticEnergyHist;
    TH1D* fEnergyDepositHist;
    TH1D* fWireDeltaEHist;
    TH2D* fWireDeltaESinThetaVsDetEHist;
    TH1D* fHitZHist;
    TH1D* fVertexZHist;
    TH1D* fDetectorIdHist;
    TH2D* fHitXYHist;
    TH2D* fVertexXYHist;
    bool fIncludeElectrons;
};

#endif
