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