File indexing completed on 2026-04-03 08:16:42
0001
0002
0003
0004
0005
0006 #include "SpinMon.h"
0007
0008 #include <onlmon/OnlMon.h> // for OnlMon
0009 #include <onlmon/OnlMonDB.h>
0010 #include <onlmon/OnlMonServer.h>
0011
0012 #include <Event/msg_profile.h>
0013
0014 #include <Event/Event.h>
0015 #include <Event/EventTypes.h>
0016 #include <Event/msg_profile.h>
0017
0018 #include <TH1.h>
0019 #include <TH2.h>
0020 #include <TRandom.h>
0021
0022 #include <cmath>
0023 #include <cstdio> // for printf
0024 #include <fstream>
0025 #include <iostream>
0026 #include <sstream>
0027 #include <string> // for allocator, string, char_traits
0028
0029 SpinMon::SpinMon(const std::string &name)
0030 : OnlMon(name)
0031 {
0032
0033
0034 return;
0035 }
0036
0037 SpinMon::~SpinMon()
0038 {
0039
0040 return;
0041 }
0042
0043 int SpinMon::Init()
0044 {
0045 hpCspinpatternBlue = new TH1I("h1_pCspinpatternBlue", "", 120, -0.5, 119.5);
0046 hpCspinpatternYellow = new TH1I("h1_pCspinpatternYellow", "", 120, -0.5, 119.5);
0047
0048 hspinpatternBlue = new TH1I("h1_spinpatternBlue", "", 120, -0.5, 119.5);
0049 hspinpatternYellow = new TH1I("h1_spinpatternYellow", "", 120, -0.5, 119.5);
0050
0051 pCspin_patternBlueUp = new TH2I("h2_pCspinpatternBlueUp", "", 120, -0.5, 119.5, 2, 0.5, 2.5);
0052 pCspin_patternBlueDown = new TH2I("h2_pCspinpatternBlueDown", "", 120, -0.5, 119.5, 2, 0.5, 2.5);
0053 pCspin_patternBlueUnpol = new TH2I("h2_pCspinpatternBlueUnpol", "", 120, -0.5, 119.5, 2, 0.5, 2.5);
0054
0055 pCspin_patternYellowUp = new TH2I("h2_pCspinpatternYellowUp", "", 120, -0.5, 119.5, 2, 0.5, 2.5);
0056 pCspin_patternYellowDown = new TH2I("h2_pCspinpatternYellowDown", "", 120, -0.5, 119.5, 2, 0.5, 2.5);
0057 pCspin_patternYellowUnpol = new TH2I("h2_pCspinpatternYellowUnpol", "", 120, -0.5, 119.5, 2, 0.5, 2.5);
0058
0059 spin_patternBlueUp = new TH2I("h2_spinpatternBlueUp", "", 120, -0.5, 119.5, 2, 0.5, 2.5);
0060 spin_patternBlueDown = new TH2I("h2_spinpatternBlueDown", "", 120, -0.5, 119.5, 2, 0.5, 2.5);
0061 spin_patternBlueUnpol = new TH2I("h2_spinpatternBlueUnpol", "", 120, -0.5, 119.5, 2, 0.5, 2.5);
0062
0063 spin_patternYellowUp = new TH2I("h2_spinpatternYellowUp", "", 120, -0.5, 119.5, 2, 0.5, 2.5);
0064 spin_patternYellowDown = new TH2I("h2_spinpatternYellowDown", "", 120, -0.5, 119.5, 2, 0.5, 2.5);
0065 spin_patternYellowUnpol = new TH2I("h2_spinpatternYellowUnpol", "", 120, -0.5, 119.5, 2, 0.5, 2.5);
0066
0067 hpolBlue = new TH1D("h1_polBlue", "", 1, 0, 1);
0068 hpolYellow = new TH1D("h1_polYellow", "", 1, 0, 1);
0069
0070 hxingshift = new TH1I("h1_xingshift", "", 2, 0, 2);
0071
0072 hfillnumber = new TH1I("h1_fillnumber", "", 2, 0, 2);
0073 hfilltypeBlue = new TH1I("h1_filltypeBlue", "", 1, 0, 1);
0074 hfilltypeYellow = new TH1I("h1_filltypeYellow", "", 1, 0, 1);
0075
0076 hCorrect = new TH1F("hCorrect","Bunch Numbers", 130, -0.5, 129.5);
0077 hAbortgap = new TH1F("hAbortgap","Bunch Numbers", 130, -0.5, 129.5);
0078 hForbidden = new TH1F("hForbidden","Bunch Numbers", 130, -0.5, 129.5);
0079
0080 for (int i = 0; i < NTRIG; i++)
0081 {
0082 gl1_counter[i] = new TH1I(Form("gl1_counter_trig%d", i), Form("gl1p_trig%d", i), 120, -0.5, 119.5);
0083 }
0084
0085 for (auto &scalercount : scalercounts)
0086 {
0087 for (unsigned long &j : scalercount)
0088 {
0089 j = 0;
0090 }
0091 }
0092
0093 OnlMonServer *se = OnlMonServer::instance();
0094
0095 se->registerHisto(this, hpCspinpatternBlue);
0096 se->registerHisto(this, hpCspinpatternYellow);
0097 se->registerHisto(this, pCspin_patternBlueUp);
0098 se->registerHisto(this, pCspin_patternBlueDown);
0099 se->registerHisto(this, pCspin_patternBlueUnpol);
0100 se->registerHisto(this, pCspin_patternYellowUp);
0101 se->registerHisto(this, pCspin_patternYellowDown);
0102 se->registerHisto(this, pCspin_patternYellowUnpol);
0103
0104 se->registerHisto(this, hspinpatternBlue);
0105 se->registerHisto(this, hspinpatternYellow);
0106 se->registerHisto(this, spin_patternBlueUp);
0107 se->registerHisto(this, spin_patternBlueDown);
0108 se->registerHisto(this, spin_patternBlueUnpol);
0109 se->registerHisto(this, spin_patternYellowUp);
0110 se->registerHisto(this, spin_patternYellowDown);
0111 se->registerHisto(this, spin_patternYellowUnpol);
0112
0113 se->registerHisto(this, hpolBlue);
0114 se->registerHisto(this, hpolYellow);
0115 se->registerHisto(this, hxingshift);
0116 se->registerHisto(this, hfillnumber);
0117 se->registerHisto(this, hfilltypeBlue);
0118 se->registerHisto(this, hfilltypeYellow);
0119
0120 se->registerHisto(this, hCorrect);
0121 se->registerHisto(this, hAbortgap);
0122 se->registerHisto(this, hForbidden);
0123
0124 for (auto &i : gl1_counter)
0125 {
0126 se->registerHisto(this, i);
0127 }
0128
0129 Reset();
0130
0131 return 0;
0132 }
0133
0134 int SpinMon::BeginRun(const int )
0135 {
0136
0137
0138 return 0;
0139 }
0140
0141 int SpinMon::process_event(Event *e )
0142 {
0143 evtcnt++;
0144
0145 if (e->getEvtType() == 9)
0146 {
0147
0148 pBlueSpin = e->getPacket(packet_BLUESPIN);
0149 pYellSpin = e->getPacket(packet_YELLSPIN);
0150
0151 pBluePol = e->getPacket(packet_BLUEPOL);
0152 pYellPol = e->getPacket(packet_YELLPOL);
0153
0154 pBlueAsym = e->getPacket(packet_BLUEASYM);
0155 pYellAsym = e->getPacket(packet_YELLASYM);
0156
0157 pBlueIntPattern = e->getPacket(packet_BLUEINTPATTERN);
0158 pYellIntPattern = e->getPacket(packet_YELLINTPATTERN);
0159 pBluePolPattern = e->getPacket(packet_BLUEPOLPATTERN);
0160 pYellPolPattern = e->getPacket(packet_YELLPOLPATTERN);
0161
0162 pBlueFillNumber = e->getPacket(packet_BLUEFILLNUMBER);
0163 pYellFillNumber = e->getPacket(packet_YELLFILLNUMBER);
0164
0165
0166
0167 float polBlue = -999;
0168 float polBlueErr = -999;
0169 if (pBluePol)
0170 {
0171 polBlue = pBluePol->iValue(0) / 10000.0;
0172 polBlueErr = pBluePol->iValue(1) / 10000.0;
0173 delete pBluePol;
0174 }
0175 hpolBlue->SetBinContent(1, polBlue);
0176 hpolBlue->SetBinError(1, polBlueErr);
0177
0178 float polYellow = -999;
0179 float polYellowErr = -999;
0180 if (pYellPol)
0181 {
0182 polYellow = pYellPol->iValue(0) / 10000.0;
0183 polYellowErr = pYellPol->iValue(1) / 10000.0;
0184 delete pYellPol;
0185 }
0186 hpolYellow->SetBinContent(1, polYellow);
0187 hpolYellow->SetBinError(1, polYellowErr);
0188
0189
0190
0191 float fillnumberBlue = 0;
0192 float fillnumberYellow = 0;
0193 if (pBlueFillNumber)
0194 {
0195 fillnumberBlue = pBlueFillNumber->iValue(0);
0196 delete pBlueFillNumber;
0197 }
0198 if (pYellFillNumber)
0199 {
0200 fillnumberYellow = pYellFillNumber->iValue(0);
0201 delete pYellFillNumber;
0202 }
0203 hfillnumber->SetBinContent(1, fillnumberBlue);
0204 hfillnumber->SetBinContent(2, fillnumberYellow);
0205
0206
0207
0208 hxingshift->SetBinContent(1, defaultxingshift);
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288 if (pBlueIntPattern && pBluePolPattern)
0289 {
0290 int numbluefill = 0;
0291 for (int i = 0; i < 360; i += 3)
0292 {
0293 blueFillPattern[i / 3] = pBlueIntPattern->iValue(i);
0294 if (pBlueIntPattern->iValue(i))
0295 {
0296 blueSpinPattern[i / 3] = pBluePolPattern->iValue(i);
0297 numbluefill++;
0298 }
0299 else
0300 {
0301 blueSpinPattern[i / 3] = 10;
0302 }
0303
0304 hspinpatternBlue->SetBinContent((i / 3) + 1, blueSpinPattern[i / 3]);
0305 if (blueSpinPattern[i / 3] == 1)
0306 {
0307 spin_patternBlueUp->Fill(i / 3, 1);
0308 }
0309 if (blueSpinPattern[i / 3] == -1)
0310 {
0311 spin_patternBlueDown->Fill(i / 3, 1);
0312 }
0313 if (blueSpinPattern[i / 3] == 0)
0314 {
0315 spin_patternBlueUnpol->Fill(i / 3, 1);
0316 }
0317 }
0318
0319 hfilltypeBlue->SetBinContent(1, numbluefill);
0320
0321 delete pBlueIntPattern;
0322 delete pBluePolPattern;
0323 }
0324
0325 if (pYellIntPattern && pYellPolPattern)
0326 {
0327 int numyellfill = 0;
0328 for (int i = 0; i < 360; i += 3)
0329 {
0330 yellFillPattern[i / 3] = pYellIntPattern->iValue(i);
0331 if (pYellIntPattern->iValue(i))
0332 {
0333 yellSpinPattern[i / 3] = pYellPolPattern->iValue(i);
0334 numyellfill++;
0335 }
0336 else
0337 {
0338 yellSpinPattern[i / 3] = 10;
0339 }
0340
0341 hspinpatternYellow->SetBinContent((i / 3) + 1, yellSpinPattern[i / 3]);
0342
0343 if (yellSpinPattern[i / 3] == 1)
0344 {
0345 spin_patternYellowUp->Fill(i / 3, 2);
0346 }
0347 if (yellSpinPattern[i / 3] == -1)
0348 {
0349 spin_patternYellowDown->Fill(i / 3, 2);
0350 }
0351 if (yellSpinPattern[i / 3] == 0)
0352 {
0353 spin_patternYellowUnpol->Fill(i / 3, 2);
0354 }
0355 }
0356
0357 hfilltypeYellow->SetBinContent(1, numyellfill);
0358
0359 delete pYellIntPattern;
0360 delete pYellPolPattern;
0361 }
0362
0363
0364
0365
0366
0367
0368
0369 if (pBlueAsym)
0370 {
0371 for (int i = 0; i < 360; i += 3)
0372 {
0373 float blueAsyms = pBlueAsym->iValue(i) / 10000.0;
0374 float blueAsymsErr = pBlueAsym->iValue(i + 360) / 10000.0;
0375
0376 float bluebot = blueAsyms - blueAsymsErr;
0377 float bluetop = blueAsyms + blueAsymsErr;
0378
0379 if (blueAsyms != 0 || bluebot != 0 || bluetop != 0)
0380 {
0381 if (bluebot > 0 && bluetop > 0)
0382 {
0383 hpCspinpatternBlue->SetBinContent((i / 3) + 1, 1);
0384 pCspin_patternBlueUp->Fill(i / 3, 1);
0385 }
0386 else if (bluebot < 0 && bluetop < 0)
0387 {
0388 hpCspinpatternBlue->SetBinContent((i / 3) + 1, -1);
0389 pCspin_patternBlueDown->Fill(i / 3, 1);
0390 }
0391 else if (bluebot <= 0 && bluetop >= 0)
0392 {
0393 hpCspinpatternBlue->SetBinContent((i / 3) + 1, 0);
0394
0395 }
0396 }
0397 else
0398 {
0399 hpCspinpatternBlue->SetBinContent((i / 3) + 1, 10);
0400 }
0401 }
0402 delete pBlueAsym;
0403 }
0404
0405 if (pYellAsym)
0406 {
0407 for (int i = 0; i < 360; i += 3)
0408 {
0409 float yellAsyms = pYellAsym->iValue(i) / 10000.0;
0410 float yellAsymsErr = pYellAsym->iValue(i + 360) / 10000.0;
0411
0412 float yellbot = yellAsyms - yellAsymsErr;
0413 float yelltop = yellAsyms + yellAsymsErr;
0414
0415 if (yellAsyms != 0 || yellbot != 0 || yelltop != 0)
0416 {
0417 if (yellbot > 0 && yelltop > 0)
0418 {
0419 hpCspinpatternYellow->SetBinContent((i / 3) + 1, 1);
0420 pCspin_patternYellowUp->Fill(i / 3, 2);
0421 }
0422 else if (yellbot < 0 && yelltop < 0)
0423 {
0424 hpCspinpatternYellow->SetBinContent((i / 3) + 1, -1);
0425 pCspin_patternYellowDown->Fill(i / 3, 2);
0426 }
0427 else if (yellbot <= 0 && yelltop >= 0)
0428 {
0429 hpCspinpatternYellow->SetBinContent((i / 3) + 1, 0);
0430
0431 }
0432 }
0433 else
0434 {
0435 hpCspinpatternYellow->SetBinContent((i / 3) + 1, 10);
0436 }
0437 }
0438 delete pYellAsym;
0439 }
0440
0441 }
0442
0443 else if (e->getEvtType() == 1)
0444 {
0445
0446
0447 p_gl1 = e->getPacket(packetid_GL1);
0448
0449 if (p_gl1)
0450 {
0451
0452 int bunchnr = (p_gl1->lValue(0, "BunchNumber") + defaultxingshift) % NBUNCHES;
0453 if ( bunchnr <= 110)
0454 {
0455 hCorrect->Fill(bunchnr);
0456 }
0457 else if ( bunchnr >110 && bunchnr <120)
0458 {
0459 hAbortgap->Fill(bunchnr);
0460 }
0461 else
0462 {
0463 hForbidden->Fill(125);
0464 }
0465
0466 for (int i = 0; i < 16; i++)
0467 {
0468
0469 int counts = p_gl1->lValue(i, "GL1PLIVE");
0470
0471 gl1_counter[i]->SetBinContent(bunchnr + 1, counts);
0472 scalercounts[i][bunchnr] = counts;
0473 }
0474 delete p_gl1;
0475 }
0476
0477
0478
0479 if (evtcnt > 4999 && evtcnt % 5000 == 0)
0480 {
0481 CalculateCrossingShift(xingshift, scalercounts, success);
0482
0483 if (success)
0484 {
0485 addxingshift = xingshift;
0486 }
0487 }
0488 hxingshift->SetBinContent(2, addxingshift);
0489 }
0490
0491 return 0;
0492 }
0493
0494 int SpinMon::Reset()
0495 {
0496
0497 evtcnt = 0;
0498 return 0;
0499 }
0500
0501 int SpinMon::CalculateCrossingShift(int &xing, uint64_t counts[NTRIG][NBUNCHES], bool &succ)
0502 {
0503 succ = false;
0504 int shift_array[NTRIG] = {0};
0505
0506 int trig_inactive_array[NTRIG] = {0};
0507
0508 int last_active_index = 0;
0509
0510 int _temp;
0511 for (int itrig = 0; itrig < NTRIG; itrig++)
0512 {
0513 long long _counts = 0;
0514 for (int ii = 0; ii < NBUNCHES; ii++)
0515 {
0516 _counts += counts[itrig][ii];
0517 }
0518
0519 if (_counts < 10000)
0520 {
0521 trig_inactive_array[itrig] = 1;
0522 }
0523 else
0524 {
0525 last_active_index = itrig;
0526 }
0527
0528 long long abort_sum_prev = _counts;
0529
0530 _temp = 0;
0531 for (int ishift = 0; ishift < NBUNCHES; ishift++)
0532 {
0533 long long abort_sum = 0;
0534 for (int iunfillbunch = 0; iunfillbunch < NBUNCHES; iunfillbunch++)
0535 {
0536 if (blueFillPattern[iunfillbunch] && yellFillPattern[iunfillbunch])
0537 {
0538 continue;
0539 }
0540 int shiftbunch = iunfillbunch - ishift;
0541 if (shiftbunch < 0)
0542 {
0543 shiftbunch = 120 + shiftbunch;
0544 }
0545 abort_sum += counts[itrig][(shiftbunch) % NBUNCHES];
0546 }
0547 if (abort_sum < abort_sum_prev)
0548 {
0549 abort_sum_prev = abort_sum;
0550 _temp = ishift;
0551 }
0552 }
0553
0554 shift_array[itrig] = _temp;
0555 }
0556
0557 for (int itrig = 0; itrig < NTRIG; itrig++)
0558 {
0559
0560 if (!trig_inactive_array[itrig])
0561 {
0562 if (shift_array[itrig] == shift_array[last_active_index])
0563 {
0564 xing = shift_array[itrig];
0565 succ = true;
0566 }
0567 else
0568 {
0569 xing = 0;
0570 succ = false;
0571 return 0;
0572 }
0573 }
0574 }
0575
0576
0577 return 0;
0578 }