Improving event printout
[u/mrichter/AliRoot.git] / STEER / 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 }
6e2a8152 101 if (!file) AliFatal(Form("AliESDInputHandlerRP: %s.RecPoints.root not found in %s ! \n", det->GetName(), fPathName->Data()));
80d90f25 102 fRFiles->Add(file);
103 }
4d2a7370 104
6e2a8152 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
4d2a7370 111
a847da94 112 // Reset the event number
113 fEventNumber = -1;
114 fFileNumber = 0;
80d90f25 115 // Get number of events from esd tree
e6fac821 116 printf("AliESDInputHandlerRP::Init() %d %d\n",__LINE__, fNEvents);
a847da94 117 return kTRUE;
118}
119
120Bool_t AliESDInputHandlerRP::BeginEvent(Long64_t entry)
121{
122 // Begin the next event
80f41a3d 123 //
a847da94 124 if (entry == -1) {
125 fEventNumber++;
126 entry = fEventNumber;
127 } else {
128 fEventNumber = entry;
129 }
130
4d2a7370 131 if (entry >= fNEvents) {
132 AliWarning(Form("AliESDInputHandlerRP: Event number out of range %5d %5d\n", entry, fNEvents));
a847da94 133 return kFALSE;
134 }
80f41a3d 135
136 LoadEvent(entry);
137
138 // Delegate to base class
139 return AliESDInputHandler::BeginEvent(entry);
140
a847da94 141}
142
f207215d 143Bool_t AliESDInputHandlerRP::LoadEvent(Int_t iev)
a847da94 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
80d90f25 159 TIter next(fRFiles);
160 TFile* file;
0d277acb 161 Int_t idx = 0;
30cd6a86 162
8ac4fa64 163 while ((file = (TFile*) next()))
80d90f25 164 {
165 file->GetObject(folder, fDirR);
0d277acb 166
80d90f25 167 if (!fDirR) {
168 AliWarning(Form("AliESDInputHandlerRP: Event #%5d not found\n", iev));
169 return kFALSE;
170 }
4d2a7370 171 TTree* tree = 0;
30cd6a86 172 fDirR->GetObject("TreeR", tree);
0d277acb 173 fRDirs ->AddAt(fDirR, idx );
174 fRTrees->AddAt(tree, idx++);
a847da94 175 }
a847da94 176 return kTRUE;
177}
178
179Bool_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();
80d90f25 190 TIter next(fDetectors);
191 TNamed* det;
a847da94 192 TFile* file;
8ac4fa64 193 while ((det = (TNamed*) next()))
80d90f25 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);
a847da94 202 }
203 return ok;
204}
205
206Bool_t AliESDInputHandlerRP::Notify(const char *path)
207{
208 // Notify about directory change
209 // The directory is taken from the 'path' argument
80d90f25 210 //
e6fac821 211 AliInfo(Form("Directory change %s \n", path));
80d90f25 212 // Get path to directory
a847da94 213 TString fileName(path);
1d3ace83 214
80d90f25 215 if(fileName.Contains("#")){
1d3ace83 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", "");
80d90f25 220 }
1d3ace83 221
80d90f25 222 //
1d3ace83 223 // At this point we have a path to the directory or to the archive anchor
a847da94 224 *fPathName = fileName;
80d90f25 225 //
226 // Now filter the files containing RecPoints *.RecPoints.*
a847da94 227
80d90f25 228 TSeqCollection* members;
30cd6a86 229
80d90f25 230
231 if (fIsArchive) {
232 // Archive
c38123b8 233 TFile* file = TFile::Open(fPathName->Data());
234 TArchiveFile* arch = file->GetArchive();
235 members = arch->GetMembers();
80d90f25 236 } else {
c38123b8 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 }
80d90f25 248 }
30cd6a86 249
80d90f25 250 TIter next(members);
251 TFile* entry;
ad61d1be 252 Int_t ien = 0;
30cd6a86 253 fDetectors->Delete();
4d2a7370 254
80d90f25 255 while ( (entry = (TFile*) next()) )
256 {
80d90f25 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();
30cd6a86 264 printf("Found file with RecPoints for %s \n", det.Data());
8ac4fa64 265 TNamed* ent = new TNamed(det.Data(), det.Data());
30cd6a86 266 fRTrees->AddAt(0, ien);
8ac4fa64 267 ent->SetUniqueID(ien++);
268 fDetectors->Add(ent);
80d90f25 269 }
e6fac821 270 if(tokens) delete tokens;
80d90f25 271 } // loop over files
4d2a7370 272
80d90f25 273
274 // Now we have the path and the list of detectors
a847da94 275
80d90f25 276 printf("AliESDInputHandlerRP::Notify() Path: %s\n", fPathName->Data());
277 //
a847da94 278 ResetIO();
279 InitIO("");
80d90f25 280 // Some clean-up
281 members->Delete();
4d2a7370 282
a847da94 283 return kTRUE;
284}
285
286Bool_t AliESDInputHandlerRP::FinishEvent()
287{
288 // Clean-up after each event
0d277acb 289 fRDirs->Delete();
a847da94 290 AliESDInputHandler::FinishEvent();
291 return kTRUE;
292}
293
294void AliESDInputHandlerRP::ResetIO()
295{
296// Delete trees and files
99e7aa7d 297 fRFiles->Clear("nodelete");
a847da94 298 fExtension="";
299}
ad61d1be 300
a4e31092 301TTree* AliESDInputHandlerRP::GetTreeR(const char* det)
ad61d1be 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}