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

Last change on this file since 16091 was 16060, checked in by tbretz, 12 years ago
Code from Main.js related to taking a run including the solution for possible drs problems.
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!="Configuring2")
180 throw e;
181
182 console.out("");
183 console.out("Waiting for fadctrl to get configured timed out... checking for in-run FAD loss.");
184
185 var con = sub_connections.get();
186 var stat = con.obj['status'];
187
188 console.out("Sending MCP/RESET");
189 dim.send("MCP/RESET");
190
191 dim.wait("FTM_CONTROL", "Idle", 3000);
192 dim.wait("FAD_CONTROL", "Connected", 3000);
193 dim.wait("MCP", "Idle", 3000);
194
195 var list = [];
196 for (var i=0; i<40; i++)
197 if (stat[i]!=0x43)
198 list.push(i);
199
200 reconnect(list, "configuration");
201
202 if (n==2)
203 throw e;
204
205 dim.wait("MCP", "Idle", 3000);
206 }
207 }
208
209 dim.wait("MCP", "Idle", time>0 ? time*1250 : undefined); // run time plus 25%
210
211 if (incomplete)
212 {
213 console.out("");
214 console.out(" - MCP: "+dim.state("MCP").name);
215 console.out(" - FAD_CONTROL: "+dim.state("FAD_CONTROL").name);
216 console.out(" - FTM_CONTROL: "+dim.state("FTM_CONTROL").name);
217
218 dim.wait("MCP", "Idle", 3000);
219 dim.wait("FTM_CONTROL", "Idle", 3000);
220
221 // Necessary to allow the disconnect, reconnect
222 dim.send("FAD_CONTROL/CLOSE_OPEN_FILES");
223 dim.wait("FAD_CONTROL", "Connected", 3000);
224
225 var list = [];
226 for (var i=0; i<40; i++)
227 if (incomplete&(1<<i))
228 list.push(i);
229
230 reconnect(list, "data taking");
231
232 return false;
233 }
234
235 return true;
236}
237
238undefined;
Note: See TracBrowser for help on using the repository browser.