java/trunk/users/src/main/java/org/lcsim/hps/users/ngraf
--- java/trunk/users/src/main/java/org/lcsim/hps/users/ngraf/NearestNeighborClusterDriver.java 2014-01-30 23:04:02 UTC (rev 139)
+++ java/trunk/users/src/main/java/org/lcsim/hps/users/ngraf/NearestNeighborClusterDriver.java 2014-01-30 23:23:09 UTC (rev 140)
@@ -18,64 +18,71 @@
/**
* This is a test
+ *
* @author Norman A Graf
*
* @version $Id:
*/
public class NearestNeighborClusterDriver extends Driver
{
- // Histogram manager
- private AIDA aida;
// Minimum E for cluster seed.
double seedEMin = .00 * ECalUtils.GeV;
// Minimum E to add hit to cluster.
double addEMin = .00 * ECalUtils.GeV;
// Map of crystals to their neighbors.
HPSEcal3.NeighborMap _neighborMap = null;
+ String ecalCollectionName;
+ String ecalName = "Ecal";
+ String clusterCollectionName = "NearestNeighborEcalClusters";
+ public void setEcalName(String ecalName)
+ {
+ this.ecalName = ecalName;
+ }
+
+ public void setEcalCollectionName(String ecalCollectionName)
+ {
+ this.ecalCollectionName = ecalCollectionName;
+ }
+
+ public void setClusterCollectionName(String clusterCollectionName)
+ {
+ this.clusterCollectionName = clusterCollectionName;
+ }
+
+ public void setSeedEMin(double seedEMin)
+ {
+ this.seedEMin = seedEMin;
+ }
+
+ public void setAddEMin(double addEMin)
+ {
+ this.addEMin = addEMin;
+ if (seedEMin < addEMin) {
+ seedEMin = addEMin;
+ }
+ }
+
@Override
- public void processChildren(EventHeader event)
+ protected void process(EventHeader event)
{
- boolean debug = true;
- // This is example code to run over a simple lcio file output from slic
- // on which no calorimeter digitzation has been done.
- // Therefore need to convert from SimCalorimeterHit to CalorimeterHit
- // This would normally be done as a part of the reconstruction
- if (event.hasCollection(SimCalorimeterHit.class, "EcalHits")) {
- // Get the list of sim ECal hits.
- List<SimCalorimeterHit> hits = event.get(SimCalorimeterHit.class, "EcalHits");
+ // Fetch the list of CalorimeterHits
+ if (event.hasCollection(CalorimeterHit.class, ecalCollectionName)) {
+
+ List<CalorimeterHit> hits = event.get(CalorimeterHit.class, ecalCollectionName);
- // create a list of CalorimeterHits
// Make a hit map for quick lookup by ID.
Map<Long, CalorimeterHit> hitMap = new HashMap<Long, CalorimeterHit>();
- for (SimCalorimeterHit hit : hits) {
+ for (CalorimeterHit hit : hits) {
hitMap.put(hit.getCellID(), hit);
}
// Cluster the hits using a nearest neighbor algorithm
- int flag = 1 << LCIOConstants.CLBIT_HITS;
List<NearestNeighborCluster> clusters = createNearestNeighborClusters(hitMap);
- // quick analysis example
- if(debug) System.out.println("found " + clusters.size() + " clusters");
- double eTop = 0.;
- double eBottom = 0.;
- for (Cluster clus : clusters) {
- if(debug)System.out.println("x: " + clus.getPosition()[0] + " y: " + clus.getPosition()[1] + " iPhi: " + clus.getIPhi() + " iTheta " + clus.getITheta());
- if(clus.getPosition()[1]>0)
- {
- eTop+=clus.getEnergy();
- }
- else
- {
- eBottom += clus.getEnergy();
- }
- }
- //fill the histograms
- aida.cloud1D("Top energy sum").fill(eTop);
- aida.cloud1D("Bottom energy sum").fill(eBottom);
- aida.cloud1D("energy diff").fill(eTop-eBottom);
+
//add the list of clusters to the event
- event.put("NearestNeighborClusters", clusters, NearestNeighborCluster.class, flag);
+ int flag = 1 << LCIOConstants.CLBIT_HITS;
+ event.put(clusterCollectionName, clusters, NearestNeighborCluster.class, flag);
}
}
@@ -83,17 +90,16 @@
protected void detectorChanged(Detector detector)
{
// Get the Subdetector.
- HPSEcal3 ecal = (HPSEcal3) detector.getSubdetector("Ecal");
+ HPSEcal3 ecal = (HPSEcal3) detector.getSubdetector(ecalName);
// Cache the neighbor map for use by the nearest neighbor clustering algorithm
_neighborMap = ecal.getNeighborMap();
- // set up some plotting infrastructure
- aida = AIDA.defaultInstance();
- aida.tree().cd("/");
}
/**
* Run the clustering algorithm over the list of hit crystals
- * @param map Map of CalorimeterHit keyed on CellID. Note that this map is modified
+ *
+ * @param map Map of CalorimeterHit keyed on CellID. Note that this map is
+ * modified
* @return The list of found clusters
*/
public List<NearestNeighborCluster> createNearestNeighborClusters(Map<Long, CalorimeterHit> map)
@@ -113,4 +119,4 @@
}
return clusters;
}
-}
\ No newline at end of file
+}
java/trunk/users/src/test/java/org/lcsim/hps/users/ngraf
--- java/trunk/users/src/test/java/org/lcsim/hps/users/ngraf/NearestNeighborClusterDriverTest.java 2014-01-30 23:04:02 UTC (rev 139)
+++ java/trunk/users/src/test/java/org/lcsim/hps/users/ngraf/NearestNeighborClusterDriverTest.java 2014-01-30 23:23:09 UTC (rev 140)
@@ -2,34 +2,122 @@
import java.io.File;
import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
import junit.framework.TestCase;
+import org.lcsim.event.CalorimeterHit;
+import org.lcsim.event.Cluster;
+import org.lcsim.event.EventHeader;
+import org.lcsim.event.SimCalorimeterHit;
+import org.lcsim.util.Driver;
+import org.lcsim.util.aida.AIDA;
import org.lcsim.util.cache.FileCache;
import org.lcsim.util.loop.LCSimLoop;
+import hep.aida.IAnalysisFactory;
+import hep.aida.IPlotter;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
/**
*
* @author ngraf
*/
public class NearestNeighborClusterDriverTest extends TestCase
{
- static final String testURLBase = "http://www.slac.stanford.edu/~ngraf/hps_data/";
+
+ static final String testURLBase = "http://www.slac.stanford.edu/~ngraf/hps_data/";
static final String testFileName = "outfile3.slcio";
private final int nEvents = 500;
- public void testClustering() throws Exception {
-
+ public void testClustering() throws Exception
+ {
File lcioInputFile = null;
-
URL testURL = new URL(testURLBase + "/" + testFileName);
FileCache cache = new FileCache();
lcioInputFile = cache.getCachedFile(testURL);
-
//Process and write out the file
LCSimLoop loop = new LCSimLoop();
loop.setLCIORecordSource(lcioInputFile);
- loop.add(new NearestNeighborClusterDriver());
+ loop.add(new SimpleConverterDriver());
+ NearestNeighborClusterDriver clusterer = new NearestNeighborClusterDriver();
+ clusterer.setEcalCollectionName("CalorimeterHits");
+ clusterer.setClusterCollectionName("NearestNeighborEcalClusters");
+ loop.add(clusterer);
+ loop.add(new SimpleAnalysis());
loop.loop(nEvents, null);
loop.dispose();
+ }
+ class SimpleConverterDriver extends Driver
+ {
+
+ @Override
+ protected void process(EventHeader event)
+ {
+ if (event.hasCollection(SimCalorimeterHit.class, "EcalHits")) {
+ // Get the list of sim ECal hits.
+ List<SimCalorimeterHit> simhits = event.get(SimCalorimeterHit.class, "EcalHits");
+
+ // create a list of CalorimeterHits
+ List<CalorimeterHit> calHits = new ArrayList<CalorimeterHit>();
+ for (SimCalorimeterHit hit : simhits) {
+ calHits.add(hit);
+ }
+ event.put("CalorimeterHits", calHits);
+ }
+ }
}
+
+ class SimpleAnalysis extends Driver
+ {
+
+ // Histogram manager
+ private AIDA aida = AIDA.defaultInstance();
+ // Set the following to true in order to see the plots
+ private boolean debug = false;
+
+ @Override
+ protected void process(EventHeader event)
+ {
+ List<Cluster> clusters = event.get(Cluster.class, "NearestNeighborEcalClusters");
+ if(debug) System.out.println("found " + clusters.size() + " clusters");
+ double eTop = 0.;
+ double eBottom = 0.;
+ for (Cluster clus : clusters) {
+ if(debug) System.out.println("x: " + clus.getPosition()[0] + " y: " + clus.getPosition()[1] + " iPhi: " + clus.getIPhi() + " iTheta " + clus.getITheta());
+ if (clus.getPosition()[1] > 0) {
+ eTop += clus.getEnergy();
+ } else {
+ eBottom += clus.getEnergy();
+ }
+ }
+ //fill the histograms
+ aida.cloud1D("Top energy sum").fill(eTop);
+ aida.cloud1D("Bottom energy sum").fill(eBottom);
+ aida.cloud1D("energy diff").fill(eTop - eBottom);
+ }
+
+ @Override
+ protected void endOfData()
+ {
+ System.out.println("end of data");
+ if (debug) {
+ try{
+ //BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
+ IAnalysisFactory af = IAnalysisFactory.create();
+ IPlotter plotter = af.createPlotterFactory().create("Nearest Neighbor Clustering Analysis");
+ plotter.createRegions(1, 2, 0);
+ plotter.region(0).plot(aida.cloud1D("Top energy sum"));
+ plotter.region(1).plot(aida.cloud1D("Bottom energy sum"));
+ plotter.show();
+ //stdin.readLine();
+ Thread.sleep(2000);
+ }
+ catch(Exception e)
+ {
+ }
+ }
+ }
+ }
}