19#include <ROOT/RDataFrame.hxx>
45using Clock = std::chrono::high_resolution_clock;
51 auto result = std::shared_ptr<TH1D>(
static_cast<TH1D *
>(
h.GetPtr()->Clone()));
52 result->SetDirectory(
nullptr);
60constexpr const char *
kRawDataUrl =
"http://root.cern./files/tutorials/GlobalLandTemperaturesByCity.csv";
68 auto t1 = Clock::now();
74 auto fieldYear = model->MakeField<std::uint32_t>(
"Year");
75 auto fieldMonth = model->MakeField<std::uint32_t>(
"Month");
76 auto fieldDay = model->MakeField<std::uint32_t>(
"Day");
77 auto fieldAvgTemp = model->MakeField<
float>(
"AverageTemperature");
78 auto fieldTempUncrty = model->MakeField<
float>(
"AverageTemperatureUncertainty");
79 auto fieldCity = model->MakeField<std::string>(
"City");
80 auto fieldCountry = model->MakeField<std::string>(
"Country");
81 auto fieldLat = model->MakeField<
float>(
"Latitude");
82 auto fieldLong = model->MakeField<
float>(
"Longitude");
92 constexpr int kMaxCharsPerLine = 128;
93 while (
file->Readln(record)) {
94 if (record.length() >= kMaxCharsPerLine)
95 throw std::runtime_error(
"record too long: " + record);
100 std::replace(record.begin(), record.end(),
',',
' ');
101 char country[kMaxCharsPerLine];
102 char city[kMaxCharsPerLine];
103 int nFields = sscanf(record.c_str(),
"%u-%u-%u %f %f %s %s %fN %fE",
104 fieldYear.get(), fieldMonth.get(), fieldDay.get(),
105 fieldAvgTemp.get(), fieldTempUncrty.get(), country, city,
106 fieldLat.get(), fieldLong.get());
111 *fieldCountry = country;
116 if (++nRecords % 1000000 == 0)
117 std::cout <<
" ... converted " << nRecords <<
" records" << std::endl;
121 std::cout << nSkipped <<
" records skipped" << std::endl;
122 std::cout << nRecords <<
" records processed" << std::endl;
124 auto t2 = Clock::now();
125 std::cout << std::endl
126 <<
"Processing Time: "
127 << std::chrono::duration_cast<std::chrono::seconds>(t2 -
t1).count()
128 <<
" seconds\n" << std::endl;
136 df.Display()->Print();
139 auto min_value = df.Min(
"AverageTemperature");
140 auto max_value = df.Max(
"AverageTemperature");
143 auto fnWinter = [](
int month) {
return month == 12 || month == 1 || month == 2; };
144 auto fnSpring = [](
int month) {
return month == 3 || month == 4 || month == 5; };
145 auto fnSummer = [](
int month) {
return month == 6 || month == 7 || month == 8; };
146 auto fnFall = [](
int month) {
return month == 9 || month == 10 || month == 11; };
149 auto dfWinter = df.Filter(fnWinter, {
"Month"});
150 auto dfSpring = df.Filter(fnSpring, {
"Month"});
151 auto dfSummer = df.Filter(fnSummer, {
"Month"});
152 auto dfFall = df.Filter(fnFall, {
"Month"});
155 auto winterCount = dfWinter.Count();
156 auto springCount = dfSpring.Count();
157 auto summerCount = dfSummer.Count();
158 auto fallCount = dfFall.Count();
161 auto fn1993_to_2002 = [](
int year) {
return year >= 1993 && year <= 2002; };
162 auto fn2003_to_2013 = [](
int year) {
return year >= 2003 && year <= 2013; };
165 auto df1993_to_2002 = df.Filter(fn1993_to_2002, {
"Year"});
166 auto df2003_to_2013 = df.Filter(fn2003_to_2013, {
"Year"});
169 auto decade_1993_to_2002_Count = *df1993_to_2002.Count();
170 auto decade_2003_to_2013_Count = *df2003_to_2013.Count();
173 auto fallHistResultPtr = dfFall.Histo1D({
"Fall Average Temp",
"Average Temperature by Season", 100, -40, 40},
"AverageTemperature");
174 auto winterHistResultPtr = dfWinter.Histo1D({
"Winter Average Temp",
"Average Temperature by Season", 100, -40, 40},
"AverageTemperature");
175 auto springHistResultPtr = dfSpring.Histo1D({
"Spring Average Temp",
"Average Temperature by Season", 100, -40, 40},
"AverageTemperature");
176 auto summerHistResultPtr = dfSummer.Histo1D({
"Summer Average Temp",
"Average Temperature by Season", 100, -40, 40},
"AverageTemperature");
179 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");
180 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");
185 std::cout << std::endl <<
"The Minimum temperature is: " << *min_value << std::endl;
186 std::cout <<
"The Maximum temperature is: " << *max_value << std::endl;
189 std::cout << std::endl <<
"The count for Winter: " << *winterCount<< std::endl;
190 std::cout <<
"The count for Spring: " << *springCount << std::endl;
191 std::cout <<
"The count for Summer: " << *summerCount << std::endl;
192 std::cout <<
"The count for Fall: " << *fallCount << std::endl;
195 std::cout << std::endl <<
"The count for 1993_to_2002: " << decade_1993_to_2002_Count << std::endl;
196 std::cout <<
"The count for 2003_to_2013: " <<decade_2003_to_2013_Count << std::endl;
205 fallHist->SetLineColor(
kOrange);
206 fallHist->SetLineWidth(6);
208 winterHist->SetLineColor(
kBlue);
209 winterHist->SetLineWidth(6);
211 springHist->SetLineColor(
kGreen);
212 springHist->SetLineWidth(6);
214 summerHist->SetLineColor(
kRed);
215 summerHist->SetLineWidth(6);
222 hist_1993_to_2002->SetLineColor(
kViolet);
223 hist_1993_to_2002->SetLineWidth(6);
225 hist_2003_to_2013->SetLineColor(
kSpring);
226 hist_2003_to_2013->SetLineWidth(6);
237 auto legend = std::make_shared<TLegend>(0.15,0.65,0.53,0.85);
238 legend->AddEntry(fallHist.get(),
"fall",
"l");
239 legend->AddEntry(winterHist.get(),
"winter",
"l");
240 legend->AddEntry(springHist.get(),
"spring",
"l");
241 legend->AddEntry(summerHist.get(),
"summer",
"l");
246 auto canvas2 =
RCanvas::Create(
"Average Temperature: 1993_to_2002 vs. 2003_to_2013");
251 auto legend2 = std::make_shared<TLegend>(0.1,0.7,0.48,0.9);
252 legend2->AddEntry(hist_1993_to_2002.get(),
"1993_to_2002",
"l");
253 legend2->AddEntry(hist_2003_to_2013.get(),
"2003_to_2013",
"l");
#define R__LOAD_LIBRARY(LIBRARY)
R__EXTERN TSystem * gSystem
static std::shared_ptr< RCanvas > Create(const std::string &title)
Create new canvas instance.
static std::unique_ptr< RNTupleModel > Create()
Common user-tunable settings for storing ntuples.
void SetCompression(int val)
static std::unique_ptr< RNTupleWriter > Recreate(std::unique_ptr< RNTupleModel > model, std::string_view ntupleName, std::string_view storage, const RNTupleWriteOptions &options=RNTupleWriteOptions())
Provides v7 drawing facilities for TObject types (TGraph etc).
The RRawFile provides read-only access to local and remote files.
static std::unique_ptr< RRawFile > Create(std::string_view url, ROptions options=ROptions())
Factory method that returns a suitable concrete implementation according to the transport in the url.
Smart pointer for the return type of actions.
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.
void global_temperatures()
constexpr const char * kRawDataUrl
std::shared_ptr< TH1D > GetDrawableHist(ROOT::RDF::RResultPtr< TH1D > &h)
std::chrono::high_resolution_clock Clock
constexpr const char * kNTupleFileName
RDataFrame MakeNTupleDataFrame(std::string_view ntupleName, std::string_view fileName)
@ kUseGeneralPurpose
Use the new recommended general-purpose setting; it is a best trade-off between compression ratio/dec...