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