Print

Print


Author: [log in to unmask]
Date: Fri Oct 23 11:25:00 2015
New Revision: 3886

Log:
Fix the svt header checks in order to properly debug for the whole event.

Modified:
    java/trunk/evio/src/main/java/org/hps/evio/AugmentedSvtEvioReader.java
    java/trunk/evio/src/main/java/org/hps/evio/SvtEventFlagger.java
    java/trunk/record-util/src/main/java/org/hps/record/svt/SvtEventHeaderChecker.java
    java/trunk/record-util/src/main/java/org/hps/record/svt/SvtHeaderDataInfo.java
    java/trunk/users/src/main/java/org/hps/users/phansson/SvtHeaderAnalysisDriver.java

Modified: java/trunk/evio/src/main/java/org/hps/evio/AugmentedSvtEvioReader.java
 =============================================================================
--- java/trunk/evio/src/main/java/org/hps/evio/AugmentedSvtEvioReader.java	(original)
+++ java/trunk/evio/src/main/java/org/hps/evio/AugmentedSvtEvioReader.java	Fri Oct 23 11:25:00 2015
@@ -6,17 +6,11 @@
 import java.util.List;
 
 import org.hps.record.svt.SvtEventHeaderChecker;
+import org.hps.record.svt.SvtEvioExceptions.SvtEvioHeaderException;
 import org.hps.record.svt.SvtHeaderDataInfo;
-import org.hps.record.svt.SvtEvioExceptions.SvtEvioHeaderApvBufferAddressException;
-import org.hps.record.svt.SvtEvioExceptions.SvtEvioHeaderApvFrameCountException;
-import org.hps.record.svt.SvtEvioExceptions.SvtEvioHeaderApvReadErrorException;
-import org.hps.record.svt.SvtEvioExceptions.SvtEvioHeaderException;
-import org.hps.record.svt.SvtEvioExceptions.SvtEvioHeaderMultisampleErrorBitException;
-import org.hps.record.svt.SvtEvioExceptions.SvtEvioHeaderOFErrorException;
-import org.hps.record.svt.SvtEvioExceptions.SvtEvioHeaderSkipCountException;
-import org.hps.record.svt.SvtEvioExceptions.SvtEvioHeaderSyncErrorException;
 import org.lcsim.event.EventHeader;
 import org.lcsim.lcio.LCIOUtil;
