Logo ROOT  
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
12const char* dbname = "mysql://host.domain/test";
13const char* username = "user";
14const 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
21void sqlcanvas()
22{
23 canvas_write();
24 canvas_read();
25}
26
27void 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);
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
142void 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
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
#define gDirectory
Definition: TDirectory.h:384
#define gROOT
Definition: TROOT.h:405
R__EXTERN TStyle * gStyle
Definition: TStyle.h:414
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:39
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:38
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition: TAttMarker.h:45
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition: TAttText.h:42
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
The Canvas class.
Definition: TCanvas.h:23
void Draw(Option_t *option="") override
Draw a canvas.
Definition: TCanvas.cxx:850
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
1-Dim function class
Definition: TF1.h:213
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
void ls(Option_t *option="") const override
List file contents.
Definition: TFile.cxx:1423
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:3894
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TH1.cxx:8917
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:28
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:153
The most important graphics class in the ROOT system.
Definition: TPad.h:28
void SetGrid(Int_t valuex=1, Int_t valuey=1) override
Definition: TPad.h:330
void SetLogy(Int_t value=1) override
Set Lin/Log scale for Y.
Definition: TPad.cxx:5934
void RedrawAxis(Option_t *option="") override
Redraw the frame axis.
Definition: TPad.cxx:5297
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition: TPad.cxx:597
TFrame * GetFrame() override
Get frame.
Definition: TPad.cxx:2831
void Draw(Option_t *option="") override
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1268
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:21
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:189
void Draw(Option_t *option="") override
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:240
Profile Histogram.
Definition: TProfile.h:32
Access an SQL db via the TFile interface.
Definition: TSQLFile.h:30
void SetStatH(Float_t h=0.1)
Definition: TStyle.h:384
void SetStatColor(Color_t color=19)
Definition: TStyle.h:374
void SetStatW(Float_t w=0.19)
Definition: TStyle.h:383
void Draw(Option_t *opt) override
Default Draw method for all objects.
Definition: TTree.h:428
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:52
return c1
Definition: legend1.C:41
TF1 * f1
Definition: legend1.C:11