View Javadoc

1   package org.lcsim.recon.tracking.digitization.sisim.config;
2   
3   import java.util.List;
4   
5   import org.lcsim.detector.DetectorElementStore;
6   import org.lcsim.detector.IDetectorElement;
7   import org.lcsim.detector.identifier.IExpandedIdentifier;
8   import org.lcsim.detector.identifier.IIdentifier;
9   import org.lcsim.detector.identifier.IIdentifierDictionary;
10  import org.lcsim.detector.tracker.silicon.SiSensor;
11  import org.lcsim.event.EventHeader;
12  import org.lcsim.event.EventHeader.LCMetaData;
13  import org.lcsim.event.RawTrackerHit;
14  
15  /**
16   * Assigns {@link org.lcsim.detector.IDetectorElement}s to {@link org.lcsim.event.RawTrackerHit}s.
17   * 
18   * @author jeremym
19   */
20  public class RawTrackerHitSensorSetup extends CollectionHandler {
21      
22      public RawTrackerHitSensorSetup() {
23      }
24  
25      public void setReadoutCollections(String[] collectionNames) {
26          super.setCollections(collectionNames);
27      }
28  
29  
30      protected void process(EventHeader header) {
31          super.process(header);
32          List<List<RawTrackerHit>> collections = header.get(RawTrackerHit.class);
33          for (List<RawTrackerHit> collection : collections) {
34              LCMetaData meta = header.getMetaData(collection);
35              if (canHandle(meta.getName())) {                
36                  // Set the sensor links on the hits.
37                  setSensors(meta, collection);
38              }
39          }
40      }
41      
42      static public final void setSensors(LCMetaData meta, List<RawTrackerHit> hits) {
43  
44          // Get the ID dictionary and field information.
45          IIdentifierDictionary dict = meta.getIDDecoder().getSubdetector().getDetectorElement().getIdentifierHelper().getIdentifierDictionary();
46          int fieldIdx = dict.getFieldIndex("side");
47          int sideIdx = dict.getFieldIndex("strip");
48          
49          for (RawTrackerHit hit : hits) {
50                 
51              // The "side" and "strip" fields needs to be stripped from the ID for sensor lookup.
52              IExpandedIdentifier expId = dict.unpack(hit.getIdentifier());
53              expId.setValue(fieldIdx, 0);
54              expId.setValue(sideIdx, 0);
55              IIdentifier strippedId = dict.pack(expId);
56          
57              // Find the sensor DetectorElement.
58              List<IDetectorElement> des = DetectorElementStore.getInstance().find(strippedId);
59              if (des == null || des.size() == 0) {
60                  throw new RuntimeException("Failed to find any DetectorElements with stripped ID <0x" + Long.toHexString(strippedId.getValue()) + ">.");
61              }
62              else if (des.size() == 1) {
63                  hit.setDetectorElement((SiSensor)des.get(0));
64              }
65              else {
66                  // Use first sensor found, which should work unless there are sensors with duplicate IDs.
67                  for (IDetectorElement de : des) {
68                      if (de instanceof SiSensor) {
69                          hit.setDetectorElement((SiSensor)de);
70                          break;
71                      }
72                  }
73              }   
74              
75              // No sensor was found.
76              if (hit.getDetectorElement() == null) {
77                  throw new RuntimeException("No sensor was found for hit with stripped ID <0x" + Long.toHexString(strippedId.getValue()) + ">.");
78              }
79          }
80      }
81      
82  }