#include <TROOT.h>
#include <TSystem.h>
#include <TChain.h>
#include <TFile.h>
#include <TString.h>
#include <iostream>

#define MONITORS

//Constants for analyses
//Event information
const int mPrimVertZbins = 4;                //Number of z bins for analysis
const double mPrimVertZ[2] = {-30., 30.};    //Redefine values of the vertex Z for each run [cm]
const double mPrimVertR = 2.;                //Distance in the XY plane [cm]
const double mPrimVertVpdVz[2] = {-5., 5.};  //VpdZ - TPCZ difference
const double mPrimVertXShift = 0.;
const double mPrimVertYShift = 0.;
const int    mCentBins = 9;
const Bool_t mUseZdcCorrection = true;

//Track information
const double mChargedKaonMass = 0.493677;
const double mChargedPionMass = 0.13957018;
const int mPosCharge = +1;
const int mNegCharge = -1;
const int mTrackType = 1; 

const int mTrackNHits[2] = {15, 50};          //Number of track hits
const double mTrackNHitsFit[2] = {15, 50};     //Number of track hits used in fit
const double mTrackDCA[2] = {0., 2.};      //DCA of the track and PV [cm]
const double mTrackASplitVal = 0.52;
const double mTrackMom[2] = {0.15, 1.55};     //General momentum of the track [GeV/c]
const double mTrackTransvMom[2] = {0.15, 1.55};  //General transverse momentum of the track [GeV/c]
const double mTrackEta[2] = { -1., 1.};

const short mDetectorSelection = 4;

const double mTrackTpcMom[2] = {0.15, 0.55};  //Track momentum for TPC idnetification [GeV/c]
const double mnSigmaElectron[2] = {-1e9, 1e9};
const double mnSigmaPion[2] = {-1e9., 1e9};
const double mnSigmaKaon[2] = {-2., 2.};
const double mnSigmaProton[2] = {-1e9, 1e9};

const double mTrackTofMom[2] = {0.15, 1.55};   //Track momentum for ToF identification [GeV/c]
const double mPionTofMsqr[2] = {-0.02, 0.062}; //Based on p+p data
const double mKaonTofMsqr[2] = {0.20, 0.32};    //Based on p+p data

const double mTrackTnTMom[2] = {0.15, 1.55};
const double mTnTNSigmaElectron[2] = {-1e9, 1e9};
const double mTnTNSigmaPion[2] = {-1e9., 1e9};
const double mTnTNSigmaKaon[2] = {-3., 3.};
const double mTnTNSigmaProton[2] = {-1e9, 1e9};

const double mTTTMomThresh = 0.45; // 0-TPC, 1-ToF, 2-TPC+ToF, 3-ToF||TPC, 4-TPC&&TOF(P>Pthr)||TPC(P<Pthr)

//Pair information
const double mFmrCut[2] = {-1.1, 0.1};        //Not more than 10% of merged rows
const double mSplitLevelCut[2] = {-0.5, 0.6}; //Splitting level from paper. StHbtPair::quality()
const double mPairKt[2] = {0.,1.5};           //Pair transverse momentum cut
const double mPairQinv[2] = {0., 0.8};         //Pair relative momentum
const double mAveSep[2] = {5., 5000.};         //Average separation of tracks within TPC

//Histograms values
const int mPairKtBins = 24;
const double mPairKtVal[2] = {0.05, 1.25};
const int mQinvBins = 40;                      //10 MeV/c^2 bin width
const double mQinvVal[2] = {0., 0.4};          //Redefine for each analysis

const Char_t *defaultInFile = "st_physics.list";
const Char_t *defaultOutFile = "oExpKaon_MuDST_1D.root";

