ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
sqlcanvas.C
Go to the documentation of this file.
1 // This is slight modification of ntuple1.C example.
2 // A canvas with subpads is produced, stored to an sql data base and read back
3 // To run this macro, you need the hsimple.root file, produced by the hsimple.C macro
4 // Author: S.Linev
5 
6 // example configuration for MySQL 4.1
7 const char* dbname = "mysql://host.domain/test";
8 const char* username = "user";
9 const char* userpass = "pass";
10 
11 // example configuration for Oracle 9i
12 //const char* dbname = "oracle://host.domain/db-test";
13 //const char* username = "user";
14 //const char* userpass = "pass";
15 
16 void sqlcanvas()
17 {
18  canvas_write();
19  canvas_read();
20 }
21 
23 {
24  //just in case this script is executed multiple times
25  delete gROOT->GetListOfFiles()->FindObject("hsimple.root");
26  delete gROOT->GetListOfCanvases()->FindObject("c1");
27 
28  gBenchmark->Start("ntuple1");
29  //
30  // Connect ROOT histogram/ntuple demonstration file
31  // generated by example hsimple.C.
32  TFile *f1 = new TFile("hsimple.root");
33  //
34  // Create a canvas, with 4 pads
35  //
36  TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
37  TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.48,0.98,21);
38  TPad *pad2 = new TPad("pad2","This is pad2",0.52,0.52,0.98,0.98,21);
39  TPad *pad3 = new TPad("pad3","This is pad3",0.02,0.02,0.48,0.48,21);
40  TPad *pad4 = new TPad("pad4","This is pad4",0.52,0.02,0.98,0.48,1);
41  pad1->Draw();
42  pad2->Draw();
43  pad3->Draw();
44  pad4->Draw();
45  //
46  // Change default style for the statistics box
47  gStyle->SetStatW(0.30);
48  gStyle->SetStatH(0.20);
49  gStyle->SetStatColor(42);
50  //
51  // Display a function of one ntuple column imposing a condition
52  // on another column.
53  pad1->cd();
54  pad1->SetGrid();
55  pad1->SetLogy();
56  pad1->GetFrame()->SetFillColor(15);
57  TNtuple *ntuple = (TNtuple*)f1->Get("ntuple");
58  ntuple->SetLineColor(1);
59  ntuple->SetFillStyle(1001);
60  ntuple->SetFillColor(45);
61  ntuple->Draw("3*px+2","px**2+py**2>1");
62  ntuple->SetFillColor(38);
63  ntuple->Draw("2*px+2","pz>2","same");
64  ntuple->SetFillColor(5);
65  ntuple->Draw("1.3*px+2","(px^2+py^2>4) && py>0","same");
66  pad1->RedrawAxis();
67  //
68  // Display the profile of two columns
69  // The profile histogram produced is saved in the current directory with
70  // the name hprofs
71  pad2->cd();
72  pad2->SetGrid();
73  pad2->GetFrame()->SetFillColor(32);
74  ntuple->Draw("pz:px>>hprofs","","goffprofs");
75  TProfile *hprofs = (TProfile*)gDirectory->Get("hprofs");
76  hprofs->SetMarkerColor(5);
77  hprofs->SetMarkerSize(0.7);
78  hprofs->SetMarkerStyle(21);
79  hprofs->Fit("pol2");
80  // Get pointer to fitted function and modify its attributes
81  TF1 *fpol2 = hprofs->GetFunction("pol2");
82  fpol2->SetLineWidth(4);
83  fpol2->SetLineColor(2);
84  //
85  // Display a scatter plot of two columns with a selection.
86  // Superimpose the result of another cut with a different marker color
87  pad3->cd();
88  pad3->GetFrame()->SetFillColor(38);
89  pad3->GetFrame()->SetBorderSize(8);
90  ntuple->SetMarkerColor(1);
91  ntuple->Draw("py:px","pz>1");
92  ntuple->SetMarkerColor(2);
93  ntuple->Draw("py:px","pz<1","same");
94  //
95  // Display a 3-D scatter plot of 3 columns. Superimpose a different selection.
96  pad4->cd();
97  ntuple->Draw("pz:py:px","(pz<10 && pz>6)+(pz<4 && pz>3)");
98  ntuple->SetMarkerColor(4);
99  ntuple->Draw("pz:py:px","pz<6 && pz>4","same");
100  ntuple->SetMarkerColor(5);
101  ntuple->Draw("pz:py:px","pz<4 && pz>3","same");
102  TPaveText *l4 = new TPaveText(-0.9,0.5,0.9,0.95);
103  l4->SetFillColor(42);
104  l4->SetTextAlign(12);
105  l4->AddText("You can interactively rotate this view in 2 ways:");
106  l4->AddText(" - With the RotateCube in clicking in this pad");
107  l4->AddText(" - Selecting View with x3d in the View menu");
108  l4->Draw();
109  //
110  c1->cd();
111  c1->Update();
112  gStyle->SetStatColor(19);
113  gBenchmark->Show("ntuple1");
114 
115  TSQLFile* fsql1 = new TSQLFile(dbname, "recreate", username, userpass);
116  if (fsql1->IsZombie()) { delete fsql1; return; }
117 
118 // changing TSQLFile configuration, you may improve speed
119 // of reading or writing object to/from sql database
120 
121 // fsql1->SetUseSuffixes(kFALSE);
122 // fsql1->SetArrayLimit(1000);
123 // fsql1->SetUseIndexes(1);
124 // fsql1->SetTablesType("ISAM");
125 // fsql1->SetUseTransactions(kFALSE);
126 
127 
128  // Unncomment this line to see all SQL commands in log file
129  // fsql1->StartLogFile("canvas.log");
130 
131  gBenchmark->Start("writeSQL");
132  c1->Write("Canvas");
133  gBenchmark->Show("writeSQL");
134  delete fsql1;
135 }
136 
138 {
139  TFile* f2 = new TSQLFile(dbname, "open", username, userpass);
140  if (f2->IsZombie()) { delete f2; return; }
141 
142  f2->ls();
143  gBenchmark->Start("readSQL");
144  TCanvas* cc = (TCanvas*) f2->Get("Canvas");
145  gBenchmark->Show("readSQL");
146  if (cc!=0) cc->Draw();
147 
148  delete f2;
149 }
150 
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:823
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
virtual void ls(Option_t *option="") const
List file contents.
Definition: TFile.cxx:1361
tuple ntuple
Definition: hsimple.py:39
const char * userpass
Definition: sqlcanvas.C:9
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:211
const char * dbname
Definition: sqlcanvas.C:7
void SetStatColor(Color_t color=19)
Definition: TStyle.h:384
void SetStatH(Float_t h=0.1)
Definition: TStyle.h:394
tuple l4
Definition: ntuple1.py:92
TCanvas * c1
Definition: legend1.C:2
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
tuple f2
Definition: surfaces.py:24
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
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
TPad * pad1
Definition: hcons.C:13
void sqlcanvas()
Definition: sqlcanvas.C:16
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
#define gROOT
Definition: TROOT.h:344
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
Bool_t IsZombie() const
Definition: TObject.h:141
virtual void SetFillStyle(Style_t fstyle)
Definition: TAttFill.h:52
TPad * pad3
Definition: hcons.C:13
Profile Historam.
Definition: TProfile.h:34
tuple hprofs
Definition: ntuple1.py:59
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:514
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
void canvas_write()
Definition: sqlcanvas.C:22
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2729
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:326
void canvas_read()
Definition: sqlcanvas.C:137
virtual void SetMarkerColor(Color_t mcolor=1)
Definition: TAttMarker.h:51
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:4705
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:63
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1192
virtual void SetTextAlign(Short_t align=11)
Definition: TAttText.h:55
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
tuple pad4
Definition: ntuple1.py:21
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)
Definition: TAttFill.h:50
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
The most important graphics class in the ROOT system.
Definition: TPad.h:46
void SetStatW(Float_t w=0.19)
Definition: TStyle.h:393
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
virtual void SetMarkerSize(Size_t msize=1)
Definition: TAttMarker.h:54
The Canvas class.
Definition: TCanvas.h:48
const char * username
Definition: sqlcanvas.C:8
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:35
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:360
TPad * pad2
Definition: hcons.C:13
tuple fpol2
Definition: ntuple1.py:67
virtual void Draw(Option_t *option="")
Draw a canvas.
Definition: TCanvas.cxx:795
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:2179
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:3607
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TH1.cxx:8382
virtual void SetLogy(Int_t value=1)
Set Lin/Log scale for Y.
Definition: TPad.cxx:5314