ROOTANA
Loading...
Searching...
No Matches
testODB.cxx
Go to the documentation of this file.
1//
2// testODB.cxx --- test ODB access functions
3//
4// K.Olchanski
5//
6// $Id$
7//
8
9#include <stdio.h>
10#include <sys/time.h>
11#include <iostream>
12#include <assert.h>
13#include <signal.h>
14#include <string.h>
15
16#include "TMidasOnline.h"
17#include "TMidasEvent.h"
18#include "midasio.h"
19#include "mvodb.h"
20
21MVOdb* gOdb = NULL;
22
23// Main function call
24
25int main(int argc, char *argv[])
26{
27 setbuf(stdout,NULL);
28 setbuf(stderr,NULL);
29
30 signal(SIGILL, SIG_DFL);
31 signal(SIGBUS, SIG_DFL);
32 signal(SIGSEGV, SIG_DFL);
33 signal(SIGPIPE, SIG_DFL);
34
35 const char* hostname = NULL;
36 const char* exptname = NULL;
37 const char* filename = argv[1];
38 bool online = false;
39 bool xmlfile = false;
40 bool jsonfile = false;
41
42 if (!filename)
43 online = true;
44 else if (strstr(filename, ".xml")!=0)
45 xmlfile = true;
46 else if (strstr(filename, ".json")!=0)
47 jsonfile = true;
48
49 if (online) {
51
52 int err = midas->connect(hostname, exptname, "rootana");
53 if (err != 0) {
54 fprintf(stderr,"Cannot connect to MIDAS, error %d\n", err);
55 return -1;
56 }
57
58 gOdb = MakeMidasOdb(midas->fDB);
59 } else if (xmlfile) {
60 gOdb = MakeXmlFileOdb(filename);
61 //odb->DumpTree();
62 } else if (jsonfile) {
63 gOdb = MakeJsonFileOdb(filename);
64 //odb->DumpTree();
65 } else {
66 TMReaderInterface* reader = TMNewReader(filename);
67
68 if (reader->fError)
69 {
70 printf("Cannot open input file \"%s\"\n",filename);
71 delete reader;
72 return -1;
73 }
74
75 while (1)
76 {
77 TMidasEvent event;
78 if (!TMReadEvent(reader, &event))
79 break;
80
81 int eventId = event.GetEventId();
82 //printf("Have an event of type %d\n",eventId);
83
84 if ((eventId & 0xFFFF) == 0x8000)
85 {
86 // begin run
87 //event.Print();
88
89 //
90 // Load ODB contents from the ODB XML file
91 //
92 if (gOdb) {
93 delete gOdb;
94 gOdb = NULL;
95 }
96 gOdb = MakeFileDumpOdb(event.GetData(),event.GetDataSize());
97 break;
98 }
99 }
100
101 reader->Close();
102 delete reader;
103 reader = NULL;
104
105 if (!gOdb)
106 {
107 printf("Failed to load ODB from input file \"%s\"\n",filename);
108 return -1;
109 }
110 }
111
112#if 0
113 printf("read run number (odbReadInt): %d\n", gOdb->odbReadInt("/runinfo/Run number"));
114 printf("read array size of /test: %d\n", gOdb->odbReadArraySize("/test"));
115 printf("read array size of /runinfo/run number: %d\n", gOdb->odbReadArraySize("/runinfo/Run number"));
116 printf("read array size of /test/intarr: %d\n", gOdb->odbReadArraySize("/test/intarr"));
117 printf("read array values:\n");
118 int size = gOdb->odbReadArraySize("/test/intarr");
119 for (int i=0; i<size; i++)
120 printf(" intarr[%d] = %d\n", i, gOdb->odbReadInt("/test/intarr", i));
121 printf("read double value: %f\n", gOdb->odbReadDouble("/test/dblvalue"));
122 printf("read uint32 value: %d\n", gOdb->odbReadUint32("/test/dwordvalue"));
123 printf("read bool value: %d\n", gOdb->odbReadBool("/test/boolvalue"));
124 const char* s = gOdb->odbReadString("/test/stringvalue");
125 int len=0;
126 if (s)
127 len = strlen(s);
128 printf("read string value: [%s] length %d\n", s, len);
129
130 printf("\nTry non-existent entries...\n\n");
131
132 printf("read array size: %d\n", gOdb->odbReadArraySize("/test/doesnotexist"));
133 printf("read uint32 value: %d\n", gOdb->odbReadUint32("/test/doesnotexist", 0, -9999));
134
135 printf("\nTry wrong types...\n\n");
136
137 printf("read float value: %f\n", gOdb->odbReadDouble("/test/fltvalue"));
138 printf("read uint32 value: %d\n", gOdb->odbReadUint32("/test/wordvalue"));
139 printf("read int value: %d\n", gOdb->odbReadInt("/test/wordvalue"));
140
141 printf("\nTry wrong array indices...\n\n");
142
143 printf("try to index a non-array: %f\n", gOdb->odbReadDouble("/test/dblvalue", 10, -9999));
144 printf("try invalid index -1: %d\n", gOdb->odbReadInt("/test/intarr", -1, -9999));
145 printf("try invalid index 999: %d\n", gOdb->odbReadInt("/test/intarr", 999, -9999));
146 printf("try invalid index 1 for double value: %f\n", gOdb->odbReadDouble("/test/dblvalue", 1, -9999));
147#endif
148
149 return 0;
150}
151
152//end
Definition mvodb.h:21
virtual int Close()=0
MIDAS event.
Definition TMidasEvent.h:22
uint32_t GetDataSize() const
return the event size
uint16_t GetEventId() const
return the event id
char * GetData()
return pointer to the data buffer
static TMidasOnline * instance()
int connect(const char *hostname, const char *exptname, const char *progname)
Connect to MIDAS experiment.
HNDLE fDB
ODB handle.
TMEvent * TMReadEvent(TMReaderInterface *reader)
Definition midasio.cxx:585
TMReaderInterface * TMNewReader(const char *source)
Definition midasio.cxx:447
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
MVOdb * MakeXmlFileOdb(const char *filename, MVOdbError *error=NULL)
Definition mxmlodb.cxx:689
MVOdb * MakeJsonFileOdb(const char *filename, MVOdbError *error=NULL)
Definition mjsonodb.cxx:536
int main(int argc, char *argv[])
Definition testODB.cxx:25
MVOdb * gOdb
Definition testODB.cxx:21