Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 // $Id: RDBCp.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( "create table " +tableName+ "("
0062      "name varchar(22) not null, histos lo not null)" );
0063   
0064    printf("Table %s created.\n",tableName.Data());
0065    delete stmt;
0066    
0067    TSQLPreparedStatement* pstmt = con->PrepareStatement( 
0068                                        "insert into " +tableName+ 
0069                                        "(name,histos) values(?,?)" );
0070    
0071    hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
0072    hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
0073    hprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
0074    
0075    // Set a fill color for the TH1F
0076    hpx->SetFillColor(48);
0077    
0078    pstmt->SetString(1,"hpx");
0079    pstmt->SetObject(2,hpx);
0080    pstmt->ExecuteUpdate();
0081    
0082    pstmt->SetString(1,"hpxpy");
0083    pstmt->SetObject(2,hpxpy);
0084    pstmt->ExecuteUpdate();
0085    
0086    pstmt->SetString(1,"hprof");
0087    pstmt->SetObject(2,hprof);
0088    pstmt->ExecuteUpdate();
0089    
0090    con->Commit();
0091    delete pstmt;
0092 }
0093 
0094 //__________________________________________________________________
0095 void DropStuff(TSQLConnection* con)
0096 {
0097    // Drops the database objects.
0098    
0099    TSQLStatement* stmt = con->CreateStatement();
0100    stmt->ExecuteUpdate("drop table " + tableName);
0101    printf("Dropped table %s\n",tableName.Data());
0102    delete stmt;
0103 }
0104 
0105 //__________________________________________________________________
0106 void Fill(TSQLConnection* con)
0107 {
0108    // Endless loop filling histograms with random numbers
0109    
0110    TString str;
0111 
0112    TSQLStatement* stmt = 
0113      con->CreateStatement();// kTYPE_FORWARD_ONLY, kCONCUR_UPDATABLE);
0114    if (!stmt) return;
0115       
0116    TSQLResultSet* rs;
0117          
0118    Float_t px, py, pz;
0119    int ii = 0;
0120    
0121    // Endless loop filling histograms with random numbers
0122    while (1) 
0123      {
0124        gRandom->Rannor(px,py);
0125        pz = px*px + py*py;
0126        hpx->Fill(px);
0127        hpxpy->Fill(px,py);
0128        hprof->Fill(px,pz);
0129        
0130        if ((ii % 1000) == 0) 
0131      { // updates all objects in db 
0132        rs = stmt->ExecuteQuery("select name, histos from "
0133                    + tableName);
0134          
0135        if (!rs) break; // failed to select 
0136        
0137        while(rs->Next()) 
0138          {
0139            str = rs->GetString(1);
0140            if(str==hpx->GetName()) rs->UpdateObject(2,hpx);
0141            else if(str==hpxpy->GetName()) rs->UpdateObject(2,hpxpy);
0142            else if(str==hprof->GetName()) rs->UpdateObject(2,hprof);
0143            rs->UpdateRow();
0144          } 
0145        con->Commit();
0146        delete rs;        
0147      }       
0148        ii++;
0149      }
0150    delete stmt;
0151 }
0152       
0153 //__________________________________________________________________
0154 Int_t oproducer( const Text_t* dsn, 
0155                  const Text_t* usr, 
0156                  const Text_t* pwd )
0157 {
0158    // 
0159    
0160    TSQLConnection* con;
0161    TString str;
0162   
0163    // set error handler
0164    TSQL::SetHandler("Catch(TSQLException*)");
0165   
0166    str = "dsn="; str += dsn; 
0167    str += "; uid="; str += usr; 
0168    
0169    // con = gSQLDriverManager->GetConnection(dsn,usr,pwd);
0170    con = gSQLDriverManager->GetConnection(str);
0171    
0172    if(!con) return -1;  // failed to connect
0173 
0174    printf("\t\t\t DONE.\n");
0175    
0176    if( con->GetMetaData()->SupportsTransactions() ) {
0177       con->SetAutoCommit(kFALSE);
0178    }
0179 
0180    TSQL::UnsetHandler(); // ignore errors
0181    // DropStuff(con);
0182    TSQL::SetHandler("Catch(TSQLException*)");
0183    CreateStuff(con);
0184    // Fill(con);        // endless loop here
0185    TSQL::UnsetHandler(); // ignore errors 
0186    // DropStuff(con);
0187    con->Commit();
0188    con->Close();
0189    return 0;
0190 }
0191 
0192 //___________________________________________________________________
0193 void Catch(TSQLException* e)
0194 {
0195    // handle exceptions
0196    
0197    TString str = e->GetMessage(); 
0198    printf("%s\n",str.Data());
0199 }