/***************************************************************************
 *
 * $Id: QinvCorrFctnKt.cxx,v 1.3 2003/09/02 17:58:20 perev Exp $
 *
 * Author: Mercedes Lopez Noriega, OSU, mercedes@pacific.mps.ohio-state.edu
 ***************************************************************************
 *
 * Description: part of STAR HBT Framework: StHbtMaker package
 *   QinvCFs for a given number of CFs (nCFs) between ktLo and ktHi 
 *   where kt is the four-momentum of the pair 
 *
 ***************************************************************************
 *
 * $Log: QinvCorrFctnKt.cxx,v $
 * Revision 1.3  2003/09/02 17:58:20  perev
 * gcc 3.2 updates + WarnOff
 *
 * Revision 1.2  2003/01/31 19:21:09  magestro
 * Cleared up simple compiler warnings on i386_linux24
 *
 * Revision 1.1  2002/05/17 14:26:21  mercedes
 * N Qinv CFs (kt bins) between ktmin and ktmax
 *
 *
 **************************************************************************/

#include "StHbtMaker/CorrFctn/QinvCorrFctnKt.h"
//#include "StHbtMaker/Infrastructure/StHbtHisto.hh"
#include <cstdio>
#include <sstream>

#ifdef __ROOT__ 
ClassImp(QinvCorrFctnKt)
#endif

//____________________________
QinvCorrFctnKt::QinvCorrFctnKt(char* title,
			       const int& nbins, const float& QinvLo, 
			       const float& QinvHi,
			       const int& nCFs, const float& KtLo, 
			       const float& KtHi){
  mNumberCFs = nCFs;
  mKtMin = KtLo;
  mKtMax = KtHi;
  mDeltaKt = (mKtMax-mKtMin)/mNumberCFs;

  // set up numerators
  mNumerator = new StHbt1DHisto[mNumberCFs];

  // set up denominators
  mDenominator = new StHbt1DHisto[mNumberCFs];

  stringstream TitNum,TitDen,TitCurrent;
  TitNum.str("");TitDen.str("");
  TitNum<<title<<"_Num";
  TitDen<<title<<"_Den";

  for (int i=0; i<mNumberCFs; i++) {
    
    TitCurrent.str("");
    TitCurrent<<TitNum.str().c_str()<<"_bin_"<<i;
    mNumerator[i].SetName(TitCurrent.str().c_str());
    mNumerator[i].SetTitle(TitCurrent.str().c_str());
    mNumerator[i].SetBins(nbins,QinvLo,QinvHi);
    mNumerator[i].SetDirectory(0);
    mNumerator[i].Sumw2();
    
    TitCurrent.str("");
    TitCurrent<<TitDen.str().c_str()<<"_bin_"<<i;
    mDenominator[i].SetName(TitCurrent.str().c_str());
    mDenominator[i].SetTitle(TitCurrent.str().c_str());
    mDenominator[i].SetBins(nbins,QinvLo,QinvHi);
    mDenominator[i].SetDirectory(0);
    mDenominator[i].Sumw2();
  }
}

//_________________
QinvCorrFctnKt::~QinvCorrFctnKt(){
  delete [] mNumerator;
  delete [] mDenominator;
}

//_________________
void QinvCorrFctnKt::WriteOutHistos() {

  for(int i=0; i<mNumberCFs; i++) {
    mNumerator[i].Write();
    mDenominator[i].Write();
  }
}

//_________________
void QinvCorrFctnKt::Finish(){
  /* Ratio creation might be here */
}

//_________________
StHbtString QinvCorrFctnKt::Report(){
  int mNumeratorEntries=0;
  int mDenominatorEntries=0;

  for (int i=0; i<mNumberCFs; i++){
    mNumeratorEntries += (int)mNumerator[i].GetEntries();
    mDenominatorEntries += (int)mDenominator[i].GetEntries();
  }
  string stemp = "Qinv Correlation Function Report:\n";
  char ctemp[100];
  sprintf(ctemp,"Number of entries in all numerators:\t%i\n",mNumeratorEntries);
  stemp += ctemp;
  sprintf(ctemp,"Number of entries in all denominators:\t%i\n",mDenominatorEntries);
  stemp += ctemp;
  StHbtString returnThis = stemp;
  return returnThis;
}

//____________________________
void QinvCorrFctnKt::AddRealPair(const StHbtPair* pair){
  int mIndex = (int)(((fabs(pair->kT())-mKtMin)/mDeltaKt));
  if ((mIndex>=0)&&(mIndex<mNumberCFs)){
    mNumerator[mIndex].Fill(fabs(pair->qInv()),1.0);
  }
}

//____________________________
void QinvCorrFctnKt::AddMixedPair(const StHbtPair* pair){
  int  mIndex = (int)(((fabs(pair->kT())-mKtMin)/mDeltaKt));
  if ((mIndex>=0)&&(mIndex<mNumberCFs)){
    mDenominator[mIndex].Fill(fabs(pair->qInv()),1.0);
  }
}


