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

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