lcsim/src/org/lcsim/recon/cluster/directedtree
diff -u -r1.5 -r1.6
--- DirectedTreeClusterer.java 21 Jun 2006 18:03:31 -0000 1.5
+++ DirectedTreeClusterer.java 23 Jun 2006 20:43:53 -0000 1.6
@@ -1,92 +1,53 @@
package org.lcsim.recon.cluster.directedtree;
-import java.io.IOException;
import java.util.*;
-import hep.aida.*;
-import hep.physics.vec.Hep3Vector;
-import hep.physics.vec.BasicHep3Vector;
-
-import org.lcsim.util.aida.AIDA;
-import org.lcsim.util.Driver;
-import org.lcsim.event.EventHeader;
+import org.lcsim.recon.cluster.util.Clusterer;
import org.lcsim.event.CalorimeterHit;
-import org.lcsim.event.MCParticle;
-import org.lcsim.geometry.compact.Readout;
-// import org.lcsim.geometry.segmentation.SegmentationBase;
+import org.lcsim.event.Cluster;
import org.lcsim.geometry.IDDecoder;
import org.lcsim.geometry.subdetector.CylindricalCalorimeter;
import org.lcsim.geometry.subdetector.CalorimeterType;
-import org.lcsim.digisim.CellSelector;
-import org.lcsim.recon.cluster.util.CalHitMapMgr;
-import org.lcsim.recon.cluster.util.BasicCluster;
-import org.lcsim.recon.cluster.util.SortClustersBySize;
import org.lcsim.recon.cluster.util.CalorimeterHitESort;
-import org.lcsim.event.Cluster;
-public class DirectedTreeClusterer extends Driver {
+public class DirectedTreeClusterer implements Clusterer {
private int _debug = 0;
+ private LoadMyCalorimeterHit _loader;
+ private ClusterBuilder _clusBuilder;
+ private RunControlParameters _rcp = null;
+ private int _nLyr, _nZ, _nPhi;
+ private String _distType,_calType;
- public DirectedTreeClusterer() {
- _rcp = new RunControlParameters();
+ public DirectedTreeClusterer(RunControlParameters rcp) {
+ _rcp = rcp;
_loader = LoadMyCalorimeterHit.getInstance();
_clusBuilder= new ClusterBuilder(_rcp);
}
- public void setSteeringFile(String filename) {
- _rcp.setSteeringFile(filename);
- }
-
- public void process(EventHeader event) {
-
- _evtnum = event.getEventNumber();
- if(_debug>0) {
- System.out.println("Start DirectedTreeClusterer, event #"+_evtnum);
+ public List<Cluster> createClusters(List<CalorimeterHit> hits) {
+ Map<Long,CalorimeterHit> hitmap = new HashMap<Long,CalorimeterHit>();
+ for(CalorimeterHit hit : hits) {
+ hitmap.put( hit.getCellID(), hit );
}
+ return createClusters(hitmap);
+ }
- if( underUserControl() ) {
- // Assumes that collection is a hitmap. Failure otherwise.
- Map<Long,CalorimeterHit> hitmap = new HashMap<Long,CalorimeterHit>( (Map<Long,CalorimeterHit>)event.get(_inputHitMapName) );
-
- // define densities (should use user-provided hitmaps instead??)
- _loader.setDefaultDensities();
- // do the clustering
- List<BasicCluster> result = processHitmap( hitmap );
- // store results
- event.put( _outputClusterListName, result, Cluster.class, _flags);
-
- if( !_outputHitMapName.equals("") ) {
- // get list of useds successfully clustered
- Collection<CalorimeterHit> usedHits = new HashSet<CalorimeterHit>();
- for(Cluster clus : result) {
- usedHits.addAll(clus.getCalorimeterHits());
- }
-
- // get list of unclustered hits
- Map<Long,CalorimeterHit> unusedHits = new HashMap<Long,CalorimeterHit>(hitmap);
- for(CalorimeterHit hit : usedHits) {
- unusedHits.remove( hit.getCellID() );
- }
+ public List<Cluster> createClusters(Map<Long,CalorimeterHit> hitmap) {
+ List<List<CalorimeterHit>> trees = makeTree(hitmap);
- // append hitmap of unused hits to the event
- event.put( _outputHitMapName, unusedHits );
- }
- }
+ List<Cluster> recoClusColl = new ArrayList<Cluster>();
+ if(trees.size()>0)
+ recoClusColl = _clusBuilder.makeClusters( trees );
- else {
- processOriginal(event);
- }
+ return recoClusColl;
}
- /** This is the main clustering routine
- */
public List<List<CalorimeterHit>> makeTree(Map<Long,CalorimeterHit> cahitmap) {
-
if(_debug>0) {
System.out.println("DTreeClusterer.makeTree(): #hits="+cahitmap.size());
}
- _ParentMap = new HashMap<Long,Long>();
- List<CalorimeterHit> RootVec = new ArrayList<CalorimeterHit>();
+ Map<Long,Long> parentMap = new HashMap<Long,Long>();
+ List<CalorimeterHit> rootVec = new ArrayList<CalorimeterHit>();
// process hits according to raw energy
List<CalorimeterHit> sortedHits = new ArrayList<CalorimeterHit>(cahitmap.values());
@@ -96,7 +57,6 @@
long cellid = ihit.getCellID();
double idens = _loader.getDensity( cellid );
-// SegmentationBase segm = segBase;
IDDecoder segm = ihit.getIDDecoder();
segm.setID(cellid);
if(_debug>1) {
@@ -219,14 +179,14 @@
}
if(maxdensDiff<0.0){
- RootVec.add(ihit);
+ rootVec.add(ihit);
if(_debug>1) System.out.println("Bonafide root");
}
if(maxdensDiff>0.0){
Long key1 = new Long(cellid);
Long key2 = new Long(maxdensID);
- _ParentMap.put(key1,key2);
+ parentMap.put(key1,key2);
if(_debug>1) {
System.out.println(" maxdensDiff>0: id="
+MyTools.printID(cellid)
@@ -240,12 +200,12 @@
List<Long> removeItems = new ArrayList<Long>();
for(Long jkey : nVec) {
List<Long> temporary = new ArrayList<Long>();
- Long parent = _ParentMap.get(jkey);
+ Long parent = parentMap.get(jkey);
while(parent!=null){
temporary.add(parent);
Long lkey = parent;
- parent = _ParentMap.get(lkey);
+ parent = parentMap.get(lkey);
}
if(temporary.contains(cellid)){
removeItems.add(jkey);
@@ -254,7 +214,7 @@
nVec.removeAll(removeItems);
if(nVec.size()==0){
- RootVec.add(ihit);
+ rootVec.add(ihit);
}
else{
double dmin = 9999.;
@@ -269,14 +229,14 @@
parentKey = jkey;
}
}
- _ParentMap.put(cellid,parentKey);
+ parentMap.put(cellid,parentKey);
if(_debug>1) System.out.println("Cell is matched to identical density neighbor");
}
}
}
else{
if(_rcp.getMinimumDensity()==1 && _rcp.getMaximumDensity()==100000) { // default values
- RootVec.add(ihit);
+ rootVec.add(ihit);
if(_debug>1) System.out.println("Zero density root");
}
}
@@ -285,16 +245,16 @@
List<CalorimeterHit> startingPoints = new ArrayList<CalorimeterHit>();
for( CalorimeterHit ihit : sortedHits ) {
long cellid = ihit.getCellID();
- boolean isParent = _ParentMap.containsValue(cellid);
- boolean isRoot = RootVec.contains(ihit);
+ boolean isParent = parentMap.containsValue(cellid);
+ boolean isRoot = rootVec.contains(ihit);
if(!isParent && !isRoot){
startingPoints.add(ihit);
}
}
if(_debug>1) {
System.out.println("# starting points = "+startingPoints.size()
- +", pars="+_ParentMap.size()
- +", roots="+RootVec.size());
+ +", pars="+parentMap.size()
+ +", roots="+rootVec.size());
}
List<List<CalorimeterHit>> branches = new ArrayList<List<CalorimeterHit>>();
@@ -308,8 +268,8 @@
if(_debug>1)
System.out.println("Starting point #"+i+": "+MyTools.printID(cellid));
Long key = new Long(cellid);
- while(_ParentMap.containsKey(cellid)){
- Long jkey = _ParentMap.get(cellid);
+ while(parentMap.containsKey(cellid)){
+ Long jkey = parentMap.get(cellid);
CalorimeterHit jhit = cahitmap.get(jkey);
branches.get(i).add(jhit);
cellid = jkey.longValue();
@@ -317,7 +277,7 @@
}
int nTrivial = 0;
- int vsiz = RootVec.size();
+ int vsiz = rootVec.size();
if(_debug>0) System.out.println("no. of roots = "+vsiz);
List<List<CalorimeterHit>> caTrees = new ArrayList<List<CalorimeterHit>>();
for(int i=0;i<vsiz;i++) {
@@ -325,8 +285,7 @@
}
for(int i=0;i<vsiz;i++) {
- CalorimeterHit ihit = RootVec.get(i);
- long tmpID = ihit.getCellID();
+ CalorimeterHit ihit = rootVec.get(i);
caTrees.get(i).add(ihit);
for(int j=0;j<startingPoints.size();j++) {
List<CalorimeterHit> jbranch = branches.get(j);
@@ -335,14 +294,17 @@
for( CalorimeterHit jhit : jbranch ) {
if(!caTrees.get(i).contains(jhit)) {
caTrees.get(i).add(jhit);
- long tmppID = jhit.getCellID();
-// System.out.println("Branches id="+MyTools.printID(tmppID));
+ if(_debug>2) {
+ long tmppID = jhit.getCellID();
+ System.out.println("Branch id="+MyTools.printID(tmppID));
+ }
}
}
}
}
if(_debug>0) {
if(caTrees.get(i).size()>0) {
+ long tmpID = ihit.getCellID();
System.out.println("ROOT id="+MyTools.printID(tmpID)
+", #hits="+caTrees.get(i).size());
}
@@ -357,6 +319,9 @@
// System.out.println("ROOT: #hits="+rootHits.size());
// }
+ // cleanup
+ parentMap.clear();
+ rootVec.clear();
return caTrees;
}
@@ -378,114 +343,7 @@
return region;
}
- /** Original behaviour was to process a pre-defined list of
- * collections, each collection being a List<CalorimeterHits>
- */
- private void processOriginal(EventHeader event)
- {
- if(_rcp.ClusterSeparately()) {
- for(String colName : _origNames) {
- // get a hitmap
- Map<Long,CalorimeterHit> hitmap = _expert.getCollHitMap(colName);
- // define densities
- _loader.setDensities(colName, hitmap);
- // do the clustering
- List<BasicCluster> result = processHitmap( hitmap );
- // store the results
- String newName = new String(colName+"DTreeClusters");
- event.put( newName, result, Cluster.class, _flags );
- }
- }
- else {
- // one-pass clustering: a single hitmap is needed
- Map<Long,CalorimeterHit> hitmap = new HashMap<Long,CalorimeterHit>();
- for(String colName : _origNames) {
- Map<Long,CalorimeterHit> tmpmap = _expert.getCollHitMap(colName);
- hitmap.putAll( tmpmap );
- // define densities
- _loader.setDensities(colName, hitmap);
- }
-
- // do the clustering
- List<BasicCluster> result = processHitmap( hitmap );
- // store the results
- String newName = new String("SinglePassDTreeClusters");
- event.put( newName, result, Cluster.class, _flags );
- }
- }
-
- // Not doing much for now, but makeTree will be moved out to a standalone class at some point
- private List<BasicCluster> processHitmap( Map<Long,CalorimeterHit> hitmap )
- {
- List<List<CalorimeterHit>> trees = makeTree(hitmap);
-
- List<BasicCluster> recoClusColl = new ArrayList<BasicCluster>();
- if(trees.size()>0)
- recoClusColl = _clusBuilder.makeClusters( trees );
-
- return recoClusColl;
- }
-
- /** set name of collection of hits to be clustered */
- public void setInputHitMap(String name) {
- _inputHitMapName = name;
- this.setUserControl();
- }
-
- /** set name of output collection of clusters reconstructed */
- public void setOutputClusterList(String name) {
- _outputClusterListName = name;
- this.setUserControl();
- }
-
- /** set name of output hit map with left-over hits */
- public void setOutputHitMap(String name) {
- _outputHitMapName = name;
- }
-
- private void setUserControl() {
- if( !_inputHitMapName.equals("") &&
- !_outputClusterListName.equals("") ) {
- _userControl = true;
- }
- }
-
- public boolean underUserControl() {
- return _userControl;
- }
-
- public void setDebugLevel(int deb) {
- _debug = deb;
- }
-
- /** Set minimum number of hits required for clusters to be saved
- */
- public void setMinimumClusterSize(int minsize) {
+ public void setMinimumSize(int minsize) {
_clusBuilder.setMinimumSize(minsize);
}
-
- // *** FIELDS ***
-
- private RunControlParameters _rcp;
- private LoadMyCalorimeterHit _loader;
- private CalHitMapMgr _expert = CalHitMapMgr.getInstance();
- private Map<Long,Long> _ParentMap;
- private Map<Long,CalorimeterHit> embhitmap,emehitmap,hdbhitmap,hdehitmap;
- private String _distType,_calType;
- private int _nLyr;
- private int _nZ;
- private int _nPhi;
- private int _evtnum;
-
- private ClusterBuilder _clusBuilder;
- private int _flags = 1<<31; // store hits in clusters
-
- // collection names for user steering
- private String _inputHitMapName = "";
- private String _outputHitMapName = "";
- private String _outputClusterListName = "";
- private boolean _userControl = false;
-
- private String[] _origNames = {"EcalBarrDigiHits", "EcalEndcapDigiHits",
- "HcalBarrDigiHits", "HcalEndcapDigiHits"};
}