-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathefficiency.cxx
149 lines (127 loc) · 4.77 KB
/
efficiency.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Get Efficiency for FEP, SEP and DEP from root files created by Geant4
* Filenames are supposed to be formatted like <energy>.root (e.g. 100.root)
* Note: In a root macro, the function with the same name as the file will be executed
* usage: $ root -l -b -q 'effi.cxx("/scratch/jmayer/G4")'
* --> for ROOT Version 6 or higher <--
*/
int FileSize(const char *name)
{
int size = 0;
if (FILE *file = fopen(name, "r")) {
fseek(file, 0, SEEK_END);
size = ftell(file);
fclose(file);
}
return size;
}
inline double PeakVolume(const TH1D &th, const int &pos)
{
int width = 2;
return 2. * th.Integral(pos - width, pos + width) - 1. * th.Integral(pos - 2 * width, pos + 2 * width);
}
inline double Efficiency(const TH1D &th, const int &pos, const int &off)
{
if (off > pos) {
return 0;
} else {
return PeakVolume(th, pos - off) / (th.GetEntries() * 1.);
}
}
std::vector<TH1D> SpectraFromFile(const TString &directory, const TString &file)
{
std::vector<TH1D> spectra;
TFile *f = TFile::Open(directory + file);
f->cd("histograms");
TDirectory *histdir = gDirectory;
TKey *key;
TIter nextkey(histdir->GetListOfKeys());
while ((key = (TKey *)nextkey())) {
if (gROOT->GetClass(key->GetClassName())->InheritsFrom(TH1D::Class())) {
spectra.push_back(*dynamic_cast<TH1D *>(key->ReadObj()));
}
}
return spectra;
}
std::vector<TString> GeBGOPairNumbers(const std::vector<TString> &in)
{
std::vector<TString> out;
for (const TString &l1 : in) {
if (l1.BeginsWith("Ge")) {
for (const TString &l2 : in) {
if (l2.BeginsWith("BGO") && TString(l2(3, 2)).EqualTo(TString(l1(2, 2)))) {
out.push_back(TString(l2(3, 2)));
}
}
}
}
return out;
}
std::vector<TString> LeavesInTChain(TChain *t)
{
std::vector<TString> leafnames;
TLeaf *obj;
TObjArrayIter nextleaf(t->GetListOfLeaves());
while ((obj = (TLeaf *)nextleaf())) {
leafnames.push_back(TString(obj->GetName()));
}
return leafnames;
}
std::vector<TH1D> VetoedSpectraFromTChain(const TString &directory, const TString &file)
{
std::vector<TH1D> spectra;
// TODO: Replace cstr this with someting better
char cstr[80];
sprintf(cstr, "%s%d_t*.root", directory.Data(), file.Atoi());
TChain *t = new TChain("ntuple/Horus");
t->CanDeleteRefs(false);
t->Add(cstr);
if (t->GetNbranches() > 0) {
for (const TString &leaf : GeBGOPairNumbers(LeavesInTChain(t))) {
TString name = "V" + leaf;
TH1D *h = new TH1D(name, name, 20000, 0, 20000);
// Project TChain Data to spectrum with name name, leaf GeXX with energy in keV, if the corresponding BGO has no deposited energy
t->Project(name, "Ge" + leaf + "*1000.", "BGO" + leaf + "==0");
spectra.push_back(*h);
delete h;
}
}
return spectra;
}
std::vector<TString> EfficiencyFilesIn(const TString &dirname)
{
const TString ext = ".root";
std::vector<TString> m;
TSystemDirectory dir(dirname, dirname);
TList *files = dir.GetListOfFiles();
if (files) {
TSystemFile *file;
TString fname;
TIter next(files);
while ((file = (TSystemFile *)next())) {
fname = file->GetName();
// Check if file is not a directory, is a root file, is not a partial (_t) root file, and is of resonable size (== finished)
if (!file->IsDirectory() && fname.EndsWith(ext) && !fname.SubString("_t") && FileSize(dirname + fname) > 20000) {
m.push_back(fname);
}
}
}
std::sort(m.begin(), m.end(), [](const TString &a, const TString &b) -> bool { return a.Atoi() < b.Atoi(); });
return m;
}
void efficiency(const TString &directory)
{
const auto files = EfficiencyFilesIn(directory);
for (const auto &file : files) {
int energy = file.Atoi();
cout << "# Data for E = " << energy << " keV from file " << file << ":" << endl;
auto spectra = SpectraFromFile(directory, file);
for (const TH1D &h : spectra) {
cout << h.GetName() << "\t" << energy << "\t" << Efficiency(h, energy, 0) << "\t" << Efficiency(h, energy, 511) << "\t" << Efficiency(h, energy, 2 * 511) << endl;
}
auto vetoedspectra = VetoedSpectraFromTChain(directory, file);
for (const TH1D &h : vetoedspectra) {
cout << h.GetName() << "\t" << energy << "\t" << Efficiency(h, energy, 0) << "\t" << Efficiency(h, energy, 511) << "\t" << Efficiency(h, energy, 2 * 511) << endl;
}
}
}