ATLAS Offline Software
HanUtils.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "TList.h"
6 #include "TObjArray.h"
7 #include "TKey.h"
8 #include "TDirectory.h"
9 #include "TROOT.h"
10 #include <string>
11 #include <iostream>
12 #include "CxxUtils/starts_with.h"
14 
15 namespace dqi {
16 
18 
19 TSeqCollection* newTList( const char *name, TObject *obj )
20 {
21  TList *ret = new TList();
22  ret->SetName(name);
23  if (obj != 0)
24  ret->Add(obj);
25  return ret;
26 }
27 
28 TSeqCollection* newTObjArray( const char *name, TObject *obj, Int_t size )
29 {
30  TObjArray *ret = new TObjArray(size);
31  ret->SetName(name);
32  if (obj != NULL)
33  ret->Add(obj);
34  return ret;
35 }
36 
37 TKey* getObjKey( TDirectory* dir, const std::string& path )
38 {
39  if( dir == 0 )
40  return 0;
41 
42  std::string::size_type i = path.find_first_of('/');
43  if( i != std::string::npos ) {
44  std::string dName( path, 0, i );
45  std::string pName( path, i+1, std::string::npos );
46  if( dName != "" ) {
47  TDirectory* subDir = dir->GetDirectory(dName.c_str());
48  if (subDir != 0) {
49  TKey* rv = getObjKey( subDir, pName );
50  //delete subDir;
51  return rv;
52  }
53  // TKey* key = dir->FindKey( dName.c_str() );
54  // if( key != 0 ) {
55  // TDirectory* subDir = dynamic_cast<TDirectory*>( key->ReadObj() );
56  // TKey* rv = getObjKey( subDir, pName );
57  // // delete subDir;
58  // return rv;
59  // }
60  return 0;
61  }
62  return getObjKey( dir, pName );
63  }
64 
65  return dir->FindKey( path.c_str() );
66 }
67 
69  m_lock(root_mutex),
70  m_useRecursiveDelete(gROOT->MustClean())
71 {
72  gROOT->SetMustClean(false);
73 }
74 
76 {
77  gROOT->SetMustClean(m_useRecursiveDelete);
78 }
79 
80 void
81 dolsr(const TDirectory* dir, std::vector<std::string>& hists, const TDirectory* topdir)
82 {
83  // permit calling with two arguments
84  if (topdir == NULL) {
85  topdir = dir;
86  }
87  TIter keys(dir->GetListOfKeys());
88  TKey* key;
89  std::string fullpath(dir->GetPath());
90  std::string toppath(topdir->GetPath());
91  std::string::size_type toppathlen = toppath.length();
92  while ((key = dynamic_cast<TKey*>(keys())) != NULL) {
93  if (CxxUtils::starts_with(key->GetClassName(), "TDirectory")) {
94  TDirectory* newdir = dynamic_cast<TDirectory*>(key->ReadObj());
95  if (!newdir) {
96  std::cerr << "WARNING: cannot read directory "
97  << fullpath << "/" << key->GetName()
98  << "; skipping" << std::endl;
99  } else {
100  dolsr(newdir, hists, topdir);
101  }
102  delete newdir;
103  } else {
104  if (std::string(key->GetName()) == "metadata") {
105  continue;
106  }
107  std::string path;
108  if (fullpath.substr(0, toppathlen) == toppath) {
109  int extra = 1;
110  if (toppath[toppathlen-1] == '/') extra = 0;
111  path = fullpath.substr(toppathlen + extra, std::string::npos);
112  } else {
113  path = fullpath;
114  }
115  hists.push_back(path+"/"+std::string(key->GetName()));
116  }
117  }
118 }
119 
121 HanHistogramLink(TDirectory* dir, const std::string& path)
122  : m_dir(dir)
123  , m_path(path)
124 {}
125 
127 getObject()
128 {
129  //std::cout << "Getting " << m_dir->GetPath() << " " << m_path << std::endl;
130  return m_dir->Get(m_path.c_str());
131  /* std::unique_ptr<TKey> key(getObjKey(m_dir, m_path));
132  if (key.get() == 0) return 0;
133  TObject* rv = key->ReadObj();
134  if (TH1* hptr = dynamic_cast<TH1*>(rv)) { hptr->SetDirectory(0); }
135  return rv; */
136 }
137 
138 }
dqi::dolsr
void dolsr(const TDirectory *dir, std::vector< std::string > &hists, const TDirectory *topdir=nullptr)
Definition: HanUtils.cxx:81
CxxUtils::starts_with
bool starts_with(const char *s, const char *prefix)
Test whether one null-terminated byte string starts with another.
m_dir
TDirectory & m_dir
The directory we need to return to.
Definition: OutputStreamData.cxx:41
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:126
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
dqi::root_mutex
std::mutex root_mutex
Definition: HanUtils.cxx:17
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
lumiFormat.i
int i
Definition: lumiFormat.py:92
ret
T ret(T t)
Definition: rootspy.cxx:260
HanUtils.h
m_path
std::string m_path
the path being used
Definition: OutputStreamData.cxx:88
dqi::DisableMustClean::m_useRecursiveDelete
bool m_useRecursiveDelete
Definition: HanUtils.h:36
MakeTH3DFromTH2Ds.hists
hists
Definition: MakeTH3DFromTH2Ds.py:72
dqi::DisableMustClean::DisableMustClean
DisableMustClean()
Definition: HanUtils.cxx:68
starts_with.h
C++20-like starts_with/ends_with for strings.
beamspotman.dir
string dir
Definition: beamspotman.py:623
python.handimod.extra
int extra
Definition: handimod.py:522
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
dq_make_web_display.rv
def rv
Definition: dq_make_web_display.py:219
dqi::newTList
TSeqCollection * newTList(const char *name, TObject *obj=0)
Definition: HanUtils.cxx:19
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:790
dqi::newTObjArray
TSeqCollection * newTObjArray(const char *name, TObject *obj=0, Int_t size=TCollection::kInitCapacity)
Definition: HanUtils.cxx:28
python.PyAthena.obj
obj
Definition: PyAthena.py:135
dqi
Definition: CompositeAlgorithm.h:16
beamspotman.fullpath
string fullpath
Definition: beamspotman.py:1039
dqi::getObjKey
TKey * getObjKey(TDirectory *dir, const std::string &path)
Definition: HanUtils.cxx:37
dqi::DisableMustClean::~DisableMustClean
~DisableMustClean()
Definition: HanUtils.cxx:75
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37