File indexing completed on 2024-04-06 12:06:53
0001
0002
0003
0004
0005
0006
0007 package cms.dip.tracker.beamspot;
0008
0009 import cern.dip.*;
0010 import java.lang.Thread;
0011 import java.io.*;
0012 import java.text.*;
0013 import java.util.Date;
0014 import java.util.BitSet;
0015
0016 public class BeamSpotDipServer
0017 extends Thread
0018 implements Runnable,DipPublicationErrorHandler
0019 {
0020
0021 public static boolean verbose = false;
0022 public static boolean overwriteQuality = true;
0023 public static String subjectCMS = "dip/CMS/Tracker/BeamSpot";
0024 public static String subjectLHC = "dip/CMS/LHC/LuminousRegion";
0025 public static String subjectPV = "dip/CMS/Tracker/PrimaryVertices";
0026 public static String sourceFile = "/nfshome0/dqmpro/BeamMonitorDQM/BeamFitResults.txt";
0027 public static String sourceFile1 = "/nfshome0/dqmpro/BeamMonitorDQM/BeamFitResults_TkStatus.txt";
0028 public static int[] timeoutLS = {1,2};
0029
0030
0031 public final static String[] qualities = {"Uncertain","Bad","Good"};
0032 public final static boolean publishStatErrors = true;
0033 public final static int secPerLS = 23;
0034 public final static int rad2urad = 1000000;
0035 public final static int cm2um = 10000;
0036 public final static int cm2mm = 10;
0037 public final static int intLS = 1;
0038
0039
0040 public final static double[] trans = {-0.09,-0.11,-0.12};
0041 public final static double[] angles = {-0.00002,-0.00016,-0.00122};
0042 public final static double[] rotX = {Math.cos(angles[0]),Math.sin(angles[0]),Math.tan(angles[0])};
0043 public final static double[] rotY = {Math.cos(angles[1]),Math.sin(angles[1]),Math.tan(angles[1])};
0044 public final static double[] rotZ = {Math.cos(angles[2]),Math.sin(angles[2]),Math.tan(angles[2])};
0045
0046
0047 DipFactory dip;
0048 DipData messageCMS;
0049 DipData messageLHC;
0050 DipData messagePV;
0051 DipPublication publicationCMS;
0052 DipPublication publicationLHC;
0053 DipPublication publicationPV;
0054
0055 int runnum = 0;
0056 String startTime = getDateTime();
0057 String endTime = getDateTime();
0058 long startTimeStamp = 0;
0059 long endTimeStamp = 0;
0060 String lumiRange = "0 - 0";
0061 String quality = "Uncertain";
0062 int type = -1;
0063 float x = 0;
0064 float y = 0;
0065 float z = 0;
0066 float dxdz = 0;
0067 float dydz = 0;
0068 float err_x = 0;
0069 float err_y = 0;
0070 float err_z = 0;
0071 float err_dxdz = 0;
0072 float err_dydz = 0;
0073 float width_x = 0;
0074 float width_y = 0;
0075 float sigma_z = 0;
0076 float err_width_x = 0;
0077 float err_width_y = 0;
0078 float err_sigma_z = 0;
0079
0080 int events = 0;
0081 float meanPV = 0;
0082 float err_meanPV = 0;
0083 float rmsPV = 0;
0084 float err_rmsPV = 0;
0085 int maxPV = 0;
0086 int nPV = 0;
0087
0088 float Size[] = new float[3];
0089 float Centroid[] = new float[3];
0090 float Tilt[] = new float[2];
0091
0092
0093 boolean keepRunning;
0094 long lastFitTime = 0;
0095 long lastModTime = 0;
0096 BitSet alive = new BitSet(8);
0097 int idleTime = 0;
0098 int lsCount = 0;
0099 int currentLS = 0;
0100
0101 public void handleException(DipPublication publication,
0102 DipException e)
0103 {
0104 System.out.println("handleException: " + getDateTime());
0105 System.out.println("Error handler for " +
0106 publication.getTopicName() +
0107 " called because " + e.getMessage());
0108 e.printStackTrace();
0109 }
0110
0111 public void run()
0112 {
0113 java.util.Date now = new java.util.Date();
0114
0115 try
0116 {
0117 dip = Dip.create("CmsBeamSpot_"+now.getTime());
0118
0119 System.out.println("Server Started at " + getDateTime());
0120 System.out.println("Making publication " + subjectCMS);
0121 publicationCMS = dip.createDipPublication(subjectCMS, this);
0122 messageCMS = dip.createDipData();
0123
0124 System.out.println("Making publication " + subjectLHC);
0125 publicationLHC = dip.createDipPublication(subjectLHC, this);
0126 messageLHC = dip.createDipData();
0127
0128 System.out.println("Making publication " + subjectPV);
0129 publicationPV = dip.createDipPublication(subjectPV, this);
0130 messagePV = dip.createDipData();
0131
0132 trueRcd(false);
0133 publishRcd("UNINITIALIZED","",true,false);
0134 keepRunning = true;
0135 }
0136 catch ( DipException e )
0137 {
0138 System.err.println("DipException [start up]: " + getDateTime());
0139 keepRunning = false;
0140 }
0141
0142 quality = qualities[0];
0143
0144 while (keepRunning)
0145 {
0146 try
0147 {
0148 File logFile = new File(sourceFile);
0149
0150 if (!logFile.exists()) {
0151 if (verbose) System.out.println("Source File: " + sourceFile + " doesn't exist!");
0152 polling();
0153 continue;
0154 }
0155 else {
0156 FileReader fr = new FileReader(logFile);
0157 BufferedReader br = new BufferedReader(fr);
0158 lastModTime = logFile.lastModified();
0159 if (lastFitTime == 0)
0160 lastFitTime = lastModTime;
0161 if (logFile.length() == 0) {
0162 if (lastModTime > lastFitTime) {
0163 String tmp = tkStatus();
0164 System.out.println("New run starts. Run number: " + runnum);
0165 if (verbose) System.out.println("Initial lastModTime = " + getDateTime(lastModTime));
0166 }
0167 lastFitTime = lastModTime;
0168 }
0169
0170 if (lastModTime > lastFitTime) {
0171 if (verbose) {
0172 System.out.println("Time of last fit = " + getDateTime(lastFitTime));
0173 System.out.println("Time of current fit = " + getDateTime(lastModTime));
0174 }
0175 lastFitTime = lastModTime;
0176 if (logFile.length() > 0) {
0177 if (verbose) System.out.println("Read record from " + sourceFile);
0178 if (readRcd(br)) {
0179 trueRcd(true);
0180 alive.clear();
0181 alive.flip(7);
0182 }
0183 else fakeRcd();
0184 if (verbose) System.out.println("Publish new record");
0185 lsCount = 0;
0186 idleTime = 0;
0187 }
0188 br.close();
0189 fr.close();
0190 }
0191 else{
0192 br.close();
0193 fr.close();
0194 polling();
0195 continue;
0196 }
0197 }
0198
0199 if (overwriteQuality) publishRcd(qualities[0],"Testing",true,true);
0200 else if (quality == qualities[1]) publishRcd(quality,"No BeamFit or Fit Fails",true,true);
0201 else publishRcd(quality,"",true,true);
0202
0203 } catch (IOException e) {
0204 System.err.println("IOException [Loop]: " + getDateTime());
0205 e.printStackTrace();
0206 };
0207 }
0208 }
0209
0210 private void polling()
0211 {
0212 if (lsCount != 0 && lsCount%60 == 0) {
0213 System.out.println("Waiting for data..." + getDateTime());
0214 }
0215 try { Thread.sleep(1000); }
0216 catch(InterruptedException e) {
0217 System.err.println("InterruptedException [polling]: " + getDateTime());
0218 e.printStackTrace();
0219 keepRunning = false;
0220 }
0221 lsCount++;
0222 idleTime++;
0223 if ((lsCount%(timeoutLS[0]*secPerLS) == 0)
0224 && (lsCount%(timeoutLS[1]*secPerLS) != 0)) {
0225 if (!alive.get(1)) alive.flip(1);
0226 if (!alive.get(2)) {
0227 if (!alive.get(7)) fakeRcd();
0228 else trueRcd(false);
0229 publishRcd("Uncertain","No new data for " + idleTime + " seconds",false,false);
0230 }
0231 else {
0232 fakeRcd();
0233 String warnMsg = "No new data for " + idleTime + "seconds: ";
0234 warnMsg += tkStatus();
0235 publishRcd("Bad",warnMsg,false,false);
0236 }
0237 }
0238 else if (lsCount%(timeoutLS[1]*secPerLS) == 0) {
0239 if (!alive.get(2)) alive.flip(2);
0240
0241 fakeRcd();
0242
0243 String warnMsg = "No new data for " + idleTime + "seconds: ";
0244 warnMsg += tkStatus();
0245 publishRcd("Bad",warnMsg,false,false);
0246 }
0247 }
0248
0249 String tkStatus()
0250 {
0251 File logFile = new File(sourceFile1);
0252 if (!logFile.exists() || logFile.length() == 0) {
0253 return "No CMS Tracker status available. No DAQ/DQM.";
0254 }
0255 else {
0256 int nthLnInRcd = 0;
0257 String record = new String();
0258 String outstr = new String();
0259 try
0260 {
0261 FileReader fr = new FileReader(logFile);
0262 BufferedReader br = new BufferedReader(fr);
0263 while ((record = br.readLine()) != null) {
0264
0265 nthLnInRcd ++;
0266 String[] tmp;
0267 tmp = record.split("\\s");
0268 switch(nthLnInRcd) {
0269 case 7:
0270 if (!tmp[1].contains("Yes"))
0271 outstr = "CMS Tracker OFF.";
0272 else
0273 outstr = "CMS not taking data or No beam.";
0274 break;
0275 case 8:
0276 runnum = new Integer(tmp[1]);
0277 break;
0278 default:
0279 break;
0280 }
0281 }
0282 br.close();
0283 fr.close();
0284 }
0285 catch (Exception e) {
0286 System.err.println("Exception [tkStatus]: " + getDateTime());
0287 e.printStackTrace();
0288 }
0289 return outstr;
0290 }
0291 }
0292
0293 private boolean readRcd(BufferedReader file_)
0294 {
0295 int nthLnInRcd = 0;
0296 String record = new String();
0297 boolean rcdQlty = false;
0298 try
0299 {
0300 while ((record = file_.readLine()) != null) {
0301
0302 nthLnInRcd ++;
0303 String[] tmp;
0304 tmp = record.split("\\s");
0305 switch(nthLnInRcd) {
0306 case 1:
0307 if (!record.startsWith("Run")){
0308 System.out.println("Reading of results text file interrupted. " + getDateTime());
0309 return false;
0310 }
0311 runnum = new Integer(tmp[1]);
0312 System.out.println("Run: " + runnum);
0313 break;
0314 case 2:
0315 startTime = tmp[1]+" "+tmp[2]+" "+tmp[3];
0316 startTimeStamp = new Long(tmp[4]);
0317
0318 break;
0319 case 3:
0320 endTime = tmp[1]+" "+tmp[2]+" "+tmp[3];
0321 endTimeStamp = new Long(tmp[4]);
0322 System.out.println("TimeStamp of fit: " + endTimeStamp + " [sec]");
0323 System.out.println("Time of fit: " + endTime);
0324 break;
0325 case 4:
0326 lumiRange = record.substring(10);
0327 System.out.println("LS: " + lumiRange);
0328 currentLS = new Integer(tmp[3]);
0329
0330 break;
0331 case 5:
0332 type = new Integer(tmp[1]);
0333 if (overwriteQuality) quality = qualities[0];
0334 else if (type >= 2) quality = qualities[2];
0335 else quality = qualities[1];
0336 break;
0337 case 6:
0338 x = new Float(tmp[1]);
0339 System.out.format("X0 in CMS RF = %13.7f [cm]%n", x);
0340 break;
0341 case 7:
0342 y = new Float(tmp[1]);
0343 System.out.format("Y0 in CMS RF = %13.7f [cm]%n", y);
0344 break;
0345 case 8:
0346 z = new Float(tmp[1]);
0347 System.out.format("Z0 in CMS RF = %13.7f [cm]%n", z);
0348 break;
0349 case 9:
0350 sigma_z = new Float(tmp[1]);
0351 System.out.format("Sigma Z = %11.5f [cm]%n", sigma_z);
0352 break;
0353 case 10:
0354 dxdz = new Float(tmp[1]);
0355
0356 break;
0357 case 11:
0358 dydz = new Float(tmp[1]);
0359
0360 break;
0361 case 12:
0362 width_x = new Float(tmp[1]);
0363 System.out.format("Sigma X = %14.8f [cm]%n", width_x);
0364 break;
0365 case 13:
0366 width_y = new Float(tmp[1]);
0367 System.out.format("Sigma Y = %14.8f [cm]%n", width_y);
0368 break;
0369 case 14:
0370 err_x = new Float(Math.sqrt(Double.parseDouble(tmp[1])));
0371
0372 break;
0373 case 15:
0374 err_y = new Float(Math.sqrt(Double.parseDouble(tmp[2])));
0375
0376 break;
0377 case 16:
0378 err_z = new Float(Math.sqrt(Double.parseDouble(tmp[3])));
0379
0380 break;
0381 case 17:
0382 err_sigma_z = new Float(Math.sqrt(Double.parseDouble(tmp[4])));
0383
0384 break;
0385 case 18:
0386 err_dxdz = new Float(Math.sqrt(Double.parseDouble(tmp[5])));
0387
0388 break;
0389 case 19:
0390 err_dydz = new Float(Math.sqrt(Double.parseDouble(tmp[6])));
0391
0392 break;
0393 case 20:
0394 err_width_x = new Float(Math.sqrt(Double.parseDouble(tmp[7])));
0395 err_width_y = err_width_x;
0396 break;
0397 case 21:
0398 System.out.println("EmittanceX");
0399 break;
0400 case 22:
0401 System.out.println("EmittanceY");
0402 break;
0403 case 23:
0404 System.out.println("BetaStar");
0405 break;
0406 case 24:
0407 events = new Integer(tmp[1]);
0408
0409 break;
0410 case 25:
0411 meanPV = new Float(tmp[1]);
0412
0413 break;
0414 case 26:
0415 err_meanPV = new Float(tmp[1]);
0416
0417 break;
0418 case 27:
0419 rmsPV = new Float(tmp[1]);
0420
0421 break;
0422 case 28:
0423 err_rmsPV = new Float(tmp[1]);
0424
0425 break;
0426 case 29:
0427 maxPV = new Integer(tmp[1]);
0428
0429 break;
0430 case 30:
0431 nPV = new Integer(tmp[1]);
0432
0433 rcdQlty = true;
0434 if (verbose) System.out.println("End of reading current record");
0435 break;
0436
0437 default:
0438 break;
0439 }
0440 }
0441 file_.close();
0442 }
0443 catch (IOException e) {
0444 System.err.println("IOException [readRcd]: " + getDateTime());
0445 e.printStackTrace();
0446 }
0447 return rcdQlty;
0448 }
0449
0450 private void CMS2LHCRF_POS(float x, float y, float z)
0451 {
0452 if (x != 0) {
0453 double tmpx = x;
0454 Centroid[0] = new Float(tmpx);
0455 Centroid[0] *= -1.0*cm2um;
0456 }
0457 else
0458 Centroid[0] = x;
0459 if (y != 0) {
0460 double tmpy = y;
0461 Centroid[1] = new Float(tmpy);
0462 Centroid[1] *= cm2um;
0463 }
0464 else
0465 Centroid[1] = y;
0466 if (z != 0) {
0467 double tmpz = z;
0468 Centroid[2] = new Float(tmpz);
0469 Centroid[2] *= -1.0*cm2mm;
0470 }
0471 else
0472 Centroid[2] = z;
0473 }
0474
0475 private void trueRcd(boolean verbose_)
0476 {
0477 try
0478 {
0479
0480 CMS2LHCRF_POS(x,y,z);
0481
0482 Tilt[0] = dxdz*rad2urad;
0483 Tilt[1] = (dydz != 0 ? (dydz*-1*rad2urad) : 0);
0484
0485 Size[0] = width_x*cm2um;
0486 Size[1] = width_y*cm2um;
0487 Size[2] = sigma_z*cm2mm;
0488
0489 if (verbose_) {
0490 System.out.format( "X0 in LHC RF = %11.5f [microns]%n", Centroid[0]);
0491 System.out.format( "Y0 in LHC RF = %11.5f [microns]%n", Centroid[1]);
0492 System.out.format( "Z0 in LHC RF = %13.7f [mm]%n", Centroid[2]);
0493 }
0494
0495 messageCMS.insert("runnum",runnum);
0496 messageCMS.insert("startTime",startTime);
0497 messageCMS.insert("endTime",endTime);
0498 messageCMS.insert("startTimeStamp",startTimeStamp);
0499 messageCMS.insert("endTimeStamp",endTimeStamp);
0500 messageCMS.insert("lumiRange",lumiRange);
0501 messageCMS.insert("quality",quality);
0502 messageCMS.insert("type",type);
0503 messageCMS.insert("x",x);
0504 messageCMS.insert("y",y);
0505 messageCMS.insert("z",z);
0506 messageCMS.insert("dxdz",dxdz);
0507 messageCMS.insert("dydz",dydz);
0508 messageCMS.insert("width_x",width_x);
0509 messageCMS.insert("width_y",width_y);
0510 messageCMS.insert("sigma_z",sigma_z);
0511 if (publishStatErrors) {
0512 messageCMS.insert("err_x",err_x);
0513 messageCMS.insert("err_y",err_y);
0514 messageCMS.insert("err_z",err_z);
0515 messageCMS.insert("err_dxdz",err_dxdz);
0516 messageCMS.insert("err_dydz",err_dydz);
0517 messageCMS.insert("err_width_x",err_width_x);
0518 messageCMS.insert("err_width_y",err_width_y);
0519 messageCMS.insert("err_sigma_z",err_sigma_z);
0520 }
0521 messageLHC.insert("Size",Size);
0522 messageLHC.insert("Centroid",Centroid);
0523 messageLHC.insert("Tilt",Tilt);
0524
0525 messagePV.insert("runnum",runnum);
0526 messagePV.insert("startTime",startTime);
0527 messagePV.insert("endTime",endTime);
0528 messagePV.insert("startTimeStamp",startTimeStamp);
0529 messagePV.insert("endTimeStamp",endTimeStamp);
0530 messagePV.insert("lumiRange",lumiRange);
0531 messagePV.insert("events",events);
0532 messagePV.insert("meanPV",meanPV);
0533 messagePV.insert("err_meanPV",err_meanPV);
0534 messagePV.insert("rmsPV",rmsPV);
0535 messagePV.insert("err_rmsPV",err_rmsPV);
0536 messagePV.insert("maxPV",maxPV);
0537 messagePV.insert("nPV",nPV);
0538 } catch (DipException e){
0539 System.err.println("DipException [trueRcd]: " + getDateTime());
0540 System.err.println("Failed to send data because " + e.getMessage());
0541 e.printStackTrace();
0542 }
0543 }
0544
0545 private void fakeRcd()
0546 {
0547 try
0548 {
0549 Centroid[0] = 0;
0550 Centroid[1] = 0;
0551 Centroid[2] = 0;
0552
0553 Size[0] = 0;
0554 Size[1] = 0;
0555 Size[2] = 0;
0556
0557 Tilt[0] = 0;
0558 Tilt[1] = 0;
0559
0560 messageLHC.insert("Size",Size);
0561 messageLHC.insert("Centroid",Centroid);
0562 messageLHC.insert("Tilt",Tilt);
0563 } catch (DipException e){
0564 System.err.println("DipException [fakeRcd]: " + getDateTime());
0565 System.err.println("Failed to send data because " + e.getMessage());
0566 e.printStackTrace();
0567 }
0568 }
0569
0570 private void publishRcd(String qlty_, String err_, boolean pubCMS_, boolean fitTime_)
0571 {
0572 try
0573 {
0574 boolean updateCMS_ = pubCMS_ && (currentLS%intLS == 0);
0575 if (alive.get(7)) {
0576 if (updateCMS_) System.out.println("Publish record to CCC and CMS (beam spot scaler...)");
0577 else
0578 if (!alive.get(1) && !alive.get(2)) System.out.println("Publish record to CCC only");
0579 }
0580 DipTimestamp zeit;
0581 if (fitTime_) {
0582 long epoch;
0583 epoch = endTimeStamp*1000;
0584 System.out.println("epoch = " + epoch + " [ms]");
0585 zeit = new DipTimestamp(epoch);
0586 }
0587 else zeit = new DipTimestamp();
0588
0589 if(updateCMS_) publicationCMS.send(messageCMS, zeit);
0590 publicationLHC.send(messageLHC, zeit);
0591 publicationPV.send(messagePV, zeit);
0592
0593 if (qlty_ == qualities[0]) {
0594 if (updateCMS_) publicationCMS.setQualityUncertain(err_);
0595 publicationLHC.setQualityUncertain(err_);
0596 }
0597 else if (qlty_ == qualities[1]) {
0598 if (updateCMS_) publicationCMS.setQualityBad(err_);
0599 publicationLHC.setQualityBad(err_);
0600 }
0601 else if (qlty_ == "UNINITIALIZED") {
0602 if (updateCMS_) publicationCMS.setQualityBad("UNINITIALIZED");
0603 publicationLHC.setQualityBad("UNINITIALIZED");
0604 }
0605 } catch (DipException e){
0606 System.err.println("DipException [publishRcd]: " + getDateTime());
0607 System.err.println("Failed to send data because " + e.getMessage());
0608 e.printStackTrace();
0609 }
0610 }
0611
0612 private String getDateTime()
0613 {
0614 DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
0615 Date date = new Date();
0616 return dateFormat.format(date);
0617 }
0618
0619 private String getDateTime(long epoch)
0620 {
0621 DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
0622 Date date = new Date(epoch);
0623 return dateFormat.format(date);
0624 }
0625
0626 private BeamSpotDipServer(String args[])
0627 {
0628 this.verbose = args[0].matches("true");
0629 this.overwriteQuality = args[1].matches("true");
0630 this.subjectCMS = args[2];
0631 this.subjectLHC = args[3];
0632 this.sourceFile = args[4];
0633 this.timeoutLS[0] = new Integer(args[5]);
0634 this.timeoutLS[1] = new Integer(args[6]);
0635 this.sourceFile1 = args[7];
0636 this.subjectPV = args[8];
0637 }
0638
0639 public static void main(String args[])
0640 {
0641 BeamSpotDipServer server = new BeamSpotDipServer(args);
0642 server.start();
0643 }
0644 }