1 /**************************************************************************
2 * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 //-------------------------------------------------------------------------
19 // Event handler for ESD input
20 // Author: Andreas Morsch, CERN
21 //-------------------------------------------------------------------------
26 #include <TArchiveFile.h>
27 #include <TObjArray.h>
30 #include <TObjString.h>
31 #include <TProcessID.h>
34 #include "AliESDInputHandler.h"
35 #include "AliESDEvent.h"
36 #include "AliESDfriend.h"
39 #include "AliRunTag.h"
40 #include "AliEventTag.h"
42 #include "AliESDpid.h"
44 ClassImp(AliESDInputHandler)
46 static Option_t *gESDDataType = "ESD";
48 //______________________________________________________________________________
49 AliESDInputHandler::AliESDInputHandler() :
50 AliInputEventHandler(),
66 fFriendFileName("AliESDfriends.root")
68 // default constructor
71 //______________________________________________________________________________
72 AliESDInputHandler::~AliESDInputHandler()
75 if (fRunTag) delete fRunTag;
79 //______________________________________________________________________________
80 AliESDInputHandler::AliESDInputHandler(const char* name, const char* title):
81 AliInputEventHandler(name, title), fEvent(0x0), fFriend(0x0), fESDpid(0x0), fAnalysisType(0),
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")
87 //______________________________________________________________________________
88 Bool_t AliESDInputHandler::Init(TTree* tree, Option_t* opt)
91 // Initialisation necessary for each new tree
96 if (!fTree) return kFALSE;
97 // fTree->GetEntry(0);
100 if (!fEvent) fEvent = new AliESDEvent();
101 fEvent->ReadFromTree(fTree);
102 fNEvents = fTree->GetEntries();
103 if (fReadFriends) ConnectFriends();
105 if (fMixingHandler) fMixingHandler->Init(tree, opt);
109 //______________________________________________________________________________
110 Bool_t AliESDInputHandler::BeginEvent(Long64_t entry)
113 // Copy from old to new format if necessary
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()));
117 AliESD* old = ((AliESDEvent*) fEvent)->GetAliESDOld();
119 ((AliESDEvent*)fEvent)->CopyFromOldESD();
124 fHLTTree->GetEntry(entry);
131 fIsSelectedResult = 0;
132 if (fEventCuts && !IsUserCallSelectionMask())
133 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
136 ((AliESDEvent*)fEvent)->SetESDfriend(fFriend);
139 if (fMixingHandler) fMixingHandler->BeginEvent(entry);
140 if (fUseTags && fRunTag) {
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()));
146 fEventTag = fRunTag->GetEventTag(entry);
151 //______________________________________________________________________________
152 void AliESDInputHandler::CheckSelectionMask()
154 // This method can be called by a task only if IsUserCallSelectionMask is true.
155 if (!fEventCuts || !IsUserCallSelectionMask()) return;
156 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
159 //______________________________________________________________________________
160 void AliESDInputHandler::ConnectFriends()
162 // Connect the friends tree as soon as available.
164 // Handle the friends first
166 if (!fTree->FindBranch("ESDfriend.")) {
167 // Try to add ESDfriend. branch as friend
168 TString esdFriendTreeFName;
169 esdFriendTreeFName = (fTree->GetCurrentFile())->GetName();
170 TString basename = gSystem->BaseName(esdFriendTreeFName);
171 Int_t index = basename.Index("#")+1;
172 basename.Remove(index);
173 basename += fFriendFileName;
174 TString dirname = gSystem->DirName(esdFriendTreeFName);
176 esdFriendTreeFName = dirname + basename;
178 TTree* cTree = fTree->GetTree();
179 if (!cTree) cTree = fTree;
180 cTree->AddFriend("esdFriendTree", esdFriendTreeFName.Data());
181 cTree->SetBranchStatus("ESDfriend.", 1);
182 fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
183 if (fFriend) cTree->SetBranchAddress("ESDfriend.", &fFriend);
187 //______________________________________________________________________________
188 Bool_t AliESDInputHandler::FinishEvent()
191 if(fEvent)fEvent->Reset();
192 if (fMixingHandler) fMixingHandler->FinishEvent();
196 //______________________________________________________________________________
197 Bool_t AliESDInputHandler::Notify(const char* path)
199 // Notify a directory change
200 static Bool_t firsttime = kFALSE;
201 AliInfo(Form("Directory change %s \n", path));
203 // Handle the friends first
205 if (fReadFriends) ConnectFriends();
210 fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
211 fUserInfo=fTree->GetTree()->GetUserInfo();
215 // Get HLTesdTree from current file
216 TTree* cTree = fTree;
217 if (fTree->GetTree()) cTree = fTree->GetTree();
218 TFile* cFile = cTree->GetCurrentFile();
219 cFile->GetObject("HLTesdTree", fHLTTree);
222 if (!fHLTEvent) fHLTEvent = new AliESDEvent();
223 fHLTEvent->ReadFromTree(fHLTTree);
228 if (fMixingHandler) fMixingHandler->Notify(path);
234 // Setup the base path
235 TString pathName(path);
236 Int_t index = pathName.Index("#");
239 pathName = pathName(0,index);
241 pathName = gSystem->DirName(pathName);
243 if (fTree->GetCurrentFile()->GetArchive()) zip = kTRUE;
244 if (pathName.IsNull()) pathName = "./";
245 printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
250 fRunTag = new AliRunTag();
253 const char* tagPattern = "ESD.tag.root";
257 TObjArray* arr = fTree->GetCurrentFile()->GetArchive()->GetMembers();
260 while ((objarchive = next())) {
261 sname = objarchive->GetName();
262 if (sname.Contains(tagPattern)) {
263 tagFilename = pathName;
264 if (index>=0) tagFilename += "#";
265 else tagFilename += "/";
266 tagFilename += sname;
267 AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
268 break; // There should be only one such file in the archive
270 } // archive file loop
272 void * dirp = gSystem->OpenDirectory(pathName.Data());
274 sname = gSystem->GetDirEntry(dirp);
275 if (sname.IsNull()) break;
276 if (sname.Contains(tagPattern)) {
277 tagFilename = pathName;
279 tagFilename += sname;
280 AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
284 gSystem->FreeDirectory(dirp);
286 if (tagFilename.IsNull()) {
287 if (firsttime) AliWarning(Form("Tag file not found in directory: %s", pathName.Data()));
289 delete fRunTag; fRunTag = 0;
292 TFile *tagfile = TFile::Open(tagFilename);
294 AliError(Form("Cannot open tag file: %s", tagFilename.Data()));
295 delete fRunTag; fRunTag = 0;
298 fTreeT = (TTree*)tagfile->Get("T"); // file is the owner
300 AliError(Form("Cannot get tree of tags from file: %s", tagFilename.Data()));
301 delete fRunTag; fRunTag = 0;
305 fTreeT->SetBranchAddress("AliTAG",&fRunTag);
308 // Notify the mixing handler after the tags are loaded
309 if (fMixingHandler) fMixingHandler->Notify(path);
313 //______________________________________________________________________________
314 Option_t *AliESDInputHandler::GetDataType() const
316 // Returns handled data type.
320 //______________________________________________________________________________
321 Int_t AliESDInputHandler::GetNEventAcceptedInFile()
323 // Get number of events in file accepted by the tag cuts
324 // return -1 if no info is available
326 TList *luo = fTree->GetUserInfo();
328 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
331 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
332 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
333 if (fTagCutSumm) break;
336 AliInfo(Form("No tag summary map in input tree\n"));
341 TObjString *ostr = 0;
342 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
343 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
345 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
349 iTagInfo = strdup(ostr->GetString().Data());
351 Int_t iAcc = atoi(strtok(iTagInfo, ","));
353 AliInfo(Form("Got %i accepted events for file %s", iAcc, fTree->GetCurrentFile()->GetName()));
360 //______________________________________________________________________________
361 Int_t AliESDInputHandler::GetNEventRejectedInFile()
363 // Get number of events in file rejected by the tag cuts
364 // return -1 if no info is available
366 TList *luo = fTree->GetUserInfo();
368 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
371 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
372 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
373 if (fTagCutSumm) break;
376 AliInfo(Form("No tag summary map in input tree\n"));
381 TObjString *ostr = 0;
382 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
383 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
385 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
389 iTagInfo = strdup(ostr->GetString().Data());
391 strtok(iTagInfo, ",");
392 Int_t iRej = atoi(strtok(NULL, ","));
394 AliInfo(Form("Got %i accepted events for file %s", iRej, fTree->GetCurrentFile()->GetName()));
401 //______________________________________________________________________________
402 Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal, Int_t *aAccepted, Int_t *aRejected)
404 // Get number of events in the full chain
405 // Count accepted and rejected events
406 // return kFALSE if no info is available
408 TList *luo = fTree->GetUserInfo();
410 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
413 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
414 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
415 if (fTagCutSumm) break;
418 AliInfo(Form("No tag summary map in input tree\n"));
423 TMapIter *tIter = new TMapIter(fTagCutSumm);
425 Int_t iTotList=0, iAccList=0, iRejList=0;
428 while ((cobj = tIter->Next())) {
429 TObjString *kstr = (TObjString *) cobj;
430 TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
431 // printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
433 iTagInfo = strdup(vstr->GetString().Data());
435 Int_t iAcc = atoi(strtok(iTagInfo, ","));
436 Int_t iRej = atoi(strtok(NULL, ","));
441 iTotList += (iAcc+iRej);
445 *aAccepted = iAccList;
446 *aRejected = iRejList;
451 //______________________________________________________________________________
452 Int_t AliESDInputHandler::GetNFilesEmpty()
454 // Count number of files in which all events were de-selected
455 // For such files Notify() will NOT be called
456 // return -1 if no info is available
458 TList *luo = fTree->GetUserInfo();
460 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
463 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
464 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
465 if (fTagCutSumm) break;
468 AliInfo(Form("No tag summary map in input tree\n"));
473 TMapIter *tIter = new TMapIter(fTagCutSumm);
475 Int_t iFilesEmpty = 0;
478 while ((cobj = tIter->Next())) {
479 TObjString *kstr = (TObjString *) cobj;
480 TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
481 // printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
483 iTagInfo = strdup(vstr->GetString().Data());
485 Int_t iAcc = atoi(strtok(iTagInfo, ","));
486 Int_t iRej = atoi(strtok(NULL, ","));
488 if ((iAcc == 0) && ((iRej+iAcc)>0))
496 //______________________________________________________________________________
497 TObject *AliESDInputHandler::GetStatistics(Option_t *option) const
499 // Get the statistics histogram(s) from the physics selection object. This
500 // should be called during FinishTaskOutput(). Option can be empty (default
501 // statistics histogram) or BIN0.
502 if (!fEventCuts) return NULL;
505 if (opt=="BIN0") return fEventCuts->GetStatistics("BIN0");
506 else return fEventCuts->GetStatistics("ALL");
509 //______________________________________________________________________________
510 void AliESDInputHandler::CreatePIDResponse(Bool_t isMC/*=kFALSE*/)
513 // create the pid response object if it does not exist yet
516 fESDpid=new AliESDpid(isMC);