create general emcal task lib
[u/mrichter/AliRoot.git] / PWG / EMCAL / macros / CreateAODChain.C
CommitLineData
11878584 1/* $Id$ */
2
3// This helper macros creates a chain of AOD files for you.
4// Source can be either a text file with the file paths or
5// a directory. In the latter case all AOD files in all
6// subdirectories are considered.
7//
8// Author: Jan.Fiete.Grosse-Oetringhaus@cern.ch
9
10TChain* CreateAODChain(
11 const char* aDataDir = "AODfiles.txt",
12 Int_t aRuns = 20,
13 Int_t offset = 0,
14 Bool_t addFileName = kFALSE,
15 Bool_t addFriend = kFALSE,
16 const char* check = 0)
17{
18 // creates chain of files in a given directory or file containing a list.
19 // In case of directory the structure is expected as:
20 // <aDataDir>/<dir0>/AliAODs.root
21 // <aDataDir>/<dir1>/AliAODs.root
22 // ...
23 //
24 // if addFileName is true the list only needs to contain the directories that contain the AliAODs.root files
25 // if check is != 0 the files that work are written back into the textfile with the name check
26
27 if (!aDataDir)
28 return 0;
29
30 Long_t id, size, flags, modtime;
31 if (gSystem->GetPathInfo(aDataDir, &id, &size, &flags, &modtime))
32 {
33 printf("%s not found.\n", aDataDir);
34 return 0;
35 }
36
37 TChain* chain = new TChain("aodTree");
38 TChain* chainFriend = 0;
39
40 // if (addFriend)
41 // chainFriend = new TChain("esdFriendTree");
42
43 if (flags & 2)
44 {
45 TString execDir(gSystem->pwd());
46 TSystemDirectory* baseDir = new TSystemDirectory(".", aDataDir);
47 TList* dirList = baseDir->GetListOfFiles();
48 Int_t nDirs = dirList->GetEntries();
49 gSystem->cd(execDir);
50
51 Int_t count = 0;
52
53 for (Int_t iDir=0; iDir<nDirs; ++iDir)
54 {
55 TSystemFile* presentDir = (TSystemFile*) dirList->At(iDir);
56 if (!presentDir || !presentDir->IsDirectory() || strcmp(presentDir->GetName(), ".") == 0 || strcmp(presentDir->GetName(), "..") == 0)
57 continue;
58
59 if (offset > 0)
60 {
61 --offset;
62 continue;
63 }
64
65 if (count++ == aRuns)
66 break;
67
68 TString presentDirName(aDataDir);
69 presentDirName += "/";
70 presentDirName += presentDir->GetName();
71
72 chain->Add(presentDirName + "/AliAODs.root/aodTree");
73 }
74 }
75 else
76 {
77 // Open the input stream
78 ifstream in;
79 in.open(aDataDir);
80
81 ofstream outfile;
82 if (check)
83 outfile.open(check);
84
85 Int_t count = 0;
86
87 // Read the input list of files and add them to the chain
88 TString line;
89 while (in.good())
90 {
91 in >> line;
92
93 if (line.Length() == 0)
94 continue;
95
96 if (offset > 0)
97 {
98 offset--;
99 continue;
100 }
101
102 if (count++ == aRuns)
103 break;
104
105 TString aodFile(line);
106
107 if (addFileName)
108 aodFile += "/AliAODs.root";
109
110
111 if (check)
112 {
113 TFile* file = TFile::Open(aodFile);
114 if (!file)
115 continue;
116 file->Close();
117 /*
118 if (chainFriend)
119 {
120 TFile* file = TFile::Open(esdFileFriend);
121 if (!file)
122 continue;
123 file->Close();
124 }
125 */
126 outfile << line.Data() << endl;
127 printf("%s\n", line.Data());
128 }
129
130 // add aod file
131 chain->Add(aodFile);
132
133 // add file
134 // if (chainFriend)
135 // chainFriend->Add(esdFileFriend);
136 }
137
138 in.close();
139
140 if (check)
141 outfile.close();
142 }
143
144 // if (chainFriend)
145 // chain->AddFriend(chainFriend);
146
147 return chain;
148}
149
150void ChainToTextFile(TChain* chain, const char* target)
151{
152 // write a text list of the files in the chain
153
154 TObjArray* list = chain->GetListOfFiles();
155 TIterator* iter = list->MakeIterator();
156 TObject* obj = 0;
157
158 ofstream outfile;
159 outfile.open(target);
160
161 while ((obj = iter->Next())) {
162 TString fileName(obj->GetTitle());
163
164 outfile << fileName.Data() << endl;
165 }
166
167 outfile.close();
168
169 delete iter;
170}
171
172TObjArray* Chain2List(TChain* chain)
173{
174 // returns a TObjArray of TObjStrings of the file names in the chain
175
176 TObjArray* result = new TObjArray;
177
178 for (Int_t i=0; i<chain->GetListOfFiles()->GetEntries(); i++)
179 result->Add(new TObjString(chain->GetListOfFiles()->At(i)->GetTitle()));
180
181 return result;
182}
183
184void LookupWrite(TChain* chain, const char* target)
185{
186 // looks up the chain and writes the remaining files to the text file target
187
188 chain->Lookup();
189
190 ChainToTextFile(chain, target);
191}
192
193TChain* CreateChain(const char* treeName, const char* aDataDir, Int_t aRuns, Int_t offset = 0)
194{
195 // creates chain of files in a given directory or file containing a list.
196
197 if (!treeName || !aDataDir)
198 return 0;
199
200 TChain* chain = new TChain(treeName);
201
202 // Open the input stream
203 ifstream in;
204 in.open(aDataDir);
205
206 Int_t count = 0;
207
208 // Read the input list of files and add them to the chain
209 TString line;
210 while(in.good())
211 {
212 in >> line;
213
214 if (line.Length() == 0)
215 continue;
216
217 if (offset > 0)
218 {
219 --offset;
220 continue;
221 }
222
223 if (count++ == aRuns)
224 break;
225
226 chain->Add(line);
227 }
228
229 in.close();
230
231 return chain;
232}