View Javadoc

1   package org.lcsim.mc.fast.tracking;
2   
3   /**
4    *  $Id: MCFastTracking.java,v 1.19 2009/05/29 22:49:55 timb Exp $
5    */
6   import hep.physics.particle.Particle;
7   import java.io.IOException;
8   import java.util.ArrayList;
9   import java.util.Iterator;
10  import java.util.List;
11  
12  import org.lcsim.conditions.ConditionsEvent;
13  import org.lcsim.conditions.ConditionsListener;
14  import org.lcsim.conditions.ConditionsSet;
15  import org.lcsim.event.EventHeader;
16  import org.lcsim.event.Track;
17  import org.lcsim.event.LCRelation;
18  import org.lcsim.event.MCParticle;
19  import org.lcsim.util.Driver;
20  import org.lcsim.event.base.MyLCRelation;
21  
22  /**
23   * Fast Monte Carlo tracking simulator
24   */
25  public class MCFastTracking extends Driver implements ConditionsListener {
26      private TrackResolutionTables parm;
27      private SimpleTables SmTbl;
28      private boolean beamSpotConstraint;
29      private boolean simple;
30      private final static double[] IP = { 0, 0, 0 };
31      private boolean defaultMC = true;
32      private String fsname;
33      private String outputListName = null;
34  
35      public MCFastTracking() {
36          this(false);
37      }
38  
39      public MCFastTracking(boolean beamSpotConstraint) {
40          this.beamSpotConstraint = beamSpotConstraint;
41      }
42  
43      public MCFastTracking(boolean beamSpotConstraint, boolean simple) {
44          this.beamSpotConstraint = beamSpotConstraint;
45          this.simple = simple;
46      }
47  
48      public void setBeamSpotConstraint(boolean beamSpotConstraint) {
49          this.beamSpotConstraint = beamSpotConstraint;
50          if (parm != null) {
51              ConditionsSet conditions = getConditionsManager().getConditions("TrackParameters");
52              parm = setTrackResolutionTables(conditions, beamSpotConstraint);
53          }
54      }
55  
56      public boolean isBeamSpotConstraint() {
57          return this.beamSpotConstraint;
58      }
59  
60      private TrackResolutionTables setTrackResolutionTables(ConditionsSet conditions, boolean beamSpotConstraint) {
61          try {
62              return new TrackResolutionTables(conditions, beamSpotConstraint);
63          } catch (IOException x) {
64              throw new RuntimeException("Error reading track resolution tables", x);
65          }
66      }
67  
68      public void setFSList(String fslist) {
69          fsname = fslist;
70          defaultMC = false;
71      }
72  
73      protected void process(EventHeader event) {
74          if (defaultMC) {
75              fsname = "MCParticle";
76          } else {
77              if (!event.hasCollection(MCParticle.class, fsname)) {
78                  System.err.println("Collection " + fsname + " not found. Default Final State particles being used");
79                  fsname = "MCParticle";
80                  defaultMC = true;
81              }
82          }
83          if (parm == null) {
84              ConditionsSet conditions = getConditionsManager().getConditions("TrackParameters");
85              conditions.addConditionsListener(this);
86              parm = setTrackResolutionTables(conditions, beamSpotConstraint);
87          }
88  
89          if (SmTbl == null) {
90              ConditionsSet conditions = getConditionsManager().getConditions("SimpleTrack");
91              conditions.addConditionsListener(this);
92              SmTbl = new SimpleTables(conditions);
93          }
94  
95          double bField = event.getDetector().getFieldMap().getField(IP)[2];
96          boolean hist = getHistogramLevel() > 0;
97  
98          List<Track> trackList = new ArrayList<Track>();
99          List<LCRelation> lcrelationList = new ArrayList<LCRelation>();
100         for (Iterator i = event.get(MCParticle.class, fsname).iterator(); i.hasNext();) {
101             Particle p = (Particle) i.next();
102 
103             // filter for FINAL_STATE
104             if (defaultMC) {
105                 if (p.getGeneratorStatus() != Particle.FINAL_STATE) {
106                     continue;
107                 }
108             }
109             double pCharge = p.getCharge();
110             if (pCharge == 0 || Double.isNaN(pCharge) || pCharge == Double.NEGATIVE_INFINITY || pCharge == Double.POSITIVE_INFINITY) {
111                 continue;
112             }
113 
114             double[] momentum = p.getMomentum().v();
115             if (Double.isNaN(momentum[0]) || Double.isNaN(momentum[1]) || Double.isNaN(momentum[2])) {
116                 continue;
117             }
118 
119             double pt2 = (momentum[0] * momentum[0]) + (momentum[1] * momentum[1]);
120             double pt = Math.sqrt(pt2);
121             double ptot = Math.sqrt(pt2 + (momentum[2] * momentum[2]));
122             double cosTheta = momentum[2] / ptot;
123 
124             // within acceptance
125             if (pt < parm.getPtMin()) {
126                 continue;
127             }
128             if (Math.abs(cosTheta) > parm.getPolarOuter()) {
129                 continue;
130             }
131 
132             try {
133                 ReconTrack newTrack = new ReconTrack(bField, parm, SmTbl, getRandom(), p, hist, simple);
134                 trackList.add(newTrack);
135                 lcrelationList.add(new MyLCRelation((Track) newTrack, (MCParticle) p));
136             } catch (hep.physics.particle.properties.UnknownParticleIDException x) {
137                 System.out.println("WARNING: MCFastTracking ignored a particle of type " + p.getPDGID());
138             }
139         }
140         if (outputListName == null) {
141             outputListName = "Tracks";
142         }
143         event.put(outputListName, trackList, Track.class, 0);
144         event.put("TracksToMCP", lcrelationList, LCRelation.class, 0);
145     }
146 
147     /** Specify the name under which to write out the list of tracks to the event. Default is EventHeader.TRACKS ("Tracks") */
148     public void setOutputList(String name) {
149         outputListName = name;
150     }
151 
152     public void conditionsChanged(ConditionsEvent event) {
153         ConditionsSet conditions = getConditionsManager().getConditions("TrackParameters");
154         ConditionsSet simpleconditions = getConditionsManager().getConditions("SimpleTrack");
155         parm = setTrackResolutionTables(conditions, beamSpotConstraint);
156         SmTbl = new SimpleTables(simpleconditions);
157     }
158 }