Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RDBCproducer.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RDBCproducer.C
1 // $Id: RDBCproducer.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( "delete from " + tableName);
62  delete stmt;
63 
65  "insert into " +tableName+
66  "(name,histos) values(?,?)" );
67 
68  hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
69  hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
70  hprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
71 
72  // Set a fill color for the TH1F
73  hpx->SetFillColor(48);
74 
75  Float_t px, py, pz;
76  int ii = 0;
77 
78  // Endless loop filling histograms with random numbers
79  for (ii = 0; ii < 1000; ii++)
80  {
81  gRandom->Rannor(px,py);
82  pz = px*px + py*py;
83  hpx->Fill(px);
84  hpxpy->Fill(px,py);
85  hprof->Fill(px,pz);
86  }
87 
88  pstmt->SetString(1,"hpx");
89  pstmt->SetObject(2,hpx);
90  pstmt->ExecuteUpdate();
91 
92  pstmt->SetString(1,"hpxpy");
93  pstmt->SetObject(2,hpxpy);
94  pstmt->ExecuteUpdate();
95 
96  pstmt->SetString(1,"hprof");
97  pstmt->SetObject(2,hprof);
98  pstmt->ExecuteUpdate();
99 
100  con->Commit();
101  delete pstmt;
102 }
103 
104 //__________________________________________________________________
106 {
107  // Drops the database objects.
108 
109  TSQLStatement* stmt = con->CreateStatement();
110  stmt->ExecuteUpdate("drop table " + tableName);
111  printf("Dropped table %s\n",tableName.Data());
112  delete stmt;
113 }
114 
115 //__________________________________________________________________
117 {
118  // Endless loop filling histograms with random numbers
119 
120  TString str;
121 
122  TSQLStatement* stmt =
124  if (!stmt) return;
125 
126  con->Print("a");
127 
128  TSQLResultSet* rs;
129 
130  Float_t px, py, pz;
131  int ii = 0;
132 
133  // Endless loop filling histograms with random numbers
134  while (1)
135  {
136  gRandom->Rannor(px,py);
137  pz = px*px + py*py;
138  hpx->Fill(px);
139  hpxpy->Fill(px,py);
140  hprof->Fill(px,pz);
141 
142  if ((ii % 1000) == 0)
143  { // updates all objects in db
144  rs = stmt->ExecuteQuery("select name, histos from "
145  + tableName);
146 
147  if (!rs) break; // failed to select
148 
149  while(rs->Next())
150  {
151  str = rs->GetString(1);
152  if(str==hpx->GetName()) rs->UpdateObject(2,hpx);
153  else if(str==hpxpy->GetName()) rs->UpdateObject(2,hpxpy);
154  else if(str==hprof->GetName()) rs->UpdateObject(2,hprof);
155  rs->UpdateRow();
156  }
157  con->Commit();
158  delete rs;
159  }
160  ii++;
161  }
162  delete stmt;
163 }
164 
165 //__________________________________________________________________
166 Int_t oproducer( const Text_t* dsn,
167  const Text_t* usr,
168  const Text_t* pwd )
169 {
170  //
171 
173  TString str;
174 
175  // set error handler
176  TSQL::SetHandler("Catch(TSQLException*)");
177 
178  str = "dsn="; str += dsn;
179  str += "; uid="; str += usr;
180 
181  // con = gSQLDriverManager->GetConnection(dsn,usr,pwd);
182  con = gSQLDriverManager->GetConnection(str);
183 
184  gSystem->Sleep(5000);
185 
186  if(!con) return -1; // failed to connect
187 
188  if( con->GetMetaData()->SupportsTransactions() ) {
189  con->SetAutoCommit(kFALSE);
190  }
191 
192  TSQL::UnsetHandler(); // ignore errors
193  // DropStuff(con);
194  TSQL::SetHandler("Catch(TSQLException*)");
195  CreateStuff(con);
196  // Fill(con); // endless loop here
197  TSQL::UnsetHandler(); // ignore errors
198  // DropStuff(con);
199  con->Commit();
200  con->Close();
201  return 0;
202 }
203 
204 //___________________________________________________________________
206 {
207  // handle exceptions
208 
209  TString str = e->GetMessage();
210  printf("%s\n",str.Data());
211 }