// -*- C++ -*-

//
// Pibero Djawotho <pibero@indiana.edu>
// Indiana University
// 4 October 2006
//

#ifndef StUpsilonTreeMaker_h
#define StUpsilonTreeMaker_h

// ROOT
class TFile;
class TTree;
class TH1F;
class TH2F;

// STAR
class StMuEvent;
class StMuTrack;
class StEmcCluster;
class StEmcDecoder;
class StEmcGeom;
class StBemcTables;
class StEmcPosition;
class StSPtrVecEmcCluster;

// Local
class EmcHit;
class EmcCluster;
class Track;
class Event;
//class struct_myevent;
class TNtuple;

// ROOT
#include "TVector3.h"
#include "TH1.h"
#include "TH2.h"
#include "TH3.h"

// STAR
#include "StMaker.h"
#include "StEnumerations.h"
#include "L2Result.h"//dd
#include <vector>
#include <map>
#include "SurroundingTowers.h"
//#include "StHeavyTrigger/Upsilon.hh"

using std::vector;

class StUpsilonTreeMaker : public StMaker {
public:
  StUpsilonTreeMaker(const char* name = "upsilon_tree_maker") : StMaker(name) {}
  ~StUpsilonTreeMaker() {}

  void Clear(Option_t* option = "");
  int Init();
  int InitRun(int runNumber);
  int Make();
  int Finish();

private:
  static const double BEMC_RADIUS;
  static const double BEMC_OUTER_RADIUS;
  static const double BSMDE_RADIUS;
  static const double BSMDP_RADIUS;
  static const double CTB_RADIUS;

  bool accept(StMuEvent* event) const;
  bool accept(StMuTrack* track) const;
  bool accept(Track* track) const;
  bool acceptSoftId(StMuTrack* track);//ddas
  bool acceptIndex(StMuTrack* track) const;//ddas
  bool checkForValidTrackPair(Track* tr1,Track* tr2);
  void makeEmcHits();
  void makeEmcClusters();
  int makeSmdClusters();
  int makeSmdEtaClusters(const StSPtrVecEmcCluster& clusters);
  int makeSmdPhiClusters(const StSPtrVecEmcCluster& clusters);
  void makeCtbHits();
  void makeTracks();

  void project_to_tower_fill_his();
  void fill_histograms();
  void fill_event_histograms();
  void fill_ntuples_by_TreeReader();
  void definetree();
  void clear_vector();
  void clear_tree_array();
  void scan_tracks();
  void fill_tree_par();
  bool track_project_to_tower(StMuTrack* track);
  void fill_his_tower_eff();

  void getFirstEmcHit(Track* track);
  void getLastEmcHit(Track* track);
  void getEmcCluster(Track* track);
  void getSmdEtaCluster(Track* track);
  void getSmdPhiCluster(Track* track);
  void getCtbHit(Track* track);
  void makeParticles();
  void transferMuTrackToTrackMultimap(StMuTrack* trk1,Track* trk2);
  EmcHit* getEmcHit(int id) const;

  TFile* mFile;
//  TTree* mTree;
  Event* mEvent;
 
  TH2F *his_dedx_vs_p_all_primary;
  TH2F *his_dedx_vs_p_track_with_emc;
  TH2F *his_eta_vs_nHitFit_all_primary;
  TH2F *his_eta_vs_nHitFit_track_with_emc;
  TH1F* mRefMult;
  TH1F* mZvertex;
  TH2F* RefMult_vs_vz;
  TH1F* good_RefMult;
  TH2F *delta_eta_vs_vz_highpt;
  TH2F *delta_eta_vs_vz_highpt_after_correction;
  TH3F *tower_eta_tower_phi_run;
  TH3F *track_eta_track_phi_run;
  TH3F *track_proj_eta_track_phi_run;
  TH3F *match_eta_match_phi_run;
  TH3F *match_eta_match_phi_run_check;
  TH3F *my_own_match_eta_match_phi_run;
//  TH3F *my_own_match_eta_match_phi_run2;
//  TH2F *match_id_correlation_geomID_vs_towerID;
//  TH2F *check_matched_entries;
  TH1F* tower_fired_softid;
  TH1F* track_proj_softid;
  TH1F* matched_softid;




  TNtuple* mUps;

//  TH1F *hvertex1;
//  TH1F *hvertex2;
  TTree* myTree;
  int m_Run;
  int m_evtID;
  int m_refmult;
  float m_vz;  
  float m_mag_field;  
  float m_vpdVertex;  
  float m_zdcVertex;  
  float m_bbcVertex;  

  int m_NPrimaryTracks;
  float m_pt[5000];
  float m_p[5000];
  float m_eta[5000];
  float m_phi[5000];
  float m_project_eta[5000];
  float m_project_phi[5000];
  float m_dedx[5000];
  float m_nSigE[5000];
  int m_q[5000];
  int m_nFitPoints[5000];
  int m_project_softid[5000][3][3];  // eta(-1,1) phi (-1,1) unit

  int m_NTowers;
  float m_adc[5000];
  float m_tower_eta[5000];
  float m_tower_phi[5000];
  float m_tower_E[5000];
  int m_softid[5000];
  int m_NNeighbors[5000];
  int m_neighbor[5000][8];

	float project_eta,project_phi;
	int project_softid[3][3];

	int m_project_softid_center[5000];
	float m_project_tower_E[5000];
	float m_project_cluster_E[5000];
	float m_project_distance[5000];
	float m_px[5000];
	float m_py[5000];
	float m_pz[5000];
	int m_refmult_EFTPC,m_refmult_WFTPC;

  StEmcDecoder* mEmcDecoder;
  StEmcGeom* mEmcGeom;
  StBemcTables* mBemcTables;
  StEmcPosition* mEmcPosition;
  int mStatus[4801];
  //vector<int> mVecSoftIdTrig;//ddas
  vector<SurroundingTowers> mVecSoftIdTrig;//for storing all candidate Ids
  multimap<Track*,int> mTrackTowerMap;
  multimap<StMuTrack*,int> mStMuTrackTowerMap;//stores mutrack and towerId
  vector<pair <int,int> > mTrigtowers; //this is to access the StHeavyTrigger

  ClassDef(StUpsilonTreeMaker, 1);
};

#endif
