]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliESDInputHandlerRP.cxx
Added protections to avoid crashes with Print() when ESDEvent is fetched from the...
[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 <TObjArray.h>
31 #include <TProcessID.h>
32 #include <TSystem.h>
33
34 #include "AliESDInputHandlerRP.h"
35 #include "AliESDEvent.h"
36 #include "AliESD.h"
37 #include "AliLog.h"
38
39 ClassImp(AliESDInputHandlerRP)
40
41 //______________________________________________________________________________
42 AliESDInputHandlerRP::AliESDInputHandlerRP() :
43     AliESDInputHandler(),
44     fRTrees(   new TObjArray()),
45     fRDirs (   new TObjArray()),
46     fRFiles(   new TList()),
47     fDetectors(new TList()),
48     fDirR(0),
49     fEventNumber(-1),
50     fFileNumber(0),
51     fEventsPerFile(0),
52     fExtension(""),
53     fPathName(new TString("./")),
54     fIsArchive(kFALSE)
55 {
56   // Default constructor
57 }
58
59
60 //______________________________________________________________________________
61 AliESDInputHandlerRP::AliESDInputHandlerRP(const char* name, const char* title):
62     AliESDInputHandler(name, title),
63     fRTrees(   new TObjArray()),
64     fRDirs (   new TObjArray()),
65     fRFiles(   new TList()),
66     fDetectors(new TList()),
67     fDirR(0),
68     fEventNumber(-1),
69     fFileNumber(0),
70     fEventsPerFile(0),
71     fExtension(""),
72     fPathName(new TString("./")),
73     fIsArchive(kFALSE)
74 {
75     // Constructor
76 }
77
78 //______________________________________________________________________________
79 AliESDInputHandlerRP::~AliESDInputHandlerRP() 
80 {
81   // Destructor
82 }
83
84 Bool_t AliESDInputHandlerRP::Init(Option_t* opt)
85 {
86     //
87     // Initialize input
88     //
89     if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
90     //
91     TIter next(fDetectors);
92     TNamed* det;
93     TFile* file = 0;
94     while ((det = (TNamed*) next()))
95     {
96         if (!fIsArchive) {
97             file = TFile::Open(Form("%s%s.RecPoints.root", fPathName->Data(), det->GetName()));
98         } else {
99             file = TFile::Open(Form("%s#%s.RecPoints.root", fPathName->Data(), det->GetName()));
100         }
101         if (!file) AliFatal(Form("AliESDInputHandlerRP: %s.RecPoints.root not found in %s ! \n", det->GetName(), fPathName->Data()));
102         fRFiles->Add(file);
103     }
104
105     if (file) {
106         fEventsPerFile = file->GetNkeys() - file->GetNProcessIDs();
107     } else {
108         AliFatal(Form("AliESDInputHandlerRP: No file with RecPoints found in %s ! \n", fPathName->Data()));
109     }
110     
111
112     // Reset the event number
113     fEventNumber      = -1;
114     fFileNumber       =  0;
115     // Get number of events from esd tree 
116     printf("AliESDInputHandlerRP::Init() %d %d\n",__LINE__, fNEvents);
117     return kTRUE;
118 }
119
120 Bool_t AliESDInputHandlerRP::BeginEvent(Long64_t entry)
121 {
122     // Begin the next event
123     //
124     if (entry == -1) {
125         fEventNumber++;
126         entry = fEventNumber;
127     } else {
128         fEventNumber = entry;
129     }
130     
131     if (entry >= fNEvents) {
132         AliWarning(Form("AliESDInputHandlerRP: Event number out of range %5d %5d\n", entry, fNEvents));
133         return kFALSE;
134     }
135     
136     LoadEvent(entry);
137
138     // Delegate to base class
139     return AliESDInputHandler::BeginEvent(entry);
140
141 }
142
143 Bool_t AliESDInputHandlerRP::LoadEvent(Int_t iev)
144 {
145     // Load the event number iev
146     //
147     // Calculate the file number
148     Int_t inew  = iev / fEventsPerFile;
149     if (inew != fFileNumber) {
150         fFileNumber = inew;
151         if (!OpenFile(fFileNumber)){
152             return kFALSE;
153         }
154     }
155     // Folder name
156     char folder[20];
157     sprintf(folder, "Event%d", iev);
158     // Tree R
159     TIter next(fRFiles);
160     TFile* file;
161     Int_t idx  = 0;
162     
163     while ((file = (TFile*) next()))
164     {
165         file->GetObject(folder, fDirR);
166         
167         if (!fDirR) {
168             AliWarning(Form("AliESDInputHandlerRP: Event #%5d not found\n", iev));
169             return kFALSE;
170         }
171         TTree* tree = 0;
172         fDirR->GetObject("TreeR", tree);
173         fRDirs ->AddAt(fDirR, idx  );
174         fRTrees->AddAt(tree,  idx++);
175     }
176     return kTRUE;
177 }
178
179 Bool_t AliESDInputHandlerRP::OpenFile(Int_t i)
180 {
181     // Open file i
182     Bool_t ok = kTRUE;
183     if (i > 0) {
184         fExtension = Form("%d", i);
185     } else {
186         fExtension = "";
187     }
188     
189     fRFiles->Delete();
190     TIter next(fDetectors);
191     TNamed* det;
192     TFile* file;
193     while ((det = (TNamed*) next()))
194     {
195         if (!fIsArchive) {
196             file = TFile::Open(Form("%s%s.RecPoints%s.root", fPathName->Data(), det->GetName(), fExtension));
197         } else {
198             file = TFile::Open(Form("%s#%s.RecPoints%s.root", fPathName->Data(), det->GetName(), fExtension));
199         }
200         if (!file) AliFatal(Form("AliESDInputHandlerRP: RecPoints.root not found in %s ! \n", fPathName->Data()));
201         fRFiles->Add(file);
202     }
203     return ok;
204 }
205
206 Bool_t AliESDInputHandlerRP::Notify(const char *path)
207 {
208   // Notify about directory change
209   // The directory is taken from the 'path' argument
210   // 
211     AliInfo(Form("Directory change %s \n", path));
212     // Get path to directory
213     TString fileName(path);
214
215     if(fileName.Contains("#")){
216     // If this is an archive it will contain a # 
217       fIsArchive = kTRUE;
218     } else  if(fileName.Contains("AliESDs.root")){
219       fileName.ReplaceAll("AliESDs.root", "");
220     }
221
222     //
223     // At this point we have a path to the directory or to the archive anchor
224     *fPathName = fileName;
225     //
226     // Now filter the files containing RecPoints *.RecPoints.*
227
228     TSeqCollection* members;
229
230     
231     if (fIsArchive) {
232         // Archive
233       TFile* file = TFile::Open(fPathName->Data());
234       TArchiveFile* arch = file->GetArchive();
235       members = arch->GetMembers();
236     } else {
237         // Directory or alien archive
238       if (fileName.BeginsWith("alien:")) {
239         TFile* file = TFile::Open(Form("%s/root_archive.zip", fPathName->Data()));
240         TArchiveFile* arch = file->GetArchive();
241         members = arch->GetMembers();
242       } else {  
243         TString wd = gSystem->WorkingDirectory();
244         TSystemDirectory dir(".", fPathName->Data());
245         members = dir.GetListOfFiles();
246         gSystem->cd(wd);
247       }  
248     }
249
250     TIter next(members);
251     TFile* entry;
252     Int_t ien = 0;
253     fDetectors->Delete();
254     
255     while ( (entry = (TFile*) next()) )
256     {
257         TString name(entry->GetName());
258         TObjArray* tokens = name.Tokenize(".");
259         Int_t ntok = tokens->GetEntries();
260         if (ntok <= 1) continue;
261         TString str = ((TObjString*) tokens->At(1))->GetString();
262         if (!(strcmp(str.Data(), "RecPoints"))){
263             TString det = ((TObjString*) tokens->At(0))->GetString();
264             printf("Found file with RecPoints for %s \n", det.Data());
265             TNamed* ent = new TNamed(det.Data(), det.Data());
266             fRTrees->AddAt(0, ien);
267             ent->SetUniqueID(ien++);
268             fDetectors->Add(ent);
269         }
270         if(tokens) delete tokens;
271     } // loop over files
272
273
274     // Now we have the path and the list of detectors
275     
276     printf("AliESDInputHandlerRP::Notify() Path: %s\n", fPathName->Data());
277     //
278     ResetIO();
279     InitIO("");
280     // Some clean-up
281     members->Delete();
282
283     return kTRUE;
284 }
285
286 Bool_t AliESDInputHandlerRP::FinishEvent()
287 {
288     // Clean-up after each event
289     fRDirs->Delete();
290     AliESDInputHandler::FinishEvent();
291     return kTRUE;
292 }
293
294 void AliESDInputHandlerRP::ResetIO()
295 {
296 // Delete trees and files
297     fRFiles->Clear("nodelete");
298     fExtension="";
299 }
300
301 TTree* AliESDInputHandlerRP::GetTreeR(const char* det)
302 {
303 // Return pointer to RecPoint tree for detector det
304     TNamed* entry = (TNamed*) (fDetectors->FindObject(det));
305     if (!entry) {
306         AliWarning(Form("AliESDInputHandlerRP: No RecPoints for detector %s available \n", det));
307         return 0;
308     } else {
309         Int_t ien = entry->GetUniqueID();
310         return ((TTree*) (fRTrees->At(ien)));
311     }
312 }