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