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