ATLAS Offline Software
Loading...
Searching...
No Matches
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
14namespace dqi {
15
16std::mutex root_mutex;
17
18TSeqCollection* 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
27TSeqCollection* 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
36TKey* 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
69 m_useRecursiveDelete(gROOT->MustClean())
70{
71 gROOT->SetMustClean(false);
72}
73
78
79void
80dolsr(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
120HanHistogramLink(TDirectory* dir, const std::string& path)
121 : m_dir(dir)
122 , m_path(path)
123{}
124
126getObject()
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}
std::unique_lock< std::mutex > m_lock
Definition HanUtils.h:35
TSeqCollection * newTObjArray(const char *name, TObject *obj=0, Int_t size=TCollection::kInitCapacity)
Definition HanUtils.cxx:27
std::mutex root_mutex
Definition HanUtils.cxx:16
void dolsr(const TDirectory *dir, std::vector< std::string > &hists, const TDirectory *topdir=nullptr)
Definition HanUtils.cxx:80
TKey * getObjKey(TDirectory *dir, const std::string &path)
Definition HanUtils.cxx:36
TSeqCollection * newTList(const char *name, TObject *obj=0)
Definition HanUtils.cxx:18