//_________________
void expKaonHBT_MuDST_1D(const Char_t *inFileList = defaultInFile,
			 const Char_t *oFileName = defaultOutFile) {

  std::cout << "*********************************" << std::endl
	    << "*     expKaonHBT_auau200_1D     *" << std::endl
	    << "*              Start            *" << std::endl
	    << "*********************************" << std::endl << endl;

  //Load libraries
  std::cout << "Loading libraries..." << std::endl;
  gROOT->LoadMacro("$STAR/StRoot/StMuDSTMaker/COMMON/macros/loadSharedLibraries.C");
  loadSharedLibraries();
  gSystem->Load("libMinuit");
  gSystem->Load("StRefMultCorr");
  gSystem->Load("StFlowMaker");   //Should be placed before HBT
  gSystem->Load("StHbtMaker");
  gSystem->Load("StarClassLibrary");
  gSystem->Load("libgsl");
  gSystem->Load("libgslcblas");

  std::cout << "Libraries have been successfully loaded" << std::endl;

  //Create chain
  StChain* mChain = new StChain("StChain");
  mChain->SetDebug(0);
  StMuDebug::setLevel(0);

  //Set muDstMaker
  Int_t nMaxFileToRead = 1e9;
  std::cout << Form("Try to read file: %s", inFileList) << std::endl;
  StMuDstMaker *mMuDstMaker = 
    new StMuDstMaker(0, 0, "", inFileList, "MuDst", nMaxFileToRead);

  mMuDstMaker->SetStatus("*",0);
  mMuDstMaker->SetStatus("Event*",1);
  mMuDstMaker->SetStatus("MuEvent*",1);
  mMuDstMaker->SetStatus("PrimaryVertices*",1);
  mMuDstMaker->SetStatus("PrimaryTracks*",1);
  mMuDstMaker->SetStatus("GlobalTracks*",1);
  mMuDstMaker->SetStatus("BTofHeader*",1);

  TChain *mCurrentTree = mMuDstMaker->chain();
  Long64_t mNEvents = mCurrentTree->GetEntries();
  std::cout << Form("Number of events in TTree: %d", mNEvents) << std::endl;

  //Set StHbtMaker and StHbtManager
  StHbtMaker *mHbtMaker = new StHbtMaker();
  mHbtMaker->SetDebug(0);
  StHbtManager *mHbtManager = mHbtMaker->HbtManager();

  //Set StHbtReader
  gregsStHbtMuDstReader* mHbtMuDstMakerReader = new gregsStHbtMuDstReader(mMuDstMaker);
  mHbtMuDstMakerReader->setTrackType(1);
  mHbtMuDstMakerReader->setReadTracks(true);
  mHbtMuDstMakerReader->setReadV0s(false);
  mHbtMuDstMakerReader->AddTriggerId(350003);          //mb-protected
  mHbtMuDstMakerReader->AddTriggerId(350013);          //mb-protected
  mHbtMuDstMakerReader->AddTriggerId(350023);          //mb-protected
  mHbtMuDstMakerReader->AddTriggerId(350033);          //mb-protected
  mHbtMuDstMakerReader->AddTriggerId(350043);          //mb-protected
  mHbtMuDstMakerReader->SetProcessAllVertices(false);
  mHbtManager->SetEventReader(mHbtMuDstMakerReader);

  //Set basic event cut
  gregsEventCut *mBasicEventCut = new gregsEventCut();
  mBasicEventCut->SetCollisionType(1);     //0-pp, 1-AuAu
  mBasicEventCut->SetEventMult(0, 1000);
  mBasicEventCut->SetVertZPos(mPrimVertZ[0], mPrimVertZ[1]);
  mBasicEventCut->SetVzVpdVzDiff(mPrimVertVpdVz[0],mPrimVertVpdVz[1]);
  mBasicEventCut->SetVertRPos(mPrimVertR);
  mBasicEventCut->SetCentralityDefinition(mCentBins);
  mBasicEventCut->SetEventCentralityBins(0, 8);  //For 9 centrality bins
  mBasicEventCut->SetZdcRefMultCorrection(mUseZdcCorrection);

  //Set basic kaon single-particle cut
  gregsSimpleTrackCut *mBasicTrackCut = new gregsSimpleTrackCut();
  mBasicTrackCut->SetCharge(mPosCharge);
  mBasicTrackCut->SetMass(mChargedKaonMass);
  mBasicTrackCut->SetType(mTrackType);
  mBasicTrackCut->SetNHits(mTrackNHits[0], mTrackNHits[1]);
  mBasicTrackCut->SetNHitsFit(mTrackNHitsFit[0], mTrackNHitsFit[1]);
  mBasicTrackCut->SetAntiSplit(mTrackASplitVal);
  mBasicTrackCut->SetEta(mTrackEta[0], mTrackEta[1]);
  mBasicTrackCut->SetDCA(mTrackDCA[0], mTrackDCA[1]);
  mBasicTrackCut->SetDCAGlobal(mTrackDCA[0], mTrackDCA[1]);
  mBasicTrackCut->SetP(mTrackMom[0], mTrackMom[1]);

  mBasicTrackCut->SetDetectorSelection(mDetectorSelection);

  mBasicTrackCut->SetTpcMomentum(mTrackTpcMom[0], mTrackTpcMom[1]);
  mBasicTrackCut->SetNSigmaElectron(mnSigmaElectron[0],mnSigmaElectron[1]);
  mBasicTrackCut->SetNSigmaPion(mnSigmaPion[0], mnSigmaPion[1]);
  mBasicTrackCut->SetNSigmaKaon(mnSigmaKaon[0], mnSigmaKaon[1]);
  mBasicTrackCut->SetNSigmaProton(mnSigmaProton[0], mnSigmaProton[1]);

  mBasicTrackCut->SetTofMassSqr(mKaonTofMsqr[0],mKaonTofMsqr[1]);
  mBasicTrackCut->SetTofMomentum(mTrackTofMom[0], mTrackTofMom[1]);

  mBasicTrackCut->SetTnTMomentum(mTrackTnTMom[0], mTrackTnTMom[1]);
  mBasicTrackCut->SetTnTNSigmaElectron(mTnTNSigmaElectron[0], mTnTNSigmaElectron[1]);
  mBasicTrackCut->SetTnTNSigmaPion(mTnTNSigmaPion[0], mTnTNSigmaPion[1]);
  mBasicTrackCut->SetTnTNSigmaKaon(mTnTNSigmaKaon[0], mTnTNSigmaKaon[1]);
  mBasicTrackCut->SetTnTNSigmaProton(mTnTNSigmaProton[0], mTnTNSigmaProton[1]);

  mBasicTrackCut->SetTTTMomentumThresh(mTTTMomThresh);

  //Set basic kaon pair cut
  gregsTrackPairCut *mBasicPairCut = new gregsTrackPairCut();
  mBasicPairCut->SetFracOfMergedRow(mFmrCut[0],mFmrCut[1]);
  mBasicPairCut->SetQuality(mSplitLevelCut[0],mSplitLevelCut[1]);
  mBasicPairCut->SetKt(mPairKt[0],mPairKt[1]);
  mBasicPairCut->SetQinv(mPairQinv[0],mPairQinv[1]);
  mBasicPairCut->SetAverageSeparation(mAveSep[0],mAveSep[1]);

 Int_t mLocalCharge, mMultBins, mMultLow, mMultHi, mEvents2Mix, mCurrentPid;
 Int_t mCentLo, mCentHi;

  ////////////////////////////////////////////
  //                                        //
  //       Like-sign meson analysis         //
  //                                        //
  ////////////////////////////////////////////

  //The multiplicity analysis matrix is 2x5x10, where
  //the first element corresponds to the particle charge: (pos, neg),
  //the second one corresponds to the PID selection: 
  //0-TPC, 1-ToF, 2-TPC+ToF, 3-ToF||TPC, 4-TPC&&TOF(P>Pthr)||TPC(P<Pthr),
  //and the third one corresponds to the RefMult intervals as listed below:
  //Centralitiy: 9 centrality bins and integrated one.

  //StHbtAnalysis, cut, CF and monitor declarations
  StHbtVertexMultAnalysis *mKaonMultAnalysis[2][5][10]; 

  //Cuts
  gregsEventCut *mKaonEventCut[2][5][9];
  gregsSimpleTrackCut *mKaonTrackCut[2][5][9];
  gregsTrackPairCut *mKaonPairCut[2][5][9];

  //CorrFctns
  QinvCorrFctnKt *mKaonQinvMixKtCF[2][5][9];

#ifdef MONITORS
  //Cut monitors
  fxtEventCutMonitor* mFxtKaonEventCutMonitorPass[2][5][9];
  fxtTrackCutMonitor* mFxtKaonTrackCutMonitorPass[2][5][9];
  fxtPairCutMonitor* mFxtKaonPairCutMonitorPass[2][5][9];
  fxtEventCutMonitor* mFxtKaonEventCutMonitorFail[2][5][9];
  fxtTrackCutMonitor* mFxtKaonTrackCutMonitorFail[2][5][9];
  fxtPairCutMonitor* mFxtKaonPairCutMonitorFail[2][5][9];
#endif

  //Force to work with TPC+TOF pid!!! See event loop
  for(Int_t iCharge = 1; iCharge < 2; iCharge++) {
    for(Int_t iPid = 2; iPid < 3; iPid++) {
      for(Int_t iMult = 9; iMult < 10; iMult++) {

	switch(iCharge) {
	case 0:  mLocalCharge = mPosCharge;    break;
	case 1:  mLocalCharge = mNegCharge;    break;
	default: mLocalCharge = mPosCharge;    break;
	};

	switch(iPid) {
	case 0:  mCurrentPid = 0; break; //TPC only
	case 1:  mCurrentPid = 1; break; //TOF only
	case 2:  mCurrentPid = 2; break; //TPC+TOF
	case 3:  mCurrentPid = 3; break; //TPC||TOF
	case 4:  mCurrentPid = 4; break; //TPC (p<pthresh) && TOF+TPC (p>pthresh)
	default: mCurrentPid = 4; break;
	};

        switch(iMult) {
        case 8: mCentLo=8; mCentHi=8; mMultLow=466; mMultHi=800;  mMultBins=2; mEvents2Mix=3; break;
        case 7: mCentLo=7; mCentHi=7; mMultLow=396; mMultHi=465;  mMultBins=2; mEvents2Mix=3; break;
	case 6: mCentLo=6; mCentHi=6; mMultLow=281; mMultHi=395;  mMultBins=2; mEvents2Mix=3; break;
        case 5: mCentLo=5; mCentHi=5; mMultLow=193; mMultHi=280;  mMultBins=2; mEvents2Mix=3; break;
        case 4: mCentLo=4; mCentHi=4; mMultLow=125; mMultHi=192;  mMultBins=2;  mEvents2Mix=3; break;
        case 3: mCentLo=3; mCentHi=3; mMultLow=76;  mMultHi=124;  mMultBins=2;  mEvents2Mix=3; break;
	case 2: mCentLo=2; mCentHi=2; mMultLow=43;  mMultHi=75;   mMultBins=2;  mEvents2Mix=5; break;
        case 1: mCentLo=1; mCentHi=1; mMultLow=22;  mMultHi=42;   mMultBins=2;  mEvents2Mix=10; break;
        case 0: mCentLo=0; mCentHi=0; mMultLow=10;  mMultHi=21;   mMultBins=2;  mEvents2Mix=10; break;
	default: mCentLo=0; mCentHi=8; mMultLow=10;  mMultHi=800;  mMultBins=10; mEvents2Mix=5; break;
        };

	/////////////////////////////////////
	//                                 //
	//      Like-sign pion analysis    //
	//                                 //
	/////////////////////////////////////

	//Create analysis
	mKaonMultAnalysis[iCharge][iPid][iMult] = new StHbtVertexMultAnalysis(mPrimVertZbins,
									      mPrimVertZ[0],
									      mPrimVertZ[1],
									      mMultBins,
									      mMultLow,
									      mMultHi);

	//Create event cut
	mKaonEventCut[iCharge][iPid][iMult] = new gregsEventCut(*mBasicEventCut);
	mKaonEventCut[iCharge][iPid][iMult]->SetCentralityDefinition(mCentBins);
	mKaonEventCut[iCharge][iPid][iMult]->SetEventCentralityBins(mCentLo, mCentHi);
	mKaonEventCut[iCharge][iPid][iMult]->SetZdcRefMultCorrection(mUseZdcCorrection);

#ifdef MONITORS
	mFxtKaonEventCutMonitorPass[iCharge][iPid][iMult] = 
	  new fxtEventCutMonitor("eventKaonPass", Form("_%d_%d_%d",iCharge,iPid,iMult));
	mFxtKaonEventCutMonitorFail[iCharge][iPid][iMult] = 
	  new fxtEventCutMonitor("eventKaonFail", Form("_%d_%d_%d",iCharge,iPid,iMult));
	mKaonEventCut[iCharge][iPid][iMult]->AddCutMonitor(mFxtKaonEventCutMonitorPass[iCharge][iPid][iMult],
							   mFxtKaonEventCutMonitorFail[iCharge][iPid][iMult]);
#endif
	mKaonMultAnalysis[iCharge][iPid][iMult]->SetEventCut(mKaonEventCut[iCharge][iPid][iMult]);


	//Create single-track cuts and add them to the analysis
	mKaonTrackCut[iCharge][iPid][iMult] = new gregsSimpleTrackCut(*mBasicTrackCut);

#ifdef MONITORS
	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult] = 
	  new fxtTrackCutMonitor(Form("_trackKaonPass_%d_%d_%d_",iCharge,iPid,iMult), mChargedKaonMass);
	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult] = 
	  new fxtTrackCutMonitor(Form("_trackKaonFail_%d_%d_%d_",iCharge,iPid,iMult), mChargedKaonMass);
	mKaonTrackCut[iCharge][iPid][iMult]->AddCutMonitor(mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult],
							   mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]);
