Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RDBCconsumer.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RDBCconsumer.C
1 // $Id: RDBCconsumer.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 consumer script. Create a canvas and 3 pads. Connect
8 // to database table, that was created by RDBCproducer.C.
9 // It reads the histograms from database and displays them
10 // in the pads.
11 //
13 //
14 // 1. Run another root session on another window(or computer)
15 // 2. Start the RDBCproducer.C script over there
16 // 3. Start RDBCconsumer script:
17 //
18 // root[] gSystem->Load("libRDBC.so"); // load library
19 // root[] .L RDBCconsumer.C // load macro
20 // root[] RDBCconsumer(dsn,uid,pwd);
21 //
22 //Begin_Html
23 /*
24 <img src="oconsumer.gif">
25 */
26 //End_Html
27 #ifndef __CINT__
28 // g++ -c -W RDBCconsumer.C -I$ROOTSYS/include
29 
30 #include <TError.h>
31 #include <TH1.h>
32 #include <TH2.h>
33 #include <TProfile.h>
34 #include <TCanvas.h>
35 #include <TSystem.h>
36 #include <TROOT.h>
37 
38 #include <RDBC/TSQLDriverManager.h>
39 #include <RDBC/TSQLConnection.h>
40 #include <RDBC/TSQLResultSet.h>
43 
44 #endif // __CINT__
45 
46 const TString tableName = "object_test";
47 
48 // our histos
49 TH1F* hpx=0;
50 TH2F* hpxpy=0;
51 TProfile* hprof=0;
52 
53 //__________________________________________________________________
55 {
56  // Drops the database objects.
57 
58  TSQLStatement* stmt = con->CreateStatement();
59  stmt->ExecuteUpdate("drop table " + tableName);
60  printf("Dropped table %s\n",tableName.Data());
61  delete stmt;
62 }
63 
64 //__________________________________________________________________
65 Int_t RDBCconsumer( const Text_t* dsn,
66  const Text_t* usr,
67  const Text_t* pwd )
68 {
69  // - connects to database
70  // - if table with histos exists ( implies that oproducer.C
71  // script is running on another window/computer )
72  // periodically reads them out to display
73 
74  TObject *to;
76  TString str;
77 
78  // set error handler
79  TSQL::SetHandler("Catch(TSQLException*)");
80 
81  str = "dsn="; str += dsn;
82  str += "; uid="; str += usr;
83 
84  // con = gSQLDriverManager->GetConnection(dsn,usr,pwd);
85  con = gSQLDriverManager->GetConnection(str);
86 
87  if(!con) return -1; // failed to connect
88 
89  printf("\t\t\t DONE.\n");
90 
91  TCanvas *c1;
92  TPad *pad1, *pad2, *pad3;
93 
94  c1 = new TCanvas("c1","Reading objects from Database Example",200,10,700,780);
95  pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.98,0.98,21);
96  pad2 = new TPad("pad2","This is pad2",0.02,0.02,0.48,0.48,21);
97  pad3 = new TPad("pad3","This is pad3",0.52,0.02,0.98,0.48,21);
98  pad1->Draw();
99  pad2->Draw();
100  pad3->Draw();
101 
104 
105 loop: // wait while oproducer.C script is not started
106  TSQLResultSet* rs;
107  rs = stmt->ExecuteQuery("select name, histos from " + tableName);
108 
109  if (!rs) {
110  for(int i=0; i<50; i++) {
111  gSystem->Sleep(100); // sleep for 0.1 seconds
112  gSystem->ProcessEvents();
113  }
114  printf("\n\n\t\t** producer not connected yet **\n\n");
115  goto loop;
116  }
117 
118  // Loop displaying the histograms. Once the producer stops this
119  // script will break out of the loop.
120  Double_t oldentries = 0;
121  for (int j = 0; j < 100; j++)
122  {
123  if(hpx) delete hpx;
124  if(hpxpy) delete hpxpy;
125  if(hprof) delete hprof;
126 
127  rs = stmt->ExecuteQuery("select name, histos from " + tableName);
128  if (!rs) break;
129  // rs->BeforeFirst();
130  while (rs->Next())
131  {
132  str = rs->GetString(1);
133  cout << str << endl;
134  to = rs->GetObject(2);
135  if(str=="hpx") hpx = (TH1F *)to;
136  else if(str=="hpxpy" ) hpxpy = (TH2F *)to;
137  else if(str=="hprof") hprof = (TProfile *)to;
138  }
139  if (hpx->GetEntries() == oldentries) break;
140  oldentries = hpx->GetEntries();
141  pad1->cd();
142  if (hpx) hpx->Draw();
143  pad2->cd();
144  if (hprof) hprof->Draw();
145  pad3->cd();
146  if (hpxpy) hpxpy->Draw("cont");
147  c1->Modified();
148  c1->Update();
149 
150  gSystem->Sleep(100); // sleep for 0.1 seconds
151  // if (gSystem->ProcessEvents()) break;
152  delete rs;
153  }
154 
155  delete stmt;
156 
157  // DropStuff(con);
158  con->Commit();
159  con->Close();
160  return 0;
161 }
162 
163 //___________________________________________________________________
165 {
166  // handle exceptions
167 
168  TString str = e->GetMessage();
169  printf("%s\n",str.Data());
170 }