ROOTANA
test_midasio.cxx
Go to the documentation of this file.
1 //
2 // test_midasio.cxx --- test midasio classes
3 //
4 // K.Olchanski
5 //
6 
7 #include <stdio.h>
8 #include <sys/time.h>
9 #include <iostream>
10 #include <assert.h>
11 #include <signal.h>
12 #include <string.h>
13 #include <stdlib.h> // exit()
14 
15 #ifdef HAVE_MIDAS
16 #include "TMidasOnline.h"
17 #endif
18 #ifdef HaVE_MVODB
19 #include "mvodb.h"
20 #endif
21 
22 #include "midasio.h"
23 
24 std::string toString(int i)
25 {
26  char buf[256];
27  sprintf(buf, "%d", i);
28  return buf;
29 }
30 
31 void print_ia(const std::vector<int> &ia)
32 {
33  int size = ia.size();
34  printf("int[%d] has [", size);
35  for (int i=0; i<size; i++) {
36  if (i>0)
37  printf(", ");
38  printf("%d", ia[i]);
39  }
40  printf("]");
41 }
42 
43 void print_da(const std::vector<double> &da)
44 {
45  int size = da.size();
46  printf("int[%d] has [", size);
47  for (int i=0; i<size; i++) {
48  if (i>0)
49  printf(", ");
50  printf("%f", da[i]);
51  }
52  printf("]");
53 }
54 
55 static int gCountFail = 0;
56 
57 void report_fail(const char* text)
58 {
59  printf("FAIL: %s\n", text);
60  gCountFail++;
61 }
62 
63 void usage()
64 {
65  fprintf(stderr, "Usage: test_midasio.exe [-h] [-o output_file] input_file1 [input_file2...]\n");
66  exit(1); // DOES NOT RETURN
67 }
68 
69 // Main function
70 
71 int main(int argc, char *argv[])
72 {
73  setbuf(stdout,NULL);
74  setbuf(stderr,NULL);
75 
76  signal(SIGILL, SIG_DFL);
77  signal(SIGBUS, SIG_DFL);
78  signal(SIGSEGV, SIG_DFL);
79  signal(SIGPIPE, SIG_DFL);
80 
81 #ifdef HAVE_MIDAS
82  const char* hostname = NULL;
83  const char* exptname = NULL;
84 #endif
85  std::vector<std::string> rfilenames;
86  std::string wfilename;
87  bool online = false;
88  bool test_init = false;
89 
90  for (int i=1; i<argc; i++) {
91  if (strcmp(argv[i], "-h")==0) {
92  usage();
93  } else if (strcmp(argv[i], "--help")==0) {
94  usage();
95  } else if (strcmp(argv[i], "-i")==0) {
96  test_init = true;
97  } else if (strcmp(argv[i], "-o")==0) {
98  i++;
99  if (i == argc) usage();
100  wfilename = argv[i];
101  } else if (argv[i][0] == '-') {
102  fprintf(stderr, "invalid switch: \"%s\"\n", argv[i]);
103  usage();
104  } else {
105  rfilenames.push_back(argv[i]);
106  }
107  }
108 
109  TMWriterInterface* writer = NULL;
110 
111  if (!wfilename.empty()) {
112  writer = TMNewWriter(wfilename.c_str());
113  }
114 
115  if (test_init) {
116  for (int i=0; i<10; i++) {
117  TMEvent event;
118  event.PrintHeader();
119  event.Init(1);
120  event.PrintHeader();
121  //event.DumpHeader();
122  uint32_t buf32[3];
123  buf32[0] = 0x11111111;
124  buf32[1] = 0x22222222;
125  buf32[2] = 0x33333333;
126  event.AddBank("AAAA", TID_UINT32, (char*)buf32, sizeof(buf32));
127  event.PrintHeader();
128  //event.DumpHeader();
129  event.PrintBanks();
130  uint64_t buf64[3];
131  buf64[0] = 0x1111111111111111;
132  buf64[1] = 0x2222222222222222;
133  buf64[2] = 0x3333333333333333;
134  event.AddBank("BBBB", TID_UINT64, (char*)buf64, sizeof(buf64));
135  event.PrintHeader();
136  //event.DumpHeader();
137  event.PrintBanks();
138  if (writer) {
139  TMWriteEvent(writer, &event);
140  }
141  }
142  }
143 
144  if (rfilenames.empty())
145  online = true;
146 
147 #if HAVE_MVODB
148  MVOdb* odb = NULL;
149  MVOdbError odberror;
150 #endif
151 
152  if (online) {
153 #ifdef HAVE_MIDAS
154  printf("Using MidasOdb\n");
155  midas = TMidasOnline::instance();
156 
157  int err = midas->connect(hostname, exptname, "test_mvodb");
158  if (err != 0) {
159  fprintf(stderr,"Cannot connect to MIDAS, error %d\n", err);
160  return -1;
161  }
162 
163  odb = MakeMidasOdb(midas->fDB, &odberror);
164 
165  if (midas)
166  midas->disconnect();
167 #else
168  printf("MIDAS support not available, sorry! Bye.\n");
169  exit(1);
170 #endif
171  } else {
172  int counter = 0;
173  for (size_t ri=0; ri<rfilenames.size(); ri++) {
174  TMReaderInterface* reader = TMNewReader(rfilenames[ri].c_str());
175 
176  if (reader->fError) {
177  printf("Cannot open input file \"%s\"\n", rfilenames[ri].c_str());
178  delete reader;
179  continue;
180  }
181 
182  while (1) {
183  TMEvent* e = TMReadEvent(reader);
184 
185  if (!e) {
186  // EOF
187  delete e;
188  break;
189  }
190 
191  if (e->error) {
192  // broken event
193  printf("event with error, bye!\n");
194  delete e;
195  break;
196  }
197 
198  printf("Event: id 0x%04x, mask 0x%04x, serial 0x%08x, time 0x%08x, data size %d\n",
199  e->event_id,
200  e->trigger_mask,
201  e->serial_number,
202  e->time_stamp,
203  e->data_size);
204 
205  if ((e->event_id & 0xFFFF) == 0x8000) {
206  printf("Event: this is a begin of run ODB dump\n");
207 #ifdef HAVE_MVODB
208  odb = MakeFileDumpOdb(event.GetData(),event.GetDataSize(), &odberror);
209 #endif
210  if (writer) {
211  TMWriteEvent(writer, e);
212  }
213  delete e;
214  continue;
215  } else if ((e->event_id & 0xFFFF) == 0x8001) {
216  printf("Event: this is an end of run ODB dump\n");
217 #ifdef HAVE_MVODB
218  odb = MakeFileDumpOdb(event.GetData(),event.GetDataSize(), &odberror);
219 #endif
220  if (writer) {
221  TMWriteEvent(writer, e);
222  }
223  delete e;
224  continue;
225  }
226 
227  if (counter == 0) {
228  e->PrintBanks(2);
229  } else if (counter == 1) {
230  e->PrintBanks(1);
231  } else {
232  e->PrintBanks();
233  }
234 
235  if (writer) {
236  TMWriteEvent(writer, e);
237  }
238 
239  counter++;
240 
241  delete e;
242  }
243 
244  reader->Close();
245  delete reader;
246  }
247  }
248 
249  if (writer) {
250  writer->Close();
251  delete writer;
252  writer = NULL;
253  }
254 
255  return 0;
256 }
257 
258 //end
259 /* emacs
260  * Local Variables:
261  * tab-width: 8
262  * c-basic-offset: 3
263  * indent-tabs-mode: nil
264  * End:
265  */
Definition: mvodb.h:21
void PrintHeader() const
Definition: midasio.cxx:1099
void PrintBanks(int level=0)
Definition: midasio.cxx:1112
uint32_t time_stamp
MIDAS event time stamp (unix time in sec)
Definition: midasio.h:60
uint32_t serial_number
MIDAS event serial number.
Definition: midasio.h:59
uint32_t data_size
MIDAS event data size.
Definition: midasio.h:61
uint16_t trigger_mask
MIDAS trigger mask.
Definition: midasio.h:58
uint16_t event_id
MIDAS event ID.
Definition: midasio.h:57
bool error
event has an error - incomplete, truncated, inconsistent or corrupted
Definition: midasio.h:55
virtual int Close()=0
virtual int Close()=0
static TMidasOnline * instance()
#define TID_UINT64
Definition: midasio.h:38
void TMWriteEvent(TMWriterInterface *writer, const TMEvent *event)
Definition: midasio.cxx:651
TMReaderInterface * TMNewReader(const char *source)
Definition: midasio.cxx:447
TMWriterInterface * TMNewWriter(const char *destination)
Definition: midasio.cxx:543
#define TID_UINT32
Definition: midasio.h:23
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
void print_da(const std::vector< double > &da)
int main(int argc, char *argv[])
static int gCountFail
void usage()
void report_fail(const char *text)
void print_ia(const std::vector< int > &ia)
std::string toString(int i)