ATLAS Offline Software
Loading...
Searching...
No Matches
TFileLooper Class Reference

Base class to loop over all the keys in a ROOT file. More...

#include <TFileLooper.h>

Inheritance diagram for TFileLooper:
Collaboration diagram for TFileLooper:

Public Member Functions

 TFileLooper ()=default
virtual ~TFileLooper ()=default
 TFileLooper (const TFileLooper &other)=delete
TFileLooperoperator= (const TFileLooper &other)=delete
virtual Int_t run (const char *filename, const char *rootDir=0)
 Start processing.
virtual void processKey (TDirectory &dir, TKey &key)
 Method called for every key.

Processing hooks

TFile * m_file {nullptr}
TString m_rootDir
TString m_skipDirs
Bool_t m_verbose {kFALSE}
Bool_t m_passBeforeFail {kFALSE}
Int_t m_errorCode {0}
std::vector< TPRegexp > m_failRE
std::vector< TPRegexp > m_passRE
std::vector< std::string > m_skippedObjects
virtual void beginJob ()
virtual void endJob ()
virtual void beforeFile ()
virtual void afterFile ()
virtual void beforeDir ()
virtual void afterDir ()
virtual void beforeObj ()
virtual void afterObj ()
void setDirsToSkip (const char *skipDirs)
 Skip this comma separated list of directories.
void addFailRegexp (const char *regexp)
 Skip keys that match this regexp.
void addPassRegexp (const char *regexp)
 Never skip keys that match this regexp.
void passBeforeFailRegexp (Bool_t passBeforeFail=kTRUE)
 Reverse pass/fail logic. First check on pass then on fail.
void setVerbose (Bool_t verbose=kTRUE)
 Set verbose mode.
Bool_t verbose () const
 Query verbose mode.
const TFile * file () const
 Current ROOT file.
TString rootDir () const
 Current directory.
TString getPathFromDir (const TDirectory &dir)
TString getKeyPath (const TDirectory &dir, const TKey &key)
void processDir (TDirectory &dir)
void processFile (const char *filename, const char *rootDir=0)
Bool_t skipDir (const TDirectory &dir)
Bool_t skipObject (const char *name)
TString getDrawOptions (const TH1 &h)

Detailed Description

Base class to loop over all the keys in a ROOT file.

Author
Frank Winklmeier

Loop over all the keys in a given ROOT file. Apply regular expressions to the keys to relevant keys. The action for each key needs to be implemented in a derived class.

Definition at line 35 of file TFileLooper.h.

Constructor & Destructor Documentation

◆ TFileLooper() [1/2]

TFileLooper::TFileLooper ( )
default

◆ ~TFileLooper()

virtual TFileLooper::~TFileLooper ( )
virtualdefault

◆ TFileLooper() [2/2]

TFileLooper::TFileLooper ( const TFileLooper & other)
delete

Member Function Documentation

◆ addFailRegexp()

void TFileLooper::addFailRegexp ( const char * regexp)

Skip keys that match this regexp.

Definition at line 160 of file TFileLooper.cxx.

161{
162 if (regexp) {
163 m_failRE.emplace_back(regexp);
164 }
165}
std::vector< TPRegexp > m_failRE
Definition TFileLooper.h:96

◆ addPassRegexp()

void TFileLooper::addPassRegexp ( const char * regexp)

Never skip keys that match this regexp.

Definition at line 169 of file TFileLooper.cxx.

170{
171 if (regexp) {
172 m_passRE.emplace_back(regexp);
173 }
174}
std::vector< TPRegexp > m_passRE
Definition TFileLooper.h:97

◆ afterDir()

virtual void TFileLooper::afterDir ( )
inlinevirtual

Definition at line 59 of file TFileLooper.h.

59{}

◆ afterFile()

virtual void TFileLooper::afterFile ( )
inlinevirtual

Definition at line 57 of file TFileLooper.h.

57{}

◆ afterObj()

virtual void TFileLooper::afterObj ( )
inlinevirtual

Definition at line 61 of file TFileLooper.h.

61{}

◆ beforeDir()

virtual void TFileLooper::beforeDir ( )
inlinevirtual

Definition at line 58 of file TFileLooper.h.

58{}

◆ beforeFile()

virtual void TFileLooper::beforeFile ( )
inlinevirtual

Definition at line 56 of file TFileLooper.h.

56{}

◆ beforeObj()

virtual void TFileLooper::beforeObj ( )
inlinevirtual

Definition at line 60 of file TFileLooper.h.

60{}

◆ beginJob()

virtual void TFileLooper::beginJob ( )
inlinevirtual

Reimplemented in TRootCompare.

