ATLAS Offline Software
getTree.cxx
Go to the documentation of this file.
1 /*
2 Copyright (C) 2002-2017 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 = key->GetName();
66  file->Close();
67  return name;
68  }
69 
70 }
covarianceTool.prob
prob
Definition: covarianceTool.py:678
H5Utils::getTree
std::string getTree(const std::string &file_name)
Definition: getTree.cxx:36
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
physics_parameters.file_name
string file_name
Definition: physics_parameters.py:32
file
TFile * file
Definition: tile_monitor.h:29
H5Utils
HDF5 Tuple Writer.
Definition: common.h:20
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
getTree.h
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
python.dummyaccess.exists
def exists(filename)
Definition: dummyaccess.py:9
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37