//
// Pibero Djawotho <pibero@indiana.edu>
// Indiana University
// 4 October 2006
//

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

// Local
#include "Track.h"
#include "Particle.h"

ClassImp(Particle);

Particle::Particle()
{
  mL2Sim=0;
  mCharge = 0;
  mCosTheta = 0;
}

Particle::Particle(const Particle& particle)
{
  mL2Sim = particle.mL2Sim;
  mCharge = particle.mCharge;
  mCosTheta = particle.mCosTheta;
  mMomentum = particle.mMomentum;
}

Particle::Particle(Track* track1, Track* track2)
{
  mCharge = track1->charge() + track2->charge();
  TLorentzVector p1;
  TLorentzVector p2;
  p1.SetVectM(track1->momentum(), StElectron::instance()->mass());
  p2.SetVectM(track2->momentum(), StElectron::instance()->mass());
  mMomentum = p1 + p2;
  mCosTheta = track1->momentum().Unit() * track2->momentum().Unit();
  mDaughters.Add(track1);
  mDaughters.Add(track2);
}

bool Particle::level2() const
{
  Track* track1 = (Track*)mDaughters.First();
  Track* track2 = (Track*)mDaughters.Last ();
  EmcCluster* cluster1 = track1->emcCluster();
  EmcCluster* cluster2 = track2->emcCluster();
  if (!cluster1) return false;
  if (!cluster2) return false;
  if (cluster1->energy() < cluster2->energy()) swap(cluster1, cluster2);
//  if (cluster1->energy() < 4.0) return false;
//  if (cluster2->energy() < 2.5) return false;
  if (cluster1->energy() < 4.5) return false;
  if (cluster2->energy() < 3.0) return false;
  float cosTheta = cluster1->position().Unit() * cluster2->position().Unit();
//  if (cosTheta > 0.5) return false;
  if (cosTheta > 0.) return false;
  float invMass = sqrt(2 * cluster1->energy() * cluster2->energy() * (1 - cosTheta));
//  if (invMass < 6 || invMass > 15) return false;
  if (invMass < 6.5 || invMass > 25) return false;
  return true;
}
