Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
sqltables.C File Reference

Detailed Description

This is an example illustrating how the TSQLFile class can be used.

Histogram, list of TBox and clones array of TBox objects are stored to TSQLFile and read back. Except for the specific TSQLFile configuration, the TSQLFile functionality is absolutely similar to a normal root TFile

// example configuration for MySQL 4.1
const char* dbname = "mysql://host.domain/test";
const char* username = "user";
const char* userpass = "pass";
// example configuration for Oracle 9i
//const char* dbname = "oracle://host.domain/db-test";
//const char* username = "user";
//const char* userpass = "pass";
void sqltables()
{
}
{
// first connect to data base
// "recreate" option delete all your tables !!!!
TSQLFile* f = new TSQLFile(dbname, "recreate", username, userpass);
if (f->IsZombie()) { delete f; return; }
// you can change configuration only until first object
// is written to TSQLFile
f->SetUseSuffixes(kFALSE);
f->SetArrayLimit(1000);
f->SetUseIndexes(1);
// f->SetTablesType("ISAM");
// f->SetUseTransactions(kFALSE);
// lets first write histogram
TH1I* h1 = new TH1I("histo1","histo title", 1000, -4., 4.);
h1->FillRandom("gaus",10000);
h1->Write("histo");
// here we create list of objects and store them as single key
// without kSingleKey all TBox objects will appear as separate keys
TList* arr = new TList;
for(Int_t n=0;n<10;n++) {
TBox* b = new TBox(n*10,n*100,n*20,n*200);
arr->Add(b, Form("option_%d_option",n));
}
arr->Write("list",TObject::kSingleKey);
// clones array is also stored as single key
TClonesArray clones("TBox",10);
for(int n=0;n<10;n++)
new (clones[n]) TBox(n*10,n*100,n*20,n*200);
clones.Write("clones",TObject::kSingleKey);
// close connection to database
delete f;
}
{
// now open connection to database for read-only
if (f->IsZombie()) { delete f; return; }
// see list of keys
f->ls();
// get histogram from DB and draw it
TH1* h1 = (TH1*) f->Get("histo");
if (h1!=0) {
h1->Draw();
}
// get TList with other objects
TObject* obj = f->Get("list");
cout << "Printout of TList object" << endl;
if (obj!=0) obj->Print("*");
delete obj;
// and get TClonesArray
obj = f->Get("clones");
cout << "Printout of TClonesArray object" << endl;
if (obj!=0) obj->Print("*");
delete obj;
// this is query to select data of hole class from different tables
cout << "================ TBox QUERY ================ " << endl;
cout << f->MakeSelectQuery(TBox::Class()) << endl;
cout << "================ END of TBox QUERY ================ " << endl;
cout << "================== TH1I QUERY ================ " << endl;
cout << f->MakeSelectQuery(TH1I::Class()) << endl;
cout << "================ END of TH1I QUERY ================ " << endl;
// close connection to database
delete f;
}
#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:94
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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.
1-D histogram with an int per channel (see TH1 documentation)
Definition TH1.h:563
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:8939
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng=nullptr)
Definition TH1.cxx:3499
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3037
A doubly linked list.
Definition TList.h:38
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:898
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition TObject.cxx:654
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
Author
Sergey Linev

Definition in file sqltables.C.