3 * @author Christian Holm Christensen <cholm@dalsgaard.hehi.nbi.dk>
4 * @date Tue Jul 12 10:20:07 2011
6 * @brief Script to generate a chain of files
8 * @ingroup pwglf_forward_scripts
11 * Check if a path points to a file
15 * @return True if the path points to a regular file
17 * @ingroup pwglf_forward_scripts
20 IsFile(const char* path)
26 gSystem->GetPathInfo(path, &id, &size, &flags, &modtime);
27 return !((flags & 0x2) == 0x2);
31 * Test if we can open a file
33 * @param name Name of file
34 * @param pattern Pattern to check against
36 * @return True on success
39 TestFile(const TString& name, const char* pattern=0)
41 // If this is not a root file, ignore
42 if (!name.EndsWith(".root")) return false;
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;
49 TFile* test = TFile::Open(name.Data(), "READ");
50 if (!test || test->IsZombie()) {
51 Warning("TestFile", "Failed to open file %s", name.Data());
60 * Scan a directory (optionally recursive) for data files to add to
61 * the chain. Only ROOT files, and files which name contain the
62 * passed pattern are considered.
64 * @param dir Directory to scan
65 * @param chain Chain to add data to
66 * @param pattern Pattern that the file name must contain
67 * @param recursive Whether to scan recursively
69 * @ingroup pwglf_forward_scripts
72 ScanDirectory(TSystemDirectory* dir, TChain* chain,
73 const char* pattern, bool recursive)
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);
80 // Sort list of files and check if we should add it
83 TSystemFile* file = 0;
84 while ((file = static_cast<TSystemFile*>(next()))) {
85 TString name(file->GetName());
87 // Ignore special links
88 if (name == "." || name == "..") continue;
90 // Check if this is a directory
91 if (file->IsDirectory()) {
93 ScanDirectory(static_cast<TSystemDirectory*>(file),chain,
99 TString data(Form("%s/%s", file->GetTitle(), name.Data()));
102 if (!TestFile(data, pattern)) continue;
107 * Scan an input list of files
109 * @param chain Chain to add to
110 * @param path File with list of files to add
111 * @param treeName Name of tree in files
113 * @return true on success
116 ScanInputList(TChain* chain, const TString& path, const char* treeName=0)
118 std::ifstream in(path.Data());
120 Error("ScanInputList", "Failed to open input list %s", path.Data());
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;
137 * Make a chain of specified data
139 * @param what What data to chain. Possible values are
140 * - ESD Event summary data (AliESD)
141 * - AOD Analysis object data (AliAOD)
142 * - MC Simulation data (galice)
143 * @param datadir Data directory to scan
144 * @param recursive Whether to recurse into sub-directories
146 * @return Pointer to newly create chain, or null
148 * @ingroup pwglf_forward_scripts
151 MakeChain(const char* what, const char* datadir, bool recursive=false)
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"; }
161 Error("MakeChain", "Unknown mode '%s' (not one of ESD,AOD, or MC)", what);
165 // --- Our data chain ----------------------------------------------
166 TChain* chain = new TChain(treeName);
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));
173 TSystemDirectory d(datadir, datadir);
174 ScanDirectory(&d, chain, pattern, recursive);
176 else if (path.EndsWith(".root")) {
177 if (TestFile(path, pattern)) chain->Add(path);
180 // Input seems to be a list - parse it
181 ScanInputList(chain, path);
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",