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