hps-java/src/main/java/org/lcsim/hps/users/phansson
diff -N TrigRateDriver.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ TrigRateDriver.java 21 Jun 2012 15:35:15 -0000 1.1
@@ -0,0 +1,388 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.lcsim.hps.users.phansson;
+
+import hep.aida.*;
+import hep.aida.ref.plotter.PlotterRegion;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.lcsim.detector.identifier.ExpandedIdentifier;
+import org.lcsim.detector.identifier.IExpandedIdentifier;
+import org.lcsim.detector.identifier.IIdentifier;
+import org.lcsim.detector.identifier.IIdentifierHelper;
+import org.lcsim.event.CalorimeterHit;
+import org.lcsim.event.EventHeader;
+import org.lcsim.event.Track;
+import org.lcsim.geometry.Detector;
+import org.lcsim.geometry.IDDecoder;
+import org.lcsim.geometry.Subdetector;
+import org.lcsim.hps.monitoring.AIDAFrame;
+import org.lcsim.hps.recon.ecal.HPSEcalCluster;
+import org.lcsim.hps.recon.ecal.HPSEcalConditions;
+import org.lcsim.hps.recon.tracking.EcalTrackMatch;
+import org.lcsim.util.Driver;
+import org.lcsim.util.aida.AIDA;
+
+/**
+ *
+ * @author phansson+
+ */
+public class TrigRateDriver extends Driver {
+
+ int nevents = 0;
+ boolean debug = true;
+ protected IDDecoder dec = null;
+ protected Subdetector ecal;
+ private String ecalName = "Ecal";
+
+
+ private static int crystalCols;
+ private static int crystalRows;
+ private boolean hideFrame = true;
+
+ private String outputPlotFileName = "test.aida";
+ private String trackCollectionName = "MatchedTracks";
+ private String ecalClusterCollectionName = "EcalClusters";
+
+
+
+ EcalTrackMatch trkMatchTool;
+
+ private boolean doTracking = true;
+
+ private AIDA aida = AIDA.defaultInstance();
+ private IAnalysisFactory af = aida.analysisFactory();
+ IHistogramFactory hf = aida.histogramFactory();
+ private AIDAFrame plotterFrame;
+ private AIDAFrame plotterFrameTrig;
+
+ IPlotter plotter_trig_tag;
+
+ private int trigger[] = {0,0};
+
+ private static int nThr = 5;
+ private int eThr[] = {400,500,600,700,800};
+
+
+ private int refreshRate = 1000;
+
+ // Cluster energy correction
+ double C_ep = 1.0; //1.0/0.3;
+
+
+
+ public void startOfData() {
+ }
+
+ public void detectorChanged(Detector detector) {
+ // Get the Subdetector.
+ ecal = detector.getSubdetector(ecalName);
+
+ // Cache ref to decoder.
+ dec = ecal.getIDDecoder();
+
+ //Ecal geometry
+
+ crystalCols = 46;
+ crystalRows = 5;
+
+
+
+
+
+ plotterFrame = new AIDAFrame();
+ plotterFrame.setTitle("TrigRateFrame");
+
+ IPlotterStyle style;
+
+
+ IPlotter plotter_hitmap_gr = af.createPlotterFactory().create();
+ plotter_hitmap_gr.createRegions(2,6,0);
+ plotter_hitmap_gr.setTitle("Cluster hit map gr");
+ plotter_hitmap_gr.style().statisticsBoxStyle().setVisible(false);
+ plotterFrame.addPlotter(plotter_hitmap_gr);
+
+ IPlotter plotter_hitY_gr = af.createPlotterFactory().create();
+ plotter_hitY_gr.createRegions(2,6,0);
+ plotter_hitY_gr.setTitle("Cluster hit Y gr");
+ plotter_hitY_gr.style().statisticsBoxStyle().setVisible(false);
+ plotterFrame.addPlotter(plotter_hitY_gr);
+
+ IPlotter plotter_ep_gr = af.createPlotterFactory().create();
+ plotter_ep_gr.createRegions(2,6,0);
+ plotter_ep_gr.setTitle("Cluster Eoverp gr");
+ plotter_ep_gr.style().statisticsBoxStyle().setVisible(false);
+ plotterFrame.addPlotter(plotter_ep_gr);
+
+
+
+ for(int iside=0;iside<2;++iside) {
+ String side = iside==0 ? "top" : "bottom";
+ double ymin = iside == 0 ? -0.5 : -5.5;
+ double ymax = iside == 0 ? 5.5 : 0.5;
+
+ IHistogram h = aida.histogram2D("Cluster hit map good region " + side, 26, -25.5, 0.5, 6, ymin, ymax);
+ plotter_hitmap_gr.region((nThr+1)*iside).plot(h);
+
+ IHistogram hy = aida.histogram1D("Cluster hit Y good region " + side, 7, -0.5, 6.5);
+ plotter_hitY_gr.region((nThr+1)*iside).plot(hy);
+
+ IHistogram hep = aida.histogram1D("Cluster Eoverp good region " + side, 50, 0, 2);
+ plotter_ep_gr.region((nThr+1)*iside).plot(hep);
+
+
+ for(int i=0;i<nThr;++i) {
+ int reg = ((nThr+1)*iside)+(i+1);
+ if(debug) System.out.println("reg " + reg);
+ h = aida.histogram2D("Cluster E>" + eThr[i] + "GeV hit map good region " + side, 26, -25.5, 0.5, 6, ymin, ymax);
+ plotter_hitmap_gr.region(reg).plot(h);
+
+ hy = aida.histogram1D("Cluster E>" + eThr[i] + "GeV hit Y good region " + side, 7, -0.5,6.5);
+ plotter_hitY_gr.region(reg).plot(hy);
+
+ hep = aida.histogram1D("Cluster E>" + eThr[i] + "GeV Eoverp good region " + side, 50, 0,2);
+ plotter_ep_gr.region(reg).plot(hep);
+
+
+ }
+ }
+
+
+ for(int i=0;i<2*(nThr+1);++i) {
+
+ if(debug) System.out.println("i " + i);
+
+ style = plotter_hitmap_gr.region(i).style();
+ style.setParameter("hist2DStyle", "colorMap");
+ style.dataStyle().fillStyle().setParameter("colorMapScheme", "rainbow");
+ ((PlotterRegion) plotter_hitmap_gr.region(i)).getPlot().setAllowUserInteraction(true);
+ ((PlotterRegion) plotter_hitmap_gr.region(i)).getPlot().setAllowPopupMenus(true);
+ }
+
+
+
+
+
+ if(!hideFrame) {
+
+ plotterFrame.pack();
+ plotterFrame.setVisible(true);
+ }
+
+
+
+
+
+
+
+ }
+
+ public TrigRateDriver() {
+
+ trkMatchTool = new EcalTrackMatch(false);
+
+ }
+
+
+
+
+ public void setDebug(boolean flag) {
+ this.debug = flag;
+ }
+
+ public void setOutputPlotFileName( String name ) {
+ this.outputPlotFileName = name;
+ }
+
+ public void setHideFrame( boolean val ) {
+ this.hideFrame = val;
+ }
+
+
+
+ public void process(EventHeader event) {
+ ++nevents;
+ if( debug ) {
+ System.out.println("Processing event " + nevents);
+ }
+
+
+
+ //fastTracking(event);
+
+ List<HPSEcalCluster> clusters = event.get(HPSEcalCluster.class, ecalClusterCollectionName);
+
+
+ if(debug) System.out.println( clusters.size() + " ECal clusters in the event");
+
+ //plotClusterDistr(clusters,"");
+
+
+ List<Track> tracks = null;
+ if(doTracking) {
+ if(event.hasCollection(Track.class, trackCollectionName)) {
+ tracks = event.get(Track.class, trackCollectionName);
+ } else {
+ tracks = new ArrayList<Track>();
+ }
+ if(debug) System.out.println( tracks.size() + " tracks in this event");
+ }
+
+
+ for(HPSEcalCluster cl : clusters) {
+
+
+ int[] crystalPair = getCrystalPair(cl);
+
+
+
+ boolean clusterGoodRegion = false;
+ if(crystalPair[0]<0 && crystalPair[1]>1) clusterGoodRegion = true;
+ if(crystalPair[0]<0 && crystalPair[1]<-1) clusterGoodRegion = true;
+
+ if(debug) {
+ if(crystalPair[1]<-1) {
+ System.out.println("BOTTOM " + clusterGoodRegion);
+ }
+ }
+
+
+ double clEnergyCorr = cl.getEnergy()*C_ep;
+
+ if(clusterGoodRegion) {
+
+
+ String side = crystalPair[1]>0 ? "top" : "bottom";
+ aida.histogram2D("Cluster hit map good region " + side).fill(crystalPair[0], crystalPair[1]);
+ int hitY = crystalPair[1]>0 ? crystalPair[1] : (-1*crystalPair[1]);
+ aida.histogram1D("Cluster hit Y good region " + side).fill(hitY);
+ double eoverp = -1;
+ if(doTracking) {
+ trkMatchTool.setCluster(cl);
+ trkMatchTool.match(tracks);
+ if(trkMatchTool.isMatchedY(20)) {
+ eoverp = cl.getEnergy()/(trkMatchTool.getMatchedTrack().getPX()*1000);
+ }
+ }
+ if(eoverp>0) aida.histogram1D("Cluster Eoverp good region " + side).fill(eoverp);
+
+ if(debug) System.out.println("Ep = " + eoverp + " doTracking " + doTracking);
+
+
+ for(int i=0;i<nThr;++i){
+ if(cl.getEnergy()>eThr[i]) {
+ aida.histogram2D("Cluster E>" + eThr[i] + "GeV hit map good region " + side).fill(crystalPair[0], crystalPair[1]);
+ aida.histogram1D("Cluster E>" + eThr[i] + "GeV hit Y good region " + side).fill(hitY);
+ if(eoverp>0) aida.histogram1D("Cluster E>" + eThr[i] + "GeV Eoverp good region " + side).fill(eoverp);
+ }
+
+ }
+ }
+
+ }
+
+
+
+
+
+
+ }
+
+
+
+
+
+
+
+
+
+
+
+
+ private boolean hasBadNeighbours(HPSEcalCluster cluster) {
+ //check if this cluster has a neighbour that is dead or bad
+ if(!HPSEcalConditions.badChannelsLoaded()) return false;
+ List<CalorimeterHit> hits = cluster.getCalorimeterHits();
+ IIdentifierHelper helper = HPSEcalConditions.getHelper();
+ IExpandedIdentifier expId = new ExpandedIdentifier(helper.getIdentifierDictionary().getNumberOfFields());
+ expId.setValue(helper.getFieldIndex("system"), ecal.getSystemID());
+
+ boolean bad = false;
+
+ System.out.println("Checking cluster with " + cluster.getSize() + " hits for bad neighbours");
+
+ for(CalorimeterHit hit : hits) {
+ IIdentifier compactId = hit.getIdentifier();
+ //x-check
+ if(HPSEcalConditions.isBadChannel(hit.getCellID())) {
+ System.out.println("This cluster has a bad channel hit included!? ");
+ int x = helper.getValue(compactId, "ix");
+ int y = helper.getValue(compactId, "iy");
+ System.out.println(x + "," + y + " id " + hit.getCellID());
+ System.exit(1);
+ }
+ //Find crystal pair
+
+
+
+ int x = helper.getValue(compactId, "ix");
+ int y = helper.getValue(compactId, "iy");
+ System.out.println("Hit at " + x + "," + y);
+ for(int ix=x-1;ix!=x+2;++ix) {
+ for(int iy=y-1;iy!=y+2;++iy) {
+ expId.setValue(helper.getFieldIndex("ix"), ix);
+ expId.setValue(helper.getFieldIndex("iy"), iy);
+ IIdentifier compactId_t = helper.pack(expId);
+ System.out.println("Check" + ix + "," + iy + " id " + compactId_t.getValue());
+ if(HPSEcalConditions.isBadChannel(compactId_t.getValue())) {
+ System.out.println("This cell was BAD!");
+ return true;
+ }
+ }
+ }
+
+
+ }
+ return false;
+
+ }
+
+
+
+ public int[] getCrystalPair(HPSEcalCluster cluster) {
+ int[] pos = new int[2];
+ pos[0] = cluster.getSeedHit().getIdentifierFieldValue("ix");
+ pos[1] = cluster.getSeedHit().getIdentifierFieldValue("iy");
+
+ //System.out.println("cluster ix,iy " + pos[0] + "," + pos[1] + " from pos " + cluster.getSeedHit().getPositionVec().toString());
+ return pos;
+ //getCrystalPair(cluster.getPosition());
+ }
+
+
+
+
+
+
+
+
+
+ public void endOfData() {
+
+ if (outputPlotFileName != "")
+ try {
+ aida.saveAs(outputPlotFileName);
+ } catch (IOException ex) {
+ Logger.getLogger(TrigRateDriver.class.getName()).log(Level.SEVERE, "Couldn't save aida plots to file " + outputPlotFileName, ex);
+ }
+ //displayFastTrackingPlots();
+
+ }
+
+
+}