import hep.analysis.*;
import hep.physics.*;
import hep.lcd.util.driver.*;
import hep.lcd.event.*;
import java.util.*;

public class MyAnalysis extends Driver
{
	public MyAnalysis()
	{
		new ProfilePlot("Etot vs NParticles");
	}
	// Called by the framework to process each event
	public void process(LCDEvent event)
	{
		// Loop over the MC particles
		ParticleVector list = event.getMCParticles();
		Enumeration e = list.particles();
		
		double etot = 0;
		while (e.hasMoreElements())
		{
			MCParticle mc = (MCParticle) e.nextElement();
			
			// histogram particle energy
			double energy = mc.getEnergy();
			histogram("Particle Energy").fill(energy);
			
			// reject non final state particles
			histogram("Particle Status").fill(mc.getStatusCode());
		    if (mc.getStatusCode() != mc.FINALSTATE) continue;
			
			// reject neutral particles
			ParticleType type = mc.getType();
			int charge = (int) type.getCharge();
			histogram("Particle Charge").fill(charge);
		    if (charge == 0) continue;
			
			// Some more histograms
			String name = type.getName();
			histogram("Particle Type").fill(name);
			// Create a folder for each particle type
			HistogramFolder.setDefaultFolder("/"+name);
			histogram("Energy").fill(energy);
			HistogramFolder.setDefaultFolder("/");
			
			etot += energy;
		}
		histogram("Etot").fill(etot);
		//histogram("Etot vs NParticles").fillW(list.size(),etot);
	}
	public void beforeFirstEvent()
	{
		startTime = System.currentTimeMillis();
	}
	public void afterLastEvent()
	{
		long endTime = System.currentTimeMillis();
		int elapsed = (int) (endTime - startTime);
		histogram("elapsedTime").fill(elapsed);
		try
		{
			String host = java.net.InetAddress.getLocalHost().getHostName();
			histogram("Time By Host").fillW(host,elapsed);
		}
		catch (java.net.UnknownHostException x) {}
	}
	private long startTime;
}