Skip to content

Commit 26aa37b

Browse files
authored
New T2D function for ALERT AHDC (#1178)
* Added new T2D functional form and new wire-by-wire T2D database table call: time_to_distance_wire * Removed old T2D database calls and added some checks on NaN and null parameters
1 parent b819ade commit 26aa37b

File tree

3 files changed

+240
-231
lines changed

3 files changed

+240
-231
lines changed

reconstruction/alert/src/main/java/org/jlab/rec/ahdc/Hit/HitReader.java

Lines changed: 193 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -19,194 +19,199 @@ public HitReader(DataEvent event, AlertDCDetector detector, boolean simulation)
1919
fetch_AHDCHits(event, detector);
2020
if (simulation) fetch_TrueAHDCHits(event);
2121
}
22-
23-
public final void fetch_AHDCHits(DataEvent event, AlertDCDetector detector) {
24-
25-
ArrayList<Hit> hits = new ArrayList<>();
26-
27-
if (!event.hasBank("AHDC::adc")) {
28-
this.set_AHDCHits(hits);
29-
return;
30-
}
31-
32-
double startTime = 0.0;
33-
if (event.hasBank("REC::Event") && !sim) {
34-
DataBank bankRecEvent = event.getBank("REC::Event");
35-
startTime = bankRecEvent.getFloat("startTime", 0);
36-
}
37-
38-
RawDataBank bankDGTZ = new RawDataBank("AHDC::adc");
39-
bankDGTZ.read(event);
40-
41-
for (int i = 0; i < bankDGTZ.rows(); i++) {
42-
43-
int id = bankDGTZ.trueIndex(i) + 1;
44-
int number = bankDGTZ.getByte("layer", i); // e.g. 11,12,21,... (this matches CCDB "layer")
45-
int layer = number % 10;
46-
int superlayer = (number % 100) / 10;
47-
int sector = bankDGTZ.getInt("sector", i);
48-
int wire = bankDGTZ.getShort("component", i);
49-
50-
// RAW quantities from bank
51-
double adcRaw = bankDGTZ.getInt("ADC", i);
52-
double leadingEdgeTime = bankDGTZ.getFloat("leadingEdgeTime", i);
53-
double timeOverThreshold = bankDGTZ.getFloat("timeOverThreshold", i);
54-
double adcOffset = bankDGTZ.getFloat("ped", i);
55-
int wfType = bankDGTZ.getShort("wfType", i);
56-
57-
// CCDB key
58-
int key_value = sector * 10000 + number * 100 + wire;
59-
60-
// -----------------------------
61-
// Raw hit cuts
62-
// -----------------------------
63-
// double[] rawHitCuts = CalibrationConstantsLoader.AHDC_RAW_HIT_CUTS.get(key_value);
64-
//if (rawHitCuts == null) continue;
65-
66-
67-
double[] rawHitCuts = CalibrationConstantsLoader.AHDC_RAW_HIT_CUTS.get(key_value);
68-
if (rawHitCuts == null) {throw new IllegalStateException("Missing CCDB table /calibration/alert/ahdc/raw_hit_cuts for key=" + key_value+ " (check run/variation + key mapping)");
69-
}
22+
public double T2Dfunction(int key, double time){
23+
double[] time2distance = CalibrationConstantsLoader.AHDC_TIME_TO_DISTANCE_WIRE.get(key);
24+
if (time2distance == null){
25+
throw new IllegalStateException("Missing CCDB /calibration/alert/ahdc/time_to_distance_wire for key=" + key + " (check run/variation + key mapping)");
26+
}
27+
28+
if (time2distance[7] == 0.0 || time2distance[9] == 0.0){
29+
throw new IllegalStateException("Incorrect table values in CCDB /calibration/alert/ahdc/time_to_distance_wire. t1_width ("+ time2distance[7] +") or t2_width ("+time2distance[9]+") for key: " + key + " must not equal zero. Please check database table issues.");
30+
}
31+
32+
// T2D function consists of three 1st order polynomials (p1, p2, p3) and two transition functions (t1, t2).
33+
34+
double p1 = (time2distance[0] + time2distance[1]*time);
35+
double p2 = (time2distance[2] + time2distance[3]*time);
36+
double p3 = (time2distance[4] + time2distance[5]*time);
37+
38+
double t1 = 1.0/(1.0 + Math.exp(-(time - time2distance[6])/time2distance[7]));
39+
double t2 = 1.0/(1.0 + Math.exp(-(time - time2distance[8])/time2distance[9]));
40+
41+
double retval = (p1)*(1.0 - t1) + (t1)*(p2)*(1.0 - t2) + (t2)*(p3);
42+
return retval;
43+
}
44+
45+
public final void fetch_AHDCHits(DataEvent event, AlertDCDetector detector) {
46+
47+
ArrayList<Hit> hits = new ArrayList<>();
48+
49+
if (!event.hasBank("AHDC::adc")) {
50+
this.set_AHDCHits(hits);
51+
return;
52+
}
53+
54+
double startTime = 0.0;
55+
if (event.hasBank("REC::Event") && !sim) {
56+
DataBank bankRecEvent = event.getBank("REC::Event");
57+
startTime = bankRecEvent.getFloat("startTime", 0);
58+
}
59+
60+
RawDataBank bankDGTZ = new RawDataBank("AHDC::adc");
61+
bankDGTZ.read(event);
62+
63+
for (int i = 0; i < bankDGTZ.rows(); i++) {
64+
65+
int id = bankDGTZ.trueIndex(i) + 1;
66+
int number = bankDGTZ.getByte("layer", i); // e.g. 11,12,21,... (this matches CCDB "layer")
67+
int layer = number % 10;
68+
int superlayer = (number % 100) / 10;
69+
int sector = bankDGTZ.getInt("sector", i);
70+
int wire = bankDGTZ.getShort("component", i);
71+
72+
// RAW quantities from bank
73+
double adcRaw = bankDGTZ.getInt("ADC", i);
74+
double leadingEdgeTime = bankDGTZ.getFloat("leadingEdgeTime", i);
75+
double timeOverThreshold = bankDGTZ.getFloat("timeOverThreshold", i);
76+
double adcOffset = bankDGTZ.getFloat("ped", i);
77+
int wfType = bankDGTZ.getShort("wfType", i);
78+
79+
// CCDB key
80+
int key_value = sector * 10000 + number * 100 + wire;
81+
82+
// -----------------------------
83+
// Raw hit cuts
84+
// -----------------------------
85+
// double[] rawHitCuts = CalibrationConstantsLoader.AHDC_RAW_HIT_CUTS.get(key_value);
86+
//if (rawHitCuts == null) continue;
87+
88+
89+
double[] rawHitCuts = CalibrationConstantsLoader.AHDC_RAW_HIT_CUTS.get(key_value);
90+
if (rawHitCuts == null) {throw new IllegalStateException("Missing CCDB table /calibration/alert/ahdc/raw_hit_cuts for key=" + key_value+ " (check run/variation + key mapping)");
91+
}
7092

71-
double t_min = rawHitCuts[0];
72-
double t_max = rawHitCuts[1];
73-
double tot_min = rawHitCuts[2];
74-
double tot_max = rawHitCuts[3];
75-
double adc_min = rawHitCuts[4];
76-
double adc_max = rawHitCuts[5];
77-
double ped_min = rawHitCuts[6];
78-
double ped_max = rawHitCuts[7];
79-
80-
// -----------------------------
81-
// Time calibration + t->d
82-
// -----------------------------
83-
//double[] timeOffsets = CalibrationConstantsLoader.AHDC_TIME_OFFSETS.get(key_value);
84-
//if (timeOffsets == null) continue;
85-
86-
// double[] timeOffsets = CalibrationConstantsLoader.AHDC_TIME_OFFSETS.get(key_value);
87-
//if (timeOffsets == null) {
88-
//throw new IllegalStateException("Missing AHDC time_offsets for key=" + key_value);
89-
//}
90-
91-
double[] timeOffsets = CalibrationConstantsLoader.AHDC_TIME_OFFSETS.get(key_value);
93+
double t_min = rawHitCuts[0];
94+
double t_max = rawHitCuts[1];
95+
double tot_min = rawHitCuts[2];
96+
double tot_max = rawHitCuts[3];
97+
double adc_min = rawHitCuts[4];
98+
double adc_max = rawHitCuts[5];
99+
double ped_min = rawHitCuts[6];
100+
double ped_max = rawHitCuts[7];
101+
102+
// -----------------------------
103+
// Time calibration + t->d
104+
// -----------------------------
105+
//double[] timeOffsets = CalibrationConstantsLoader.AHDC_TIME_OFFSETS.get(key_value);
106+
//if (timeOffsets == null) continue;
107+
108+
// double[] timeOffsets = CalibrationConstantsLoader.AHDC_TIME_OFFSETS.get(key_value);
109+
//if (timeOffsets == null) {
110+
//throw new IllegalStateException("Missing AHDC time_offsets for key=" + key_value);
111+
//}
112+
113+
double[] timeOffsets = CalibrationConstantsLoader.AHDC_TIME_OFFSETS.get(key_value);
92114

93-
if (timeOffsets == null) {
94-
throw new IllegalStateException("Missing CCDB /calibration/alert/ahdc/time_offsets for key=" + key_value + " (check run/variation + key mapping)");
95-
}
96-
97-
98-
99-
100-
double[] time2distance = CalibrationConstantsLoader.AHDC_TIME_TO_DISTANCE.get(10101);
101-
if (time2distance == null) continue;
102-
103-
double t0 = timeOffsets[0];
104-
105-
double p0 = time2distance[0];
106-
double p1 = time2distance[1];
107-
double p2 = time2distance[2];
108-
double p3 = time2distance[3];
109-
double p4 = time2distance[4];
110-
double p5 = time2distance[5];
111-
112-
double time = leadingEdgeTime - t0 - startTime;
113-
114-
// -----------------------------
115-
// ToT correction (new CCDB)
116-
// convention: ToT_corr = ToT_raw * totCorr
117-
// -----------------------------
118-
double totUsed = timeOverThreshold;
119-
if (!sim) {
120-
double[] totArr = CalibrationConstantsLoader.AHDC_TIME_OVER_THRESHOLD.get(key_value);
121-
if (totArr != null && totArr.length > 0) {
122-
double totCorr = totArr[0];
123-
totUsed = timeOverThreshold * totCorr;
124-
}
125-
}
126-
127-
// -----------------------------
128-
// Hit selection (cuts)
129-
// NOTE: we cut on totUsed (corrected ToT). If you want RAW-ToT cuts,
130-
// replace totUsed with timeOverThreshold in the condition.
131-
// -----------------------------
132-
boolean passCuts =
133-
(wfType <= 2) &&
134-
(adcRaw >= adc_min) && (adcRaw <= adc_max) &&
135-
(time >= t_min) && (time <= t_max) &&
136-
(timeOverThreshold >= tot_min) && (timeOverThreshold <= tot_max)&&
137-
//(totUsed >= tot_min) && (totUsed <= tot_max) &&
138-
(adcOffset >= ped_min) && (adcOffset <= ped_max);
139-
140-
if (!passCuts && !sim) continue;
141-
142-
// -----------------------------
143-
// DOCA from calibrated time
144-
// -----------------------------
145-
double doca = p0
146-
+ p1*Math.pow(time, 1.0)
147-
+ p2*Math.pow(time, 2.0)
148-
+ p3*Math.pow(time, 3.0)
149-
+ p4*Math.pow(time, 4.0)
150-
+ p5*Math.pow(time, 5.0);
151-
152-
if (time < 0) doca = 0.0;
153-
154-
// -----------------------------
155-
// ADC gain calibration (new gains schema: gainCorr is index 0)
156-
// convention: ADC_cal = ADC_raw * gainCorr
157-
// -----------------------------
158-
double adcCal = adcRaw;
159-
if (!sim) {
160-
double[] gainArr = CalibrationConstantsLoader.AHDC_ADC_GAINS.get(key_value);
161-
if (gainArr != null && gainArr.length > 0) {
162-
double gainCorr = gainArr[0];
163-
adcCal = adcRaw * gainCorr;
164-
}
165-
}
166-
167-
Hit h = new Hit(id, superlayer, layer, wire, doca, adcRaw, time);
168-
h.setWirePosition(detector);
169-
h.setADC(adcCal); // place to store calibrated ADC
170-
h.setToT(totUsed); // place to store caibrated ToT
171-
hits.add(h);
172-
}
173-
174-
this.set_AHDCHits(hits);
175-
}
176-
177-
public final void fetch_TrueAHDCHits(DataEvent event) {
178-
179-
ArrayList<TrueHit> truehits = new ArrayList<>();
180-
181-
if (event.hasBank("MC::True")) {
182-
DataBank bankSIMU = event.getBank("MC::True");
183-
for (int i = 0; i < bankSIMU.rows(); i++) {
184-
int pid = bankSIMU.getInt("pid", i);
185-
double x_true = bankSIMU.getFloat("avgX", i);
186-
double y_true = bankSIMU.getFloat("avgY", i);
187-
double z_true = bankSIMU.getFloat("avgZ", i);
188-
double trackE = bankSIMU.getFloat("trackE", i);
189-
190-
truehits.add(new TrueHit(pid, x_true, y_true, z_true, trackE));
191-
}
192-
}
193-
194-
this.set_TrueAHDCHits(truehits);
195-
}
196-
197-
public ArrayList<Hit> get_AHDCHits() {
198-
return _AHDCHits;
199-
}
200-
201-
public void set_AHDCHits(ArrayList<Hit> hits) {
202-
this._AHDCHits = hits;
203-
}
204-
205-
public ArrayList<TrueHit> get_TrueAHDCHits() {
206-
return _TrueAHDCHits;
207-
}
208-
209-
public void set_TrueAHDCHits(ArrayList<TrueHit> trueHits) {
210-
this._TrueAHDCHits = trueHits;
211-
}
115+
if (timeOffsets == null) {
116+
throw new IllegalStateException("Missing CCDB /calibration/alert/ahdc/time_offsets for key=" + key_value + " (check run/variation + key mapping)");
117+
}
118+
119+
120+
121+
double t0 = timeOffsets[0];
122+
double time = leadingEdgeTime - t0 - startTime;
123+
124+
// -----------------------------
125+
// ToT correction (new CCDB)
126+
// convention: ToT_corr = ToT_raw * totCorr
127+
// -----------------------------
128+
double totUsed = timeOverThreshold;
129+
if (!sim) {
130+
double[] totArr = CalibrationConstantsLoader.AHDC_TIME_OVER_THRESHOLD.get(key_value);
131+
if (totArr != null && totArr.length > 0) {
132+
double totCorr = totArr[0];
133+
totUsed = timeOverThreshold * totCorr;
134+
}
135+
}
136+
137+
// -----------------------------
138+
// Hit selection (cuts)
139+
// NOTE: we cut on totUsed (corrected ToT). If you want RAW-ToT cuts,
140+
// replace totUsed with timeOverThreshold in the condition.
141+
// -----------------------------
142+
boolean passCuts =
143+
(wfType <= 2) &&
144+
(adcRaw >= adc_min) && (adcRaw <= adc_max) &&
145+
(time >= t_min) && (time <= t_max) &&
146+
(timeOverThreshold >= tot_min) && (timeOverThreshold <= tot_max)&&
147+
//(totUsed >= tot_min) && (totUsed <= tot_max) &&
148+
(adcOffset >= ped_min) && (adcOffset <= ped_max);
149+
150+
if (!passCuts && !sim) continue;
151+
152+
// -----------------------------
153+
// DOCA from calibrated time
154+
// -----------------------------
155+
double doca = T2Dfunction(key_value,time);
156+
157+
if (time < 0) doca = 0.0;
158+
159+
// -----------------------------
160+
// ADC gain calibration (new gains schema: gainCorr is index 0)
161+
// convention: ADC_cal = ADC_raw * gainCorr
162+
// -----------------------------
163+
double adcCal = adcRaw;
164+
if (!sim) {
165+
double[] gainArr = CalibrationConstantsLoader.AHDC_ADC_GAINS.get(key_value);
166+
if (gainArr != null && gainArr.length > 0) {
167+
double gainCorr = gainArr[0];
168+
adcCal = adcRaw * gainCorr;
169+
}
170+
}
171+
172+
Hit h = new Hit(id, superlayer, layer, wire, doca, adcRaw, time);
173+
h.setWirePosition(detector);
174+
h.setADC(adcCal); // place to store calibrated ADC
175+
h.setToT(totUsed); // place to store caibrated ToT
176+
hits.add(h);
177+
}
178+
179+
this.set_AHDCHits(hits);
180+
}
181+
182+
public final void fetch_TrueAHDCHits(DataEvent event) {
183+
184+
ArrayList<TrueHit> truehits = new ArrayList<>();
185+
186+
if (event.hasBank("MC::True")) {
187+
DataBank bankSIMU = event.getBank("MC::True");
188+
for (int i = 0; i < bankSIMU.rows(); i++) {
189+
int pid = bankSIMU.getInt("pid", i);
190+
double x_true = bankSIMU.getFloat("avgX", i);
191+
double y_true = bankSIMU.getFloat("avgY", i);
192+
double z_true = bankSIMU.getFloat("avgZ", i);
193+
double trackE = bankSIMU.getFloat("trackE", i);
194+
195+
truehits.add(new TrueHit(pid, x_true, y_true, z_true, trackE));
196+
}
197+
}
198+
199+
this.set_TrueAHDCHits(truehits);
200+
}
201+
202+
public ArrayList<Hit> get_AHDCHits() {
203+
return _AHDCHits;
204+
}
205+
206+
public void set_AHDCHits(ArrayList<Hit> hits) {
207+
this._AHDCHits = hits;
208+
}
209+
210+
public ArrayList<TrueHit> get_TrueAHDCHits() {
211+
return _TrueAHDCHits;
212+
}
213+
214+
public void set_TrueAHDCHits(ArrayList<TrueHit> trueHits) {
215+
this._TrueAHDCHits = trueHits;
216+
}
212217
}

0 commit comments

Comments
 (0)