Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
prdfoStream.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file prdfoStream.cc
1 #include <prdfoStream.h>
2 
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 
8 #include "EvtConstants.h"
9 
10 using namespace std;
11 
12 // the constructor first ----------------
13 prdfoStream::prdfoStream (const char *filename, const int length)
14 {
15  buffer_sequence = 1;
16 
17  _filename = filename;
18 
19  int *b = new int [length];
20  bptr = ( buffer_ptr ) b;
21 
22  data_ptr = &(bptr->data[0]);
23  max_length = length; // in 32bit units
24  max_size = max_length;
25  bptr->ID = -64;
26  bptr->Bufseq = buffer_sequence;
27 
28  bptr->Runnr = 0;
29  has_buffer = 0;
30  defunct = 0;
31 
32  // test if the file exists, do not overwrite
33  fd = open(_filename.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE ,
34  S_IRWXU | S_IROTH | S_IRGRP );
35  if (fd < 0)
36  {
37  cout << " error opening file " << _filename << endl;
38  perror ( _filename.c_str());
39  fd = 0;
40  defunct = 1;
41  }
42 
43 }
44 
45 
46 // the destructor... ----------------
48 {
49  writeout();
50  int *b = (int *) bptr;
51  delete [] b;
52  if (fd) close (fd);
53 }
54 
55 
56 
57 int prdfoStream::prepare_next( const int iseq
58  , const int irun)
59 {
60  if (defunct)
61  {
62  cout << "defunct oStream object " << endl;
63  return -1;
64  }
65 
66  // re-initialize the event header length
67  bptr->Length = BUFFERHEADERLENGTH*4;
68  bptr->ID = -64;
69  bptr->Bufseq = iseq;
70  if (irun>0) bptr->Runnr = irun;
71 
72  current_index = 0;
73  left = max_size - BUFFERHEADERLENGTH - EOBLENGTH;
74  has_end = 0;
75  has_buffer =1;
76  return 0;
77 }
78 
79 // ---------------------------------------------------------
81 {
82  if (defunct)
83  {
84  cout << "defunct oStream object " << endl;
85  return -1;
86  }
87 
88  if ( ! has_buffer) prepare_next (buffer_sequence++, evt->getRunNumber() );
89 
90  int evtsize = evt->getEvtLength();
91 
92  if (evtsize*4 > left-EOBLENGTH)
93  {
94  //cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Prdf buffer being written" << endl;
95  writeout();
96  }
97 
98  int nw;
99  evt->Copy( &(bptr->data[current_index]), evtsize, &nw);
100  left -= evtsize;
101  current_index += evtsize;
102  bptr->Length += evtsize*4;
103  return 0;
104 }
105 
106 
107 // ----------------------------------------------------------
109 {
110  if (has_end) return -1;
111  bptr->data[current_index++] = 2;
112  bptr->data[current_index++] = 0;
113  bptr->Length += 2*4;
114 
115  has_end = 1;
116  // cout << "addind EOB" << endl;
117  return 0;
118 }
119 
120 unsigned int prdfoStream::writeout ()
121 {
122  if ( bptr->Length == BUFFERHEADERLENGTH*4) return 0;
123  if (!has_end) addEoB();
124 
125  unsigned int bytes;
126 
127  int blockcount = ( bptr->Length + 8192 -1)/8192;
128  int bytecount = blockcount*8192;
129  bytes = writen ( fd, (char *) bptr , bytecount );
130  has_buffer = 0;
131  return bytes;
132 }
133 
134 
135 unsigned int prdfoStream::writen (int fd, char *ptr, const unsigned int nbytes)
136 {
137 
138  unsigned int nleft, nwritten;
139  nleft = nbytes;
140  while ( nleft>0 )
141  {
142  nwritten = write (fd, ptr, nleft);
143  if ( nwritten < 0 )
144  return nwritten;
145 
146  nleft -= nwritten;
147  ptr += nwritten;
148  }
149  return (nbytes-nleft);
150 }