View Javadoc

1   package org.lcsim.plugin.browser;
2   
3   import java.awt.BorderLayout;
4   import java.util.ArrayList;
5   import java.util.Collections;
6   import java.util.Enumeration;
7   import java.util.HashMap;
8   import org.freehep.util.ScientificFormat;
9   
10  import javax.swing.tree.DefaultTreeModel;
11  import javax.swing.tree.TreeNode;
12  import java.util.List;
13  import java.util.Map;
14  import javax.swing.JComponent;
15  import javax.swing.JScrollPane;
16  import javax.swing.JTree;
17  import org.lcsim.event.EventHeader;
18  import org.lcsim.event.MCParticle;
19  
20  /**
21   *
22   * @author tonyj
23   * @version $Id: LCSimEventTree.java,v 1.2 2007/04/09 05:11:11 tonyj Exp $
24   */
25  class LCSimEventTree extends JComponent
26  {
27     LCSimEventTree()
28     {
29        setLayout(new BorderLayout());
30        m_root = new ParticleNode();
31        m_tree = new JTree(m_root);
32        m_tree.setRootVisible(false);
33        add(new JScrollPane(m_tree), BorderLayout.CENTER);
34     }
35  
36     void setEvent(EventHeader event)
37     {
38        m_root.clear();
39        m_event = event;
40           
41        if (m_event != null && m_event.hasCollection(MCParticle.class, EventHeader.MC_PARTICLES))
42        {
43           // Any particle which has no parents should appear at the
44           // root level
45           // Note: Particles whose only parent is themselves are counted as having no
46           // parents!
47           // Note: Sometimes the parent/daughter information is inconsistent, so we 
48           // use _only_ parent information to build the tree
49           // Note: We only use the first parent, second parent info is ignored.
50           
51           Map<MCParticle,ParticleNode> temp = new HashMap<MCParticle,ParticleNode>();
52           
53           List<MCParticle> particles = event.get(MCParticle.class,EventHeader.MC_PARTICLES);
54           for (MCParticle p : particles )
55           {
56              ParticleNode node = new ParticleNode(p);
57              temp.put(p,node);
58           }
59           for (MCParticle p : particles )
60           {
61              List<MCParticle> parents = p.getParents();
62              boolean hasParent = parents != null && !parents.isEmpty() && parents.get(0) != p;
63  
64              ParticleNode parentNode = hasParent ? temp.get(parents.get(0)) : m_root;
65              parentNode.addChild((ParticleNode) temp.get(p));
66           }
67        }
68        ((DefaultTreeModel) m_tree.getModel()).nodeStructureChanged(m_root);
69     }
70     
71     private EventHeader m_event;
72     private JTree m_tree;
73     private ParticleNode m_root;
74     private ScientificFormat format = new ScientificFormat();
75     
76     private class ParticleNode implements TreeNode
77     {
78        ParticleNode()
79        {
80        }
81        ParticleNode(MCParticle mc)
82        {
83           this.particle = mc;
84        }
85        void addChild(ParticleNode child)
86        {
87           children.add(child);
88           child.parent = this;
89        }
90        public int getIndex(TreeNode node)
91        {
92           return children.indexOf(node);
93        }
94        public TreeNode getChildAt(int index)
95        {
96           return (TreeNode) children.get(index);
97        }
98        public TreeNode getParent()
99        {
100          return parent;
101       }
102       public boolean getAllowsChildren()
103       {
104          return true;
105       }
106       public Enumeration children()
107       {
108          return Collections.enumeration(children);
109       }
110       public boolean isLeaf()
111       {
112          return children.isEmpty();
113       }
114       public int getChildCount()
115       {
116          return children.size();
117       }
118       void clear()
119       {
120          children.clear();
121       }
122       public String toString()
123       {
124          if (particle == null)
125          {
126             if (m_event == null) return "No Event";
127             else return "Run "+m_event.getRunNumber()+" Event "+m_event.getEventNumber();
128          }
129          else
130          {
131             String result = String.valueOf(particle.getType().getName());
132             result += "(E="+format.format(particle.getEnergy())+" status="+MCParticleTableModel.convert(particle.getGeneratorStatus())+")";
133             return result;
134          }
135       }
136       private MCParticle particle;
137       private List children = new ArrayList();
138       private TreeNode parent;
139    }
140 }