Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
SQLiteIPLocation.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_sql
3/// \notebook -js
4/// This tutorial demonstrates how TSQLServer can be used to create a connection with a SQlite3 database.
5/// It accesses the Sqlite data base.
6/// Download from https://root.cern/files/root_download_stats.sqlite
7/// The world map is hold by a TH2Poly histogram which, after filling, will show
8/// the world wide dispersion of ROOT's users.
9/// To histogram filling, is done having as input parameters
10/// the two columns of the database: "IPLongitude' - for the longitude, and the
11/// "IPLatitude" - for the latitude.
12/// The data related to the latitude and the longitude has been provided from the
13/// log files storing the users IP.
14/// This product includes GeoLite2 data created by MaxMind, available from
15/// <a href="http://www.maxmind.com">http://www.maxmind.com</a>.
16///
17/// \macro_code
18///
19/// \author Alexandra-Maria Dobrescu 08/2018
20
21#include <TSQLiteServer.h>
22#include <TSQLiteResult.h>
23#include <TSQLRow.h>
24#include <TString.h>
25#include <TH2F.h>
26
27void SQLiteIPLocation() {
28
29 TSQLServer *db = TSQLServer::Connect("sqlite://root_download_stats.sqlite", "", "");
30
31 TFile *F = TFile::Open("http://root.cern/files/WM.root");
32 TH2Poly *WM;
33 WM = (TH2Poly*) F->Get("WM");
34 const char *location = "SELECT IPLatitude, IPLongitude FROM accesslog;";
35 TSQLResult *locationRes = db->Query(location);
36
37 while (TSQLRow *row = locationRes->Next()) {
38 if (!row->GetField(0)[0])
39 continue;
40 std::string sLatitude(row->GetField(0));
41 std::string sLongitude(row->GetField(1));
42 float latitude = std::stof(sLatitude);
43 float longitude = std::stof(sLongitude);
44 WM->Fill(longitude, latitude);
45
46 delete row;
47 }
48
49 TCanvas *locationHistogram = new TCanvas();
50
51 locationHistogram->SetLogz(1);
52 locationHistogram->ToggleEventStatus();
53 WM->Draw("colz");
54}
The Canvas class.
Definition TCanvas.h:23
virtual void ToggleEventStatus()
Toggle event statusbar.
Definition TCanvas.cxx:2431
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
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:4089
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
2D Histogram with Polygonal Bins
Definition TH2Poly.h:66
Int_t Fill(Double_t x, Double_t y) override
Increment the bin containing (x,y) by 1.
Definition TH2Poly.cxx:664
void SetLogz(Int_t value=1) override
Set Lin/Log scale for Z.
Definition TPad.cxx:6111
virtual TSQLRow * Next()=0
virtual TSQLResult * Query(const char *sql)=0
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.: mysql://pcroot....
#define F(x, y, z)