Logo ROOT  
Reference Guide
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
14 const char* dbname = "mysql://host.domain/test";
15 const char* username = "user";
16 const 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 
24 void sqltables()
25 {
26  tables_write();
27  tables_read();
28 }
29 
30 void 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 
71 void 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 }
n
const Int_t n
Definition: legend1.C:16
f
#define f(i)
Definition: RSha256.hxx:104
TCollection::Write
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write all objects in this collection.
Definition: TCollection.cxx:672
TObject::Print
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TObject.cxx:552
TH1I
1-D histogram with an int per channel (see TH1 documentation)}
Definition: TH1.h:532
Form
char * Form(const char *fmt,...)
Int_t
int Int_t
Definition: RtypesCore.h:45
TH1::FillRandom
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3502
b
#define b(i)
Definition: RSha256.hxx:100
h1
TH1F * h1
Definition: legend1.C:5
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TObject::kSingleKey
@ kSingleKey
write collection with single key
Definition: TObject.h:87
TH1::SetDirectory
virtual void SetDirectory(TDirectory *dir)
By default when an histogram is created, it is added to the list of histogram objects in the current ...
Definition: TH1.cxx:8612
TBox
Create a Box.
Definition: TBox.h:22
TObject::Write
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:795
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
TH1
TH1 is the base class of all histogramm classes in ROOT.
Definition: TH1.h:58
Class
void Class()
Definition: Class.C:29
TClonesArray
An array of clone (identical) objects.
Definition: TClonesArray.h:29
TList
A doubly linked list.
Definition: TList.h:44
TSQLFile
Access an SQL db via the TFile interface.
Definition: TSQLFile.h:30
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3050