View Javadoc

1   package org.lcsim.recon.tracking.trflayer;
2   import java.util.Map;
3   import java.util.HashMap;
4   import java.util.TreeMap;
5   import java.util.Collection;
6   import java.util.Iterator;
7   import java.util.List;
8   import java.util.ArrayList;
9   
10  import org.lcsim.recon.tracking.trfutil.RandomSimulator;
11  
12  import org.lcsim.recon.tracking.trfbase.VTrack;
13  
14  /**
15   * A detector simulator contains a detector and a list of layer
16   * simulators which match the layers in the detector.
17   *<p>
18   * The base simulator is constructed from the detector reference.
19   * Subclasses add layer simulators either directly or by extraction from
20   * other detector simulators.
21   *<p>
22   * Methods to add or drop clusters invoke the corresponding method on
23   * each of the layer simulators.
24   *<p>
25   * Although this class is not abstract, the constructor is hidden and
26   * users must provide subclasses to instantiate.  The expectation is that
27   * users will add layers or detectors in the subclass constructor.
28   *
29   *@author Norman A. Graf
30   *@version 1.0
31   *
32   **/
33  
34  public class DetectorSimulator extends RandomSimulator
35  {
36      
37      // attributes
38      
39      // Detector.
40      private Detector _det;
41      
42      // Layer simulators.
43      private Map _lsims;
44      
45      // methods
46      
47      // Constructors from a detector.
48      protected DetectorSimulator( Detector det)
49      {
50          //_lsims = new HashMap();
51          _lsims = new TreeMap(); // for ordering of layers
52          _det = det;
53      }
54      
55      // Add a layer simulator by name.
56      // Name must be known to the detector.
57      // Detector and layer simulator must reference the same layer
58      // object.
59      // Return nonzero for error.
60      protected DetSimReturnStatus
61              addLayerSimulator(String name, LayerSimulator lsim)
62      {
63          // Check the name.
64          if ( ! _det.isAssigned(name) ) return DetSimReturnStatus.UNKNOWN_NAME;
65          
66          // Fetch the layers and check for match.
67          Layer detlyr = _det.layer(name);
68          if(detlyr instanceof InteractingLayer) detlyr = ((InteractingLayer)detlyr).layer();
69          Layer simlyr = lsim.layer();
70          
71          if ( !detlyr.equals(simlyr) ) return DetSimReturnStatus.LAYER_MISMATCH;
72          
73          _lsims.put(name, lsim);
74          
75          return DetSimReturnStatus.OK;
76          
77      }
78      
79      // Add all the layer simulators from a detector simulator.
80      // Each name must be known to the local detector.
81      // Detector and each layer simulator must reference the same
82      // layer object.
83      // Return nonzero for error.
84      protected DetSimReturnStatus
85              addDetectorSimulator(  DetectorSimulator dsim)
86      {
87          // Loop over layer simulators.
88          for(Iterator i=dsim._lsims.entrySet().iterator(); i.hasNext(); )
89          {
90              Map.Entry e = (Map.Entry) i.next();
91              String name = (String) e.getKey();
92              LayerSimulator lsim = (LayerSimulator) e.getValue();
93              DetSimReturnStatus stat = addLayerSimulator(name,lsim);
94              if ( stat != DetSimReturnStatus.OK ) return stat;
95          }
96          
97          return DetSimReturnStatus.OK;
98          
99      }
100     
101     // methods
102     
103     //
104     
105     /**
106      *Return the detector.
107      *
108      * @return Detector
109      */
110     public Detector detector()
111     {
112         return _det;
113     }
114     
115     //
116     
117     /**
118      *Return the list of generators.
119      *
120      * @return list of HitGenerators
121      */
122     public List generators()
123     {
124         List gens = new ArrayList();
125         // Loop over layer simulators.
126         for(Iterator i=_lsims.entrySet().iterator(); i.hasNext(); )
127         {
128             Map.Entry e = (Map.Entry) i.next();
129             
130             LayerSimulator lsim = (LayerSimulator) e.getValue();
131             List newgens = lsim.generators();
132             for ( Iterator igen=newgens.iterator(); igen.hasNext(); )
133             {
134                 gens.add(igen.next());
135             }
136         }
137         return gens;
138     }
139     
140     //
141     
142     /**
143      *Use the specified track to add clusters with each layer simulator.
144      *
145      * @param   trv VTrack for which to generate clusters
146      */
147     public void addClusters( VTrack trv )
148     {
149         addClusters(trv, 0);
150     }
151     
152     
153     /**
154      *Use the specified track to add clusters with each layer simulator.
155      *
156      * @param   trv VTrack for which to generate clusters
157      * @param   mcid MC track ID to associate with this track
158      */
159     public void addClusters( VTrack trv, int mcid )
160     {
161         
162         // Loop over layer simulators.
163         for (Iterator i=_lsims.entrySet().iterator(); i.hasNext(); )
164         {
165             Map.Entry e = (Map.Entry) i.next();
166             LayerSimulator lsim = (LayerSimulator) e.getValue();
167             lsim.addClusters(trv, mcid);
168         }
169         
170     }
171     
172     //
173     
174     /**
175      *Drop clusters from each layer simulator.
176      *
177      */
178     public void dropClusters()
179     {
180         // Loop over layer simulators.
181         for (Iterator i=_lsims.entrySet().iterator(); i.hasNext(); )
182         {
183             Map.Entry e = (Map.Entry) i.next();
184             LayerSimulator lsim = (LayerSimulator) e.getValue();
185             lsim.dropClusters();
186         }
187         
188     }
189     
190     //
191     
192     /**
193      *write out the generators
194      *
195      */
196     public void printGenerators()
197     {
198         
199     }
200     
201     
202     /**
203      *output stream
204      *
205      * @return String representation of this class
206      */
207     public String toString()
208     {
209         int size = _lsims.size();
210         StringBuffer sb = new StringBuffer(getClass().getName()+" with " + size + " layer generator");
211         if ( size != 1 ) sb.append("s");
212         if ( size == 0 )
213         {
214             sb.append(".");
215         }
216         else
217         {
218             sb.append(":\n");
219             if ( _lsims.size() == 0 )
220             {
221                 sb.append("There are no generators.");
222                 return sb.toString();
223             }
224             for (Iterator i=_lsims.entrySet().iterator(); i.hasNext(); )
225             {
226                 Map.Entry e = (Map.Entry) i.next();
227                 
228                 sb.append( "\n" + e.getKey()+": "+e.getValue() + "\n");
229             }
230         }
231         return sb.toString();
232     }
233 }
234