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

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