#endif
	mKaonMultAnalysis[iCharge][iPid][iMult]->SetFirstParticleCut(mKaonTrackCut[iCharge][iPid][iMult]);
	mKaonMultAnalysis[iCharge][iPid][iMult]->SetSecondParticleCut(mKaonTrackCut[iCharge][iPid][iMult]);

	//Create pair cut
	mKaonPairCut[iCharge][iPid][iMult] = new gregsTrackPairCut(*mBasicPairCut);
#ifdef MONITORS
	mFxtKaonPairCutMonitorPass[iCharge][iPid][iMult] = 
	  new fxtPairCutMonitor(Form("_pairKaonPass_%d_%d_%d_",iCharge,iPid,iMult));
	mFxtKaonPairCutMonitorPass[iCharge][iPid][iMult]->SetParticleMass(mChargedKaonMass);
	mFxtKaonPairCutMonitorFail[iCharge][iPid][iMult] = 
	  new fxtPairCutMonitor(Form("_pairKaonFail_%d_%d_%d_",iCharge,iPid,iMult));
	mFxtKaonPairCutMonitorFail[iCharge][iPid][iMult]->SetParticleMass(mChargedKaonMass);
	mKaonPairCut[iCharge][iPid][iMult]->AddCutMonitor(mFxtKaonPairCutMonitorPass[iCharge][iPid][iMult],
							  mFxtKaonPairCutMonitorFail[iCharge][iPid][iMult]);
