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"
43 ClassImp(AliESDInputHandler)
45 static Option_t *gESDDataType = "ESD";
47 //______________________________________________________________________________
48 AliESDInputHandler::AliESDInputHandler() :
49 AliInputEventHandler(),
65 fFriendFileName("AliESDfriends.root")
67 // default constructor
70 //______________________________________________________________________________
71 AliESDInputHandler::~AliESDInputHandler()
74 if (fRunTag) delete fRunTag;
77 //______________________________________________________________________________
78 AliESDInputHandler::AliESDInputHandler(const char* name, const char* title):
79 AliInputEventHandler(name, title), fEvent(0x0), fFriend(0x0), fESDpid(0x0), fAnalysisType(0),
80 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")
85 //______________________________________________________________________________
86 Bool_t AliESDInputHandler::Init(TTree* tree, Option_t* opt)
89 // Initialisation necessary for each new tree
94 if (!fTree) return kFALSE;
95 // fTree->GetEntry(0);
98 if (!fEvent) fEvent = new AliESDEvent();
99 fEvent->ReadFromTree(fTree);
100 fNEvents = fTree->GetEntries();
102 if (fMixingHandler) fMixingHandler->Init(tree, opt);
107 //______________________________________________________________________________
108 Bool_t AliESDInputHandler::BeginEvent(Long64_t entry)
111 // Copy from old to new format if necessary
112 static Bool_t called = kFALSE;
113 if (!called && fEventCuts && IsUserCallSelectionMask())
114 AliInfo(Form("The ESD input handler expects that the first task calls AliESDInputHandler::CheckSelectionMask() %s", fEventCuts->ClassName()));
115 AliESD* old = ((AliESDEvent*) fEvent)->GetAliESDOld();
117 ((AliESDEvent*)fEvent)->CopyFromOldESD();
122 fHLTTree->GetEntry(entry);
129 fIsSelectedResult = 0;
130 if (fEventCuts && !IsUserCallSelectionMask())
131 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
134 ((AliESDEvent*)fEvent)->SetESDfriend(fFriend);
137 if (fMixingHandler) fMixingHandler->BeginEvent(entry);
138 if (fUseTags && fRunTag) {
140 if (entry >= fRunTag->GetNEvents()) {
141 AliError(Form("Current event %d does not match max range from run tag: 0-%d", (Int_t)entry, fRunTag->GetNEvents()));
144 fEventTag = fRunTag->GetEventTag(entry);
149 //______________________________________________________________________________
150 void AliESDInputHandler::CheckSelectionMask()
152 // This method can be called by a task only if IsUserCallSelectionMask is true.
153 if (!fEventCuts || !IsUserCallSelectionMask()) return;
154 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
157 //______________________________________________________________________________
158 Bool_t AliESDInputHandler::FinishEvent()
161 if(fEvent)fEvent->Reset();
162 if (fMixingHandler) fMixingHandler->FinishEvent();
166 //______________________________________________________________________________
167 Bool_t AliESDInputHandler::Notify(const char* path)
169 // Notify a directory change
170 static Bool_t firsttime = kFALSE;
171 AliInfo(Form("Directory change %s \n", path));
173 // Handle the friends first
175 if (!fTree->FindBranch("ESDfriend.") && fReadFriends) {
176 // Try to add ESDfriend. branch as friend
177 TString esdTreeFName, esdFriendTreeFName;
178 esdTreeFName = (fTree->GetCurrentFile())->GetName();
179 esdFriendTreeFName = esdTreeFName;
180 esdFriendTreeFName.ReplaceAll("AliESDs.root", fFriendFileName.Data());
181 TTree* cTree = fTree->GetTree();
182 if (!cTree) cTree = fTree;
183 cTree->AddFriend("esdFriendTree", esdFriendTreeFName.Data());
184 cTree->SetBranchStatus("ESDfriend.", 1);
185 fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
186 if (fFriend) cTree->SetBranchAddress("ESDfriend.", &fFriend);
192 fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
195 // Get HLTesdTree from current file
196 TTree* cTree = fTree;
197 if (fTree->GetTree()) cTree = fTree->GetTree();
198 TFile* cFile = cTree->GetCurrentFile();
199 cFile->GetObject("HLTesdTree", fHLTTree);
202 if (!fHLTEvent) fHLTEvent = new AliESDEvent();
203 fHLTEvent->ReadFromTree(fHLTTree);
208 if (fMixingHandler) fMixingHandler->Notify(path);
214 // Setup the base path
215 TString pathName(path);
216 Int_t index = pathName.Index("#");
219 pathName = pathName(0,index);
221 pathName = gSystem->DirName(pathName);
223 if (fTree->GetCurrentFile()->GetArchive()) zip = kTRUE;
224 if (pathName.IsNull()) pathName = "./";
225 printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
230 fRunTag = new AliRunTag();
233 const char* tagPattern = "ESD.tag.root";
237 TObjArray* arr = fTree->GetCurrentFile()->GetArchive()->GetMembers();
240 while ((objarchive = next())) {
241 sname = objarchive->GetName();
242 if (sname.Contains(tagPattern)) {
243 tagFilename = pathName;
244 if (index>=0) tagFilename += "#";
245 else tagFilename += "/";
246 tagFilename += sname;
247 AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
248 break; // There should be only one such file in the archive
250 } // archive file loop
252 void * dirp = gSystem->OpenDirectory(pathName.Data());
254 sname = gSystem->GetDirEntry(dirp);
255 if (sname.IsNull()) break;
256 if (sname.Contains(tagPattern)) {
257 tagFilename = pathName;
259 tagFilename += sname;
260 AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
264 gSystem->FreeDirectory(dirp);
266 if (tagFilename.IsNull()) {
267 if (firsttime) AliWarning(Form("Tag file not found in directory: %s", pathName.Data()));
269 delete fRunTag; fRunTag = 0;
272 TFile *tagfile = TFile::Open(tagFilename);
274 AliError(Form("Cannot open tag file: %s", tagFilename.Data()));
275 delete fRunTag; fRunTag = 0;
278 fTreeT = (TTree*)tagfile->Get("T"); // file is the owner
280 AliError(Form("Cannot get tree of tags from file: %s", tagFilename.Data()));
281 delete fRunTag; fRunTag = 0;
285 fTreeT->SetBranchAddress("AliTAG",&fRunTag);
288 // Notify the mixing handler after the tags are loaded
289 if (fMixingHandler) fMixingHandler->Notify(path);
293 //______________________________________________________________________________
294 Option_t *AliESDInputHandler::GetDataType() const
296 // Returns handled data type.
300 //______________________________________________________________________________
301 Int_t AliESDInputHandler::GetNEventAcceptedInFile()
303 // Get number of events in file accepted by the tag cuts
304 // return -1 if no info is available
306 TList *luo = fTree->GetUserInfo();
308 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
311 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
312 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
313 if (fTagCutSumm) break;
316 AliInfo(Form("No tag summary map in input tree\n"));
321 TObjString *ostr = 0;
322 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
323 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
325 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
329 iTagInfo = strdup(ostr->GetString().Data());
331 Int_t iAcc = atoi(strtok(iTagInfo, ","));
333 AliInfo(Form("Got %i accepted events for file %s", iAcc, fTree->GetCurrentFile()->GetName()));
340 //______________________________________________________________________________
341 Int_t AliESDInputHandler::GetNEventRejectedInFile()
343 // Get number of events in file rejected by the tag cuts
344 // return -1 if no info is available
346 TList *luo = fTree->GetUserInfo();
348 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
351 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
352 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
353 if (fTagCutSumm) break;
356 AliInfo(Form("No tag summary map in input tree\n"));
361 TObjString *ostr = 0;
362 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
363 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
365 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
369 iTagInfo = strdup(ostr->GetString().Data());
371 strtok(iTagInfo, ",");
372 Int_t iRej = atoi(strtok(NULL, ","));
374 AliInfo(Form("Got %i accepted events for file %s", iRej, fTree->GetCurrentFile()->GetName()));
381 //______________________________________________________________________________
382 Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal, Int_t *aAccepted, Int_t *aRejected)
384 // Get number of events in the full chain
385 // Count accepted and rejected events
386 // return kFALSE if no info is available
388 TList *luo = fTree->GetUserInfo();
390 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
393 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
394 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
395 if (fTagCutSumm) break;
398 AliInfo(Form("No tag summary map in input tree\n"));
403 TMapIter *tIter = new TMapIter(fTagCutSumm);
405 Int_t iTotList=0, iAccList=0, iRejList=0;
408 while ((cobj = tIter->Next())) {
409 TObjString *kstr = (TObjString *) cobj;
410 TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
411 // printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
413 iTagInfo = strdup(vstr->GetString().Data());
415 Int_t iAcc = atoi(strtok(iTagInfo, ","));
416 Int_t iRej = atoi(strtok(NULL, ","));
420 iTotList += (iAcc+iRej);
424 *aAccepted = iAccList;
425 *aRejected = iRejList;
430 //______________________________________________________________________________
431 Int_t AliESDInputHandler::GetNFilesEmpty()
433 // Count number of files in which all events were de-selected
434 // For such files Notify() will NOT be called
435 // return -1 if no info is available
437 TList *luo = fTree->GetUserInfo();
439 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
442 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
443 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
444 if (fTagCutSumm) break;
447 AliInfo(Form("No tag summary map in input tree\n"));
452 TMapIter *tIter = new TMapIter(fTagCutSumm);
454 Int_t iFilesEmpty = 0;
457 while ((cobj = tIter->Next())) {
458 TObjString *kstr = (TObjString *) cobj;
459 TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
460 // printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
462 iTagInfo = strdup(vstr->GetString().Data());
464 Int_t iAcc = atoi(strtok(iTagInfo, ","));
465 Int_t iRej = atoi(strtok(NULL, ","));
467 if ((iAcc == 0) && ((iRej+iAcc)>0))
475 //______________________________________________________________________________
476 TObject *AliESDInputHandler::GetStatistics(Option_t *option) const
478 // Get the statistics histogram(s) from the physics selection object. This
479 // should be called during FinishTaskOutput(). Option can be empty (default
480 // statistics histogram) or BIN0.
481 if (!fEventCuts) return NULL;
484 if (opt=="BIN0") return fEventCuts->GetStatistics("BIN0");
485 else return fEventCuts->GetStatistics("ALL");