]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWGLF/FORWARD/analysis2/scripts/MakeChain.C
Fixed references from PWG2 -> PWGLF - very efficiently done using ETags.
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / scripts / MakeChain.C
CommitLineData
56236b95 1/**
2 * @file MakeChain.C
3 * @author Christian Holm Christensen <cholm@dalsgaard.hehi.nbi.dk>
4 * @date Tue Jul 12 10:20:07 2011
5 *
6 * @brief Script to generate a chain of files
7 *
bd6f5206 8 * @ingroup pwglf_forward_scripts
56236b95 9 */
10/**
11 * Check if a path points to a file
12 *
13 * @param path Path
14 *
15 * @return True if the path points to a regular file
16 *
bd6f5206 17 * @ingroup pwglf_forward_scripts
56236b95 18 */
19Bool_t
20IsFile(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
30/**
31 * Test if we can open a file
32 *
33 * @param name Name of file
34 * @param pattern Pattern to check against
35 *
36 * @return True on success
37 */
38Bool_t
39TestFile(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;
e2213ed5 49 TFile* test = TFile::Open(name.Data(), "READ");
56236b95 50 if (!test || test->IsZombie()) {
e2213ed5 51 Warning("TestFile", "Failed to open file %s", name.Data());
56236b95 52 ret = false;
53 }
54 else
55 test->Close();
56 return ret;
57}
58
b2e7f2d6 59/**
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.
63 *
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
56199f2b 68 *
bd6f5206 69 * @ingroup pwglf_forward_scripts
b2e7f2d6 70 */
71void
72ScanDirectory(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)
b5c9a732 93 ScanDirectory(static_cast<TSystemDirectory*>(file),chain,
94 pattern,recursive);
b2e7f2d6 95 continue;
96 }
97
b2e7f2d6 98 // Get the path
99 TString data(Form("%s/%s", file->GetTitle(), name.Data()));
100
56236b95 101 // Check the fuile
102 if (!TestFile(data, pattern)) continue;
b2e7f2d6 103 chain->Add(data);
b2e7f2d6 104 }
105}
56236b95 106/**
107 * Scan an input list of files
108 *
109 * @param chain Chain to add to
110 * @param path file with list of files to add
111 *
112 * @return true on success
113 */
114Bool_t
e2213ed5 115ScanInputList(TChain* chain, const TString& path, const char* treeName=0)
56236b95 116{
117 std::ifstream in(path.Data());
118 if (!in) {
119 Error("ScanInputList", "Failed to open input list %s", path.Data());
120 return false;
121 }
122 TString line;
123 while (in.good()) {
124 line.ReadLine(in); // Skip white-space
125 if (line.IsNull()) break; // Nothing -> EOF
126 if (line[0] == '#') continue; // Ignore comment lines
127 if (!TestFile(line, 0)) continue;
128 chain->Add(line);
129 }
130 in.close();
131 return true;
132}
133
134
b2e7f2d6 135/**
136 * Make a chain of specified data
137 *
138 * @param what What data to chain. Possible values are
139 * - ESD Event summary data (AliESD)
140 * - AOD Analysis object data (AliAOD)
141 * - MC Simulation data (galice)
142 * @param datadir Data directory to scan
143 * @param recursive Whether to recurse into sub-directories
144 *
145 * @return Pointer to newly create chain, or null
56199f2b 146 *
bd6f5206 147 * @ingroup pwglf_forward_scripts
b2e7f2d6 148 */
149TChain*
150MakeChain(const char* what, const char* datadir, bool recursive=false)
151{
152 TString w(what);
153 w.ToUpper();
154 const char* treeName = 0;
155 const char* pattern = 0;
156 if (w.Contains("ESD")) { treeName = "esdTree"; pattern = "AliESD"; }
157 else if (w.Contains("AOD")) { treeName = "aodTree"; pattern = "AliAOD"; }
158 else if (w.Contains("MC")) { treeName = "TE"; pattern = "galice"; }
159 else {
160 Error("MakeChain", "Unknown mode '%s' (not one of ESD,AOD, or MC)", what);
161 return 0;
162 }
163
164 // --- Our data chain ----------------------------------------------
165 TChain* chain = new TChain(treeName);
166
56236b95 167 // --- Get list of files --------------------------------------------
b2e7f2d6 168 // Open source directory, and make sure we go back to were we were
169 TString oldDir(gSystem->WorkingDirectory());
56236b95 170 TString path(gSystem->ExpandPathName(datadir));
171 if (!IsFile(path)) {
172 TSystemDirectory d(datadir, datadir);
173 ScanDirectory(&d, chain, pattern, recursive);
174 }
175 else if (path.EndsWith(".root")) {
176 if (TestFile(path, pattern)) chain->Add(path);
177 }
178 else {
179 // Input seems to be a list - parse it
180 ScanInputList(chain, path);
181 }
182
183 // Make sure we do not make an empty chain
184 if (chain->GetListOfFiles()->GetEntries() <= 0) {
185 Warning("MakeChain", "Chain %s is empty for input %s",
186 treeName, datadir);
187 delete chain;
188 chain = 0;
189 }
b2e7f2d6 190 return chain;
191}
56199f2b 192//
193// EOF
194//