+
 
 /**
  * @author Per Hansson Adrian <[log in to unmask]>
@@ -33,7 +27,8 @@
         super();
     }
 
-
+    
+    
     
     @Override
     protected void processSvtHeaders(List<SvtHeaderDataInfo> headers, EventHeader lcsimEvent) throws SvtEvioHeaderException {
@@ -44,34 +39,39 @@
         
         // if we want we can control the behavior here depending on if we want the run to stop
         
-        try {
-            SvtEventHeaderChecker.checkSvtHeaders(headers);
-        } catch (SvtEvioHeaderApvBufferAddressException
-                | SvtEvioHeaderApvFrameCountException
-                | SvtEvioHeaderMultisampleErrorBitException
-                | SvtEvioHeaderApvReadErrorException
-                | SvtEvioHeaderSyncErrorException
-                | SvtEvioHeaderOFErrorException
-                | SvtEvioHeaderSkipCountException e) {
+        List<SvtEvioHeaderException> exceptions = SvtEventHeaderChecker.checkSvtHeaders(headers);
 
-            // add skimming flag
+        if( !exceptions.isEmpty() ) {
+
+            // print some debug info 
+            
+            List<String> exceptionNames = SvtEventHeaderChecker.getSvtEvioHeaderExceptionNames(exceptions);
+            String names = "";
+            for(String str : exceptionNames) names += str + " ";
+            
+            LOGGER.info("Caught " + exceptions.size() + " SvtEvioHeaderExceptions for event " + lcsimEvent.getEventNumber() + " of " + exceptionNames.size() + " types: " + names);
+            
+            //LOGGER.fine("List all of them.\n");
+            //int i = 0;
+            //for(SvtEvioHeaderException e : exceptions ) {
+            //    LOGGER.fine("Exception " + (i++) + " for event " + lcsimEvent.getEventNumber() + ":\n" + e.getMessage());
+            //}
+
+            // add event flag
             SvtEventFlagger.voidAddHeaderCheckResultToMetaData(false, lcsimEvent);
-            
+
             // add stuff to the event meta data
             SvtEventFlagger.AddHeaderInfoToMetaData(headers, lcsimEvent);
+
+            // then throw the first exception again to be caught in the event builder if we want to
+            if(throwHeaderExceptions)
+                throw new SvtEvioHeaderException(exceptions.get(0));
             
-            // then throw the exception again to be caught in the event builder
-            if(throwHeaderExceptions)
-                throw new SvtEvioHeaderException(e);
-            else {
-                LOGGER.info("caught SvtEvioHeaderException exception for event " + lcsimEvent.getEventNumber());
-                return; // need to return to prevent me from setting the GOOD flag outside the caught exceptions.
-            }
+        } else { 
+            // add skimming flag - the header is OK since I would never get here otherwise
+            SvtEventFlagger.voidAddHeaderCheckResultToMetaData(true, lcsimEvent);
         }
-        
-        // add skimming flag - the header is OK since I would never get here otherwise
-        SvtEventFlagger.voidAddHeaderCheckResultToMetaData(true, lcsimEvent);
-        
+
         // Add SVT header data to the event
         //this.addSvtHeadersToEventEventCollection(headers, lcsimEvent);
 

Modified: java/trunk/evio/src/main/java/org/hps/evio/SvtEventFlagger.java
 =============================================================================
--- java/trunk/evio/src/main/java/org/hps/evio/SvtEventFlagger.java	(original)
+++ java/trunk/evio/src/main/java/org/hps/evio/SvtEventFlagger.java	Fri Oct 23 11:25:00 2015
@@ -1,12 +1,17 @@
 package org.hps.evio;
 
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
-
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.lang3.ArrayUtils;
 import org.hps.conditions.database.DatabaseConditionsManager;
 import org.hps.conditions.svt.SvtAlignmentConstant;
 import org.hps.conditions.svt.SvtBiasConstant;
@@ -185,4 +190,64 @@
         }
         
     }
+    
+    private static final Pattern rocIdPattern  = Pattern.compile("svt_.*_roc(\\d+)");
+    
+    public static int getRocFromSvtHeaderName(String seq) {
+        Matcher m = rocIdPattern.matcher(seq);
+        if(m == null) 
+            throw new RuntimeException("null matcher, don't think this should happen");
+        if( !m.matches() ) 
+            return -1;
+        else
+            return Integer.parseInt( m.group(1) );
+    }
+    
+    
+    
+    public static List<SvtHeaderDataInfo>  getHeaderInfoToMetaData(EventHeader lcsimEvent) {
+        Map<Integer, Integer> headers = new HashMap<Integer,Integer>();
+        Map<Integer, Integer> tails = new HashMap<Integer,Integer>();
+        Map<Integer, Integer[]> multisampleHeaders = new HashMap<Integer,Integer[]>();
+        
+        
+        for(Map.Entry<String, int[]> entry : lcsimEvent.getIntegerParameters().entrySet()) {
+            
+            int roc = getRocFromSvtHeaderName(entry.getKey());
+            
+            if( roc == -1) {
+                continue;
+            }
+            //LOGGER.logger.fine("processing entry \"" + entry.getKey()+ "\"" + " for roc "  + roc);
+            int[] value = entry.getValue();
+           
+            if(entry.getKey().contains("svt_event_header_roc"))
+                headers.put(roc, value[0]);
+            
+            if(entry.getKey().contains("svt_event_tail_roc")) 
+                tails.put(roc, value[0]);
+                
+            // really need to copy?
+            if(entry.getKey().contains("svt_multisample_headers_roc")) {
+                Integer[] tmp = ArrayUtils.toObject(value); //new Integer[value.length];
+                multisampleHeaders.put(roc, tmp);
+            }
+                    
+        }
+        
+        // create the new objects
+        List<SvtHeaderDataInfo> headerDataInfo = new ArrayList<SvtHeaderDataInfo>();
+        for(Integer roc : headers.keySet()) {
+            int header = headers.get(roc);
+            int tail = tails.get(roc);
+            Integer[] ms = multisampleHeaders.get(roc);
+            headerDataInfo.add(new SvtHeaderDataInfo(roc, header, tail, ms));
+        }
+        
+       return headerDataInfo;
+        
+    }
+    
+    
+    
 }

Modified: java/trunk/record-util/src/main/java/org/hps/record/svt/SvtEventHeaderChecker.java
 =============================================================================
--- java/trunk/record-util/src/main/java/org/hps/record/svt/SvtEventHeaderChecker.java	(original)
+++ java/trunk/record-util/src/main/java/org/hps/record/svt/SvtEventHeaderChecker.java	Fri Oct 23 11:25:00 2015
@@ -3,9 +3,13 @@
  */
 package org.hps.record.svt;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.hps.record.svt.SvtEvioExceptions.*;
 
