]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/EMCAL/macros/CreateESDChain.C
update from salvatore to include its refit tracks with correct label
[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       TString esdFileFriend(esdFile);
113       esdFileFriend.ReplaceAll("AliESDs.root", "AliESDfriends.root");
114         
115       if (check)
116       {
117         TFile* file = TFile::Open(esdFile);
118         if (!file)
119           continue;
120         file->Close();
121         
122         if (chainFriend)
123         {
124           TFile* file = TFile::Open(esdFileFriend);
125           if (!file)
126             continue;
127           file->Close();
128         }
129         
130         outfile << line.Data() << endl;
131         printf("%s\n", line.Data());
132       }        
133         
134         // add esd file
135       chain->Add(esdFile);
136
137         // add file
138       if (chainFriend)
139         chainFriend->Add(esdFileFriend);
140     }
141
142     in.close();
143     
144     if (check)
145       outfile.close();
146   }
147   
148   if (chainFriend)
149     chain->AddFriend(chainFriend);
150
151   return chain;
152 }
153
154 void ChainToTextFile(TChain* chain, const char* target)
155 {
156   // write a text list of the files in the chain
157   
158   TObjArray* list = chain->GetListOfFiles();
159   TIterator* iter = list->MakeIterator();
160   TObject* obj = 0;
161
162   ofstream outfile;
163   outfile.open(target);
164
165   while ((obj = iter->Next())) {
166     TString fileName(obj->GetTitle());
167     
168     outfile << fileName.Data() << endl;
169   }
170
171   outfile.close();
172
173   delete iter;
174
175
176 TObjArray* Chain2List(TChain* chain)
177 {
178   // returns a TObjArray of TObjStrings of the file names in the chain
179
180   TObjArray* result = new TObjArray;
181
182   for (Int_t i=0; i<chain->GetListOfFiles()->GetEntries(); i++)
183     result->Add(new TObjString(chain->GetListOfFiles()->At(i)->GetTitle()));
184
185   return result;
186 }
187
188 void LookupWrite(TChain* chain, const char* target)
189 {
190   // looks up the chain and writes the remaining files to the text file target
191
192   chain->Lookup();
193
194   ChainToTextFile(chain, target);
195 }
196
197 TChain* CreateChain(const char* treeName, const char* aDataDir, Int_t aRuns, Int_t offset = 0)
198 {
199   // creates chain of files in a given directory or file containing a list.
200
201   if (!treeName || !aDataDir)
202     return 0;
203
204   TChain* chain = new TChain(treeName);
205   
206   // Open the input stream
207   ifstream in;
208   in.open(aDataDir);
209
210   Int_t count = 0;
211
212   // Read the input list of files and add them to the chain
213   TString line;
214   while(in.good()) 
215   {
216     in >> line;
217       
218     if (line.Length() == 0)
219       continue;      
220     
221     if (offset > 0)
222     {
223       --offset;
224       continue;
225     }
226
227     if (count++ == aRuns)
228       break;
229
230     chain->Add(line);
231   }
232
233   in.close();
234
235   return chain;
236 }