Skip to content

Commit

Permalink
Merge pull request #999 from JeffersonLab/iss993
Browse files Browse the repository at this point in the history
Iss993
  • Loading branch information
cbravo135 authored Jun 16, 2023
2 parents 3e39141 + 4aae850 commit 4312f9f
Show file tree
Hide file tree
Showing 14 changed files with 2,550 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class KFTrackTimePlots extends Driver {
double maxTime = 40;
private boolean removeRandomEvents = true;

private boolean useStyles=false;

public void setRemoveRandomEvents(boolean doit) {
this.removeRandomEvents = doit;
}
Expand Down Expand Up @@ -99,21 +101,22 @@ protected void detectorChanged(Detector detector) {
List<HpsSiSensor> sensors = detector.getSubdetector(subdetectorName).getDetectorElement()
.findDescendants(HpsSiSensor.class);

StyleRegistry styleRegistry = StyleRegistry.getStyleRegistry();
IStyleStore store = styleRegistry.getStore("DefaultStyleStore");
IPlotterStyle style2d = store.getStyle("DefaultColorMapStyle");
style2d.setParameter("hist2DStyle", "colorMap");
style2d.dataStyle().fillStyle().setParameter("colorMapScheme", "rainbow");
// style2d.zAxisStyle().setParameter("scale", "log");
style2d.zAxisStyle().setVisible(false);
style2d.dataBoxStyle().setVisible(false);

IPlotterStyle styleOverlay = store.getStyle("DefaultHistogram1DStyle");
styleOverlay.dataStyle().errorBarStyle().setVisible(true);
styleOverlay.dataStyle().fillStyle().setVisible(false);
styleOverlay.legendBoxStyle().setVisible(false);
styleOverlay.dataStyle().outlineStyle().setVisible(false);

if (useStyles){
StyleRegistry styleRegistry = StyleRegistry.getStyleRegistry();
IStyleStore store = styleRegistry.getStore("DefaultStyleStore");
IPlotterStyle style2d = store.getStyle("DefaultColorMapStyle");
style2d.setParameter("hist2DStyle", "colorMap");
style2d.dataStyle().fillStyle().setParameter("colorMapScheme", "rainbow");
// style2d.zAxisStyle().setParameter("scale", "log");
style2d.zAxisStyle().setVisible(false);
style2d.dataBoxStyle().setVisible(false);

IPlotterStyle styleOverlay = store.getStyle("DefaultHistogram1DStyle");
styleOverlay.dataStyle().errorBarStyle().setVisible(true);
styleOverlay.dataStyle().fillStyle().setVisible(false);
styleOverlay.legendBoxStyle().setVisible(false);
styleOverlay.dataStyle().outlineStyle().setVisible(false);
}
plotters.put("Track Hit Charge: L0-L3", plotterFactory.create("2a Hit Charge: L0-L3"));
plotters.get("Track Hit Charge: L0-L3").createRegions(4, 4);
plotters.put("Track Hit Charge: L4-L6", plotterFactory.create("2b Hit Charge: L4-L6"));
Expand Down Expand Up @@ -218,22 +221,22 @@ protected void detectorChanged(Detector detector) {
.plot(trackTrigTime.get("Bottom"), SvtPlotUtils.createStyle(plotterFactory, "Track Time [ns]", "event time%24 (ns)"));

trackTimeRange.put("Top",
histogramFactory.createHistogram1D("Top Track Time Range", 75, 0, 30.0));
histogramFactory.createHistogram1D("Top Track Time Range", 50, 0, 50.0));
plotters.get("Track Hit Time Range")
.region(0)
.plot(trackTimeRange.get("Top"), SvtPlotUtils.createStyle(plotterFactory, "Track Time Range [ns]", ""));
trackTimeRange.put("Bottom",
histogramFactory.createHistogram1D("Bottom Track Time Range", 75, 0, 30.0));
histogramFactory.createHistogram1D("Bottom Track Time Range", 50, 0, 50.0));
plotters.get("Track Hit Time Range")
.region(1)
.plot(trackTimeRange.get("Bottom"), SvtPlotUtils.createStyle(plotterFactory, "Track Time Range [ns]", ""));
trackTimeMinMax.put("Top",
histogramFactory.createHistogram2D("Top Earliest vs Latest Track Hit Times", 80, -25, 25.0, 80, -25, 25.0));
histogramFactory.createHistogram2D("Top Earliest vs Latest Track Hit Times", 80, -40, 40.0, 80, -40, 40.0));
plotters.get("Track Hit Time Range")
.region(2)
.plot(trackTimeMinMax.get("Top"), SvtPlotUtils.createStyle(plotterFactory, "Earliest Time (ns)", "Latest Time (ns)"));
trackTimeMinMax.put("Bottom",
histogramFactory.createHistogram2D("Bottom Earliest vs Latest Track Hit Times", 80, -25, 25.0, 80, -25, 25.0));
histogramFactory.createHistogram2D("Bottom Earliest vs Latest Track Hit Times", 80, -40, 40.0, 80, -40, 40.0));
plotters.get("Track Hit Time Range")
.region(3)
.plot(trackTimeMinMax.get("Bottom"), SvtPlotUtils.createStyle(plotterFactory, "Earliest Time (ns)", "Latest Time (ns)"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


import org.apache.commons.math3.util.Pair;

import org.hps.conditions.database.DatabaseConditionsManager;
import org.hps.conditions.svt.SvtSyncStatus.SvtSyncStatusCollection;
import org.hps.conditions.svt.SvtTimingConstants;
Expand All @@ -16,6 +26,8 @@
import org.lcsim.recon.cat.util.Const;
import org.lcsim.util.Driver;



// TODO: Add class documentation.
public class RawTrackerHitFitterDriver extends Driver {

Expand All @@ -37,17 +49,22 @@ public class RawTrackerHitFitterDriver extends Driver {
private boolean useTruthTime = false;
private boolean subtractTOF = false;
private boolean subtractTriggerTime = false;
private boolean correctChanT0 = true;
private boolean correctChanT0 = false;
// private boolean correctPhaseDepT0Shift = false;
private boolean subtractRFTime = false;
private boolean correctPerSensorPerPhase=false;
private Boolean syncGood = true;
private Boolean isMC = false;
private Map<Pair,Double> sensorPhaseCalibConstants=new HashMap<Pair,Double>();

private double trigTimeScale = 43.0;// the mean time of the trigger...changes with run period!!! 43.0 is for 2015 Eng. Run

private double trigTimeOffset = 14.0;

private double tsCorrectionScale = 240;

private boolean isFirstEvent=true;

private TrackerHitUtils tkHitUtils=new TrackerHitUtils();

/**
* Report time relative to the nearest expected truth event time.
*
Expand Down Expand Up @@ -93,6 +110,10 @@ public void setSubtractRFTime(boolean subtractRFTime) {
this.subtractRFTime = subtractRFTime;
}

public void setCorrectPerSensorPerPhase(boolean correctPerSensorPerPhase) {
this.correctPerSensorPerPhase = correctPerSensorPerPhase;
}

public void setTrigTimeScale(double time) {
this.trigTimeScale = time;
}
Expand Down Expand Up @@ -161,14 +182,23 @@ protected void detectorChanged(Detector detector) {
syncGood = true;
getLogger().config("svt_sync_statuses was not found.");
}


}

@Override
public void process(EventHeader event) {
if (!event.hasCollection(RawTrackerHit.class, rawHitCollectionName))
// System.out.println(rawHitCollectionName + " does not exist; skipping event");
return;


if(isFirstEvent && correctPerSensorPerPhase){
//this loads the sensorPhaseCalibConstants or returns false if run not in range or calib file not found/broken
//...there should be a better way to get run number...right?
correctPerSensorPerPhase=LoadPerSensorPerPhaseConstants(event);
}
isFirstEvent=false;

jitter = -666;
if (subtractRFTime)
if (event.hasCollection(TriggerTime.class, "TriggerTime")) {
Expand Down Expand Up @@ -238,6 +268,28 @@ public void process(EventHeader event) {
//===> fit.setT0(fit.getT0() - constants.getT0Shift());
fit.setT0(fit.getT0() - sensor.getT0Shift());
}

//////////// mg 5/17/2023 .... this is for reading sensor/phase dependent shifts using database...
/////////// I should make new branch for reading from db because we may want to
/////////// do that in future but for now remove
// if (correctPhaseDepT0Shift){
// Double phaseShifts[]=sensor.getT0PhaseShifts();
// int phase=(int)((event.getTimeStamp()%24)/4);
// fit.setT0(fit.getT0()-phaseShifts[phase]);
//}
// correct time per sensor and event phase (using the constants read in by resource file)
if(correctPerSensorPerPhase){
String sensorName=sensor.getName();
String simpleName=tkHitUtils.getSimpleNameFromSensorName(sensorName);
// System.out.println(sensorName+" --> "+simpleName);
Long evtPhaseL=(event.getTimeStamp() % 24)/4;
Integer evtPhase=evtPhaseL.intValue();
Pair<String,Integer> evtPair=new Pair(simpleName,evtPhase);
Double calConstant=sensorPhaseCalibConstants.get(evtPair);
// System.out.println("shifting t0 by "+calConstant);
fit.setT0(fit.getT0()-calConstant);
}

if (subtractTOF) {
double tof = hit.getDetectorElement().getGeometry().getPosition().magnitude() / (Const.SPEED_OF_LIGHT * Const.nanosecond);
fit.setT0(fit.getT0() - tof);
Expand Down Expand Up @@ -270,4 +322,63 @@ public void process(EventHeader event) {
event.put(fitCollectionName, fits, ShapeFitParameters.class, genericObjectFlags);
event.put(fittedHitCollectionName, hits, FittedRawTrackerHit.class, relationFlags);
}

private boolean LoadPerSensorPerPhaseConstants(EventHeader event){

// get run number from first event
int runNumber= event.getRunNumber();
String infilePreResDir = "/org/hps/recon/tracking/timingCorrections/";
String infilePre = "run";
String infilePost = "_calib_constants_final.txt";
String runString="foobar";

if(runNumber>10000 && runNumber<10464){
runString="10377";
}else if(runNumber>=10464 && runNumber<10660){
runString="10564";
}else if(runNumber>=10660 && runNumber<11000){
runString="10666";
}else if(runNumber>14000 && runNumber<14566){ //2021 constants
runString="14495";
}else if(runNumber>=14566 &&runNumber<14626){
runString="14569";
}else if(runNumber>=14626 && runNumber<14680){
runString="14654";
}else if(runNumber>=14680 && runNumber<15000){
runString="14720";
} else {
getLogger().config("Run is not in range defined by correctPerSensorPerPhase ... corrections not performed");
return false;
}

//ok, overwrite the run string for some individual funny runs
if(runNumber==10687 || runNumber==10711||runNumber==10713||runNumber==10714){
runString=Integer.toString(runNumber);
}



String infile=infilePreResDir+infilePre+runString+infilePost;
InputStream inRatios = this.getClass().getResourceAsStream(infile);
System.out.println("reading in per-sensor per-phase calibs from "+infile);
BufferedReader reader = new BufferedReader(new InputStreamReader(inRatios));
String line;
String delims = "[ ]+";// this will split strings between one or more spaces
try {
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(delims);
System.out.println("sensor_phase = " + tokens[0] + "; constant = " + tokens[1]);
String[] sensor_phase = tokens[0].split("_phase");
// System.out.println("Making Pair::sensor name = "+sensor_phase[0]+", phase = "+Integer.parseInt(sensor_phase[1]));
Pair<String, Integer> senPhPair=new Pair(sensor_phase[0],Integer.parseInt(sensor_phase[1]));
Double constant=Double.parseDouble(tokens[1]);
sensorPhaseCalibConstants.put(senPhPair, constant);
}
} catch (IOException ex) {
getLogger().config("died while reading "+infile);
return false;
}
return true;
}

}
73 changes: 73 additions & 0 deletions tracking/src/main/java/org/hps/recon/tracking/TrackerHitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.lcsim.detector.IDetectorElement;
import org.lcsim.detector.ITransform3D;
Expand Down Expand Up @@ -273,6 +274,78 @@ public static IIdentifier makeSimTrackerHitId(SiSensor sensor) {
return helper.pack(id);

}
//gets the sensor name that hps-java assigns from the simple name used in hpstr (and saved in e.g. the timing calibration resource files)
// example: simple name = "L0T_axial" --> "module_L1b_module_axial_sensor0"
public String getSensorNameFromSimpleName(String simpleName){
//the format of simpleName == LX[T/B]_[axial/stereo]_[[[slot/hole if necessary]]]
//
String[] splitSimpleName=simpleName.split("_");
int lenSimpleName=splitSimpleName.length;
String layerSimple=splitSimpleName[0];
String axSt=splitSimpleName[1];
String slHoSimple="";
if(lenSimpleName == 3 )
slHoSimple=splitSimpleName[2];
String sensorLayer;
if (layerSimple.contains("T"))
sensorLayer=layerSimple.replace("T","t");
else
sensorLayer=layerSimple.replace("B","b");
String sensorSlHo="";
if(lenSimpleName==3){
if(slHoSimple=="pos")
sensorSlHo="slot";
else
sensorSlHo="hole";
}

String sensorName="";
String sensorNamePre="module_"+sensorLayer+"_module_"+axSt+"_";
if(lenSimpleName==4)
sensorName=sensorNamePre+sensorSlHo+"_sensor0";
else
sensorName=sensorNamePre+"_sensor0";
return sensorName;
}
// just the flip of above...get the simple name from sensor name
public String getSimpleNameFromSensorName(String sensorName){
//the format of simpleName == LX[T/B]_[axial/stereo]_[[[slot/hole if necessary]]]
//format for sensor name is: "module_LX[t/b]_module_[axial/stereo]_[[[slot/hole if necessary]]]_sensor0"
String[] splitSensorName=sensorName.split("_");
int lenSensorName=splitSensorName.length;
String layerSensor=splitSensorName[1];
String axSt=splitSensorName[3];
String slHoSensor="";
//decrement the layer number...
Scanner in=new Scanner(layerSensor).useDelimiter("[^0-9]+");
int laySensor=in.nextInt();
int laySimple=laySensor-1;
String layerSensorMid=layerSensor.replace(Integer.toString(laySensor),Integer.toString(laySimple));

if(lenSensorName == 6 )
slHoSensor=splitSensorName[4];
String simpleLayer="";
if (layerSensorMid.contains("t"))
simpleLayer=layerSensorMid.replace("t","T");
else
simpleLayer=layerSensorMid.replace("b","B");
String simpleSlHo="";
// System.out.println(sensorName+" "+slHoSensor);
if(lenSensorName==6){
if(slHoSensor.contains("slot"))
simpleSlHo="pos";
else
simpleSlHo="ele";
}

String simpleName="";
String simpleNamePre=simpleLayer+"_"+axSt;
if(lenSensorName==6)
simpleName=simpleNamePre+"_"+simpleSlHo;
else
simpleName=simpleNamePre;
return simpleName;
}

// public List<SiTrackerHit> stripClusterToSiHits(HelicalTrackStrip strip, List<SiTrackerHit>
// siTrackerHits, boolean stripsInTrackingFrame)
Expand Down
Loading

0 comments on commit 4312f9f

Please sign in to comment.