Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RDBCemp.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RDBCemp.C
1 // $Id: RDBCemp.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 // RDBCemp - Demo macro
8 //
9 // DESCRIPTION
10 //
11 // This is a sample macro to demostrate how to retrieve data
12 // from Oracle database and draw simple graphs.
13 //
14 // REQUIREMENTS
15 //
16 // o ORACLE demo table EMP. To create this demo table for MySQL run:
17 //
18 // $ mysql test -u username -p < demobld.mysql
19 //
21 //
22 // usage:
23 //
24 // root[] gSystem->Load("libRDBC.so"); // load library
25 // root[] .L RDBCemp.C // load macro
26 // root[] RDBCemp(dsn,[usr],[pwd]); // execute the function from macro
27 //
28 //Begin_Html
29 /*
30 <img src="oemp.gif">
31 */
32 //End_Html
33 
34 #ifndef __CINT__
35 #include <TError.h>
36 #include <TString.h>
37 #include <RDBC/TSQLDriverManager.h>
38 #include <RDBC/TSQLConnection.h>
40 #include <RDBC/TSQLResultSet.h>
44 #include <RDBC/TSQLTypes.h>
45 #include <TROOT.h>
46 #include <TGraph.h>
47 #include <TCanvas.h>
48 #include <TH1.h>
49 #include <TText.h>
50 #include <TStyle.h>
51 #include <TInterpreter.h>
52 #include <stdlib.h>
53 #endif
54 
55 //___________________________________________________________________
56 void DrawGraph(TGraph* graph, TString* labels, const TString& xname,
57  Float_t xmin,Float_t xmax,Float_t ymin,Float_t ymax)
58 {
59  //
60 
61  gPad->SetFrameFillColor(18);
62  gStyle->SetOptStat(0);
63  Int_t nrDivisions = graph->GetN();
64 
65  TH1F *frame = new TH1F(graph->GetName(),graph->GetTitle(),2,xmin,xmax+1);
66  frame->SetMinimum(ymin);
67  frame->SetMaximum(ymax);
68  frame->SetLabelOffset(10);
69  frame->SetLabelSize(0.0);
70  frame->SetNdivisions(0);
71  frame->SetTitleOffset(1.1,"X");
72  frame->SetXTitle((char*)xname.Data());
73  frame->SetTitleOffset(1.1,"Y");
74  frame->Draw();
75 
76  TText *t = new TText();
77  t->SetTextAlign(22);
78  t->SetTextSize(0.022);
79 
80  Float_t span = xmax-xmin;
81  Float_t step = Float_t(span/nrDivisions);
82  Float_t dist = TMath::Abs(ymax-ymin);
83 
84  for (Int_t j=0; j<nrDivisions;j++) {
85  Float_t xpos = step+xmin+j*step;
86  Float_t ypos = ymin-0.05*dist;
87  t->DrawText(xpos,ypos,labels[j].Data());
88  }
89 
90 //-- Finally plot the graph
91  graph->SetFillColor(rand()%149);
92  graph->SetLineWidth(1.0);
93  graph->Draw("B");
94 }
95 
96 //___________________________________________________________________
97 Int_t RDBCemp( const Text_t* dsn,
98  const Text_t* usr="scott",
99  const Text_t* pwd="tiger" )
100 {
101  //
102 
103  TString str;
104 
105  // set error handler
106  TSQL::SetHandler("Catch(TSQLException*)");
107 
108  str = "Connecting to dsn="; str += dsn;
109  str += ", uid="; str += usr;
110  str += ", pwd="; str += pwd;
111  str += " ...";
112  printf("%s\n",str.Data());
113 
114  TSQLConnection* gConn = gSQLDriverManager->GetConnection(dsn,usr,pwd);
115  if(!gConn) return -1;
116 
117  printf("\t\t\t DONE.\n");
118 
119  TSQLStatement* stmt = gConn->CreateStatement();
120  TSQLResultSet* rs = stmt->ExecuteQuery("select * from EMP");
121  if(!rs) return -1;
122 
123  const Int_t nrows = 14;
124  Float_t sal[nrows];
125  Float_t empno[nrows];
126  Float_t mgr[nrows];
127  Float_t comm[nrows];
128  Float_t row[nrows];
129  TString ename[nrows];
130 
131  for(int i=0; i<nrows; i++) {
132  rs->Next(); // iterate rows
133  empno[i] = rs->GetInt(1); // empno
134  ename[i] = rs->GetString(2); // ename
135  mgr[i] = rs->GetFloat(4); // mgr
136  sal[i] = rs->GetFloat(6); // sal
137  comm[i] = rs->GetFloat(7); // comm
138  row[i] = rs->GetRow();
139  }
140 
141  TGraph* gr; //
142  TCanvas* c = new TCanvas("EMP","EMP table",900,600);
143  c->Divide(2,2);
144 
145  Float_t xmin = 0;
146  Float_t xmax = nrows;
147  Float_t ymin;
148  Float_t ymax;
149  Int_t ind;
150 
151  c->cd(1);
152  gPad->SetFillColor(38);
153  gPad->SetToolTipText("This is EMPNO distribution");
154  ind = TMath::LocMin(nrows,empno);
155  ymin = 7000; //empno[ind];
156  ind = TMath::LocMax(nrows,empno);
157  ymax = empno[ind]*1.05;
158  gr = new TGraph(nrows,row,empno);
159  gr->SetName("empno");
160  gr->SetTitle("empno");
161  DrawGraph(gr,ename,"ename",xmin,xmax,ymin,ymax);
162 
163  c->cd(2);
164  gPad->SetFillColor(30);
165  gPad->SetToolTipText("This is SAL distribution");
166  ind = TMath::LocMin(nrows,sal);
167  ymin = sal[ind]*0.8;
168  ind = TMath::LocMax(nrows,sal);
169  ymax = sal[ind]*1.05;
170  gr = new TGraph(nrows,row,sal);
171  gr->SetName("sal");
172  gr->SetTitle("sal");
173  DrawGraph(gr,ename,"sal",xmin,xmax,ymin,ymax);
174 
175  c->cd(3);
176  gPad->SetFillColor(47);
177  gPad->SetToolTipText("This is MGR distribution");
178  ind = TMath::LocMin(nrows,mgr);
179  ymin = mgr[ind]*0.9;
180  ymin = 7000;
181  ind = TMath::LocMax(nrows,mgr);
182  ymax = mgr[ind]*1.05;
183  gr = new TGraph(nrows,row,mgr);
184  gr->SetName("mgr");
185  gr->SetTitle("mgr");
186  DrawGraph(gr,ename,"ename",xmin,xmax,ymin,ymax);
187 
188  c->cd(4);
189  gPad->SetFillColor(42);
190  gPad->SetToolTipText("This is COMM distribution");
191  ind = TMath::LocMin(nrows,comm);
192  ymin = comm[ind]*0.9;
193  ind = TMath::LocMax(nrows,comm);
194  ymax = comm[ind]*1.05;
195  gr = new TGraph(nrows,row,comm);
196  gr->SetName("comm");
197  gr->SetTitle("comm");
198  DrawGraph(gr,ename,"ename",xmin,xmax,ymin,ymax);
199 // gConn->Close();
200  return 0;
201 }
202 
203 //___________________________________________________________________
205 {
206  // handle exceptions
207 
208  TString str = e->GetMessage();
209  printf("SQL Error: %s\n",str.Data());
210 }
211 
213 #ifdef STANDALONE
214 
215 #include <TROOT.h>
216 #include <TSystem.h>
217 #include <iostream>
218 #include <TApplication.h>
219 
220 //---- Main program ------------------------------------------------------------
221 
222 TROOT root("RDBCemp", "EMP table graphs");
223 
224 int main(int argc, char **argv)
225 {
226  if(argc!=3 && argc!=4) {
227  cerr << "Usage: " << argv[0] << " url username" << endl
228  << "or " << argv[0] << " url username password" << endl;
229  return 0;
230  }
231 
232  TApplication theApp("App", &argc, argv);
233 
234  if (gROOT->IsBatch()) {
235  cerr << argv[0] << "cannot run in batch mode" << endl;
236  return 1;
237  }
238 
239  gSystem->Load("libRDBC");
240  Int_t ret;
241 
242  if(argc==3) ret= RDBCemp(argv[1],argv[2],"");
243  else ret = RDBCemp(argv[1],argv[2],argv[3]);
244 
245  if(ret) return ret; // failure
246 
247  theApp.Run();
248  return 0;
249 }
250 #endif