Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include <stdlib.h>
0002 #include <signal.h>
0003 #include <string>
0004 
0005 #include <stdio.h>
0006 #include <stdlib.h>
0007 #include <unistd.h>
0008 #include <fcntl.h>
0009 
0010 #include "fileEventiterator.h"
0011 #include "oncsEventiterator.h"
0012 #include "Event.h"
0013 
0014 #include "gzbuffer.h"
0015 #include "oncsBuffer.h"
0016 
0017 #include <iostream>
0018 #include <iomanip>
0019 
0020 using namespace std;
0021 
0022 #ifdef HAVE_GETOPT_H
0023 #include "getopt.h"
0024 #endif
0025 
0026 #include<vector>
0027 #include <boost/algorithm/string.hpp>
0028 #include <boost/lexical_cast.hpp>
0029 
0030 #define coutfl cout << __LINE__ << "  " << __FILE__ << " "
0031 
0032 
0033 #define RCDAQEVENTITERATOR 1
0034 #define FILEEVENTITERATOR 2
0035 #define TESTEVENTITERATOR 3
0036 #define ONCSEVENTITERATOR 4
0037 
0038 #if defined(SunOS) || defined(Linux) || defined(OSF1)
0039 void sig_handler(int);
0040 #else
0041 void sig_handler(...);
0042 #endif
0043 
0044 std::vector<int> packetSelection;
0045 
0046 
0047 
0048 int rangeParser ( const std::string string, std::vector<int> &selection);
0049 int subeventid=0;
0050 
0051 
0052 
0053 // we make it a global variable so the signal; handler can get at it.
0054 
0055 void dump(Event *);
0056 void dlist(Event *);
0057   
0058 void exitmsg()
0059 {
0060   COUT << "** usage: lastEvent -div file" << std::endl;
0061   exit(0);
0062 }
0063 
0064 void evtcountexitmsg()
0065 {
0066 
0067   exit(0);
0068 }
0069 
0070 
0071 void exithelp()
0072 {
0073   COUT << std::endl;
0074   COUT << "lastEvent finds the last event(s) in a given file in an efficient way." << std::endl;
0075   COUT << "  List of options: " << std::endl;
0076   //  COUT << " -e <event number>" << std::endl;
0077   //COUT << " -c <number> get nth event (-e gives event with number n)" << std::endl;
0078   COUT << "  -n <number> show the last n events. At most the number of events in the last buffer will be shown." << std::endl;
0079   COUT << "  -i identify the event (default unless -d is given)" << std::endl;
0080   COUT << "  -d dump this event" << std::endl;
0081   COUT << "  -l list the packets (as is dlist)" << std::endl;
0082   COUT << "  -p dump these packet ids - same selections as with ddump" << std::endl;
0083 
0084   exit(0);
0085 }
0086 
0087 #if defined(SunOS) || defined(Linux) || defined(OSF1)
0088 void sig_handler(int i)
0089 #else
0090   void sig_handler(...)
0091 #endif
0092 {
0093   COUT << "sig_handler: signal seen " << std::endl;
0094   exit(0);
0095 }
0096 
0097 
0098 int 
0099 main(int argc, char *argv[])
0100 {
0101 
0102   
0103   if (argc < 2) exitmsg();
0104 
0105   int dump_flag = 0;
0106   int identify_flag = 0;
0107   int list_flag = 0;
0108   unsigned int repeatcount=1;
0109 
0110 
0111   //  extern char *optarg;
0112   extern int optind;
0113   int c;
0114 
0115   
0116   while ((c = getopt(argc, argv, "hildvn:p:")) != EOF)
0117     switch (c) 
0118       {
0119       case 'd':
0120     dump_flag=1;
0121         break;
0122 
0123       case 'i':
0124     identify_flag=1;
0125         break;
0126 
0127       case 'l':
0128     list_flag=1;
0129         break;
0130 
0131       case 'n':
0132     if ( !sscanf(optarg, "%d", &repeatcount) ) exitmsg();
0133     break;
0134 
0135       case 'p':
0136     if ( rangeParser ( optarg, packetSelection) ) exitmsg();
0137     subeventid=1;  // yes, select
0138     break;
0139 
0140       case 'h':
0141     exithelp();
0142     break;
0143       }
0144 
0145   if ( ! dump_flag) identify_flag=1;
0146 
0147   
0148   unsigned int allocatedsize = 0;
0149   PHDWORD *bp = 0;
0150 
0151   if ( optind+1 > argc) exitmsg();
0152     
0153   int fd = open(argv[optind], O_RDONLY | O_LARGEFILE);
0154   if (fd == -1)
0155     {
0156       COUT << "Could not open input stream" << std::endl;
0157       exit(1);
0158     }
0159 
0160   off64_t file_size = lseek64(fd, 0, SEEK_END);
0161   off64_t current_position = file_size;
0162 
0163   unsigned int buffer_size = 0;
0164   unsigned int initialbuffer[BUFFERBLOCKSIZE];
0165   char *cp = (char *) initialbuffer;
0166 
0167   unsigned int xc;
0168 
0169 
0170   while (current_position > 0)
0171     {
0172 
0173 
0174       current_position -= BUFFERBLOCKSIZE;
0175       if (current_position < 0)
0176     {
0177       buffer_size += current_position; // Adjust buffer size for the last read
0178       current_position = 0; // Ensure we don't seek before the beginning
0179     }
0180       
0181         // Seek to the current position
0182         lseek64(fd, current_position, SEEK_SET);
0183 
0184         // Read data into the buffer
0185         ssize_t bytes_read = read(fd, initialbuffer, BUFFERBLOCKSIZE);
0186         if (bytes_read == -1)
0187       {
0188             perror("Error reading from the file");
0189             close(fd);
0190             return 1;
0191       }
0192 
0193       // get the buffer length into a dedicated variable
0194       if (initialbuffer[1] == BUFFERMARKER || initialbuffer[1]== GZBUFFERMARKER 
0195       ||  initialbuffer[1]== LZO1XBUFFERMARKER || initialbuffer[1]== LZO1CBUFFERMARKER ||initialbuffer[1]== ONCSBUFFERMARKER) 
0196     {
0197       buffer_size = initialbuffer[0];
0198     }
0199       else
0200     {
0201       unsigned int  marker = buffer::u4swap(initialbuffer[1]);
0202       if (marker == BUFFERMARKER || marker == GZBUFFERMARKER || marker ==  LZO1XBUFFERMARKER || initialbuffer[1]== LZO1CBUFFERMARKER || marker == ONCSBUFFERMARKER)
0203         {
0204           buffer_size = buffer::u4swap(initialbuffer[0]);
0205         }
0206     }
0207       if ( buffer_size)
0208     {
0209       //cout << "found buffer at " << current_position << " record  " << current_position / BUFFERBLOCKSIZE << endl;
0210       break;
0211     }
0212     }
0213 
0214   buffer *bptr = 0;
0215   
0216   int i;
0217   if (bp) 
0218     {
0219       // this tests if we have enough space in the existing buffer
0220       if  (buffer_size > allocatedsize*4)   // no, we delete and make a bigger one
0221     {
0222       delete [] bp;
0223       i = (buffer_size +BUFFERBLOCKSIZE-1) /BUFFERBLOCKSIZE;
0224       allocatedsize = i * BUFFERBLOCKSIZE/4;
0225       bp = new PHDWORD[allocatedsize];
0226     }
0227     }
0228   else
0229     {
0230       i = (buffer_size +BUFFERBLOCKSIZE-1) /BUFFERBLOCKSIZE;
0231       allocatedsize = i * BUFFERBLOCKSIZE/4;
0232       bp = new PHDWORD[allocatedsize];
0233 
0234     }
0235 
0236   memcpy ( bp, initialbuffer, BUFFERBLOCKSIZE);
0237  
0238   cp = (char *) bp;
0239 
0240   // and update the destination buffer pointer
0241   cp += BUFFERBLOCKSIZE;
0242 
0243   PHDWORD read_so_far =  BUFFERBLOCKSIZE;
0244 
0245   int errorinread=0;
0246 
0247   // we calculate how many BUFFERBLOCKSIZE-sized records we need to read
0248   // we have already one, so this is the number of records -1.
0249   // normally we would round up  (buffer_size + BUFFERBLOCKSIZE -1) /BUFFERBLOCKSIZE
0250   int records_to_read =  (buffer_size -1) /BUFFERBLOCKSIZE;
0251   unsigned int bytes_to_read   =  records_to_read * BUFFERBLOCKSIZE;
0252   
0253   xc = read ( fd, cp, bytes_to_read);
0254   if ( xc < bytes_to_read ) 
0255     {
0256       COUT << "error in buffer, salvaging" << std::endl;
0257       bp[0] = read_so_far + xc; 
0258       errorinread =1;
0259     }
0260 
0261   // and initialize the current_index to be the first event
0262 
0263   if ( ( initialbuffer[1]== GZBUFFERMARKER || 
0264        buffer::u4swap(initialbuffer[1])== GZBUFFERMARKER ||
0265        initialbuffer[1]== LZO1XBUFFERMARKER || 
0266        buffer::u4swap(initialbuffer[1])== LZO1XBUFFERMARKER )
0267        && errorinread  )
0268     {
0269       bptr = 0;
0270       return -3;
0271     }
0272 
0273   i = buffer::makeBuffer( bp, allocatedsize, &bptr);
0274 
0275   Event *evt;
0276 
0277   std::vector<Event *> evt_vector;
0278   
0279   while ( (evt =  bptr->getEvent()) )
0280     {
0281       evt_vector.push_back(evt);
0282     }
0283 
0284   
0285   if ( evt_vector.size() > 0)
0286     {
0287       //coutfl << "repeatcount " << repeatcount << " vector size " << evt_vector.size() << endl;
0288       if ( repeatcount > evt_vector.size()) repeatcount = evt_vector.size();
0289       for ( unsigned int i = evt_vector.size() - repeatcount; i< evt_vector.size(); i++)
0290     {
0291       //cout << setw(4) << i << " ";
0292       if (identify_flag) evt_vector[i]->identify();
0293       if (list_flag)     dlist(evt_vector[i]);
0294       if (dump_flag)     dump(evt_vector[i]);
0295     }
0296     }
0297   
0298   for ( unsigned int i = 0; i < evt_vector.size(); i++)
0299     {
0300       delete evt_vector[i];
0301     }
0302   evt_vector.clear();
0303 
0304   
0305   return 0;
0306 }
0307 
0308 void dump(Event * evt)
0309 {
0310 
0311   if ( subeventid)
0312     {
0313       Packet *s;
0314       std::vector<int>::const_iterator vit;
0315       for (vit= packetSelection.begin(); vit!= packetSelection.end(); ++vit)
0316     {
0317       if ( (s = evt->getPacket(*vit)) )  // get the subevent
0318         {
0319           s->dump();
0320           delete s;
0321         }
0322     }
0323     }
0324   else
0325     {
0326       Packet *p[10000];
0327       int nw = evt->getPacketList(p, 10000);
0328       for (int i=0; i<nw; i++)
0329     {
0330       p[i]->dump();
0331       delete p[i];
0332     }
0333       
0334     }
0335 
0336 }
0337 
0338 void dlist(Event * evt)
0339 {
0340   Packet *p[10000];
0341   int nw = evt->getPacketList(p, 10000);
0342   for (int i=0; i<nw; i++)
0343     {
0344       p[i]->identify();
0345       delete p[i];
0346     }
0347   
0348 }
0349 
0350 
0351 
0352 int rangeParser ( const std::string string, std::vector<int> &selection)
0353 {
0354   std::vector<std::string>::const_iterator it, itr;
0355   std::vector<std::string> strs,r;
0356 
0357 //  std::vector<int>::const_iterator vit;
0358   int low,high,i;
0359   boost::split(strs,string, boost::is_any_of(","));
0360 
0361   for (it= strs.begin(); it!= strs.end(); ++it)
0362     {
0363       boost::split(r,*it,boost::is_any_of("-"));
0364 
0365       itr = r.begin();
0366       low = high =boost::lexical_cast<int>(r[0]);
0367       itr++;
0368       if(itr!=r.end())
0369     {
0370       high = boost::lexical_cast<int>(r[1]);
0371     }
0372       for(i=low;i<=high;++i)
0373     {
0374       selection.push_back(i);
0375     }
0376     }
0377 
0378   //  for(vit= selection.begin(); vit!= selection.end(); ++vit)
0379   //  {
0380   //    std::cout<<*vit<<std::endl;
0381   //  }
0382   return 0;
0383 
0384 }