testODB.cxx

Go to the documentation of this file.
00001 //
00002 // testODB.cxx --- test ODB access functions
00003 //
00004 // K.Olchanski
00005 //
00006 // $Id$
00007 //
00008 
00009 #include <stdio.h>
00010 #include <sys/time.h>
00011 #include <iostream>
00012 #include <assert.h>
00013 #include <signal.h>
00014 
00015 #include "TMidasOnline.h"
00016 #include "TMidasFile.h"
00017 #include "TMidasEvent.h"
00018 #ifdef HAVE_ROOT
00019 #include "XmlOdb.h"
00020 #include "HttpOdb.h"
00021 #endif
00022 
00023 VirtualOdb* gOdb = NULL;
00024 
00025 // Main function call
00026 
00027 int main(int argc, char *argv[])
00028 {
00029    setbuf(stdout,NULL);
00030    setbuf(stderr,NULL);
00031  
00032    signal(SIGILL,  SIG_DFL);
00033    signal(SIGBUS,  SIG_DFL);
00034    signal(SIGSEGV, SIG_DFL);
00035    signal(SIGPIPE, SIG_DFL);
00036  
00037    const char* hostname = NULL;
00038    const char* exptname = NULL;
00039    const char* filename = argv[1];
00040    bool online  = false;
00041    bool xmlfile = false;
00042    bool httpfile = false;
00043 
00044    if (!filename)
00045      online = true;
00046    else if (strstr(filename, ".xml")!=0)
00047      xmlfile = true;
00048    else
00049      httpfile = true;
00050 
00051    if (online)
00052      {
00053        TMidasOnline *midas = TMidasOnline::instance();
00054 
00055        int err = midas->connect(hostname, exptname, "rootana");
00056        if (err != 0)
00057          {
00058            fprintf(stderr,"Cannot connect to MIDAS, error %d\n", err);
00059            return -1;
00060          }
00061 
00062        gOdb = midas;
00063      }
00064    else if (xmlfile)
00065      {
00066 #ifdef HAVE_ROOT
00067        XmlOdb* odb = new XmlOdb(filename);
00068        //odb->DumpTree();
00069        gOdb = odb;
00070 #else
00071        printf("This program is compiled without support for XML ODB access\n");
00072        return -1;
00073 #endif
00074      }
00075    else if (httpfile)
00076      {
00077 #ifdef HAVE_ROOT
00078        HttpOdb* odb = new HttpOdb(filename);
00079        //odb->DumpTree();
00080        gOdb = odb;
00081 #else
00082        printf("This program is compiled without support for HTTP ODB access\n");
00083        return -1;
00084 #endif
00085      }
00086    else
00087      {
00088 #ifdef HAVE_ROOT
00089        TMidasFile f;
00090        bool tryOpen = f.Open(filename);
00091 
00092        if (!tryOpen)
00093          {
00094            printf("Cannot open input file \"%s\"\n",filename);
00095            return -1;
00096          }
00097 
00098        while (1)
00099          {
00100            TMidasEvent event;
00101            if (!f.Read(&event))
00102              break;
00103 
00104            int eventId = event.GetEventId();
00105            //printf("Have an event of type %d\n",eventId);
00106 
00107            if ((eventId & 0xFFFF) == 0x8000)
00108              {
00109                // begin run
00110                //event.Print();
00111 
00112                //
00113                // Load ODB contents from the ODB XML file
00114                //
00115                if (gOdb)
00116                  delete gOdb;
00117                gOdb = new XmlOdb(event.GetData(),event.GetDataSize());
00118                break;
00119              }
00120          }
00121 
00122        if (!gOdb)
00123          {
00124            printf("Failed to load ODB from input file \"%s\"\n",filename);
00125            return -1;
00126          }
00127 #else
00128        printf("This program is compiled without support for XML ODB access\n");
00129        return -1;
00130 #endif
00131      }
00132 
00133    printf("read run number (odbReadInt): %d\n", gOdb->odbReadInt("/runinfo/Run number"));
00134    printf("read array size of /test: %d\n", gOdb->odbReadArraySize("/test"));
00135    printf("read array size of /runinfo/run number: %d\n", gOdb->odbReadArraySize("/runinfo/Run number"));
00136    printf("read array size of /test/intarr: %d\n", gOdb->odbReadArraySize("/test/intarr"));
00137    printf("read array values:\n");
00138    int size = gOdb->odbReadArraySize("/test/intarr");
00139    for (int i=0; i<size; i++)
00140      printf("  intarr[%d] = %d\n", i, gOdb->odbReadInt("/test/intarr", i));
00141    printf("read double value: %f\n", gOdb->odbReadDouble("/test/dblvalue"));
00142    printf("read uint32 value: %d\n", gOdb->odbReadUint32("/test/dwordvalue"));
00143    printf("read bool value: %d\n", gOdb->odbReadBool("/test/boolvalue"));
00144    const char* s = gOdb->odbReadString("/test/stringvalue");
00145    int len=0;
00146    if (s)
00147      len = strlen(s);
00148    printf("read string value: [%s] length %d\n", s, len);
00149 
00150    printf("\nTry non-existent entries...\n\n");
00151 
00152    printf("read array size: %d\n", gOdb->odbReadArraySize("/test/doesnotexist"));
00153    printf("read uint32 value: %d\n", gOdb->odbReadUint32("/test/doesnotexist", 0, -9999));
00154 
00155    printf("\nTry wrong types...\n\n");
00156 
00157    printf("read float value: %f\n", gOdb->odbReadDouble("/test/fltvalue"));
00158    printf("read uint32 value: %d\n", gOdb->odbReadUint32("/test/wordvalue"));
00159    printf("read int value: %d\n", gOdb->odbReadInt("/test/wordvalue"));
00160 
00161    printf("\nTry wrong array indices...\n\n");
00162 
00163    printf("try to index a non-array: %f\n", gOdb->odbReadDouble("/test/dblvalue", 10, -9999));
00164    printf("try invalid index -1: %d\n", gOdb->odbReadInt("/test/intarr", -1, -9999));
00165    printf("try invalid index 999: %d\n", gOdb->odbReadInt("/test/intarr", 999, -9999));
00166    printf("try invalid index 1 for double value: %f\n", gOdb->odbReadDouble("/test/dblvalue", 1, -9999));
00167    
00168    return 0;
00169 }
00170 
00171 //end

Generated on 12 Feb 2016 for ROOT Analyzer by  doxygen 1.6.1