Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
daq_device_file.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file daq_device_file.cc
1 
2 
3 #include <iostream>
4 
5 #include <daq_device_file.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10 #include <fcntl.h>
11 
12 
13 
14 using namespace std;
15 
17  , const int subeventid
18  , const char *fn
19  , const int delete_flag
20  , const int maxlength)
21 {
22 
23  m_eventType = eventtype;
24  m_subeventid = subeventid;
25  filename = fn;
26  _maxlength = maxlength;
27  _delete_flag = 0;
28  if ( delete_flag) _delete_flag = 1;
29 
30 }
31 
33 {
34 }
35 
36 
37 
38 // the put_data function
39 
40 int daq_device_file::put_data(const int etype, int * adr, const int length )
41 {
42 
43  int my_fd;
44  int nbytes;
45 
46  struct stat buf;
47 
48  if (etype != m_eventType ) // not our id
49  {
50  return 0;
51  }
52 
53 
54  int status = stat (filename.c_str(), &buf);
55  if ( status)
56  {
57  return 0;
58  }
59  else
60  {
61  nbytes = buf.st_size;
62  my_fd = open (filename.c_str(), O_RDONLY | O_LARGEFILE);
63  if ( my_fd < 0)
64  {
65  return 0;
66  }
67  }
68 
69 
70 
71 
72  if ( daq_getEventFormat() ) // we are writing PRDF
73  {
74 
75 
76  if ( (nbytes+3)/4 + 6 + 1 >= length)
77  {
78  cout << __FILE__ << " " << __LINE__
79  <<" too large payload in Packet " << m_subeventid
80  << " size " << (nbytes+3)/4 + 6 +1
81  << " max is " << length << " -- truncated" << endl;
82  nbytes = 4*(length - 6 -2);
83  }
84 
85 
86  formatPacketHdr(adr);
87 
88  packetdata_ptr sevt = (packetdata_ptr) adr;
89 
90  // update id's etc
91  sevt->sub_id = m_subeventid;
92  sevt->sub_type=1;
93  sevt->sub_decoding = 30000 + IDCSTR;
94 
95  char *d = (char *) &sevt->data;
96 
97  int c = read( my_fd, d, nbytes);
98 
99  close (my_fd);
100 
101  if ( _delete_flag) unlink (filename.c_str());
102 
103  int padding = c%8;
104  if ( padding) padding = 8-padding;
105 
106  sevt->structureinfo += padding;
107  sevt->sub_length += (c + padding) /4;
108 
109  // std::cout << __FILE__ << " " << __LINE__ << " c " << c << " pad " << padding << " length " << sevt->sub_length << std::endl;
110 
111  return sevt->sub_length;
112  }
113 
114  else // the good format
115  {
116 
117  if ( (nbytes+3)/4 + SEVTHEADERLENGTH + 1 >= length)
118  {
119  cout << __FILE__ << " " << __LINE__
120  <<" too large payload in Packet " << m_subeventid
121  << " size " << (nbytes+3)/4 + SEVTHEADERLENGTH +1
122  << " max is " << length << " -- truncated" << endl;
123  nbytes = 4*(length - SEVTHEADERLENGTH -2);
124  }
125 
126  subevtdata_ptr sevt = (subevtdata_ptr) adr;
127  // set the initial subevent length
129 
130  // update id's etc
131  sevt->sub_id = m_subeventid;
132  sevt->sub_type=1;
133  sevt->sub_decoding = IDCSTR;
134  sevt->reserved[0] = 0;
135  sevt->reserved[1] = 0;
136 
137  char *d = (char *) &sevt->data;
138 
139  int c = read( my_fd, d, nbytes);
140 
141  close (my_fd);
142 
143  if ( _delete_flag) unlink (filename.c_str());
144 
145  sevt->sub_padding = c%8;
146  if ( sevt->sub_padding) sevt->sub_padding = 8 - sevt->sub_padding;
147 
148  sevt->sub_length += (c + sevt->sub_padding) /4;
149  return sevt->sub_length;
150  }
151 
152 }
153 
154 
155 void daq_device_file::identify(std::ostream& os) const
156 {
157 
158  os << "File Device Event Type: " << m_eventType
159  << " Subevent id: " << m_subeventid;
160  if ( _delete_flag ) os << " reading from and deleting ";
161  else os << " reading from ";
162  os << filename << endl;
163 
164 }
165 
166 int daq_device_file::max_length(const int etype) const
167 {
168  if (etype != m_eventType) return 0;
169  return _maxlength + SEVTHEADERLENGTH +1;
170 }
171