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);
129 static Int_t prevRunNumber = -1;
130 if (prevRunNumber != fEvent->GetRunNumber() && NeedField()) {
131 fEvent->InitMagneticField();
132 prevRunNumber = fEvent->GetRunNumber();
137 fIsSelectedResult = 0;
138 if (fEventCuts && !IsUserCallSelectionMask())
139 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
142 ((AliESDEvent*)fEvent)->SetESDfriend(fFriend);
145 // set transient pointer to event inside tracks
146 fEvent->ConnectTracks();
148 if (fMixingHandler) fMixingHandler->BeginEvent(entry);
149 if (fUseTags && fRunTag) {
151 if (entry >= fRunTag->GetNEvents()) {
152 AliError(Form("Current event %d does not match max range from run tag: 0-%d", (Int_t)entry, fRunTag->GetNEvents()));
155 fEventTag = fRunTag->GetEventTag(entry);
160 //______________________________________________________________________________
161 void AliESDInputHandler::CheckSelectionMask()
163 // This method can be called by a task only if IsUserCallSelectionMask is true.
164 if (!fEventCuts || !IsUserCallSelectionMask()) return;
165 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
168 //______________________________________________________________________________
169 void AliESDInputHandler::ConnectFriends()
171 // Connect the friends tree as soon as available.
173 // Handle the friends first
175 TTree* cTree = fTree->GetTree();
176 if (!cTree) cTree = fTree;
177 if (!cTree->FindBranch("ESDfriend.")) {
178 // Try to add ESDfriend. branch as friend
179 TString esdFriendTreeFName;
180 esdFriendTreeFName = (fTree->GetCurrentFile())->GetName();
181 TString basename = gSystem->BaseName(esdFriendTreeFName);
182 Int_t index = basename.Index("#")+1;
183 basename.Remove(index);
184 basename += fFriendFileName;
185 TString dirname = gSystem->DirName(esdFriendTreeFName);
187 esdFriendTreeFName = dirname + basename;
189 cTree->AddFriend("esdFriendTree", esdFriendTreeFName.Data());
190 cTree->SetBranchStatus("ESDfriend.", 1);
191 fFriend = fEvent->FindFriend();
192 if (fFriend) cTree->SetBranchAddress("ESDfriend.", &fFriend);
196 //______________________________________________________________________________
197 Bool_t AliESDInputHandler::FinishEvent()
200 if(fEvent)fEvent->Reset();
201 if (fFriend) fFriend->Reset();
202 if (fMixingHandler) fMixingHandler->FinishEvent();
206 //______________________________________________________________________________
207 Bool_t AliESDInputHandler::Notify(const char* path)
209 // Notify a directory change
210 static Bool_t firsttime = kFALSE;
211 AliInfo(Form("Directory change %s \n", path));
213 // Handle the friends first
215 if (fReadFriends) ConnectFriends();
220 fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
221 fUserInfo=fTree->GetTree()->GetUserInfo();
225 // Get HLTesdTree from current file
226 TTree* cTree = fTree;
227 if (fTree->GetTree()) cTree = fTree->GetTree();
228 TFile* cFile = cTree->GetCurrentFile();
229 cFile->GetObject("HLTesdTree", fHLTTree);
232 if (!fHLTEvent) fHLTEvent = new AliESDEvent();
233 fHLTEvent->ReadFromTree(fHLTTree);
238 if (fMixingHandler) fMixingHandler->Notify(path);
244 // Setup the base path
245 TString pathName(path);
246 Int_t index = pathName.Index("#");
249 pathName = pathName(0,index);
251 pathName = gSystem->DirName(pathName);
253 if (fTree->GetCurrentFile()->GetArchive()) zip = kTRUE;
254 if (pathName.IsNull()) pathName = "./";
255 printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
260 fRunTag = new AliRunTag();
263 const char* tagPattern = "ESD.tag.root";
267 TObjArray* arr = fTree->GetCurrentFile()->GetArchive()->GetMembers();
270 while ((objarchive = next())) {
271 sname = objarchive->GetName();
272 if (sname.Contains(tagPattern)) {
273 tagFilename = pathName;
274 if (index>=0) tagFilename += "#";
275 else tagFilename += "/";
276 tagFilename += sname;
277 AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
278 break; // There should be only one such file in the archive
280 } // archive file loop
282 void * dirp = gSystem->OpenDirectory(pathName.Data());
284 sname = gSystem->GetDirEntry(dirp);
285 if (sname.IsNull()) break;
286 if (sname.Contains(tagPattern)) {
287 tagFilename = pathName;
289 tagFilename += sname;
290 AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
294 gSystem->FreeDirectory(dirp);
296 if (tagFilename.IsNull()) {
297 if (firsttime) AliWarning(Form("Tag file not found in directory: %s", pathName.Data()));
299 delete fRunTag; fRunTag = 0;
302 TFile *tagfile = TFile::Open(tagFilename);
304 AliError(Form("Cannot open tag file: %s", tagFilename.Data()));
305 delete fRunTag; fRunTag = 0;
308 fTreeT = (TTree*)tagfile->Get("T"); // file is the owner
310 AliError(Form("Cannot get tree of tags from file: %s", tagFilename.Data()));
311 delete fRunTag; fRunTag = 0;
315 fTreeT->SetBranchAddress("AliTAG",&fRunTag);
318 // Notify the mixing handler after the tags are loaded
319 if (fMixingHandler) fMixingHandler->Notify(path);
323 //______________________________________________________________________________
324 Option_t *AliESDInputHandler::GetDataType() const
326 // Returns handled data type.
330 //______________________________________________________________________________
331 Int_t AliESDInputHandler::GetNEventAcceptedInFile()
333 // Get number of events in file accepted by the tag cuts
334 // return -1 if no info is available
336 TList *luo = fTree->GetUserInfo();
338 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
341 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
342 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
343 if (fTagCutSumm) break;
346 AliInfo(Form("No tag summary map in input tree\n"));
351 TObjString *ostr = 0;
352 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
353 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
355 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
359 iTagInfo = strdup(ostr->GetString().Data());
361 Int_t iAcc = atoi(strtok(iTagInfo, ","));
363 AliInfo(Form("Got %i accepted events for file %s", iAcc, fTree->GetCurrentFile()->GetName()));
370 //______________________________________________________________________________
371 Int_t AliESDInputHandler::GetNEventRejectedInFile()
373 // Get number of events in file rejected by the tag cuts
374 // return -1 if no info is available
376 TList *luo = fTree->GetUserInfo();
378 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
381 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
382 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
383 if (fTagCutSumm) break;
386 AliInfo(Form("No tag summary map in input tree\n"));
391 TObjString *ostr = 0;
392 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
393 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
395 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
399 iTagInfo = strdup(ostr->GetString().Data());
401 strtok(iTagInfo, ",");
402 Int_t iRej = atoi(strtok(NULL, ","));
404 AliInfo(Form("Got %i accepted events for file %s", iRej, fTree->GetCurrentFile()->GetName()));
411 //______________________________________________________________________________
412 Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal, Int_t *aAccepted, Int_t *aRejected)
414 // Get number of events in the full chain
415 // Count accepted and rejected events
416 // return kFALSE if no info is available
418 TList *luo = fTree->GetUserInfo();
420 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
423 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
424 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
425 if (fTagCutSumm) break;
428 AliInfo(Form("No tag summary map in input tree\n"));
433 TMapIter *tIter = new TMapIter(fTagCutSumm);
435 Int_t iTotList=0, iAccList=0, iRejList=0;
438 while ((cobj = tIter->Next())) {
439 TObjString *kstr = (TObjString *) cobj;
440 TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
441 // printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
443 iTagInfo = strdup(vstr->GetString().Data());
445 Int_t iAcc = atoi(strtok(iTagInfo, ","));
446 Int_t iRej = atoi(strtok(NULL, ","));
451 iTotList += (iAcc+iRej);
455 *aAccepted = iAccList;
456 *aRejected = iRejList;
461 //______________________________________________________________________________
462 Int_t AliESDInputHandler::GetNFilesEmpty()
464 // Count number of files in which all events were de-selected
465 // For such files Notify() will NOT be called
466 // return -1 if no info is available
468 TList *luo = fTree->GetUserInfo();
470 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
473 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
474 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
475 if (fTagCutSumm) break;
478 AliInfo(Form("No tag summary map in input tree\n"));
483 TMapIter *tIter = new TMapIter(fTagCutSumm);
485 Int_t iFilesEmpty = 0;
488 while ((cobj = tIter->Next())) {
489 TObjString *kstr = (TObjString *) cobj;
490 TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
491 // printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
493 iTagInfo = strdup(vstr->GetString().Data());
495 Int_t iAcc = atoi(strtok(iTagInfo, ","));
496 Int_t iRej = atoi(strtok(NULL, ","));
498 if ((iAcc == 0) && ((iRej+iAcc)>0))
506 //______________________________________________________________________________
507 TObject *AliESDInputHandler::GetStatistics(Option_t *option) const
509 // Get the statistics histogram(s) from the physics selection object. This
510 // should be called during FinishTaskOutput(). Option can be empty (default
511 // statistics histogram) or BIN0.
512 if (!fEventCuts) return NULL;
515 if (opt=="BIN0") return fEventCuts->GetStatistics("BIN0");
516 else return fEventCuts->GetStatistics("ALL");
519 //______________________________________________________________________________
520 void AliESDInputHandler::CreatePIDResponse(Bool_t isMC/*=kFALSE*/)
523 // create the pid response object if it does not exist yet
526 fESDpid=new AliESDpid(isMC);