11package org .jlab .rec .ahdc .Hit ;
22
33import java .util .ArrayList ;
4+ import java .util .List ;
45
56import org .jlab .io .base .DataBank ;
67import org .jlab .io .base .DataEvent ;
78import org .jlab .detector .banks .RawDataBank ;
89import org .jlab .geom .detector .alert .AHDC .AlertDCDetector ;
9- import org .jlab .rec . alert . constants . CalibrationConstantsLoader ;
10+ import org .jlab .utils . groups . IndexedTable ;
1011
1112public class HitReader {
1213
1314 private ArrayList <Hit > _AHDCHits ;
1415 private ArrayList <TrueHit > _TrueAHDCHits ;
1516 private boolean sim = false ;
1617
17- public HitReader (DataEvent event , AlertDCDetector detector , boolean simulation ) {
18+ private IndexedTable rawHitCutsTable ;
19+ private IndexedTable timeOffsetsTable ;
20+ private IndexedTable timeToDistanceWireTable ;
21+ private IndexedTable timeOverThresholdTable ;
22+ private IndexedTable adcGainsTable ;
23+
24+ public HitReader (DataEvent event , AlertDCDetector detector , boolean simulation ,
25+ IndexedTable rawHitCuts ,
26+ IndexedTable timeOffsets ,
27+ IndexedTable timeToDistanceWire ,
28+ IndexedTable timeOverThreshold ,
29+ IndexedTable adcGains ) {
1830 sim = simulation ;
19- fetch_AHDCHits (event , detector );
31+ fetch_AHDCHits (event , detector , rawHitCuts , timeOffsets , timeToDistanceWire , timeOverThreshold , adcGains );
2032 if (simulation ) fetch_TrueAHDCHits (event );
2133 }
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-
34+
35+ public double T2Dfunction (int sector , int layer , int wire , double time ){
36+ long hash = timeToDistanceWireTable .getList ().getIndexGenerator ().hashCode (sector , layer , wire );
37+ List <Double > t2d = timeToDistanceWireTable .getDoublesByHash (hash );
38+
3239 // 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 ;
40+ // Column order: p1_int(0), p1_slope(1), p2_int(2), p2_slope(3), p3_int(4), p3_slope(5),
41+ // t1_x0(6), t1_width(7), t2_x0(8), t2_width(9), z0(10), z1(11), z2(12), extra1(13), extra2(14), chi2ndf(15)
42+
43+ double p1 = (t2d .get (0 ) + t2d .get (1 )*time );
44+ double p2 = (t2d .get (2 ) + t2d .get (3 )*time );
45+ double p3 = (t2d .get (4 ) + t2d .get (5 )*time );
46+
47+ double t1 = 1.0 /(1.0 + Math .exp (-(time - t2d .get (6 ))/t2d .get (7 )));
48+ double t2 = 1.0 /(1.0 + Math .exp (-(time - t2d .get (8 ))/t2d .get (9 )));
49+
50+ return (p1 )*(1.0 - t1 ) + (t1 )*(p2 )*(1.0 - t2 ) + (t2 )*(p3 );
4351 }
4452
45- public final void fetch_AHDCHits (DataEvent event , AlertDCDetector detector ) {
53+ public final void fetch_AHDCHits (DataEvent event , AlertDCDetector detector ,
54+ IndexedTable rawHitCuts , IndexedTable timeOffsets ,
55+ IndexedTable timeToDistanceWire , IndexedTable totCorrTable ,
56+ IndexedTable adcGains ) {
57+ this .rawHitCutsTable = rawHitCuts ;
58+ this .timeOffsetsTable = timeOffsets ;
59+ this .timeToDistanceWireTable = timeToDistanceWire ;
60+ this .timeOverThresholdTable = totCorrTable ;
61+ this .adcGainsTable = adcGains ;
4662
4763 ArrayList <Hit > hits = new ArrayList <>();
4864
@@ -63,7 +79,7 @@ public final void fetch_AHDCHits(DataEvent event, AlertDCDetector detector) {
6379 for (int i = 0 ; i < bankDGTZ .rows (); i ++) {
6480
6581 int id = bankDGTZ .trueIndex (i ) + 1 ;
66- int number = bankDGTZ .getByte ("layer" , i ); // e.g. 11,12,21,... (this matches CCDB "layer")
82+ int number = bankDGTZ .getByte ("layer" , i );
6783 int layer = number % 10 ;
6884 int superlayer = (number % 100 ) / 10 ;
6985 int sector = bankDGTZ .getInt ("sector" , i );
@@ -76,103 +92,52 @@ public final void fetch_AHDCHits(DataEvent event, AlertDCDetector detector) {
7692 double adcOffset = bankDGTZ .getFloat ("ped" , i );
7793 int wfType = bankDGTZ .getShort ("wfType" , i );
7894
79- // CCDB key
80- int key_value = sector * 10000 + number * 100 + wire ;
81-
82- // -----------------------------
8395 // 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- }
92-
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 );
114-
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 ];
96+ double t_min = rawHitCutsTable .getDoubleValue ("t_min" , sector , number , wire );
97+ double t_max = rawHitCutsTable .getDoubleValue ("t_max" , sector , number , wire );
98+ double tot_min = rawHitCutsTable .getDoubleValue ("tot_min" , sector , number , wire );
99+ double tot_max = rawHitCutsTable .getDoubleValue ("tot_max" , sector , number , wire );
100+ double adc_min = rawHitCutsTable .getDoubleValue ("adc_min" , sector , number , wire );
101+ double adc_max = rawHitCutsTable .getDoubleValue ("adc_max" , sector , number , wire );
102+ double ped_min = rawHitCutsTable .getDoubleValue ("ped_min" , sector , number , wire );
103+ double ped_max = rawHitCutsTable .getDoubleValue ("ped_max" , sector , number , wire );
104+
105+ // Time calibration
106+ double t0 = timeOffsetsTable .getDoubleValue ("t0" , sector , number , wire );
122107 double time = leadingEdgeTime - t0 - startTime ;
123108
124- // -----------------------------
125- // ToT correction (new CCDB)
126- // convention: ToT_corr = ToT_raw * totCorr
127- // -----------------------------
109+ // ToT correction
128110 double totUsed = timeOverThreshold ;
129111 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- }
112+ double totCorr = timeOverThresholdTable .getDoubleValue ("totCorr" , sector , number , wire );
113+ if (totCorr != 0.0 ) totUsed = timeOverThreshold * totCorr ;
135114 }
136115
137- // -----------------------------
138116 // 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- // -----------------------------
142117 boolean passCuts =
143118 (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 );
119+ (adcRaw >= adc_min ) && (adcRaw <= adc_max ) &&
120+ (time >= t_min ) && (time <= t_max ) &&
121+ (timeOverThreshold >= tot_min ) && (timeOverThreshold <= tot_max ) &&
122+ (adcOffset >= ped_min ) && (adcOffset <= ped_max );
149123
150124 if (!passCuts && !sim ) continue ;
151125
152- // -----------------------------
153126 // DOCA from calibrated time
154- // -----------------------------
155- double doca = T2Dfunction (key_value ,time );
156-
127+ double doca = T2Dfunction (sector , number , wire , time );
157128 if (time < 0 ) doca = 0.0 ;
158129
159- // -----------------------------
160- // ADC gain calibration (new gains schema: gainCorr is index 0)
161- // convention: ADC_cal = ADC_raw * gainCorr
162- // -----------------------------
130+ // ADC gain calibration
163131 double adcCal = adcRaw ;
164132 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- }
133+ double gainCorr = adcGainsTable .getDoubleValue ("gainCorr" , sector , number , wire );
134+ if (gainCorr != 0.0 ) adcCal = adcRaw * gainCorr ;
170135 }
171136
172137 Hit h = new Hit (id , superlayer , layer , wire , doca , adcRaw , time );
173138 h .setWirePosition (detector );
174- h .setADC (adcCal ); // place to store calibrated ADC
175- h .setToT (totUsed ); // place to store caibrated ToT
139+ h .setADC (adcCal );
140+ h .setToT (totUsed );
176141 hits .add (h );
177142 }
178143
0 commit comments