#endif
	mKaonMultAnalysis[iCharge][iPid][iMult]->SetPairCut(mKaonPairCut[iCharge][iPid][iMult]);

	//Correlation function initialization
	mKaonQinvMixKtCF[iCharge][iPid][iMult] = 
	  new QinvCorrFctnKt(Form("hKaonQinvMixKt_%d_%d_%d",iCharge,iPid,iMult),
			     mQinvBins, mQinvVal[0], mQinvVal[1],
			     mPairKtBins, mPairKtVal[0], mPairKtVal[1]);

	mKaonMultAnalysis[iCharge][iPid][iMult]->AddCorrFctn(mKaonQinvMixKtCF[iCharge][iPid][iMult]);

	//Set number of events to mix
	mKaonMultAnalysis[iCharge][iPid][iMult]->SetNumEventsToMix(mEvents2Mix);

	//Add analysis to the manager
	mHbtManager->AddAnalysis(mKaonMultAnalysis[iCharge][iPid][iMult]);
	
      } //for(Int_t iMult=0; iMult<9; iMult++)
    } //for(Int_t iPid=0; iPid<5; iPid++)
  } //for(Int_t iCharge=0; iCharge<2; iCharge++)

  //StChain initialization
  mChain->Init();

  //Start processing
  Int_t iRet = 0;
  Int_t mNEventsProcessed = 0;
  Float_t mPercentCounter = 0.05;
  Float_t mProgress = 0.;

  unsigned int mSubCounter = 1;              // time
  time_t mStartTime, mStopTime, mDiffTime;   // time
  float mFrac;                               // time
  mStartTime = time(0);                      // time

  //mNEvents = 15;
  for(Int_t iEvent=0; iEvent<mNEvents; iEvent++) {

    mProgress = (Float_t)iEvent / (Float_t)mNEvents;
    mNEventsProcessed++;
    
    if(mProgress >= mPercentCounter) {
      mPercentCounter += 0.05;
      mStopTime = time(0);
      mDiffTime = difftime(mStopTime, mStartTime);
      mFrac = (float)mDiffTime*(float)(mNEvents - iEvent)/(float)iEvent;
      mSubCounter++;
      std::cout << Form("Processing progress: %4.2f%%. Time left: %.1f sec", mProgress*100., mFrac)
		<< std::endl;
    }

    mChain->Clear();
    iRet = mChain->Make(iEvent);
    if(iRet!=0) {
      std::cout << "expKaonHBT_pp200_1D[ERROR] - iRet: " << iRet << std::endl;
      break;
    }
  }

  mChain->Finish();

  //Create output file
  TFile *oFile = new TFile(oFileName, "RECREATE");

  //Write hbt histograms to the file
  std::cout << "Writing histograms to the output file...";

