]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG0/CreateESDChain.C
adding helper function to count empty bins in AliCorrectionMatrix
[u/mrichter/AliRoot.git] / PWG0 / CreateESDChain.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 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
24 TChain* 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)
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>/AliESDs.root
29   // <aDataDir>/<dir1>/AliESDs.root
30   // ...
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
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("esdTree");
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 + "/AliESDs.root/esdTree");
81     }
82   }
83   else
84   {
85     // Open the input stream
86     ifstream in;
87     in.open(aDataDir);
88
89     Int_t count = 0;
90
91     // Read the input list of files and add them to the chain
92     TString line;
93     while(in.good()) 
94     {
95       in >> line;
96       
97       if (line.Length() == 0)
98         continue;      
99       
100       if (offset > 0)
101       {
102         --offset;
103         continue;
104       }
105
106       if (count++ == aRuns)
107         break;
108
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         
135         // add esd file
136       chain->Add(esdFile);
137
138         // add file
139       if (chainFriend)
140         chainFriend->Add(esdFileFriend);
141     }
142
143     in.close();
144   }
145   
146   if (chainFriend)
147     chain->AddFriend(chainFriend);
148
149   return chain;
150 }
151
152 void LookupWrite(TChain* chain, const char* target)
153 {
154   // looks up the chain and writes the remaining files to the text file target
155
156   chain->Lookup();
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     outfile << obj->GetTitle() << endl;
167
168   outfile.close();
169
170   delete iter;
171 }