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

Last change on this file since 20023 was 18898, checked in by dneise, 7 years ago
hack to allow for data taking while agilent not available, done 13.02.17, never checked in until today ... why???
File size: 7.6 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, idx) { sum+=val; sq+=val*val; if (val>max) max=val; if (val<min) min=val; if (func && func(val, idx)) 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
30if (dim.state("FTM_CONTROL").name=="TriggerOn")
31{
32 dim.send("FTM_CONTROL/STOP_TRIGGER");
33 dim.wait("FTM_CONTROL", "Valid");
34}
35
36
37include('scripts/CheckStates.js');
38
39var table =
40[
41 [ "MCP", [ "Idle" ] ],
42 [ "AGILENT_CONTROL_24V", [ "VoltageOn" ] ],
43 [ "AGILENT_CONTROL_50V", [ "VoltageOn" ] ],
44// [ "AGILENT_CONTROL_80V", [ "VoltageOn" ] ],
45 [ "AGILENT_CONTROL_80V", [ "VoltageOn", "Disconnected" ] ], //hack to allow for data taking while agilent not available
46 [ "FTM_CONTROL", [ "Valid" ] ],
47 [ "FAD_CONTROL", [ "Connected", "RunInProgress" ] ],
48 [ "BIAS_CONTROL", [ "Disconnected", "VoltageOff" ] ],
49 [ "DATA_LOGGER", [ "WaitForRun", "NightlyFileOpen", "Logging" ] ],
50];
51
52console.out("Checking states.");
53if (!checkStates(table))
54{
55 throw new Error("Something unexpected has happened. One of the servers",
56 "is in a state in which it should not be. Please,",
57 "try to find out what happened...");
58}
59
60// ===================================================================
61
62include('scripts/Hist1D.js');
63include('scripts/Hist2D.js');
64
65console.out("Checking power on time");
66
67var service_drs = new Subscription("FAD_CONTROL/DRS_RUNS");
68
69var runs = service_drs.get(5000, false);
70//if (!runs)
71// throw new Error("Could not connect to FAD_CONTROL/DRS_RUNS");
72
73var power = dim.state("AGILENT_CONTROL_50V").time;
74var now = new Date();
75
76var diff = (now-runs.time)/3600000;
77
78console.out(" * Now: "+now);
79console.out(" * Last power cycle: "+power);
80console.out(" * Last DRS calib set: "+(runs.data?runs.time:"none"));
81
82
83if (1)//diff>8 && now.getHours()>16 || runs.time<power)
84{
85 console.out("Checking send.");
86 checkSend(["FAD_CONTROL", "MCP", "RATE_CONTROL"]);
87 console.out("Checking send: done");
88
89 //console.out("Most probablay the camera has not been checked for underflows yet.");
90
91 var service_event = new Subscription("FAD_CONTROL/EVENT_DATA");
92
93 dim.send("FAD_CONTROL/START_DRS_CALIBRATION");
94 dim.send("FAD_CONTROL/SET_FILE_FORMAT", 0);
95
96 var sub_runs = new Subscription("FAD_CONTROL/RUNS");
97 var sruns = sub_runs.get(5000, false);
98
99 if (dim.state("FAD_CONTROL").name=="RunInProgress" || sruns.qos==1)
100 {
101 dim.send("FAD_CONTROL/CLOSE_OPEN_FILES");
102 dim.wait("FAD_CONTROL", "Connected", 3000);
103
104 console.out("Waiting for open files to be closed...");
105 v8.timeout(60000, function() { if (sub_runs.get(0, false).qos==0) return true; });
106
107 // Although the file should be closed now, the processing might still be on-going
108 // and delayed events might be received. The only fix for that issue is to
109 // add the run number to the data we are waiting for
110 v8.sleep(5000);
111 }
112
113 sub_runs.close();
114
115 console.out("Starting drs-gain... waiting for new event");
116
117 var sub_startrun = new Subscription("FAD_CONTROL/START_RUN");
118 var sub_incomplete = new Subscription("FAD_CONTROL/INCOMPLETE");
119 var sub_connections = new Subscription("FAD_CONTROL/CONNECTIONS");
120 sub_connections.get(5000);
121 sub_startrun.get(5000);
122
123 include('scripts/takeRun.js');
124
125 while (1)
126 {
127 var event_counter = service_event.get(10000, false).counter;
128
129 var stop = function ()
130 {
131 while (1)
132 {
133 if (dim.state("MCP").name=="TakingData" && service_event.get(0, false).counter>event_counter)
134 {
135 dim.send("MCP/STOP");
136 console.out("Sent MCP/STOP.");
137 return;
138 }
139 v8.sleep(100);
140 }
141 }
142
143 var thread = new Thread(250, stop);
144
145 var rc = takeRun("drs-gain");
146
147 thread.kill();
148
149 if (rc)
150 break;
151 }
152
153 console.out("Event received.");
154
155 sub_incomplete.close();
156 sub_connections.close();
157 sub_startrun.close();
158
159
160 // FIXME: Restore DRS calibration in case of failure!!
161 // FAD Re-connect in case of failure?
162 // MCP/RESET in case of failure?
163 // Proper error reporting!
164
165 var event = service_event.get(3000);//, false);
166 service_event.close();
167
168 console.out("Run stopped.");
169
170 dim.send("RATE_CONTROL/STOP"); // GlobalThresholdSet -> Connected
171 dim.wait("MCP", "Idle", 3000);
172
173 var nn = runs.data && runs.data.length>0 && runs.obj['roi']>0 ? runs.obj['run'].reduce(Func.max) : -1;
174 if (nn>0)
175 {
176 var night = runs.obj['night'];
177
178 var yy = night/10000;
179 var mm = (night/100)%100;
180 var dd = night%100;
181
182 var filefmt = "/loc_data/raw/%d/%02d/%02d/%8d_%03d.drs.fits";
183
184 dim.log("Trying to restore last DRS calibration #"+nn+" ["+runs.time+"; "+night+"]");
185
186 // FIXME: Timeout
187 var drs_counter = service_drs.get(0, false).counter;
188 dim.send("FAD_CONTROL/LOAD_DRS_CALIBRATION", filefmt.$(yy, mm, dd, night, nn));
189
190 try
191 {
192 var now = new Date();
193 v8.timeout(3000, function() { if (service_drs.get(0, false).counter>drs_counter) return true; });
194 dim.log("Last DRS calibration restored ["+(new Date()-now)/1000+"s]");
195 }
196 catch (e)
197 {
198 console.warn("Restoring last DRS calibration failed.");
199 }
200 }
201
202 var hist = Hist2D(16, -2048.5, 2048.5, 11, -10, 100);
203
204 var data = event.obj;
205
206 for (var i=0; i<1440; i++)
207 hist.fill(data.avg[i], isNaN(data.rms[i])?-1:data.rms[i]);
208
209 hist.print();
210
211 var stat0 = Func.stat(data.avg, function(val, idx) { if (val<600) console.out(" PIX[hw="+idx+"]="+val); return val<600; });
212 var stat1 = Func.stat(data.rms);
213
214 console.out("Avg[min]=%.1f".$(stat0.min));
215 console.out("Avg[avg]=%.1f +- %.1f".$(stat0.avg, stat0.rms));
216 console.out("Avg[max]=%.1f".$(+stat0.max));
217 console.out("Avg[cnt]="+stat0.count);
218 console.out("");
219 console.out("Rms[min]=%.1f".$(stat1.min));
220 console.out("Rms[avg]=%.1f +- %.1f".$(stat1.avg, stat1.rms));
221 console.out("Rms[max]=%.1f".$(stat1.max));
222 console.out(("%78s\n".$("")).replace(/ /g, "="));
223
224 // OK UNDERFLOW
225 // ------------------------------------------------------
226 // Avg[min]=722.0 Avg[min]=-380.0
227 // Avg[avg]=815.9 +- 45.9 Avg[avg]= 808.0 +- 102.0
228 // Avg[max]=930.5 Avg[max]= 931.1
229 // Avg[cnt]=0 Avg[cnt]= 9
230
231 // Rms[min]=14.0 Rms[min]=13.9
232 // Rms[avg]=16.5 +- 1.6 Rms[avg]=18.8 +- 26.8
233 // Rms[max]=44.0 Rms[max]=382.1
234
235 if (stat0.count>0)
236 {
237 if (stat0.count>8)
238 throw new Error("Underflow condition detected in about "+parseInt(stat0.count/9+.5)+" DRS.");
239
240 console.warn("There is probably an underflow condition in one DRS... please check manually.");
241 }
242}
243
244service_drs.close();
Note: See TracBrowser for help on using the repository browser.