]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliESDInputHandlerRP.cxx
Access to tree via detector name.
[u/mrichter/AliRoot.git] / STEER / AliESDInputHandlerRP.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //-------------------------------------------------------------------------
19 //     Event handler for ESD input reading the RecPoint Trees in parallel
20 //     Author: Andreas Morsch, CERN
21 //-------------------------------------------------------------------------
22
23 #include <TTree.h>
24 #include <TList.h>
25 #include <TFile.h>
26 #include <TArchiveFile.h>
27 #include <TSystemDirectory.h>
28 #include <TString.h>
29 #include <TObjString.h>
30 #include <TProcessID.h>
31
32 #include "AliESDInputHandlerRP.h"
33 #include "AliESDEvent.h"
34 #include "AliESD.h"
35 #include "AliLog.h"
36
37 ClassImp(AliESDInputHandlerRP)
38
39 //______________________________________________________________________________
40 AliESDInputHandlerRP::AliESDInputHandlerRP() :
41     AliESDInputHandler(),
42     fRTrees(   new TList()),
43     fRFiles(   new TList()),
44     fDetectors(new TList()),
45     fDirR(0),
46     fEventNumber(-1),
47     fNEvent(-1),
48     fFileNumber(0),
49     fEventsPerFile(0),
50     fExtension(""),
51     fPathName(new TString("./")),
52     fIsArchive(kFALSE)
53 {
54   // Default constructor
55 }
56
57
58 //______________________________________________________________________________
59 AliESDInputHandlerRP::AliESDInputHandlerRP(const char* name, const char* title):
60     AliESDInputHandler(name, title),
61     fRTrees(   new TList()),
62     fRFiles(   new TList()),
63     fDetectors(new TList()),
64     fDirR(0),
65     fEventNumber(-1),
66     fNEvent(-1),
67     fFileNumber(0),
68     fEventsPerFile(0),
69     fExtension(""),
70     fPathName(new TString("./")),
71     fIsArchive(kFALSE)
72 {
73     // Constructor
74 }
75
76 //______________________________________________________________________________
77 AliESDInputHandlerRP::~AliESDInputHandlerRP() 
78 {
79   // Destructor
80 }
81
82 Bool_t AliESDInputHandlerRP::Init(Option_t* opt)
83 {
84     //
85     // Initialize input
86     //
87     if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
88     //
89     TIter next(fDetectors);
90     TNamed* det;
91     TFile* file = 0;
92     while (det = ((TNamed*) next()))
93     {
94         if (!fIsArchive) {
95             file = TFile::Open(Form("%s%s.RecPoints.root", fPathName->Data(), det->GetName()));
96         } else {
97             file = TFile::Open(Form("%s#%s.RecPoints.root", fPathName->Data(), det->GetName()));
98         }
99         if (!file) AliFatal(Form("AliESDInputHandlerRP: TPC.RecPoints.root not found in %s ! \n", fPathName->Data()));
100         fRFiles->Add(file);
101     }
102     fEventsPerFile = file->GetNkeys() - file->GetNProcessIDs();
103     // Reset the event number
104     fEventNumber      = -1;
105     fFileNumber       =  0;
106     // Get number of events from esd tree 
107     fNEvent           =  fTree->GetEntries();
108     
109     printf("AliESDInputHandler::Init() %d\n",__LINE__);
110     return kTRUE;
111 }
112
113 Bool_t AliESDInputHandlerRP::BeginEvent(Long64_t entry)
114 {
115     // Begin the next event
116     // Delegate first to base class
117     AliESDInputHandler::BeginEvent(entry);
118 //
119     if (entry == -1) {
120         fEventNumber++;
121         entry = fEventNumber;
122     } else {
123         fEventNumber = entry;
124     }
125     
126     if (entry >= fNEvent) {
127         AliWarning(Form("AliESDInputHandlerRP: Event number out of range %5d %5d\n", entry, fNEvent));
128         return kFALSE;
129     }
130     return LoadEvent(entry);
131 }
132
133 Bool_t AliESDInputHandlerRP::LoadEvent(Int_t iev)
134 {
135     // Load the event number iev
136     //
137     // Calculate the file number
138     Int_t inew  = iev / fEventsPerFile;
139     if (inew != fFileNumber) {
140         fFileNumber = inew;
141         if (!OpenFile(fFileNumber)){
142             return kFALSE;
143         }
144     }
145     // Folder name
146     char folder[20];
147     sprintf(folder, "Event%d", iev);
148     // Tree R
149     TIter next(fRFiles);
150     TFile* file;
151     while (file = ((TFile*) next()))
152     {
153         file->GetObject(folder, fDirR);
154         if (!fDirR) {
155             AliWarning(Form("AliESDInputHandlerRP: Event #%5d not found\n", iev));
156             return kFALSE;
157         }
158         TTree* tree;
159         fDirR ->GetObject("TreeR", tree);
160         fRTrees->Add(tree);
161         tree->ls();
162     }
163     return kTRUE;
164 }
165
166 Bool_t AliESDInputHandlerRP::OpenFile(Int_t i)
167 {
168     // Open file i
169     Bool_t ok = kTRUE;
170     if (i > 0) {
171         fExtension = Form("%d", i);
172     } else {
173         fExtension = "";
174     }
175     
176     fRFiles->Delete();
177     TIter next(fDetectors);
178     TNamed* det;
179     TFile* file;
180     while (det = ((TNamed*) next()))
181     {
182         if (!fIsArchive) {
183             file = TFile::Open(Form("%s%s.RecPoints%s.root", fPathName->Data(), det->GetName(), fExtension));
184         } else {
185             file = TFile::Open(Form("%s#%s.RecPoints%s.root", fPathName->Data(), det->GetName(), fExtension));
186         }
187         if (!file) AliFatal(Form("AliESDInputHandlerRP: RecPoints.root not found in %s ! \n", fPathName->Data()));
188         fRFiles->Add(file);
189     }
190     return ok;
191 }
192
193 Bool_t AliESDInputHandlerRP::Notify(const char *path)
194 {
195   // Notify about directory change
196   // The directory is taken from the 'path' argument
197   // 
198     // Get path to directory
199     TString fileName(path);
200     if(fileName.Contains("AliESDs.root")){
201         fileName.ReplaceAll("AliESDs.root", "");
202     }
203     // If this is an archive it will contain a # 
204     if(fileName.Contains("#")){
205         fileName.ReplaceAll("#", "");
206     }
207     //
208     // At this point we have a path to the directory or to the archive
209     *fPathName = fileName;
210     //
211     // Now filter the files containing RecPoints *.RecPoints.*
212     fIsArchive = kFALSE;
213     if (fPathName->Contains(".zip")) fIsArchive = kTRUE;
214
215     TSeqCollection* members;
216     
217     if (fIsArchive) {
218         // Archive
219         TFile* file = TFile::Open(fPathName->Data());
220         TArchiveFile* arch = file->GetArchive();
221         members = arch->GetMembers();
222     } else {
223         // Directory
224         TSystemDirectory dir(".", fPathName->Data());
225         members = dir.GetListOfFiles();
226     }
227     
228     TIter next(members);
229     TFile* entry;
230     Int_t ien = 0;
231     while ( (entry = (TFile*) next()) )
232     {
233         printf("File %s \n", entry->GetName());
234         TString name(entry->GetName());
235         TObjArray* tokens = name.Tokenize(".");
236         Int_t ntok = tokens->GetEntries();
237         if (ntok <= 1) continue;
238         TString str = ((TObjString*) tokens->At(1))->GetString();
239         if (!(strcmp(str.Data(), "RecPoints"))){
240             TString det = ((TObjString*) tokens->At(0))->GetString();
241             printf("Name  %s \n", det.Data());
242             TNamed* entry = new TNamed(det.Data(), det.Data());
243             entry->SetUniqueID(ien++);
244             fDetectors->Add(entry);
245         }
246     } // loop over files
247     
248
249     // Now we have the path and the list of detectors
250     
251     printf("AliESDInputHandlerRP::Notify() Path: %s\n", fPathName->Data());
252     //
253     ResetIO();
254     InitIO("");
255     // Some clean-up
256     members->Delete();
257     //
258     return kTRUE;
259 }
260
261 Bool_t AliESDInputHandlerRP::FinishEvent()
262 {
263     // Clean-up after each event
264     delete fDirR;  fDirR = 0;
265     AliESDInputHandler::FinishEvent();
266     return kTRUE;
267 }
268
269 void AliESDInputHandlerRP::ResetIO()
270 {
271 // Delete trees and files
272     fRTrees->Delete();
273     fRFiles->Delete();
274     fExtension="";
275 }
276
277 TTree* AliESDInputHandlerRP::GetTreeR(char* det)
278 {
279 // Return pointer to RecPoint tree for detector det
280     TNamed* entry = (TNamed*) (fDetectors->FindObject(det));
281     if (!entry) {
282         AliWarning(Form("AliESDInputHandlerRP: No RecPoints for detector %s available \n", det));
283         return 0;
284     } else {
285         Int_t ien = entry->GetUniqueID();
286         return ((TTree*) (fRTrees->At(ien)));
287     }
288 }