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