Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:21:01

0001 #include "InttMon.h"
0002 
0003 #include <onlmon/OnlMonServer.h>
0004 
0005 #include <Event/Event.h>
0006 #include <Event/packet.h>
0007 
0008 #include <TH1.h>
0009 #include <TH2.h>
0010 
0011 #include <iostream>
0012 #include <limits>
0013 
0014 InttMon::InttMon(const std::string &name)
0015   : OnlMon(name)
0016 {
0017   //  plist = new Packet *[1];
0018   return;
0019 }
0020 
0021 InttMon::~InttMon()
0022 {
0023   //  delete[] plist;
0024 }
0025 
0026 int InttMon::Init()
0027 {
0028   OnlMonServer *se = OnlMonServer::instance();
0029 
0030   // histograms
0031   // GetBinContent(1): # of rcdaq events
0032   // GetBinContent(2): # of unique BCOs
0033   // GetBinContent(3): BCO order error
0034   // GetBinContent(4): Time at SOR as seconds since epoch
0035   // GetBinContent(4): Time at present or EOR as seconds since epoch
0036   EvtHist = new TH1I("InttEvtHist", "InttEvtHist", 5, 0.0, 1.0);
0037   HitHist = new TH1D("InttHitHist", "InttHitHist", (NFEES * NCHIPS), 0.0, 1.0); // 26*14
0038   BcoHist = new TH2D("InttBcoHist", "InttBcoHist", 2, 0.0, 1.0, (NFEES * NBCOS), 0.0, 1.0); // 128*14
0039 
0040   // Decoded BCOs as function of real time, implemented as ring buffer
0041   LogHist = new TH1I("InttLogHist", "InttLogHist", m_LOG_DURATION / m_LOG_INTERVAL, 0.0, m_LOG_DURATION);
0042 
0043   se->registerHisto(this, EvtHist);
0044   se->registerHisto(this, HitHist);
0045   se->registerHisto(this, BcoHist);
0046   se->registerHisto(this, LogHist);
0047   //...
0048 
0049   return 0;
0050 }
0051 
0052 int InttMon::BeginRun(const int /* run_num */)
0053 {
0054   EvtHist->Reset();
0055   HitHist->Reset();
0056   BcoHist->Reset();
0057   LogHist->Reset();
0058 
0059   m_unique_bcos.clear();
0060   m_unique_bco_count = 0;
0061   m_most_recent_bco = std::numeric_limits<unsigned long long>::max();
0062   m_last_flushed_bco = std::numeric_limits<unsigned long long>::max();
0063 
0064   m_log_bin = 0;
0065   m_logged_bcos = 0;
0066 
0067   m_start_time = std::chrono::system_clock::now();
0068 
0069   // I'm being pedantic with this block (instead of using auto)
0070   // since I don't use chrono that much
0071   std::chrono::time_point<std::chrono::system_clock> const now = std::chrono::system_clock::now();
0072   std::chrono::duration<double> duration = now.time_since_epoch();
0073   double seconds = duration.count();
0074   EvtHist->SetBinContent(4, (int)seconds); // Time at SOR as seconds since epoch
0075 
0076   return 0;
0077 }
0078 
0079 int InttMon::process_event(Event *evt)
0080 {
0081   for (int pid = 3001; pid < 3009; ++pid)
0082   {
0083     Packet *pkt = evt->getPacket(pid);
0084     if (!pkt)
0085     {
0086       continue;
0087     }
0088 
0089     // hits
0090     for (int n = 0; n < pkt->iValue(0, "NR_HITS"); ++n)
0091     {
0092       int fee = pkt->iValue(n, "FEE");
0093       int chp = (pkt->iValue(n, "CHIP_ID") + 25) % 26;
0094       int fphx_bco = pkt->iValue(n, "FPHX_BCO");
0095       int bco_diff = ((0x7f & pkt->lValue(n, "BCO")) - fphx_bco + 128) % 128;
0096       HitHist->AddBinContent(fee * NCHIPS + chp + 1);  // +1 to start at bin 1
0097       BcoHist->AddBinContent(BcoHist->GetBin(1, fee * NBCOS + bco_diff + 1));   // +1 to start at bin 1
0098       BcoHist->AddBinContent(BcoHist->GetBin(2, fee * NBCOS + fphx_bco + 1));   // +1 to start at bin 1
0099     }
0100 
0101     // bcos
0102     for (int n = 0; n < pkt->iValue(0, "NR_BCOS"); ++n)
0103     {
0104       unsigned long long bco_full = pkt->lValue(n, "BCOLIST");
0105 
0106       // more recent that the variable we track it with, or variable we track it with hasn't been set to a "real" value yet
0107       if(m_bco_less(m_most_recent_bco, bco_full) || (m_most_recent_bco == std::numeric_limits<unsigned long long>::max()))
0108       {
0109           m_most_recent_bco = bco_full;
0110       }
0111 
0112       // make sure it's not in the range of bcos we've "flushed" into our counter (and that the variable we track it with has been set to a "real" value)
0113       // This check should be moot--the fees have their BCOs in order--but I leave it in anyways b/c it is not expensive and confirms this assumption
0114       if(!m_bco_less(m_last_flushed_bco, bco_full) && (m_last_flushed_bco != std::numeric_limits<unsigned long long>::max()))
0115       {
0116           EvtHist->SetBinContent(3, 1);
0117           continue;
0118       }
0119 
0120       m_unique_bcos.insert(bco_full);
0121     }
0122 
0123     delete pkt;
0124   }
0125 
0126   // Go through our list of unique BCOs and "flush" them into a counter
0127   if(100 < m_unique_bcos.size())
0128   {
0129     std::set<unsigned long long, bco_comparator_s>::const_iterator bco_itr = m_unique_bcos.begin();
0130     // for(bco_itr = m_unique_bcos.begin(); bco_itr != m_unique_bcos.end(); ++bco_itr)
0131     for(int n = 0; n < 10; ++n)
0132     {
0133       if(bco_itr == m_unique_bcos.end())
0134       {
0135         break;
0136       }
0137 
0138       // if((m_most_recent_bco == std::numeric_limits<unsigned long long>::max()) || m_bco_less(m_most_recent_bco - m_MAX_BCO_DIFF, *bco_itr))
0139       if( m_most_recent_bco == std::numeric_limits<unsigned long long>::max() )
0140       {
0141         break;
0142       }
0143 
0144       ++m_unique_bco_count;
0145       m_last_flushed_bco = *bco_itr;
0146       ++bco_itr;
0147     }
0148     m_unique_bcos.erase(m_unique_bcos.begin(), bco_itr);
0149   }
0150 
0151   EvtHist->AddBinContent(1);
0152   EvtHist->SetBinContent(2, m_unique_bco_count + m_unique_bcos.size());
0153 
0154 
0155   std::chrono::time_point<std::chrono::system_clock> const now = std::chrono::system_clock::now();
0156   std::chrono::duration<double> duration = now.time_since_epoch();
0157   double seconds = duration.count();
0158   EvtHist->SetBinContent(5, (int)seconds); // Time at present or EOR as seconds since epoch
0159 
0160   duration = now - m_start_time; // Time since we updated the decoding rate histogram--we update this every m_LOG_INTERVAL even if we don't decode BCOs
0161   seconds = duration.count();
0162 
0163   int N = LogHist->GetNbinsX();
0164   int logged_seconds = m_LOG_INTERVAL * (N * LogHist->GetBinContent(N + 1) + m_log_bin);
0165 
0166   // See if we should increment m_log_bin
0167   while(logged_seconds < seconds)
0168   {
0169     m_logged_bcos = m_unique_bco_count + m_unique_bcos.size();
0170 
0171     if(m_log_bin == N - 1)
0172     {
0173       LogHist->AddBinContent(N + 1); // number of times content was wrapped
0174     }
0175     m_log_bin = (m_log_bin + 1) % N;
0176 
0177     LogHist->SetBinContent(m_log_bin, 0);
0178     LogHist->SetBinContent(N, m_log_bin);
0179 
0180     logged_seconds += m_LOG_INTERVAL;
0181   }
0182 
0183   LogHist->SetBinContent(m_log_bin, m_unique_bco_count + m_unique_bcos.size() - m_logged_bcos);
0184 
0185   // if(m_unique_bco_count + m_unique_bcos.size() - m_logged_bcos)
0186   // {
0187   //   std::cout << "decoded " << m_unique_bco_count + m_unique_bcos.size() - m_logged_bcos << std::endl;
0188   // }
0189 
0190   // if(!((int)(EvtHist->GetBinContent(1)) % m_evt_per_cout))
0191   // {
0192   //   std::cout << std::hex;
0193   //   std::cout << "last flushed: 0x" << m_last_flushed_bco << std::endl;
0194   //   std::cout << "most recent:  0x" << m_most_recent_bco << std::endl;
0195   //   std::cout << std::dec;
0196   // }
0197 
0198 
0199   return 0;
0200 }
0201 
0202 int InttMon::Reset()
0203 {
0204     return 0;
0205 }
0206 
0207 int InttMon::MiscDebug()
0208 {
0209   // for (int fee = 0; fee < 14; ++fee)
0210   // {
0211   //   for (int chp = 0; chp < 26; ++chp)
0212   //   {
0213   //     HitHist->SetBinContent(fee * NCHIPS + chp + 1, chp);
0214   //   }
0215   // }
0216 
0217   // for (int fee = 0; fee < 14; ++fee)
0218   // {
0219   //   for (int bco = 0; bco < 128; ++bco)
0220   //   {
0221   //     BcoHist->SetBinContent(fee * NBCOS + bco + 1, fee);
0222   //   }
0223   // }
0224 
0225   return 0;
0226 }
0227 
0228 bool InttMon::bco_comparator_s::operator()(unsigned long long const& lhs, unsigned long long const& rhs) const
0229 {
0230   return (rhs - lhs + 2 * MAX) % MAX < (lhs - rhs + 2 *MAX) % MAX;
0231 }
0232 
0233 //             Ladder Structure                //
0234 //=============================================//
0235 //      U14     U1   Ladder_z  Type B  North   //
0236 //      U15     U2      .        .       .     //
0237 //      U16     U3      3        .       .     //
0238 //      U17     U4      .        .       .     //
0239 //      U18     U5   Ladder_z  Type B    .     //
0240 //------------------------------------   .     //
0241 //      U19     U6   Ladder_z  Type A    .     //
0242 //      U20     U7      .        .       .     //
0243 //      U21     U8      .        .       .     //
0244 //      U22     U9      2        .       .     //
0245 //      U23     U10     .        .       .     //
0246 //      U24     U11     .        .       .     //
0247 //      U25     U12     .        .       .     //
0248 //      U26     U13  Ladder_z  Type A  North   //
0249 //---------------------------------------------//
0250 //      U13     U26  Ladder_z  Type A  South   //
0251 //      U12     U25     .        .       .     //
0252 //      U11     U24     .        .       .     //
0253 //      U10     U23     .        .       .     //
0254 //      U9      U22     0        .       .     //
0255 //      U8      U21     .        .       .     //
0256 //      U7      U20     .        .       .     //
0257 //      U6      U19  Ladder_z  Type A    .     //
0258 //------------------------------------   .     //
0259 //      U5      U18  Ladder_z  Type B    .     //
0260 //      U4      U17     .        .       .     //
0261 //      U3      U16     1        .       .     //
0262 //      U2      U15     .        .       .     //
0263 //      U1      U14  Ladder_z  Type B  South   //
0264 //=============================================//