Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:41

0001 
0002 /*
0003 **  readFrame.C
0004 **
0005 **    Reads a frame, accesses packets and fills structures 
0006 **    like the drift chamber STAF table dDchDCM
0007 **
0008 */
0009 #include <sys/types.h>
0010 #include <sys/stat.h>
0011 #include <fcntl.h>
0012 #include <unistd.h>
0013 
0014 #include <stdio.h>
0015 
0016 #include "ophBuffer.h"
0017 #include "EventTypes.h"
0018 
0019 #include "phenixOnline.h"
0020 
0021 #include "Cframe.h"
0022 
0023 #define MAXSIZE 8192
0024 #define MAXBUFFERSIZE 256*8192
0025 
0026 
0027 int main (int argc, char** argv) 
0028 {
0029   int runnumber;
0030 
0031   char *filename;
0032   if (argc>=4) filename=argv[1];
0033   else 
0034     {
0035       COUT << "usage: " << argv[0] << " DATAFILE outfile run-number [frames to combine]"<< std::endl; 
0036       return 1; 
0037     }
0038 
0039   int fd =  open(filename, O_RDONLY | O_LARGEFILE);
0040   if (fd < 0  ) 
0041     {
0042       COUT << "ERROR: could not open " << filename << std::endl; 
0043       return 1;
0044     }
0045   unlink (argv[2]);
0046   int outfile = open(argv[2], O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE , 
0047           S_IRWXU | S_IROTH | S_IRGRP ); 
0048   if (outfile < 0 ) 
0049     {
0050       COUT << "ERROR: could not open " << argv[2] << std::endl; 
0051       return 1;
0052     }
0053 
0054   // read the run number
0055   sscanf(argv[3], "%d", &runnumber);
0056   COUT << "will write run number " << runnumber << std::endl; 
0057 
0058    // read the run number
0059   int frames_to_combine = 1;
0060   if (argc==5)
0061     {
0062       sscanf(argv[4], "%d", &frames_to_combine);
0063       COUT << "will combine  " << frames_to_combine << " frames for each event" <<std::endl; 
0064     }
0065  static PHDWORD databuffer[MAXBUFFERSIZE];
0066   ophBuffer *ob = new ophBuffer(outfile, databuffer, MAXBUFFERSIZE, runnumber);
0067 
0068   // add the begin-run event
0069   ob->nextEvent(100, BEGRUNEVENT);
0070 
0071   PHDWORD frame_ptr[MAXSIZE];
0072 
0073   int eventnumber = 0;
0074   // read the first woird of the frame to see how long it is
0075   int nframe = 0;
0076 
0077   int nread;
0078   nread = read(fd, frame_ptr,4*6);    /* read frame from file into memory */ 
0079 
0080   int framelen;
0081   if (! checkFrameEndianism(frame_ptr) ) framelen = singleDwordByteSwap(frame_ptr[0]
0082 );
0083   else framelen = frame_ptr[0];
0084 
0085   // now read the rest...
0086   nread = read(fd, &frame_ptr[6], 4*(framelen-6));
0087   if (! checkFrameEndianism(frame_ptr) ) byteSwapFrame(frame_ptr);
0088 
0089   ob->nextEvent(MAXBUFFERSIZE-100, DATAEVENT);
0090   eventnumber++;
0091   int reallength = 0;
0092 
0093   while ( nread > 0 )
0094     {
0095       // and add the one and only frame as a whole)
0096       ob->addFrame(frame_ptr);
0097       reallength += frame_ptr[0];
0098       nframe++;
0099           
0100       // * now start to read in the next frame
0101           
0102       nread = read(fd, frame_ptr,4*6);    /* read frame from file into memory */ 
0103       
0104       if (! checkFrameEndianism(frame_ptr) ) framelen = singleDwordByteSwap(frame_ptr[0]);
0105       else framelen = frame_ptr[0];
0106 
0107       // now read the rest...
0108       nread = read (fd, &frame_ptr[6], 4*(framelen-6));
0109       if (! checkFrameEndianism(frame_ptr) ) byteSwapFrame(frame_ptr);
0110       if ( nframe >=frames_to_combine)
0111     {
0112       ob->nextEvent(reallength +  100, DATAEVENT);
0113       eventnumber++;
0114       reallength = 0;
0115       nframe = 0;
0116     }
0117     }
0118 
0119   // add the end-run event
0120   ob->nextEvent(100, ENDRUNEVENT);
0121 
0122   COUT << eventnumber << " data events written " 
0123        << std::endl;
0124 
0125 
0126   // delete the oBuffer to wrap things up
0127   delete ob;
0128 
0129   return 0;
0130 }
0131 
0132