Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:22:01

0001 // $Id: RDBCconsumer.C,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
0002 //
0003 //     This file is part of the RDBC
0004 //     Author: Valeriy Onuchin <onuchin@sirius.ihep.su>
0005 /////////////////////////////////////////////////////////////////////
0006 //
0007 // Histogram consumer script. Create a canvas and 3 pads. Connect
0008 // to database table, that was created by RDBCproducer.C.
0009 // It reads the histograms from database and displays them
0010 // in the pads.
0011 //
0012 //////////////////////////////////////////////////////////////////////
0013 // 
0014 //  1. Run another root session on another window(or computer)
0015 //  2. Start the RDBCproducer.C script over there
0016 //  3. Start RDBCconsumer script:
0017 //  
0018 //    root[] gSystem->Load("libRDBC.so");    // load library
0019 //    root[] .L RDBCconsumer.C               // load macro
0020 //    root[] RDBCconsumer(dsn,uid,pwd); 
0021 //  
0022 //Begin_Html
0023 /*
0024 <img src="oconsumer.gif">
0025 */
0026 //End_Html
0027 #ifndef __CINT__   
0028 // g++ -c -W RDBCconsumer.C -I$ROOTSYS/include
0029 
0030 #include <TError.h>
0031 #include <TH1.h>
0032 #include <TH2.h>
0033 #include <TProfile.h>
0034 #include <TCanvas.h>
0035 #include <TSystem.h>
0036 #include <TROOT.h>
0037 
0038 #include <RDBC/TSQLDriverManager.h>
0039 #include <RDBC/TSQLConnection.h>
0040 #include <RDBC/TSQLResultSet.h>
0041 #include <RDBC/TSQLResultSetMetaData.h>
0042 #include <RDBC/TSQLPreparedStatement.h>
0043 
0044 #endif // __CINT__
0045 
0046 const TString tableName = "object_test";
0047 
0048 // our histos
0049 TH1F* hpx=0;
0050 TH2F* hpxpy=0;
0051 TProfile* hprof=0;
0052 
0053 //__________________________________________________________________
0054 void DropStuff(TSQLConnection* con)
0055 {
0056    // Drops the database objects.
0057    
0058    TSQLStatement* stmt = con->CreateStatement();
0059    stmt->ExecuteUpdate("drop table " + tableName);
0060    printf("Dropped table %s\n",tableName.Data());
0061    delete stmt;
0062 }
0063    
0064 //__________________________________________________________________
0065 Int_t RDBCconsumer( const Text_t* dsn, 
0066                  const Text_t* usr, 
0067                  const Text_t* pwd )
0068 {
0069    // - connects to database 
0070    // - if table with histos exists ( implies that oproducer.C 
0071    //   script is running on another window/computer )
0072    //   periodically reads them out to display  
0073    
0074   TObject *to;
0075    TSQLConnection* con;
0076    TString str;
0077   
0078    // set error handler
0079    TSQL::SetHandler("Catch(TSQLException*)");
0080   
0081    str = "dsn="; str += dsn; 
0082    str += "; uid="; str += usr; 
0083    
0084    // con = gSQLDriverManager->GetConnection(dsn,usr,pwd);
0085    con = gSQLDriverManager->GetConnection(str);
0086    
0087    if(!con) return -1;  // failed to connect
0088 
0089    printf("\t\t\t DONE.\n");
0090    
0091    TCanvas *c1;
0092    TPad *pad1, *pad2, *pad3;      
0093    
0094    c1 = new TCanvas("c1","Reading objects from Database Example",200,10,700,780);
0095    pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.98,0.98,21);
0096    pad2 = new TPad("pad2","This is pad2",0.02,0.02,0.48,0.48,21);
0097    pad3 = new TPad("pad3","This is pad3",0.52,0.02,0.98,0.48,21);
0098    pad1->Draw();
0099    pad2->Draw();
0100    pad3->Draw(); 
0101    
0102    TSQLStatement* stmt = con->CreateStatement(kTYPE_FORWARD_ONLY,
0103                           kCONCUR_READ_ONLY);
0104 
0105 loop: // wait while oproducer.C script is not started
0106    TSQLResultSet* rs;
0107    rs = stmt->ExecuteQuery("select name, histos from " + tableName);
0108    
0109    if (!rs) {
0110      for(int i=0; i<50; i++) {
0111        gSystem->Sleep(100);   // sleep for 0.1 seconds
0112        gSystem->ProcessEvents();
0113      }
0114      printf("\n\n\t\t** producer not connected yet **\n\n");
0115      goto loop; 
0116    }   
0117            
0118    // Loop displaying the histograms. Once the producer stops this
0119    // script will break out of the loop.
0120    Double_t oldentries = 0;
0121    for (int j = 0; j < 100; j++) 
0122      {
0123        if(hpx) delete hpx;
0124        if(hpxpy) delete hpxpy;
0125        if(hprof) delete hprof;
0126        
0127        rs = stmt->ExecuteQuery("select name, histos from " + tableName);
0128        if (!rs) break;
0129        //       rs->BeforeFirst();
0130        while (rs->Next()) 
0131      {
0132        str = rs->GetString(1);
0133        cout << str << endl;
0134        to = rs->GetObject(2);
0135        if(str=="hpx") hpx = (TH1F *)to;
0136        else if(str=="hpxpy" ) hpxpy  = (TH2F *)to;
0137        else if(str=="hprof") hprof  = (TProfile *)to;
0138      }   
0139        if (hpx->GetEntries() == oldentries) break;
0140        oldentries = hpx->GetEntries();
0141        pad1->cd();
0142        if (hpx) hpx->Draw();
0143        pad2->cd();
0144        if (hprof) hprof->Draw();
0145        pad3->cd();
0146        if (hpxpy) hpxpy->Draw("cont");
0147        c1->Modified();
0148        c1->Update();
0149        
0150        gSystem->Sleep(100);   // sleep for 0.1 seconds
0151        //       if (gSystem->ProcessEvents()) break;
0152        delete rs;
0153      } 
0154    
0155    delete stmt;
0156    
0157    //   DropStuff(con);
0158    con->Commit();
0159    con->Close();
0160    return 0;
0161 }
0162       
0163 //___________________________________________________________________
0164 void Catch(TSQLException* e)
0165 {
0166    // handle exceptions
0167    
0168    TString str = e->GetMessage(); 
0169    printf("%s\n",str.Data());
0170 }