1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
//
// File: BeamPixelDipServer.java (W.Badgett, G.Y.Jeng)
//
package cms.dip.tracker.beamspot;
import cern.dip.*;
import java.lang.Thread;
import java.io.*;
import java.text.*;
import java.util.Date;
import java.util.BitSet;
public class BeamPixelDipServer
extends Thread
implements Runnable,DipPublicationErrorHandler
{
// Input parameters
public static boolean verbose = false;
public static boolean overwriteQuality = true; //if true, change quality to qualities[0]
public static String subjectCMS = "dip/CMS/Tracker/BeamPixel";
public static String subjectLHC = "dip/CMS/LHCTEST/LuminousRegion";
// public static String subjectDummy = "dummy";//to have same # of arug as in BeamSpot file as same class run both of them with same # of arguemnts
public static String sourceFile = "/nfshome0/dqmpro/BeamMonitorDQM/BeamPixelResults.txt";
public static int[] timeoutLS = {5,10}; //LumiSections
public final static boolean publishStatErrors = true;
public final static int secPerLS = 23;
public final static int rad2urad = 1000000;
public final static int cm2um = 10000;
public final static int cm2mm = 10;
public final static String[] qualities = {"Uncertain","Bad","Good"};
DipFactory dip;
DipData messageCMS;
DipData messageLHC;
DipPublication publicationCMS;
DipPublication publicationLHC;
int runnum = 0;
String startTime = getDateTime();
String endTime = getDateTime();
long startTimeStamp = 0;
long endTimeStamp = 0;
String lumiRange = "0 - 0";
String quality = "Uncertain";
int type = -1;
float x = 0;
float y = 0;
float z = 0;
float dxdz = 0;
float dydz = 0;
float err_x = 0;
float err_y = 0;
float err_z = 0;
float err_dxdz = 0;
float err_dydz = 0;
float width_x = 0;
float width_y = 0;
float sigma_z = 0;
float err_width_x = 0;
float err_width_y = 0;
float err_sigma_z = 0;
float Size[] = new float[3];
float Centroid[] = new float[3];
float Tilt[] = new float[2];
boolean keepRunning;
long lastFitTime = 0;
long lastModTime = 0;
BitSet alive = new BitSet(8);
int idleTime = 0;
int lsCount = 0;
public void handleException(DipPublication publication,
DipException e)
{
System.out.println("handleException: " + getDateTime());
System.out.println("Error handler for " +
publication.getTopicName() +
" called because " + e.getMessage());
e.printStackTrace();
}
public void run()
{
java.util.Date now = new java.util.Date();
try
{
dip = Dip.create("CmsBeamPixel_"+now.getTime());
System.out.println("Server Started at " + getDateTime());
System.out.println("Making publication " + subjectCMS);
publicationCMS = dip.createDipPublication(subjectCMS, this);
messageCMS = dip.createDipData();
System.out.println("Making publication " + subjectLHC);
publicationLHC = dip.createDipPublication(subjectLHC, this);
messageLHC = dip.createDipData();
keepRunning = true;
}
catch ( DipException e )
{
System.err.println("DipException: " + getDateTime());
keepRunning = false;
}
quality = qualities[0];
while (keepRunning)
{
try
{
File logFile = new File(sourceFile);
if (!logFile.exists()) {
polling();
continue;
}
else {
FileReader fr = new FileReader(logFile);
BufferedReader br = new BufferedReader(fr);
lastModTime = logFile.lastModified();
if (lastFitTime == 0)
lastFitTime = lastModTime;
if (logFile.length() == 0) {
lastFitTime = lastModTime;
if (!alive.get(6)) {
System.out.println("New run starts");
if (verbose) System.out.println("Initial lastModTime = " + getDateTime(lastModTime));
alive.flip(6);
}
}
if (lastModTime > lastFitTime) {
if (verbose) {
System.out.println("Time of last fit = " + getDateTime(lastFitTime));
System.out.println("Time of current fit = " + getDateTime(lastModTime));
}
lastFitTime = lastModTime;
if (logFile.length() > 0) {
if (verbose) System.out.println("Read record from " + sourceFile);
if (readRcd(br)) {
trueRcd();
alive.clear();
alive.flip(7);
}
else fakeRcd();
if (verbose) System.out.println("Publish new record");
lsCount = 0;
idleTime = 0;
}
br.close();
fr.close();
}
else{
br.close();
fr.close();
polling();
continue;
}
}
// Quality of the publish results
if (overwriteQuality) publishRcd(qualities[0],"Testing",true,true);
else if (quality == qualities[1]) publishRcd(quality,"No BeamFit or Fit Fails",true,true);
else publishRcd(quality,"",true,true);
} catch (IOException e) {
System.err.println("IOException: " + getDateTime());
e.printStackTrace();
};
}
}
private void polling()
{
if (lsCount != 0 && lsCount%60 == 0) {
System.out.println("Waiting for data..." + getDateTime());
}
try { Thread.sleep(1000); }//every sec
catch(InterruptedException e) {
System.err.println("InterruptedException: " + getDateTime());
e.printStackTrace();
keepRunning = false;
}
lsCount++;
idleTime++;
if ((lsCount%(timeoutLS[0]*secPerLS) == 0)
&& (lsCount%(timeoutLS[1]*secPerLS) != 0)) {//fist time out
if (!alive.get(1)) alive.flip(1);
if (!alive.get(2)) {
if (!alive.get(7)) fakeRcd();
else trueRcd();
publishRcd("Uncertain","No new data for " + idleTime + " seconds",false,false);
}
else {
fakeRcd();
publishRcd("Bad","No new data for " + idleTime + " seconds",false,false);
}
}
else if (lsCount%(timeoutLS[1]*secPerLS) == 0) {//second time out
if (!alive.get(2)) alive.flip(2);
//if(!alive.get(7))
fakeRcd();
//else trueRcd();
publishRcd("Bad","No new data for " + idleTime + " seconds",false,false);
}
}
private boolean readRcd(BufferedReader file_)
{
int nthLnInRcd = 0;
String record = new String();
boolean rcdQlty = false;
try
{
while ((record = file_.readLine()) != null) {
//System.out.println(record);
nthLnInRcd ++;
String[] tmp;
tmp = record.split("\\s");
switch(nthLnInRcd) {
case 1:
if (!record.startsWith("Run")){
System.out.println("Reading of results text file interrupted. " + getDateTime());
return false;
}
runnum = new Integer(tmp[1]);
System.out.println("Run: " + runnum);
break;
case 2:
//startTime = record.substring(15);
startTime = tmp[1]+" "+tmp[2]+" "+tmp[3];
startTimeStamp = new Long(tmp[4]);
//System.out.println("Time of begin run: " + startTime);
break;
case 3:
//endTime = record.substring(13);
endTime = tmp[1]+" "+tmp[2]+" "+tmp[3];
endTimeStamp = new Long(tmp[4]);
System.out.println("Time of fit: " + endTime);
break;
case 4:
lumiRange = record.substring(10);
System.out.println("LS: " + lumiRange);
break;
case 5:
type = new Integer(tmp[1]);
if (overwriteQuality) quality = qualities[0];
else if (type >= 2) quality = qualities[2];
else quality = qualities[1];
break;
case 6:
x = new Float(tmp[1]);
System.out.println("x0 = " + x + " [cm]");
break;
case 7:
y = new Float(tmp[1]);
System.out.println("y0 = " + y + " [cm]");
break;
case 8:
z = new Float(tmp[1]);
System.out.println("z0 = " + z + " [cm]");
break;
case 9:
sigma_z = new Float(tmp[1]);
System.out.println("sigma_z = " + sigma_z + " [cm]");
break;
case 10:
dxdz = new Float(tmp[1]);
break;
case 11:
dydz = new Float(tmp[1]);
break;
case 12:
width_x = new Float(tmp[1]);
break;
case 13:
width_y = new Float(tmp[1]);
break;
case 14:
err_x = new Float(Math.sqrt(Double.parseDouble(tmp[1])));
//System.out.println(err_x);
break;
case 15:
err_y = new Float(Math.sqrt(Double.parseDouble(tmp[2])));
//System.out.println(err_y);
break;
case 16:
err_z = new Float(Math.sqrt(Double.parseDouble(tmp[3])));
//System.out.println(err_z);
break;
case 17:
err_sigma_z = new Float(Math.sqrt(Double.parseDouble(tmp[4])));
//System.out.println(err_sigma_z);
break;
case 18:
err_dxdz = new Float(Math.sqrt(Double.parseDouble(tmp[5])));
//System.out.println(err_dxdz);
break;
case 19:
err_dydz = new Float(Math.sqrt(Double.parseDouble(tmp[6])));
//System.out.println(err_dydz);
break;
case 20:
err_width_x = new Float(Math.sqrt(Double.parseDouble(tmp[7])));
err_width_y = err_width_x;
rcdQlty = true;
System.out.println("End of results");
break;
default:
break;
}
}
file_.close();
}
catch (IOException e) {
System.err.println("IOException: " + getDateTime());
e.printStackTrace();
}
return rcdQlty;
}
private void trueRcd()
{
try
{
Centroid[0] = x*-1*cm2um;
Centroid[1] = y*cm2um;
Centroid[2] = z*-1*cm2mm;
Size[0] = width_x*cm2um;
Size[1] = width_y*cm2um;
Size[2] = sigma_z*cm2mm;
Tilt[0] = dxdz*rad2urad;
Tilt[1] = dydz*-1*rad2urad;
messageCMS.insert("runnum",runnum);
messageCMS.insert("startTime",startTime);
messageCMS.insert("endTime",endTime);
messageCMS.insert("startTimeStamp",startTimeStamp);
messageCMS.insert("endTimeStamp",endTimeStamp);
messageCMS.insert("lumiRange",lumiRange);
messageCMS.insert("quality",quality);
messageCMS.insert("type",type); //Unknown=-1, Fake=0, Tracker=2(Good)
messageCMS.insert("x",x);
messageCMS.insert("y",y);
messageCMS.insert("z",z);
messageCMS.insert("dxdz",dxdz);
messageCMS.insert("dydz",dydz);
messageCMS.insert("width_x",width_x);
messageCMS.insert("width_y",width_y);
messageCMS.insert("sigma_z",sigma_z);
if (publishStatErrors) {
messageCMS.insert("err_x",err_x);
messageCMS.insert("err_y",err_y);
messageCMS.insert("err_z",err_z);
messageCMS.insert("err_dxdz",err_dxdz);
messageCMS.insert("err_dydz",err_dydz);
messageCMS.insert("err_width_x",err_width_x);
messageCMS.insert("err_width_y",err_width_y);
messageCMS.insert("err_sigma_z",err_sigma_z);
}
messageLHC.insert("Size",Size);
messageLHC.insert("Centroid",Centroid);
messageLHC.insert("Tilt",Tilt);
} catch (DipException e){
System.err.println("DipException: " + getDateTime());
System.err.println("Failed to send data because " + e.getMessage());
e.printStackTrace();
}
}
private void fakeRcd()
{
try
{
Centroid[0] = 0;
Centroid[1] = 0;
Centroid[2] = 0;
Size[0] = 0;
Size[1] = 0;
Size[2] = 0;
Tilt[0] = 0;
Tilt[1] = 0;
messageLHC.insert("Size",Size);
messageLHC.insert("Centroid",Centroid);
messageLHC.insert("Tilt",Tilt);
} catch (DipException e){
System.err.println("DipException: " + getDateTime());
System.err.println("Failed to send data because " + e.getMessage());
e.printStackTrace();
}
}
private void publishRcd(String qlty_,String err_, boolean pubCMS_, boolean fitTime_)
{
try
{
try
{
DipTimestamp zeit;
if (fitTime_) {
long epoch = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss zz").parse(endTime).getTime();
zeit = new DipTimestamp(epoch);
}
else zeit = new DipTimestamp();
if(pubCMS_) publicationCMS.send(messageCMS, zeit);
publicationLHC.send(messageLHC, zeit);
} catch (ParseException e) {
System.err.println("ParseException: " + getDateTime());
System.err.println("Publishing failed due to time parsing because " + e.getMessage());
e.printStackTrace();
}
if (qlty_ == qualities[0]) {
if (pubCMS_) publicationCMS.setQualityUncertain(err_);
publicationLHC.setQualityUncertain(err_);
}
else if (qlty_ == qualities[1]) {
if (pubCMS_) publicationCMS.setQualityBad(err_);
publicationLHC.setQualityBad(err_);
}
} catch (DipException e){
System.err.println("DipException: " + getDateTime());
System.err.println("Failed to send data because " + e.getMessage());
e.printStackTrace();
}
}
private String getDateTime()
{
DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
Date date = new Date();
return dateFormat.format(date);
}
private String getDateTime(long epoch)
{
DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
Date date = new Date(epoch);
return dateFormat.format(date);
}
private BeamPixelDipServer(String args[])
{
this.verbose = args[0].matches("true");
this.overwriteQuality = args[1].matches("true");
this.subjectCMS = args[2];
this.subjectLHC = args[3];
this.sourceFile = args[4];
this.timeoutLS[0] = new Integer(args[5]);
this.timeoutLS[1] = new Integer(args[6]);
// this.subjectDummy = args[7];
}
public static void main(String args[])
{
BeamPixelDipServer server = new BeamPixelDipServer(args);
server.start();
}
}
|