#ifndef SIMINCLHH
#define SIMINCLHH

#include "../rec/rec2/Params.hh"
#include "rootutil.hh"

#include "TrackSegment.hh"
#include "SimDet.hh"
#include "SimStringDet.hh"
#include "SimHit.hh"


#include "TObject.h"
#include "TStopwatch.h"
#include "DetectorModel.hh"

class G4RunManager;
class G4UImanager;
class AAPrimaryGeneratorAction;

#include "TH3D.h"

#ifndef __CLING__
#include "AAPrimaryGeneratorAction.hh"
#endif

//extern SimStringDet g_det;



/*! The Sim class forms the main interface to the simulation */

struct Sim
{  
  SimDet*            det;
  DetectorModelBase* watermodel;

  G4RunManager* runManager; //!
  AAPrimaryGeneratorAction* aa_primary_generator_action;
  G4UImanager* UI;  //!

  // counters 
  int n_track_seg;
  int n_total_photons;
  bool track_photons;

  TH1F* h_beta;
  TH1F* h_energy;
  TH1F* h_type;
  TH1F* h_emission;

  // collections
  vector<Trk>    tracks;
  vector<SimHit> simhits;
  vector<TH3D>   histograms; 

  // state of the simulation
  Trk* current_track; // set in simulate_geant

  Sim();

  bool _init_geant();

  void init_histograms( TH3D& proto , int noms ) 
  { 
    for (int i =0; i< noms; i++ )
    {
      proto.SetName( ("pdf"+str(i)).c_str() );
      histograms.push_back( proto );
      
    }



  }


  double simulate_geant( vector< Trk >& mc_input_tracks,
			                   bool remember_track_segments = true);


  // --- steering the behaviour and verbosity ---

  bool record_tracks;    
  bool verbose_tracks;  // print track summary after each trackingaction
  bool fill_histograms; 

  void init_settings()
  {
    record_tracks   = true;
    verbose_tracks  = false;
    fill_histograms = true;
    n_total_photons = 0;
    n_track_seg     = 0;

    h_beta   = new TH1F("h_beta","h_beta;beta;total track length"  , 50 , 0.7, 1.0 );
    h_energy = new TH1F("h_energy","h_energy;log (energy/GeV);total track length", 50, -5, 5 );
    h_type   = new TH1F("h_type","h_type;type,total track length", 200, -100,100);
    
  }
 
  void record_hit( const Photon& photon, int iseg  , const Intersect& intersec ) 
  {

    print ("dummy record hit");

  }


  void record_hit( const Photon& photon, const PhotonSegment& photon_segment , const Intersect& intersec ) 
  { 
    TIMESCOPE
    // nb: the omid is not unique, but can be duplicated for oms on the same D,z
 
    static Hit h;
    const double l = (photon_segment.pos - intersec.pos).len();
		h.t   = photon_segment.t + l / photon.group_velocity;
    h.pos = intersec.pos;
    h.dir = -photon_segment.dir;
  
    Paramst pars( *current_track, h );

    print ("omid ",intersec.om->id, histograms.size() );

    histograms[intersec.om->id].Fill( pars.theta, pars.phi, pars.t );


    // print ("got a hit", intersec.om->id, intersec.om->radius, intersec.pos , photon_segment.pos , photon_segment.dir );
    // print ("dir len", photon_segment.dir.len() );
    // print ("dom pos", intersec.om->pos);
    // print ("hit pos", intersec.pos );
  }

  virtual ~Sim() {}
  ClassDef(Sim,1)
};


extern Sim* g_sim;



#endif