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

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