View Javadoc

1   package org.lcsim.util.heprep;
2   
3   import hep.graphics.heprep.HepRepFactory;
4   import hep.graphics.heprep.HepRepInstance;
5   import hep.graphics.heprep.HepRepPoint;
6   import hep.graphics.heprep.HepRepType;
7   import hep.graphics.heprep.HepRepTypeTree;
8   import hep.graphics.heprep.HepRepInstanceTree;
9   import java.awt.Color;
10  import java.util.Arrays;
11  import java.util.Collections;
12  import java.util.List;
13  import org.lcsim.event.CalorimeterHit;
14  import org.lcsim.event.Cluster;
15  import org.lcsim.event.EventHeader;
16  import org.lcsim.event.EventHeader.LCMetaData;
17  import org.lcsim.event.SimCalorimeterHit;
18  import org.lcsim.spacegeom.CartesianPoint;
19  import org.lcsim.spacegeom.SpacePoint;
20  
21  /**
22   *
23   * @author tonyj
24   * @version $Id: ClusterConverter.java,v 1.8 2010/05/05 20:36:35 ngraf Exp $
25   */
26  class ClusterConverter implements HepRepCollectionConverter
27  {
28      private Color[] colors;
29      
30      ClusterConverter()
31      {
32          ColorMap cm = new RainbowColorMap();
33          colors = new Color[20];
34          for (int i=0; i<colors.length; i++) colors[i] = cm.getColor(((double) i)/colors.length,1);
35          // Shuffle the elements in the array
36          Collections.shuffle(Arrays.asList(colors));
37      }
38      public boolean canHandle(Class k)
39      {
40          return Cluster.class.isAssignableFrom(k);
41      }
42      public void convert(EventHeader event, List collection, HepRepFactory factory, HepRepTypeTree typeTree, HepRepInstanceTree instanceTree)
43      {
44          LCMetaData meta = event.getMetaData(collection);
45          String name = meta.getName();
46          int flags = meta.getFlags();
47          
48          HepRepType typeX = factory.createHepRepType(typeTree, name);
49          typeX.addAttValue("layer",LCSimHepRepConverter.HITS_LAYER);
50          typeX.addAttValue("drawAs","Point");
51          typeX.addAttValue("color",Color.RED);
52          typeX.addAttValue("fill",true);
53          typeX.addAttValue("fillColor",Color.RED);
54          typeX.addAttValue("MarkName","Box");
55          typeX.addAttDef("energy", "Hit Energy", "physics", "");
56          typeX.addAttDef("cluster", "Cluster Energy", "physics", "");
57          
58          int i = 0;
59          
60          for (Cluster cluster : (List<Cluster>) collection)
61          {
62              Color clusterColor = colors[i];
63              i = (i+1) % colors.length;
64              // Some clusters (for example created by the FastMC) may not have hits
65              // now draw cluster centroid as star
66              double[]  pos = cluster.getPosition();
67              HepRepInstance instanceC = factory.createHepRepInstance(instanceTree, typeX);
68              HepRepPoint cp = factory.createHepRepPoint(instanceC, pos[0],pos[1],pos[2]);
69              
70              SpacePoint point = new CartesianPoint(pos);
71              instanceC.addAttValue("drawAs", "Ellipsoid");
72              // TODO Change from fixed size to reflect true cluster dimensions.
73              instanceC.addAttValue("Radius", 5);
74              instanceC.addAttValue("Radius2", 5);
75              instanceC.addAttValue("Radius3", 20);
76              //TODO Change to use cluster direction instead of connecting origin to centroid.
77              double theta = point.theta(); 
78              double phi = point.phi(); 
79              instanceC.addAttValue("Phi", phi);
80              instanceC.addAttValue("Theta", theta);            
81              
82              instanceC.addAttValue("MarkName","Star");
83              instanceC.addAttValue("cluster",cluster.getEnergy());
84              instanceC.addAttValue("color",clusterColor);
85              instanceC.addAttValue("MarkSize",10);
86              
87              List<CalorimeterHit> hits = cluster.getCalorimeterHits();
88              if (hits != null)
89              {
90                  for (CalorimeterHit hit : hits)
91                  {
92                      // FixMe: What if hit doesn't have position?
93                  	double hitpos[] = null;
94                  	try {
95                  		hitpos = hit.getPosition();
96                  	}
97                  	catch (Exception x)
98                  	{}
99                  	
100                 	if (hitpos != null)
101                 	{
102                                 double e = 0.;
103                                 if(hit instanceof SimCalorimeterHit)
104                                 {
105                                     e = hit.getRawEnergy();
106                                 }
107                                 else
108                                 {
109                                     e = hit.getCorrectedEnergy();
110                                 }
111                 		pos = hit.getPosition();
112                 		HepRepInstance instanceX = factory.createHepRepInstance(instanceC, typeX);
113                 		instanceX.addAttValue("energy",e);
114                 		instanceX.addAttValue("MarkSize",5);
115                 		//instanceX.addAttValue("cluster",cluster.getEnergy());
116                 		instanceX.addAttValue("color",clusterColor);
117                 		instanceX.addAttValue("showparentattributes", true);
118                 		instanceX.addAttValue("pickparent", true);
119                 		HepRepPoint pp = factory.createHepRepPoint(instanceX,pos[0],pos[1],pos[2]);
120                 	}
121                 }
122             }
123         }
124     }
125 }