Logo ROOT   6.07/09
Reference Guide
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 
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:830
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:49
virtual void ls(Option_t *option="") const
List file contents.
Definition: TFile.cxx:1367
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:211
void SetStatColor(Color_t color=19)
Definition: TStyle.h:379
void SetStatH(Float_t h=0.1)
Definition: TStyle.h:389
return c1
Definition: legend1.C:41
R__EXTERN TStyle * gStyle
Definition: TStyle.h:418
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
Definition: TPaveText.cxx:160
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
#define gROOT
Definition: TROOT.h:364
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:157
Bool_t IsZombie() const
Definition: TObject.h:127
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:44
Profile Historam.
Definition: TProfile.h:34
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:526
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:174
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2746
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:318
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:43
virtual void RedrawAxis(Option_t *option="")
Redraw the frame axis Redrawing axis may be necessary in case of superimposed histograms when one or ...
Definition: TPad.cxx:4736
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:54
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1208
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition: TAttText.h:47
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:46
Access an SQL db via the TFile interface.
Definition: TSQLFile.h:32
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:30
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:42
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
The most important graphics class in the ROOT system.
Definition: TPad.h:37
void SetStatW(Float_t w=0.19)
Definition: TStyle.h:388
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:45
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition: TAttMarker.h:46
The Canvas class.
Definition: TCanvas.h:41
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:27
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:366
virtual void Draw(Option_t *option="")
Draw a canvas.
Definition: TCanvas.cxx:795
double f2(const double *x)
1-Dim function class
Definition: TF1.h:149
TF1 * f1
Definition: legend1.C:11
#define gDirectory
Definition: TDirectory.h:221
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2183
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition: TH1.cxx:3565
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TH1.cxx:8118
virtual void SetLogy(Int_t value=1)
Set Lin/Log scale for Y.
Definition: TPad.cxx:5347