Definition at line 54 of file TFileLooper.h.

54{}

◆ endJob()

virtual void TFileLooper::endJob ( )
inlinevirtual

Reimplemented in TRootCompare.

Definition at line 55 of file TFileLooper.h.

55{}

◆ file()

const TFile * TFileLooper::file ( ) const
inline

Current ROOT file.

Definition at line 83 of file TFileLooper.h.

83{ return m_file; }
TFile * m_file
Definition TFileLooper.h:89

◆ getDrawOptions()

TString TFileLooper::getDrawOptions ( const TH1 & h)
protected

◆ getKeyPath()

TString TFileLooper::getKeyPath ( const TDirectory & dir,
const TKey & key )
protected

Definition at line 188 of file TFileLooper.cxx.

189{
190 TString s(dir.GetPath()+TString("/")+key.GetName());
191 // Remove all chars from beginning including ":/"
192 s.Replace(0,s.First(":")+2,0);
193
194 return s;
195}

◆ getPathFromDir()

TString TFileLooper::getPathFromDir ( const TDirectory & dir)
protected

Definition at line 178 of file TFileLooper.cxx.

179{
180 TString s(dir.GetPath());
181 if (s.Index("../")==0) s.Remove(0,3);
182 s.ReplaceAll(":","");
183 return s;
184}

◆ operator=()

TFileLooper & TFileLooper::operator= ( const TFileLooper & other)
delete

◆ passBeforeFailRegexp()

void TFileLooper::passBeforeFailRegexp ( Bool_t passBeforeFail = kTRUE)
inline

Reverse pass/fail logic. First check on pass then on fail.

Definition at line 74 of file TFileLooper.h.

74{ m_passBeforeFail = passBeforeFail; }
Bool_t m_passBeforeFail
Definition TFileLooper.h:93

◆ processDir()

void TFileLooper::processDir ( TDirectory & dir)
protected

Definition at line 73 of file TFileLooper.cxx.

74{
75 TString s(getPathFromDir(dir));
76
77 if (skipDir(dir)) {
78 cout << "Skipping " << s << endl;
79 return;
80 }
81
82 if (m_verbose) cout << "Reading directory "<< dir.GetPath() << endl;
83
84 // Sort directory content
85 TList* dirList = dir.GetListOfKeys();
86 dirList->Sort();
87
88 TIter next(dirList);
89 TKey* key;
90 while ((key = (TKey*)next())) {
91 // if (skipObject(getKeyPath(dir,*key))) continue;
92
93 TString className(key->GetClassName());
94 if (className=="TDirectoryFile" || className=="TDirectory") {
95 dir.cd(key->GetName());
96 beforeDir();
97 processDir(*gDirectory);
98 afterDir();
99 }
100 else {
101 if (skipObject(getKeyPath(dir,*key))) {
102 m_skippedObjects.push_back(getKeyPath(dir,*key).Data());
103 continue;
104 }
105 processKey(dir, *key);
106 }
107 }
108}
@ Data
Definition BaseObject.h:11
std::vector< std::string > m_skippedObjects
Definition TFileLooper.h:99
void processDir(TDirectory &dir)
TString getKeyPath(const TDirectory &dir, const TKey &key)
Bool_t m_verbose
Definition TFileLooper.h:92
virtual void beforeDir()
Definition TFileLooper.h:58
Bool_t skipObject(const char *name)
Bool_t skipDir(const TDirectory &dir)
TString getPathFromDir(const TDirectory &dir)
virtual void afterDir()
Definition TFileLooper.h:59
virtual void processKey(TDirectory &dir, TKey &key)
Method called for every key.

◆ processFile()

void TFileLooper::processFile ( const char * filename,
const char * rootDir = 0 )
protected

Definition at line 44 of file TFileLooper.cxx.

45{
46 m_file = new TFile(filename);
47 if (m_file->IsZombie()) {
48 cout << "Cannot open "<<filename << endl;
49 m_errorCode = 1;
50 delete m_file;
51 return;
52 }
53
54 if (rootDir) {
55 if (!m_file->cd(rootDir)) {
56 cout << "Cannot change to directory " << rootDir << endl;
57 m_errorCode = 1;
58 return;
59 }
61 }
62 else m_file->cd();
63
64 beforeFile();
65 processDir(*gDirectory);
66 afterFile();
67
68 delete m_file; // calls Close()
69}
virtual void afterFile()
Definition TFileLooper.h:57
TString m_rootDir
Definition TFileLooper.h:90
TString rootDir() const
Current directory.
Definition TFileLooper.h:86
virtual void beforeFile()
Definition TFileLooper.h:56
Int_t m_errorCode
Definition TFileLooper.h:94

