ATLAS Offline Software
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 
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 
24 using namespace std;
25 
26 Int_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
44 void 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  }
60  m_rootDir = rootDir;
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
73 void 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 
110 void 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
118 Bool_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
129 Bool_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...
160 void 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.
169 void 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"
178 TString 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"
188 TString 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 }
TFileLooper::skipDir
Bool_t skipDir(const TDirectory &dir)
Definition: TFileLooper.cxx:118
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
get_generator_info.result
result
Definition: get_generator_info.py:21
Data
@ Data
Definition: BaseObject.h:11
TFileLooper::addFailRegexp
void addFailRegexp(const char *regexp)
Skip keys that match this regexp.
Definition: TFileLooper.cxx:160
TFileLooper.h
TFileLooper class.
TFileLooper::processFile
void processFile(const char *filename, const char *rootDir=0)
Definition: TFileLooper.cxx:44
TFileLooper::run
virtual Int_t run(const char *filename, const char *rootDir=0)
Start processing.
Definition: TFileLooper.cxx:26
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
JetTagCalibConfig.className
string className
Definition: JetTagCalibConfig.py:31
fillPileUpNoiseLumi.next
next
Definition: fillPileUpNoiseLumi.py:52
ParseInputs.gDirectory
gDirectory
Definition: Final2012/ParseInputs.py:133
TFileLooper::addPassRegexp
void addPassRegexp(const char *regexp)
Never skip keys that match this regexp.
Definition: TFileLooper.cxx:169
beamspotman.dir
string dir
Definition: beamspotman.py:623
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
TFileLooper::getPathFromDir
TString getPathFromDir(const TDirectory &dir)
Definition: TFileLooper.cxx:178
re
const boost::regex re(r_e)
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::skipObject
Bool_t skipObject(const char *name)
Definition: TFileLooper.cxx:129
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37