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

Last change on this file since 15047 was 15038, checked in by tbretz, 12 years ago
display no time if no drs calib
File size: 5.7 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/*
63runs = {
64 "name" : "FAD_CONTROL/DRS_RUNS",
65 "format" : "I:1;I:3",
66 "qos" : 0,
67 "size" : 16,
68 "counter" : 0,
69 "time" : new Date("2012-11-20T05:23:52.804Z"),
70 "data" : [300,[148,149,154]]
71 };
72*/
73
74// Wait for agilent to be online with state information
75// FIXME: timeout
76while (!dim.state("AGILENT_CONTROL"))
77 v8.sleep();
78
79var power = dim.state("AGILENT_CONTROL").time;
80var now = new Date();
81
82var diff = (now-runs.time)/3600000;
83
84console.out(" * Now: "+now);
85console.out(" * Last power cycle: "+power);
86console.out(" * Last DRS calib set: "+(runs.data?runs.time:"none"));
87//console.out(JSON.stringify(dim.state("AGILENT_CONTROL")));
88
89
90if (1)//diff>8 && now.getHours()>16 || runs.time<power)
91{
92 //console.out("Most probablay the camera has not been checked for underflows yet.");
93
94 // Wait for dim.send to be ready
95 // FIXME: timeout
96 while (!dim.send("FAD_CONTROL"))
97 v8.sleep();
98
99 // FAD_CONTROL/RAW_DATA:
100 // roi,roi_tm,num_fad,num_ftm,type,num_boards,error,
101 // dummy,time,time_board,start_pix,start_tm,adc
102
103 // FAD_CONTROL/EVENT_DATA
104 // avg,rms,max,pos
105
106 var service_event = new Subscription("FAD_CONTROL/EVENT_DATA");
107 var event = service_event.get(5000, false);
108 //if (!event)
109 // throw new Error("Could not connect to FAD_CONTROL/EVENT_DATA");
110
111 var event_counter = event.counter;
112
113 dim.send("FAD_CONTROL/START_DRS_CALIBRATION");
114 dim.send("FAD_CONTROL/SET_FILE_FORMAT", 0);
115
116 console.out("Starting drs-gain");
117 dim.send("MCP/START", -1, 3, "drs-gain");
118
119 // FIXME: add a timeout here (10s)
120 console.out("Waiting for new event");
121 var d = new Date();
122 while (event_counter == service_event.get(0, false).counter)
123 v8.sleep();
124
125 console.out("Event received: "+(new Date()-d)+"ms");
126
127 console.out("Stop run");
128 dim.send("MCP/STOP");
129 while (!dim.send("RATE_CONTROL"))
130 {
131 v8.sleep(800);
132 console.out("waiting for RATE_CONTROL to send the necessary format information");
133 }
134 dim.send("RATE_CONTROL/STOP"); // GlobalThresholdSet -> Connected
135 dim.wait("MCP", "Idle", 3000);
136
137 var nn = runs.data && runs.data[0]>0 ? runs.data[1].reduce(Func.max) : -1;
138 if (nn>0)
139 {
140 console.out("Trying to restore last DRS calibration #"+nn+" ["+runs.time+"]");
141
142 var night = new Date(runs.time-1000*60*60*12);
143 var yy = night.getUTCFullYear();
144 var mm = night.getUTCMonth()+1;
145 var dd = night.getUTCDate();
146
147 var filefmt = "/loc_data/raw/%d/%02d/%02d/%4d%02d%02d_%03d.drs.fits";
148
149 // FIXME: Timeout
150 var drs_counter = service_drs.get(0, false);
151 dim.send("FAD_CONTROL/LOAD_DRS_CALIBRATION", filefmt.$(yy, mm, dd, yy, mm, dd, nn));
152 while (drs_counter == service_drs.get(0, false).counter)
153 v8.sleep();
154 }
155
156 //console.out(JSON.stringify(service_drs.get(0, false)));
157
158 event = service_event.get(0, false);
159
160 service_event.close();
161
162 var hist = Hist2D(16, -2048.5, 2048.5, 11, -10, 100);
163
164 for (var i=0; i<1440; i++)
165 hist.fill(event.data[0][i], isNaN(event.data[1][i])?-1:event.data[1][i]);
166
167 //console.out(("%78s".$("")).replace(/ /g, "-"));
168 hist.print();
169 //console.out(("%78s".$("")).replace(/ /g, "-"));
170
171 // Could also be <512
172 var stat0 = Func.stat(event.data[0], function(val) { if (val<0) console.out(" VAL="+val); return val<0; });
173 var stat1 = Func.stat(event.data[1]);
174
175 console.out("Avg[min]=%.1f".$(stat0.min));
176 console.out("Avg[avg]=%.1f +- %.1f".$(stat0.avg, stat0.rms));
177 console.out("Avg[max]=%.1f".$(+stat0.max));
178 console.out("Avg[cnt]="+stat0.count);
179 console.out("");
180 console.out("Rms[min]=%.1f".$(stat1.min));
181 console.out("Rms[avg]=%.1f +- %.1f".$(stat1.avg, stat1.rms));
182 console.out("Rms[max]=%.1f".$(stat1.max));
183 console.out(("%78s\n".$("")).replace(/ /g, "="));
184}
185
186service_drs.close();
Note: See TracBrowser for help on using the repository browser.