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