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 
0005 #include <daq_device_file.h>
0006 #include <sys/types.h>
0007 #include <unistd.h>
0008 #include <sys/stat.h>
0009 #include <sys/types.h>
0010 #include <fcntl.h>
0011 
0012  
0013 
0014 using namespace std;
0015 
0016 daq_device_file::daq_device_file(const int eventtype
0017                  , const int subeventid 
0018                  , const char *fn
0019                  , const int delete_flag
0020                  , const int maxlength)
0021 {
0022 
0023   m_eventType  = eventtype;
0024   m_subeventid = subeventid;
0025   filename = fn;
0026   _maxlength = maxlength;
0027   _delete_flag = 0;
0028   if ( delete_flag)  _delete_flag = 1;
0029 
0030 }
0031 
0032 daq_device_file::~daq_device_file()
0033 {
0034 }
0035 
0036 
0037 
0038 // the put_data function
0039 
0040 int daq_device_file::put_data(const int etype, int * adr, const int length )
0041 {
0042 
0043   int my_fd;
0044   int nbytes;
0045 
0046   struct stat buf;
0047 
0048   if (etype != m_eventType )  // not our id
0049     {
0050       return 0;
0051     }
0052   
0053 
0054   int status = stat (filename.c_str(), &buf); 
0055   if ( status) 
0056     {
0057       return 0;
0058     }
0059   else
0060     {
0061       nbytes = buf.st_size;
0062       my_fd = open (filename.c_str(), O_RDONLY | O_LARGEFILE);
0063       if  ( my_fd < 0) 
0064     {
0065       return 0;
0066     }
0067     }
0068   
0069 
0070  
0071 
0072   if ( daq_getEventFormat() ) // we are writing PRDF
0073     {
0074 
0075 
0076       if ( (nbytes+3)/4 +  6 + 1 >= length)
0077     {
0078       cout << __FILE__ << "  " << __LINE__ 
0079            <<" too large payload in Packet " <<  m_subeventid 
0080            << "  size " <<  (nbytes+3)/4 + 6 +1  
0081            << "  max is " << length << " -- truncated" << endl;
0082       nbytes = 4*(length - 6 -2);
0083     }
0084 
0085 
0086       formatPacketHdr(adr);
0087 
0088       packetdata_ptr sevt =  (packetdata_ptr) adr;
0089   
0090       // update id's etc
0091       sevt->sub_id =  m_subeventid;
0092       sevt->sub_type=1;
0093       sevt->sub_decoding = 30000 + IDCSTR;
0094   
0095       char  *d = (char *) &sevt->data;
0096 
0097       int c = read( my_fd, d, nbytes);
0098 
0099       close (my_fd);
0100 
0101       if ( _delete_flag) unlink (filename.c_str());      
0102 
0103       int padding  = c%8;
0104       if ( padding) padding = 8-padding;
0105 
0106       sevt->structureinfo += padding;
0107       sevt->sub_length += (c + padding) /4;
0108 
0109       //      std::cout << __FILE__ << " " << __LINE__ << " c " << c << " pad " << padding << " length  " << sevt->sub_length << std::endl; 
0110 
0111       return  sevt->sub_length;
0112     }
0113 
0114   else // the good format
0115     {
0116 
0117       if ( (nbytes+3)/4 +  SEVTHEADERLENGTH + 1 >= length)
0118     {
0119       cout << __FILE__ << "  " << __LINE__ 
0120            <<" too large payload in Packet " <<  m_subeventid 
0121            << "  size " <<  (nbytes+3)/4 + SEVTHEADERLENGTH +1  
0122            << "  max is " << length << " -- truncated" << endl;
0123       nbytes = 4*(length - SEVTHEADERLENGTH -2);
0124     }
0125 
0126       subevtdata_ptr sevt =  (subevtdata_ptr) adr;
0127       // set the initial subevent length
0128       sevt->sub_length =  SEVTHEADERLENGTH;
0129   
0130       // update id's etc
0131       sevt->sub_id =  m_subeventid;
0132       sevt->sub_type=1;
0133       sevt->sub_decoding = IDCSTR;
0134       sevt->reserved[0] = 0;
0135       sevt->reserved[1] = 0;
0136   
0137       char  *d = (char *) &sevt->data;
0138 
0139       int c = read( my_fd, d, nbytes);
0140 
0141       close (my_fd);
0142 
0143       if ( _delete_flag) unlink (filename.c_str());      
0144 
0145       sevt->sub_padding = c%8;
0146       if ( sevt->sub_padding) sevt->sub_padding = 8 - sevt->sub_padding;
0147 
0148       sevt->sub_length += (c + sevt->sub_padding) /4;
0149       return  sevt->sub_length;
0150     }
0151 
0152 }
0153 
0154 
0155 void daq_device_file::identify(std::ostream& os) const
0156 {
0157   
0158   os  << "File Device  Event Type: " << m_eventType 
0159       << " Subevent id: " << m_subeventid;
0160   if ( _delete_flag ) os <<  " reading from and deleting ";
0161   else os <<  " reading from ";
0162   os << filename << endl;
0163 
0164 }
0165 
0166 int daq_device_file::max_length(const int etype) const
0167 {
0168   if (etype != m_eventType) return 0;
0169   return  _maxlength  + SEVTHEADERLENGTH +1;
0170 }
0171