lcsim/src/org/lcsim/recon/cluster/analysis
diff -N CreateClusterAnalysisLists.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ CreateClusterAnalysisLists.java 5 Oct 2005 17:30:38 -0000 1.1
@@ -0,0 +1,248 @@
+package org.lcsim.recon.cluster.analysis;
+
+import org.lcsim.recon.cluster.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.lcsim.event.MCParticle;
+import org.lcsim.event.CalorimeterHit;
+import org.lcsim.event.SimCalorimeterHit;
+import org.lcsim.util.Driver;
+import org.lcsim.event.EventHeader;
+import hep.physics.vec.*;
+
+/**
+ * @author Ron Cassell
+ */
+public class CreateClusterAnalysisLists extends Driver
+{
+ String[] hitlistnames;
+ String[] clusterlistnames;
+ String fslistname;
+ String pclistname;
+ String cplistname;
+ /**
+ *
+ * @param calhitlists
+ * @param clusterlist
+ * @param fslist
+ * @param pclist
+ * @param cplist
+ */
+ public CreateClusterAnalysisLists(String[] calhitlists,String[] clusterlists,String fslist,String pclist,String cplist)
+ {
+ hitlistnames = calhitlists;
+ clusterlistnames = clusterlists;
+ fslistname = fslist;
+ pclistname = pclist;
+ cplistname = cplist;
+ }
+ /**
+ * Process an event.
+ *
+ * @param event
+ */
+ protected void process(EventHeader event)
+ {
+//
+// Get the Lists from event
+//
+ List<MCParticle> mcpall = null;
+ List<MCParticle> mcpfs = null;
+ List<BasicCluster> clusters = new ArrayList<BasicCluster>();
+ List<List<MCParticle>> mclists = event.get(MCParticle.class);
+ for(List<MCParticle> mclist:mclists)
+ {
+ if(event.getMetaData(mclist).getName().compareTo("MCParticle") == 0)mcpall = mclist;
+ if(event.getMetaData(mclist).getName().compareTo(fslistname) == 0)mcpfs = mclist;
+ }
+ List<List<BasicCluster>> cllists = event.get(BasicCluster.class);
+ for(List<BasicCluster> cllist:cllists)
+ {
+ for(int i=0;i<clusterlistnames.length;i++)
+ {
+ if(event.getMetaData(cllist).getName().compareTo(clusterlistnames[i]) == 0)
+ {
+ for(BasicCluster c:cllist)
+ {
+ clusters.add(c);
+ }
+ }
+ }
+ }
+//
+// Create a list of unclustered hits, and fill it with all the calorimeter hits
+//
+ List<CalorimeterHit> unclustered = new ArrayList<CalorimeterHit>();
+ List<List<CalorimeterHit>> callists = event.get(CalorimeterHit.class);
+ for(List<CalorimeterHit> callist:callists)
+ {
+ for(int i=0;i<hitlistnames.length;i++)
+ {
+ if(event.getMetaData(callist).getName().compareTo(hitlistnames[i]) == 0)
+ {
+ for(CalorimeterHit h:callist)
+ {
+ unclustered.add(h);
+ }
+ }
+ }
+ }
+//
+// If any of the necessary lists are null, write empty MCPClusterInfo and ClusterMCPInfo
+// lists to event, write an error message and return
+//
+ if( (mcpall == null)||(mcpfs == null)||(clusters.size() == 0) )
+ {
+ event.put(cplistname,new ArrayList<ClusterMCPInfo>());
+ event.put(pclistname,new ArrayList<MCPClusterInfo>());
+ String lnames = " ";
+ if(mcpall == null)lnames += "MCParticles ";
+ if(mcpfs == null)lnames += "final state particles "+fslistname+" ";
+ if(clusters.size() == 0)lnames += "clusters ";
+ System.err.println("CreateClusterAnalysisLists:: Input lists"+lnames+"do not exist in event: Empty analysis lists written");
+ return;
+ }
+//
+// Create the map of MCParticle to MCPClusterInfo, and a map of MCParticle to final state MCParticle
+//
+ Map<MCParticle,MCParticle> fsmap = new HashMap<MCParticle,MCParticle>();
+ Map<MCParticle,MCPClusterInfo> pclmap = new HashMap<MCParticle,MCPClusterInfo>();
+//
+// Loop over all MCParticles in event, filling final state map and map to MCPClusterInfo
+//
+ for(MCParticle p:mcpall)
+ {
+ MCParticle pp = traceit(p,mcpfs);
+ fsmap.put(p,pp);
+ if(p == pp)pclmap.put(p,new MCPClusterInfo(p,fsmap));
+ }
+//
+// Create the list of ClusterMCPInfo
+//
+ List<ClusterMCPInfo> cmc = new ArrayList<ClusterMCPInfo>();
+//
+// Loop over all the clusters
+//
+ for(BasicCluster c:clusters)
+ {
+//
+// For each cluster, create a ClusterMCPInfo
+//
+ cmc.add(new ClusterMCPInfo(c,fsmap));
+//
+// Find each MCParticle that contributed to this cluster, and add this cluster
+// to the MCPClusterInfo
+//
+ List<MCParticle> contmc = new ArrayList<MCParticle>();
+ for(CalorimeterHit h:c.getCalorimeterHits())
+ {
+//
+// and also remove all the clustered hits from the unclustered list
+//
+ unclustered.remove(h);
+ SimCalorimeterHit simhit = (SimCalorimeterHit) h;
+ for(int i=0;i<simhit.getMCParticleCount();i++)
+ {
+ MCParticle p = fsmap.get(simhit.getMCParticle(i));
+ if(p == null)p = simhit.getMCParticle(i);
+ if(!contmc.contains(p))contmc.add(p);
+ }
+ }
+//
+// Have all the contributing MCParticles for this cluster, so add this
+// cluster to each MCPClusterInfo
+//
+ for(MCParticle p:contmc)
+ {
+ if(pclmap.containsKey(p))
+ {
+ pclmap.get(p).AddCluster(c);
+ }
+ else
+ {
+ pclmap.put(p,new MCPClusterInfo(p,fsmap));
+ pclmap.get(p).AddCluster(c);
+ }
+ }
+ }
+//
+// Through with all the clustered, so unclustered hit list should be
+// correct. So add the unclustered hits to the MCPClusterInfo block
+//
+ List<MCParticle> contmc = new ArrayList<MCParticle>();
+ for(CalorimeterHit h:unclustered)
+ {
+ SimCalorimeterHit simhit = (SimCalorimeterHit) h;
+ for(int i=0;i<simhit.getMCParticleCount();i++)
+ {
+ MCParticle p = fsmap.get(simhit.getMCParticle(i));
+ if(p == null)p = simhit.getMCParticle(i);
+ if(!contmc.contains(p))contmc.add(p);
+ }
+//
+// Have all the contributers for this hit, so add this unclustered hit to each
+// MCPClusterInfo block
+//
+ for(MCParticle p:contmc)
+ {
+ if(pclmap.containsKey(p))
+ {
+ pclmap.get(p).AddUnclusteredHit(simhit);
+ }
+ else
+ {
+ pclmap.put(p,new MCPClusterInfo(p,fsmap));
+ pclmap.get(p).AddUnclusteredHit(simhit);
+ }
+ }
+ }
+//
+// MCPClusterInfo list now complete, so can fill the ClusterMCPInfo blocks
+//
+ for(ClusterMCPInfo cm:cmc)
+ {
+ cm.FillInfo(pclmap);
+ }
+//
+// Both lists complete, write them to event
+//
+ event.put(cplistname,cmc);
+ event.put(pclistname,new ArrayList<MCPClusterInfo>(pclmap.values()));
+//
+// Also write the MCParticle to MCPClusterInfo map to event, for convenience in analysis driver
+//
+ List<Map<MCParticle,MCPClusterInfo>> lpclmap = new ArrayList<Map<MCParticle,MCPClusterInfo>>();
+ lpclmap.add(pclmap);
+ event.put("MCPPtoMCPClusterInfoMap",lpclmap);
+ }
+ /**
+ * Trace a MCParticle back to a final state particle
+ * return null if none found
+ *
+ * @param p - particle to trace
+ * @param fs - List of final state particles
+ */
+ public MCParticle traceit(MCParticle p, List<MCParticle> fs)
+ {
+ MCParticle rp = null;
+//
+// If the particle is in the final state list, we're done
+//
+ if(fs.contains(p))return p;
+//
+// If not, check its parent. No need to go farther than single parent case
+//
+ MCParticle pp = p;
+ while( pp.getParents().size() == 1)
+ {
+ pp = pp.getParents().get(0);
+ if(fs.contains(pp))return pp;
+ }
+//
+// Didn't find one, return null
+//
+ return rp;
+ }
+}
\ No newline at end of file