@@ -20,6 +24,9 @@
 public class SvtEventHeaderChecker {
 
     private static Logger LOGGER = Logger.getLogger(SvtEventHeaderChecker.class.getPackage().getName());
+    static {
+        LOGGER.setLevel(Level.INFO);
+    }
     
         /**
      * Check the integrity of the SVT header information.
@@ -33,7 +40,9 @@
          * @throws SvtEvioHeaderSyncErrorException 
      * @throws SvtEvioHeaderException
      */
-    public static void checkSvtHeaders(List<SvtHeaderDataInfo> headers) throws SvtEvioHeaderApvBufferAddressException, SvtEvioHeaderApvFrameCountException, SvtEvioHeaderMultisampleErrorBitException, SvtEvioHeaderApvReadErrorException, SvtEvioHeaderSyncErrorException, SvtEvioHeaderOFErrorException, SvtEvioHeaderSkipCountException  {
+    //public static List<SvtEvioHeaderException>  checkSvtHeaders(List<SvtHeaderDataInfo> headers) throws SvtEvioHeaderApvBufferAddressException, SvtEvioHeaderApvFrameCountException, SvtEvioHeaderMultisampleErrorBitException, SvtEvioHeaderApvReadErrorException, SvtEvioHeaderSyncErrorException, SvtEvioHeaderOFErrorException, SvtEvioHeaderSkipCountException  {
+    public static List<SvtEvioHeaderException>  checkSvtHeaders(List<SvtHeaderDataInfo> headers) {
+          
         LOGGER.fine("check " + headers.size() + " headers  ");
         int[] bufferAddresses = new int[6];
         int[] firstFrameCounts = new int[6];
@@ -44,6 +53,8 @@
         int[] readError;
         int count;
         int multisampleHeaderTailerrorBit;
+        // create a list to hold all active exceptions
+        List<SvtEvioHeaderException> exceptions = new ArrayList<SvtEvioHeaderException>();
         for( SvtHeaderDataInfo headerDataInfo : headers ) {
             LOGGER.fine("checking header: " + headerDataInfo.toString());
 
@@ -66,21 +77,21 @@
 
                 // check if there was any read errors
                 readError = SvtEvioUtils.getApvReadErrors(multisampleHeader);
-
+                
                 if( bufAddresses.length != 6)
-                    throw new SvtEvioHeaderApvBufferAddressException("Invalid number of APV buffer addresses.");
+                    exceptions.add(new SvtEvioHeaderApvBufferAddressException("Invalid number of APV buffer addresses."));
 
                 if( frameCounts.length != 6)
-                    throw new SvtEvioHeaderApvFrameCountException("Invalid number of APV frame counts.");
+                    exceptions.add( new SvtEvioHeaderApvFrameCountException("Invalid number of APV frame counts."));
 
                 if( readError.length != 6)
-                    throw new SvtEvioHeaderApvFrameCountException("Invalid number of read errors.");
+                    exceptions.add( new SvtEvioHeaderApvFrameCountException("Invalid number of read errors."));
 
                 // Check for error bit
                 if( multisampleHeaderTailerrorBit != 0) {
-                    throw new SvtEvioHeaderMultisampleErrorBitException("A multisample header error bit was set for " + 
+                    exceptions.add( new SvtEvioHeaderMultisampleErrorBitException("A multisample header error bit was set for " + 
                                                                         getMultisampleDebugString(headerDataInfo, SvtEvioUtils.getMultisampleTailWord(multisampleHeader)) + 
-                                                                        getDebugString(bufAddresses, frameCounts, readError)); 
+                                                                        getDebugString(bufAddresses, frameCounts, readError))); 
                 }
 
                 // print debug
@@ -100,20 +111,20 @@
 
                     // Check that apv buffer addresses match
                     if( !Arrays.equals(bufferAddresses, bufAddresses)) {
-                        throw new SvtEvioHeaderApvBufferAddressException("The APV buffer addresses in this event do not match " + 
+                        exceptions.add( new SvtEvioHeaderApvBufferAddressException("The APV buffer addresses in this event do not match " + 
                                                                             getMultisampleDebugString(headerDataInfo, SvtEvioUtils.getMultisampleTailWord(multisampleHeader)) +
                                                                             getDebugString(bufAddresses, frameCounts, readError) +
                                                                             " compared to " +
-                                                                            getDebugString(bufferAddresses, firstFrameCounts, readError)); 
+                                                                            getDebugString(bufferAddresses, firstFrameCounts, readError))); 
                     }
 
                     // Check that apv frame count match
                     if( !Arrays.equals(firstFrameCounts, frameCounts)) {
-                        throw new SvtEvioHeaderApvFrameCountException("The APV frame counts in this event do not match " + 
+                        exceptions.add( new SvtEvioHeaderApvFrameCountException("The APV frame counts in this event do not match " + 
                                 getMultisampleDebugString(headerDataInfo, SvtEvioUtils.getMultisampleTailWord(multisampleHeader)) +
                                 getDebugString(bufAddresses, frameCounts, readError) +
                                 " compared to " +
-                                getDebugString(bufferAddresses, firstFrameCounts, readError)); 
+                                getDebugString(bufferAddresses, firstFrameCounts, readError))); 
                     }
                 }
 
@@ -125,9 +136,9 @@
                     LOGGER.fine("frame count " + iFrame + "  " + frameCounts[iFrame]  + " ( " + Integer.toHexString( frameCounts[iFrame]) + " )");
 
                     if( frameCounts[iFrame] > 15  ||  (count < 15 && frameCounts[iFrame] < count) || ( count == 15 && frameCounts[iFrame] != 0 ) ) {
-                        throw new SvtEvioHeaderApvFrameCountException("The APV frame counts in this events are invalid " + 
+                        exceptions.add( new SvtEvioHeaderApvFrameCountException("The APV frame counts in this events are invalid " + 
                                 getMultisampleDebugString(headerDataInfo, SvtEvioUtils.getMultisampleTailWord(multisampleHeader)) +
-                                getDebugString(bufAddresses, frameCounts, readError)); 
+                                getDebugString(bufAddresses, frameCounts, readError))); 
                     }
                     count = frameCounts[iFrame];
                 }
@@ -135,9 +146,9 @@
                 for (int iReadError=0; iReadError<readError.length; ++iReadError) {
                     LOGGER.fine("read error " + iReadError + "  " + readError[iReadError]  + " ( " + Integer.toHexString( readError[iReadError]) + " )");
                     if( readError[iReadError] != 1)  {// active low
-                        throw new SvtEvioHeaderApvReadErrorException("Read error occurred " + 
+                        exceptions.add( new SvtEvioHeaderApvReadErrorException("Read error occurred " + 
                                 getMultisampleDebugString(headerDataInfo, SvtEvioUtils.getMultisampleTailWord(multisampleHeader)) +
-                                getDebugString(bufAddresses, frameCounts, readError)); 
+                                getDebugString(bufAddresses, frameCounts, readError))); 
                     }
                 }
 
@@ -148,40 +159,58 @@
             // Check the header data
             // Parts of this get its input from the multisample which has already been checked
             // therefore I don't expect these to happen.
-            checkSvtHeaderData(headerDataInfo);
-
-            
-
-        }
-
-    }
-    
-    public static void checkSvtHeaderData(SvtHeaderDataInfo header) throws SvtEvioHeaderSyncErrorException, SvtEvioHeaderOFErrorException, SvtEvioHeaderSkipCountException {
+            List<SvtEvioHeaderException> svtHeaderDataExceptions = checkSvtHeaderData(headerDataInfo);
+            
+            exceptions.addAll(svtHeaderDataExceptions);
+
+            
+
+        }
+        
+        return exceptions;
+
+    }
+    
+    //public static List<SvtEvioHeaderException> checkSvtHeaderData(SvtHeaderDataInfo header) throws SvtEvioHeaderSyncErrorException, SvtEvioHeaderOFErrorException, SvtEvioHeaderSkipCountException {
+    public static List<SvtEvioHeaderException> checkSvtHeaderData(SvtHeaderDataInfo header)  {
+        
         int tail = header.getTail();
         LOGGER.fine("checkSvtHeaderData tail " + tail + "( " + Integer.toHexString(tail) + " ) " +
                                                  " errorbit   " +  Integer.toHexString(SvtEvioUtils.getSvtTailSyncErrorBit(tail)) +
                                                  " OFerrorbit " +  Integer.toHexString(SvtEvioUtils.getSvtTailOFErrorBit(tail)) + 
                                                  " checkSvtHeaderData skipcount  " +  Integer.toHexString(SvtEvioUtils.getSvtTailMultisampleSkipCount(tail)));
+        
+        List<SvtEvioHeaderException> exceptions = new ArrayList<SvtEvioHeaderException>();
+        
         if( SvtEvioUtils.getSvtTailSyncErrorBit(tail) != 0) {
-            throw new SvtEvioExceptions.SvtEvioHeaderSyncErrorException("This SVT header had a SyncError " + header.toString());
+            exceptions.add( new SvtEvioHeaderSyncErrorException("This SVT header had a SyncError " + getHeaderDebugString(header)));
         }
         else if( SvtEvioUtils.getSvtTailOFErrorBit(tail) != 0) {
-            throw new SvtEvioHeaderOFErrorException("This header had a OverFlowError " + header.toString());
+            exceptions.add( new SvtEvioHeaderOFErrorException("This header had a OverFlowError " + getHeaderDebugString(header)));
         }
         else if( SvtEvioUtils.getSvtTailMultisampleSkipCount(tail) != 0) {
-            throw new SvtEvioHeaderSkipCountException("This header had a skipCount " + SvtEvioUtils.getSvtTailMultisampleSkipCount(tail) + " error " + header.toString());
-        }
-        LOGGER.fine("checkSvtHeaderData passed all I guess");
-    }
-    
-    
+            exceptions.add( new SvtEvioHeaderSkipCountException("This header had a skipCount " + SvtEvioUtils.getSvtTailMultisampleSkipCount(tail) + " error " + getHeaderDebugString(header)));
+        }
+        if(exceptions.size() == 0)
+            LOGGER.fine("checkSvtHeaderData passed all I guess");
+        else
+            LOGGER.fine("checkSvtHeaderData problem found");
+        return exceptions;
+    }
+    
+    
+    private static String getHeaderDebugString(SvtHeaderDataInfo headerDataInfo) {
+        return headerDataInfo.toString();
+    }
+
     private static String getMultisampleDebugString(SvtHeaderDataInfo headerDataInfo, int multisampleHeaderTailWord) {
-        String s = " header" + headerDataInfo.toString() +
+        String s = getHeaderDebugString(headerDataInfo) + 
                 " multisample: feb " + SvtEvioUtils.getFebIDFromMultisampleTail(multisampleHeaderTailWord) + 
                 " hybrid " + SvtEvioUtils.getFebHybridIDFromMultisampleTail(multisampleHeaderTailWord) + 
-                " apv " + SvtEvioUtils.getApvFromMultisampleTail(multisampleHeaderTailWord);
+                " apv " + SvtEvioUtils.getApvFromMultisampleTail(multisampleHeaderTailWord) + " ";
         return s;
     }
