]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliESDInputHandlerRP.cxx
Getter for the beam energy
[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     fRTrees->Clear();
156     
157     TIter next(fRFiles);
158     TFile* file;
159     while ((file = (TFile*) next()))
160     {
161         file->GetObject(folder, fDirR);
162         if (!fDirR) {
163             AliWarning(Form("AliESDInputHandlerRP: Event #%5d not found\n", iev));
164             return kFALSE;
165         }
166         TTree* tree;
167         fDirR ->GetObject("TreeR", tree);
168         fRTrees->Add(tree);
169     }
170     return kTRUE;
171 }
172
173 Bool_t AliESDInputHandlerRP::OpenFile(Int_t i)
174 {
175     // Open file i
176     Bool_t ok = kTRUE;
177     if (i > 0) {
178         fExtension = Form("%d", i);
179     } else {
180         fExtension = "";
181     }
182     
183     fRFiles->Delete();
184     TIter next(fDetectors);
185     TNamed* det;
186     TFile* file;
187     while ((det = (TNamed*) next()))
188     {
189         if (!fIsArchive) {
190             file = TFile::Open(Form("%s%s.RecPoints%s.root", fPathName->Data(), det->GetName(), fExtension));
191         } else {
192             file = TFile::Open(Form("%s#%s.RecPoints%s.root", fPathName->Data(), det->GetName(), fExtension));
193         }
194         if (!file) AliFatal(Form("AliESDInputHandlerRP: RecPoints.root not found in %s ! \n", fPathName->Data()));
195         fRFiles->Add(file);
196     }
197     return ok;
198 }
199
200 Bool_t AliESDInputHandlerRP::Notify(const char *path)
201 {
202   // Notify about directory change
203   // The directory is taken from the 'path' argument
204   // 
205     // Get path to directory
206     TString fileName(path);
207     if(fileName.Contains("AliESDs.root")){
208         fileName.ReplaceAll("AliESDs.root", "");
209     }
210     // If this is an archive it will contain a # 
211     if(fileName.Contains("#")){
212         fileName.ReplaceAll("#", "");
213     }
214     //
215     // At this point we have a path to the directory or to the archive
216     *fPathName = fileName;
217     //
218     // Now filter the files containing RecPoints *.RecPoints.*
219     fIsArchive = kFALSE;
220     if (fPathName->Contains(".zip")) fIsArchive = kTRUE;
221
222     TSeqCollection* members;
223     
224     if (fIsArchive) {
225         // Archive
226         TFile* file = TFile::Open(fPathName->Data());
227         TArchiveFile* arch = file->GetArchive();
228         members = arch->GetMembers();
229     } else {
230         // Directory
231         TSystemDirectory dir(".", fPathName->Data());
232         members = dir.GetListOfFiles();
233     }
234     
235     TIter next(members);
236     TFile* entry;
237     Int_t ien = 0;
238     while ( (entry = (TFile*) next()) )
239     {
240         printf("File %s \n", entry->GetName());
241         TString name(entry->GetName());
242         TObjArray* tokens = name.Tokenize(".");
243         Int_t ntok = tokens->GetEntries();
244         if (ntok <= 1) continue;
245         TString str = ((TObjString*) tokens->At(1))->GetString();
246         if (!(strcmp(str.Data(), "RecPoints"))){
247             TString det = ((TObjString*) tokens->At(0))->GetString();
248             printf("Name  %s \n", det.Data());
249             TNamed* ent = new TNamed(det.Data(), det.Data());
250             ent->SetUniqueID(ien++);
251             fDetectors->Add(ent);
252         }
253     } // loop over files
254     
255
256     // Now we have the path and the list of detectors
257     
258     printf("AliESDInputHandlerRP::Notify() Path: %s\n", fPathName->Data());
259     //
260     ResetIO();
261     InitIO("");
262     // Some clean-up
263     members->Delete();
264     //
265     return kTRUE;
266 }
267
268 Bool_t AliESDInputHandlerRP::FinishEvent()
269 {
270     // Clean-up after each event
271     delete fDirR;  fDirR = 0;
272     AliESDInputHandler::FinishEvent();
273     return kTRUE;
274 }
275
276 void AliESDInputHandlerRP::ResetIO()
277 {
278 // Delete trees and files
279     fRTrees->Delete();
280     fRFiles->Delete();
281     fExtension="";
282 }
283
284 TTree* AliESDInputHandlerRP::GetTreeR(char* det)
285 {
286 // Return pointer to RecPoint tree for detector det
287     TNamed* entry = (TNamed*) (fDetectors->FindObject(det));
288     if (!entry) {
289         AliWarning(Form("AliESDInputHandlerRP: No RecPoints for detector %s available \n", det));
290         return 0;
291     } else {
292         Int_t ien = entry->GetUniqueID();
293         return ((TTree*) (fRTrees->At(ien)));
294     }
295 }