Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001  
0002 
0003 #include <iostream>
0004 #include <string.h>
0005 
0006 #include <daq_device_rtclock.h>
0007 
0008 using namespace std;
0009 
0010 daq_device_rtclock::daq_device_rtclock(const int eventtype
0011                      , const int subeventid
0012                      , const int n_what)
0013 {
0014 
0015   m_eventType  = eventtype;
0016   m_subeventid = subeventid;
0017 }
0018 
0019 daq_device_rtclock::~daq_device_rtclock()
0020 {
0021 }
0022 
0023 
0024 
0025 // the put_data function
0026 
0027 int daq_device_rtclock::put_data(const int etype, int * adr, const int length )
0028 {
0029 
0030   if (etype != m_eventType )  // not our id
0031     {
0032       return 0;
0033     }
0034 
0035 
0036   struct timespec clk;
0037   struct timespec clk2;
0038 
0039 
0040   if ( daq_getEventFormat() ) // we are writing PRDF
0041     {
0042 
0043       formatPacketHdr(adr);
0044       packetdata_ptr sevt =  (packetdata_ptr) adr;
0045       // update id's etc
0046       sevt->sub_id =  m_subeventid;
0047       sevt->sub_type =  4;
0048       sevt->sub_decoding = 30000+9;
0049       
0050       int  *d = (int *) &sevt->data;
0051       int s = clock_gettime(CLOCK_MONOTONIC_RAW, &clk);
0052       int sm = clock_gettime(CLOCK_REALTIME, &clk2);
0053       
0054       if  (s)  // error, set everything to 0
0055     {
0056       for ( int i = 0; i < 9; i++)  *d++ = 0;
0057     }
0058       else
0059     {
0060       *d++ = clk.tv_sec;
0061       memcpy(d, &clk.tv_nsec, sizeof(clk.tv_nsec));
0062       d+=2;
0063       *d++ = previous_clk.tv_sec;
0064       memcpy(d, &previous_clk.tv_nsec, sizeof(previous_clk.tv_nsec));
0065       d+=2;
0066       *d++ = clk2.tv_sec;
0067       memcpy(d, &clk2.tv_nsec, sizeof(clk2.tv_nsec));
0068       d+=2;
0069     }
0070       previous_clk = clk;
0071 
0072       sevt->sub_length += 9;
0073       return  sevt->sub_length;
0074     }
0075 
0076   else
0077     {
0078       sevt =  (subevtdata_ptr) adr;
0079       // set the initial subevent length
0080       sevt->sub_length =  SEVTHEADERLENGTH;
0081 
0082       // update id's etc
0083       sevt->sub_id =  m_subeventid;
0084       sevt->sub_type=4;
0085       sevt->sub_decoding = IDRTCLK;
0086       sevt->reserved[0] = 0;
0087       sevt->reserved[1] = 0;
0088       
0089       
0090       int  *d = (int *) &sevt->data;
0091       int s = clock_gettime(CLOCK_MONOTONIC_RAW, &clk);
0092       int sm = clock_gettime(CLOCK_MONOTONIC, &clk2);
0093       
0094       if  (s)  // error, set everything to 0
0095     {
0096       for ( int i = 0; i < 6; i++)  *d++ = 0;
0097     }
0098       else
0099     {
0100       *d++ = clk.tv_sec;
0101       memcpy(d, &clk.tv_nsec, sizeof(clk.tv_nsec));
0102       d+=2;
0103       *d++ = previous_clk.tv_sec;
0104       memcpy(d, &previous_clk.tv_nsec, sizeof(previous_clk.tv_nsec));
0105       d+=2;
0106       *d++ = clk2.tv_sec;
0107       memcpy(d, &clk2.tv_nsec, sizeof(clk2.tv_nsec));
0108       d+=2;
0109     }
0110       previous_clk = clk;
0111 
0112       sevt->sub_padding = 1;
0113       sevt->sub_length += 10;
0114       return  sevt->sub_length;
0115     }
0116 }
0117 
0118 
0119 void daq_device_rtclock::identify(std::ostream& os) const
0120 {
0121   
0122   os  << "Real-time clock Device  Event Type: " << m_eventType << " Subevent id: " << m_subeventid << endl; 
0123 
0124 }
0125 
0126 int daq_device_rtclock::max_length(const int etype) const
0127 {
0128   if (etype != m_eventType) return 0;
0129   return  (6 + SEVTHEADERLENGTH);
0130 }
0131 
0132 int  daq_device_rtclock::init()
0133 {
0134   clock_gettime(CLOCK_MONOTONIC_RAW, &previous_clk);
0135 
0136   return 0;
0137 }
0138 
0139 
0140