/***

      StFemtoDstPicoRun11Maker

  COMMENT: This class converts PicoDst for Run11 to FemtoDst
  VERSION: 1.0
  AUTHOR: Nikita Ermakov
  E-MAIL: coffe92@gmail.com

  CHANGE LOG:

    Version 1.0: First version

***/

#ifndef STFEMTODSTPICORUN11MAKER_HH
#define STFEMTODSTPICORUN11MAKER_HH

#include "StMaker.h"
#include "StThreeVector.hh"
#include "StThreeVectorF.hh"
#include "StPhysicalHelixD.hh"

#include "StPicoDstMakerRun11/StPicoDst.h"
#include "StPicoDstMakerRun11/StPicoDstMaker.h"
#include "StPicoDstMakerRun11/StPicoEvent.h"
#include "StPicoDstMakerRun11/StPicoTrack.h"
#include "StPicoDstMakerRun11/StPicoBTofPidTraits.h"

#include "StFemtoEvent.h"
#include "StFemtoTrack.h"

#include "StBTofHeader.h"
#include "TBranch.h"
#include "StPhysicalHelixD.hh"
#include "StDcaGeometry.h"

#include <TString.h>
#include <TFile.h>
#include <TLorentzVector.h>
#include <TTree.h>
#include <TH1.h>
#include <TH2.h>
#include <TRandom3.h>

#include <vector>
#include <iostream>

#ifndef St_NO_NAMESPACES
using namespace std;
#endif

//
// Predefinisions
//
class StFemtoEvent;
class StFemtoTrack;

class StPicoDst;
class StPicoEvent;
class StPicoTrack;



class StFemtoDstPicoRun11Maker : public StMaker
{
 public:
  StFemtoDstPicoRun11Maker(StPicoDstMaker *picoMaker,
                           const Char_t *oFileName);
  ~StFemtoDstPicoRun11Maker();

  void Clear(Option_t *option = "");
  Int_t Init();
  Int_t Make();
  Int_t Finish();

  void SetCompressionLevel(Int_t comp)        { mCompression = comp; }

  //
  // Setters
  //
  // global parameters of the maker
  void SetPicoDstMaker(StPicoDstMaker *maker)     { mPicoMaker = maker; }

  // event
  void SetVtxZCut(const float& lo, const Float_t& hi)   { mPrimVtxZ[0]=lo; mPrimVtxZ[1]=hi; }
  void SetVtxRCut(const float& lo, const Float_t& hi)   { mPrimVtxR[0]=lo; mPrimVtxR[1]=hi; }
  void SetVtxShift(const float& xShift, const Float_t& yShift) { mPrimVtxXShift=xShift; mPrimVtxYShift=yShift; }
  void SetVtxVpdVzDiffCut(const float& lo, const Float_t& hi)  { mPrimVtxVpdVzDiff[0]=lo; mPrimVtxVpdVzDiff[1]=hi;}

  // track
  void SetParticleMomentum(const float& lo, const Float_t& hi) { mTrackMomentum[0]=lo; mTrackMomentum[1]=hi; }
  void SetTrackDcaGlobal(const float& lo, const Float_t& hi) { mTrackDcaGlobal[0]=lo; mTrackDcaGlobal[1]=hi; }
  void SetTrackNHitsFit(const Int_t& lo, const Int_t& hi) { mTrackNHitsFit[0]=lo; mTrackNHitsFit[1]=hi; }
  void SetTrackEta(const float& lo, const Float_t& hi)  { mTrackEta[0]=lo; mTrackEta[1]=hi; }

  //
  // Exclusion cuts
  //
  void SetRemovePions(bool &remove)    { mRemovePions = remove; }
  void SetRemoveKaons(bool &remove)    { mRemoveKaons = remove; }
  void SetRemoveProtons(bool &remove)  { mRemoveProtons = remove; }
  void SetRemoveMomThreshold(float &t) { mPthresh = t; }
  void SetTpcPionNSigma(float &lo, Float_t &hi)    { mTpcPionNSigma[0] = lo; mTpcPionNSigma[1] = hi; }
  void SetTofPionMassSqr(float &lo, Float_t &hi)   { mTofPionMassSqr[0] = lo; mTofPionMassSqr[1] = hi; }
  void SetTpcKaonNSigma(float &lo, Float_t &hi)    { mTpcKaonNSigma[0] = lo; mTpcKaonNSigma[1] = hi; }
  void SetTofKaonMassSqr(float &lo, Float_t &hi)   { mTofKaonMassSqr[0] = lo; mTofKaonMassSqr[1] = hi; }
  void SetTpcProtonNSigma(float &lo, Float_t &hi)  { mTpcProtonNSigma[0] = lo; mTpcProtonNSigma[1] = hi; }
  void SetTofProtonMassSqr(float &lo, Float_t &hi) { mTofProtonMassSqr[0] = lo; mTofProtonMassSqr[1] = hi; }

 private:

  const Char_t *mOutFileName;
  TFile *mOutFile;
  TTree *mTree;
  Int_t mCompression;                  // see ROOT documentation for more info

  Int_t  mNBytes;                      // number of writed bytes to the mTree

  //
  // Global counters
  //
  Int_t  mNEventsIn;
  Int_t  mNEventsPassed;

  bool isGlobalTrack;
  //
  // Event cuts
  //
  float mPrimVtxZ[2];
  float mPrimVtxR[2];
  float mPrimVtxVpdVzDiff[2];
  float mPrimVtxXShift;
  float mPrimVtxYShift;
  //
  // Single-particle cuts
  //
  float    mTrackMomentum[2];
  float    mTrackDcaGlobal[2];
  UShort_t mTrackNHitsFit[2];
  float    mTrackEta[2];
  //
  //Cuts for exclusion
  //
  bool   mRemovePions;
  bool   mRemoveKaons;
  bool   mRemoveProtons;
  float  mPthresh;
  float  mTpcPionNSigma[2];
  float  mTofPionMassSqr[2];
  float  mTpcKaonNSigma[2];
  float  mTofKaonMassSqr[2];
  float  mTpcProtonNSigma[2];
  float  mTofProtonMassSqr[2];
  //
  // Global variables
  //
  StPicoDstMaker       *mPicoMaker;
  StPicoTrack          *mPicoTrack;
  StThreeVectorF        mPrimP, mGlobP, mDca;
  float                 mMassSqr;
  bool                  mIsTofTrack;
  TRandom3             *mRandom;
  unsigned int          mTmap0, mTmap1;

  //
  // Femto event structure
  //
  StFemtoEvent *mFemtoEvent;

  //
  // Internal class methods
  //
  //
  bool AcceptPrimVtx(const StThreeVectorF &vtxPos, float vpdVz);
  bool AcceptTrack();
  bool AcceptRefMult2();
  bool AcceptDcaGlobal(float dcaToPrimVtx);
  void CalcDca(const StPhysicalHelixD &helix,
               const StThreeVectorF   &vtxPos);
  void TopologyMapCalc();

  //
  // Pion, Kaon and Proton selection
  //
  bool IsTpcPion();
  bool IsTofPion();
  bool IsTpcKaon();
  bool IsTofKaon();
  bool IsTpcProton();
  bool IsTofProton();

  ClassDef(StFemtoDstPicoRun11Maker, 1)
};

#endif