+
     
     private static String getDebugString(int[] bufAddresses, int[] frameCounts, int[] readError ) {
         String s = "";
@@ -194,6 +223,55 @@
         return s;
     }
     
+    
+    public static int getDAQComponentFromExceptionMsg(SvtEvioHeaderException exception, String type) {
+        //Clean up character return before proceeding, I don't know why the regexp fails but whatever
+        
+        String str;
+        int charRet_idx = exception.getMessage().indexOf('\n');
+        if(charRet_idx != -1) str = exception.getMessage().substring(0, exception.getMessage().indexOf('\n'));
+        else str = exception.getMessage();
+        
+        // now match
+        Matcher m = Pattern.compile(".*\\s" + type + "\\s(\\d+)\\s.*").matcher(str);
+        int id = -1;
+        try {
+            if(m.matches())
+                id = Integer.parseInt(m.group(1));
+            else 
+                id = -2;            
+        } catch (Exception e) {
+            throw new RuntimeException("exception when matching \"" + str + "\" with \"" + m.pattern().toString() + "\"", e);
+        }
+        //System.out.println("got " + id + " from " + m.pattern().toString() + " for \""+ str + "\"");
+        return id;
+    }    
+    
+    public static String getSvtEvioHeaderExceptionCompactMessage(SvtEvioHeaderException e) {
+        String str = "Exception type " + e.getClass().getSimpleName();
+        int roc = getDAQComponentFromExceptionMsg(e, "num");
+        if(roc<0) throw new RuntimeException("Got " + roc + " from matching \"" + e.getMessage()  + " seem to have failed. Shouldn't happen?");
+        int feb = getDAQComponentFromExceptionMsg(e, "feb");
+        int hybrid = getDAQComponentFromExceptionMsg(e, "hybrid");
+        int apv = getDAQComponentFromExceptionMsg(e, "apv");
+        str += " for roc " + roc + " feb " + feb + " hybrid " + hybrid + " apv " + apv;
+        return str;
+    }
+    
+    public static String getSvtEvioHeaderExceptionName(SvtEvioHeaderException e) {
+        return e.getClass().getSimpleName();
+    }
+    
+    public static List<String> getSvtEvioHeaderExceptionNames(List<SvtEvioHeaderException> exceptions) {
+        List<String> l = new ArrayList<String>();
+        for (SvtEvioHeaderException e : exceptions) {
+            String name = getSvtEvioHeaderExceptionName(e);
+            if( !l.contains(name)) l.add(name);
+        }
+        return l;
+    }
+    
+    
     /**
      * Private construction to avoid class being instantiated
      */

