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

Last change on this file since 16357 was 16098, checked in by tbretz, 12 years ago
For the event the descriptions are needed.
File size: 7.1 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);
60//if (!runs)
61// throw new Error("Could not connect to FAD_CONTROL/DRS_RUNS");
62
63var power = dim.state("AGILENT_CONTROL").time;
64var now = new Date();
65
66var diff = (now-runs.time)/3600000;
67
68console.out(" * Now: "+now);
69console.out(" * Last power cycle: "+power);
70console.out(" * Last DRS calib set: "+(runs.data?runs.time:"none"));
71
72
73if (1)//diff>8 && now.getHours()>16 || runs.time<power)
74{
75 console.out("Checking send.");
76 checkSend(["FAD_CONTROL", "MCP", "RATE_CONTROL"]);
77 console.out("Checking send: done");
78
79 //console.out("Most probablay the camera has not been checked for underflows yet.");
80
81 var service_event = new Subscription("FAD_CONTROL/EVENT_DATA");
82
83 dim.send("FAD_CONTROL/START_DRS_CALIBRATION");
84 dim.send("FAD_CONTROL/SET_FILE_FORMAT", 0);
85
86 // This needs a better solution! The problem is that the
87 // event builder might still be writing a file (WritingData),
88 // but even if the file is closed (CLOSE_OPEN_FILES) the
89 // thread sending data (EVENT_DATA) might still be processing
90 // events. Thus there is no real guarantee that we do not receive
91 // data of the previous run after we have started a new one.
92 if (dim.state("FAD_CONTROL").name=="WritingData")
93 {
94 dim.send("FAD_CONTROL/CLOSE_OPEN_FILES");
95 dim.wait("FAD_CONTROL", "Connected", 3000);
96 }
97 v8.sleep(1000); // Let 'timeout' in SendRawData pass
98
99 console.out("Starting drs-gain... waiting for new event");
100
101 var sub_incomplete = new Subscription("FAD_CONTROL/INCOMPLETE");
102 var sub_connections = new Subscription("FAD_CONTROL/CONNECTIONS");
103 var sub_startrun = new Subscription("FAD_CONTROL/START_RUN");
104 sub_startrun.get(5000);
105 sub_connections.get(5000);
106 sub_startrun.get(5000);
107
108 include('scripts/takeRun.js');
109
110 while (1)
111 {
112 var event_counter = service_event.get(10000, false).counter;
113
114 var stop = function ()
115 {
116 while (1)
117 {
118 if (dim.state("MCP").name=="TakingData" && service_event.get(0, false).counter>event_counter)
119 {
120 dim.send("MCP/STOP");
121 return;
122 }
123 v8.sleep(100);
124 }
125 }
126
127 var thread = new Thread(250, stop);
128
129 var rc = takeRun("drs-gain");
130
131 thread.kill();
132
133 if (rc)
134 break;
135 }
136
137 console.out("Event received.");
138
139 sub_incomplete.close();
140 sub_connections.close();
141 sub_startrun.close();
142
143
144 // FIXME: Restore DRS calibration in case of failure!!
145 // FAD Re-connect in case of failure?
146 // MCP/RESET in case of failure?
147 // Proper error reporting!
148
149 var event = service_event.get(3000);//, false);
150 service_event.close();
151
152 console.out("Run stopped.");
153
154 dim.send("RATE_CONTROL/STOP"); // GlobalThresholdSet -> Connected
155 dim.wait("MCP", "Idle", 3000);
156
157 var nn = runs.data && runs.data.length>0 && runs.obj['roi']>0 ? runs.obj['run'].reduce(Func.max) : -1;
158 if (nn>0)
159 {
160 console.out("Trying to restore last DRS calibration #"+nn+" ["+runs.time+"]");
161
162 var night;
163 if (Sun.horizon(null, runs.time).isUp)
164 night = new Date(runs.time);
165 else
166 night = new Date(runs.time-1000*60*60*12);
167
168 var yy = night.getUTCFullYear();
169 var mm = night.getUTCMonth()+1;
170 var dd = night.getUTCDate();
171
172 var filefmt = "/loc_data/raw/%d/%02d/%02d/%4d%02d%02d_%03d.drs.fits";
173
174 // FIXME: Timeout
175 var drs_counter = service_drs.get(0, false).counter;
176 dim.send("FAD_CONTROL/LOAD_DRS_CALIBRATION", filefmt.$(yy, mm, dd, yy, mm, dd, nn));
177
178 var now = new Date();
179 v8.timeout(3000, function() { if (service_drs.get(0, false).counter>drs_counter) return true; });
180
181 console.out("Last DRS calibration restored ["+(new Date()-now)/1000+"s]");
182 }
183
184 var hist = Hist2D(16, -2048.5, 2048.5, 11, -10, 100);
185
186 var data = event.obj;
187
188 for (var i=0; i<1440; i++)
189 hist.fill(data.avg[i], isNaN(data.rms[i])?-1:data.rms[i]);
190
191 hist.print();
192
193 var stat0 = Func.stat(data.avg, function(val) { if (val<0) console.out(" VAL="+val); return val<0; });
194 var stat1 = Func.stat(data.rms);
195
196 console.out("Avg[min]=%.1f".$(stat0.min));
197 console.out("Avg[avg]=%.1f +- %.1f".$(stat0.avg, stat0.rms));
198 console.out("Avg[max]=%.1f".$(+stat0.max));
199 console.out("Avg[cnt]="+stat0.count);
200 console.out("");
201 console.out("Rms[min]=%.1f".$(stat1.min));
202 console.out("Rms[avg]=%.1f +- %.1f".$(stat1.avg, stat1.rms));
203 console.out("Rms[max]=%.1f".$(stat1.max));
204 console.out(("%78s\n".$("")).replace(/ /g, "="));
205
206 // OK UNDERFLOW
207 // ------------------------------------------------------
208 // Avg[min]=722.0 Avg[min]=-380.0
209 // Avg[avg]=815.9 +- 45.9 Avg[avg]= 808.0 +- 102.0
210 // Avg[max]=930.5 Avg[max]= 931.1
211 // Avg[cnt]=0 Avg[cnt]= 9
212
213 // Rms[min]=14.0 Rms[min]=13.9
214 // Rms[avg]=16.5 +- 1.6 Rms[avg]=18.8 +- 26.8
215 // Rms[max]=44.0 Rms[max]=382.1
216
217 if (stat0.count>0)
218 {
219 if (stat0.count>8)
220 throw new Error("Underflow condition detected in about "+parseInt(stat0.count/9+.5)+" DRS.");
221
222 log.warn("There is probably an underflow condition in one DRS... please check manually.");
223 }
224}
225
226service_drs.close();
Note: See TracBrowser for help on using the repository browser.