fix to the proper NSD
[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"
d3dd3d14 42
43ClassImp(AliESDInputHandler)
44
c2b6979d 45static Option_t *gESDDataType = "ESD";
46
d3dd3d14 47//______________________________________________________________________________
6989bff3 48AliESDInputHandler::AliESDInputHandler() :
49 AliInputEventHandler(),
0cd61c1d 50 fEvent(0x0),
620ad672 51 fFriend(0x0),
67675e42 52 fESDpid(0x0),
1aa09fd1 53 fAnalysisType(0),
4d2a7370 54 fNEvents(0),
6ccd727b 55 fHLTEvent(0x0),
56 fHLTTree(0x0),
57 fUseHLT(kFALSE),
f0f29480 58 fTagCutSumm(0x0),
1aa09fd1 59 fUseTags(kFALSE),
60 fChainT(0),
61 fTreeT(0),
98c482f7 62 fRunTag(0),
cec9e119 63 fEventTag(0),
038879cc 64 fReadFriends(0),
b626cd71 65 fFriendFileName("AliESDfriends.root")
d3dd3d14 66{
67 // default constructor
68}
69
70//______________________________________________________________________________
71AliESDInputHandler::~AliESDInputHandler()
72{
1aa09fd1 73 // destructor
cec9e119 74 if (fRunTag) delete fRunTag;
d3dd3d14 75}
76
77//______________________________________________________________________________
78AliESDInputHandler::AliESDInputHandler(const char* name, const char* title):
67675e42 79 AliInputEventHandler(name, title), fEvent(0x0), fFriend(0x0), fESDpid(0x0), fAnalysisType(0),
cec9e119 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")
d3dd3d14 81{
e12e402c 82 // Constructor
d3dd3d14 83}
84
aaf2d706 85//______________________________________________________________________________
1aa09fd1 86Bool_t AliESDInputHandler::Init(TTree* tree, Option_t* opt)
d3dd3d14 87{
80ce1b4e 88 //
300d5701 89 // Initialisation necessary for each new tree
80ce1b4e 90 //
1aa09fd1 91 fAnalysisType = opt;
7b2a359f 92 fTree = tree;
300d5701 93
6073f8c9 94 if (!fTree) return kFALSE;
cd843abf 95// fTree->GetEntry(0);
6ccd727b 96
b481861d 97
efea3f54 98 if (!fEvent) fEvent = new AliESDEvent();
60996693 99 fEvent->ReadFromTree(fTree);
4d2a7370 100 fNEvents = fTree->GetEntries();
b890a10d 101
7b2a359f 102 if (fMixingHandler) fMixingHandler->Init(tree, opt);
b890a10d 103
d3dd3d14 104 return kTRUE;
105}
106
aaf2d706 107//______________________________________________________________________________
6ccd727b 108Bool_t AliESDInputHandler::BeginEvent(Long64_t entry)
d3dd3d14 109{
9b98c4df 110
d3dd3d14 111 // Copy from old to new format if necessary
aaf2d706 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()));
d7749dec 115 AliESD* old = ((AliESDEvent*) fEvent)->GetAliESDOld();
116 if (old) {
cec9e119 117 ((AliESDEvent*)fEvent)->CopyFromOldESD();
118 old->Reset();
d7749dec 119 }
6ccd727b 120
121 if (fHLTTree) {
122 fHLTTree->GetEntry(entry);
123 }
124
f0ee78b0 125 fNewEvent = kTRUE;
b2af62ab 126 //
127 // Event selection
128 //
aaf2d706 129 fIsSelectedResult = 0;
130 if (fEventCuts && !IsUserCallSelectionMask())
131 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
620ad672 132 //
133 // Friends
134 ((AliESDEvent*)fEvent)->SetESDfriend(fFriend);
aaf2d706 135 called = kTRUE;
7b2a359f 136
cec9e119 137 if (fMixingHandler) fMixingHandler->BeginEvent(entry);
138 if (fUseTags && fRunTag) {
139 fEventTag = 0;
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()));
142 return kTRUE;
143 }
144 fEventTag = fRunTag->GetEventTag(entry);
145 }
d7749dec 146 return kTRUE;
d3dd3d14 147}
148
aaf2d706 149//______________________________________________________________________________
150void AliESDInputHandler::CheckSelectionMask()
151{
152// This method can be called by a task only if IsUserCallSelectionMask is true.
153 if (!fEventCuts || !IsUserCallSelectionMask()) return;
b2747f93 154 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
aaf2d706 155}
156
157//______________________________________________________________________________
e12e402c 158Bool_t AliESDInputHandler::FinishEvent()
159{
160 // Finish the event
b890a10d 161 if(fEvent)fEvent->Reset();
7b2a359f 162 if (fMixingHandler) fMixingHandler->FinishEvent();
b890a10d 163 return kTRUE;
6989bff3 164}
165
aaf2d706 166//______________________________________________________________________________
1aa09fd1 167Bool_t AliESDInputHandler::Notify(const char* path)
168{
cec9e119 169 // Notify a directory change
170 static Bool_t firsttime = kFALSE;
171 AliInfo(Form("Directory change %s \n", path));
172 //
173 // Handle the friends first
174 //
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);
80ce1b4e 185 fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
cec9e119 186 if (fFriend) cTree->SetBranchAddress("ESDfriend.", &fFriend);
187 }
188 //
189 //
190 SwitchOffBranches();
191 SwitchOnBranches();
192 fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
193 //
194 if (fUseHLT) {
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);
50aaaf00 200
cec9e119 201 if (fHLTTree) {
202 if (!fHLTEvent) fHLTEvent = new AliESDEvent();
203 fHLTEvent->ReadFromTree(fHLTTree);
50aaaf00 204 }
cec9e119 205 }
50aaaf00 206
cec9e119 207 if (!fUseTags) {
208 if (fMixingHandler) fMixingHandler->Notify(path);
209 return kTRUE;
210 }
1aa09fd1 211
cec9e119 212 Bool_t zip = kFALSE;
1aa09fd1 213
cec9e119 214 // Setup the base path
215 TString pathName(path);
216 Int_t index = pathName.Index("#");
217 if (index>=0) {
218 zip = kTRUE;
219 pathName = pathName(0,index);
220 } else {
221 pathName = gSystem->DirName(pathName);
222 }
223 if (fTree->GetCurrentFile()->GetArchive()) zip = kTRUE;
224 if (pathName.IsNull()) pathName = "./";
225 printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
6ccd727b 226
cec9e119 227 if (fRunTag) {
228 fRunTag->Clear();
229 } else {
230 fRunTag = new AliRunTag();
231 }
1aa09fd1 232
cec9e119 233 const char* tagPattern = "ESD.tag.root";
234 TString sname;
235 TString tagFilename;
236 if (zip) {
237 TObjArray* arr = fTree->GetCurrentFile()->GetArchive()->GetMembers();
238 TIter next(arr);
239 TObject *objarchive;
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
249 }//pattern check
250 } // archive file loop
251 } else {
252 void * dirp = gSystem->OpenDirectory(pathName.Data());
253 while(1) {
254 sname = gSystem->GetDirEntry(dirp);
255 if (sname.IsNull()) break;
256 if (sname.Contains(tagPattern)) {
257 tagFilename = pathName;
258 tagFilename += "/";
259 tagFilename += sname;
260 AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
261 break;
262 }//pattern check
263 }//directory loop
62e57953 264 gSystem->FreeDirectory(dirp);
cec9e119 265 }
266 if (tagFilename.IsNull()) {
267 if (firsttime) AliWarning(Form("Tag file not found in directory: %s", pathName.Data()));
268 firsttime = kFALSE;
269 delete fRunTag; fRunTag = 0;
1aa09fd1 270 return kTRUE;
cec9e119 271 }
272 TFile *tagfile = TFile::Open(tagFilename);
273 if (!tagfile) {
274 AliError(Form("Cannot open tag file: %s", tagFilename.Data()));
275 delete fRunTag; fRunTag = 0;
276 return kTRUE;
277 }
278 fTreeT = (TTree*)tagfile->Get("T"); // file is the owner
279 if (!fTreeT) {
280 AliError(Form("Cannot get tree of tags from file: %s", tagFilename.Data()));
281 delete fRunTag; fRunTag = 0;
282 return kTRUE;
283 }
284
285 fTreeT->SetBranchAddress("AliTAG",&fRunTag);
286 fTreeT->GetEntry(0);
287 delete tagfile;
288 // Notify the mixing handler after the tags are loaded
289 if (fMixingHandler) fMixingHandler->Notify(path);
290 return kTRUE;
1aa09fd1 291}
292
aaf2d706 293//______________________________________________________________________________
c2b6979d 294Option_t *AliESDInputHandler::GetDataType() const
295{
296// Returns handled data type.
297 return gESDDataType;
298}
f0f29480 299
aaf2d706 300//______________________________________________________________________________
f0f29480 301Int_t AliESDInputHandler::GetNEventAcceptedInFile()
302{
303 // Get number of events in file accepted by the tag cuts
304 // return -1 if no info is available
305 if (!fTagCutSumm) {
306 TList *luo = fTree->GetUserInfo();
307 if (!luo) {
308 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
309 return -1;
310 }
311 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
312 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
313 if (fTagCutSumm) break;
314 }
315 if (!fTagCutSumm) {
316 AliInfo(Form("No tag summary map in input tree\n"));
317 return -1;
318 }
319 }
320
321 TObjString *ostr = 0;
322 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
323 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
324 else {
325 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
326 return -1;
327 }
328 char *iTagInfo;
329 iTagInfo = strdup(ostr->GetString().Data());
330
331 Int_t iAcc = atoi(strtok(iTagInfo, ","));
332
333 AliInfo(Form("Got %i accepted events for file %s", iAcc, fTree->GetCurrentFile()->GetName()));
334
335 free(iTagInfo);
336
337 return iAcc;
338}
aaf2d706 339
340//______________________________________________________________________________
f0f29480 341Int_t AliESDInputHandler::GetNEventRejectedInFile()
342{
343 // Get number of events in file rejected by the tag cuts
344 // return -1 if no info is available
345 if (!fTagCutSumm) {
346 TList *luo = fTree->GetUserInfo();
347 if (!luo) {
348 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
349 return -1;
350 }
351 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
352 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
353 if (fTagCutSumm) break;
354 }
355 if (!fTagCutSumm) {
356 AliInfo(Form("No tag summary map in input tree\n"));
357 return -1;
358 }
359 }
360
361 TObjString *ostr = 0;
362 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
363 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
364 else {
365 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
366 return -1;
367 }
368 char *iTagInfo;
369 iTagInfo = strdup(ostr->GetString().Data());
370
371 strtok(iTagInfo, ",");
372 Int_t iRej = atoi(strtok(NULL, ","));
373
374 AliInfo(Form("Got %i accepted events for file %s", iRej, fTree->GetCurrentFile()->GetName()));
375
376 free(iTagInfo);
377
378 return iRej;
379}
aaf2d706 380
381//______________________________________________________________________________
5e6a3170 382Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal, Int_t *aAccepted, Int_t *aRejected)
c334d0d3 383{
384 // Get number of events in the full chain
385 // Count accepted and rejected events
386 // return kFALSE if no info is available
387 if (!fTagCutSumm) {
388 TList *luo = fTree->GetUserInfo();
389 if (!luo) {
390 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
391 return kFALSE;
392 }
393 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
394 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
395 if (fTagCutSumm) break;
396 }
397 if (!fTagCutSumm) {
398 AliInfo(Form("No tag summary map in input tree\n"));
399 return kFALSE;
400 }
401 }
402
403 TMapIter *tIter = new TMapIter(fTagCutSumm);
404
405 Int_t iTotList=0, iAccList=0, iRejList=0;
406
407 TObject *cobj;
9a587f77 408 while ((cobj = tIter->Next())) {
c334d0d3 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());
412 char *iTagInfo;
413 iTagInfo = strdup(vstr->GetString().Data());
414
415 Int_t iAcc = atoi(strtok(iTagInfo, ","));
416 Int_t iRej = atoi(strtok(NULL, ","));
417
418 iAccList += iAcc;
419 iRejList += iRej;
420 iTotList += (iAcc+iRej);
421 }
422
423 *aTotal = iTotList;
424 *aAccepted = iAccList;
425 *aRejected = iRejList;
426
427 return kTRUE;
428}
429
aaf2d706 430//______________________________________________________________________________
c334d0d3 431Int_t AliESDInputHandler::GetNFilesEmpty()
432{
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
436 if (!fTagCutSumm) {
437 TList *luo = fTree->GetUserInfo();
438 if (!luo) {
439 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
440 return -1;
441 }
442 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
443 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
444 if (fTagCutSumm) break;
445 }
446 if (!fTagCutSumm) {
447 AliInfo(Form("No tag summary map in input tree\n"));
448 return -1;
449 }
450 }
451
452 TMapIter *tIter = new TMapIter(fTagCutSumm);
453
454 Int_t iFilesEmpty = 0;
455
456 TObject *cobj;
9a587f77 457 while ((cobj = tIter->Next())) {
c334d0d3 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());
461 char *iTagInfo;
462 iTagInfo = strdup(vstr->GetString().Data());
463
464 Int_t iAcc = atoi(strtok(iTagInfo, ","));
465 Int_t iRej = atoi(strtok(NULL, ","));
466
467 if ((iAcc == 0) && ((iRej+iAcc)>0))
468 iFilesEmpty++;
469 }
470
471 return iFilesEmpty;
472
473}
70908d05 474
475//______________________________________________________________________________
476TObject *AliESDInputHandler::GetStatistics(Option_t *option) const
477{
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;
482 TString opt(option);
483 opt.ToUpper();
484 if (opt=="BIN0") return fEventCuts->GetStatistics("BIN0");
485 else return fEventCuts->GetStatistics("ALL");
486}