Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
global_temperatures.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_v7
3///
4/// This ROOT 7 example demonstrates how to use RNTuple in combination with ROOT 6 features like RDataframe and visualizations.
5/// It ingests climate data and creates a model with fields like AverageTemperature. Then it uses RDataframe to process and filter the climate data for
6/// average temperature per city by season. Then it does the same for average temperature per city for the years between 1993-2002, and 2003-2013.
7/// Finally, the tutorial visualizes this processed data through histograms.
8///
9/// TODO(jblomer): re-enable once issues are fixed (\macro_image (rcanvas_js))
10/// \macro_code
11///
12///
13/// NOTE: Until C++ runtime modules are universally used, we explicitly load the ntuple library. Otherwise
14/// triggering autoloading from the use of templated types would require an exhaustive enumeration
15/// of "all" template instances in the LinkDef file.
16///
17/// \warning The RNTuple classes are experimental at this point.
18/// Functionality, interface, and data format is still subject to changes.
19/// Do not use for real data! During ROOT setup, configure the following flags:
20/// `-DCMAKE_CXX_STANDARD=14 -Droot7=ON -Dwebgui=ON`
21///
22/// \date 2021-02-26
23/// \author John Yoon
24
25R__LOAD_LIBRARY(ROOTNTuple)
26#include <ROOT/RDataFrame.hxx>
27#include <ROOT/RNTuple.hxx>
28#include <ROOT/RNTupleDS.hxx>
30#include <ROOT/RNTupleModel.hxx>
31#include <ROOT/RCanvas.hxx>
32#include <ROOT/RColor.hxx>
35#include <ROOT/RRawFile.hxx>
36#include <TH1D.h>
37#include <TLegend.h>
38#include <TSystem.h>
39
40#include <algorithm>
41#include <cassert>
42#include <cstdio>
43#include <fstream>
44#include <iostream>
45#include <memory>
46#include <string>
47#include <sstream>
48#include <stdexcept>
49#include <utility>
50#include <chrono>
51
52using Clock = std::chrono::high_resolution_clock;
53using RRawFile = ROOT::Internal::RRawFile;
54using namespace ROOT::Experimental;
55
56// Helper function to handle histogram pointer ownership.
57std::shared_ptr<TH1D> GetDrawableHist(ROOT::RDF::RResultPtr<TH1D> &h) {
58 auto result = std::shared_ptr<TH1D>(static_cast<TH1D *>(h.GetPtr()->Clone()));
59 result->SetDirectory(nullptr);
60 return result;
61}
62
63// Climate data is downloadable at the following URL:
64// https://www.kaggle.com/berkeleyearth/climate-change-earth-surface-temperature-data
65// The original data set is from http://berkeleyearth.org/archive/data/
66// License CC BY-NC-SA 4.0
67constexpr const char *kRawDataUrl = "http://root.cern./files/tutorials/GlobalLandTemperaturesByCity.csv";
68constexpr const char *kNTupleFileName = "GlobalLandTemperaturesByCity.root";
69
70void Ingest() {
71 int nRecords = 0;
72 int nSkipped = 0;
73 std::cout << "Converting " << kRawDataUrl << " to " << kNTupleFileName << std::endl;
74
75 auto t1 = Clock::now();
76
77 // Create a unique pointer to an empty data model.
78 auto model = RNTupleModel::Create();
79 // To define the data model, create fields with a given C++ type and name. Fields are roughly TTree branches.
80 // MakeField returns a shared pointer to a memory location to fill the ntuple with data.
81 auto fieldYear = model->MakeField<std::uint32_t>("Year");
82 auto fieldMonth = model->MakeField<std::uint32_t>("Month");
83 auto fieldDay = model->MakeField<std::uint32_t>("Day");
84 auto fieldAvgTemp = model->MakeField<float>("AverageTemperature");
85 auto fieldTempUncrty = model->MakeField<float>("AverageTemperatureUncertainty");
86 auto fieldCity = model->MakeField<std::string>("City");
87 auto fieldCountry = model->MakeField<std::string>("Country");
88 auto fieldLat = model->MakeField<float>("Latitude");
89 auto fieldLong = model->MakeField<float>("Longitude");
90
91 // Hand-over the data model to a newly created ntuple of name "globalTempData", stored in kNTupleFileName.
92 // In return, get a unique pointer to a fillable ntuple (first compress the file).
93 RNTupleWriteOptions options;
95 auto ntuple = RNTupleWriter::Recreate(std::move(model), "GlobalTempData", kNTupleFileName, options);
96
97 auto file = RRawFile::Create(kRawDataUrl);
98 std::string record;
99 constexpr int kMaxCharsPerLine = 128;
100 while (file->Readln(record)) {
101 if (record.length() >= kMaxCharsPerLine)
102 throw std::runtime_error("record too long: " + record);
103
104 // Parse lines of the form:
105 // 1743-11-01,6.068,1.7369999999999999,Ã…rhus,Denmark,57.05N,10.33E
106 // and skip records with empty fields.
107 std::replace(record.begin(), record.end(), ',', ' ');
108 char country[kMaxCharsPerLine];
109 char city[kMaxCharsPerLine];
110 int nFields = sscanf(record.c_str(), "%u-%u-%u %f %f %s %s %fN %fE",
111 fieldYear.get(), fieldMonth.get(), fieldDay.get(),
112 fieldAvgTemp.get(), fieldTempUncrty.get(), country, city,
113 fieldLat.get(), fieldLong.get());
114 if (nFields != 9) {
115 nSkipped++;
116 continue;
117 }
118 *fieldCountry = country;
119 *fieldCity = city;
120
121 ntuple->Fill();
122
123 if (++nRecords % 1000000 == 0)
124 std::cout << " ... converted " << nRecords << " records" << std::endl;
125 }
126
127 // Display the total time to process the data.
128 std::cout << nSkipped << " records skipped" << std::endl;
129 std::cout << nRecords << " records processed" << std::endl;
130
131 auto t2 = Clock::now();
132 std::cout << std::endl
133 << "Processing Time: "
134 << std::chrono::duration_cast<std::chrono::seconds>(t2 - t1).count()
135 << " seconds\n" << std::endl;
136}
137
138// Every data result that we want to get is declared first, and it is only upon their declaration that
139// they are actually used. This stems from motivations relating to efficiency and optimization.
140void Analyze() {
141 // Create a RDataframe by wrapping around NTuple.
142 auto df = ROOT::Experimental::MakeNTupleDataFrame("GlobalTempData", kNTupleFileName);
143 df.Display()->Print();
144
145 // Declare the minimum and maximum temperature from the dataset.
146 auto min_value = df.Min("AverageTemperature");
147 auto max_value = df.Max("AverageTemperature");
148
149 // Functions to filter by each season from date formatted "1944-12-01."
150 auto fnWinter = [](int month) { return month == 12 || month == 1 || month == 2; };
151 auto fnSpring = [](int month) { return month == 3 || month == 4 || month == 5; };
152 auto fnSummer = [](int month) { return month == 6 || month == 7 || month == 8; };
153 auto fnFall = [](int month) { return month == 9 || month == 10 || month == 11; };
154
155 // Create a RDataFrame per season.
156 auto dfWinter = df.Filter(fnWinter, {"Month"});
157 auto dfSpring = df.Filter(fnSpring, {"Month"});
158 auto dfSummer = df.Filter(fnSummer, {"Month"});
159 auto dfFall = df.Filter(fnFall, {"Month"});
160
161 // Get the count for each season.
162 auto winterCount = dfWinter.Count();
163 auto springCount = dfSpring.Count();
164 auto summerCount = dfSummer.Count();
165 auto fallCount = dfFall.Count();
166
167 // Functions to filter for the time period between 2003-2013, and 1993-2002.
168 auto fn1993_to_2002 = [](int year) { return year >= 1993 && year <= 2002; };
169 auto fn2003_to_2013 = [](int year) { return year >= 2003 && year <= 2013; };
170
171 // Create a RDataFrame for decades 1993_to_2002 & 2003_to_2013.
172 auto df1993_to_2002 = df.Filter(fn1993_to_2002, {"Year"});
173 auto df2003_to_2013 = df.Filter(fn2003_to_2013, {"Year"});
174
175 // Get the count for each decade.
176 auto decade_1993_to_2002_Count = *df1993_to_2002.Count();
177 auto decade_2003_to_2013_Count = *df2003_to_2013.Count();
178
179 // Configure histograms for each season.
180 auto fallHistResultPtr = dfFall.Histo1D({"Fall Average Temp", "Average Temperature by Season", 100, -40, 40}, "AverageTemperature");
181 auto winterHistResultPtr = dfWinter.Histo1D({"Winter Average Temp", "Average Temperature by Season", 100, -40, 40}, "AverageTemperature");
182 auto springHistResultPtr = dfSpring.Histo1D({"Spring Average Temp", "Average Temperature by Season", 100, -40, 40}, "AverageTemperature");
183 auto summerHistResultPtr = dfSummer.Histo1D({"Summer Average Temp", "Average Temperature by Season", 100, -40, 40}, "AverageTemperature");
184
185 // Configure histograms for each decade.
186 auto hist_1993_to_2002_ResultPtr = df1993_to_2002.Histo1D({"1993_to_2002 Average Temp", "Average Temperature: 1993_to_2002 vs. 2003_to_2013", 100, -40, 40}, "AverageTemperature");
187 auto hist_2003_to_2013_ResultPtr = df2003_to_2013.Histo1D({"2003_to_2013 Average Temp", "Average Temperature: 1993_to_2002 vs. 2003_to_2013", 100, -40, 40}, "AverageTemperature");
188
189 //____________________________________________________________________________________
190
191 // Display the minimum and maximum temperature values.
192 std::cout << std::endl << "The Minimum temperature is: " << *min_value << std::endl;
193 std::cout << "The Maximum temperature is: " << *max_value << std::endl;
194
195 // Display the count for each season.
196 std::cout << std::endl << "The count for Winter: " << *winterCount<< std::endl;
197 std::cout << "The count for Spring: " << *springCount << std::endl;
198 std::cout << "The count for Summer: " << *summerCount << std::endl;
199 std::cout << "The count for Fall: " << *fallCount << std::endl;
200
201 // Display the count for each decade.
202 std::cout << std::endl << "The count for 1993_to_2002: " << decade_1993_to_2002_Count << std::endl;
203 std::cout << "The count for 2003_to_2013: " <<decade_2003_to_2013_Count << std::endl;
204
205 // Transform histogram in order to address ROOT 7 v 6 version compatibility
206 auto fallHist = GetDrawableHist(fallHistResultPtr);
207 auto winterHist = GetDrawableHist(winterHistResultPtr);
208 auto springHist = GetDrawableHist(springHistResultPtr);
209 auto summerHist = GetDrawableHist(summerHistResultPtr);
210
211 // Set an orange histogram for fall.
212 fallHist->SetLineColor(kOrange);
213 fallHist->SetLineWidth(6);
214 // Set a blue histogram for winter.
215 winterHist->SetLineColor(kBlue);
216 winterHist->SetLineWidth(6);
217 // Set a green histogram for spring.
218 springHist->SetLineColor(kGreen);
219 springHist->SetLineWidth(6);
220 // Set a red histogram for summer.
221 summerHist->SetLineColor(kRed);
222 summerHist->SetLineWidth(6);
223
224 // Transform histogram in order to address ROOT 7 v 6 version compatibility
225 auto hist_1993_to_2002 = GetDrawableHist(hist_1993_to_2002_ResultPtr);
226 auto hist_2003_to_2013 = GetDrawableHist(hist_2003_to_2013_ResultPtr);
227
228 // Set a violet histogram for 1993_to_2002.
229 hist_1993_to_2002->SetLineColor(kViolet);
230 hist_1993_to_2002->SetLineWidth(6);
231 // Set a spring-green histogram for 2003_to_2013.
232 hist_2003_to_2013->SetLineColor(kSpring);
233 hist_2003_to_2013->SetLineWidth(6);
234
235
236 // Create a canvas to display histograms for average temperature by season.
237 auto canvas = RCanvas::Create("Average Temperature by Season");
238 canvas->Draw<TObjectDrawable>(fallHist, "L");
239 canvas->Draw<TObjectDrawable>(winterHist, "L");
240 canvas->Draw<TObjectDrawable>(springHist, "L");
241 canvas->Draw<TObjectDrawable>(summerHist, "L");
242
243 // Create a legend for the seasons canvas.
244 auto legend = std::make_shared<TLegend>(0.15,0.65,0.53,0.85);
245 legend->AddEntry(fallHist.get(),"fall","l");
246 legend->AddEntry(winterHist.get(),"winter","l");
247 legend->AddEntry(springHist.get(),"spring","l");
248 legend->AddEntry(summerHist.get(),"summer","l");
249 canvas->Draw<TObjectDrawable>(legend, "L");
250 canvas->Show();
251
252 // Create a canvas to display histograms for average temperature for 1993_to_2002 & 2003_to_2013.
253 auto canvas2 = RCanvas::Create("Average Temperature: 1993_to_2002 vs. 2003_to_2013");
254 canvas2->Draw<TObjectDrawable>(hist_1993_to_2002, "L");
255 canvas2->Draw<TObjectDrawable>(hist_2003_to_2013, "L");
256
257 // Create a legend for the two decades canvas.
258 auto legend2 = std::make_shared<TLegend>(0.1,0.7,0.48,0.9);
259 legend2->AddEntry(hist_1993_to_2002.get(),"1993_to_2002","l");
260 legend2->AddEntry(hist_2003_to_2013.get(),"2003_to_2013","l");
261 canvas2->Draw<TObjectDrawable>(legend2, "L");
262 canvas2->Show();
263}
264
265void global_temperatures() {
266 //if NOT zero (the file does NOT already exist), then Ingest
267 if (gSystem->AccessPathName(kNTupleFileName) != 0) {
268 Ingest();
269
270 }
271 Analyze();
272}
#define h(i)
Definition RSha256.hxx:106
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:472
@ kRed
Definition Rtypes.h:66
@ kOrange
Definition Rtypes.h:67
@ kGreen
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
@ kViolet
Definition Rtypes.h:67
@ kSpring
Definition Rtypes.h:67
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
Common user-tunable settings for storing ntuples.
Provides v7 drawing facilities for TObject types (TGraph, TH1, TH2, etc).
The RRawFile provides read-only access to local and remote files.
Definition RRawFile.hxx:43
Smart pointer for the return type of actions.
1-D histogram with a double per channel (see TH1 documentation)}
Definition TH1.h:618
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1296
RDataFrame MakeNTupleDataFrame(std::string_view ntupleName, std::string_view fileName)
Definition file.py:1
@ kUseGeneralPurpose
Use the new recommended general-purpose setting; it is a best trade-off between compression ratio/dec...
Definition Compression.h:54
auto * t1
Definition textangle.C:20