Logo ROOT  
Reference Guide
SQLiteIPLocation.C File Reference

Detailed Description

View in nbviewer Open in SWAN

This tutorial demonstrates how TSQLServer can be used to create a connection with a SQlite3 database. It accesses the Sqlite data base. Download from https://root.cern/download/root_download_stats.sqlite The world map is hold by a TH2Poly histogram which, after filling, will show the world wide dispersion of ROOT's users. To histogram filling, is done having as input parameters the two columns of the database: "IPLongitude' - for the longitude, and the "IPLatitude" - for the latitude. The data related to the latitude and the longitude has been provided from the log files storing the users IP. This product includes GeoLite2 data created by MaxMind, available from http://www.maxmind.com.

#include <TSQLiteServer.h>
#include <TSQLiteResult.h>
#include <TSQLRow.h>
#include <TString.h>
#include <TH2F.h>
void SQLiteIPLocation() {
TSQLServer *db = TSQLServer::Connect("sqlite://root_download_stats.sqlite", "", "");
TFile *F = TFile::Open("http://root.cern.ch/files/WM.root");
TH2Poly *WM;
WM = (TH2Poly*) F->Get("WM");
const char *location = "SELECT IPLatitude, IPLongitude FROM accesslog;";
TSQLResult *locationRes = db->Query(location);
while (TSQLRow *row = locationRes->Next()) {
if (!row->GetField(0)[0])
continue;
std::string sLatitude(row->GetField(0));
std::string sLongitude(row->GetField(1));
float latitude = std::stof(sLatitude);
float longitude = std::stof(sLongitude);
WM->Fill(longitude, latitude);
delete row;
}
TCanvas *locationHistogram = new TCanvas();
locationHistogram->SetLogz(1);
locationHistogram->ToggleEventStatus();
WM->Draw("colz");
}
Author
Alexandra-Maria Dobrescu 08/2018

Definition in file SQLiteIPLocation.C.

TSQLResult::Next
virtual TSQLRow * Next()=0
F
#define F(x, y, z)
TSQLiteResult.h
TFile::Open
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3946
TSQLiteServer.h
TString.h
TPad::SetLogz
virtual void SetLogz(Int_t value=1)
Set Lin/Log scale for Z.
Definition: TPad.cxx:5916
TSQLResult
Definition: TSQLResult.h:32
TSQLServer::Connect
static TSQLServer * Connect(const char *db, const char *uid, const char *pw)
The db should be of the form: <dbms>://<host>[:<port>][/<database>], e.g.
Definition: TSQLServer.cxx:61
TSQLServer::Query
virtual TSQLResult * Query(const char *sql)=0
TSQLRow
Definition: TSQLRow.h:30
TSQLRow.h
TCanvas::ToggleEventStatus
virtual void ToggleEventStatus()
Toggle event statusbar.
Definition: TCanvas.cxx:2442
TH2F.h
TFile
Definition: TFile.h:54
TCanvas
Definition: TCanvas.h:23
TH2Poly::Fill
virtual Int_t Fill(Double_t x, Double_t y)
Increment the bin containing (x,y) by 1.
Definition: TH2Poly.cxx:589
TH2Poly
Definition: TH2Poly.h:66
TSQLServer
Definition: TSQLServer.h:41
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2997