Back to home page

sPhenix code displayed by LXR

 
 

    


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

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