Modified: java/trunk/record-util/src/main/java/org/hps/record/svt/SvtHeaderDataInfo.java
 =============================================================================
--- java/trunk/record-util/src/main/java/org/hps/record/svt/SvtHeaderDataInfo.java	(original)
+++ java/trunk/record-util/src/main/java/org/hps/record/svt/SvtHeaderDataInfo.java	Fri Oct 23 11:25:00 2015
@@ -29,6 +29,14 @@
         this.header = header;
         this.tail = tail;
         this.multisampleheader = multisampleheaders;
+    }
+    
+    public SvtHeaderDataInfo(int num, int header, int tail, Integer[] multisampleheaders) {
+        this.num = num;
+        this.header = header;
+        this.tail = tail;
+        this.multisampleheader = new int[ multisampleheaders.length ];
+        for(int i=0; i<multisampleheaders.length; ++i) this.multisampleheader[i] = multisampleheaders[i];
     }
     
     public void setMultisampleHeaders(int[] multisampleheaders) {

Modified: java/trunk/users/src/main/java/org/hps/users/phansson/SvtHeaderAnalysisDriver.java
 =============================================================================
--- java/trunk/users/src/main/java/org/hps/users/phansson/SvtHeaderAnalysisDriver.java	(original)
+++ java/trunk/users/src/main/java/org/hps/users/phansson/SvtHeaderAnalysisDriver.java	Fri Oct 23 11:25:00 2015
@@ -6,8 +6,10 @@
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.logging.FileHandler;
 import java.util.logging.Level;
@@ -16,7 +18,12 @@
 import java.util.regex.Pattern;
 
 import org.hps.analysis.trigger.util.TriggerDataUtils;
+import org.hps.evio.AugmentedSvtEvioReader;
+import org.hps.evio.SvtEventFlagger;
+import org.hps.record.svt.SvtEventHeaderChecker;
 import org.hps.record.svt.SvtEvioUtils;
+import org.hps.record.svt.SvtHeaderDataInfo;
+import org.hps.record.svt.SvtEvioExceptions.SvtEvioHeaderException;
 import org.hps.util.BasicLogFormatter;
 import org.lcsim.event.EventHeader;
 import org.lcsim.event.GenericObject;
@@ -32,6 +39,8 @@
     private final Logger logger = Logger.getLogger(SvtHeaderAnalysisDriver.class.getSimpleName());
     private int nEventsProcessed = 0;
     private Date eventDate = new Date(0);
+    private int nEventsProcessedHeaderBad = 0;
+    private int nEventsProcessedWithHeaderInfo = 0;
     private int nRceSyncErrorCountN = 0;
     private int nRceOFErrorCount = 0;
     private int nRceSkipCount = 0;
@@ -41,7 +50,9 @@
     FileWriter fileWriter; 
     PrintWriter printWriter;
     private final String triggerBankCollectionName = "TriggerBank";
-    private static final Pattern rocIdPattern  = Pattern.compile("svt_.*_roc(\\d+)");
+    Map<Integer, Map<String, Integer> > exceptionCount = new HashMap<Integer, Map<String,Integer>>(); 
+
+    
 
     
     /**
@@ -71,16 +82,6 @@
         
     }
     
-    
-    private int getRoc(String seq) {
-        Matcher m = rocIdPattern.matcher(seq);
-        if(m == null) 
-            throw new RuntimeException("null matcher, don't think this should happen");
-        if( !m.matches() ) 
-            return -1;
-        else
-            return Integer.parseInt( m.group(1) );
-    }
     
     
     @Override
@@ -114,16 +115,65 @@
             logger.warning("No svt_event_header_good flag found for run " + event.getRunNumber() + " event " + event.getEventNumber() + " date " + eventDate.toString() + " processed " + nEventsProcessed);
 
         // print if the flag is bad
-        if (headerFlag == 0) 
+        if (headerFlag == 0) {
             logger.info("svt_event_header_good " + headerFlag + " for run " + event.getRunNumber() + " event " + event.getEventNumber() + " date " + eventDate.toString() + " processed " + nEventsProcessed);
-                   
+            nEventsProcessedHeaderBad++;
+        }
+        
+        
+        List<SvtHeaderDataInfo> headerDataInfoList = SvtEventFlagger.getHeaderInfoToMetaData(event);
+        
+        logger.fine("found " + headerDataInfoList.size() + " SvtHeaderDataInfo in this event");
+        
+        // check that heder info is there if there was an error
+        if( headerFlag == 0 && headerDataInfoList.size()==0)
+            throw new RuntimeException("event has bad flag " + headerFlag + " but no SvtHeaderDataInfo");
+        
+        
+        // Get all the exceptions
+        List<SvtEvioHeaderException> exceptions = SvtEventHeaderChecker.checkSvtHeaders(headerDataInfoList);
+        
+
+        // Catalog and count them
+        
+        if(exceptions.size()>0) {
+            nEventsProcessedWithHeaderInfo++;
+            logger.info("found " + exceptions.size() + " SvtEvioHeaderExceptions in this event");
+        }
+        
+        for(SvtEvioHeaderException e : exceptions) {
+            String str = SvtEventHeaderChecker.getSvtEvioHeaderExceptionCompactMessage(e);
+            logger.info("Run " + event.getRunNumber() + " event " + event.getEventNumber() + " " + str);
+            String name = SvtEventHeaderChecker.getSvtEvioHeaderExceptionName(e);
+            Integer roc = SvtEventHeaderChecker.getDAQComponentFromExceptionMsg(e, "num");
+            if(!exceptionCount.containsKey(roc)) {
+                Map<String, Integer> m = new HashMap<String,Integer>();
+                exceptionCount.put(roc, m);
+            }
+            Map<String, Integer> typeCount = exceptionCount.get(roc);
+            if( !typeCount.containsKey(name) ) typeCount.put(name, 0);
+            int n = typeCount.get(name) + 1;
+            typeCount.put(name, n);
+        }
+        
+        // number of headers processed is just the size
+        nRceSvtHeaders += exceptionCount.size();
+        
+        nEventsProcessed++;
+        
+        /*
+        
+        // count how many containers of SVT header info I see
+        // they should only be there when there is an error
+        // check that the event flag and this make sense
+        int[] nRceErrorsPerEvent = {0,0,0};
         
         Map<Integer, Integer> rceHeaderCount = new HashMap<Integer, Integer>();
 
         // Get all the headers in the event
         for(Map.Entry<String, int[]> entry : event.getIntegerParameters().entrySet()) {
             
-            int roc = getRoc(entry.getKey());
+            int roc = SvtEventFlagger.getRocFromSvtHeaderName(entry.getKey());
             
             if( roc == -1) {
                 logger.fine("skip this entry \"" + entry.getKey());
@@ -148,12 +198,15 @@
             
             
             // check if this is a header
-            if(entry.getKey().contains("svt_event_header_roc"))
+            if(entry.getKey().contains("svt_event_header_roc")) {
                 logger.fine("found SVT header \"" + Integer.toHexString(value[0]) + "\" for \"" + entry.getKey()+ "\"" + " roc + " + roc);
+                nRceErrorsPerEvent[0]++;
+            }
             
             
             // Analyze the SVT event tails
             if(entry.getKey().contains("svt_event_tail_roc")) {
+                nRceErrorsPerEvent[1]++;
 
                 logger.fine("found SVT tail \"" + Integer.toHexString(value[0]) + "\" for \"" + entry.getKey()+ "\""+ " roc + "  + roc );
                 
@@ -183,7 +236,10 @@
             
             if(entry.getKey().contains("svt_multisample_headers_roc")) {
 
+                nRceErrorsPerEvent[2]++;
+
                 logger.fine("found " + value.length + " SVT multisample headers:");
+
                 
                 for(int i=0; i< value.length/4; ++i) {
                     
@@ -213,21 +269,25 @@
                 }
             }
             
-                    // keep track how many headers have errors
+            // keep track how many headers have errors
             if( syncError > 0) nRceSyncErrorCountN++;
             if( oFError > 0 ) nRceOFErrorCount++;
             if( skipCount > 0 ) nRceSkipCount++;
             if( multisampleErrorBits > 0 ) nRceMultisampleErrorCount++;
         }
+        // check that the counts make sense
+        if(! ( nRceErrorsPerEvent[0] == nRceErrorsPerEvent[1] && nRceErrorsPerEvent[0] == nRceErrorsPerEvent[2])) 
+            throw new RuntimeException("counts of header, tail and multisample headers are crazy: " + nRceErrorsPerEvent[0] + " vs " +nRceErrorsPerEvent[1] + " vs " + nRceErrorsPerEvent[2]);
+                
+        if( headerFlag == 0 && nRceErrorsPerEvent[0]==0)
+            throw new RuntimeException("event has bad flag " + headerFlag + " but counts of header, tail and multisample headers are zero?: " + nRceErrorsPerEvent[0] + " vs " +nRceErrorsPerEvent[1] + " vs " + nRceErrorsPerEvent[2]);
         
         
         for(Map.Entry<Integer, Integer> entry : rceHeaderCount.entrySet())
             logger.fine("ROC " + entry.getKey() + " count " + entry.getValue());
-
-        // number of headers processed is just the size
-        nRceSvtHeaders += rceHeaderCount.size();
-        
-        nEventsProcessed++;
+        */
+
+      
     }
     
     private void checkBitValueRange(int val) {
@@ -237,12 +297,44 @@
     
     @Override
     protected void endOfData() {
-        logger.info("endOfData: processed " + nEventsProcessed +  "events date " + eventDate.toString());
+        logger.info("nEventsProcessed " + nEventsProcessed);
+        logger.info("nEventsProcessedHeaderBad " + nEventsProcessedHeaderBad);
+        logger.info("nEventsProcessedWithHeaderInfo " + nEventsProcessedWithHeaderInfo);
         logger.info("nRceSvtHeaders " + nRceSvtHeaders);
-        logger.info("nRceSyncErrorCountN " + nRceSyncErrorCountN);
-        logger.info("nRceOFErrorCount " + nRceOFErrorCount);
-        logger.info("nRceSkipCount " + nRceSkipCount);
-        logger.info("nRceMultisampleErrorCount " + nRceMultisampleErrorCount);
+        //Map<Integer, Map<String, Integer> > exceptionCount = new HashMap<Integer, Map<String,Integer>>(); 
+        
+        //print roc's with errors
+        String rocs = "";
+        for(Integer roc : exceptionCount.keySet()) rocs += roc + " ";
+        logger.info("There were " + exceptionCount.keySet().size() + " rocs with any error: " + rocs);
+        
+        //print type of errors
+        Map<String, Integer> errorTypeCount = new HashMap<String,Integer>(); 
+        for(Map.Entry<Integer, Map<String, Integer> > rCount : exceptionCount.entrySet()) {
+            Map<String,Integer> m = rCount.getValue();
+            for(Map.Entry<String,Integer> entry : m.entrySet()) {
+                if( !errorTypeCount.containsKey(entry.getKey()) ) {
+                    errorTypeCount.put(entry.getKey(),0);
+                }
+                int n = errorTypeCount.get(entry.getKey()) + 1;
+                errorTypeCount.put(entry.getKey(), n);
+            }
+        }
+        logger.info("There are " + errorTypeCount.entrySet().size()+ " type of error occuring in the ROCs:");
+        for(Map.Entry<String,Integer> entry : errorTypeCount.entrySet()) {
+            String rocsWithError = "";
+            for(Map.Entry<Integer, Map<String, Integer> > rCount : exceptionCount.entrySet()) {
+                if(rCount.getValue().containsKey(entry.getKey())) {
+                    rocsWithError += rCount.getKey() + ":" + rCount.getValue().get(entry.getKey()) + " ";
+                }
+            }
+            logger.info(entry.getKey() + " " + entry.getValue() + " (individual roc counts " + rocsWithError + " )");
+        }
+        
+//        logger.info("nRceSyncErrorCountN " + nRceSyncErrorCountN);
+//        logger.info("nRceOFErrorCount " + nRceOFErrorCount);
+//        logger.info("nRceSkipCount " + nRceSkipCount);
+//        logger.info("nRceMultisampleErrorCount " + nRceMultisampleErrorCount);
         
     }