]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG0/CreateESDChain.C
new function to get process type
[u/mrichter/AliRoot.git] / PWG0 / CreateESDChain.C
CommitLineData
dc740de4 1/* $Id$ */
2
f10a1859 3/**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5 * *
6 * Author: The ALICE Off-line Project. *
7 * Contributors are mentioned in the code where appropriate. *
8 * *
9 * Permission to use, copy, modify and distribute this software and its *
10 * documentation strictly for non-commercial purposes is hereby granted *
11 * without fee, provided that the above copyright notice appears in all *
12 * copies and that both the copyright notice and this permission notice *
13 * appear in the supporting documentation. The authors make no claims *
14 * about the suitability of this software for any purpose. It is *
15 * provided "as is" without express or implied warranty. *
16 **************************************************************************/
17
18// This helper macros creates a chain of ESD files for you. Source can be either a text
19// file with the file paths or a directory. In the latter case all ESD files in all subdirectories
20// are considered.
21//
22// Author: Jan.Fiete.Grosse-Oetringhaus@cern.ch
23
1cad76da 24TChain* CreateESDChain(const char* aDataDir = "ESDfiles.txt", Int_t aRuns = 20, Int_t offset = 0, Bool_t addFileName = kFALSE, Bool_t addFriend = kFALSE, const char* check = 0)
539b6cb4 25{
0ab29cfa 26 // creates chain of files in a given directory or file containing a list.
27 // In case of directory the structure is expected as:
dc740de4 28 // <aDataDir>/<dir0>/AliESDs.root
dc740de4 29 // <aDataDir>/<dir1>/AliESDs.root
dc740de4 30 // ...
f10a1859 31 //
32 // if addFileName is true the list only needs to contain the directories that contain the AliESDs.root files
33 // if addFriend is true a file AliESDfriends.root is expected in the same directory and added to the chain as friend
1cad76da 34 // if check is != 0 the files that work are written back into the textfile with the name check
dc740de4 35
539b6cb4 36 if (!aDataDir)
37 return 0;
38
0ab29cfa 39 Long_t id, size, flags, modtime;
40 if (gSystem->GetPathInfo(aDataDir, &id, &size, &flags, &modtime))
41 {
bdfe2916 42 printf("%s not found.\n", aDataDir);
0ab29cfa 43 return 0;
44 }
45
539b6cb4 46 TChain* chain = new TChain("esdTree");
f10a1859 47 TChain* chainFriend = 0;
48
49 if (addFriend)
50 chainFriend = new TChain("esdFriendTree");
539b6cb4 51
0ab29cfa 52 if (flags & 2)
539b6cb4 53 {
0ab29cfa 54 TString execDir(gSystem->pwd());
55 TSystemDirectory* baseDir = new TSystemDirectory(".", aDataDir);
56 TList* dirList = baseDir->GetListOfFiles();
57 Int_t nDirs = dirList->GetEntries();
58 gSystem->cd(execDir);
539b6cb4 59
0ab29cfa 60 Int_t count = 0;
dc740de4 61
0ab29cfa 62 for (Int_t iDir=0; iDir<nDirs; ++iDir)
63 {
64 TSystemFile* presentDir = (TSystemFile*) dirList->At(iDir);
65 if (!presentDir || !presentDir->IsDirectory() || strcmp(presentDir->GetName(), ".") == 0 || strcmp(presentDir->GetName(), "..") == 0)
66 continue;
49dc84d9 67
0ab29cfa 68 if (offset > 0)
69 {
70 --offset;
71 continue;
72 }
539b6cb4 73
0ab29cfa 74 if (count++ == aRuns)
75 break;
539b6cb4 76
0ab29cfa 77 TString presentDirName(aDataDir);
78 presentDirName += "/";
79 presentDirName += presentDir->GetName();
37dbb69e 80
0ab29cfa 81 chain->Add(presentDirName + "/AliESDs.root/esdTree");
82 }
83 }
84 else
85 {
86 // Open the input stream
87 ifstream in;
88 in.open(aDataDir);
dc740de4 89
1cad76da 90 ofstream outfile;
91 if (check)
92 outfile.open(check);
93
0ab29cfa 94 Int_t count = 0;
37dbb69e 95
0ab29cfa 96 // Read the input list of files and add them to the chain
f10a1859 97 TString line;
116083b1 98 while (in.good())
f10a1859 99 {
100 in >> line;
116083b1 101
f10a1859 102 if (line.Length() == 0)
116083b1 103 continue;
104
0ab29cfa 105 if (offset > 0)
106 {
116083b1 107 offset--;
0ab29cfa 108 continue;
109 }
37dbb69e 110
0ab29cfa 111 if (count++ == aRuns)
112 break;
37dbb69e 113
f10a1859 114 TString esdFile(line);
115
116 if (addFileName)
117 esdFile += "/AliESDs.root";
118
119 TString esdFileFriend(esdFile);
120 esdFileFriend.ReplaceAll("AliESDs.root", "AliESDfriends.root");
121
122 if (check)
123 {
124 TFile* file = TFile::Open(esdFile);
125 if (!file)
126 continue;
127 file->Close();
128
129 if (chainFriend)
130 {
131 TFile* file = TFile::Open(esdFileFriend);
132 if (!file)
133 continue;
134 file->Close();
135 }
136
1cad76da 137 outfile << line.Data() << endl;
f10a1859 138 printf("%s\n", line.Data());
139 }
140
0ab29cfa 141 // add esd file
f10a1859 142 chain->Add(esdFile);
143
144 // add file
145 if (chainFriend)
146 chainFriend->Add(esdFileFriend);
16e24ca3 147 }
148
0ab29cfa 149 in.close();
1cad76da 150
151 if (check)
29771dc8 152 outfile.close();
37dbb69e 153 }
f10a1859 154
155 if (chainFriend)
156 chain->AddFriend(chainFriend);
37dbb69e 157
37dbb69e 158 return chain;
159}
7f8cf861 160
6f052bf3 161void ChainToTextFile(TChain* chain, const char* target)
7f8cf861 162{
6f052bf3 163 // write a text list of the files in the chain
164
7f8cf861 165 TObjArray* list = chain->GetListOfFiles();
166 TIterator* iter = list->MakeIterator();
167 TObject* obj = 0;
168
169 ofstream outfile;
170 outfile.open(target);
171
eb884e16 172 while ((obj = iter->Next())) {
173 TString fileName(obj->GetTitle());
174
eb884e16 175 outfile << fileName.Data() << endl;
176 }
7f8cf861 177
178 outfile.close();
179
180 delete iter;
6f052bf3 181}
182
116083b1 183TObjArray* Chain2List(TChain* chain)
184{
185 // returns a TObjArray of TObjStrings of the file names in the chain
186
187 TObjArray* result = new TObjArray;
188
189 for (Int_t i=0; i<chain->GetListOfFiles()->GetEntries(); i++)
190 result->Add(new TObjString(chain->GetListOfFiles()->At(i)->GetTitle()));
191
192 return result;
193}
194
6f052bf3 195void LookupWrite(TChain* chain, const char* target)
196{
197 // looks up the chain and writes the remaining files to the text file target
198
199 chain->Lookup();
200
201 ChainToTextFile(chain, target);
7f8cf861 202}
eb884e16 203
1cad76da 204TChain* CreateChain(const char* treeName, const char* aDataDir, Int_t aRuns, Int_t offset = 0)
205{
206 // creates chain of files in a given directory or file containing a list.
eb884e16 207
1cad76da 208 if (!treeName || !aDataDir)
209 return 0;
eb884e16 210
1cad76da 211 TChain* chain = new TChain(treeName);
eb884e16 212
1cad76da 213 // Open the input stream
214 ifstream in;
215 in.open(aDataDir);
216
217 Int_t count = 0;
218
219 // Read the input list of files and add them to the chain
220 TString line;
221 while(in.good())
222 {
223 in >> line;
224
225 if (line.Length() == 0)
226 continue;
eb884e16 227
1cad76da 228 if (offset > 0)
229 {
230 --offset;
eb884e16 231 continue;
1cad76da 232 }
233
234 if (count++ == aRuns)
235 break;
eb884e16 236
1cad76da 237 chain->Add(line);
238 }
eb884e16 239
1cad76da 240 in.close();
eb884e16 241
1cad76da 242 return chain;
eb884e16 243}