Index: /trunk/FACT++/scripts/CheckUnderflow.js
===================================================================
--- /trunk/FACT++/scripts/CheckUnderflow.js	(revision 15047)
+++ /trunk/FACT++/scripts/CheckUnderflow.js	(revision 15048)
@@ -60,15 +60,4 @@
 if (!runs)
     throw new Error("Could not connect to FAD_CONTROL/DRS_RUNS");
-/*
-runs = {
-    "name"    : "FAD_CONTROL/DRS_RUNS",
-    "format"  : "I:1;I:3",
-    "qos"     : 0,
-    "size"    : 16,
-    "counter" : 0,
-    "time"    : new Date("2012-11-20T05:23:52.804Z"),
-    "data"    : [300,[148,149,154]]
-  };
-*/
 
 // Wait for agilent to be online with state information
@@ -85,27 +74,16 @@
 console.out(" * Last power cycle:   "+power);
 console.out(" * Last DRS calib set: "+(runs.data?runs.time:"none"));
-//console.out(JSON.stringify(dim.state("AGILENT_CONTROL")));
 
 
 if (1)//diff>8 && now.getHours()>16 || runs.time<power)
 {
+    console.out("Checking send.");
+    checkSend(["FAD_CONTROL", "MCP", "RATE_CONTROL"]);
+    console.out("Checking send: done");
+
     //console.out("Most probablay the camera has not been checked for underflows yet.");
-
-    // Wait for dim.send to be ready
-    // FIXME: timeout
-    while (!dim.send("FAD_CONTROL"))
-        v8.sleep();
-
-    // FAD_CONTROL/RAW_DATA:
-    //  roi,roi_tm,num_fad,num_ftm,type,num_boards,error,
-    //  dummy,time,time_board,start_pix,start_tm,adc
-
-    // FAD_CONTROL/EVENT_DATA
-    // avg,rms,max,pos
 
     var service_event = new Subscription("FAD_CONTROL/EVENT_DATA");
     var event = service_event.get(5000, false);
-    //if (!event)
-    //    throw new Error("Could not connect to FAD_CONTROL/EVENT_DATA");
 
     var event_counter = event.counter;
@@ -125,15 +103,15 @@
     console.out("Event received: "+(new Date()-d)+"ms");
 
-    console.out("Stop run");
     dim.send("MCP/STOP");
-    while (!dim.send("RATE_CONTROL"))
-    {
-        v8.sleep(800);
-        console.out("waiting for RATE_CONTROL to send the necessary format information");
-    }
+
+    event = service_event.get(0, false);
+    service_event.close();
+
+    console.out("Run stopped.");
+
     dim.send("RATE_CONTROL/STOP"); // GlobalThresholdSet -> Connected
     dim.wait("MCP", "Idle", 3000);
 
-    var nn = runs.data && runs.data[0]>0 ? runs.data[1].reduce(Func.max) : -1;
+    var nn = run.data && runs.data.length>0 && runs.obj['roi']>0 ? runs.obj['run'][1].reduce(Func.max) : -1;
     if (nn>0)
     {
@@ -154,22 +132,15 @@
     }
 
-    //console.out(JSON.stringify(service_drs.get(0, false)));
-
-    event = service_event.get(0, false);
-
-    service_event.close();
-
     var hist = Hist2D(16, -2048.5, 2048.5, 11, -10, 100);
 
+    var data = event.obj;
+
     for (var i=0; i<1440; i++)
-        hist.fill(event.data[0][i], isNaN(event.data[1][i])?-1:event.data[1][i]);
+        hist.fill(data.avg[i], isNaN(data.rms[i])?-1:data.rms[i]);
 
-    //console.out(("%78s".$("")).replace(/ /g, "-"));
     hist.print();
-    //console.out(("%78s".$("")).replace(/ /g, "-"));
 
-    // Could also be <512
-    var stat0 = Func.stat(event.data[0], function(val) { if (val<0) console.out(" VAL="+val); return val<0; });
-    var stat1 = Func.stat(event.data[1]);
+    var stat0 = Func.stat(data.avg, function(val) { if (val<0) console.out(" VAL="+val); return val<0; });
+    var stat1 = Func.stat(data.rms);
 
     console.out("Avg[min]=%.1f".$(stat0.min));
@@ -182,4 +153,23 @@
     console.out("Rms[max]=%.1f".$(stat1.max));
     console.out(("%78s\n".$("")).replace(/ /g, "="));
+
+    //      OK                            UNDERFLOW
+    // ------------------------------------------------------
+    // Avg[min]=722.0                Avg[min]=-380.0
+    // Avg[avg]=815.9 +- 45.9        Avg[avg]= 808.0 +- 102.0
+    // Avg[max]=930.5                Avg[max]= 931.1
+    // Avg[cnt]=0                    Avg[cnt]= 9
+
+    // Rms[min]=14.0                 Rms[min]=13.9
+    // Rms[avg]=16.5 +- 1.6          Rms[avg]=18.8 +- 26.8
+    // Rms[max]=44.0                 Rms[max]=382.1
+
+    if (stat0.count==0)
+        return;
+
+    if (stat0.count>8)
+        throw new Error("Underflow condition detected in about "+stat0.count/9+" DRS.");
+
+    log.warn("There is probably an underflow condition in one DRS... please check manually.");
 }
 
