ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
PhysicsAnalysis
AnalysisCommon
HDF5Utils
util
getTree.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3
*/
4
5
#include "
getTree.h
"
6
7
#include "TFile.h"
8
#include <stdexcept>
9
#include <set>
10
#include <fstream>
11
#include <regex>
12
#include <memory>
13
14
#include "TKey.h"
15
16
namespace
{
17
bool
is_remote(
const
std::string& file_name) {
18
std::regex remote_check(
"[a-z]+:.*"
);
19
if
(std::regex_match(file_name, remote_check)) {
20
return
true
;
21
}
22
return
false
;
23
}
24
bool
exists
(
const
std::string& file_name) {
25
std::ifstream
file
(
file_name
.c_str(), std::ios::binary);
26
if
(!
file
) {
27
file
.close();
28
return
false
;
29
}
30
file
.close();
31
return
true
;
32
}
33
}
34
35
namespace
H5Utils
{
36
std::string
getTree
(
const
std::string& file_name) {
37
if
(!
exists
(file_name) && !is_remote(file_name)) {
38
throw
std::logic_error(file_name +
" doesn't exist"
);
39
}
40
std::unique_ptr<TFile>
file
(TFile::Open(file_name.c_str()));
41
if
(!
file
|| !
file
->IsOpen() ||
file
->IsZombie()) {
42
throw
std::logic_error(
"can't open "
+ file_name);
43
}
44
std::set<std::string> keys;
45
int
n_keys =
file
->GetListOfKeys()->GetSize();
46
if
(n_keys == 0) {
47
throw
std::logic_error(
"no keys found in file"
);
48
}
49
for
(
int
keyn = 0; keyn < n_keys; keyn++) {
50
keys.insert(
file
->GetListOfKeys()->At(keyn)->GetName());
51
}
52
size_t
n_unique = keys.size();
53
if
(n_unique > 1) {
54
std::string prob =
"Can't decide which tree to use, choose one of {"
;
55
size_t
uniq_n = 0;
56
for
(
const
auto
& key: keys) {
57
prob.append(key);
58
uniq_n++;
59
if
(uniq_n < n_unique) prob.append(
", "
);
60
}
61
prob.append(
"} with the --tree-name option"
);
62
throw
std::logic_error(prob);
63
}
64
auto
* key =
dynamic_cast<
TKey*
>
(
file
->GetListOfKeys()->At(0));
65
std::string name;
66
if
(key){
67
name = key->GetName();
68
}
69
file
->Close();
70
return
name;
71
}
72
73
}
exists
bool exists(const std::string &filename)
does a file exist
Definition
computils.cxx:256
getTree.h
H5Utils
HDF5 Tuple Writer.
Definition
CompressedTypes.h:16
H5Utils::getTree
std::string getTree(const std::string &file_name)
Definition
getTree.cxx:36
physics_parameters.file_name
str file_name
Definition
physics_parameters.py:32
file
TFile * file
Definition
tile_monitor.h:29
Generated on
for ATLAS Offline Software by
1.17.0