Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
prdfsplit.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file prdfsplit.cc
1 
2 
3 #include "buffer.h"
4 #include <cstdio>
5 #include <iostream>
6 #include <iomanip>
7 #include <cstdlib>
8 #include <csignal>
9 
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 
15 
16 
17 void exitmsg()
18 {
19  std::cout << "** usage: prdflist infile outfile1 outfile2 ..." << std::endl;
20  exit(0);
21 }
22 
23 
24 // this function returns o if this not a
25 // valid buffer marker, or 1 for a buffer that
26 // doesn't need swapping, -1 for one that does need swapping
27 
28 int check_buffermarker ( const unsigned int bm)
29 {
30 
31  if ( bm == BUFFERMARKER ||
32  bm == GZBUFFERMARKER ||
33  bm == LZO1XBUFFERMARKER )
34  {
35  return 1;
36  }
37 
38  else if ( buffer::u4swap(bm) == BUFFERMARKER ||
41  {
42  return -1;
43  }
44 
45 
46  return 0;
47 }
48 
49 
50 int main(int argc, char *argv[])
51 {
52 
53  unsigned int buffer[8*8192];
54 
55  int i;
56  int fd;
57 
58  int nr_outfiles = argc-2;
59  int *fdout = new int[nr_outfiles];
60 
61 
62  fd = open(argv[1], O_RDONLY | O_LARGEFILE);
63 
64  for ( i=0; i< nr_outfiles; i++)
65  {
66  // std::cout << "opening file " << argv[2+i] << std::endl;
67 
68  fdout[i] = open(argv[2+i], O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE ,
69  S_IRWXU | S_IROTH | S_IRGRP);
70  if ( fdout[i] < 0)
71  {
72  std::cout << " could not open " << argv[2+i] << std::endl;
73  exit(1);
74  }
75 
76  }
77 
78 
79  int length;
80  int ip;
81 
82  int total_read = 0;
83 
84  int xc;
85 
86  int current_fdnr = 0;
87 
88  xc = read ( fd, (char *)buffer, 8192);
89  // total_read++;
90  while ( xc == 8192 )
91  {
92 
93  ip = 8192;
94 
95  int markerstatus;
96  if ( (markerstatus = check_buffermarker(buffer[1]) ) )
97  {
98 
99  // std::cout << " new buffer " << buffer[0] << std::endl;
100 
101  if ( markerstatus == -1)
102  {
103  length = buffer::i4swap(buffer[0]);
104  }
105  else
106  {
107  length = buffer[0];
108  }
109 
110 
111  int nwritten = write (fdout[current_fdnr], buffer, 8192);
112  if ( nwritten < 8192)
113  {
114  std::cout << " could not write output " << total_read << std::endl;
115  exit(1);
116  }
117 
118  while ( ip < length)
119  {
120  xc = read ( fd, (char *)buffer, 8192);
121  if ( xc < 8192 )
122  {
123  std::cout << "end or error in read loop at rec " << total_read << std::endl;
124  exit(1);
125  }
126  total_read++;
127  ip+= 8192;
128  nwritten = write (fdout[current_fdnr], buffer, 8192);
129  }
130 
131  }
132 
133  if ( ++current_fdnr >= nr_outfiles) current_fdnr = 0;
134 
135  // std::cout << " current fdnr: " << current_fdnr << std::endl;
136  xc = read ( fd, (char *)buffer, 8192);
137 
138  }
139  for ( i=0; i< nr_outfiles; i++)
140  {
141  close (fdout[i]);
142  }
143  return 0;
144 
145 }
146