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

Last change on this file since 16385 was 16385, checked in by tbretz, 11 years ago
Theoretically, the fadctrl can also hang in Configuring1 because that is when we now wait for the messages to be sent.
File size: 7.2 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 v8.sleep(1000);
90 console.out(" ...checking connection");
91 dim.wait("FAD_CONTROL", "Connected", 3000);
92 console.out("");
93}
94
95function takeRun(type, count, time)
96{
97 if (!count)
98 count = -1;
99 if (!time)
100 time = -1;
101
102 var nextrun = sub_startrun.get().obj['next'];
103 console.out(" Take run %3d".$(nextrun)+": N="+count+" T="+time+"s ["+type+"]");
104
105 // FIXME: Replace by callback?
106 //
107 // DN: I believe instead of waiting for 'TakingData' one could split this
108 // up into two checks with an extra condition:
109 // if type == 'data':
110 // wait until ThresholdCalibration starts:
111 // --> this time should be pretty identical for each run
112 // if this takes longer than say 3s:
113 // there might be a problem with one/more FADs
114 //
115 // wait until "TakingData":
116 // --> this seems to take even some minutes sometimes...
117 // (might be optimized rather soon, but still in the moment...)
118 // if this takes way too long:
119 // there might be something broken,
120 // so maybe a very high time limit is ok here.
121 // I think there is not much that can go wrong,
122 // when the Thr-Calib has already started. Still it might be nice
123 // If in the future RateControl is written so to find out that
124 // in case the threshold finding algorithm does
125 // *not converge as usual*
126 // it can complain, and in this way give a hint, that the weather
127 // might be a little bit too bad.
128 // else:
129 // wait until "TakingData":
130 // --> in a non-data run this time should be pretty short again
131 // if this takes longer than say 3s:
132 // there might be a problem with one/more FADs
133 //
134
135 // Use this if you use the rate control to calibrate by rates
136 //if (!dim.wait("MCP", "TakingData", -300000) )
137 //{
138 // throw new Error("MCP took longer than 5 minutes to start TakingData"+
139 // "maybe this idicates a problem with one of the FADs?");
140 //}
141
142 // Here we could check and handle fad losses
143
144 incomplete = 0;
145
146 var start = true;
147
148 for (var n=0; n<3; n++)
149 {
150 if (start)
151 dim.send("MCP/START", time, count, type);
152
153 try
154 {
155 dim.wait("MCP", "TakingData", 15000);
156 break;
157 }
158 catch (e)
159 {
160 if (dim.state("MCP").name=="TriggerOn" &&
161 dim.state("FAD_CONTROL").name=="Connected" &&
162 dim.state("FTM_CONTROL").name=="TriggerOn")
163 {
164 console.out("");
165 console.out("Waiting for TakingData timed out. Everything looks ok, but file not yet open... waiting once more.");
166 start = false;
167 continue;
168 }
169
170 start = true;
171
172 console.out("");
173 console.out(" + MCP: "+dim.state("MCP").name);
174 console.out(" + FAD_CONTROL: "+dim.state("FAD_CONTROL").name);
175 console.out(" + FTM_CONTROL: "+dim.state("FTM_CONTROL").name);
176 console.out("");
177
178 if (dim.state("MCP").name!="Configuring3" ||
179 (dim.state("FAD_CONTROL").name!="Configuring1" &&
180 dim.state("FAD_CONTROL").name!="Configuring2"))
181 throw e;
182
183 console.out("");
184 console.out("Waiting for fadctrl to get configured timed out... checking for in-run FAD loss.");
185
186 var con = sub_connections.get();
187 var stat = con.obj['status'];
188
189 console.out("Sending MCP/RESET");
190 dim.send("MCP/RESET");
191
192 dim.wait("FTM_CONTROL", "Idle", 3000);
193 dim.wait("FAD_CONTROL", "Connected", 3000);
194 dim.wait("MCP", "Idle", 3000);
195
196 var list = [];
197 for (var i=0; i<40; i++)
198 if (stat[i]!=0x43)
199 list.push(i);
200
201 reconnect(list, "configuration");
202
203 if (n==2)
204 throw e;
205
206 dim.wait("MCP", "Idle", 3000);
207 }
208 }
209
210 dim.wait("MCP", "Idle", time>0 ? time*1250 : undefined); // run time plus 25%
211
212 if (incomplete)
213 {
214 console.out("");
215 console.out(" - MCP: "+dim.state("MCP").name);
216 console.out(" - FAD_CONTROL: "+dim.state("FAD_CONTROL").name);
217 console.out(" - FTM_CONTROL: "+dim.state("FTM_CONTROL").name);
218
219 dim.wait("MCP", "Idle", 3000);
220 dim.wait("FTM_CONTROL", "Idle", 3000);
221
222 // Necessary to allow the disconnect, reconnect
223 dim.send("FAD_CONTROL/CLOSE_OPEN_FILES");
224 dim.wait("FAD_CONTROL", "Connected", 3000);
225
226 var list = [];
227 for (var i=0; i<40; i++)
228 if (incomplete&(1<<i))
229 list.push(i);
230
231 reconnect(list, "data taking");
232
233 return false;
234 }
235
236 return true;
237}
238
239undefined;
Note: See TracBrowser for help on using the repository browser.