为什么这段代码在 ROOT 下编译,但没有做任何它想做的事情?
Why does this code compile in ROOT, but does not do any of what it's meant to do?
我正在尝试编写一段代码来执行以下任务:
- 读入包含几年(1722 年到 2013 年)平均每日温度的数据文件
- 求年平均气温
- 制作这些年平均温度的直方图
- 通过此数据进行拟合,以便在给定特定年份(例如 2060 年)的情况下,您可以推断以确定该年的平均温度。
到目前为止我写的代码,包括在下面,应该能够执行任务 1 到 3。它分为四个部分:一个头文件 "sketch.h",一个 C++ 脚本 "sketch.cpp"、另一个 C++ 脚本 "project.cpp" 和一些 ROOT 绘图文件 "rootlogon.C".
文件 "sketch.h" 和 "sketch.cpp" 包含 class 的定义及其成员的实现。"project.cpp" 是实例化分析对象的文件。 "rootlogon.C" 对直方图和图形的外观有一些预定义的首选项。
想法是,如果 ROOT 运行 在包含这 4 段代码的同一文件夹中,它将自动加载它们。
sketch.h
#ifndef TEMPTRENDER_H
#define TEMPTRENDER_H
#include <string>
class tempTrender
{
public:
tempTrender(std::string filePath); //Construct using the specified filePath
//void tempPerYear(int yearToExtrapolate); //Make a histogram of average temperature per year, then fit and extrapolate to the given year
void read_datFile(); //will be used to read in the data file
private:
std::string pathToFile; //my uppsala .dat file
int Year;
int Month;
int Day;
double Temp_exp;
double Temp_corr;
int dat_ID;
std::string helpString;
std::string filePath;
int numEntries; //total number of measurements done (entries in the data set)
int totTemp; //sum of daily temperatures in a given year
int counter;
int first_Year; //the year in which the measurements began
double AvgTemp; //average annual temperature
};
#endif
sketch.cpp
#include <iostream>
#include "sketch.h"
#include <fstream>
#include <string>
#include <sstream>
//ROOT library objects
#include <TF1.h> // 1d function class
#include <TH1.h> // 1d histogram classes
#include <TH2.h> // 2d histogram classes
#include <TRandom.h> // random generators
#include <TStyle.h> // style object
#include <TMath.h> // math functions
#include <TCanvas.h> // canvas object
#include <TGraph.h> //For the graphs part
#include <TLegend.h> //for the legends
using namespace std;
tempTrender::tempTrender(string filePath)
{
std::cout << "The user supplied " << filePath << " as the path to the data file." << endl;
string pathToFile = filePath;
}
//reading in data file
void tempTrender::read_datFile()
{
ifstream datFile(filePath.c_str()); //opening data file, as a C-string for later manipulation
if(!datFile) cout<<"Error: the file could not be read"<<endl; //just checking if the file was read in successfully ;-)
int n = 0; //initialise a counter
while(getline(datFile, helpString)) n++; //counting the number of lines in the data set;
numEntries = n;
//returning to the beginning of .dat file
datFile.clear();
datFile.seekg(0, ios::beg);
for(int i=0; i<numEntries; i++)
{
datFile >> Year >> Month >> Day >> Temp_exp >> Temp_corr >> dat_ID; //putting input data into categories
std::cout<<Year<<endl; //checking that the categories contain the right data
std::cout<<"I'm alive"<<endl;
}
//calculating average temperature and creating a histogram of the values
TH1F* hAvgTemp = new TH1F("hAvgTemp", "Average Annual Temperature: 1722-2013" , 600, 1722, 2013);
while(datFile>> Year >> Month >> Day >> Temp_exp >> Temp_corr >> dat_ID)
{
while(first_Year == 1722 && Year == first_Year)
{
for(int Month=0; Month < 13; Month++) //loop over every month
{
for(int Day =0; Day < 32; Day++) //loop over every day
{
counter++; //count the number of measurements
totTemp += Temp_corr; //sum up the daily temperatures over all months of first_Year
AvgTemp = (totTemp/counter); //average temperature for first_Year. One number.
}
}
hAvgTemp->Fill(AvgTemp); //start filling a histogram with the average temperatures
cout << AvgTemp << endl; //show that the calculation worked
}
first_Year = first_Year + 1; //go to the next year and run the loop again
}
//creating canvas for hAvgTemp histogram
TCanvas* c2 = new TCanvas("c1", "hAvgTemp Canvas", 900, 600);
hAvgTemp->SetMinimum(0);
hAvgTemp->Draw();
c2->SaveAs("AvgTemp.png");
}
project.cpp
#include "sketch.h"
#include <string>
#include <iostream>
using namespace std;
void project()
{
string pathToFile = "path_to_datFile.dat"; //path to data file
tempTrender t(pathToFile); //Instantiate analysis object
//t.tempPerYear(2050);
}
rootlogon.C
void rootlogon() {
gStyle->SetOptStat(0); //Let's make our plots look a bit better. Get rid of the stat box
gStyle->SetOptTitle(0); //Get rid of the title (good plots use legends instead)
gStyle->SetTitleSize(0.05, "x"); //Use bigger text on the axes
gStyle->SetTitleSize(0.05, "y");
gStyle->SetLabelSize(0.05, "x"); //Use bigger labels too
gStyle->SetLabelSize(0.05, "y");
gStyle->SetPadTopMargin(0.05); //Change the margins to fit our new sizes
gStyle->SetPadRightMargin(0.05);
gStyle->SetPadBottomMargin(0.16);
gStyle->SetPadLeftMargin(0.16);
gROOT->ProcessLine(".L sketch.cpp+"); //Load the classes that we want to use automatically - Convenient!
gROOT->ProcessLine(".L project.cpp+"); //The + means to recompile only if it changed since last time
}
数据文件提取
我在下面包含了我正在分析的数据文件的摘录。列分别为:年、月、日、测量温度、校正温度和数据 ID。
1722 1 12 1.9 1.8 1
1722 1 13 2.3 2.2 1
1722 1 14 1.8 1.7 1
1722 1 15 .9 .8 1
1722 1 16 -1.8 -1.9 1
. . . .
2013 12 29 3.8 3.6 1
2013 12 30 1.6 1.4 1
2013 12 31 3.3 3.1 1
代码在 ROOT 中编译,但不执行任何其他操作。我已经尝试了很长时间,但没有成功,以弄清楚它出了什么问题。任何帮助将不胜感激。此外,对于如何处理任务 4 的提示也将不胜感激。我是C++新手,欢迎批评指正
要使 rootlogon.C
脚本真正执行某些操作,您需要调用一个函数。我相信您想执行名为 project()
的 "main" 函数。如果是这种情况,那么您需要将 rootlogon.C
修改为:
void rootlogon() {
// ...
gROOT->ProcessLine(".L sketch.cpp+"); //Load the classes that we want to use automatically - Convenient!
gROOT->ProcessLine(".L project.cpp+"); //The + means to recompile only if it changed since last time
project(); // <- Add this line
}
gROOT->ProcessLine(".L ...")
指令只是编译代码并加载 ROOT 中的库,但您仍然需要调用该函数。
我正在尝试编写一段代码来执行以下任务:
- 读入包含几年(1722 年到 2013 年)平均每日温度的数据文件
- 求年平均气温
- 制作这些年平均温度的直方图
- 通过此数据进行拟合,以便在给定特定年份(例如 2060 年)的情况下,您可以推断以确定该年的平均温度。
到目前为止我写的代码,包括在下面,应该能够执行任务 1 到 3。它分为四个部分:一个头文件 "sketch.h",一个 C++ 脚本 "sketch.cpp"、另一个 C++ 脚本 "project.cpp" 和一些 ROOT 绘图文件 "rootlogon.C".
文件 "sketch.h" 和 "sketch.cpp" 包含 class 的定义及其成员的实现。"project.cpp" 是实例化分析对象的文件。 "rootlogon.C" 对直方图和图形的外观有一些预定义的首选项。
想法是,如果 ROOT 运行 在包含这 4 段代码的同一文件夹中,它将自动加载它们。
sketch.h
#ifndef TEMPTRENDER_H
#define TEMPTRENDER_H
#include <string>
class tempTrender
{
public:
tempTrender(std::string filePath); //Construct using the specified filePath
//void tempPerYear(int yearToExtrapolate); //Make a histogram of average temperature per year, then fit and extrapolate to the given year
void read_datFile(); //will be used to read in the data file
private:
std::string pathToFile; //my uppsala .dat file
int Year;
int Month;
int Day;
double Temp_exp;
double Temp_corr;
int dat_ID;
std::string helpString;
std::string filePath;
int numEntries; //total number of measurements done (entries in the data set)
int totTemp; //sum of daily temperatures in a given year
int counter;
int first_Year; //the year in which the measurements began
double AvgTemp; //average annual temperature
};
#endif
sketch.cpp
#include <iostream>
#include "sketch.h"
#include <fstream>
#include <string>
#include <sstream>
//ROOT library objects
#include <TF1.h> // 1d function class
#include <TH1.h> // 1d histogram classes
#include <TH2.h> // 2d histogram classes
#include <TRandom.h> // random generators
#include <TStyle.h> // style object
#include <TMath.h> // math functions
#include <TCanvas.h> // canvas object
#include <TGraph.h> //For the graphs part
#include <TLegend.h> //for the legends
using namespace std;
tempTrender::tempTrender(string filePath)
{
std::cout << "The user supplied " << filePath << " as the path to the data file." << endl;
string pathToFile = filePath;
}
//reading in data file
void tempTrender::read_datFile()
{
ifstream datFile(filePath.c_str()); //opening data file, as a C-string for later manipulation
if(!datFile) cout<<"Error: the file could not be read"<<endl; //just checking if the file was read in successfully ;-)
int n = 0; //initialise a counter
while(getline(datFile, helpString)) n++; //counting the number of lines in the data set;
numEntries = n;
//returning to the beginning of .dat file
datFile.clear();
datFile.seekg(0, ios::beg);
for(int i=0; i<numEntries; i++)
{
datFile >> Year >> Month >> Day >> Temp_exp >> Temp_corr >> dat_ID; //putting input data into categories
std::cout<<Year<<endl; //checking that the categories contain the right data
std::cout<<"I'm alive"<<endl;
}
//calculating average temperature and creating a histogram of the values
TH1F* hAvgTemp = new TH1F("hAvgTemp", "Average Annual Temperature: 1722-2013" , 600, 1722, 2013);
while(datFile>> Year >> Month >> Day >> Temp_exp >> Temp_corr >> dat_ID)
{
while(first_Year == 1722 && Year == first_Year)
{
for(int Month=0; Month < 13; Month++) //loop over every month
{
for(int Day =0; Day < 32; Day++) //loop over every day
{
counter++; //count the number of measurements
totTemp += Temp_corr; //sum up the daily temperatures over all months of first_Year
AvgTemp = (totTemp/counter); //average temperature for first_Year. One number.
}
}
hAvgTemp->Fill(AvgTemp); //start filling a histogram with the average temperatures
cout << AvgTemp << endl; //show that the calculation worked
}
first_Year = first_Year + 1; //go to the next year and run the loop again
}
//creating canvas for hAvgTemp histogram
TCanvas* c2 = new TCanvas("c1", "hAvgTemp Canvas", 900, 600);
hAvgTemp->SetMinimum(0);
hAvgTemp->Draw();
c2->SaveAs("AvgTemp.png");
}
project.cpp
#include "sketch.h"
#include <string>
#include <iostream>
using namespace std;
void project()
{
string pathToFile = "path_to_datFile.dat"; //path to data file
tempTrender t(pathToFile); //Instantiate analysis object
//t.tempPerYear(2050);
}
rootlogon.C
void rootlogon() {
gStyle->SetOptStat(0); //Let's make our plots look a bit better. Get rid of the stat box
gStyle->SetOptTitle(0); //Get rid of the title (good plots use legends instead)
gStyle->SetTitleSize(0.05, "x"); //Use bigger text on the axes
gStyle->SetTitleSize(0.05, "y");
gStyle->SetLabelSize(0.05, "x"); //Use bigger labels too
gStyle->SetLabelSize(0.05, "y");
gStyle->SetPadTopMargin(0.05); //Change the margins to fit our new sizes
gStyle->SetPadRightMargin(0.05);
gStyle->SetPadBottomMargin(0.16);
gStyle->SetPadLeftMargin(0.16);
gROOT->ProcessLine(".L sketch.cpp+"); //Load the classes that we want to use automatically - Convenient!
gROOT->ProcessLine(".L project.cpp+"); //The + means to recompile only if it changed since last time
}
数据文件提取
我在下面包含了我正在分析的数据文件的摘录。列分别为:年、月、日、测量温度、校正温度和数据 ID。
1722 1 12 1.9 1.8 1
1722 1 13 2.3 2.2 1
1722 1 14 1.8 1.7 1
1722 1 15 .9 .8 1
1722 1 16 -1.8 -1.9 1
. . . .
2013 12 29 3.8 3.6 1
2013 12 30 1.6 1.4 1
2013 12 31 3.3 3.1 1
代码在 ROOT 中编译,但不执行任何其他操作。我已经尝试了很长时间,但没有成功,以弄清楚它出了什么问题。任何帮助将不胜感激。此外,对于如何处理任务 4 的提示也将不胜感激。我是C++新手,欢迎批评指正
要使 rootlogon.C
脚本真正执行某些操作,您需要调用一个函数。我相信您想执行名为 project()
的 "main" 函数。如果是这种情况,那么您需要将 rootlogon.C
修改为:
void rootlogon() {
// ...
gROOT->ProcessLine(".L sketch.cpp+"); //Load the classes that we want to use automatically - Convenient!
gROOT->ProcessLine(".L project.cpp+"); //The + means to recompile only if it changed since last time
project(); // <- Add this line
}
gROOT->ProcessLine(".L ...")
指令只是编译代码并加载 ROOT 中的库,但您仍然需要调用该函数。