diff --git a/etc/bankdefs/hipo4/README.md b/etc/bankdefs/hipo4/README.md index cf2881f836..270e947e30 100644 --- a/etc/bankdefs/hipo4/README.md +++ b/etc/bankdefs/hipo4/README.md @@ -386,6 +386,8 @@ | 12 | `ATOF::tdc` | TDC bank for the ALERT TOF | | 21 | `ATOF::hits` | Reconstructed ATOF hits | | 22 | `ATOF::clusters` | Clusters in ATOF | +| 31 | `ATOF::testhits` | Reconstructed ATOF hits | +| 32 | `ATOF::testclusters` | Clusters in ATOF | ## Group 22600 @@ -407,3 +409,4 @@ | 26 | `AHDC::KFTrack` | Reco Kalman Filter Tracks | | 30 | `AHDC_AI::Prediction` | Prediction given by AI | | 31 | `ALERT::Projections` | Track Projections to ATOF | +| 35 | `ATOF::testVeff` | veff test for ATOF | diff --git a/etc/bankdefs/hipo4/alert.json b/etc/bankdefs/hipo4/alert.json index 7999d13c2b..5efaaa8f6f 100644 --- a/etc/bankdefs/hipo4/alert.json +++ b/etc/bankdefs/hipo4/alert.json @@ -49,10 +49,38 @@ "name": "L_at_wedge", "type": "F", "info": "path length at atof wedge (inner surface) in mm" + } + ] + },{ + "name": "ATOF::testVeff", + "group": 23000, + "item": 35, + "info": "veff test for ATOF", + "entries": [ + { + "name": "id", + "type": "S", + "info": "calib id" + },{ + "name": "iCluster", + "type": "S", + "info": "id of the cluster" + },{ + "name": "iBar", + "type": "S", + "info": "id bar hit" },{ - "name": "L_in_wedge", + "name": "ldiff", "type": "F", - "info": "path length inside atof wedge in mm" + "info": "length diff mm" + }, { + "name": "tdiff", + "type": "F", + "info": "time diff ns" + },{ + "name": "module", + "type": "S", + "info": "module" } ] },{ @@ -103,6 +131,62 @@ "info": "id of cluster to which the hit was associated" } ] + },{ + "name": "ATOF::testhits", + "group": 22500, + "item": 31, + "info": "Reconstructed ATOF hits", + "entries": [ + { + "name": "id", + "type": "S", + "info": "hit id" + }, { + "name": "sector", + "type": "I", + "info": "atof sector" + }, { + "name": "layer", + "type": "I", + "info": "atof layer" + },{ + "name": "component", + "type": "I", + "info": "atof component" + },{ + "name": "time", + "type": "F", + "info": "time in ns" + },{ + "name": "TDC", + "type": "I", + "info": "TDC" + },{ + "name": "x", + "type": "F", + "info": "x position in mm" + }, { + "name": "y", + "type": "F", + "info": "y position in mm" + }, { + "name": "z", + "type": "F", + "info": "z position in mm" + },{ + "name": "energy", + "type": "F", + "info": "deposited energy in MeV" + },{ + "name": "TOT", + "type": "I", + "info": "TOT" + },{ + "name": "clusterid", + "type": "S", + "info": "id of cluster to which the hit was associated" + } + ] },{ "name": "ATOF::clusters", "group": 22500, @@ -155,6 +239,54 @@ "info": "id of the projected track matched. it is -1 if a straight track was assigned." } ] + },{ + "name": "ATOF::testclusters", + "group": 22500, + "item": 32, + "info": "Clusters in ATOF", + "entries": [ + { + "name": "id", + "type": "S", + "info": "hit id" + }, { + "name": "N_bar", + "type": "I", + "info": "number of hits from the bars" + }, { + "name": "N_wedge", + "type": "I", + "info": "number of hits from the wedges" + },{ + "name": "time", + "type": "F", + "info": "time in ns" + },{ + "name": "TDC", + "type": "I", + "info": "TDC" + },{ + "name": "x", + "type": "F", + "info": "x position in mm" + }, { + "name": "y", + "type": "F", + "info": "y position in mm" + }, { + "name": "z", + "type": "F", + "info": "z position in mm" + },{ + "name": "energy", + "type": "F", + "info": "energy in MeV" + },{ + "name": "TOT", + "type": "I", + "info": "TOT" + } + ] },{ "name": "AHDC::Hits", "group": 23000, diff --git a/reconstruction/alert/src/main/java/org/jlab/rec/atof/banks/TestBankWriter.java b/reconstruction/alert/src/main/java/org/jlab/rec/atof/banks/TestBankWriter.java new file mode 100644 index 0000000000..cb0c73574f --- /dev/null +++ b/reconstruction/alert/src/main/java/org/jlab/rec/atof/banks/TestBankWriter.java @@ -0,0 +1,140 @@ +package org.jlab.rec.atof.banks; + +import java.util.ArrayList; +import org.jlab.io.base.DataBank; +import org.jlab.io.base.DataEvent; +import org.jlab.rec.atof.cluster.ATOFCluster; +import org.jlab.rec.atof.hit.ATOFHit; +import org.jlab.rec.atof.hit.BarHit; +import org.jlab.rec.atof.veff.VeffCalibration; + +/** + * The {@code RecoBankWriter} writes the banks needed for the atof testing: hits + * and clusters info. + * + * @author pilleux + */ +public class TestBankWriter { + + public static DataBank fillATOFTestHitBank(DataEvent event, ArrayList wedgeHits, ArrayList barHits) { + ArrayList hitList = new ArrayList<>(); + hitList.addAll(wedgeHits); + hitList.addAll(barHits); + DataBank bank = event.createBank("ATOF::testhits", hitList.size()); + if (bank == null) { + System.err.println("COULD NOT CREATE A ATOF::testhits BANK!!!!!!"); + return null; + } + + for (int i = 0; i < hitList.size(); i++) { + bank.setShort("id", i, (short) (i + 1)); + bank.setShort("clusterid", i, (short) hitList.get(i).getAssociatedClusterIndex()); + bank.setInt("sector", i, (int) hitList.get(i).getSector()); + bank.setInt("layer", i, (int) hitList.get(i).getLayer()); + bank.setInt("component", i, (int) hitList.get(i).getComponent()); + bank.setInt("TDC", i, (int) hitList.get(i).getTdc()); + bank.setFloat("time", i, (float) hitList.get(i).getTime()); + bank.setFloat("x", i, (float) (hitList.get(i).getX())); + bank.setFloat("y", i, (float) (hitList.get(i).getY())); + bank.setFloat("z", i, (float) (hitList.get(i).getZ())); + bank.setInt("TOT", i, (int) hitList.get(i).getTot()); + bank.setFloat("energy", i, (float) hitList.get(i).getEnergy()); + } + return bank; + } + + public static DataBank fillATOFTestClusterBank(DataEvent event, ArrayList clusterList) { + + DataBank bank = event.createBank("ATOF::testclusters", clusterList.size()); + + if (bank == null) { + System.err.println("COULD NOT CREATE A ATOF::testclusters BANK!!!!!!"); + return null; + } + + for (int i = 0; i < clusterList.size(); i++) { + bank.setShort("id", i, (short) (i + 1)); + bank.setInt("N_bar", i, (int) clusterList.get(i).getBarHits().size()); + bank.setInt("N_wedge", i, (int) clusterList.get(i).getWedgeHits().size()); + bank.setInt("TDC", i, (int) clusterList.get(i).getTdc()); + bank.setFloat("time", i, (float) clusterList.get(i).getTime()); + bank.setFloat("x", i, (float) (clusterList.get(i).getX())); + bank.setFloat("y", i, (float) (clusterList.get(i).getY())); + bank.setFloat("z", i, (float) (clusterList.get(i).getZ())); + bank.setInt("TOT", i, (int) clusterList.get(i).getTot()); + bank.setFloat("energy", i, (float) clusterList.get(i).getEnergy()); + } + return bank; + } + + public static DataBank fillATOFTestVeff(DataEvent event, ArrayList calibrations) { + + DataBank bank = event.createBank("ATOF::testVeff", calibrations.size()); + + if (bank == null) { + System.err.println("COULD NOT CREATE A ATOF::testclusters BANK!!!!!!"); + return null; + } + for (int i = 0; i < calibrations.size(); i++) { + bank.setShort("id", i, (short) (i + 1)); + bank.setShort("iCluster", i, (short) calibrations.get(i).getICluster()); + bank.setShort("iBar", i, (short) calibrations.get(i).getIBarHit()); + bank.setFloat("ldiff", i, (float) calibrations.get(i).getLdiff()); + bank.setFloat("tdiff", i, (float) calibrations.get(i).getTdiff()); + bank.setShort("module", i, (short) calibrations.get(i).getModule()); + } + return bank; + } + + /** + * Appends the atof banks to an event. + * + * @param event the {@link DataEvent} in which to append the banks + * @param clusterList the {@link ArrayList} of {@link ATOFCluster} + * containing the clusters info to be added to the bank + * @param wedgeHits the {@link ArrayList} of {@link ATOFHit} containing the + * wedge hits info to be added + * @param barHits the {@link ArrayList} of {@link BarHit} containing the bar + * hits info to be added + * + * @return 0 if it worked, 1 if it failed + * + */ + public int appendATOFBanks(DataEvent event, ArrayList wedgeHits, ArrayList barHits, ArrayList clusterList) { + + DataBank hitbank = this.fillATOFTestHitBank(event, wedgeHits, barHits); + if (hitbank != null) { + event.appendBank(hitbank); + } else { + return 1; + } + + DataBank clusterbank = fillATOFTestClusterBank(event, clusterList); + if (clusterbank != null) { + event.appendBank(clusterbank); + } else { + return 1; + } + + return 0; + } + + public int appendVeffBanks(DataEvent event, ArrayList calibrations) { + + DataBank veffbank = fillATOFTestVeff(event, calibrations); + if (veffbank != null) { + event.appendBank(veffbank); + } else { + return 1; + } + + return 0; + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + } + +} diff --git a/reconstruction/alert/src/main/java/org/jlab/rec/atof/veff/VeffCalibration.java b/reconstruction/alert/src/main/java/org/jlab/rec/atof/veff/VeffCalibration.java new file mode 100644 index 0000000000..660d33ad2e --- /dev/null +++ b/reconstruction/alert/src/main/java/org/jlab/rec/atof/veff/VeffCalibration.java @@ -0,0 +1,60 @@ +package org.jlab.rec.atof.veff; + +public class VeffCalibration { + double ldiff, tdiff; + int iCluster, iBarHit, module; + + public double getLdiff() { + return ldiff; + } + + public void setLdiff(double ldiff) { + this.ldiff = ldiff; + } + + public double getTdiff() { + return tdiff; + } + + public void setTdiff(double tdiff) { + this.tdiff = tdiff; + } + + // Getter and Setter for iCluster + public int getICluster() { + return iCluster; + } + + public void setICluster(int iCluster) { + this.iCluster = iCluster; + } + + // Getter and Setter for iBarHit + public int getIBarHit() { + return iBarHit; + } + + public void setIBarHit(int iBarHit) { + this.iBarHit = iBarHit; + } + + // Getter and Setter for module + public int getModule() { + return module; + } + + public void setModule(int module) { + this.module = module; + } + + VeffCalibration(int module, double ldiff, double tdiff, int iCluster, int iBarHit){ + this.module=module; + this.ldiff = ldiff; + this.tdiff = tdiff; + this.iCluster = iCluster; + this.iBarHit = iBarHit; + } + + public static void main(String[] args) { + } +} diff --git a/reconstruction/alert/src/main/java/org/jlab/rec/atof/veff/VeffCalibrator.java b/reconstruction/alert/src/main/java/org/jlab/rec/atof/veff/VeffCalibrator.java new file mode 100644 index 0000000000..e26c08e675 --- /dev/null +++ b/reconstruction/alert/src/main/java/org/jlab/rec/atof/veff/VeffCalibrator.java @@ -0,0 +1,87 @@ +package org.jlab.rec.atof.veff; + +import java.util.ArrayList; +import org.jlab.rec.atof.cluster.ATOFCluster; +import org.jlab.rec.atof.constants.Parameters; +import org.jlab.rec.atof.hit.ATOFHit; +import org.jlab.rec.atof.hit.BarHit; + +/** + * + * @author npilleux + */ +public class VeffCalibrator { + + private final ArrayList calibs = new ArrayList<>(); + + public ArrayList getCalibs() { + return calibs; + } + + public void setCalibs(ArrayList calibs) { + this.calibs.clear(); + if (calibs != null) { + this.calibs.addAll(calibs); + } + } + + public boolean computeCalib(ArrayList Clusters) { + calibs.clear(); + for (int i_c = 0; i_c < Clusters.size(); i_c++) { + ATOFCluster cluster = Clusters.get(i_c); + ArrayList WedgeHits = cluster.getWedgeHits(); + if (WedgeHits.size() < 1) { + continue; + } + double zFromWedge = cluster.getMaxWedgeHit().getZ(); + ArrayList BarHits = cluster.getBarHits(); + for (int i_b = 0; i_b < BarHits.size(); i_b++) { + BarHit barhit = BarHits.get(i_b); + double uphit_time = barhit.getHitUp().getTime(); + double downhit_time = barhit.getHitDown().getTime(); + calibs.add(new VeffCalibration(barhit.computeModuleIndex(),(2*zFromWedge),(uphit_time - downhit_time),i_c,i_b)); + } + } + return true; + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + /* + String input = "/Users/npilleux/Desktop/alert/atof-reconstruction/coatjava/rec_ahdc_alert_020797_small_updated_for_test.hipo"; + HipoDataSource reader = new HipoDataSource(); + reader.open(input); + VeffCalibration en = new VeffCalibration(); + en.init(); + String fileName_veff = "/Users/npilleux/Desktop/alert/atof-reconstruction/coatjava/veff_test.csv"; + try ( + PrintWriter writer = new PrintWriter(new FileWriter(fileName_veff))) { + int event_number = 0; + writer.printf("event_number,i_cluster,i_bar,module,Ldiff,tdiff%n"); + while (reader.hasEvent()) { + { + event_number++; + DataEvent event = (DataEvent) reader.getNextEvent(); + en.processDataEvent(event, writer, event_number); + System.out.print("------ \n"); + } + + } + JFrame frame = new JFrame("tsum"); + frame.setSize(2500, 800); + EmbeddedCanvas canvas = new EmbeddedCanvas(); + canvas.cd(0); + canvas.draw(en.h_veff); + frame.add(canvas); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + writer.flush(); + } catch (IOException e) { + System.err.println("An error occurred while writing the file: " + e.getMessage()); + } + */ + } +} + diff --git a/reconstruction/alert/src/main/java/org/jlab/rec/service/ATOFTestEngine.java b/reconstruction/alert/src/main/java/org/jlab/rec/service/ATOFTestEngine.java new file mode 100644 index 0000000000..586786ad75 --- /dev/null +++ b/reconstruction/alert/src/main/java/org/jlab/rec/service/ATOFTestEngine.java @@ -0,0 +1,106 @@ +package org.jlab.rec.service; + +import java.util.ArrayList; + +import org.jlab.clas.reco.ReconstructionEngine; +import org.jlab.io.base.DataBank; +import org.jlab.io.base.DataEvent; + +import org.jlab.detector.calib.utils.DatabaseConstantProvider; +import org.jlab.geom.base.Detector; +import org.jlab.geom.detector.alert.ATOF.AlertTOFFactory; +import org.jlab.rec.atof.banks.TestBankWriter; +import org.jlab.rec.atof.cluster.ATOFCluster; +import org.jlab.rec.atof.cluster.ClusterFinder; +import org.jlab.rec.atof.constants.Parameters; +import org.jlab.rec.atof.hit.ATOFHit; +import org.jlab.rec.atof.hit.BarHit; +import org.jlab.rec.atof.hit.HitFinder; +import org.jlab.rec.atof.veff.VeffCalibrator; + +/** + * Service to return ATOF test hits and clusters + * + * @author npilleux + * + */ +public class ATOFTestEngine extends ReconstructionEngine { + + public ATOFTestEngine() { + super("ATOFTest", "pilleux", "1.0"); + } + + TestBankWriter rbc; + + private Detector ATOF; + + public void setATOF(Detector ATOF) { + this.ATOF = ATOF; + } + + public Detector getATOF() { + return ATOF; + } + + @Override + public boolean processDataEvent(DataEvent event) { + + if (!event.hasBank("RUN::config")) { + return true; + } + + DataBank bank = event.getBank("RUN::config"); + + int newRun = bank.getInt("run", 0); + if (newRun == 0) { + return true; + } + + //Hit finder init + HitFinder hitfinder = new HitFinder(); + hitfinder.findHits(event, ATOF); + + ArrayList WedgeHits = hitfinder.getWedgeHits(); + ArrayList BarHits = hitfinder.getBarHits(); + + //Exit if hit lists are empty + if (WedgeHits.isEmpty() && BarHits.isEmpty()) { + // System.out.println("No hits : "); + // event.show(); + return true; + } + + ClusterFinder clusterFinder = new ClusterFinder(); + int sigma_module = 5; + int sigma_component = 11; + double sigma_z = 5 * Parameters.LENGTH_ATOF; + double sigma_t = 100; + + clusterFinder.makeClusters(hitfinder, sigma_module, sigma_component, sigma_z, sigma_t, event); + ArrayList Clusters = clusterFinder.getClusters(); + + VeffCalibrator calibrator = new VeffCalibrator(); + calibrator.computeCalib(Clusters); + + if (WedgeHits.size() != 0 || BarHits.size() != 0) { + rbc.appendATOFBanks(event, WedgeHits, BarHits, Clusters); + rbc.appendVeffBanks(event, calibrator.getCalibs()); + } + return true; + } + + @Override + public boolean init() { + rbc = new TestBankWriter(); + + AlertTOFFactory factory = new AlertTOFFactory(); + DatabaseConstantProvider cp = new DatabaseConstantProvider(11, "default"); + this.ATOF = factory.createDetectorCLAS(cp); + this.registerOutputBank("ATOF::testhits", "ATOF::testclusters"); + + return true; + } + + public static void main(String arg[]) { + } +} diff --git a/reconstruction/alert/src/main/java/org/jlab/rec/service/ATOFEngine.java b/reconstruction/alert/src/main/java/org/jlab/service/atof/ATOFEngine.java similarity index 100% rename from reconstruction/alert/src/main/java/org/jlab/rec/service/ATOFEngine.java rename to reconstruction/alert/src/main/java/org/jlab/service/atof/ATOFEngine.java