#ifdef MONITORS
  for(Int_t iCharge = 1; iCharge < 2; iCharge++) {
    for(Int_t iPid = 2; iPid < 3; iPid++) {
      for(Int_t iMult = 9; iMult < 10; iMult++) {
        //Kaon analysis monitors
        mFxtKaonEventCutMonitorPass[iCharge][iPid][iMult]->VertexZ()->Write();
	mFxtKaonEventCutMonitorPass[iCharge][iPid][iMult]->VertexYvsVertexX()->Write();
	mFxtKaonEventCutMonitorPass[iCharge][iPid][iMult]->RefMult()->Write();
	mFxtKaonEventCutMonitorPass[iCharge][iPid][iMult]->VpdVzDiff()->Write();
	mFxtKaonEventCutMonitorFail[iCharge][iPid][iMult]->VertexZ()->Write();
	mFxtKaonEventCutMonitorFail[iCharge][iPid][iMult]->VertexYvsVertexX()->Write();
	mFxtKaonEventCutMonitorFail[iCharge][iPid][iMult]->RefMult()->Write();
	mFxtKaonEventCutMonitorFail[iCharge][iPid][iMult]->VpdVzDiff()->Write();

	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult]->DCAGlobal()->Write();
	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult]->Nhits()->Write();
	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult]->P()->Write();
	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult]->Pt()->Write();
	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult]->PtVsNsigmaPion()->Write();
	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult]->PtVsNsigmaKaon()->Write();
	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult]->PvsDedx()->Write();
	//	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult]->PtvsDedx()->Write();
	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult]->Rapidity()->Write();
	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult]->PseudoRapidity()->Write();
	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult]->PtVsMassSqr()->Write();
	mFxtKaonTrackCutMonitorPass[iCharge][iPid][iMult]->PvsInvBeta()->Write();
	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]->DCAGlobal()->Write();
	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]->Nhits()->Write();
	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]->P()->Write();
	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]->Pt()->Write();
	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]->PtVsNsigmaPion()->Write();
	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]->PtVsNsigmaKaon()->Write();
	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]->PvsDedx()->Write();
	//	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]->PtvsDedx()->Write();
	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]->Rapidity()->Write();
	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]->PseudoRapidity()->Write();
	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]->PtVsMassSqr()->Write();
	mFxtKaonTrackCutMonitorFail[iCharge][iPid][iMult]->PvsInvBeta()->Write();

	mFxtKaonPairCutMonitorPass[iCharge][iPid][iMult]->Kt()->Write();
	//	mFxtKaonPairCutMonitorPass[iCharge][iPid][iMult]->Minv()->Write();
	//	mFxtKaonPairCutMonitorPass[iCharge][iPid][iMult]->Mt()->Write();
	mFxtKaonPairCutMonitorPass[iCharge][iPid][iMult]->FMRvsQinv()->Write();
	mFxtKaonPairCutMonitorPass[iCharge][iPid][iMult]->SLvsQinv()->Write();
	mFxtKaonPairCutMonitorPass[iCharge][iPid][iMult]->AveSepVsQinv()->Write();
	mFxtKaonPairCutMonitorFail[iCharge][iPid][iMult]->Kt()->Write();
	//	mFxtKaonPairCutMonitorFail[iCharge][iPid][iMult]->Minv()->Write();
	//	mFxtKaonPairCutMonitorFail[iCharge][iPid][iMult]->Mt()->Write();
	mFxtKaonPairCutMonitorFail[iCharge][iPid][iMult]->FMRvsQinv()->Write();
	mFxtKaonPairCutMonitorFail[iCharge][iPid][iMult]->SLvsQinv()->Write();
	mFxtKaonPairCutMonitorFail[iCharge][iPid][iMult]->AveSepVsQinv()->Write();

      } //for(Int_t iMult=0; iMult<9; iMult++)
    } //for(Int_t iPid=0; iPid<5; iPid++)
  } //for(Int_t iCharge=0; iCharge<2; iCharge++)
