source: trunk/FACT++/scripts/takeRun.js@ 16991

Last change on this file since 16991 was 16860, checked in by tbretz, 11 years ago
Added 'use strict'
File size: 7.7 KB
Line 
1'use strict';
2
3// ================================================================
4// Code related to monitoring the fad system
5// ================================================================
6
7var incomplete = 0;
8
9sub_incomplete.onchange = function(evt)
10{
11 if (!evt.data)
12 return;
13
14 var inc = evt.obj['incomplete'];
15 if (!inc || inc>0xffffffffff)
16 return;
17
18 if (incomplete>0)
19 return;
20
21 if (dim.state("MCP").name!="TakingData")
22 return;
23
24 console.out("");
25 dim.log("Incomplete event ["+inc+","+incomplete+"] detected, sending MCP/STOP");
26
27 incomplete = inc;
28 dim.send("MCP/STOP");
29}
30
31// ================================================================
32// Code related to taking data
33// ================================================================
34
35/**
36 * reconnect to problematic FADs
37 *
38 * Dis- and Reconnects to FADs, found to be problematic by call-back function
39 * onchange() to have a different CONNECTION value than 66 or 67.
40 *
41 * @returns
42 * a boolean is returned.
43 * reconnect returns true if:
44 * * nothing needed to be reset --> no problems found by onchange()
45 * * the reconnection went fine.
46 *
47 * reconnect *never returns false* so far.
48 *
49 * @example
50 * if (!sub_connections.reconnect())
51 * exit();
52 */
53function reconnect(list, txt)
54{ /*
55 var reset = [ ];
56
57 for (var i=0; i<list.length; i++)
58 {
59 console.out(" FAD %2d".$(list[i])+" lost during "+txt);
60 reset.push(parseInt(list[i]/10));
61 }
62
63 reset = reset.filter(function(elem,pos){return reset.indexOf(elem)==pos;});
64
65 console.out("");
66 console.out(" FADs belong to crate(s): "+reset);
67 console.out("");
68*/
69 console.out("");
70 dim.log("Trying automatic reconnect ["+txt+",n="+list.length+"]...");
71
72 if (list.length>3)
73 throw new Error("Too many boards to be reconnected. Please check what happened.");
74
75 for (var i=0; i<list.length; i++)
76 {
77 console.out(" ...disconnect "+list[i]);
78 dim.send("FAD_CONTROL/DISCONNECT", list[i]);
79 }
80
81 console.out(" ...waiting for 3s");
82 v8.sleep(3000);
83
84 for (var i=0; i<list.length; i++)
85 {
86 console.out(" ...reconnect "+list[i]);
87 dim.send("FAD_CONTROL/CONNECT", list[i]);
88 }
89
90 console.out(" ...waiting for 1s");
91
92 // Wait for one second to bridge possible pending connects
93 v8.sleep(1000);
94
95 console.out(" ...checking connection");
96
97 // Wait for FAD_CONTROL to realize that all boards are connected
98 // FIXME: Wait for '40' boards being connected instead
99 try
100 {
101 dim.wait("FAD_CONTROL", "Connected", 3000);
102 }
103 catch (e)
104 {
105 console.out("");
106 console.out(" + FAD_CONTROL: "+dim.state("FAD_CONTROL").name);
107 console.out("");
108 throw e;
109 }
110
111 // Wait also for MCP to have all boards connected again
112 dim.wait("MCP", "Idle", 3000);
113
114 dim.log("Automatic reconnect successfull.");
115 console.out("");
116}
117
118function takeRun(type, count, time)
119{
120 if (!count)
121 count = -1;
122 if (!time)
123 time = -1;
124
125 var nextrun = sub_startrun.get().obj['next'];
126 dim.log("Take run %3d".$(nextrun)+": N="+count+" T="+time+"s ["+type+"]");
127
128 // FIXME: Replace by callback?
129 //
130 // DN: I believe instead of waiting for 'TakingData' one could split this
131 // up into two checks with an extra condition:
132 // if type == 'data':
133 // wait until ThresholdCalibration starts:
134 // --> this time should be pretty identical for each run
135 // if this takes longer than say 3s:
136 // there might be a problem with one/more FADs
137 //
138 // wait until "TakingData":
139 // --> this seems to take even some minutes sometimes...
140 // (might be optimized rather soon, but still in the moment...)
141 // if this takes way too long:
142 // there might be something broken,
143 // so maybe a very high time limit is ok here.
144 // I think there is not much that can go wrong,
145 // when the Thr-Calib has already started. Still it might be nice
146 // If in the future RateControl is written so to find out that
147 // in case the threshold finding algorithm does
148 // *not converge as usual*
149 // it can complain, and in this way give a hint, that the weather
150 // might be a little bit too bad.
151 // else:
152 // wait until "TakingData":
153 // --> in a non-data run this time should be pretty short again
154 // if this takes longer than say 3s:
155 // there might be a problem with one/more FADs
156 //
157
158 // Use this if you use the rate control to calibrate by rates
159 //if (!dim.wait("MCP", "TakingData", -300000) )
160 //{
161 // throw new Error("MCP took longer than 5 minutes to start TakingData"+
162 // "maybe this idicates a problem with one of the FADs?");
163 //}
164
165 // Here we could check and handle fad losses
166
167 incomplete = 0;
168
169 var start = true;
170
171 for (var n=0; n<3; n++)
172 {
173 if (start)
174 dim.send("MCP/START", time, count, type);
175
176 try
177 {
178 dim.wait("MCP", "TakingData", 15000);
179 break;
180 }
181 catch (e)
182 {
183 if (dim.state("MCP").name=="TriggerOn" &&
184 dim.state("FAD_CONTROL").name=="Connected" &&
185 dim.state("FTM_CONTROL").name=="TriggerOn")
186 {
187 console.out("");
188 console.out("Waiting for TakingData timed out. Everything looks ok, but file not yet open... waiting once more.");
189 start = false;
190 continue;
191 }
192
193 start = true;
194
195 console.out("");
196 console.out(" + MCP: "+dim.state("MCP").name);
197 console.out(" + FAD_CONTROL: "+dim.state("FAD_CONTROL").name);
198 console.out(" + FTM_CONTROL: "+dim.state("FTM_CONTROL").name);
199 console.out("");
200
201 if (dim.state("MCP").name!="Configuring3" ||
202 (dim.state("FAD_CONTROL").name!="Configuring1" &&
203 dim.state("FAD_CONTROL").name!="Configuring2"))
204 throw e;
205
206 console.out("");
207 console.out("Waiting for fadctrl to get configured timed out... checking for in-run FAD loss.");
208
209 var con = sub_connections.get();
210 var stat = con.obj['status'];
211
212 console.out("Sending MCP/RESET");
213 dim.send("MCP/RESET");
214
215 dim.wait("FTM_CONTROL", "Valid", 3000);
216 dim.wait("FAD_CONTROL", "Connected", 3000);
217 dim.wait("MCP", "Idle", 3000);
218
219 var list = [];
220 for (var i=0; i<40; i++)
221 if (stat[i]!=0x43)
222 list.push(i);
223
224 reconnect(list, "configuration");
225
226 if (n==2)
227 throw e;
228
229 //dim.wait("MCP", "Idle", 3000);
230 }
231 }
232
233 dim.wait("MCP", "Idle", time>0 ? time*1250 : undefined); // run time plus 25%
234
235 if (incomplete)
236 {
237 console.out("");
238 console.out(" - MCP: "+dim.state("MCP").name);
239 console.out(" - FAD_CONTROL: "+dim.state("FAD_CONTROL").name);
240 console.out(" - FTM_CONTROL: "+dim.state("FTM_CONTROL").name);
241
242 dim.wait("FTM_CONTROL", "Valid", 3000);
243 dim.wait("FAD_CONTROL", "Connected", 3000);
244 dim.wait("MCP", "Idle", 3000);
245
246 var str = incomplete.toString(2);
247 var len = str.length;
248
249 var list = [];
250 for (var i=0; i<str.length; i++)
251 if (str[str.length-i-1]=='1')
252 list.push(i);
253
254 reconnect(list, "data taking");
255
256 return false;
257 }
258
259 return true;
260}
Note: See TracBrowser for help on using the repository browser.