|
10 | 10 |
|
11 | 11 | public class HitReader { |
12 | 12 |
|
13 | | - private ArrayList<Hit> _AHDCHits; |
14 | | - private ArrayList<TrueHit> _TrueAHDCHits; |
15 | | - private boolean sim = false; |
16 | | - |
17 | | - public HitReader(DataEvent event, AlertDCDetector detector, boolean simulation) { |
18 | | - sim = simulation; |
19 | | - fetch_AHDCHits(event, detector); |
20 | | - if (simulation) fetch_TrueAHDCHits(event); |
21 | | - } |
22 | | - |
23 | | - public final void fetch_AHDCHits(DataEvent event, AlertDCDetector detector) { |
24 | | - ArrayList<Hit> hits = new ArrayList<>(); |
25 | | - |
26 | | - |
27 | | - if (event.hasBank("AHDC::adc")) { |
28 | | - // Useful if one does not run the full CLAS12 reconstrcution |
29 | | - // i.e only run the reconstruction of ALERT |
30 | | - // or use simulated data |
31 | | - double startTime = 0; |
32 | | - if (event.hasBank("REC::Event") && !sim) { |
33 | | - DataBank bankRecEvent = event.getBank("REC::Event"); |
34 | | - startTime = bankRecEvent.getFloat("startTime", 0); |
35 | | - } |
36 | | - |
37 | | - RawDataBank bankDGTZ = new RawDataBank("AHDC::adc"); |
38 | | - bankDGTZ.read(event); |
39 | | - |
40 | | - for (int i = 0; i < bankDGTZ.rows(); i++) { |
41 | | - int id = bankDGTZ.trueIndex(i) + 1; |
42 | | - int number = bankDGTZ.getByte("layer", i); |
43 | | - int layer = number % 10; |
44 | | - int superlayer = (int) (number % 100) / 10; |
45 | | - int sector = bankDGTZ.getInt("sector", i); |
46 | | - int wire = bankDGTZ.getShort("component", i); |
47 | | - double adc = bankDGTZ.getInt("ADC", i); |
48 | | - double leadingEdgeTime = bankDGTZ.getFloat("leadingEdgeTime", i); |
49 | | - double timeOverThreshold = bankDGTZ.getFloat("timeOverThreshold", i); |
50 | | - double adcOffset = bankDGTZ.getFloat("ped", i); |
51 | | - int wfType = bankDGTZ.getShort("wfType", i); |
52 | | - // Retrieve raw hit cuts from CCDB |
53 | | - int key_value = sector*10000 + number*100 + wire; |
54 | | - double[] rawHitCuts = CalibrationConstantsLoader.AHDC_RAW_HIT_CUTS.get( key_value ); |
55 | | - double t_min = rawHitCuts[0]; |
56 | | - double t_max = rawHitCuts[1]; |
57 | | - double tot_min = rawHitCuts[2]; |
58 | | - double tot_max = rawHitCuts[3]; |
59 | | - double adc_min = rawHitCuts[4]; |
60 | | - double adc_max = rawHitCuts[5]; |
61 | | - double ped_min = rawHitCuts[6]; |
62 | | - double ped_max = rawHitCuts[7]; |
63 | | - //System.out.println("t_min : " + t_min + " t_max : " + t_max + " tot_min : " + tot_min + " tot_max : " + tot_max + " adc_min : " + adc_min + " adc_max : " + adc_max + " ped_min : " + ped_min + " ped_max : " + ped_max); |
64 | | - // Retrieve t0 and t2d from CCDB |
65 | | - // What's about simulation? |
66 | | - double[] timeOffsets = CalibrationConstantsLoader.AHDC_TIME_OFFSETS.get( key_value ); |
67 | | - double[] time2distance = CalibrationConstantsLoader.AHDC_TIME_TO_DISTANCE.get( 10101 ); // the time to distance table has only one row ! (10101 is its only key) |
68 | | - double t0 = timeOffsets[0]; |
69 | | - double p0 = time2distance[0]; |
70 | | - double p1 = time2distance[1]; |
71 | | - double p2 = time2distance[2]; |
72 | | - double p3 = time2distance[3]; |
73 | | - double p4 = time2distance[4]; |
74 | | - double p5 = time2distance[5]; |
75 | | - // Apply time calibration |
76 | | - // We may need adc calibration too |
77 | | - // Remark: leadingEdgeTime already has the fine timestamp correction |
78 | | - double time = leadingEdgeTime - t0 - startTime; |
79 | | - |
80 | | - // Hit selection : wfType and additional cuts |
81 | | - if (((wfType <= 2) && (adc >= adc_min) && (adc <= adc_max) && (time >= t_min) && (time <= t_max) && (timeOverThreshold >= tot_min) && (timeOverThreshold <= tot_max) && (adcOffset >= ped_min) && (adcOffset <= ped_max)) || sim) { |
82 | | - double doca = p0 + p1*Math.pow(time,1.0) + p2*Math.pow(time,2.0) + p3*Math.pow(time,3.0) + p4*Math.pow(time,4.0) + p5*Math.pow(time, 5.0); |
83 | | - if (time < 0) doca = 0; |
84 | | - Hit h = new Hit(id, superlayer, layer, wire, doca, adc, time); |
85 | | - h.setWirePosition(detector); |
86 | | - hits.add(h); |
87 | | - } |
88 | | - } |
89 | | - } |
90 | | - this.set_AHDCHits(hits); |
91 | | - } |
92 | | - |
93 | | - public final void fetch_TrueAHDCHits(DataEvent event) { |
94 | | - ArrayList<TrueHit> truehits = new ArrayList<>(); |
95 | | - |
96 | | - DataBank bankSIMU = event.getBank("MC::True"); |
97 | | - |
98 | | - if (event.hasBank("MC::True")) { |
99 | | - for (int i = 0; i < bankSIMU.rows(); i++) { |
100 | | - int pid = bankSIMU.getInt("pid", i); |
101 | | - double x_true = bankSIMU.getFloat("avgX", i); |
102 | | - double y_true = bankSIMU.getFloat("avgY", i); |
103 | | - double z_true = bankSIMU.getFloat("avgZ", i); |
104 | | - double trackE = bankSIMU.getFloat("trackE", i); |
105 | | - |
106 | | - truehits.add(new TrueHit(pid, x_true, y_true, z_true, trackE)); |
107 | | - } |
108 | | - } |
109 | | - this.set_TrueAHDCHits(truehits); |
110 | | - } |
111 | | - |
112 | | - public ArrayList<Hit> get_AHDCHits() { |
113 | | - return _AHDCHits; |
114 | | - } |
115 | | - |
116 | | - public void set_AHDCHits(ArrayList<Hit> _AHDCHits) { |
117 | | - this._AHDCHits = _AHDCHits; |
118 | | - } |
119 | | - |
120 | | - public ArrayList<TrueHit> get_TrueAHDCHits() { |
121 | | - return _TrueAHDCHits; |
122 | | - } |
123 | | - |
124 | | - public void set_TrueAHDCHits(ArrayList<TrueHit> _TrueAHDCHits) { |
125 | | - this._TrueAHDCHits = _TrueAHDCHits; |
126 | | - } |
127 | | - |
| 13 | + private ArrayList<Hit> _AHDCHits; |
| 14 | + private ArrayList<TrueHit> _TrueAHDCHits; |
| 15 | + private boolean sim = false; |
| 16 | + |
| 17 | + public HitReader(DataEvent event, AlertDCDetector detector, boolean simulation) { |
| 18 | + sim = simulation; |
| 19 | + fetch_AHDCHits(event, detector); |
| 20 | + if (simulation) fetch_TrueAHDCHits(event); |
| 21 | + } |
| 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 | + } |
| 70 | + |
| 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); |
| 92 | + |
| 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, adcCal, time); |
| 168 | + h.setWirePosition(detector); |
| 169 | + hits.add(h); |
| 170 | + } |
| 171 | + |
| 172 | + this.set_AHDCHits(hits); |
| 173 | + } |
| 174 | + |
| 175 | + public final void fetch_TrueAHDCHits(DataEvent event) { |
| 176 | + |
| 177 | + ArrayList<TrueHit> truehits = new ArrayList<>(); |
| 178 | + |
| 179 | + if (event.hasBank("MC::True")) { |
| 180 | + DataBank bankSIMU = event.getBank("MC::True"); |
| 181 | + for (int i = 0; i < bankSIMU.rows(); i++) { |
| 182 | + int pid = bankSIMU.getInt("pid", i); |
| 183 | + double x_true = bankSIMU.getFloat("avgX", i); |
| 184 | + double y_true = bankSIMU.getFloat("avgY", i); |
| 185 | + double z_true = bankSIMU.getFloat("avgZ", i); |
| 186 | + double trackE = bankSIMU.getFloat("trackE", i); |
| 187 | + |
| 188 | + truehits.add(new TrueHit(pid, x_true, y_true, z_true, trackE)); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + this.set_TrueAHDCHits(truehits); |
| 193 | + } |
| 194 | + |
| 195 | + public ArrayList<Hit> get_AHDCHits() { |
| 196 | + return _AHDCHits; |
| 197 | + } |
| 198 | + |
| 199 | + public void set_AHDCHits(ArrayList<Hit> hits) { |
| 200 | + this._AHDCHits = hits; |
| 201 | + } |
| 202 | + |
| 203 | + public ArrayList<TrueHit> get_TrueAHDCHits() { |
| 204 | + return _TrueAHDCHits; |
| 205 | + } |
| 206 | + |
| 207 | + public void set_TrueAHDCHits(ArrayList<TrueHit> trueHits) { |
| 208 | + this._TrueAHDCHits = trueHits; |
| 209 | + } |
128 | 210 | } |
0 commit comments