EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
changehitformat.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file changehitformat.cc
1 #include "A_Event.h"
2 #include "packet.h"
3 #include "Cframe.h"
4 #include "framePackets.h"
5 #include "dataBlock.h"
6 
7 #include "frameRoutines.h"
8 #include "frameHdr.h"
9 
10 #include "fileEventiterator.h"
11 #include "testEventiterator.h"
12 #include "phenixTypes.h"
13 #include "ophBuffer.h"
14 #include "oEvent.h"
15 
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 
20 
21 
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <signal.h>
25 
26 #include <stdio.h>
27 
28 #ifdef HAVE_GETOPT_H
29 #include <getopt.h>
30 #endif
31 
32 #define DDEVENTITERATOR 1
33 #define FILEEVENTITERATOR 2
34 #define TESTEVENTITERATOR 3
35 #define DDPOOL 4
36 #define DFILE 5
37 #define DNULL 6
38 
39 
40 
41 
42 class X_Event : public A_Event
43 {
44 
45 public:
46  // constructors and destructors
47  X_Event( PHDWORD *);
48  X_Event( int *);
49  ~X_Event();
50 
51  // info & debug utils
52 
53  int change_hf(const unsigned int oldhf, const unsigned int newhf);
54 
55 
56 };
57 
59  : A_Event(data)
60 { }
61 
63  : A_Event(data)
64 { }
65 
67 { }
68 
69 int X_Event::change_hf (const unsigned int oldhf, const unsigned int newhf)
70 {
71 
72  int i = 0;
73  PHDWORD *fp;
74  PHDWORD *pp;
75 
76  while ( (fp = framelist[i++]) )
77  {
78  pp = findFramePacketIndex (fp, 0);
79  while ( pp != ptrFailure)
80  {
82  {
83  if (getUnstructPacketHitFormat(pp) == oldhf)
85  }
86  pp = findNextFramePacket(fp, pp);
87  }
88  }
89  return 0;
90 
91 }
92 
93 
94 
95 
96 #if defined(SunOS) || defined(Linux) || defined(OSF1)
97 void sig_handler(int);
98 #else
99 void sig_handler(...);
100 #endif
101 
102 
103 void exitmsg()
104 {
105  COUT << "** usage: changehitformat infile outfile old new [old new...] " << std::endl;
106  exit(0);
107 }
108 
109 void exithelp()
110 {
111  COUT << std::endl;
112 
113 }
114 
115 // The global pointer to the Eventiterator (we must be able to
116 // get at it in the signal handler)
118 
119 
120 
121 int
122 main(int argc, char *argv[])
123 {
124  int c;
125  int status = 0;
126 
127  int eventnr = 0;
128 
129  extern int optind;
130 
131  PHDWORD *buffer;
132  oBuffer *ob;
133  int fd;
134  int buffer_size = 2000000;
135 
136  if (argc <3) exitmsg();
137 
138 
139  while ((c = getopt(argc, argv, "s:d:n:w:vhi")) != EOF)
140  {
141  switch (c)
142  {
143  // the -s (source type) switch
144  case 'h':
145  exithelp();
146  break;
147 
148  default:
149  break;
150  }
151  }
152 
153 
154 
155  // install some handlers for the most common signals
156  signal(SIGKILL, sig_handler);
157  signal(SIGTERM, sig_handler);
158  signal(SIGINT, sig_handler);
159 
160  // see if we can open the file
161 
162 
163  it = new fileEventiterator(argv[optind], status);
164 
165 
166  if (status)
167  {
168  delete it;
169  COUT << "Could not open input stream" << std::endl;
170  exit(1);
171  }
172 
173  unlink ( argv[optind+1] );
174  fd = open(argv[optind+1], O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE ,
175  S_IRWXU | S_IROTH | S_IRGRP );
176  if ( fd < 0)
177  {
178  COUT << "Could not open file: " << argv[optind+1] << std::endl;
179  exit (1);
180  }
181 
182  buffer = new PHDWORD [buffer_size];
183 
184  ob = new ophBuffer (fd, buffer, buffer_size);
185 
186 
187  int paircount = 0;
188  unsigned int idold[1000];
189  unsigned int idnew[1000];
190 
191  int argind = 0;
192  while ( optind +2 + argind +1 < argc)
193  {
194  sscanf(argv[optind +2 + argind] , "%ud", &idold[paircount]);
195  sscanf(argv[optind +2 + argind + 1], "%ud", &idnew[paircount]);
196  COUT << "changing " << idold[paircount] << " -> " << idnew[paircount] << std::endl;
197  argind+=2;
198  paircount++;
199  }
200 
201 
202 
203  // ok. now go through the events
204 
205  Event *evt;
206  X_Event *nevt;
207 
208  int *eb;
209  int nw;
210  int j;
211  while ( ( evt = it->getNextEvent()) )
212  {
213 
214 
215  eb = new int [evt->getEvtLength() +100];
216  evt->Copy (eb, evt->getEvtLength() +100, &nw);
217  nevt = new X_Event(eb);
218  for (j=0; j<paircount; j++)
219  nevt->change_hf(idold[j] , idnew[j]);
220 
221  ob->addEvent(nevt);
222 
223 
224  eventnr++;
225 
226  delete evt;
227  delete nevt;
228  delete [] eb;
229 
230  }
231  delete it;
232 
233 
234  delete ob;
235  close(fd);
236 
237  return 0;
238 }
239 
240 
241 #if defined(SunOS) || defined(Linux) || defined(OSF1)
242 void sig_handler(int i)
243 #else
244  void sig_handler(...)
245 #endif
246 {
247  COUT << "sig_handler: signal seen " << std::endl;
248  if (it) delete it;
249  exit(0);
250 }
251 
252 
253