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