#include "PtStatistic.h"
#include "Event.h"
#include <iostream>
#include <math.h>

PtStatistic::PtStatistic( int id ):
 select( id ),
 nPart( 0 ),
 sum ( 0.0 ),
 sqr ( 0.0 ),
 mean( 0.0 ),
 rms ( 0.0 ) {
}


PtStatistic::~PtStatistic() {
}


void PtStatistic::add( const Event& ev ) {

  // loop over particles
  int i;
  int np = ev.nParticles();
  const Event::part_ptr* particles = ev.particles();
  for ( i = 0; i < np; ++i ) {
    // get particle
    Event::part_ptr p = particles[i];
    // check particle type (hadron/muon)
    if ( p->muonId != select ) continue;
    // update sums
    float pt = p->pt;
    ++nPart;
    sum += pt;
    sqr += pt * pt;
  }

  return;

}


void PtStatistic::compute() {
  // compute mean and rms
  mean =         sum * 1.0 / nPart;
  float diff = ( sqr * 1.0 / nPart ) - ( mean * mean );
  if ( diff < 0.0 ) rms = 0.0;
  else              rms = sqrt( diff );
  return;
}


float PtStatistic::ptMean() const {
  return mean;
}


float PtStatistic::ptRMS()  const {
  return rms;
}

