Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
prdf2prdf.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file prdf2prdf.cc
1 
2 /*
3 ** readFrame.C
4 **
5 ** Reads a frame, accesses packets and fills structures
6 ** like the drift chamber STAF table dDchDCM
7 **
8 */
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 
14 #include <stdio.h>
15 
16 #include "ophBuffer.h"
17 #include "EventTypes.h"
18 
19 #include "phenixOnline.h"
20 
21 #include "Cframe.h"
22 
23 #define MAXSIZE 8192
24 #define MAXBUFFERSIZE 256*8192
25 
26 
27 int main (int argc, char** argv)
28 {
29  int runnumber;
30 
31  char *filename;
32  if (argc>=4) filename=argv[1];
33  else
34  {
35  COUT << "usage: " << argv[0] << " DATAFILE outfile run-number [frames to combine]"<< std::endl;
36  return 1;
37  }
38 
39  int fd = open(filename, O_RDONLY | O_LARGEFILE);
40  if (fd < 0 )
41  {
42  COUT << "ERROR: could not open " << filename << std::endl;
43  return 1;
44  }
45  unlink (argv[2]);
46  int outfile = open(argv[2], O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE ,
47  S_IRWXU | S_IROTH | S_IRGRP );
48  if (outfile < 0 )
49  {
50  COUT << "ERROR: could not open " << argv[2] << std::endl;
51  return 1;
52  }
53 
54  // read the run number
55  sscanf(argv[3], "%d", &runnumber);
56  COUT << "will write run number " << runnumber << std::endl;
57 
58  // read the run number
59  int frames_to_combine = 1;
60  if (argc==5)
61  {
62  sscanf(argv[4], "%d", &frames_to_combine);
63  COUT << "will combine " << frames_to_combine << " frames for each event" <<std::endl;
64  }
65  static PHDWORD databuffer[MAXBUFFERSIZE];
66  ophBuffer *ob = new ophBuffer(outfile, databuffer, MAXBUFFERSIZE, runnumber);
67 
68  // add the begin-run event
69  ob->nextEvent(100, BEGRUNEVENT);
70 
71  PHDWORD frame_ptr[MAXSIZE];
72 
73  int eventnumber = 0;
74  // read the first woird of the frame to see how long it is
75  int nframe = 0;
76 
77  int nread;
78  nread = read(fd, frame_ptr,4*6); /* read frame from file into memory */
79 
80  int framelen;
81  if (! checkFrameEndianism(frame_ptr) ) framelen = singleDwordByteSwap(frame_ptr[0]
82 );
83  else framelen = frame_ptr[0];
84 
85  // now read the rest...
86  nread = read(fd, &frame_ptr[6], 4*(framelen-6));
87  if (! checkFrameEndianism(frame_ptr) ) byteSwapFrame(frame_ptr);
88 
90  eventnumber++;
91  int reallength = 0;
92 
93  while ( nread > 0 )
94  {
95  // and add the one and only frame as a whole)
96  ob->addFrame(frame_ptr);
97  reallength += frame_ptr[0];
98  nframe++;
99 
100  // * now start to read in the next frame
101 
102  nread = read(fd, frame_ptr,4*6); /* read frame from file into memory */
103 
104  if (! checkFrameEndianism(frame_ptr) ) framelen = singleDwordByteSwap(frame_ptr[0]);
105  else framelen = frame_ptr[0];
106 
107  // now read the rest...
108  nread = read (fd, &frame_ptr[6], 4*(framelen-6));
109  if (! checkFrameEndianism(frame_ptr) ) byteSwapFrame(frame_ptr);
110  if ( nframe >=frames_to_combine)
111  {
112  ob->nextEvent(reallength + 100, DATAEVENT);
113  eventnumber++;
114  reallength = 0;
115  nframe = 0;
116  }
117  }
118 
119  // add the end-run event
120  ob->nextEvent(100, ENDRUNEVENT);
121 
122  COUT << eventnumber << " data events written "
123  << std::endl;
124 
125 
126  // delete the oBuffer to wrap things up
127  delete ob;
128 
129  return 0;
130 }
131 
132