AliPhysics  d497547 (d497547)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MakeChain.C
Go to the documentation of this file.
1 
19 Bool_t
20 IsFile(const char* path)
21 {
22  Long_t id;
23  Long_t size;
24  Long_t flags;
25  Long_t modtime;
26  gSystem->GetPathInfo(path, &id, &size, &flags, &modtime);
27  return !((flags & 0x2) == 0x2);
28 }
29 
38 Bool_t
39 TestFile(const TString& name, const char* pattern=0)
40 {
41  // If this is not a root file, ignore
42  if (!name.EndsWith(".root")) return false;
43 
44  // If this file does not contain the pattern, ignore
45  if (pattern && pattern[0] != '\0' && !name.Contains(pattern)) return false;
46  if (name.Contains("friends")) return false;
47 
48  Bool_t ret = true;
49  TFile* test = TFile::Open(name.Data(), "READ");
50  if (!test || test->IsZombie()) {
51  Warning("TestFile", "Failed to open file %s", name.Data());
52  ret = false;
53  }
54  else
55  test->Close();
56  return ret;
57 }
58 
71 void
72 ScanDirectory(TSystemDirectory* dir, TChain* chain,
73  const char* pattern, bool recursive)
74 {
75  // Get list of files, and go back to old working directory
76  TString oldDir(gSystem->WorkingDirectory());
77  TList* files = dir->GetListOfFiles();
78  gSystem->ChangeDirectory(oldDir);
79 
80  // Sort list of files and check if we should add it
81  files->Sort();
82  TIter next(files);
83  TSystemFile* file = 0;
84  while ((file = static_cast<TSystemFile*>(next()))) {
85  TString name(file->GetName());
86 
87  // Ignore special links
88  if (name == "." || name == "..") continue;
89 
90  // Check if this is a directory
91  if (file->IsDirectory()) {
92  if (recursive)
93  ScanDirectory(static_cast<TSystemDirectory*>(file),chain,
94  pattern,recursive);
95  continue;
96  }
97 
98  // Get the path
99  TString data(Form("%s/%s", file->GetTitle(), name.Data()));
100 
101  // Check the fuile
102  if (!TestFile(data, pattern)) continue;
103  chain->Add(data);
104  }
105 }
115 Bool_t
116 ScanInputList(TChain* chain, const TString& path, const char* treeName=0)
117 {
118  std::ifstream in(path.Data());
119  if (!in) {
120  Error("ScanInputList", "Failed to open input list %s", path.Data());
121  return false;
122  }
123  TString line;
124  while (in.good()) {
125  line.ReadLine(in); // Skip white-space
126  if (line.IsNull()) break; // Nothing -> EOF
127  if (line[0] == '#') continue; // Ignore comment lines
128  if (!TestFile(line, 0)) continue;
129  chain->Add(line);
130  }
131  in.close();
132  return true;
133 }
134 
135 
150 TChain*
151 MakeChain(const char* what, const char* datadir, bool recursive=false)
152 {
153  TString w(what);
154  w.ToUpper();
155  const char* treeName = 0;
156  const char* pattern = 0;
157  if (w.Contains("ESD")) { treeName = "esdTree"; pattern = "AliESD"; }
158  else if (w.Contains("AOD")) { treeName = "aodTree"; pattern = "AliAOD"; }
159  else if (w.Contains("MC")) { treeName = "TE"; pattern = "galice"; }
160  else {
161  Error("MakeChain", "Unknown mode '%s' (not one of ESD,AOD, or MC)", what);
162  return 0;
163  }
164 
165  // --- Our data chain ----------------------------------------------
166  TChain* chain = new TChain(treeName);
167 
168  // --- Get list of files --------------------------------------------
169  // Open source directory, and make sure we go back to were we were
170  TString oldDir(gSystem->WorkingDirectory());
171  TString path(gSystem->ExpandPathName(datadir));
172  if (!IsFile(path)) {
173  TSystemDirectory d(datadir, datadir);
174  ScanDirectory(&d, chain, pattern, recursive);
175  }
176  else if (path.EndsWith(".root")) {
177  if (TestFile(path, pattern)) chain->Add(path);
178  }
179  else {
180  // Input seems to be a list - parse it
181  ScanInputList(chain, path);
182  }
183 
184  // Make sure we do not make an empty chain
185  if (chain->GetListOfFiles()->GetEntries() <= 0) {
186  Warning("MakeChain", "Chain %s is empty for input %s",
187  treeName, datadir);
188  delete chain;
189  chain = 0;
190  }
191  return chain;
192 }
193 //
194 // EOF
195 //
TChain * MakeChain(const char *what, const char *datadir, bool recursive=false)
Definition: MakeChain.C:151
Bool_t ScanInputList(TChain *chain, const TString &path, const char *treeName=0)
Definition: MakeChain.C:116
TSystem * gSystem
void ScanDirectory(TSystemDirectory *dir, TChain *chain, const char *pattern, bool recursive)
Definition: MakeChain.C:72
Bool_t IsFile(const char *path)
Definition: MakeChain.C:20
Bool_t TestFile(const TString &name, const char *pattern=0)
Definition: MakeChain.C:39
TFile * file
TList with histograms for a given trigger.
void test(int runnumber=195345)
bool Bool_t
Definition: External.C:53
TDirectoryFile * dir