View Javadoc

1   package org.lcsim.event.util;
2   
3   import hep.physics.jet.FixNumberOfJetsFinder.NumJetsNotFoundException;
4   import hep.physics.jet.JadeEJetFinder;
5   import hep.physics.jet.JetFinder;
6   import hep.physics.vec.BasicHep3Vector;
7   import hep.physics.vec.Hep3Vector;
8   import hep.physics.vec.HepLorentzVector;
9   import java.util.ArrayList;
10  import java.util.Collections;
11  import java.util.HashMap;
12  import java.util.List;
13  import java.util.Map;
14  import org.lcsim.event.EventHeader;
15  import org.lcsim.event.ReconstructedParticle;
16  import org.lcsim.event.Vertex;
17  import org.lcsim.util.Driver;
18  import org.lcsim.util.aida.AIDA;
19  
20  /**
21   * A simple driver which can be used to find jets from ReconstructedParticles.
22   * The resuslting jets are stored in a new collection of ReconstructedParticles.
23   * @author tonyj
24   * @version $Id: JetDriver.java,v 1.4 2011/05/30 18:57:39 cassell Exp $
25   */
26  public class JetDriver extends Driver
27  {
28     private static final Hep3Vector IP = new BasicHep3Vector(0,0,0);
29     private static final String defaultOutputCollectionName  = "Jets";
30     private String inputCollectionName;
31     private String outputCollectionName = defaultOutputCollectionName;
32     private JetFinder finder = defaultJetFinder();
33     private boolean continueOnError = false;
34     
35     /** Creates a new instance of JetFinder with the default properties */
36     public JetDriver()
37     {
38     }
39     
40     public String getInputCollectionName()
41     {
42        return inputCollectionName;
43     }
44     
45     /**
46      * The name of the input collection to use. If not set (or set to <code>null</code> uses
47      * the first collection of ReconstructedParticles found in the event.
48      */
49     
50     public void setInputCollectionName(String inputCollectionName)
51     {
52        this.inputCollectionName = inputCollectionName;
53     }
54     
55     public String getOutputCollectionName()
56     {
57        return outputCollectionName;
58     }
59     
60     /**
61      * The name of the output collection added to the event. If not set, or set to <code>null</code>,
62      * default to "Jets"
63      */
64     public void setOutputCollectionName(String outputCollectionName)
65     {
66        this.outputCollectionName = outputCollectionName == null ? defaultOutputCollectionName : outputCollectionName;
67     }
68     
69     public JetFinder getFinder()
70     {
71        return finder;
72     }
73     
74     /**
75      * Set the jet finding algorithm to use
76      */
77     public void setFinder(JetFinder finder)
78     {
79        this.finder = finder == null ? defaultJetFinder() : finder;
80     }
81  
82     public boolean isContinueOnError()
83     {
84         return continueOnError;
85     }
86  
87     public void setContinueOnError(boolean continueOnError)
88     {
89         this.continueOnError = continueOnError;
90     }
91     
92     protected JetFinder defaultJetFinder()
93     {
94        return new JadeEJetFinder(0.005);
95     }
96     protected void process(EventHeader event)
97     {
98        super.process(event);
99        
100       boolean hist = getHistogramLevel() > 0;
101       
102       // Find the input reconstructed Particles
103       List<ReconstructedParticle> input;
104       if (inputCollectionName ==null)
105       {
106          List<List<ReconstructedParticle>> listOfLists = event.get(ReconstructedParticle.class);
107          if (listOfLists.isEmpty()) return;
108          input = listOfLists.get(0);
109       }
110       else
111       {
112          input = event.get(ReconstructedParticle.class,inputCollectionName);
113       }
114       // Build a list of 4-vectors from the reconstructed particles
115       Map<HepLorentzVector, ReconstructedParticle> map = new HashMap<HepLorentzVector, ReconstructedParticle>();
116       for (ReconstructedParticle p : input)
117       {
118          map.put(p.asFourVector(),p);
119       }
120       
121       // Pass the data to the Jet finder
122       int nJets = 0;
123       try {
124         finder.setEvent(map.keySet());
125         nJets = finder.njets();
126       } catch (NumJetsNotFoundException x) {
127           if (continueOnError) return;
128           else throw x;
129       }
130 
131       
132       if (hist)
133       {
134          AIDA aida = AIDA.defaultInstance();
135          aida.cloud1D("JetDriver/nJets").fill(finder.njets());
136          for (int i=0; i<finder.njets(); i++) aida.cloud1D("JetDriver/particlesPerJet").fill(finder.nParticlesPerJet(i));
137       }
138       
139       // Loop over the output jets and create a new ReconstructedParticle for each one, pointing back to
140       // the original particles
141       
142       List<ReconstructedParticle> output = new ArrayList<ReconstructedParticle>();
143       for (int i=0; i<nJets; i++)
144       {
145          Jet jet = new Jet(finder.jet(i));
146          for (HepLorentzVector pj : (List<HepLorentzVector>) finder.particlesInJet(i))
147          {
148             jet.addParticle(map.get(pj));
149          }
150          output.add(jet);
151       }
152       
153       // Add the list of jets to the event
154       
155       event.put(outputCollectionName, output, ReconstructedParticle.class, 0);
156    }
157    private class Jet implements ReconstructedParticle
158    {
159       private HepLorentzVector fourVector;
160       private List<ReconstructedParticle> particles = new ArrayList<ReconstructedParticle>();
161       private double charge;
162       
163       Jet(HepLorentzVector fourVector)
164       {
165          this.fourVector = fourVector;
166       }
167       
168       public void addTrack(org.lcsim.event.Track track)
169       {
170          throw new UnsupportedOperationException("Add track to jet");
171       }
172       
173       public void addParticleID(org.lcsim.event.ParticleID pid)
174       {
175          throw new UnsupportedOperationException("Add track to jet");
176       }
177       
178       public void addParticle(ReconstructedParticle particle)
179       {
180          particles.add(particle);
181          charge += particle.getCharge();
182       }
183       
184       public int getType()
185       {
186          // Fixme: what should we return for Jet?
187          return 0;
188       }
189       
190       public List<org.lcsim.event.Track> getTracks()
191       {
192          return Collections.EMPTY_LIST;
193       }
194       
195       public hep.physics.vec.Hep3Vector getReferencePoint()
196       {
197          return IP;
198       }
199       
200       public List<ReconstructedParticle> getParticles()
201       {
202          return particles;
203       }
204       
205       public List<org.lcsim.event.ParticleID> getParticleIDs()
206       {
207          return Collections.EMPTY_LIST;
208       }
209       
210       public void addCluster(org.lcsim.event.Cluster cluster)
211       {
212          throw new UnsupportedOperationException("Add track to jet");
213       }
214       
215       public HepLorentzVector asFourVector()
216       {
217          return fourVector;
218       }
219       
220       public double getCharge()
221       {
222          return charge;
223       }
224       
225       public List<org.lcsim.event.Cluster> getClusters()
226       {
227          return Collections.EMPTY_LIST;
228       }
229       
230       public double[] getCovMatrix()
231       {
232          // Fixme: Does it make sense to return anything here?
233          return null;
234       }
235       
236       public double getEnergy()
237       {
238          return fourVector.t();
239       }
240       
241       public double getGoodnessOfPID()
242       {
243          return 0;
244       }
245       
246       public double getMass()
247       {
248          return fourVector.magnitude();
249       }
250       
251       public hep.physics.vec.Hep3Vector getMomentum()
252       {
253          return fourVector.v3();
254       }
255       
256       public org.lcsim.event.ParticleID getParticleIDUsed()
257       {
258          return null;
259       }
260       public Vertex getStartVertex()
261       {
262          return null;
263       }
264    }
265 }