#endif

  //TPC+TOF identification only! See the loop
  for(Int_t iCharge = 1; iCharge<2; iCharge++) {
    for(Int_t iPid = 2; iPid<3; iPid++) {
      for(Int_t iMult = 9; iMult < 10; iMult++) {
	for(Int_t iKt = 0; iKt < mPairKtBins; iKt++) {

	  //Kaon analysis
	  mKaonQinvMixKtCF[iCharge][iPid][iMult]->Numerator(iKt)->Write();
	  mKaonQinvMixKtCF[iCharge][iPid][iMult]->Denominator(iKt)->Write();
	} //for(Int_t iKt=0; iKt<mPairKtBins; iKt++)
      } //for(Int_t iMult=0; iMult<10; iMult++)
    } //for(Int_t iPid=0; iPid<5; iPid++)
  } //for(Int_t iCharge=0; iCharge<2; iCharge++

  std::cout << "\t[DONE]" << std::endl;

  //Write the output file and close it
  std::cout << "Saving the output file...";
  oFile->Write();
  oFile->Close();
  std::cout << "\t[DONE]" << std::endl;

  delete mChain;

  std::cout << "*********************************" << std::endl
	    << "*      expKaonHBT_auau200_1D    *" << std::endl
	    << "*              Finish           *" << std::endl
	    << "*********************************" << std::endl << std::endl;
}
