Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:20:30

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