◆ processKey()

void TFileLooper::processKey ( TDirectory & dir,
TKey & key )
virtual

Method called for every key.

Only dummy implementation. Needs to be implemented in derived classes.

Reimplemented in TRootCompare.

Definition at line 110 of file TFileLooper.cxx.

111{
112 cout << "--- processKey: " << key.GetName()
113 << " in directory " << dir.GetPath() << endl;
114}

◆ rootDir()

TString TFileLooper::rootDir ( ) const
inline

Current directory.

Definition at line 86 of file TFileLooper.h.

86{ return m_rootDir; }

◆ run()

Int_t TFileLooper::run ( const char * filename,
const char * rootDir = 0 )
virtual

Start processing.

Reimplemented in TMultiFileLooper.

Definition at line 26 of file TFileLooper.cxx.

27{
28 m_errorCode = 0;
29
30 if (filename==0) {
31 cout << "Invalid file name (0)" << endl;
32 m_errorCode = 1;
33 return m_errorCode;
34 }
35
36 beginJob();
37 processFile(filename,rootDir);
38 endJob();
39 return m_errorCode;
40}
virtual void beginJob()
Definition TFileLooper.h:54
virtual void endJob()
Definition TFileLooper.h:55
void processFile(const char *filename, const char *rootDir=0)

◆ setDirsToSkip()

void TFileLooper::setDirsToSkip ( const char * skipDirs)
inline

Skip this comma separated list of directories.

Definition at line 65 of file TFileLooper.h.

65{if (skipDirs) m_skipDirs = skipDirs;}
TString m_skipDirs
Definition TFileLooper.h:91

◆ setVerbose()

void TFileLooper::setVerbose ( Bool_t verbose = kTRUE)
inline

Set verbose mode.

Definition at line 77 of file TFileLooper.h.

Bool_t verbose() const
Query verbose mode.
Definition TFileLooper.h:80

◆ skipDir()

Bool_t TFileLooper::skipDir ( const TDirectory & dir)
protected

Definition at line 118 of file TFileLooper.cxx.

119{
120 if (m_skipDirs.Contains(dir.GetName())) return kTRUE;
121 else return kFALSE;
122}

◆ skipObject()

Bool_t TFileLooper::skipObject ( const char * name)
protected

Definition at line 129 of file TFileLooper.cxx.

130{
131 Bool_t failMatch(kFALSE);
132 Bool_t passMatch(kFALSE);
133
134 for (auto& re : m_failRE) {
135 if (re.Match(name)>0) {
136 failMatch = kTRUE;
137 break;
138 }
139 }
140
141 // give object another chance to match any of the m_passRE
142 for (auto& re : m_passRE) {
143 if (re.Match(name)>0) {
144 passMatch = kTRUE;
145 break;
146 }
147 }
148
149 bool result;
150 if (m_passBeforeFail) result = (!passMatch || failMatch);
151 else result = (failMatch && !passMatch);
152
153 if (m_verbose && result) cout << "Skipping " << name << endl;
154 return result;
155
156}
const boost::regex re(r_e)

◆ verbose()

Bool_t TFileLooper::verbose ( ) const
inline

Query verbose mode.

Definition at line 80 of file TFileLooper.h.

80{return m_verbose;}

Member Data Documentation

◆ m_errorCode

Int_t TFileLooper::m_errorCode {0}
protected

Definition at line 94 of file TFileLooper.h.

94{0};

◆ m_failRE

std::vector<TPRegexp> TFileLooper::m_failRE
protected

Definition at line 96 of file TFileLooper.h.

◆ m_file

TFile* TFileLooper::m_file {nullptr}
protected

Definition at line 89 of file TFileLooper.h.

89{nullptr};

◆ m_passBeforeFail

Bool_t TFileLooper::m_passBeforeFail {kFALSE}
protected

Definition at line 93 of file TFileLooper.h.

93{kFALSE};

◆ m_passRE

std::vector<TPRegexp> TFileLooper::m_passRE
protected

Definition at line 97 of file TFileLooper.h.

◆ m_rootDir

TString TFileLooper::m_rootDir
protected

Definition at line 90 of file TFileLooper.h.

◆ m_skipDirs

TString TFileLooper::m_skipDirs
protected

Definition at line 91 of file TFileLooper.h.

◆ m_skippedObjects

std::vector<std::string> TFileLooper::m_skippedObjects
protected

Definition at line 99 of file TFileLooper.h.

◆ m_verbose

Bool_t TFileLooper::m_verbose {kFALSE}
protected

Definition at line 92 of file TFileLooper.h.

92{kFALSE};

The documentation for this class was generated from the following files: