#include "TFile.h"
#include "TNtuple.h"

#include "StEventTypes.h"
#include "StMuDSTMaker/COMMON/StMuTypes.hh"

#include "StElectronMaker.h"

ClassImp(StElectronMaker);

int StElectronMaker::Init()
{
  mFile = new TFile("electron.root", "recreate");
  mTuple = new TNtuple("electron", "Electron Tuple", "charge:p:pt:pz:eta:phi:dEdx:nHitsFit:dcaGlobal");
  return StMaker::Init();
}

int StElectronMaker::Make()
{
  if (!GetDataSet("MuDst")) {
    LOG_WARN << "No MuDst" << endm;
    return kStWarn;
  }

  if (!accept(StMuDst::event())) return kStOk;

  TIter next(StMuDst::primaryTracks());
  while (StMuTrack* track = (StMuTrack*)next()) {
    if (!accept(track)) continue;
    float tuple[] = {
      track->charge(),
      track->p().mag(),
      track->pt(),
      track->p().z(),
      track->eta(),
      track->phi(),
      track->dEdx() / keV,
      track->nHitsFit(),
      track->dcaGlobal().mag()
    };
    mTuple->Fill(tuple);
  }

  return kStOk;
}

int StElectronMaker::Finish()
{
  mFile->Write();
  mFile->Close();
  return kStOk;
}

//bool StElectronMaker::accept(StMuEvent* event) const
//{
//return (event->triggerIdCollection().nominal().isTrigger(200601) ||
//          event->triggerIdCollection().nominal().isTrigger(200602));
//}

bool StElectronMaker::accept(StMuEvent* event) const
{
return (event->triggerIdCollection().nominal().isTrigger(201601) );
  }

bool StElectronMaker::accept(StMuTrack* track) const
{
  return (track->flag() > 0 &&
          track->flag() != 701 &&
          track->flag() != 801 &&
          track->flag() != 901 &&
          track->pt() > 0.2 &&
          track->p().mag() < 100 &&
          track->nHitsFit() > 20 &&
          track->nHitsFit() / (float)track->nHitsPoss() > 0.52);
}
