Author: [log in to unmask] Date: Fri May 22 13:39:10 2015 New Revision: 3615 Log: Add copy constructor and implementation of clone to BaseCluster. LCSIM-247 Modified: projects/lcsim/trunk/event-model/src/main/java/org/lcsim/event/base/BaseCluster.java Modified: projects/lcsim/trunk/event-model/src/main/java/org/lcsim/event/base/BaseCluster.java ============================================================================= --- projects/lcsim/trunk/event-model/src/main/java/org/lcsim/event/base/BaseCluster.java (original) +++ projects/lcsim/trunk/event-model/src/main/java/org/lcsim/event/base/BaseCluster.java Fri May 22 13:39:10 2015 @@ -22,7 +22,8 @@ * <li>Added access to particle ID based on the official API.</li> * <li>Added a few utility methods for adding lists of hits and clusters.</li> * <li>Added a method for setting a hit contribution that is different from the corrected energy.</li> - * <li>Simplified the {@link #calculateProperties()} method so it doesn't do a bunch of array copies.</li> + * <li>Simplified the {@link #calculateProperties()} method so that it doesn't do a bunch of array copies.</li> + * <li>Added a copy constructor and implementation of {@link #clone()} method.</li> * </ul> * * @see org.lcsim.event.Cluster @@ -71,15 +72,16 @@ * The energy given here will override the value calculated from the hits. If this is not * desired, then another constructor should be used instead. This constructor does not * allow setting hit contributions that are different from the hit energies. - * @param hits The list of hits. - * @param energy The total energy. - * @param energyError The energy error. - * @param position The position. - * @param positionError The position error. - * @param iphi The intrinsic phi. - * @param itheta The intrinsic theta. - * @param directionError The direction error. - * @param shapeParameters The shape parameters. + * + * @param hits the list of hits + * @param energy the total energy + * @param energyError the energy error + * @param position the position + * @param positionError the position error + * @param iphi the intrinsic phi + * @param itheta the intrinsic theta + * @param directionError the direction error + * @param shapeParameters the shape parameters */ public BaseCluster( List<CalorimeterHit> hits, @@ -116,9 +118,72 @@ } /** + * Copy constructor, which will create new arrays and lists in this object so the copied cluster's + * data is not incorrectly referenced. + * <p> + * The hits in the <code>CalorimeterHit</code> list are not themselves copied. + * + * @param cluster the <code>BaseCluster</code> to copy + */ + public BaseCluster(BaseCluster cluster) { + + // Copy the hit list; the hits themselves are *not* copied. + for (CalorimeterHit hit : cluster.getCalorimeterHits()) { + this.hits.add(hit); + } + + // Copy hit contributions. + this.hitContributions = new ArrayList<Double>(); + for (Double contribution : cluster.getHitContributions()) { + this.hitContributions.add(contribution); + } + + // Set energy and energy error. + this.energy = cluster.getEnergy(); + this.energyError = cluster.getEnergyError(); + + // Copy position into new array. + this.position = new double[cluster.getPosition().length]; + System.arraycopy(cluster.getPosition(), 0, this.position, 0, cluster.getPosition().length); + + // Copy position error into new array. + this.positionError = new double[cluster.getPositionError().length]; + System.arraycopy(cluster.getPositionError(), 0, this.positionError, 0, cluster.getPositionError().length); + + // Set iphi and itheta. + this.iphi = cluster.getIPhi(); + this.itheta = cluster.getITheta(); + + // Copy direction error into new array. + this.directionError = new double[cluster.getDirectionError().length]; + System.arraycopy(cluster.getDirectionError(), 0, this.directionError, 0, cluster.getDirectionError().length); + + // Copy shape parameters into new array. + this.shapeParameters = new double[cluster.getShape().length]; + System.arraycopy(cluster.getShape(), 0, this.shapeParameters, 0, cluster.getShape().length); + + // Copy type and PID. + this.type = cluster.getType(); + this.pid = cluster.getParticleId(); + + // Set needs property calculation. + this.needsPropertyCalculation = cluster.needsPropertyCalculation(); + } + + /** + * Clone to a new object using the copy constructor. + * + * @return the new object + */ + public Object clone() { + return new BaseCluster(this); + } + + /** * Basic constructor that takes a list of hits. * It will apply the default energy calculation. - * @param hits The list of CalorimeterHits. + * + * @param hits the list of CalorimeterHits */ public BaseCluster(List<CalorimeterHit> hits) { addHits(hits); @@ -132,8 +197,9 @@ /** * Get the list of CalorimeterHits of this cluster. + * * The hits are not necessarily unique in the list. - * @return The hits comprising the cluster. + * @return the hits comprising the cluster */ public List<CalorimeterHit> getCalorimeterHits() { return hits; @@ -141,7 +207,7 @@ /** * Get the clusters that are part of this cluster. - * @return The clusters comprising the cluster. + * @return the clusters comprising the cluster */ public List<Cluster> getClusters() { return clusters; @@ -150,7 +216,8 @@ /** * Get the energy of the cluster, which by default will be the * sum of the CalorimeterHit corrected energy values. - * @return The energy of the cluster. + * + * @return the energy of the cluster */ public double getEnergy() { return energy; @@ -158,7 +225,8 @@ /** * Get the energy error. - * @return The energy error. + * + * @return the energy error */ public double getEnergyError() { return energyError; @@ -171,7 +239,8 @@ * but the contributions may be set to different values * on a per hit basis using the {@link #addHit(CalorimeterHit, double)} * method. - * @return The individual hit contribution energies. + * + * @return the individual hit contribution energies */ public double[] getHitContributions() { double[] arrayCopy = new double[hitContributions.size()]; @@ -184,7 +253,8 @@ /** * Get the list of subdetector energy contributions. * The ordering and meaning of this array is unspecified by this class. - * @return The list of subdetector energy contributions. + * + * @return the list of subdetector energy contributions */ public double[] getSubdetectorEnergies() { return subdetectorEnergies; @@ -192,7 +262,7 @@ /** * Get a value defining the type of this cluster. - * @return The type of this cluster. + * @return the type of this cluster */ public int getType() { return type; @@ -201,7 +271,8 @@ /** * Get the number of hits in the cluster, including hits in sub-clusters. * Hits belonging to more than one cluster are counted once. - * @return The size of the cluster. + * + * @return the size of the cluster */ public int getSize() { Set<CalorimeterHit> hitSet = new HashSet<CalorimeterHit>(this.hits); @@ -215,7 +286,8 @@ /** * Get the intrinsic phi direction of the cluster. - * @return The intrinsic phi direction of the cluster. + * + * @return the intrinsic phi direction of the cluster */ public double getIPhi() { checkCalculateProperties(); @@ -224,7 +296,7 @@ /** * Get the intrinsic theta direction of the cluster. - * @return The intrinsic theta direction of the cluster. + * @return the intrinsic theta direction of the cluster */ public double getITheta() { checkCalculateProperties(); @@ -233,7 +305,7 @@ /** * Get the direction error of the cluster as a double array of size 6. - * @return The direction error of the cluster. + * @return the direction error of the cluster */ public double[] getDirectionError() { checkCalculateProperties(); @@ -242,7 +314,7 @@ /** * Get the position of the cluster as a double array of size 3. - * @return The position of the cluster. + * @return the position of the cluster */ public double[] getPosition() { checkCalculateProperties(); @@ -251,7 +323,8 @@ /** * Get the position error of the cluster as a double array of size 6. - * @return The position error of the cluster. + * + * @return the position error of the cluster */ public double[] getPositionError() { checkCalculateProperties(); @@ -260,7 +333,8 @@ /** * Get the shape parameters of the cluster as a double array of unspecified size. - * @return The shape parameters of the cluster. + * + * @return the shape parameters of the cluster */ public double[] getShape() { checkCalculateProperties(); @@ -269,7 +343,8 @@ /** * Get the PDG ID of the particle hypothesis. - * @return The PID. + * + * @return the PID */ public int getParticleId() { return pid; @@ -283,8 +358,9 @@ /** * Set the position of the cluster. - * @param position The position of the cluster. - * @throws IllegalArgumentException if array is wrong size. + * + * @param position the position of the cluster + * @throws IllegalArgumentException if array is wrong size */ public void setPosition(double[] position) { if (position.length != 3) { @@ -295,8 +371,9 @@ /** * Set the position error of the cluster. - * @param positionError The position error of the cluster. - * @throws IllegalArgumentException if array is wrong size. + * + * @param positionError the position error of the cluster + * @throws IllegalArgumentException if array is wrong size */ public void setPositionError(double[] positionError) { if (positionError.length != 6) { @@ -307,7 +384,8 @@ /** * Set the intrinsic phi of the cluster. - * @param iphi The intrinsic phi of the cluster. + * + * @param iphi the intrinsic phi of the cluster */ public void setIPhi(double iphi) { this.iphi = iphi; @@ -315,6 +393,7 @@ /** * Set the intrinsic theta of the cluster. + * * @param iphi The intrinsic theta of the cluster. */ public void setITheta(double itheta) { @@ -322,9 +401,10 @@ } /** - * Set the direction error of the cluster. - * @param directionError The direction error of the cluster. - * @throws IllegalArgumentException if array is wrong size. + * Set the direction error of the cluster. + * + * @param directionError the direction error of the cluster + * @throws IllegalArgumentException if array is wrong size */ public void setDirectionError(double[] directionError) { if (directionError.length != 6) { @@ -335,7 +415,8 @@ /** * Set the shape parameters of the cluster. - * @param shapeParameters The shape parameters. + * + * @param shapeParameters the shape parameters */ public void setShapeParameters(double[] shapeParameters) { this.shapeParameters = shapeParameters; @@ -343,7 +424,8 @@ /** * Set the type of the cluster. - * @param type The type of the cluster. + * + * @param type the type of the cluster */ public void setType(int type) { this.type = type; @@ -351,7 +433,8 @@ /** * Get the PDG ID of the particle hypothesis. - * @return The PID. + * + * @return the PID */ public void setParticleId(int pid) { this.pid = pid; @@ -361,7 +444,8 @@ * Set a total energy of this cluster, overriding any energy * value that may have been automatically calculated from * hit energies. - * @param energy The total energy of this cluster. + * + * @param energy the total energy of this cluster */ public void setEnergy(double energy) { this.energy = energy; @@ -369,7 +453,8 @@ /** * Set the error on the energy measurement. - * @param energyError The error on the energ measurement. + * + * @param energyError the error on the energ measurement */ public void setEnergyError(double energyError) { this.energyError = energyError; @@ -377,7 +462,8 @@ /** * Set the subdetector energies. - * @param subdetectorEnergies The subdetector energies. + * + * @param subdetectorEnergies the subdetector energies */ public void setSubdetectorEnergies(double[] subdetectorEnergies) { this.subdetectorEnergies = subdetectorEnergies; @@ -391,7 +477,8 @@ /** * Add a list of hits to the cluster. - * @param The list of hits to add. + * + * @param the list of hits to add */ public void addHits(List<CalorimeterHit> hits) { for (CalorimeterHit hit : hits) { @@ -401,7 +488,8 @@ /** * Add a list of sub-clusters to the cluster. - * @param The list of clusters to add. + * + * @param the list of clusters to add */ public void addClusters(List<Cluster> clusters) { for (Cluster cluster : clusters) { @@ -411,7 +499,8 @@ /** * Add a sub-cluster to the cluster. - * @param cluster The cluster to add. + * + * @param cluster the cluster to add */ public void addCluster(Cluster cluster) { clusters.add(cluster); @@ -426,7 +515,8 @@ /** * Add a hit to the cluster with default energy contribution. - * @param hit The hit to add. + * + * @param hit the hit to add */ public void addHit(CalorimeterHit hit) { addHit(hit, hit.getCorrectedEnergy()); @@ -435,8 +525,9 @@ /** * Add a hit to the cluster with specified energy contribution. - * @param hit The hit to add. - * @param contribution The energy contribution of the hit [GeV]. + * + * @param hit the hit to add + * @param contribution the energy contribution of the hit [GeV] */ public void addHit(CalorimeterHit hit, double contribution) { hits.add(hit); @@ -447,7 +538,8 @@ /** * Remove a hit from the cluster. - * @param hit The hit to remove. + * + * @param hit the hit to remove */ public void removeHit(CalorimeterHit hit) { int index = hits.indexOf(hit); @@ -475,23 +567,26 @@ /** * Set a property calculator for computing position, etc. - * @param calc The property calculator. + * + * @param calc the property calculator */ public void setPropertyCalculator(ClusterPropertyCalculator calc) { this.calc = calc; } /** - * True if property calculator is set. - * @return True if property calculator is set. + * Return <code>true</code> if property calculator is set. + * + * @return <code>true</code> if property calculator is set */ public boolean hasPropertyCalculator() { return calc != null; } /** - * True if cluster is flagged as needed a property calculation. - * @return True if cluster needs property calculation. + * Return <code>true</code> if cluster is flagged as needed a property calculation. + * + * @return <code>true</code> if cluster needs property calculation */ public boolean needsPropertyCalculation() { return needsPropertyCalculation; @@ -499,7 +594,8 @@ /** * Manually set whether the cluster needs property calculation. - * @param needsPropertyCalculation Whether the cluster needs property calculation. + * + * @param needsPropertyCalculation <code>true</code> if cluster needs property calculation */ public void setNeedsPropertyCalculation(boolean needsPropertyCalculation) { this.needsPropertyCalculation = needsPropertyCalculation; @@ -509,11 +605,11 @@ * * Calculate the properties of this cluster using the current * <code>ClusterPropertyCalculator</code>. The calculated properties will be - * set on the following class variables:<br/> + * set on the following class variables:<br/> * {@link #position}, {@link #positionError}, * {@link #iphi}, {@link #itheta}, {@link #directionError}, - * and {@link #shapeParameters}. The cluster will then be flagged - * as not needing a property calculation in its current state. + * and {@link #shapeParameters}. Then {@link #needsPropertyCalculation} will be + * set to <code>false</code> until the cluster's state changes. */ public void calculateProperties() { if (!hasPropertyCalculator()) { ######################################################################## Use REPLY-ALL to reply to list To unsubscribe from the LCDET-SVN list, click the following link: https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=LCDET-SVN&A=1