-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThrowIO.cxx
113 lines (97 loc) · 2.53 KB
/
ThrowIO.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* \file ThrowIO.cxx
* \brief Implementation of I/O related functions.
*/
// std
#include <string>
#include <fstream>
// Root
#include <TFile.h>
// Throw
#include "Throw.h"
/**
* \ingroup IO
* \brief Checks whether file exists.
*
* \param filePath specifies location of the file.
*/
bool Throw::FileExists(const std::string& filePath) {
if (FILE *file = fopen(filePath.c_str(), "r")) {
fclose(file);
return true;
} else {
return false;
}
}
/**
* \ingroup IO
* \brief Prints out histogram values to a file.
*
* \param hist histogram to be printed out.
* \param filePath specifies location of the file.
*/
void Throw::PrintHist(TH1D* hist, const std::string& filePath) {
std::ofstream outFile;
outFile.open(filePath);
outFile << hist->GetName() << std::endl;
outFile << "x\ty\tx_err\ty_err" << std::endl;
for (int i = 1; i <= hist->GetXaxis()->GetNbins(); ++i) {
outFile << hist->GetBinCenter(i) << "\t";
outFile << hist->GetBinContent(i) << "\t";
outFile << hist->GetBinWidth(i) << "\t";
outFile << hist->GetBinError(i) << std::endl;
}
return;
}
/**
* \ingroup IO
* \brief Quickly output an object to a root file.
*
* The root file will be created in the location of the executable and root file
* name will be inherited from the name of the object.
*/
void Throw::QuickOut(TObject* object) {
QuickOut(object, "", "");
}
/**
* \ingroup IO
* \brief Quickly output an object to a root file.
*
* \param path root file path without root file name.
*
* The name of the file will inherited from the object name.
*/
void Throw::QuickOut(TObject* object, const std::string& path) {
QuickOut(object, path, "");
}
/**
* \ingroup IO
* \brief Quickly output an object to a root file.
*
* \param path root file path without root file name.
* \param name object name.
*
* The name of the file will inherited from the object name.
*/
void Throw::QuickOut(TObject* object,
const std::string& path,
const std::string& name) {
std::string objectPath = object->GetName();
objectPath = "./" + objectPath;
if (!path.empty()) {
objectPath = path;
}
std::string objectName = object->GetName();
if (!name.empty()) {
objectName = name;
}
system(("mkdir -p " + objectPath).c_str());
TFile* outFile = new TFile((objectPath + "/" + objectName + ".root").c_str(),
"RECREATE");
if (outFile->IsOpen()) {
object->Write(objectName.c_str());
outFile->Write();
outFile->Close();
}
delete outFile;
}