Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
sqltables.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_sql
3/// This is an example illustrating how the TSQLFile class can be used.
4/// Histogram, list of TBox and clones array of TBox objects are stored
5/// to TSQLFile and read back.
6/// Except for the specific TSQLFile configuration, the TSQLFile functionality
7/// is absolutely similar to a normal root TFile
8///
9/// \macro_code
10///
11/// \author Sergey Linev
12
13// example configuration for MySQL 4.1
14const char* dbname = "mysql://host.domain/test";
15const char* username = "user";
16const char* userpass = "pass";
17
18// example configuration for Oracle 9i
19//const char* dbname = "oracle://host.domain/db-test";
20//const char* username = "user";
21//const char* userpass = "pass";
22
23
24void sqltables()
25{
26 tables_write();
27 tables_read();
28}
29
30void tables_write()
31{
32 // first connect to data base
33 // "recreate" option delete all your tables !!!!
34 TSQLFile* f = new TSQLFile(dbname, "recreate", username, userpass);
35 if (f->IsZombie()) { delete f; return; }
36
37 // you can change configuration only until first object
38 // is written to TSQLFile
39 f->SetUseSuffixes(kFALSE);
40 f->SetArrayLimit(1000);
41 f->SetUseIndexes(1);
42// f->SetTablesType("ISAM");
43// f->SetUseTransactions(kFALSE);
44
45 // lets first write histogram
46 TH1I* h1 = new TH1I("histo1","histo title", 1000, -4., 4.);
47 h1->FillRandom("gaus",10000);
48 h1->Write("histo");
49 h1->SetDirectory(0);
50
51 // here we create list of objects and store them as single key
52 // without kSingleKey all TBox objects will appear as separate keys
53 TList* arr = new TList;
54 for(Int_t n=0;n<10;n++) {
55 TBox* b = new TBox(n*10,n*100,n*20,n*200);
56 arr->Add(b, Form("option_%d_option",n));
57 }
58 arr->Write("list",TObject::kSingleKey);
59
60 // clones array is also stored as single key
61 TClonesArray clones("TBox",10);
62 for(int n=0;n<10;n++)
63 new (clones[n]) TBox(n*10,n*100,n*20,n*200);
64 clones.Write("clones",TObject::kSingleKey);
65
66 // close connection to database
67 delete f;
68}
69
70
71void tables_read()
72{
73 // now open connection to database for read-only
74 TSQLFile* f = new TSQLFile(dbname, "open", username, userpass);
75 if (f->IsZombie()) { delete f; return; }
76
77 // see list of keys
78 f->ls();
79
80 // get histogram from DB and draw it
81 TH1* h1 = (TH1*) f->Get("histo");
82 if (h1!=0) {
83 h1->SetDirectory(0);
84 h1->Draw();
85 }
86
87 // get TList with other objects
88 TObject* obj = f->Get("list");
89 cout << "Printout of TList object" << endl;
90 if (obj!=0) obj->Print("*");
91 delete obj;
92
93 // and get TClonesArray
94 obj = f->Get("clones");
95 cout << "Printout of TClonesArray object" << endl;
96 if (obj!=0) obj->Print("*");
97 delete obj;
98
99 // this is query to select data of hole class from different tables
100 cout << "================ TBox QUERY ================ " << endl;
101 cout << f->MakeSelectQuery(TBox::Class()) << endl;
102 cout << "================ END of TBox QUERY ================ " << endl;
103
104 cout << "================== TH1I QUERY ================ " << endl;
105 cout << f->MakeSelectQuery(TH1I::Class()) << endl;
106 cout << "================ END of TH1I QUERY ================ " << endl;
107
108 // close connection to database
109 delete f;
110}
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
Create a Box.
Definition TBox.h:22
static TClass * Class()
An array of clone (identical) objects.
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override
Write all objects in this collection.
1-D histogram with an int per channel (see TH1 documentation)
Definition TH1.h:539
static TClass * Class()
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:8905
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition TH1.cxx:3519
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
Mother of all ROOT objects.
Definition TObject.h:41
@ kSingleKey
write collection with single key
Definition TObject.h:91
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition TObject.cxx:880
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition TObject.cxx:636
Access an SQL db via the TFile interface.
Definition TSQLFile.h:30
const Int_t n
Definition legend1.C:16
TH1F * h1
Definition legend1.C:5