source: trunk/FACT++/scripts/CheckUnderflow.js@ 15048

Last change on this file since 15048 was 15048, checked in by tbretz, 12 years ago
Some code cleanup; added throwing an execption if an underflow condition has been detected.
File size: 5.5 KB
Line 
1'use strict';
2
3var Func = function() { };
4Func.sum = function(a, b) { return a+b; }
5Func.sq = function(a, b) { return Math.sqrt(a*a + b*b); }
6Func.min = function(a, b) { return Math.min(a, b); }
7Func.max = function(a, b) { return Math.max(a, b); }
8Func.avg = function(arr) { return arr.reduce(Func.Sum, 0)/arr.length; }
9Func.stat = function(arr, func)
10{
11 if (arr.length==0)
12 return undefined;
13
14 var sum = 0;
15 var sq = 0;
16 var cnt = 0;
17 var min = arr[0];
18 var max = arr[0];
19 arr.forEach(function(val) { sum+=val; sq+=val*val; if (val>max) max=val; if (val<min) min=val; if (func && func(val)) cnt++ });
20 sum /= arr.length;
21 sq /= arr.length;
22
23 return { avg:sum, rms:Math.sqrt(sq-sum*sum), min:min, max:max, count:cnt };
24}
25
26// ===================================================================
27
28console.out(("\n%78s".$("")).replace(/ /g, "="));
29
30include('scripts/CheckStates.js');
31
32var table =
33[
34 [ "MCP", [ "Idle" ] ],
35 [ "AGILENT_CONTROL", [ "VoltageOn" ] ],
36 [ "FTM_CONTROL", [ "Idle" ] ],
37 [ "FAD_CONTROL", [ "Connected", "WritingData" ] ],
38 [ "BIAS_CONTROL", [ "Disconnected", "VoltageOff" ] ],
39 [ "DATA_LOGGER", [ "WaitForRun", "NightlyFileOpen", "Logging" ] ],
40];
41
42console.out("Checking states.");
43if (!checkStates(table))
44{
45 throw new Error("Something unexpected has happened. One of the servers",
46 "is in a state in which it should not be. Please,",
47 "try to find out what happened...");
48}
49
50// ===================================================================
51
52include('scripts/Hist1D.js');
53include('scripts/Hist2D.js');
54
55console.out("Checking power on time");
56
57var service_drs = new Subscription("FAD_CONTROL/DRS_RUNS");
58
59var runs = service_drs.get(5000, false);
60if (!runs)
61 throw new Error("Could not connect to FAD_CONTROL/DRS_RUNS");
62
63// Wait for agilent to be online with state information
64// FIXME: timeout
65while (!dim.state("AGILENT_CONTROL"))
66 v8.sleep();
67
68var power = dim.state("AGILENT_CONTROL").time;
69var now = new Date();
70
71var diff = (now-runs.time)/3600000;
72
73console.out(" * Now: "+now);
74console.out(" * Last power cycle: "+power);
75console.out(" * Last DRS calib set: "+(runs.data?runs.time:"none"));
76
77
78if (1)//diff>8 && now.getHours()>16 || runs.time<power)
79{
80 console.out("Checking send.");
81 checkSend(["FAD_CONTROL", "MCP", "RATE_CONTROL"]);
82 console.out("Checking send: done");
83
84 //console.out("Most probablay the camera has not been checked for underflows yet.");
85
86 var service_event = new Subscription("FAD_CONTROL/EVENT_DATA");
87 var event = service_event.get(5000, false);
88
89 var event_counter = event.counter;
90
91 dim.send("FAD_CONTROL/START_DRS_CALIBRATION");
92 dim.send("FAD_CONTROL/SET_FILE_FORMAT", 0);
93
94 console.out("Starting drs-gain");
95 dim.send("MCP/START", -1, 3, "drs-gain");
96
97 // FIXME: add a timeout here (10s)
98 console.out("Waiting for new event");
99 var d = new Date();
100 while (event_counter == service_event.get(0, false).counter)
101 v8.sleep();
102
103 console.out("Event received: "+(new Date()-d)+"ms");
104
105 dim.send("MCP/STOP");
106
107 event = service_event.get(0, false);
108 service_event.close();
109
110 console.out("Run stopped.");
111
112 dim.send("RATE_CONTROL/STOP"); // GlobalThresholdSet -> Connected
113 dim.wait("MCP", "Idle", 3000);
114
115 var nn = run.data && runs.data.length>0 && runs.obj['roi']>0 ? runs.obj['run'][1].reduce(Func.max) : -1;
116 if (nn>0)
117 {
118 console.out("Trying to restore last DRS calibration #"+nn+" ["+runs.time+"]");
119
120 var night = new Date(runs.time-1000*60*60*12);
121 var yy = night.getUTCFullYear();
122 var mm = night.getUTCMonth()+1;
123 var dd = night.getUTCDate();
124
125 var filefmt = "/loc_data/raw/%d/%02d/%02d/%4d%02d%02d_%03d.drs.fits";
126
127 // FIXME: Timeout
128 var drs_counter = service_drs.get(0, false);
129 dim.send("FAD_CONTROL/LOAD_DRS_CALIBRATION", filefmt.$(yy, mm, dd, yy, mm, dd, nn));
130 while (drs_counter == service_drs.get(0, false).counter)
131 v8.sleep();
132 }
133
134 var hist = Hist2D(16, -2048.5, 2048.5, 11, -10, 100);
135
136 var data = event.obj;
137
138 for (var i=0; i<1440; i++)
139 hist.fill(data.avg[i], isNaN(data.rms[i])?-1:data.rms[i]);
140
141 hist.print();
142
143 var stat0 = Func.stat(data.avg, function(val) { if (val<0) console.out(" VAL="+val); return val<0; });
144 var stat1 = Func.stat(data.rms);
145
146 console.out("Avg[min]=%.1f".$(stat0.min));
147 console.out("Avg[avg]=%.1f +- %.1f".$(stat0.avg, stat0.rms));
148 console.out("Avg[max]=%.1f".$(+stat0.max));
149 console.out("Avg[cnt]="+stat0.count);
150 console.out("");
151 console.out("Rms[min]=%.1f".$(stat1.min));
152 console.out("Rms[avg]=%.1f +- %.1f".$(stat1.avg, stat1.rms));
153 console.out("Rms[max]=%.1f".$(stat1.max));
154 console.out(("%78s\n".$("")).replace(/ /g, "="));
155
156 // OK UNDERFLOW
157 // ------------------------------------------------------
158 // Avg[min]=722.0 Avg[min]=-380.0
159 // Avg[avg]=815.9 +- 45.9 Avg[avg]= 808.0 +- 102.0
160 // Avg[max]=930.5 Avg[max]= 931.1
161 // Avg[cnt]=0 Avg[cnt]= 9
162
163 // Rms[min]=14.0 Rms[min]=13.9
164 // Rms[avg]=16.5 +- 1.6 Rms[avg]=18.8 +- 26.8
165 // Rms[max]=44.0 Rms[max]=382.1
166
167 if (stat0.count==0)
168 return;
169
170 if (stat0.count>8)
171 throw new Error("Underflow condition detected in about "+stat0.count/9+" DRS.");
172
173 log.warn("There is probably an underflow condition in one DRS... please check manually.");
174}
175
176service_drs.close();
Note: See TracBrowser for help on using the repository browser.