ROOTANA
event_dump.cxx
Go to the documentation of this file.
1 //
2 // Example event dump program
3 //
4 // K.Olchanski
5 //
6 // $Id$
7 //
8 
9 #include <stdio.h>
10 #include <sys/time.h>
11 #include <assert.h>
12 #include <signal.h>
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #ifdef HAVE_MIDAS
18 #include "TMidasOnline.h"
19 #endif
20 #include "TMidasEvent.h"
21 
22 #include "midasio.h"
23 #include "mvodb.h"
24 
25 #include <vector>
26 
27 MVOdb* gOdb = NULL;
28 
29 int gEventCutoff = 0;
30 bool gSaveOdb = false;
31 bool gPrintBank = false;
32 
34 {
35  //int eventId = event.GetEventId();
36  if (gPrintBank)
37  event.Print("a");
38  else
39  event.Print();
40 }
41 
42 int ProcessMidasFile(const char*fname)
43 {
44  TMReaderInterface* reader = TMNewReader(fname);
45 
46  if (reader->fError)
47  {
48  printf("Cannot open input file \"%s\"\n", fname);
49  delete reader;
50  return -1;
51  }
52 
53  int i=0;
54  while (1)
55  {
56  TMidasEvent event;
57  if (!TMReadEvent(reader, &event))
58  break;
59 
60  int eventId = event.GetEventId();
61  //printf("Have an event of type %d\n",eventId);
62 
63  if ((eventId & 0xFFFF) == 0x8000)
64  {
65  // begin run
66  event.Print();
67 
68  if (gSaveOdb)
69  {
70  char fname[256];
71 
72  char* ptr = event.GetData();
73  int size = event.GetDataSize();
74 
75  if (strncmp(ptr, "<?xml", 5) == 0)
76  sprintf(fname,"odb%05d.xml", event.GetSerialNumber());
77  else
78  sprintf(fname,"odb%05d.odb", event.GetSerialNumber());
79 
80  FILE* fp = fopen(fname,"w");
81  if (!fp)
82  {
83  fprintf(stderr,"Error: Cannot write ODB to \'%s\', errno %d (%s)\n", fname, errno, strerror(errno));
84  exit(1);
85  }
86 
87  fwrite(ptr, size, 1, fp);
88  fclose(fp);
89 
90  fprintf(stderr,"Wrote ODB to \'%s\'\n", fname);
91  exit(0);
92  }
93 
94  //
95  // Load ODB contents from the ODB XML file
96  //
97  if (gOdb)
98  delete gOdb;
99  gOdb = MakeFileDumpOdb(event.GetData(),event.GetDataSize());
100  }
101  else if ((eventId & 0xFFFF) == 0x8001)
102  {
103  // end run
104  event.Print();
105  }
106  else if ((eventId & 0xFFFF) == 0x8002)
107  {
108  // log message
109  event.Print();
110  printf("Log message: %s\n", event.GetData());
111  }
112  else
113  {
114  event.SetBankList();
115  //event.Print();
116  HandleMidasEvent(event);
117  }
118 
119  if((i%500)==0)
120  {
121  printf("Processing event %d\n",i);
122  }
123 
124  i++;
125  if ((gEventCutoff!=0)&&(i>=gEventCutoff))
126  {
127  printf("Reached event %d, exiting loop.\n", i);
128  break;
129  }
130  }
131 
132  reader->Close();
133  delete reader;
134 
135  return 0;
136 }
137 
138 #ifdef HAVE_MIDAS
139 
140 void startRun(int transition,int run,int time)
141 {
142  printf("Begin run: %d\n", run);
143 }
144 
145 void endRun(int transition,int run,int time)
146 {
147  printf("End of run %d\n",run);
148 }
149 
150 int ProcessMidasOnline(const char* hostname, const char* exptname)
151 {
153 
154  int err = midas->connect(hostname, exptname, "rootana");
155  if (err != 0)
156  {
157  fprintf(stderr,"Cannot connect to MIDAS, error %d\n", err);
158  return -1;
159  }
160 
161  gOdb = MakeMidasOdb(midas->fDB);
162 
163  midas->setTransitionHandlers(startRun,endRun,NULL,NULL);
164  midas->registerTransitions();
165 
166  /* reqister event requests */
167 
168  int req = midas->eventRequest("SYSTEM",-1,-1,(1<<1), true);
169 
170  int i=0;
171  while (1)
172  {
173  char pevent[100*1024];
174  int size = midas->receiveEvent(req, pevent, sizeof(pevent), true);
175 
176  if (size == 0)
177  {
178  if (!midas->poll(1000))
179  break;
180  continue;
181  }
182 
183  if (size <= 0)
184  break;
185 
186  TMidasEvent event;
187  memcpy(event.GetEventHeader(), pevent, sizeof(TMidas_EVENT_HEADER));
188  event.SetData(size, pevent+sizeof(TMidas_EVENT_HEADER));
189  event.SetBankList();
190  HandleMidasEvent(event);
191 
192  if ((i%500)==0)
193  {
194  printf("Processing event %d\n",i);
195  }
196 
197  i++;
198  if ((gEventCutoff!=0) && (i>=gEventCutoff))
199  {
200  printf("Reached event %d, exiting loop.\n", i);
201  break;
202  }
203  }
204 
205  /* disconnect from experiment */
206  midas->disconnect();
207 
208  return 0;
209 }
210 
211 #endif
212 
213 void help()
214 {
215  printf("\nUsage:\n");
216  printf("\n./event_dump.exe [-h] [-Hhostname] [-Eexptname] [-p] [-O] [-eMaxEvents] [file1 file2 ...]\n");
217  printf("\n");
218  printf("\t-h: print this help message\n");
219  printf("\t-Hhostname: connect to MIDAS experiment on given host\n");
220  printf("\t-Eexptname: connect to this MIDAS experiment\n");
221  printf("\t-O: save ODB from midas data file into odbNNNN.xml or .odb file\n");
222  printf("\t-e: Number of events to read from input data files\n");
223  printf("\t-p: Print bank contents\n");
224  printf("\n");
225  printf("Example1: print online events: ./event_dump.exe\n");
226  printf("Example2: print events from file: ./event_dump.exe /data/alpha/current/run00500.mid.gz\n");
227  exit(1);
228 }
229 
230 // Main function call
231 
232 int main(int argc, char *argv[])
233 {
234  setbuf(stdout,NULL);
235  setbuf(stderr,NULL);
236 
237  signal(SIGILL, SIG_DFL);
238  signal(SIGBUS, SIG_DFL);
239  signal(SIGSEGV, SIG_DFL);
240  signal(SIGPIPE, SIG_DFL);
241 
242  std::vector<std::string> args;
243  for (int i=0; i<argc; i++)
244  {
245  if (strcmp(argv[i],"-h")==0)
246  help(); // does not return
247  args.push_back(argv[i]);
248  }
249 #ifdef HAVE_MIDAS
250  const char* hostname = NULL;
251  const char* exptname = NULL;
252 #endif
253  for (unsigned int i=1; i<args.size(); i++) // loop over the commandline options
254  {
255  const char* arg = args[i].c_str();
256  //printf("argv[%d] is %s\n",i,arg);
257 
258  if (strncmp(arg,"-e",2)==0) // Event cutoff flag (only applicable in offline mode)
259  gEventCutoff = atoi(arg+2);
260 #ifdef HAVE_MIDAS
261  else if (strncmp(arg,"-H",2)==0)
262  hostname = strdup(arg+2);
263  else if (strncmp(arg,"-E",2)==0)
264  exptname = strdup(arg+2);
265 #endif
266  else if (strncmp(arg,"-O",2)==0)
267  gSaveOdb = true;
268  else if (strncmp(arg,"-p",2)==0)
269  gPrintBank = true;
270  else if (strcmp(arg,"-h")==0)
271  help(); // does not return
272  else if (arg[0] == '-')
273  help(); // does not return
274  }
275 
276  bool flag = false;
277 
278  for (unsigned int i=1; i<args.size(); i++)
279  {
280  const char* arg = args[i].c_str();
281 
282  if (arg[0] != '-')
283  {
284  flag = true;
285  ProcessMidasFile(arg);
286  }
287  }
288 
289  // if we processed some data files,
290  // do not go into online mode.
291  if (flag)
292  return 0;
293 
294 #ifdef HAVE_MIDAS
295  ProcessMidasOnline(hostname, exptname);
296 #endif
297 
298  return 0;
299 }
300 
301 //end
Definition: mvodb.h:21
virtual int Close()=0
MIDAS event.
Definition: TMidasEvent.h:22
TMidas_EVENT_HEADER * GetEventHeader()
return pointer to the event header
uint32_t GetDataSize() const
return the event size
uint16_t GetEventId() const
return the event id
Definition: TMidasEvent.cxx:93
uint32_t GetSerialNumber() const
return the serial number
char * GetData()
return pointer to the data buffer
int receiveEvent(int requestId, void *pevent, int size, bool async)
Receive event by polling.
void registerTransitions()
Ask MIDAS to tell us about run transitions.
int disconnect()
Disconnect from MIDAS.
static TMidasOnline * instance()
int connect(const char *hostname, const char *exptname, const char *progname)
Connect to MIDAS experiment.
HNDLE fDB
ODB handle.
Definition: TMidasOnline.h:58
void setTransitionHandlers(TransitionHandler start, TransitionHandler stop, TransitionHandler pause, TransitionHandler resume)
Specify user handlers for run transitions.
int eventRequest(const char *bufferName, int eventId, int triggerMask, int samplingType, bool poll=false)
Request data for delivery via callback (setEventHandler) or by polling (via receiveEvent)
bool poll(int mdelay)
Check for all MIDAS events (new data events, run transitions)
void HandleMidasEvent(TMidasEvent &event)
Definition: event_dump.cxx:33
bool gPrintBank
Definition: event_dump.cxx:31
int main(int argc, char *argv[])
Definition: event_dump.cxx:232
MVOdb * gOdb
Definition: event_dump.cxx:27
int gEventCutoff
Definition: event_dump.cxx:29
bool gSaveOdb
Definition: event_dump.cxx:30
void startRun(int transition, int run, int time)
Definition: event_dump.cxx:140
int ProcessMidasOnline(const char *hostname, const char *exptname)
Definition: event_dump.cxx:150
void help()
Definition: event_dump.cxx:213
void endRun(int transition, int run, int time)
Definition: event_dump.cxx:145
int ProcessMidasFile(const char *fname)
Definition: event_dump.cxx:42
TMReaderInterface * TMNewReader(const char *source)
Definition: midasio.cxx:447
TMEvent * TMReadEvent(TMReaderInterface *reader)
Definition: midasio.cxx:585
MVOdb * MakeMidasOdb(int hDB, MVOdbError *error=NULL)
Definition: midasodb.cxx:924
MVOdb * MakeFileDumpOdb(const char *buf, int bufsize, MVOdbError *error=NULL)
Access ODB from a midas file dump. FOrmat could be .xml, .json or .odb.
Definition: mvodb.cxx:91
Event header.
Definition: TMidasStructs.h:16