46using Clock = std::chrono::high_resolution_clock;
53 auto result = std::shared_ptr<TH1D>(
static_cast<TH1D *
>(
h.GetPtr()->Clone()));
54 result->SetDirectory(
nullptr);
62constexpr const char *kRawDataUrl =
"http://root.cern./files/tutorials/GlobalLandTemperaturesByCity.csv";
63constexpr const char *kNTupleFileName =
"GlobalLandTemperaturesByCity.root";
69 std::cout <<
"Converting " << kRawDataUrl <<
" to " << kNTupleFileName << std::endl;
71 auto t1 = Clock::now();
74 auto model = RNTupleModel::Create();
77 auto fieldYear = model->MakeField<std::uint32_t>(
"Year");
78 auto fieldMonth = model->MakeField<std::uint32_t>(
"Month");
79 auto fieldDay = model->MakeField<std::uint32_t>(
"Day");
80 auto fieldAvgTemp = model->MakeField<
float>(
"AverageTemperature");
81 auto fieldTempUncrty = model->MakeField<
float>(
"AverageTemperatureUncertainty");
82 auto fieldCity = model->MakeField<std::string>(
"City");
83 auto fieldCountry = model->MakeField<std::string>(
"Country");
84 auto fieldLat = model->MakeField<
float>(
"Latitude");
85 auto fieldLong = model->MakeField<
float>(
"Longitude");
89 auto ntuple = RNTupleWriter::Recreate(std::move(model),
"GlobalTempData", kNTupleFileName);
95 auto file = RRawFile::Create(kRawDataUrl, options);
97 constexpr int kMaxCharsPerLine = 128;
98 while (file->Readln(record)) {
99 if (record.length() >= kMaxCharsPerLine)
100 throw std::runtime_error(
"record too long: " + record);
105 std::replace(record.begin(), record.end(),
',',
' ');
106 char country[kMaxCharsPerLine];
107 char city[kMaxCharsPerLine];
109 sscanf(record.c_str(),
"%u-%u-%u %f %f %s %s %fN %fE", fieldYear.get(), fieldMonth.get(), fieldDay.get(),
110 fieldAvgTemp.get(), fieldTempUncrty.get(), country, city, fieldLat.get(), fieldLong.get());
115 *fieldCountry = country;
120 if (++nRecords % 1000000 == 0)
121 std::cout <<
" ... converted " << nRecords <<
" records" << std::endl;
125 std::cout << nSkipped <<
" records skipped" << std::endl;
126 std::cout << nRecords <<
" records processed" << std::endl;
128 auto t2 = Clock::now();
129 std::cout << std::endl
130 <<
"Processing Time: " << std::chrono::duration_cast<std::chrono::seconds>(t2 -
t1).count() <<
" seconds\n"
140 df.Display()->Print();
143 auto min_value = df.Min(
"AverageTemperature");
144 auto max_value = df.Max(
"AverageTemperature");
147 auto fnWinter = [](
int month) {
return month == 12 || month == 1 || month == 2; };
148 auto fnSpring = [](
int month) {
return month == 3 || month == 4 || month == 5; };
149 auto fnSummer = [](
int month) {
return month == 6 || month == 7 || month == 8; };
150 auto fnFall = [](
int month) {
return month == 9 || month == 10 || month == 11; };
153 auto dfWinter = df.Filter(fnWinter, {
"Month"});
154 auto dfSpring = df.Filter(fnSpring, {
"Month"});
155 auto dfSummer = df.Filter(fnSummer, {
"Month"});
156 auto dfFall = df.Filter(fnFall, {
"Month"});
159 auto winterCount = dfWinter.Count();
160 auto springCount = dfSpring.Count();
161 auto summerCount = dfSummer.Count();
162 auto fallCount = dfFall.Count();
165 auto fn1993_to_2002 = [](
int year) {
return year >= 1993 && year <= 2002; };
166 auto fn2003_to_2013 = [](
int year) {
return year >= 2003 && year <= 2013; };
169 auto df1993_to_2002 = df.Filter(fn1993_to_2002, {
"Year"});
170 auto df2003_to_2013 = df.Filter(fn2003_to_2013, {
"Year"});
173 auto decade_1993_to_2002_Count = *df1993_to_2002.Count();
174 auto decade_2003_to_2013_Count = *df2003_to_2013.Count();
177 auto fallHistResultPtr =
178 dfFall.Histo1D({
"Fall Average Temp",
"Average Temperature by Season", 100, -40, 40},
"AverageTemperature");
179 auto winterHistResultPtr =
180 dfWinter.Histo1D({
"Winter Average Temp",
"Average Temperature by Season", 100, -40, 40},
"AverageTemperature");
181 auto springHistResultPtr =
182 dfSpring.Histo1D({
"Spring Average Temp",
"Average Temperature by Season", 100, -40, 40},
"AverageTemperature");
183 auto summerHistResultPtr =
184 dfSummer.Histo1D({
"Summer Average Temp",
"Average Temperature by Season", 100, -40, 40},
"AverageTemperature");
187 auto hist_1993_to_2002_ResultPtr = df1993_to_2002.Histo1D(
188 {
"1993_to_2002 Average Temp",
"Average Temperature: 1993_to_2002 vs. 2003_to_2013", 100, -40, 40},
189 "AverageTemperature");
190 auto hist_2003_to_2013_ResultPtr = df2003_to_2013.Histo1D(
191 {
"2003_to_2013 Average Temp",
"Average Temperature: 1993_to_2002 vs. 2003_to_2013", 100, -40, 40},
192 "AverageTemperature");
197 std::cout << std::endl <<
"The Minimum temperature is: " << *min_value << std::endl;
198 std::cout <<
"The Maximum temperature is: " << *max_value << std::endl;
201 std::cout << std::endl <<
"The count for Winter: " << *winterCount << std::endl;
202 std::cout <<
"The count for Spring: " << *springCount << std::endl;
203 std::cout <<
"The count for Summer: " << *summerCount << std::endl;
204 std::cout <<
"The count for Fall: " << *fallCount << std::endl;
207 std::cout << std::endl <<
"The count for 1993_to_2002: " << decade_1993_to_2002_Count << std::endl;
208 std::cout <<
"The count for 2003_to_2013: " << decade_2003_to_2013_Count << std::endl;
211 auto fallHist = GetDrawableHist(fallHistResultPtr);
212 auto winterHist = GetDrawableHist(winterHistResultPtr);
213 auto springHist = GetDrawableHist(springHistResultPtr);
214 auto summerHist = GetDrawableHist(summerHistResultPtr);
217 fallHist->SetLineColor(
kOrange);
218 fallHist->SetLineWidth(6);
220 winterHist->SetLineColor(
kBlue);
221 winterHist->SetLineWidth(6);
223 springHist->SetLineColor(
kGreen);
224 springHist->SetLineWidth(6);
226 summerHist->SetLineColor(
kRed);
227 summerHist->SetLineWidth(6);
230 auto hist_1993_to_2002 = GetDrawableHist(hist_1993_to_2002_ResultPtr);
231 auto hist_2003_to_2013 = GetDrawableHist(hist_2003_to_2013_ResultPtr);
234 hist_1993_to_2002->SetLineColor(
kViolet);
235 hist_1993_to_2002->SetLineWidth(6);
237 hist_2003_to_2013->SetLineColor(
kSpring);
238 hist_2003_to_2013->SetLineWidth(6);
241 auto canvas = RCanvas::Create(
"Average Temperature by Season");
248 auto legend = std::make_shared<TLegend>(0.15, 0.65, 0.53, 0.85);
249 legend->AddEntry(fallHist.get(),
"fall",
"l");
250 legend->AddEntry(winterHist.get(),
"winter",
"l");
251 legend->AddEntry(springHist.get(),
"spring",
"l");
252 legend->AddEntry(summerHist.get(),
"summer",
"l");
257 auto canvas2 = RCanvas::Create(
"Average Temperature: 1993_to_2002 vs. 2003_to_2013");
262 auto legend2 = std::make_shared<TLegend>(0.1, 0.7, 0.48, 0.9);
263 legend2->AddEntry(hist_1993_to_2002.get(),
"1993_to_2002",
"l");
264 legend2->AddEntry(hist_2003_to_2013.get(),
"2003_to_2013",
"l");
269void ntpl011_global_temperatures()
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
R__EXTERN TSystem * gSystem
Provides v7 drawing facilities for TObject types (TGraph, TH1, TH2, etc).
The RRawFile provides read-only access to local and remote files.
Smart pointer for the return type of actions.
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
1-D histogram with a double per channel (see TH1 documentation)
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
On construction, an ROptions parameter can customize the RRawFile behavior.
size_t fBlockSize
Read at least fBlockSize bytes at a time. A value of zero turns off I/O buffering.