|
| 1 | +package org.jlab.detector.swaps; |
| 2 | + |
| 3 | +import org.jlab.detector.swaps.SwapTable; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Set; |
| 10 | +import org.jlab.detector.calib.utils.ConstantsManager; |
| 11 | +import org.jlab.io.base.DataBank; |
| 12 | +import org.jlab.io.base.DataEvent; |
| 13 | +import org.jlab.jnp.hipo4.data.SchemaFactory; |
| 14 | +import org.jlab.utils.groups.IndexedTable; |
| 15 | + |
| 16 | +/** |
| 17 | + * |
| 18 | + * Cache manager for cable swaps across multiple run numbers and CCDB tables, |
| 19 | + * interpreted from two ConstantsManager instances, implemented to enable |
| 20 | + * transforming a decoded HIPO file's sector/layer/component/order from the |
| 21 | + * translation tables used at decoding time into a different one. |
| 22 | + * |
| 23 | + * @author baltzell |
| 24 | + */ |
| 25 | +public class SwapManager { |
| 26 | + |
| 27 | + public static final String DEF_CURRENT_CCDB_VARIATION = "swaps"; |
| 28 | + public static final String DEF_PREVIOUS_CCDB_VARIATION = "default"; |
| 29 | + public static final String[] DEF_DETECTOR_NAMES = { |
| 30 | + "FTCAL", |
| 31 | + "FTHODO", |
| 32 | + "FTTRK", |
| 33 | + "LTCC", |
| 34 | + "ECAL", |
| 35 | + "FTOF", |
| 36 | + "HTCC", |
| 37 | + "DC", |
| 38 | + "CTOF", |
| 39 | + "CND", |
| 40 | + "BST", |
| 41 | + "RF", |
| 42 | + "BMT", |
| 43 | + "FMT", |
| 44 | + "RICH", |
| 45 | + "HEL", |
| 46 | + "BAND", |
| 47 | + "RTPC" |
| 48 | + }; |
| 49 | + |
| 50 | + private final HashMap<Integer,HashMap<String,SwapTable>> swaps = new HashMap<>(); |
| 51 | + private final Map<String,String> banksToTables = new HashMap<>(); |
| 52 | + private final Map<String,List<String>> detsToBanks = new HashMap<>(); |
| 53 | + private final Map<String,String> detsToTables = new HashMap<>(); |
| 54 | + private ConstantsManager prevConman = null; |
| 55 | + private ConstantsManager currConman = null; |
| 56 | + |
| 57 | + private static SwapManager instance = null; |
| 58 | + |
| 59 | + public Set<String> getDetectors() { |
| 60 | + return this.detsToBanks.keySet(); |
| 61 | + } |
| 62 | + public String getTable(String detectorName) { |
| 63 | + return this.detsToTables.get(detectorName); |
| 64 | + } |
| 65 | + public List<String> getBanks(String detectorName) { |
| 66 | + return this.detsToBanks.get(detectorName); |
| 67 | + } |
| 68 | + |
| 69 | + private SwapManager() {} |
| 70 | + |
| 71 | + public static SwapManager getInstance() { |
| 72 | + if (instance == null) { |
| 73 | + instance = new SwapManager(); |
| 74 | + } |
| 75 | + return instance; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param detectorNames |
| 80 | + * @param prevTimestamp in CCDB format: MM/DD/YYYY |
| 81 | + * @param currTimestamp in CCDB format: MM/DD/YYYY |
| 82 | + */ |
| 83 | + public SwapManager(List<String> detectorNames, String prevTimestamp,String currTimestamp) { |
| 84 | + this.initialize(detectorNames, prevTimestamp, currTimestamp); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param detectorNames |
| 89 | + * @param previous timestamp/variation used for translation tables during decoding |
| 90 | + * @param current timestamp/variation with correct translation tables |
| 91 | + */ |
| 92 | + public SwapManager(List<String> detectorNames,ConstantsManager previous,ConstantsManager current) { |
| 93 | + this.initialize(detectorNames, previous, current); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param detectorNames |
| 98 | + * @param prevTimestamp in CCDB format: MM/DD/YYYY |
| 99 | + * @param currTimestamp in CCDB format: MM/DD/YYYY |
| 100 | + */ |
| 101 | + public void initialize(List<String> detectorNames, String prevTimestamp,String currTimestamp) { |
| 102 | + this.initDetectors(detectorNames); |
| 103 | + this.prevConman = new ConstantsManager(); |
| 104 | + this.currConman = new ConstantsManager(); |
| 105 | + this.prevConman.setTimeStamp(prevTimestamp); |
| 106 | + this.prevConman.setVariation(DEF_PREVIOUS_CCDB_VARIATION); |
| 107 | + this.prevConman.init(new ArrayList<>(this.banksToTables.values())); |
| 108 | + if (currTimestamp != null) this.currConman.setTimeStamp(currTimestamp); |
| 109 | + this.currConman.setVariation(DEF_CURRENT_CCDB_VARIATION); |
| 110 | + this.currConman.init(new ArrayList<>(this.banksToTables.values())); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @param detectorNames |
| 115 | + * @param previous timestamp/variation used for translation tables during decoding |
| 116 | + * @param current timestamp/variation with correct translation tables |
| 117 | + */ |
| 118 | + public void initialize(List<String> detectorNames,ConstantsManager previous,ConstantsManager current) { |
| 119 | + this.initDetectors(detectorNames); |
| 120 | + this.prevConman = previous; |
| 121 | + this.currConman = current; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @param run run number |
| 126 | + * @param tableName CCDB translation table name, e.g. "/dat/tt/ecal" |
| 127 | + * @param slco array of old indices (sector,layer,component,order) |
| 128 | + * @return array of new table indices (sector/layer/component/order) |
| 129 | + */ |
| 130 | + public int[] get(int run, String tableName, int... slco) { |
| 131 | + if (this.currConman == null || this.prevConman == null) { |
| 132 | + return slco; |
| 133 | + } |
| 134 | + if (!this.swaps.containsKey(run)) { |
| 135 | + this.add(run); |
| 136 | + } |
| 137 | + if (this.swaps.get(run).containsKey(tableName)) { |
| 138 | + return this.swaps.get(run).get(tableName).get(slco); |
| 139 | + } |
| 140 | + else { |
| 141 | + return slco; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param run run number |
| 147 | + * @param tableName CCDB translation table name, e.g. "/daq/tt/ecal" |
| 148 | + * @param varName name of new index to retrieve (sector/layer/component/order) |
| 149 | + * @param slco array of old indices (sector/layer/component/order) |
| 150 | + * @return new value of the requested index (sector/layer/component/order) |
| 151 | + */ |
| 152 | + public int get(int run,String tableName,String varName,int... slco) { |
| 153 | + return this.get(run,tableName,slco)[SwapTable.getVariableIndex(varName)]; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @param run run number |
| 158 | + * @param bank ADC/TDC bank |
| 159 | + * @param row row index in bank |
| 160 | + * @return array of new table indices (sector/layer/component/order) |
| 161 | + */ |
| 162 | + public int[] get(int run, DataBank bank, int row) { |
| 163 | + final int sector = bank.getByte("sector", row); |
| 164 | + final int layer = bank.getByte("layer", row); |
| 165 | + final int comp = bank.getShort("component", row); |
| 166 | + final int order = bank.getByte("order", row); |
| 167 | + return this.get(run,banksToTables.get(bank.getDescriptor().getName()),sector,layer,comp,order); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @param event the HIPO event |
| 172 | + * @param bankName name of ADC/TDC bank |
| 173 | + * @param row row index in bank |
| 174 | + * @return array of new table indices (sector/layer/component/order) |
| 175 | + */ |
| 176 | + public int[] get(DataEvent event, String bankName, int row) { |
| 177 | + if (event.hasBank("RUN::config")) { |
| 178 | + if (event.hasBank(bankName)) { |
| 179 | + final int run = event.getBank("RUN::config").getInt("run",0); |
| 180 | + return this.get(run,event.getBank(bankName),row); |
| 181 | + } |
| 182 | + } |
| 183 | + return null; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Initialize the swaps for a given run number. |
| 188 | + * @param run |
| 189 | + */ |
| 190 | + private void add(int run) { |
| 191 | + this.swaps.put(run,new HashMap<>()); |
| 192 | + for (String tableName : this.banksToTables.values()) { |
| 193 | + IndexedTable prev = prevConman.getConstants(run, tableName); |
| 194 | + IndexedTable curr = currConman.getConstants(run, tableName); |
| 195 | + this.swaps.get(run).put(tableName,new SwapTable(prev,curr)); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public String toString() { |
| 201 | + String ret = ""; |
| 202 | + for (int run : this.swaps.keySet()) { |
| 203 | + for (String table : this.swaps.get(run).keySet()) { |
| 204 | + ret += this.swaps.get(run).get(table); |
| 205 | + } |
| 206 | + } |
| 207 | + return ret; |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Initialize the appropriate bank names and corresponding translation |
| 212 | + * table names, based on the given detector names. |
| 213 | + */ |
| 214 | + private final void initDetectors(List<String> detectorNames) { |
| 215 | + |
| 216 | + // register the detector names: |
| 217 | + List<String> thisDets = new ArrayList<>(); |
| 218 | + List<String> allDets = Arrays.asList(DEF_DETECTOR_NAMES); |
| 219 | + if (detectorNames == null || detectorNames.isEmpty()) { |
| 220 | + thisDets.addAll(allDets); |
| 221 | + } |
| 222 | + else { |
| 223 | + for (String detectorName : detectorNames) { |
| 224 | + if (allDets.contains(detectorName)) { |
| 225 | + thisDets.add(detectorName); |
| 226 | + } |
| 227 | + else { |
| 228 | + thisDets.clear(); |
| 229 | + throw new RuntimeException("[SwapManager] --> Invalid detector name: "+detectorName); |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + // set their bank/table names: |
| 235 | + SchemaFactory schema = new SchemaFactory(); |
| 236 | + schema.initFromDirectory(System.getenv("CLAS12DIR")+"/etc/bankdefs/hipo4"); |
| 237 | + for (String detName : thisDets) { |
| 238 | + // some detectors broke the bank/table naming convention: |
| 239 | + String tableName = detName.equals("BST") ? "/daq/tt/svt" : "/daq/tt/"+detName.toLowerCase(); |
| 240 | + for (String suffix : new String[]{"::adc","::tdc"}) { |
| 241 | + if (schema.hasSchema(detName+suffix)) { |
| 242 | + this.banksToTables.put(detName+suffix,tableName); |
| 243 | + this.detsToTables.put(detName,tableName); |
| 244 | + if (!this.detsToBanks.containsKey(detName)) { |
| 245 | + this.detsToBanks.put(detName,new ArrayList<String>()); |
| 246 | + } |
| 247 | + this.detsToBanks.get(detName).add(detName+suffix); |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + public static void main(String[] args) { |
| 254 | + |
| 255 | + SwapManager noman = getInstance(); |
| 256 | + System.out.println(Arrays.toString(noman.get(11014, "/daq/tt/bmt",3,5,320,0))); |
| 257 | + |
| 258 | + SwapManager man = new SwapManager(Arrays.asList("BMT"),"08/10/2020","10/13/2020"); |
| 259 | + man.get(11014,man.getTable("BMT"),"sector",3,6,8,0); |
| 260 | + System.out.println("SwapManager:\n"+man); |
| 261 | + System.out.println(man.get(11014,man.getTable("BMT"),"sector",99,22,33,44)); |
| 262 | + System.out.println(Arrays.toString(man.get(11014,man.getTable("BMT"),99,22,33,44))); |
| 263 | + System.out.println(Arrays.toString(man.get(11014,man.getTable("BMT"),3,5,320,0))); |
| 264 | + |
| 265 | + } |
| 266 | + |
| 267 | +} |
0 commit comments