Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 // $Id: RDBCproducer.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 producer script. This script stores three histogram objects 
0008 // in a datbase (a TH1F, a TH2F and a TProfile). It then fills, in an 
0009 // infinite loop (so use ctrl-c to stop this script), the three histogram 
0010 // objects with random numbers. Every 1000 fills  the objects are updated 
0011 // in database.
0012 // 
0013 //  Use the oconsumer.C script to map this file and display the histograms.
0014 //
0015 //
0016 //    REQUIREMENTS
0017 //
0018 //  You must have create table/drop table rights.
0019 //
0020 //////////////////////////////////////////////////////////////////////
0021 //
0022 // Usage:
0023 //  
0024 // root[] gSystem->Load("libRDBC.so");  // load library
0025 // root[] .L RDBCproducer.C             // load macro
0026 // root[] RDBCproducer(dsn,uid,pwd);    // execute the function from macro
0027 //  
0028 
0029 #ifndef __CINT__   
0030 // g++ -c -W RDBCproducer.C -I$ROOTSYS/include
0031 
0032 #include <TError.h>
0033 #include <TH1.h>
0034 #include <TH2.h>
0035 #include <TProfile.h>
0036 #include <TRandom.h>
0037 #include <TROOT.h>
0038 #include <TSQLDriverManager.h>
0039 #include <TSQLConnection.h>
0040 #include <TSQLResultSet.h>
0041 #include <TSQLResultSetMetaData.h>
0042 #include <TSQLPreparedStatement.h>
0043 
0044 #endif // __CINT__
0045 
0046 const TString tableName = "object_test";
0047 
0048 // our histos
0049 TH1F* hpx;
0050 TH2F* hpxpy;
0051 TProfile* hprof;
0052 
0053 //__________________________________________________________________
0054 void CreateStuff(TSQLConnection* con)
0055 {
0056    // Creates the database objects needed
0057    
0058    // create our table
0059     TSQLStatement* stmt = con->CreateStatement();
0060 
0061     stmt->ExecuteUpdate( "delete from " + tableName);
0062     delete stmt;
0063    
0064    TSQLPreparedStatement* pstmt = con->PrepareStatement( 
0065                                        "insert into " +tableName+ 
0066                                        "(name,histos) values(?,?)" );
0067    
0068    hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
0069    hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
0070    hprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
0071    
0072    // Set a fill color for the TH1F
0073    hpx->SetFillColor(48);
0074    
0075    Float_t px, py, pz;
0076    int ii = 0;
0077    
0078    // Endless loop filling histograms with random numbers
0079    for (ii = 0; ii < 1000; ii++) 
0080      {
0081        gRandom->Rannor(px,py);
0082        pz = px*px + py*py;
0083        hpx->Fill(px);
0084        hpxpy->Fill(px,py);
0085        hprof->Fill(px,pz);
0086      }
0087 
0088    pstmt->SetString(1,"hpx");
0089    pstmt->SetObject(2,hpx);
0090    pstmt->ExecuteUpdate();
0091    
0092    pstmt->SetString(1,"hpxpy");
0093    pstmt->SetObject(2,hpxpy);
0094    pstmt->ExecuteUpdate();
0095    
0096    pstmt->SetString(1,"hprof");
0097    pstmt->SetObject(2,hprof);
0098    pstmt->ExecuteUpdate();
0099    
0100    con->Commit();
0101    delete pstmt;
0102 }
0103 
0104 //__________________________________________________________________
0105 void DropStuff(TSQLConnection* con)
0106 {
0107    // Drops the database objects.
0108    
0109    TSQLStatement* stmt = con->CreateStatement();
0110    stmt->ExecuteUpdate("drop table " + tableName);
0111    printf("Dropped table %s\n",tableName.Data());
0112    delete stmt;
0113 }
0114 
0115 //__________________________________________________________________
0116 void Fill(TSQLConnection* con)
0117 {
0118    // Endless loop filling histograms with random numbers
0119    
0120    TString str;
0121 
0122    TSQLStatement* stmt = 
0123      con->CreateStatement(kTYPE_SCROLL_SENSITIVE, kCONCUR_UPDATABLE);
0124    if (!stmt) return;
0125    
0126    con->Print("a");
0127    
0128    TSQLResultSet* rs;
0129          
0130    Float_t px, py, pz;
0131    int ii = 0;
0132    
0133    // Endless loop filling histograms with random numbers
0134    while (1) 
0135      {
0136        gRandom->Rannor(px,py);
0137        pz = px*px + py*py;
0138        hpx->Fill(px);
0139        hpxpy->Fill(px,py);
0140        hprof->Fill(px,pz);
0141        
0142        if ((ii % 1000) == 0) 
0143      { // updates all objects in db 
0144        rs = stmt->ExecuteQuery("select name, histos from "
0145                    + tableName);
0146          
0147        if (!rs) break; // failed to select 
0148        
0149        while(rs->Next()) 
0150          {
0151            str = rs->GetString(1);
0152            if(str==hpx->GetName()) rs->UpdateObject(2,hpx);
0153            else if(str==hpxpy->GetName()) rs->UpdateObject(2,hpxpy);
0154            else if(str==hprof->GetName()) rs->UpdateObject(2,hprof);
0155            rs->UpdateRow();
0156          } 
0157        con->Commit();
0158        delete rs;        
0159      }       
0160        ii++;
0161      }
0162    delete stmt;
0163 }
0164       
0165 //__________________________________________________________________
0166 Int_t oproducer( const Text_t* dsn, 
0167                  const Text_t* usr, 
0168                  const Text_t* pwd )
0169 {
0170    // 
0171    
0172    TSQLConnection* con;
0173    TString str;
0174   
0175    // set error handler
0176    TSQL::SetHandler("Catch(TSQLException*)");
0177   
0178    str = "dsn="; str += dsn; 
0179    str += "; uid="; str += usr; 
0180    
0181    // con = gSQLDriverManager->GetConnection(dsn,usr,pwd);
0182    con = gSQLDriverManager->GetConnection(str);
0183 
0184    gSystem->Sleep(5000);
0185    
0186    if(!con) return -1;  // failed to connect
0187 
0188    if( con->GetMetaData()->SupportsTransactions() ) {
0189       con->SetAutoCommit(kFALSE);
0190    }
0191 
0192    TSQL::UnsetHandler(); // ignore errors
0193    //  DropStuff(con);
0194    TSQL::SetHandler("Catch(TSQLException*)");
0195    CreateStuff(con);
0196    // Fill(con);        // endless loop here
0197    TSQL::UnsetHandler(); // ignore errors 
0198    // DropStuff(con);
0199    con->Commit();
0200    con->Close();
0201    return 0;
0202 }
0203 
0204 //___________________________________________________________________
0205 void Catch(TSQLException* e)
0206 {
0207    // handle exceptions
0208    
0209    TString str = e->GetMessage(); 
0210    printf("%s\n",str.Data());
0211 }