Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RDBCp.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RDBCp.C
1 // $Id: RDBCp.C,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
2 //
3 // This file is part of the RDBC
4 // Author: Valeriy Onuchin <onuchin@sirius.ihep.su>
6 //
7 // Histogram producer script. This script stores three histogram objects
8 // in a datbase (a TH1F, a TH2F and a TProfile). It then fills, in an
9 // infinite loop (so use ctrl-c to stop this script), the three histogram
10 // objects with random numbers. Every 1000 fills the objects are updated
11 // in database.
12 //
13 // Use the oconsumer.C script to map this file and display the histograms.
14 //
15 //
16 // REQUIREMENTS
17 //
18 // You must have create table/drop table rights.
19 //
21 //
22 // Usage:
23 //
24 // root[] gSystem->Load("libRDBC.so"); // load library
25 // root[] .L RDBCproducer.C // load macro
26 // root[] RDBCproducer(dsn,uid,pwd); // execute the function from macro
27 //
28 
29 #ifndef __CINT__
30 // g++ -c -W RDBCproducer.C -I$ROOTSYS/include
31 
32 #include <TError.h>
33 #include <TH1.h>
34 #include <TH2.h>
35 #include <TProfile.h>
36 #include <TRandom.h>
37 #include <TROOT.h>
38 #include <TSQLDriverManager.h>
39 #include <TSQLConnection.h>
40 #include <TSQLResultSet.h>
41 #include <TSQLResultSetMetaData.h>
42 #include <TSQLPreparedStatement.h>
43 
44 #endif // __CINT__
45 
46 const TString tableName = "object_test";
47 
48 // our histos
49 TH1F* hpx;
50 TH2F* hpxpy;
51 TProfile* hprof;
52 
53 //__________________________________________________________________
55 {
56  // Creates the database objects needed
57 
58  // create our table
59  TSQLStatement* stmt = con->CreateStatement();
60 
61  stmt->ExecuteUpdate( "create table " +tableName+ "("
62  "name varchar(22) not null, histos lo not null)" );
63 
64  printf("Table %s created.\n",tableName.Data());
65  delete stmt;
66 
68  "insert into " +tableName+
69  "(name,histos) values(?,?)" );
70 
71  hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
72  hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
73  hprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
74 
75  // Set a fill color for the TH1F
76  hpx->SetFillColor(48);
77 
78  pstmt->SetString(1,"hpx");
79  pstmt->SetObject(2,hpx);
80  pstmt->ExecuteUpdate();
81 
82  pstmt->SetString(1,"hpxpy");
83  pstmt->SetObject(2,hpxpy);
84  pstmt->ExecuteUpdate();
85 
86  pstmt->SetString(1,"hprof");
87  pstmt->SetObject(2,hprof);
88  pstmt->ExecuteUpdate();
89 
90  con->Commit();
91  delete pstmt;
92 }
93 
94 //__________________________________________________________________
96 {
97  // Drops the database objects.
98 
99  TSQLStatement* stmt = con->CreateStatement();
100  stmt->ExecuteUpdate("drop table " + tableName);
101  printf("Dropped table %s\n",tableName.Data());
102  delete stmt;
103 }
104 
105 //__________________________________________________________________
107 {
108  // Endless loop filling histograms with random numbers
109 
110  TString str;
111 
112  TSQLStatement* stmt =
113  con->CreateStatement();// kTYPE_FORWARD_ONLY, kCONCUR_UPDATABLE);
114  if (!stmt) return;
115 
116  TSQLResultSet* rs;
117 
118  Float_t px, py, pz;
119  int ii = 0;
120 
121  // Endless loop filling histograms with random numbers
122  while (1)
123  {
124  gRandom->Rannor(px,py);
125  pz = px*px + py*py;
126  hpx->Fill(px);
127  hpxpy->Fill(px,py);
128  hprof->Fill(px,pz);
129 
130  if ((ii % 1000) == 0)
131  { // updates all objects in db
132  rs = stmt->ExecuteQuery("select name, histos from "
133  + tableName);
134 
135  if (!rs) break; // failed to select
136 
137  while(rs->Next())
138  {
139  str = rs->GetString(1);
140  if(str==hpx->GetName()) rs->UpdateObject(2,hpx);
141  else if(str==hpxpy->GetName()) rs->UpdateObject(2,hpxpy);
142  else if(str==hprof->GetName()) rs->UpdateObject(2,hprof);
143  rs->UpdateRow();
144  }
145  con->Commit();
146  delete rs;
147  }
148  ii++;
149  }
150  delete stmt;
151 }
152 
153 //__________________________________________________________________
154 Int_t oproducer( const Text_t* dsn,
155  const Text_t* usr,
156  const Text_t* pwd )
157 {
158  //
159 
161  TString str;
162 
163  // set error handler
164  TSQL::SetHandler("Catch(TSQLException*)");
165 
166  str = "dsn="; str += dsn;
167  str += "; uid="; str += usr;
168 
169  // con = gSQLDriverManager->GetConnection(dsn,usr,pwd);
170  con = gSQLDriverManager->GetConnection(str);
171 
172  if(!con) return -1; // failed to connect
173 
174  printf("\t\t\t DONE.\n");
175 
176  if( con->GetMetaData()->SupportsTransactions() ) {
177  con->SetAutoCommit(kFALSE);
178  }
179 
180  TSQL::UnsetHandler(); // ignore errors
181  // DropStuff(con);
182  TSQL::SetHandler("Catch(TSQLException*)");
183  CreateStuff(con);
184  // Fill(con); // endless loop here
185  TSQL::UnsetHandler(); // ignore errors
186  // DropStuff(con);
187  con->Commit();
188  con->Close();
189  return 0;
190 }
191 
192 //___________________________________________________________________
194 {
195  // handle exceptions
196 
197  TString str = e->GetMessage();
198  printf("%s\n",str.Data());
199 }