ATLAS Offline Software
Loading...
Searching...
No Matches
HanInputRootFile.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
7#include "TClass.h"
8#include "TH1.h"
9#include "TGraph.h"
10#include "TDirectoryFile.h"
11#include "TEfficiency.h"
12
13#include "dqm_core/exceptions.h"
14
15#include <iostream>
16#include <cstring>
17
18namespace {
19
20TKey* getObjKeyFromTDir( TDirectory* dir, const std::string& path );
21TDirectory* changeInputDir( TDirectory* dir, const std::string& path );
22
23}
24
25namespace dqi {
26
28HanInputRootFile( const std::string& rootFileName, const std::string& path )
29 : dqm_core::InputRootFile::InputRootFile(rootFileName)
30 , m_basedir(0)
31 , m_histNamesBuilt(false)
32{
33 if( path != "" ) {
34 std::cout << "Using path \"" << path << "\" in input file\n";
35 std::string pathForSearch = path;
36 pathForSearch += "/dummyName";
37 m_basedir = changeInputDir( m_file.get(), pathForSearch );
38 if( m_basedir == 0 ) {
39 std::cerr << "Cannot find \"" << path << "\" in input file\n";
40 }
41 }
42
43 if( m_basedir == 0 )
44 m_basedir = m_file.get();
45}
46
47
52
53
54void
56addListener( const boost::regex& regex, dqm_core::InputListener* listener )
57{
58 // avoid building directory until actually demanded
59 if (! m_histNamesBuilt) {
61 m_histNamesBuilt = true;
62 }
63
64 for (std::vector<std::string>::const_iterator str = m_histNames.begin();
65 str != m_histNames.end();
66 ++str) {
67 try {
68 if (boost::regex_match(*str, regex)) {
69 // is this actually a histogram/graph?
70 TObject* temp = m_basedir->Get(str->c_str());
71 if (dynamic_cast<TH1*>(temp) || dynamic_cast<TGraph*>(temp) || dynamic_cast<TEfficiency*>(temp) ) {
72 std::cout << "Regular expression " << regex << " matches " << *str << std::endl;
73 addListener(*str, listener);
74 }
75 delete temp;
76 }
77 } catch (std::exception &ex) { // should be std::runtime_error, but Coverity is not happy with that
78 std::cout << "Warning: Matching of regular expression " << regex << " failed: " << ex.what() << std::endl;
79 }
80
81 }
82}
83
84
85void
87addListener( const std::vector<std::string>& names, dqm_core::InputListener* listener )
88{
89 dqm_core::InputRootFile::addListener( names, listener );
90}
91
92
93void
95addListener(const std::string& name, dqm_core::InputListener *listener)
96{
97 // TKeys are owned by the TFile
98 TKey* key = getObjKeyFromTDir( m_basedir, name );
99 if( key == 0 ) {
100 // reduce verbosity
101 //std::cerr << "Warning: Histogram \"" << name << "\" was not found. Ignoring...\n";
102 return;
103 }
104 m_monObjects.push_back( MonBundle(name,key,listener) );
105}
106
107
108} // end namespace dqi
109
110namespace {
111
112// This method is copy-pasted from InputRootFile (it was private)
113TKey* getObjKeyFromTDir( TDirectory* dir, const std::string& path ) {
114 if( dir == 0 )
115 return 0;
116
117 std::string::size_type i = path.find_first_of('/');
118 if( i != std::string::npos ) {
119 std::string dName( path, 0, i );
120 std::string pName( path, i+1, std::string::npos );
121 if( dName != "" ) {
122 TDirectory* subDir = dir->GetDirectory(dName.c_str());
123 if (subDir != dir) {
124 return getObjKeyFromTDir( subDir, pName );
125 }
126 // TKey* key = dir->FindKey( dName.c_str() );
127 // if( key != 0 ) {
128 // TObject* obj = key->ReadObj();
129 // TDirectory* subDir = dynamic_cast<TDirectory*>( obj );
130 // TKey* rv = getObjKeyFromTDir( subDir, pName );
131 // delete obj;
132 // return rv;
133 // }
134 return 0;
135 }
136 return getObjKeyFromTDir( dir, pName );
137 }
138
139 return dir->FindKey( path.c_str() );
140}
141
142
143TDirectory* changeInputDir( TDirectory* dir, const std::string& path )
144{
145 if( dir == 0 )
146 return 0;
147
148 std::string::size_type i = path.find_first_of('/');
149 if( i != std::string::npos ) {
150 std::string dName( path, 0, i );
151 std::string pName( path, i+1, std::string::npos );
152 if( dName != "" ) {
153 TDirectory* subDir = dir->GetDirectory(dName.c_str());
154 if (subDir != dir) {
155 return changeInputDir( subDir, pName );
156 }
157 return 0;
158 // TDirectory* subDir(0);
159 // TKey* key = dir->FindKey( dName.c_str() );
160 // if( key != 0 ) {
161 // subDir = dynamic_cast<TDirectory*>( key->ReadObj() );
162 // }
163 // else {
164 // return 0;
165 // }
166 // return changeInputDir( subDir, pName );
167 }
168 return changeInputDir( dir, pName );
169 }
170
171 return dir;
172}
173
174} // end unnamed namespace
HanInputRootFile(const std::string &rootFileName, const std::string &path="")
virtual void addListener(const boost::regex &regex, dqm_core::InputListener *listener) override
std::vector< std::string > m_histNames
path
python interpreter configuration --------------------------------------—
Definition athena.py:128
void dolsr(const TDirectory *dir, std::vector< std::string > &hists, const TDirectory *topdir=nullptr)
Definition HanUtils.cxx:80