]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliESDInputHandlerRP.cxx
Pass run related information (beam energy, type, mag. field)
[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 <TObjArray.h>
31 #include <TProcessID.h>
32
33 #include "AliESDInputHandlerRP.h"
34 #include "AliESDEvent.h"
35 #include "AliESD.h"
36 #include "AliLog.h"
37
38 ClassImp(AliESDInputHandlerRP)
39
40 //______________________________________________________________________________
41 AliESDInputHandlerRP::AliESDInputHandlerRP() :
42     AliESDInputHandler(),
43     fRTrees(   new TObjArray()),
44     fRFiles(   new TList()),
45     fDetectors(new TList()),
46     fDirR(0),
47     fEventNumber(-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 TObjArray()),
62     fRFiles(   new TList()),
63     fDetectors(new TList()),
64     fDirR(0),
65     fEventNumber(-1),
66     fFileNumber(0),
67     fEventsPerFile(0),
68     fExtension(""),
69     fPathName(new TString("./")),
70     fIsArchive(kFALSE)
71 {
72     // Constructor
73 }
74
75 //______________________________________________________________________________
76 AliESDInputHandlerRP::~AliESDInputHandlerRP() 
77 {
78   // Destructor
79 }
80
81 Bool_t AliESDInputHandlerRP::Init(Option_t* opt)
82 {
83     //
84     // Initialize input
85     //
86     if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
87     //
88     TIter next(fDetectors);
89     TNamed* det;
90     TFile* file = 0;
91     while ((det = (TNamed*) next()))
92     {
93         if (!fIsArchive) {
94             file = TFile::Open(Form("%s%s.RecPoints.root", fPathName->Data(), det->GetName()));
95         } else {
96             file = TFile::Open(Form("%s#%s.RecPoints.root", fPathName->Data(), det->GetName()));
97         }
98         if (!file) AliFatal(Form("AliESDInputHandlerRP: %s.RecPoints.root not found in %s ! \n", det->GetName(), fPathName->Data()));
99         fRFiles->Add(file);
100     }
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     printf("AliESDInputHandler::Init() %d %d\n",__LINE__, fNEvents);
114     return kTRUE;
115 }
116
117 Bool_t AliESDInputHandlerRP::BeginEvent(Long64_t entry)
118 {
119     // Begin the next event
120     // Delegate first to base class
121     AliESDInputHandler::BeginEvent(entry);
122 //
123     if (entry == -1) {
124         fEventNumber++;
125         entry = fEventNumber;
126     } else {
127         fEventNumber = entry;
128     }
129     
130     if (entry >= fNEvents) {
131         AliWarning(Form("AliESDInputHandlerRP: Event number out of range %5d %5d\n", entry, fNEvents));
132         return kFALSE;
133     }
134     return LoadEvent(entry);
135 }
136
137 Bool_t AliESDInputHandlerRP::LoadEvent(Int_t iev)
138 {
139     // Load the event number iev
140     //
141     // Calculate the file number
142     Int_t inew  = iev / fEventsPerFile;
143     if (inew != fFileNumber) {
144         fFileNumber = inew;
145         if (!OpenFile(fFileNumber)){
146             return kFALSE;
147         }
148     }
149     // Folder name
150     char folder[20];
151     sprintf(folder, "Event%d", iev);
152     // Tree R
153     TIter next(fRFiles);
154     TFile* file;
155     Int_t idx = 0;
156     
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 = 0;
165         fDirR->GetObject("TreeR", tree);
166         fRTrees->AddAt(tree, idx++);
167     }
168     return kTRUE;
169 }
170
171 Bool_t AliESDInputHandlerRP::OpenFile(Int_t i)
172 {
173     // Open file i
174     Bool_t ok = kTRUE;
175     if (i > 0) {
176         fExtension = Form("%d", i);
177     } else {
178         fExtension = "";
179     }
180     
181     fRFiles->Delete();
182     TIter next(fDetectors);
183     TNamed* det;
184     TFile* file;
185     while ((det = (TNamed*) next()))
186     {
187         if (!fIsArchive) {
188             file = TFile::Open(Form("%s%s.RecPoints%s.root", fPathName->Data(), det->GetName(), fExtension));
189         } else {
190             file = TFile::Open(Form("%s#%s.RecPoints%s.root", fPathName->Data(), det->GetName(), fExtension));
191         }
192         if (!file) AliFatal(Form("AliESDInputHandlerRP: RecPoints.root not found in %s ! \n", fPathName->Data()));
193         fRFiles->Add(file);
194     }
195     return ok;
196 }
197
198 Bool_t AliESDInputHandlerRP::Notify(const char *path)
199 {
200   // Notify about directory change
201   // The directory is taken from the 'path' argument
202   // 
203     // Get path to directory
204     TString fileName(path);
205     if(fileName.Contains("AliESDs.root")){
206         fileName.ReplaceAll("AliESDs.root", "");
207     }
208     // If this is an archive it will contain a # 
209     if(fileName.Contains("#")){
210         fileName.ReplaceAll("#", "");
211     }
212     //
213     // At this point we have a path to the directory or to the archive
214     *fPathName = fileName;
215     //
216     // Now filter the files containing RecPoints *.RecPoints.*
217     fIsArchive = kFALSE;
218     if (fPathName->Contains(".zip")) fIsArchive = kTRUE;
219
220     TSeqCollection* members;
221
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     fDetectors->Delete();
238     
239     while ( (entry = (TFile*) next()) )
240     {
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("Found file with RecPoints for %s \n", det.Data());
249             TNamed* ent = new TNamed(det.Data(), det.Data());
250             fRTrees->AddAt(0, ien);
251             ent->SetUniqueID(ien++);
252             fDetectors->Add(ent);
253         }
254     } // loop over files
255
256
257     // Now we have the path and the list of detectors
258     
259     printf("AliESDInputHandlerRP::Notify() Path: %s\n", fPathName->Data());
260     //
261     ResetIO();
262     InitIO("");
263     // Some clean-up
264     members->Delete();
265
266     return kTRUE;
267 }
268
269 Bool_t AliESDInputHandlerRP::FinishEvent()
270 {
271     // Clean-up after each event
272     delete fDirR;  fDirR = 0;
273     AliESDInputHandler::FinishEvent();
274     return kTRUE;
275 }
276
277 void AliESDInputHandlerRP::ResetIO()
278 {
279 // Delete trees and files
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 }