]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG2/FORWARD/analysis2/scripts/MakeChain.C
Added class AliForwarddNdetaTask to do the dN/deta
[u/mrichter/AliRoot.git] / PWG2 / FORWARD / analysis2 / scripts / MakeChain.C
1 /** 
2  * Scan a directory (optionally recursive) for data files to add to
3  * the chain.  Only ROOT files, and files which name contain the
4  * passed pattern are considered.
5  * 
6  * @param dir        Directory to scan
7  * @param chain      Chain to add data to 
8  * @param pattern    Pattern that the file name must contain
9  * @param recursive  Whether to scan recursively 
10  */
11 void
12 ScanDirectory(TSystemDirectory* dir, TChain* chain, 
13               const char* pattern, bool recursive)
14 {
15   // Get list of files, and go back to old working directory
16   TString oldDir(gSystem->WorkingDirectory());
17   TList* files = dir->GetListOfFiles();
18   gSystem->ChangeDirectory(oldDir);
19
20   // Sort list of files and check if we should add it 
21   files->Sort();
22   TIter next(files);
23   TSystemFile* file = 0;
24   while ((file = static_cast<TSystemFile*>(next()))) {
25     TString name(file->GetName());
26     
27     // Ignore special links 
28     if (name == "." || name == "..") continue;
29
30     // Check if this is a directory 
31     if (file->IsDirectory()) { 
32       if (recursive) 
33         ScanDirectory(static_cast<TSystemDirectory*>(file),chain,recursive);
34       continue;
35     }
36     
37     // If this is not a root file, ignore 
38     if (!name.EndsWith(".root")) continue;
39
40     // If this file does not contain the pattern, ignore 
41     if (!name.Contains(pattern)) continue;
42     
43     // Get the path 
44     TString data(Form("%s/%s", file->GetTitle(), name.Data()));
45
46     chain->Add(data);
47
48   }
49 }
50
51 /** 
52  * Make a chain of specified data 
53  * 
54  * @param what       What data to chain.  Possible values are 
55  *                   - ESD Event summary data (AliESD)
56  *                   - AOD Analysis object data (AliAOD)
57  *                   - MC  Simulation data (galice)
58  * @param datadir    Data directory to scan 
59  * @param recursive  Whether to recurse into sub-directories 
60  * 
61  * @return Pointer to newly create chain, or null
62  */
63 TChain*
64 MakeChain(const char* what, const char* datadir, bool recursive=false)
65 {
66   TString w(what);
67   w.ToUpper();
68   const char* treeName = 0;
69   const char* pattern  = 0;
70   if      (w.Contains("ESD")) { treeName = "esdTree"; pattern = "AliESD"; }
71   else if (w.Contains("AOD")) { treeName = "aodTree"; pattern = "AliAOD"; }
72   else if (w.Contains("MC"))  { treeName = "TE";      pattern = "galice"; }
73   else {
74     Error("MakeChain", "Unknown mode '%s' (not one of ESD,AOD, or MC)", what);
75     return 0;
76   }
77     
78   // --- Our data chain ----------------------------------------------
79   TChain* chain = new TChain(treeName);
80
81   // --- Get list of ESDs --------------------------------------------
82   // Open source directory, and make sure we go back to were we were 
83   TString oldDir(gSystem->WorkingDirectory());
84   TSystemDirectory d(datadir, datadir);
85   ScanDirectory(&d, chain, pattern, recursive);
86
87   return chain;
88 }