removing unneeded include
[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
24TChain* CreateESDChain(const char* aDataDir = "ESDfiles.txt", Int_t aRuns = 20, Int_t offset = 0, Bool_t addFileName = kFALSE, Bool_t addFriend = kFALSE, Bool_t check = kFALSE)
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
dc740de4 34
539b6cb4 35 if (!aDataDir)
36 return 0;
37
0ab29cfa 38 Long_t id, size, flags, modtime;
39 if (gSystem->GetPathInfo(aDataDir, &id, &size, &flags, &modtime))
40 {
bdfe2916 41 printf("%s not found.\n", aDataDir);
0ab29cfa 42 return 0;
43 }
44
539b6cb4 45 TChain* chain = new TChain("esdTree");
f10a1859 46 TChain* chainFriend = 0;
47
48 if (addFriend)
49 chainFriend = new TChain("esdFriendTree");
539b6cb4 50
0ab29cfa 51 if (flags & 2)
539b6cb4 52 {
0ab29cfa 53 TString execDir(gSystem->pwd());
54 TSystemDirectory* baseDir = new TSystemDirectory(".", aDataDir);
55 TList* dirList = baseDir->GetListOfFiles();
56 Int_t nDirs = dirList->GetEntries();
57 gSystem->cd(execDir);
539b6cb4 58
0ab29cfa 59 Int_t count = 0;
dc740de4 60
0ab29cfa 61 for (Int_t iDir=0; iDir<nDirs; ++iDir)
62 {
63 TSystemFile* presentDir = (TSystemFile*) dirList->At(iDir);
64 if (!presentDir || !presentDir->IsDirectory() || strcmp(presentDir->GetName(), ".") == 0 || strcmp(presentDir->GetName(), "..") == 0)
65 continue;
49dc84d9 66
0ab29cfa 67 if (offset > 0)
68 {
69 --offset;
70 continue;
71 }
539b6cb4 72
0ab29cfa 73 if (count++ == aRuns)
74 break;
539b6cb4 75
0ab29cfa 76 TString presentDirName(aDataDir);
77 presentDirName += "/";
78 presentDirName += presentDir->GetName();
37dbb69e 79
0ab29cfa 80 chain->Add(presentDirName + "/AliESDs.root/esdTree");
81 }
82 }
83 else
84 {
85 // Open the input stream
86 ifstream in;
87 in.open(aDataDir);
dc740de4 88
0ab29cfa 89 Int_t count = 0;
37dbb69e 90
0ab29cfa 91 // Read the input list of files and add them to the chain
f10a1859 92 TString line;
93 while(in.good())
94 {
95 in >> line;
96
97 if (line.Length() == 0)
98 continue;
99
0ab29cfa 100 if (offset > 0)
101 {
102 --offset;
103 continue;
104 }
37dbb69e 105
0ab29cfa 106 if (count++ == aRuns)
107 break;
37dbb69e 108
f10a1859 109 TString esdFile(line);
110
111 if (addFileName)
112 esdFile += "/AliESDs.root";
113
114 TString esdFileFriend(esdFile);
115 esdFileFriend.ReplaceAll("AliESDs.root", "AliESDfriends.root");
116
117 if (check)
118 {
119 TFile* file = TFile::Open(esdFile);
120 if (!file)
121 continue;
122 file->Close();
123
124 if (chainFriend)
125 {
126 TFile* file = TFile::Open(esdFileFriend);
127 if (!file)
128 continue;
129 file->Close();
130 }
131
132 printf("%s\n", line.Data());
133 }
134
0ab29cfa 135 // add esd file
f10a1859 136 chain->Add(esdFile);
137
138 // add file
139 if (chainFriend)
140 chainFriend->Add(esdFileFriend);
16e24ca3 141 }
142
0ab29cfa 143 in.close();
37dbb69e 144 }
f10a1859 145
146 if (chainFriend)
147 chain->AddFriend(chainFriend);
37dbb69e 148
37dbb69e 149 return chain;
150}
7f8cf861 151
6f052bf3 152void ChainToTextFile(TChain* chain, const char* target)
7f8cf861 153{
6f052bf3 154 // write a text list of the files in the chain
155
7f8cf861 156 TObjArray* list = chain->GetListOfFiles();
157 TIterator* iter = list->MakeIterator();
158 TObject* obj = 0;
159
160 ofstream outfile;
161 outfile.open(target);
162
eb884e16 163 while ((obj = iter->Next())) {
164 TString fileName(obj->GetTitle());
165
166 fileName.Remove(fileName.Length()-13);
167
168 cout << fileName.Data() << endl;
169 outfile << fileName.Data() << endl;
170 }
7f8cf861 171
172 outfile.close();
173
174 delete iter;
6f052bf3 175}
176
177void LookupWrite(TChain* chain, const char* target)
178{
179 // looks up the chain and writes the remaining files to the text file target
180
181 chain->Lookup();
182
183 ChainToTextFile(chain, target);
7f8cf861 184}
eb884e16 185
186
187
188TChain* CreateRawChain(const char* aDataDir, Int_t aRuns = 20) {
189
190 TChain* chain = new TChain("RAW");
191
192 // ########################################################
193 // get the data dir
194 Char_t execDir[256];
195 sprintf(execDir,gSystem->pwd());
196 TSystemDirectory* baseDir = new TSystemDirectory(".",aDataDir);
197 TList* fileList = baseDir->GetListOfFiles();
198 Int_t nFiles = fileList->GetEntries();
199 // go back to the dir where this script is executed
200 gSystem->cd(execDir);
201
202 // ########################################################
203 // loop over files
204 Int_t counter = 0;
205 for (Int_t r=0; r<nFiles; r++) {
206
207 if (counter>aRuns)
208 break;
209
210 TSystemFile* presentFile = (TSystemFile*)fileList->At(r);
211 if (!presentFile || presentFile->IsDirectory())
212 continue;
213
214 if (!(TString(presentFile->GetName()).Contains(".root")))
215 continue;
216
217 counter++;
218
219 //cout << Form("%s/%s",aDataDir,presentFile->GetName()) << endl;
220
221 chain->AddFile(Form("%s/%s",aDataDir,presentFile->GetName()));
222 }
223
224
225
226}