ATLAS Offline Software
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 
18 namespace {
19 
20 TKey* getObjKeyFromTDir( TDirectory* dir, const std::string& path );
21 TDirectory* changeInputDir( TDirectory* dir, const std::string& path );
22 
23 }
24 
25 namespace dqi {
26 
28 HanInputRootFile( 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 
50 {
51 }
52 
53 
54 void
56 addListener( 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 
85 void
87 addListener( const std::vector<std::string>& names, dqm_core::InputListener* listener )
88 {
89  dqm_core::InputRootFile::addListener( names, listener );
90 }
91 
92 
93 void
95 addListener(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 
110 namespace {
111 
112 // This method is copy-pasted from InputRootFile (it was private)
113 TKey* 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 
143 TDirectory* 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
dqi::dolsr
void dolsr(const TDirectory *dir, std::vector< std::string > &hists, const TDirectory *topdir=nullptr)
Definition: HanUtils.cxx:81
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:126
dqi::HanInputRootFile::HanInputRootFile
HanInputRootFile(const std::string &rootFileName, const std::string &path="")
Definition: HanInputRootFile.cxx:28
dqm_core
Definition: HanConfig.h:31
m_file
std::unique_ptr< TFile > m_file
description: this is a custom writer for the old-school drivers that don't use an actual writer
Definition: OutputStreamData.cxx:52
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
dqi::HanInputRootFile::m_histNames
std::vector< std::string > m_histNames
Definition: HanInputRootFile.h:40
HanInputRootFile.h
lumiFormat.i
int i
Definition: lumiFormat.py:92
HanUtils.h
python.subdetectors.mmg.names
names
Definition: mmg.py:8
dqi::HanInputRootFile::~HanInputRootFile
~HanInputRootFile()
Definition: HanInputRootFile.cxx:49
calibdata.exception
exception
Definition: calibdata.py:496
beamspotman.dir
string dir
Definition: beamspotman.py:623
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
dqi::HanInputRootFile::addListener
virtual void addListener(const boost::regex &regex, dqm_core::InputListener *listener) override
Definition: HanInputRootFile.cxx:56
TH1
Definition: rootspy.cxx:268
dqi::HanInputRootFile::m_basedir
TDirectory * m_basedir
Definition: HanInputRootFile.h:38
str
Definition: BTagTrackIpAccessor.cxx:11
dqi::HanInputRootFile::m_histNamesBuilt
bool m_histNamesBuilt
Definition: HanInputRootFile.h:41
dqi
Definition: CompositeAlgorithm.h:16
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37