Skip to content

Commit

Permalink
Merge pull request #252 from JeffersonLab/iss251
Browse files Browse the repository at this point in the history
Iss251 - Fix Recon getting stuck on specific events.
  • Loading branch information
normangraf authored Nov 25, 2017
2 parents 134b902 + 477d02b commit 3acb900
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ public double getNSigmaPosition(Cluster cluster, Track track, double p){

// Get the extrapolated track position at the calorimeter:
TrackState trackStateAtEcal = TrackUtils.getTrackStateAtECal(track);
if(trackStateAtEcal == null){
// Track never made it to the ECAL, so it curled before doing this and probably extrapolateTrackUsingFieldMap aborted.
return Double.MAX_VALUE;
}
Hep3Vector tPos = new BasicHep3Vector(trackStateAtEcal.getReferencePoint());
tPos = CoordinateTransformations.transformVectorToDetector(tPos);

Expand Down
273 changes: 140 additions & 133 deletions tracking/src/main/java/org/hps/recon/tracking/TrackDataDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.lcsim.detector.tracker.silicon.HpsSiSensor;
Expand Down Expand Up @@ -33,25 +34,25 @@ public final class TrackDataDriver extends Driver {

/** logger **/
private static final Logger LOGGER = Logger.getLogger(TrackDataDriver.class.getPackage().getName());


/** The B field map */
FieldMap bFieldMap = null;

/** Collection Names */

/** Collection name of TrackResidualData objects */
private static final String TRK_RESIDUALS_COL_NAME = "TrackResiduals";

/**
* Collection name of LCRelations between a Track and Rotated
* HelicalTrackHits
*/
private static final String ROTATED_HTH_REL_COL_NAME = "RotatedHelicalTrackHitRelations";

/** Collection name of Rotated HelicalTrackHits */
private static final String ROTATED_HTH_COL_NAME = "RotatedHelicalTrackHits";

/**
* Collection name of LCRelations between a Track and TrackResidualsData
* objects.
Expand All @@ -63,23 +64,23 @@ public final class TrackDataDriver extends Driver {
* compact description.
*/
private static final String ECAL_POSITION_CONSTANT_NAME = "ecal_dface";

/** Position of the Ecal face */
private double ecalPosition = 0; // mm

/** Z position to start extrapolation from */
double extStartPos = 700; // mm

/** The extrapolation step size */
double stepSize = 5.0; // mm

/** The default number of layers */
int layerNum = 6;

/** Default constructor */
public TrackDataDriver() {
}

/**
* Set the position along Z where the extrapolation of a track should
* begin.
Expand All @@ -90,7 +91,7 @@ public TrackDataDriver() {
void setExtrapolationStartPosition(double extStartPos) {
this.extStartPos = extStartPos;
}

/**
* Set the extrapolation length between iterations through the magnetic
* field.
Expand All @@ -100,16 +101,16 @@ void setExtrapolationStartPosition(double extStartPos) {
void setStepSize(double stepSize) {
this.stepSize = stepSize;
}

/**
* Set number of tracking layers. Default is 6 layers.
*
*/

public void setLayerNum(int layerNum) {
this.layerNum = layerNum;
}

/**
* Method called by the framework when a new {@link Detector} geometry is
* loaded. This method is called at the beginning of every run and
Expand All @@ -119,14 +120,14 @@ public void setLayerNum(int layerNum) {
*/
@Override
protected void detectorChanged(Detector detector) {

// Get the field map from the detector object
bFieldMap = detector.getFieldMap();

// Get the position of the Ecal from the compact description
ecalPosition = detector.getConstants().get(ECAL_POSITION_CONSTANT_NAME).getValue();
}

/**
* Method called by the framework to process the event.
*
Expand All @@ -151,7 +152,7 @@ protected void process(EventHeader event) {
RelationalTable hitToStrips = TrackUtils.getHitToStripsTable(event);
RelationalTable hitToRotated = TrackUtils.getHitToRotatedTable(event);

// List<HelicalTrackHit> rotatedHths = event.get(HelicalTrackHit.class, ROTATED_HTH_COL_NAME);
// List<HelicalTrackHit> rotatedHths = event.get(HelicalTrackHit.class, ROTATED_HTH_COL_NAME);

// Create a container that will be used to store all TrackData objects.
List<TrackData> trackDataCollection = new ArrayList<TrackData>();
Expand Down Expand Up @@ -191,129 +192,135 @@ protected void process(EventHeader event) {

// Loop over each of the track collections retrieved from the event
for (List<Track> tracks : trackCollections) {

// Loop over all the tracks in the event
for (Track track : tracks) {

totalT0 = 0;
totalHits = 0;
t0Residuals.clear();
sensorLayers.clear();
trackResidualsX.clear();
trackResidualsY.clear();
stereoLayers.clear();
isFirstHit = true;

// TrackState trackStateForResiduals = TrackUtils.getTrackStateAtLocation(track, TrackState.AtLastHit);
// if (trackStateForResiduals == null ) trackStateForResiduals= TrackUtils.getTrackStateAtLocation(track, TrackState.AtIP);
TrackState trackStateForResiduals = TrackUtils.getTrackStateAtLocation(track, TrackState.AtIP);

// Change the position of a HelicalTrackHit to be the corrected
// one.
// FIXME: Now that multiple track collections are being used,
// which track should be used to apply the correction?
// The best track from each of the collections?
// How is the "best track" defined? --OM
//
// Loop over all stereo hits comprising a track
for (TrackerHit rotatedStereoHit : track.getTrackerHits()) {

// Add the stereo layer number associated with the track
// residual
stereoLayers.add(((HelicalTrackHit) rotatedStereoHit).Layer());

// Extrapolate the track to the stereo hit position and
// calculate track residuals
stereoHitPosition = ((HelicalTrackHit) rotatedStereoHit).getCorrectedPosition();
trackPosition = TrackUtils.extrapolateTrack(trackStateForResiduals, stereoHitPosition.x());
xResidual = trackPosition.x() - stereoHitPosition.y();
yResidual = trackPosition.y() - stereoHitPosition.z();
trackResidualsX.add(xResidual);
trackResidualsY.add((float) yResidual);

//
// Change the persisted position of both
// RotatedHelicalTrackHits and HelicalTrackHits to the
// corrected position.
try {
totalT0 = 0;
totalHits = 0;
t0Residuals.clear();
sensorLayers.clear();
trackResidualsX.clear();
trackResidualsY.clear();
stereoLayers.clear();
isFirstHit = true;

// TrackState trackStateForResiduals = TrackUtils.getTrackStateAtLocation(track, TrackState.AtLastHit);
// if (trackStateForResiduals == null ) trackStateForResiduals= TrackUtils.getTrackStateAtLocation(track, TrackState.AtIP);
TrackState trackStateForResiduals = TrackUtils.getTrackStateAtLocation(track, TrackState.AtIP);

// Change the position of a HelicalTrackHit to be the corrected
// one.
// FIXME: Now that multiple track collections are being used,
// which track should be used to apply the correction?
// The best track from each of the collections?
// How is the "best track" defined? --OM
//

// Get the HelicalTrackHit corresponding to the
// RotatedHelicalTrackHit associated with a track
helicalTrackHit = (HelicalTrackHit) hitToRotated.from(rotatedStereoHit);
((HelicalTrackHit) rotatedStereoHit).setPosition(stereoHitPosition.v());
stereoHitPosition = CoordinateTransformations.transformVectorToDetector(stereoHitPosition);
helicalTrackHit.setPosition(stereoHitPosition.v());

// Loop over the clusters comprising the stereo hit
for (HelicalTrackStrip cluster : ((HelicalTrackCross) rotatedStereoHit).getStrips()) {

totalT0 += cluster.time();
totalHits++;

if (isFirstHit) {
sensor = (HpsSiSensor) ((RawTrackerHit) cluster.rawhits().get(0)).getDetectorElement();
if (sensor.isTopLayer()) {
trackerVolume = 0;
} else if (sensor.isBottomLayer()) {
trackerVolume = 1;
// Loop over all stereo hits comprising a track
for (TrackerHit rotatedStereoHit : track.getTrackerHits()) {

// Add the stereo layer number associated with the track
// residual
stereoLayers.add(((HelicalTrackHit) rotatedStereoHit).Layer());

// Extrapolate the track to the stereo hit position and
// calculate track residuals
stereoHitPosition = ((HelicalTrackHit) rotatedStereoHit).getCorrectedPosition();
trackPosition = TrackUtils.extrapolateTrack(trackStateForResiduals, stereoHitPosition.x());
xResidual = trackPosition.x() - stereoHitPosition.y();
yResidual = trackPosition.y() - stereoHitPosition.z();
trackResidualsX.add(xResidual);
trackResidualsY.add((float) yResidual);

//
// Change the persisted position of both
// RotatedHelicalTrackHits and HelicalTrackHits to the
// corrected position.
//

// Get the HelicalTrackHit corresponding to the
// RotatedHelicalTrackHit associated with a track
helicalTrackHit = (HelicalTrackHit) hitToRotated.from(rotatedStereoHit);
((HelicalTrackHit) rotatedStereoHit).setPosition(stereoHitPosition.v());
stereoHitPosition = CoordinateTransformations.transformVectorToDetector(stereoHitPosition);
helicalTrackHit.setPosition(stereoHitPosition.v());

// Loop over the clusters comprising the stereo hit
for (HelicalTrackStrip cluster : ((HelicalTrackCross) rotatedStereoHit).getStrips()) {

totalT0 += cluster.time();
totalHits++;

if (isFirstHit) {
sensor = (HpsSiSensor) ((RawTrackerHit) cluster.rawhits().get(0)).getDetectorElement();
if (sensor.isTopLayer()) {
trackerVolume = 0;
} else if (sensor.isBottomLayer()) {
trackerVolume = 1;
}
isFirstHit = false;
}
isFirstHit = false;
}
}
}

//
// Add a track state that contains the extrapolated track position and
// parameters at the face of the Ecal.
//
LOGGER.fine("Extrapolating track with type " + Integer.toString(track.getType()) );

// Extrapolate the track to the face of the Ecal and get the TrackState
if( TrackType.isGBL(track.getType())) {
TrackState stateIP = TrackUtils.getTrackStateAtLocation(track, TrackState.AtIP);
if( stateIP == null)
throw new RuntimeException("IP track state for GBL track was not found");
TrackState stateEcalIP = TrackUtils.extrapolateTrackUsingFieldMap(stateIP, extStartPos, ecalPosition, stepSize, bFieldMap);
track.getTrackStates().add(stateEcalIP);

} else {
LOGGER.fine("Extrapolate seed track to ECal from vertex");
TrackState state = TrackUtils.extrapolateTrackUsingFieldMap(track, extStartPos, ecalPosition, stepSize, bFieldMap);
track.getTrackStates().add(state);
}

LOGGER.fine(Integer.toString(track.getTrackStates().size()) + " track states for this track at this point:");
for(TrackState state : track.getTrackStates()) {
String s = "type " + Integer.toString(track.getType()) + " location " + Integer.toString(state.getLocation()) + " refPoint (" + state.getReferencePoint()[0] + " " + state.getReferencePoint()[1] + " " + state.getReferencePoint()[2] + ") " + " params: ";
for(int i=0;i<5;++i) s += String.format(" %f", state.getParameter(i));
LOGGER.fine(s);
}


// The track time is the mean t0 of hits on a track
trackTime = totalT0 / totalHits;

// Calculate the track isolation constants for each of the
// layers
Double[] isolations = TrackUtils.getIsolations(track, hitToStrips, hitToRotated,layerNum);
double qualityArray[] = new double[isolations.length];
for (int i = 0; i < isolations.length; i++) {
qualityArray[i] = isolations[i] == null ? -99999999.0 : isolations[i];
//
// Add a track state that contains the extrapolated track position and
// parameters at the face of the Ecal.
//
LOGGER.fine("Extrapolating track with type " + Integer.toString(track.getType()) );

// Extrapolate the track to the face of the Ecal and get the TrackState
if( TrackType.isGBL(track.getType())) {
TrackState stateIP = TrackUtils.getTrackStateAtLocation(track, TrackState.AtIP);
if( stateIP == null)
throw new RuntimeException("IP track state for GBL track was not found");
TrackState stateEcalIP = TrackUtils.extrapolateTrackUsingFieldMap(stateIP, extStartPos, ecalPosition, stepSize, bFieldMap);
track.getTrackStates().add(stateEcalIP);

} else {
LOGGER.fine("Extrapolate seed track to ECal from vertex");
TrackState state = TrackUtils.extrapolateTrackUsingFieldMap(track, extStartPos, ecalPosition, stepSize, bFieldMap);
track.getTrackStates().add(state);
}

LOGGER.fine(Integer.toString(track.getTrackStates().size()) + " track states for this track at this point:");
for(TrackState state : track.getTrackStates()) {
String s = "type " + Integer.toString(track.getType()) + " location " + Integer.toString(state.getLocation()) + " refPoint (" + state.getReferencePoint()[0] + " " + state.getReferencePoint()[1] + " " + state.getReferencePoint()[2] + ") " + " params: ";
for(int i=0;i<5;++i) s += String.format(" %f", state.getParameter(i));
LOGGER.fine(s);
}


// The track time is the mean t0 of hits on a track
trackTime = totalT0 / totalHits;

// Calculate the track isolation constants for each of the
// layers
Double[] isolations = TrackUtils.getIsolations(track, hitToStrips, hitToRotated,layerNum);
double qualityArray[] = new double[isolations.length];
for (int i = 0; i < isolations.length; i++) {
qualityArray[i] = isolations[i] == null ? -99999999.0 : isolations[i];
}

// Create a new TrackData object and add it to the event
TrackData trackData = new TrackData(trackerVolume, trackTime, qualityArray);
trackDataCollection.add(trackData);
trackDataRelations.add(new BaseLCRelation(trackData, track));

// Create a new TrackResidualsData object and add it to the event
TrackResidualsData trackResiduals = new TrackResidualsData((int) trackerVolume, stereoLayers,
trackResidualsX, trackResidualsY);
trackResidualsCollection.add(trackResiduals);
trackToTrackResidualsRelations.add(new BaseLCRelation(trackResiduals, track));

}catch( Exception e){
LOGGER.log(Level.SEVERE, e.getMessage(), e);
continue;
}

// Create a new TrackData object and add it to the event
TrackData trackData = new TrackData(trackerVolume, trackTime, qualityArray);
trackDataCollection.add(trackData);
trackDataRelations.add(new BaseLCRelation(trackData, track));

// Create a new TrackResidualsData object and add it to the event
TrackResidualsData trackResiduals = new TrackResidualsData((int) trackerVolume, stereoLayers,
trackResidualsX, trackResidualsY);
trackResidualsCollection.add(trackResiduals);
trackToTrackResidualsRelations.add(new BaseLCRelation(trackResiduals, track));
}
}

// Add all collections to the event
event.put(TrackData.TRACK_DATA_COLLECTION, trackDataCollection, TrackData.class, 0);
event.put(TrackData.TRACK_DATA_RELATION_COLLECTION, trackDataRelations, LCRelation.class, 0);
Expand Down
Loading

0 comments on commit 3acb900

Please sign in to comment.