ATLAS Offline Software
Public Member Functions | List of all members
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. More...
 
virtual void processKey (TDirectory &dir, TKey &key)
 Method called for every key. More...
 

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. More...
 
void addFailRegexp (const char *regexp)
 Skip keys that match this regexp. More...
 
void addPassRegexp (const char *regexp)
 Never skip keys that match this regexp. More...
 
void passBeforeFailRegexp (Bool_t passBeforeFail=kTRUE)
 Reverse pass/fail logic. First check on pass then on fail. More...
 
void setVerbose (Bool_t verbose=kTRUE)
 Set verbose mode. More...
 
Bool_t verbose () const
 Query verbose mode. More...
 
const TFile * file () const
 Current ROOT file. More...
 
TString rootDir () const
 Current directory. More...
 
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 }

◆ 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 }

◆ 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; }

◆ 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; }

◆ 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();
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 }

◆ 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();
66  afterFile();
67 
68  delete m_file; // calls Close()
69 }

◆ 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();
38  endJob();
39  return m_errorCode;
40 }

◆ 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;}

◆ setVerbose()

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

Set verbose mode.

Definition at line 77 of file TFileLooper.h.

77 {m_verbose = verbose;}

◆ 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 }

◆ 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.

◆ 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.

◆ m_passBeforeFail

Bool_t TFileLooper::m_passBeforeFail {kFALSE}
protected

Definition at line 93 of file TFileLooper.h.

◆ 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.


The documentation for this class was generated from the following files:
TFileLooper::skipDir
Bool_t skipDir(const TDirectory &dir)
Definition: TFileLooper.cxx:118
TFileLooper::m_skipDirs
TString m_skipDirs
Definition: TFileLooper.h:91
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
get_generator_info.result
result
Definition: get_generator_info.py:21
TFileLooper::verbose
Bool_t verbose() const
Query verbose mode.
Definition: TFileLooper.h:80
TFileLooper::m_skippedObjects
std::vector< std::string > m_skippedObjects
Definition: TFileLooper.h:99
Data
@ Data
Definition: BaseObject.h:11
TFileLooper::processFile
void processFile(const char *filename, const char *rootDir=0)
Definition: TFileLooper.cxx:44
TFileLooper::m_errorCode
Int_t m_errorCode
Definition: TFileLooper.h:94
TFileLooper::endJob
virtual void endJob()
Definition: TFileLooper.h:55
JetTagCalibConfig.className
string className
Definition: JetTagCalibConfig.py:36
fillPileUpNoiseLumi.next
next
Definition: fillPileUpNoiseLumi.py:52
ParseInputs.gDirectory
gDirectory
Definition: Final2012/ParseInputs.py:133
TFileLooper::afterDir
virtual void afterDir()
Definition: TFileLooper.h:59
TFileLooper::beginJob
virtual void beginJob()
Definition: TFileLooper.h:54
TFileLooper::m_rootDir
TString m_rootDir
Definition: TFileLooper.h:90
beamspotman.dir
string dir
Definition: beamspotman.py:623
TFileLooper::rootDir
TString rootDir() const
Current directory.
Definition: TFileLooper.h:86
TFileLooper::m_passBeforeFail
Bool_t m_passBeforeFail
Definition: TFileLooper.h:93
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
TFileLooper::m_file
TFile * m_file
Definition: TFileLooper.h:89
TFileLooper::m_verbose
Bool_t m_verbose
Definition: TFileLooper.h:92
TFileLooper::m_passRE
std::vector< TPRegexp > m_passRE
Definition: TFileLooper.h:97
TFileLooper::beforeFile
virtual void beforeFile()
Definition: TFileLooper.h:56
TFileLooper::getPathFromDir
TString getPathFromDir(const TDirectory &dir)
Definition: TFileLooper.cxx:178
TFileLooper::beforeDir
virtual void beforeDir()
Definition: TFileLooper.h:58
re
const boost::regex re(r_e)
TFileLooper::m_failRE
std::vector< TPRegexp > m_failRE
Definition: TFileLooper.h:96
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
TFileLooper::processDir
void processDir(TDirectory &dir)
Definition: TFileLooper.cxx:73
TFileLooper::getKeyPath
TString getKeyPath(const TDirectory &dir, const TKey &key)
Definition: TFileLooper.cxx:188
TFileLooper::processKey
virtual void processKey(TDirectory &dir, TKey &key)
Method called for every key.
Definition: TFileLooper.cxx:110
TFileLooper::afterFile
virtual void afterFile()
Definition: TFileLooper.h:57
TFileLooper::skipObject
Bool_t skipObject(const char *name)
Definition: TFileLooper.cxx:129
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37