Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
sqlcanvas.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_sql
3 /// This is slight modification of ntuple1.C example.
4 /// A canvas with sub-pads is produced, stored to an sql data base and read back
5 /// To run this macro, you need the hsimple.root file, produced by the hsimple.C macro
6 ///
7 /// \macro_code
8 ///
9 /// \author Sergey Linev
10 
11 // example configuration for MySQL 4.1
12 const char* dbname = "mysql://host.domain/test";
13 const char* username = "user";
14 const char* userpass = "pass";
15 
16 // example configuration for Oracle 9i
17 //const char* dbname = "oracle://host.domain/db-test";
18 //const char* username = "user";
19 //const char* userpass = "pass";
20 
21 void sqlcanvas()
22 {
23  canvas_write();
24  canvas_read();
25 }
26 
27 void canvas_write()
28 {
29  //just in case this script is executed multiple times
30  delete gROOT->GetListOfFiles()->FindObject("hsimple.root");
31  delete gROOT->GetListOfCanvases()->FindObject("c1");
32 
33  gBenchmark->Start("ntuple1");
34  //
35  // Connect ROOT histogram/ntuple demonstration file
36  // generated by example hsimple.C.
37  TFile *f1 = new TFile("hsimple.root");
38  //
39  // Create a canvas, with 4 pads
40  //
41  TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
42  TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.48,0.98,21);
43  TPad *pad2 = new TPad("pad2","This is pad2",0.52,0.52,0.98,0.98,21);
44  TPad *pad3 = new TPad("pad3","This is pad3",0.02,0.02,0.48,0.48,21);
45  TPad *pad4 = new TPad("pad4","This is pad4",0.52,0.02,0.98,0.48,1);
46  pad1->Draw();
47  pad2->Draw();
48  pad3->Draw();
49  pad4->Draw();
50  //
51  // Change default style for the statistics box
52  gStyle->SetStatW(0.30);
53  gStyle->SetStatH(0.20);
54  gStyle->SetStatColor(42);
55  //
56  // Display a function of one ntuple column imposing a condition
57  // on another column.
58  pad1->cd();
59  pad1->SetGrid();
60  pad1->SetLogy();
61  pad1->GetFrame()->SetFillColor(15);
62  TNtuple *ntuple = (TNtuple*)f1->Get("ntuple");
63  ntuple->SetLineColor(1);
64  ntuple->SetFillStyle(1001);
65  ntuple->SetFillColor(45);
66  ntuple->Draw("3*px+2","px**2+py**2>1");
67  ntuple->SetFillColor(38);
68  ntuple->Draw("2*px+2","pz>2","same");
69  ntuple->SetFillColor(5);
70  ntuple->Draw("1.3*px+2","(px^2+py^2>4) && py>0","same");
71  pad1->RedrawAxis();
72  //
73  // Display the profile of two columns
74  // The profile histogram produced is saved in the current directory with
75  // the name hprofs
76  pad2->cd();
77  pad2->SetGrid();
78  pad2->GetFrame()->SetFillColor(32);
79  ntuple->Draw("pz:px>>hprofs","","goffprofs");
80  TProfile *hprofs = (TProfile*)gDirectory->Get("hprofs");
81  hprofs->SetMarkerColor(5);
82  hprofs->SetMarkerSize(0.7);
83  hprofs->SetMarkerStyle(21);
84  hprofs->Fit("pol2");
85  // Get pointer to fitted function and modify its attributes
86  TF1 *fpol2 = hprofs->GetFunction("pol2");
87  fpol2->SetLineWidth(4);
88  fpol2->SetLineColor(2);
89  //
90  // Display a scatter plot of two columns with a selection.
91  // Superimpose the result of another cut with a different marker color
92  pad3->cd();
93  pad3->GetFrame()->SetFillColor(38);
94  pad3->GetFrame()->SetBorderSize(8);
95  ntuple->SetMarkerColor(1);
96  ntuple->Draw("py:px","pz>1");
97  ntuple->SetMarkerColor(2);
98  ntuple->Draw("py:px","pz<1","same");
99  //
100  // Display a 3-D scatter plot of 3 columns. Superimpose a different selection.
101  pad4->cd();
102  ntuple->Draw("pz:py:px","(pz<10 && pz>6)+(pz<4 && pz>3)");
103  ntuple->SetMarkerColor(4);
104  ntuple->Draw("pz:py:px","pz<6 && pz>4","same");
105  ntuple->SetMarkerColor(5);
106  ntuple->Draw("pz:py:px","pz<4 && pz>3","same");
107  TPaveText *l4 = new TPaveText(-0.9,0.5,0.9,0.95);
108  l4->SetFillColor(42);
109  l4->SetTextAlign(12);
110  l4->AddText("You can interactively rotate this view in 2 ways:");
111  l4->AddText(" - With the RotateCube in clicking in this pad");
112  l4->AddText(" - Selecting View with x3d in the View menu");
113  l4->Draw();
114  //
115  c1->cd();
116  c1->Update();
117  gStyle->SetStatColor(19);
118  gBenchmark->Show("ntuple1");
119 
120  TSQLFile* fsql1 = new TSQLFile(dbname, "recreate", username, userpass);
121  if (fsql1->IsZombie()) { delete fsql1; return; }
122 
123 // changing TSQLFile configuration, you may improve speed
124 // of reading or writing object to/from sql database
125 
126 // fsql1->SetUseSuffixes(kFALSE);
127 // fsql1->SetArrayLimit(1000);
128 // fsql1->SetUseIndexes(1);
129 // fsql1->SetTablesType("ISAM");
130 // fsql1->SetUseTransactions(kFALSE);
131 
132 
133  // Unncomment this line to see all SQL commands in log file
134  // fsql1->StartLogFile("canvas.log");
135 
136  gBenchmark->Start("writeSQL");
137  c1->Write("Canvas");
138  gBenchmark->Show("writeSQL");
139  delete fsql1;
140 }
141 
142 void canvas_read()
143 {
144  TFile* f2 = new TSQLFile(dbname, "open", username, userpass);
145  if (f2->IsZombie()) { delete f2; return; }
146 
147  f2->ls();
148  gBenchmark->Start("readSQL");
149  TCanvas* cc = (TCanvas*) f2->Get("Canvas");
150  gBenchmark->Show("readSQL");
151  if (cc!=0) cc->Draw();
152 
153  delete f2;
154 }
155