ATLAS Offline Software
Loading...
Searching...
No Matches
TFileLooper.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
11
13
14#include <iostream>
15#include <sstream>
16#include "TError.h"
17#include "TH1.h"
18#include "TFile.h"
19#include "TCanvas.h"
20#include "TSystem.h"
21#include "TKey.h"
22#include "TPRegexp.h"
23
24using namespace std;
25
26Int_t TFileLooper::run(const char* filename, const char* rootDir)
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}
41
42
43// Convert one file into html
44void TFileLooper::processFile(const char* filename, const char* rootDir)
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}
70
71
72// Save all histograms in dir into png files
73void TFileLooper::processDir(TDirectory& dir)
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}
109
110void TFileLooper::processKey(TDirectory& dir, TKey& key)
111{
112 cout << "--- processKey: " << key.GetName()
113 << " in directory " << dir.GetPath() << endl;
114}
115
116
117// Check if we have to skip this directory
118Bool_t TFileLooper::skipDir(const TDirectory& dir)
119{
120 if (m_skipDirs.Contains(dir.GetName())) return kTRUE;
121 else return kFALSE;
122}
123
124
125// Check if we have to skip this object
126// Logic: 1) check if name matches any of the m_failRE
127// 2) check if name matches any of the m_passRE
128// 3) Return true if 1)=true and 2)=false
129Bool_t TFileLooper::skipObject(const char* name)
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}
157
158
159// Skip keys that match this regexp...
160void TFileLooper::addFailRegexp(const char* regexp)
161{
162 if (regexp) {
163 m_failRE.emplace_back(regexp);
164 }
165}
166
167
168// ...and do not match this regexp.
169void TFileLooper::addPassRegexp(const char* regexp)
170{
171 if (regexp) {
172 m_passRE.emplace_back(regexp);
173 }
174}
175
176// Create file system path from TDirectory path
177// Example: "../Tau-monitoring.root:/TrigSteering" -> "Tau-monitoring.root/TrigSteering"
178TString TFileLooper::getPathFromDir(const TDirectory& dir)
179{
180 TString s(dir.GetPath());
181 if (s.Index("../")==0) s.Remove(0,3);
182 s.ReplaceAll(":","");
183 return s;
184}
185
186// return key name with path
187// Example: myHist in directory myDir -> "myDir/myHist"
188TString TFileLooper::getKeyPath(const TDirectory& dir, const TKey& key)
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}
const boost::regex re(r_e)
@ Data
Definition BaseObject.h:11
TFileLooper class.
virtual void afterFile()
Definition TFileLooper.h:57
void addFailRegexp(const char *regexp)
Skip keys that match this regexp.
void addPassRegexp(const char *regexp)
Never skip keys that match this regexp.
TString m_rootDir
Definition TFileLooper.h:90
TString rootDir() const
Current directory.
Definition TFileLooper.h:86
std::vector< std::string > m_skippedObjects
Definition TFileLooper.h:99
void processDir(TDirectory &dir)
TFile * m_file
Definition TFileLooper.h:89
std::vector< TPRegexp > m_failRE
Definition TFileLooper.h:96
TString getKeyPath(const TDirectory &dir, const TKey &key)
virtual void beforeFile()
Definition TFileLooper.h:56
virtual Int_t run(const char *filename, const char *rootDir=0)
Start processing.
virtual void beginJob()
Definition TFileLooper.h:54
Bool_t m_verbose
Definition TFileLooper.h:92
virtual void endJob()
Definition TFileLooper.h:55
virtual void beforeDir()
Definition TFileLooper.h:58
std::vector< TPRegexp > m_passRE
Definition TFileLooper.h:97
Bool_t skipObject(const char *name)
Bool_t m_passBeforeFail
Definition TFileLooper.h:93
Bool_t skipDir(const TDirectory &dir)
TString getPathFromDir(const TDirectory &dir)
Int_t m_errorCode
Definition TFileLooper.h:94
void processFile(const char *filename, const char *rootDir=0)
TString m_skipDirs
Definition TFileLooper.h:91
virtual void afterDir()
Definition TFileLooper.h:59
virtual void processKey(TDirectory &dir, TKey &key)
Method called for every key.
STL namespace.