Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
28 void 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 }