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>
13 
14 namespace dqi {
15 
17 
18 TSeqCollection* newTList( const char *name, TObject *obj )
19 {
20  TList *ret = new TList();
21  ret->SetName(name);
22  if (obj != 0)
23  ret->Add(obj);
24  return ret;
25 }
26 
27 TSeqCollection* newTObjArray( const char *name, TObject *obj, Int_t size )
28 {
29  TObjArray *ret = new TObjArray(size);
30  ret->SetName(name);
31  if (obj != NULL)
32  ret->Add(obj);
33  return ret;
34 }
35 
36 TKey* getObjKey( TDirectory* dir, const std::string& path )
37 {
38  if( dir == 0 )
39  return 0;
40 
41  std::string::size_type i = path.find_first_of('/');
42  if( i != std::string::npos ) {
43  std::string dName( path, 0, i );
44  std::string pName( path, i+1, std::string::npos );
45  if( dName != "" ) {
46  TDirectory* subDir = dir->GetDirectory(dName.c_str());
47  if (subDir != 0) {
48  TKey* rv = getObjKey( subDir, pName );
49  //delete subDir;
50  return rv;
51  }
52  // TKey* key = dir->FindKey( dName.c_str() );
53  // if( key != 0 ) {
54  // TDirectory* subDir = dynamic_cast<TDirectory*>( key->ReadObj() );
55  // TKey* rv = getObjKey( subDir, pName );
56  // // delete subDir;
57  // return rv;
58  // }
59  return 0;
60  }
61  return getObjKey( dir, pName );
62  }
63 
64  return dir->FindKey( path.c_str() );
65 }
66 
68  m_lock(root_mutex),
69  m_useRecursiveDelete(gROOT->MustClean())
70 {
71  gROOT->SetMustClean(false);
72 }
73 
75 {
76  gROOT->SetMustClean(m_useRecursiveDelete);
77 }
78 
79 void
80 dolsr(const TDirectory* dir, std::vector<std::string>& hists, const TDirectory* topdir)
81 {
82  // permit calling with two arguments
83  if (topdir == NULL) {
84  topdir = dir;
85  }
86  TIter keys(dir->GetListOfKeys());
87  TKey* key;
88  std::string fullpath(dir->GetPath());
89  std::string toppath(topdir->GetPath());
90  std::string::size_type toppathlen = toppath.length();
91  while ((key = dynamic_cast<TKey*>(keys())) != NULL) {
92  if (std::string(key->GetClassName()).starts_with( "TDirectory")) {
93  TDirectory* newdir = dynamic_cast<TDirectory*>(key->ReadObj());
94  if (!newdir) {
95  std::cerr << "WARNING: cannot read directory "
96  << fullpath << "/" << key->GetName()
97  << "; skipping" << std::endl;
98  } else {
99  dolsr(newdir, hists, topdir);
100  }
101  delete newdir;
102  } else {
103  if (std::string(key->GetName()) == "metadata") {
104  continue;
105  }
106  std::string path;
107  if (fullpath.substr(0, toppathlen) == toppath) {
108  int extra = 1;
109  if (toppath[toppathlen-1] == '/') extra = 0;
110  path = fullpath.substr(toppathlen + extra, std::string::npos);
111  } else {
112  path = fullpath;
113  }
114  hists.push_back(path+"/"+std::string(key->GetName()));
115  }
116  }
117 }
118 
120 HanHistogramLink(TDirectory* dir, const std::string& path)
121  : m_dir(dir)
122  , m_path(path)
123 {}
124 
126 getObject()
127 {
128  //std::cout << "Getting " << m_dir->GetPath() << " " << m_path << std::endl;
129  return m_dir->Get(m_path.c_str());
130  /* std::unique_ptr<TKey> key(getObjKey(m_dir, m_path));
131  if (key.get() == 0) return 0;
132  TObject* rv = key->ReadObj();
133  if (TH1* hptr = dynamic_cast<TH1*>(rv)) { hptr->SetDirectory(0); }
134  return rv; */
135 }
136 
137 }
dqi::dolsr
void dolsr(const TDirectory *dir, std::vector< std::string > &hists, const TDirectory *topdir=nullptr)
Definition: HanUtils.cxx:80
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:128
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
dqi::root_mutex
std::mutex root_mutex
Definition: HanUtils.cxx:16
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
lumiFormat.i
int i
Definition: lumiFormat.py:85
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:67
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:221
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:18
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
dqi::newTObjArray
TSeqCollection * newTObjArray(const char *name, TObject *obj=0, Int_t size=TCollection::kInitCapacity)
Definition: HanUtils.cxx:27
python.PyAthena.obj
obj
Definition: PyAthena.py:132
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:36
dqi::DisableMustClean::~DisableMustClean
~DisableMustClean()
Definition: HanUtils.cxx:74
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37