Protections added in GetEvent method.
[u/mrichter/AliRoot.git] / STEER / AliESDInputHandler.cxx
... / ...
CommitLineData
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
20// Author: Andreas Morsch, CERN
21//-------------------------------------------------------------------------
22
23#include <TTree.h>
24#include <TChain.h>
25#include <TFile.h>
26#include <TArchiveFile.h>
27#include <TObjArray.h>
28#include <TSystem.h>
29#include <TString.h>
30#include <TObjString.h>
31#include <TProcessID.h>
32#include <TMap.h>
33
34#include "AliESDInputHandler.h"
35#include "AliESDEvent.h"
36#include "AliVCuts.h"
37#include "AliESD.h"
38#include "AliRunTag.h"
39#include "AliEventTag.h"
40#include "AliLog.h"
41
42ClassImp(AliESDInputHandler)
43
44static Option_t *gESDDataType = "ESD";
45
46//______________________________________________________________________________
47AliESDInputHandler::AliESDInputHandler() :
48 AliInputEventHandler(),
49 fEvent(0x0),
50 fAnalysisType(0),
51 fNEvents(0),
52 fHLTEvent(0x0),
53 fHLTTree(0x0),
54 fUseHLT(kFALSE),
55 fTagCutSumm(0x0),
56 fUseTags(kFALSE),
57 fChainT(0),
58 fTreeT(0),
59 fRunTag(0)
60{
61 // default constructor
62}
63
64//______________________________________________________________________________
65AliESDInputHandler::~AliESDInputHandler()
66{
67 // destructor
68 // delete fEvent;
69}
70
71//______________________________________________________________________________
72AliESDInputHandler::AliESDInputHandler(const char* name, const char* title):
73 AliInputEventHandler(name, title), fEvent(0x0), fAnalysisType(0),
74 fNEvents(0), fHLTEvent(0x0), fHLTTree(0x0), fUseHLT(kFALSE), fTagCutSumm(0x0), fUseTags(kFALSE), fChainT(0), fTreeT(0), fRunTag(0)
75{
76 // Constructor
77}
78
79Bool_t AliESDInputHandler::Init(TTree* tree, Option_t* opt)
80{
81 // Initialisation necessary for each new tree
82 fAnalysisType = opt;
83 fTree = tree;
84
85 if (!fTree) return kFALSE;
86 fTree->GetEntry(0);
87
88 // Get pointer to ESD event
89 SwitchOffBranches();
90 SwitchOnBranches();
91
92 if (!fEvent) fEvent = new AliESDEvent();
93 fEvent->ReadFromTree(fTree);
94 fNEvents = fTree->GetEntries();
95
96
97 return kTRUE;
98}
99
100Bool_t AliESDInputHandler::BeginEvent(Long64_t entry)
101{
102 // Copy from old to new format if necessary
103 AliESD* old = ((AliESDEvent*) fEvent)->GetAliESDOld();
104 if (old) {
105 ((AliESDEvent*)fEvent)->CopyFromOldESD();
106 old->Reset();
107 }
108
109 if (fHLTTree) {
110 fHLTTree->GetEntry(entry);
111 }
112
113 fNewEvent = kTRUE;
114 //
115 // Event selection
116 //
117 if (fEventCuts)
118 fIsSelected = fEventCuts->IsSelected(fEvent);
119
120 return kTRUE;
121}
122
123Bool_t AliESDInputHandler::FinishEvent()
124{
125 // Finish the event
126 if(fEvent)fEvent->Reset();
127 return kTRUE;
128}
129
130Bool_t AliESDInputHandler::Notify(const char* path)
131{
132 // Notify a directory change
133 AliInfo(Form("Directory change %s \n", path));
134 //
135 if (fUseHLT) {
136 // Get HLTesdTree from current file
137 TTree* cTree = fTree;
138 if (fTree->GetTree()) cTree = fTree->GetTree();
139 TFile* cFile = cTree->GetCurrentFile();
140 cFile->GetObject("HLTesdTree", fHLTTree);
141
142 if (fHLTTree) {
143 if (!fHLTEvent) fHLTEvent = new AliESDEvent();
144 fHLTEvent->ReadFromTree(fHLTTree);
145 }
146 }
147
148 if (!fUseTags) return (kTRUE);
149
150 Bool_t zip = kFALSE;
151
152 TString fileName(path);
153 if(fileName.Contains("#AliESDs.root")){
154 zip = kTRUE;
155 }
156 else if (fileName.Contains("AliESDs.root")){
157 fileName.ReplaceAll("AliESDs.root", "");
158 }
159 else if(fileName.Contains("#AliAOD.root")){
160 zip = kTRUE;
161 }
162 else if(fileName.Contains("AliAOD.root")){
163 fileName.ReplaceAll("AliAOD.root", "");
164 }
165 else if(fileName.Contains("#galice.root")){
166 // For running with galice and kinematics alone...
167 zip = kTRUE;
168 }
169 else if(fileName.Contains("galice.root")){
170 // For running with galice and kinematics alone...
171 fileName.ReplaceAll("galice.root", "");
172 }
173
174
175 TString pathName("./");
176 if (fileName.Length() != 0) {
177 pathName = fileName;
178 }
179
180 printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
181
182 if (fRunTag) {
183 fRunTag->Clear();
184 } else {
185 fRunTag = new AliRunTag();
186 }
187
188 delete fTreeT; fTreeT = 0;
189
190 if (fChainT) {
191 delete fChainT;
192 fChainT = 0;
193 }
194
195 if (!fChainT) {
196 fChainT = new TChain("T");
197 }
198
199
200
201 const char* tagPattern = "ESD.tag.root";
202 const char* name = 0x0;
203 TString tagFilename;
204 if (zip) {
205 TFile* file = fTree->GetCurrentFile();
206 TArchiveFile* arch = file->GetArchive();
207 TObjArray* arr = arch->GetMembers();
208 TIter next(arr);
209
210 while ((file = (TFile*) next())) {
211 name = file->GetName();
212 if (strstr(name,tagPattern)) {
213 tagFilename = pathName.Data();
214 tagFilename += "#";
215 tagFilename += name;
216 fChainT->Add(tagFilename);
217 AliInfo(Form("Adding %s to tag chain \n", tagFilename.Data()));
218 }//pattern check
219 } // archive file loop
220 } else {
221 void * dirp = gSystem->OpenDirectory(pathName.Data());
222 while((name = gSystem->GetDirEntry(dirp))) {
223 if (strstr(name,tagPattern)) {
224 tagFilename = pathName.Data();
225 tagFilename += "/";
226 tagFilename += name;
227 fChainT->Add(tagFilename);
228 AliInfo(Form("Adding %s to tag chain \n", tagFilename.Data()));
229 }//pattern check
230 }//directory loop
231 }
232 fChainT->SetBranchAddress("AliTAG",&fRunTag);
233 fChainT->GetEntry(0);
234 return kTRUE;
235}
236
237
238
239Option_t *AliESDInputHandler::GetDataType() const
240{
241// Returns handled data type.
242 return gESDDataType;
243}
244
245Int_t AliESDInputHandler::GetNEventAcceptedInFile()
246{
247 // Get number of events in file accepted by the tag cuts
248 // return -1 if no info is available
249 if (!fTagCutSumm) {
250 TList *luo = fTree->GetUserInfo();
251 if (!luo) {
252 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
253 return -1;
254 }
255 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
256 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
257 if (fTagCutSumm) break;
258 }
259 if (!fTagCutSumm) {
260 AliInfo(Form("No tag summary map in input tree\n"));
261 return -1;
262 }
263 }
264
265 TObjString *ostr = 0;
266 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
267 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
268 else {
269 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
270 return -1;
271 }
272 char *iTagInfo;
273 iTagInfo = strdup(ostr->GetString().Data());
274
275 Int_t iAcc = atoi(strtok(iTagInfo, ","));
276
277 AliInfo(Form("Got %i accepted events for file %s", iAcc, fTree->GetCurrentFile()->GetName()));
278
279 free(iTagInfo);
280
281 return iAcc;
282}
283Int_t AliESDInputHandler::GetNEventRejectedInFile()
284{
285 // Get number of events in file rejected by the tag cuts
286 // return -1 if no info is available
287 if (!fTagCutSumm) {
288 TList *luo = fTree->GetUserInfo();
289 if (!luo) {
290 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
291 return -1;
292 }
293 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
294 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
295 if (fTagCutSumm) break;
296 }
297 if (!fTagCutSumm) {
298 AliInfo(Form("No tag summary map in input tree\n"));
299 return -1;
300 }
301 }
302
303 TObjString *ostr = 0;
304 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
305 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
306 else {
307 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
308 return -1;
309 }
310 char *iTagInfo;
311 iTagInfo = strdup(ostr->GetString().Data());
312
313 strtok(iTagInfo, ",");
314 Int_t iRej = atoi(strtok(NULL, ","));
315
316 AliInfo(Form("Got %i accepted events for file %s", iRej, fTree->GetCurrentFile()->GetName()));
317
318 free(iTagInfo);
319
320 return iRej;
321}
322Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal, Int_t *aAccepted, Int_t *aRejected)
323{
324 // Get number of events in the full chain
325 // Count accepted and rejected events
326 // return kFALSE if no info is available
327 if (!fTagCutSumm) {
328 TList *luo = fTree->GetUserInfo();
329 if (!luo) {
330 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
331 return kFALSE;
332 }
333 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
334 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
335 if (fTagCutSumm) break;
336 }
337 if (!fTagCutSumm) {
338 AliInfo(Form("No tag summary map in input tree\n"));
339 return kFALSE;
340 }
341 }
342
343 TMapIter *tIter = new TMapIter(fTagCutSumm);
344
345 Int_t iTotList=0, iAccList=0, iRejList=0;
346
347 TObject *cobj;
348 while ((cobj = tIter->Next())) {
349 TObjString *kstr = (TObjString *) cobj;
350 TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
351 // printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
352 char *iTagInfo;
353 iTagInfo = strdup(vstr->GetString().Data());
354
355 Int_t iAcc = atoi(strtok(iTagInfo, ","));
356 Int_t iRej = atoi(strtok(NULL, ","));
357
358 iAccList += iAcc;
359 iRejList += iRej;
360 iTotList += (iAcc+iRej);
361 }
362
363 *aTotal = iTotList;
364 *aAccepted = iAccList;
365 *aRejected = iRejList;
366
367 return kTRUE;
368}
369
370Int_t AliESDInputHandler::GetNFilesEmpty()
371{
372 // Count number of files in which all events were de-selected
373 // For such files Notify() will NOT be called
374 // return -1 if no info is available
375 if (!fTagCutSumm) {
376 TList *luo = fTree->GetUserInfo();
377 if (!luo) {
378 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
379 return -1;
380 }
381 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
382 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
383 if (fTagCutSumm) break;
384 }
385 if (!fTagCutSumm) {
386 AliInfo(Form("No tag summary map in input tree\n"));
387 return -1;
388 }
389 }
390
391 TMapIter *tIter = new TMapIter(fTagCutSumm);
392
393 Int_t iFilesEmpty = 0;
394
395 TObject *cobj;
396 while ((cobj = tIter->Next())) {
397 TObjString *kstr = (TObjString *) cobj;
398 TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
399 // printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
400 char *iTagInfo;
401 iTagInfo = strdup(vstr->GetString().Data());
402
403 Int_t iAcc = atoi(strtok(iTagInfo, ","));
404 Int_t iRej = atoi(strtok(NULL, ","));
405
406 if ((iAcc == 0) && ((iRej+iAcc)>0))
407 iFilesEmpty++;
408 }
409
410 return iFilesEmpty;
411
412}