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