#include "StHbtMaker/Infrastructure/StHbtTrack.hh"
#include "StHbtMaker/Infrastructure/StHbtTypes.hh"
#include "StHbtMaker/Cut/trackCutMonitor_P_vs_Msqr.h"
#include <cstdio>
#include "StLorentzVector.hh"

#ifdef __ROOT__ 
ClassImp(trackCutMonitor_P_vs_Msqr)
#endif

//___________________________
trackCutMonitor_P_vs_Msqr::trackCutMonitor_P_vs_Msqr(){

  mHisto = new TH2D("P_vs_Msqr","Momentum vs ToF m^{2};P (GeV/c);m^{2} ((GeV/c^{2})^{2})",
		     400,-2.,2.,130,-0.1,1.2);
  mCharge = 0;
}

//___________________________
trackCutMonitor_P_vs_Msqr::trackCutMonitor_P_vs_Msqr(int charge){

  mHisto = new TH2D("P_vs_Msqr","Momentum vs TOF m^{2};P (GeV/c);m^{2} ((GeV/c^{2})^{2})",
		     400,-2.,2.,130,-0.1,1.2);
  mCharge = charge;
}

//___________________________
trackCutMonitor_P_vs_Msqr::trackCutMonitor_P_vs_Msqr(int charge, const char* TitCutMoni){

  mHisto = new TH2D(TitCutMoni,"Momentum vs TOF m^{2};P (GeV/c);m^{2} ((GeV/c^{2})^{2})",
		     400,-2.,2.,130,-0.1,1.2);
  mCharge = charge;
}

//___________________________
trackCutMonitor_P_vs_Msqr::trackCutMonitor_P_vs_Msqr(int charge, const char* TitCutMoni, 
						     const char* title,
						     int nbins1, double min1, double max1,
						     int nbins2, double min2, double max2){

  mHisto= new TH2D(TitCutMoni,title,nbins1,min1,max1,nbins2,min2,max2);
  mCharge = charge;
  mHisto->SetDirectory(0);
}

//____________________________
trackCutMonitor_P_vs_Msqr::trackCutMonitor_P_vs_Msqr(const trackCutMonitor_P_vs_Msqr& cutMoni) {
  
  mCharge = cutMoni.mCharge;
  mHisto = new TH2D(*(cutMoni.mHisto));
  mHisto->SetDirectory(0);
}

//____________________________
trackCutMonitor_P_vs_Msqr::trackCutMonitor_P_vs_Msqr(const trackCutMonitor_P_vs_Msqr& cutMoni,
						     int charge) {
  mCharge = charge;
  mHisto = new TH2D(*(cutMoni.mHisto));
  mHisto->SetDirectory(0);
}

//_____________________________
trackCutMonitor_P_vs_Msqr::~trackCutMonitor_P_vs_Msqr(){
  delete mHisto;
  cout<<"Track monitor has been destroyed!"<<endl;
}

//_____________________________
void  trackCutMonitor_P_vs_Msqr::Fill(const StHbtTrack* track){

#ifdef STHBTDEBUG
  cout<<"trackCutMonitor_P_vs_Msqr::Fill"<<endl
      <<"Track charge   = "<<track->Charge()<<endl
      <<"Track momentum = "<<track->P()<<endl
      <<"Track dEdx     = "<<track->dEdx()<<endl
      <<endl;
#endif
  
  double Mom = sqrt(track->P().x() * track->P().x() +
		    track->P().y() * track->P().y() +
		    track->P().z() * track->P().z());

  
  if ( (track->Charge() == mCharge || mCharge == 0) &&
       track->TofMassSqr()>=-0.1 )
    mHisto->Fill((Mom*track->Charge()), track->TofMassSqr());
}

//_____________________________________
void trackCutMonitor_P_vs_Msqr::Finish(){
  cout << " entries in histogram : " << mHisto->Integral() << endl;
}

//_____________________________________
TH2D* trackCutMonitor_P_vs_Msqr::Histo(){
  return mHisto;
}

//_____________________________________
StHbtString trackCutMonitor_P_vs_Msqr::Report(){
  string Stemp;
  char Ctemp[100];
  sprintf(Ctemp," trackCutMonitor_P_vs_Msqr");
  Stemp=Ctemp;
  StHbtString returnThis = Stemp;
  return returnThis;
}
