]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliESDInputHandler.cxx
As part of the efforts of the combined PID subgroup we are continuing to develop...
[u/mrichter/AliRoot.git] / STEER / AliESDInputHandler.cxx
CommitLineData
d3dd3d14 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
933fd60f 23#include <TTree.h>
1aa09fd1 24#include <TChain.h>
25#include <TFile.h>
26#include <TArchiveFile.h>
27#include <TObjArray.h>
28#include <TSystem.h>
300d5701 29#include <TString.h>
30#include <TObjString.h>
80e1b60a 31#include <TProcessID.h>
f0f29480 32#include <TMap.h>
d3dd3d14 33
34#include "AliESDInputHandler.h"
35#include "AliESDEvent.h"
620ad672 36#include "AliESDfriend.h"
b2af62ab 37#include "AliVCuts.h"
d3dd3d14 38#include "AliESD.h"
1aa09fd1 39#include "AliRunTag.h"
40#include "AliEventTag.h"
300d5701 41#include "AliLog.h"
9006fe9c 42#include "AliESDpid.h"
d3dd3d14 43
44ClassImp(AliESDInputHandler)
45
c2b6979d 46static Option_t *gESDDataType = "ESD";
47
d3dd3d14 48//______________________________________________________________________________
6989bff3 49AliESDInputHandler::AliESDInputHandler() :
50 AliInputEventHandler(),
0cd61c1d 51 fEvent(0x0),
620ad672 52 fFriend(0x0),
67675e42 53 fESDpid(0x0),
1aa09fd1 54 fAnalysisType(0),
4d2a7370 55 fNEvents(0),
6ccd727b 56 fHLTEvent(0x0),
57 fHLTTree(0x0),
58 fUseHLT(kFALSE),
f0f29480 59 fTagCutSumm(0x0),
1aa09fd1 60 fUseTags(kFALSE),
61 fChainT(0),
62 fTreeT(0),
98c482f7 63 fRunTag(0),
cec9e119 64 fEventTag(0),
038879cc 65 fReadFriends(0),
b626cd71 66 fFriendFileName("AliESDfriends.root")
d3dd3d14 67{
68 // default constructor
69}
70
71//______________________________________________________________________________
72AliESDInputHandler::~AliESDInputHandler()
73{
1aa09fd1 74 // destructor
cec9e119 75 if (fRunTag) delete fRunTag;
9006fe9c 76 delete fESDpid;
d3dd3d14 77}
78
79//______________________________________________________________________________
80AliESDInputHandler::AliESDInputHandler(const char* name, const char* title):
67675e42 81 AliInputEventHandler(name, title), fEvent(0x0), fFriend(0x0), fESDpid(0x0), fAnalysisType(0),
cec9e119 82 fNEvents(0), fHLTEvent(0x0), fHLTTree(0x0), fUseHLT(kFALSE), fTagCutSumm(0x0), fUseTags(kFALSE), fChainT(0), fTreeT(0), fRunTag(0), fEventTag(0), fReadFriends(0), fFriendFileName("AliESDfriends.root")
d3dd3d14 83{
e12e402c 84 // Constructor
d3dd3d14 85}
86
aaf2d706 87//______________________________________________________________________________
1aa09fd1 88Bool_t AliESDInputHandler::Init(TTree* tree, Option_t* opt)
d3dd3d14 89{
80ce1b4e 90 //
300d5701 91 // Initialisation necessary for each new tree
80ce1b4e 92 //
1aa09fd1 93 fAnalysisType = opt;
7b2a359f 94 fTree = tree;
300d5701 95
6073f8c9 96 if (!fTree) return kFALSE;
cd843abf 97// fTree->GetEntry(0);
6ccd727b 98
b481861d 99
efea3f54 100 if (!fEvent) fEvent = new AliESDEvent();
60996693 101 fEvent->ReadFromTree(fTree);
4d2a7370 102 fNEvents = fTree->GetEntries();
b890a10d 103
7b2a359f 104 if (fMixingHandler) fMixingHandler->Init(tree, opt);
b890a10d 105
d3dd3d14 106 return kTRUE;
107}
108
aaf2d706 109//______________________________________________________________________________
6ccd727b 110Bool_t AliESDInputHandler::BeginEvent(Long64_t entry)
d3dd3d14 111{
9b98c4df 112
d3dd3d14 113 // Copy from old to new format if necessary
aaf2d706 114 static Bool_t called = kFALSE;
115 if (!called && fEventCuts && IsUserCallSelectionMask())
116 AliInfo(Form("The ESD input handler expects that the first task calls AliESDInputHandler::CheckSelectionMask() %s", fEventCuts->ClassName()));
d7749dec 117 AliESD* old = ((AliESDEvent*) fEvent)->GetAliESDOld();
118 if (old) {
cec9e119 119 ((AliESDEvent*)fEvent)->CopyFromOldESD();
120 old->Reset();
d7749dec 121 }
6ccd727b 122
123 if (fHLTTree) {
124 fHLTTree->GetEntry(entry);
125 }
126
f0ee78b0 127 fNewEvent = kTRUE;
b2af62ab 128 //
129 // Event selection
130 //
aaf2d706 131 fIsSelectedResult = 0;
132 if (fEventCuts && !IsUserCallSelectionMask())
133 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
620ad672 134 //
135 // Friends
136 ((AliESDEvent*)fEvent)->SetESDfriend(fFriend);
aaf2d706 137 called = kTRUE;
7b2a359f 138
cec9e119 139 if (fMixingHandler) fMixingHandler->BeginEvent(entry);
140 if (fUseTags && fRunTag) {
141 fEventTag = 0;
142 if (entry >= fRunTag->GetNEvents()) {
143 AliError(Form("Current event %d does not match max range from run tag: 0-%d", (Int_t)entry, fRunTag->GetNEvents()));
144 return kTRUE;
145 }
146 fEventTag = fRunTag->GetEventTag(entry);
147 }
d7749dec 148 return kTRUE;
d3dd3d14 149}
150
aaf2d706 151//______________________________________________________________________________
152void AliESDInputHandler::CheckSelectionMask()
153{
154// This method can be called by a task only if IsUserCallSelectionMask is true.
155 if (!fEventCuts || !IsUserCallSelectionMask()) return;
b2747f93 156 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
aaf2d706 157}
158
159//______________________________________________________________________________
e12e402c 160Bool_t AliESDInputHandler::FinishEvent()
161{
162 // Finish the event
b890a10d 163 if(fEvent)fEvent->Reset();
7b2a359f 164 if (fMixingHandler) fMixingHandler->FinishEvent();
b890a10d 165 return kTRUE;
6989bff3 166}
167
aaf2d706 168//______________________________________________________________________________
1aa09fd1 169Bool_t AliESDInputHandler::Notify(const char* path)
170{
cec9e119 171 // Notify a directory change
172 static Bool_t firsttime = kFALSE;
173 AliInfo(Form("Directory change %s \n", path));
174 //
175 // Handle the friends first
176 //
177 if (!fTree->FindBranch("ESDfriend.") && fReadFriends) {
178 // Try to add ESDfriend. branch as friend
179 TString esdTreeFName, esdFriendTreeFName;
180 esdTreeFName = (fTree->GetCurrentFile())->GetName();
63dbb192 181
182 if(esdFriendTreeFName.Contains("AliESDs.root")) {
183 esdFriendTreeFName.ReplaceAll("AliESDs.root", fFriendFileName.Data());
184 } else if(esdFriendTreeFName.Contains("AliESDs_wSDD.root")) {
185 esdFriendTreeFName.ReplaceAll("AliESDs_wSDD.root", fFriendFileName.Data());
186 }
187
cec9e119 188 TTree* cTree = fTree->GetTree();
189 if (!cTree) cTree = fTree;
190 cTree->AddFriend("esdFriendTree", esdFriendTreeFName.Data());
191 cTree->SetBranchStatus("ESDfriend.", 1);
80ce1b4e 192 fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
cec9e119 193 if (fFriend) cTree->SetBranchAddress("ESDfriend.", &fFriend);
194 }
195 //
196 //
197 SwitchOffBranches();
198 SwitchOnBranches();
199 fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
200 //
201 if (fUseHLT) {
202 // Get HLTesdTree from current file
203 TTree* cTree = fTree;
204 if (fTree->GetTree()) cTree = fTree->GetTree();
205 TFile* cFile = cTree->GetCurrentFile();
206 cFile->GetObject("HLTesdTree", fHLTTree);
50aaaf00 207
cec9e119 208 if (fHLTTree) {
209 if (!fHLTEvent) fHLTEvent = new AliESDEvent();
210 fHLTEvent->ReadFromTree(fHLTTree);
50aaaf00 211 }
cec9e119 212 }
50aaaf00 213
cec9e119 214 if (!fUseTags) {
215 if (fMixingHandler) fMixingHandler->Notify(path);
216 return kTRUE;
217 }
1aa09fd1 218
cec9e119 219 Bool_t zip = kFALSE;
1aa09fd1 220
cec9e119 221 // Setup the base path
222 TString pathName(path);
223 Int_t index = pathName.Index("#");
224 if (index>=0) {
225 zip = kTRUE;
226 pathName = pathName(0,index);
227 } else {
228 pathName = gSystem->DirName(pathName);
229 }
230 if (fTree->GetCurrentFile()->GetArchive()) zip = kTRUE;
231 if (pathName.IsNull()) pathName = "./";
232 printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
6ccd727b 233
cec9e119 234 if (fRunTag) {
235 fRunTag->Clear();
236 } else {
237 fRunTag = new AliRunTag();
238 }
1aa09fd1 239
cec9e119 240 const char* tagPattern = "ESD.tag.root";
241 TString sname;
242 TString tagFilename;
243 if (zip) {
244 TObjArray* arr = fTree->GetCurrentFile()->GetArchive()->GetMembers();
245 TIter next(arr);
246 TObject *objarchive;
247 while ((objarchive = next())) {
248 sname = objarchive->GetName();
249 if (sname.Contains(tagPattern)) {
250 tagFilename = pathName;
251 if (index>=0) tagFilename += "#";
252 else tagFilename += "/";
253 tagFilename += sname;
254 AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
255 break; // There should be only one such file in the archive
256 }//pattern check
257 } // archive file loop
258 } else {
259 void * dirp = gSystem->OpenDirectory(pathName.Data());
260 while(1) {
261 sname = gSystem->GetDirEntry(dirp);
262 if (sname.IsNull()) break;
263 if (sname.Contains(tagPattern)) {
264 tagFilename = pathName;
265 tagFilename += "/";
266 tagFilename += sname;
267 AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
268 break;
269 }//pattern check
270 }//directory loop
62e57953 271 gSystem->FreeDirectory(dirp);
cec9e119 272 }
273 if (tagFilename.IsNull()) {
274 if (firsttime) AliWarning(Form("Tag file not found in directory: %s", pathName.Data()));
275 firsttime = kFALSE;
276 delete fRunTag; fRunTag = 0;
1aa09fd1 277 return kTRUE;
cec9e119 278 }
279 TFile *tagfile = TFile::Open(tagFilename);
280 if (!tagfile) {
281 AliError(Form("Cannot open tag file: %s", tagFilename.Data()));
282 delete fRunTag; fRunTag = 0;
283 return kTRUE;
284 }
285 fTreeT = (TTree*)tagfile->Get("T"); // file is the owner
286 if (!fTreeT) {
287 AliError(Form("Cannot get tree of tags from file: %s", tagFilename.Data()));
288 delete fRunTag; fRunTag = 0;
289 return kTRUE;
290 }
291
292 fTreeT->SetBranchAddress("AliTAG",&fRunTag);
293 fTreeT->GetEntry(0);
294 delete tagfile;
295 // Notify the mixing handler after the tags are loaded
296 if (fMixingHandler) fMixingHandler->Notify(path);
297 return kTRUE;
1aa09fd1 298}
299
aaf2d706 300//______________________________________________________________________________
c2b6979d 301Option_t *AliESDInputHandler::GetDataType() const
302{
303// Returns handled data type.
304 return gESDDataType;
305}
f0f29480 306
aaf2d706 307//______________________________________________________________________________
f0f29480 308Int_t AliESDInputHandler::GetNEventAcceptedInFile()
309{
310 // Get number of events in file accepted by the tag cuts
311 // return -1 if no info is available
312 if (!fTagCutSumm) {
313 TList *luo = fTree->GetUserInfo();
314 if (!luo) {
315 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
316 return -1;
317 }
318 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
319 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
320 if (fTagCutSumm) break;
321 }
322 if (!fTagCutSumm) {
323 AliInfo(Form("No tag summary map in input tree\n"));
324 return -1;
325 }
326 }
327
328 TObjString *ostr = 0;
329 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
330 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
331 else {
332 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
333 return -1;
334 }
335 char *iTagInfo;
336 iTagInfo = strdup(ostr->GetString().Data());
337
338 Int_t iAcc = atoi(strtok(iTagInfo, ","));
339
340 AliInfo(Form("Got %i accepted events for file %s", iAcc, fTree->GetCurrentFile()->GetName()));
341
342 free(iTagInfo);
343
344 return iAcc;
345}
aaf2d706 346
347//______________________________________________________________________________
f0f29480 348Int_t AliESDInputHandler::GetNEventRejectedInFile()
349{
350 // Get number of events in file rejected by the tag cuts
351 // return -1 if no info is available
352 if (!fTagCutSumm) {
353 TList *luo = fTree->GetUserInfo();
354 if (!luo) {
355 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
356 return -1;
357 }
358 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
359 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
360 if (fTagCutSumm) break;
361 }
362 if (!fTagCutSumm) {
363 AliInfo(Form("No tag summary map in input tree\n"));
364 return -1;
365 }
366 }
367
368 TObjString *ostr = 0;
369 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
370 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
371 else {
372 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
373 return -1;
374 }
375 char *iTagInfo;
376 iTagInfo = strdup(ostr->GetString().Data());
377
378 strtok(iTagInfo, ",");
379 Int_t iRej = atoi(strtok(NULL, ","));
380
381 AliInfo(Form("Got %i accepted events for file %s", iRej, fTree->GetCurrentFile()->GetName()));
382
383 free(iTagInfo);
384
385 return iRej;
386}
aaf2d706 387
388//______________________________________________________________________________
5e6a3170 389Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal, Int_t *aAccepted, Int_t *aRejected)
c334d0d3 390{
391 // Get number of events in the full chain
392 // Count accepted and rejected events
393 // return kFALSE if no info is available
394 if (!fTagCutSumm) {
395 TList *luo = fTree->GetUserInfo();
396 if (!luo) {
397 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
398 return kFALSE;
399 }
400 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
401 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
402 if (fTagCutSumm) break;
403 }
404 if (!fTagCutSumm) {
405 AliInfo(Form("No tag summary map in input tree\n"));
406 return kFALSE;
407 }
408 }
409
410 TMapIter *tIter = new TMapIter(fTagCutSumm);
411
412 Int_t iTotList=0, iAccList=0, iRejList=0;
413
414 TObject *cobj;
9a587f77 415 while ((cobj = tIter->Next())) {
c334d0d3 416 TObjString *kstr = (TObjString *) cobj;
417 TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
418 // printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
419 char *iTagInfo;
420 iTagInfo = strdup(vstr->GetString().Data());
421
422 Int_t iAcc = atoi(strtok(iTagInfo, ","));
423 Int_t iRej = atoi(strtok(NULL, ","));
96f4e3cb 424 free(iTagInfo);
c334d0d3 425
426 iAccList += iAcc;
427 iRejList += iRej;
428 iTotList += (iAcc+iRej);
429 }
430
431 *aTotal = iTotList;
432 *aAccepted = iAccList;
433 *aRejected = iRejList;
434
435 return kTRUE;
436}
437
aaf2d706 438//______________________________________________________________________________
c334d0d3 439Int_t AliESDInputHandler::GetNFilesEmpty()
440{
441 // Count number of files in which all events were de-selected
442 // For such files Notify() will NOT be called
443 // return -1 if no info is available
444 if (!fTagCutSumm) {
445 TList *luo = fTree->GetUserInfo();
446 if (!luo) {
447 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
448 return -1;
449 }
450 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
451 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
452 if (fTagCutSumm) break;
453 }
454 if (!fTagCutSumm) {
455 AliInfo(Form("No tag summary map in input tree\n"));
456 return -1;
457 }
458 }
459
460 TMapIter *tIter = new TMapIter(fTagCutSumm);
461
462 Int_t iFilesEmpty = 0;
463
464 TObject *cobj;
9a587f77 465 while ((cobj = tIter->Next())) {
c334d0d3 466 TObjString *kstr = (TObjString *) cobj;
467 TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
468 // printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
469 char *iTagInfo;
470 iTagInfo = strdup(vstr->GetString().Data());
471
472 Int_t iAcc = atoi(strtok(iTagInfo, ","));
473 Int_t iRej = atoi(strtok(NULL, ","));
2e8cd32b 474 free(iTagInfo);
c334d0d3 475 if ((iAcc == 0) && ((iRej+iAcc)>0))
476 iFilesEmpty++;
477 }
478
479 return iFilesEmpty;
480
481}
70908d05 482
483//______________________________________________________________________________
484TObject *AliESDInputHandler::GetStatistics(Option_t *option) const
485{
486// Get the statistics histogram(s) from the physics selection object. This
487// should be called during FinishTaskOutput(). Option can be empty (default
488// statistics histogram) or BIN0.
489 if (!fEventCuts) return NULL;
490 TString opt(option);
491 opt.ToUpper();
492 if (opt=="BIN0") return fEventCuts->GetStatistics("BIN0");
493 else return fEventCuts->GetStatistics("ALL");
494}
9006fe9c 495
496//______________________________________________________________________________
497void AliESDInputHandler::CreatePIDResponse(Bool_t isMC/*=kFALSE*/)
498{
499 //
500 // create the pid response object if it does not exist yet
501 //
502 if (fESDpid) return;
503 fESDpid=new AliESDpid(isMC);
504
505}
506