Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include "A_Event.h"
0002 #include "packet.h"
0003 #include "Cframe.h"
0004 #include "framePackets.h"
0005 #include "dataBlock.h"
0006 
0007 #include "frameRoutines.h"
0008 #include "frameHdr.h"
0009 
0010 #include "fileEventiterator.h"
0011 #include "testEventiterator.h"
0012 #include "phenixTypes.h"
0013 #include "ophBuffer.h"
0014 #include "oEvent.h"
0015 
0016 
0017 #include <sys/types.h>
0018 #include <sys/stat.h>
0019 #include <fcntl.h>
0020 
0021 
0022 
0023 #include <stdlib.h>
0024 #include <signal.h>
0025 #include <unistd.h>
0026 
0027 #include <stdio.h>
0028 
0029 #ifdef HAVE_GETOPT_H
0030 #include <getopt.h>
0031 #endif
0032 
0033 #define DDEVENTITERATOR 1
0034 #define FILEEVENTITERATOR 2
0035 #define TESTEVENTITERATOR 3
0036 #define DDPOOL 4
0037 #define DFILE 5
0038 #define DNULL 6
0039 
0040 
0041 
0042 
0043 class X_Event : public A_Event 
0044 {
0045 
0046 public:
0047   // constructors and destructors
0048   X_Event( PHDWORD *);
0049   X_Event( int *);
0050   ~X_Event();
0051 
0052    // info & debug utils
0053    
0054    int change_id(const int oldid, const int newid);
0055 
0056 
0057 };
0058 
0059 X_Event::X_Event (PHDWORD *data)
0060   : A_Event(data)
0061 { }
0062 
0063 X_Event::X_Event (int *data)
0064   : A_Event(data)
0065 { }
0066 
0067 X_Event::~X_Event ()
0068 { }
0069 
0070 int X_Event::change_id (const int oldid, const int newid)
0071 {
0072   
0073   int i = 0;
0074   PHDWORD *fp;
0075   PHDWORD *pp;
0076   UINT ids = oldid;
0077   
0078   while ( (fp = framelist[i++]) )
0079     {
0080       if ( ( pp = findFramePacketId (fp, ids) ) !=  ptrFailure) 
0081     {
0082       setPacketId (pp, newid);
0083     }
0084     }
0085   return 0;
0086 
0087 }
0088 
0089 
0090 
0091 
0092 #if defined(SunOS) || defined(Linux) || defined(OSF1)
0093 void sig_handler(int);
0094 #else
0095 void sig_handler(...);
0096 #endif
0097 
0098 
0099 void exitmsg()
0100 {
0101   COUT << "** usage: changeid infile outfile id1 id2 " << std::endl;
0102   exit(0);
0103 }
0104 
0105 void exithelp()
0106 {
0107   COUT <<  std::endl;
0108 
0109 }
0110 
0111 // The global pointer to the Eventiterator (we must be able to 
0112 // get at it in the signal handler) 
0113 Eventiterator *it;
0114 
0115 
0116 
0117 int 
0118 main(int argc, char *argv[])
0119 {
0120   int c;
0121   int status = 0;
0122 
0123   int eventnr = 0;
0124 
0125   extern int optind;
0126 
0127   PHDWORD  *buffer;
0128   oBuffer *ob;
0129   int fd;
0130   int buffer_size = 2000000;
0131 
0132   if (argc < 2) exitmsg();
0133 
0134   while ((c = getopt(argc, argv, "s:d:n:w:vhi")) != EOF)
0135     {
0136       switch (c) 
0137     {
0138       // the -s (source type) switch
0139     case 'h':
0140       exithelp();
0141       break;
0142 
0143     default:
0144       break;
0145     }
0146     }
0147 
0148 
0149 
0150   // install some handlers for the most common signals
0151   signal(SIGKILL, sig_handler);
0152   signal(SIGTERM, sig_handler);
0153   signal(SIGINT,  sig_handler);
0154 
0155   // see if we can open the file
0156 
0157 
0158   it = new fileEventiterator(argv[optind], status);
0159        
0160 
0161   if (status)
0162     {
0163       delete it;
0164       COUT << "Could not open input stream" << std::endl;
0165       exit(1);
0166     }
0167 
0168   unlink ( argv[optind+1] );
0169   fd = open(argv[optind+1], O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE , 
0170           S_IRWXU | S_IROTH | S_IRGRP );
0171   if ( fd < 0) 
0172     {
0173       COUT << "Could not open file: " <<  argv[optind+1] << std::endl;
0174       exit (1);
0175     }
0176 
0177   buffer = new PHDWORD [buffer_size];
0178   
0179   ob = new ophBuffer (fd, buffer, buffer_size);
0180 
0181 
0182   int paircount = 0;
0183   int idold[1000];
0184   int idnew[1000];
0185 
0186   int argind = 0;
0187   while ( optind +2 + argind +1 < argc) 
0188     {
0189       sscanf(argv[optind +2 + argind]    , "%d", &idold[paircount]); 
0190       sscanf(argv[optind +2 + argind + 1], "%d", &idnew[paircount]); 
0191       COUT << "changing  " << idold[paircount] << " -> " << idnew[paircount] << std::endl;
0192       argind+=2;
0193       paircount++;
0194     }
0195 
0196     
0197 
0198   // ok. now go through the events
0199 
0200   Event *evt;
0201   X_Event *nevt;
0202 
0203   int *eb;
0204   int nw;
0205   int j;
0206   while (  ( evt = it->getNextEvent())  )
0207     {
0208 
0209 
0210       eb = new int [evt->getEvtLength() +100];
0211       evt->Copy (eb, evt->getEvtLength() +100, &nw);
0212       nevt = new X_Event(eb);
0213       for (j=0; j<paircount; j++)
0214     nevt->change_id(idold[j] , idnew[j]);
0215 
0216       ob->addEvent(nevt);
0217 
0218 
0219       eventnr++;
0220 
0221       delete evt;
0222       delete nevt;
0223       delete [] eb;
0224 
0225     }
0226   delete it;
0227 
0228 
0229   delete ob;
0230   close(fd);
0231       
0232   return 0;
0233 }
0234     
0235 
0236 #if defined(SunOS) || defined(Linux) || defined(OSF1)
0237 void sig_handler(int i)
0238 #else
0239   void sig_handler(...)
0240 #endif
0241 {
0242   COUT << "sig_handler: signal seen " << std::endl;
0243   if (it) delete it;
0244   exit(0);
0245 }
0246 
0247 
0248