Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:57

0001 
0002 #include "GL1Manager.h"
0003 
0004 #include <Event/eventReceiverClient.h>
0005 #include <Event/Event.h>
0006 #include <Event/packet.h>
0007 
0008 #include <algorithm>
0009 
0010 #define coutfl if (verbosity) std::cout << __FILE__<< "  " << __LINE__ << " "
0011 //#define coutfl std::cout << __FILE__<< "  " << __LINE__ << " "
0012 
0013 using namespace std;
0014 
0015 GL1Manager::GL1Manager (const char * hostname, const int h, const int d)
0016 {
0017   _hostname = hostname;
0018   _broken = 0;
0019   pgl1 = 0;
0020 
0021   history_length = h;
0022   max_discrepancy = d;
0023 
0024   Reset();
0025 
0026   erc = new eventReceiverClient(hostname);
0027 }
0028 
0029 GL1Manager::~GL1Manager()
0030 {
0031   if (pgl1) delete pgl1;
0032   if ( erc) delete erc;
0033 }
0034 
0035 
0036 void GL1Manager::Reset()
0037 {
0038   desireddiff = 0;
0039   previousdiff = 0;
0040   clockdiff = 0;
0041   event_delta = 0;
0042   eventMatch = 0;
0043   packetRequested = 0;
0044   gl1_clock = 0;
0045   init_done = 0;
0046   verbosity = 0;
0047 
0048   previous_GL1clocks.clear();
0049   previous_packetclocks.clear();
0050   
0051   diff_gl1clocks.clear();
0052   diff_packetclocks.clear();
0053 
0054   if (pgl1) delete pgl1;
0055   pgl1 = 0;
0056 
0057   return;
0058 }
0059 
0060 
0061 Packet *GL1Manager::getGL1Packet()
0062 {
0063   if ( ! pgl1) return 0;
0064   packetRequested = 1;
0065   
0066   return pgl1;
0067 
0068 }
0069 
0070 
0071 int GL1Manager::getClockSync(const int evtnr, Packet * p)
0072 {
0073   return ClockSync ( evtnr, p);
0074 }
0075 
0076 
0077 Packet * GL1Manager::fetchGL1Packet( const int evtnr)
0078 {
0079   if (pgl1 && packetRequested == 0)  delete pgl1;
0080   pgl1 = 0;
0081   packetRequested = 0;
0082 
0083   coutfl << " asking for event " << evtnr + event_delta  << endl;
0084   Event *gl1Event = erc->getEvent(evtnr + event_delta);
0085 
0086   if ( !gl1Event) 
0087     {
0088       return 0;
0089     }
0090 
0091   pgl1 = gl1Event->getPacket(14001);
0092   if ( ! pgl1)
0093     {
0094       delete gl1Event;
0095       return 0;
0096     }
0097 
0098   pgl1->convert();
0099 
0100   delete gl1Event;
0101 
0102   return pgl1;
0103 }
0104 
0105 
0106 
0107 int GL1Manager::ClockSync(const int evtnr, Packet * p)
0108 {
0109 
0110   pgl1 = fetchGL1Packet(evtnr);
0111   if ( !pgl1 ) return -1;
0112   
0113   // if we are getting to the next event. delete what's left of the previous one, if any
0114   gl1_clock = pgl1->lValue(0, "BCO"); 
0115   long long packet_clock = p->lValue(0, "CLOCK");
0116 
0117   // keep the 4 vectors trimmed at the envisioned size
0118   while ( previous_GL1clocks.size() >= history_length)
0119     {
0120       previous_GL1clocks.pop_front();
0121     }
0122   while ( previous_packetclocks.size() >= history_length)
0123     {
0124       previous_packetclocks.pop_front();
0125     }
0126 
0127 
0128   // we will add to the vector in a moment. Remove the oldest element 
0129   while ( diff_gl1clocks.size() >= history_length)
0130     { 
0131       diff_gl1clocks.pop_front();
0132     }
0133   
0134   while ( diff_packetclocks.size() >= history_length)
0135     {
0136       diff_packetclocks.pop_front();
0137     }
0138 
0139 
0140 
0141 
0142   if (previous_GL1clocks.size())  // this starts at the 2nd event
0143     {
0144       uint32_t d = gl1_clock - previous_GL1clocks.back();
0145       diff_gl1clocks.push_back(d);
0146     }
0147   
0148   if (previous_packetclocks.size())
0149     {
0150       uint32_t d = packet_clock - previous_packetclocks.back();
0151       diff_packetclocks.push_back(d);
0152     }
0153 
0154 
0155 
0156   previous_GL1clocks.push_back(gl1_clock);
0157   previous_packetclocks.push_back(packet_clock);
0158 
0159 
0160   // if we reach the desired size, we declare ourselves done with the init 
0161   if  (init_done == 0 && diff_gl1clocks.size() >= history_length)
0162     {
0163       init_done = 1;
0164       event_delta = findDelta();
0165       
0166       auto xx = previous_GL1clocks.begin() + event_delta;
0167       auto yy = previous_packetclocks.begin();
0168 
0169       desireddiff = ( *xx - *yy ) & 0xffffffff;
0170       return GL1Manager_INITIALIZING;
0171     }
0172   if (init_done ==0 ) return GL1Manager_INITIALIZING;
0173 
0174 
0175   previousdiff = clockdiff;
0176   clockdiff = (gl1_clock - packet_clock) & 0xffffffff;
0177   
0178   if ( desireddiff == clockdiff)
0179     {
0180 
0181       eventMatch = 1;
0182       return GL1Manager_MATCH;
0183     }
0184   else
0185     {
0186       // did we perhaps skip an rcdaq event?
0187       
0188       Reset();
0189       //event_delta--;
0190       eventMatch = 0;
0191       return GL1Manager_MISMATCH;
0192     }
0193 }
0194 
0195 uint32_t GL1Manager::getPacketClockDifference (Packet * p) const
0196 {
0197 
0198   long long packet_clock = p->lValue(0, "CLOCK");
0199 
0200   uint32_t cd = (gl1_clock - packet_clock) & 0xffffffff;
0201   return cd;
0202 }
0203 
0204 
0205 int GL1Manager::checkPacket (Packet * p) const
0206 {
0207   uint32_t cd = getPacketClockDifference ( p);
0208   if ( cd == desireddiff) return 0;
0209   return 1;
0210 }
0211 
0212 int GL1Manager::findDelta()
0213 {
0214 
0215   if ( diff_gl1clocks.size() < history_length) return 2;
0216 
0217 
0218   // check "packet" against "GL1" first
0219 
0220 
0221   auto it = std::search(diff_gl1clocks.begin(), diff_gl1clocks.end(), diff_packetclocks.begin(), diff_packetclocks.begin() + diff_packetclocks.size() - max_discrepancy );
0222 
0223   int ret = 0;
0224 
0225   if ( it != diff_gl1clocks.end())
0226     {
0227 
0228       ret = std::distance(diff_gl1clocks.begin(), it);
0229       return ret;
0230 
0231     }
0232   // else
0233   //   {
0234   //     cout << " no match of packets in GL1 " << endl;
0235   //   }
0236 
0237   auto it2 = std::search(diff_packetclocks.begin(), diff_packetclocks.end(), diff_gl1clocks.begin(), diff_gl1clocks.begin() + diff_gl1clocks.size() - max_discrepancy  );
0238 
0239   if ( it2 != diff_packetclocks.end())
0240     {
0241       ret = -1 * std::distance(diff_packetclocks.begin(), it2);
0242     }
0243   // else
0244   //   {
0245   //     cout << " no match of packets in packet " << endl;
0246   //   }
0247 
0248   return ret;
0249 }
0250 
0251 
0252 
0253  
0254 
0255 
0256 
0257 //   uint32_t clockdiff = getPacketClockDifference ( p);
0258 //   if ( clockdiff == desireddiff) return 0;
0259 //   return 1;
0260 // }
0261 
0262 
0263 
0264   
0265 
0266 
0267 
0268 
0269