]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/ESD/AliESDInputHandler.cxx
Update master to aliroot
[u/mrichter/AliRoot.git] / STEER / ESD / 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();
9d4be30f 103 if (fReadFriends) ConnectFriends();
b890a10d 104
7b2a359f 105 if (fMixingHandler) fMixingHandler->Init(tree, opt);
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 //
9e15d933 129 static Int_t prevRunNumber = -1;
130 if (prevRunNumber != fEvent->GetRunNumber() && NeedField()) {
131 fEvent->InitMagneticField();
132 prevRunNumber = fEvent->GetRunNumber();
133 }
134 //
b2af62ab 135 // Event selection
136 //
aaf2d706 137 fIsSelectedResult = 0;
138 if (fEventCuts && !IsUserCallSelectionMask())
139 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
620ad672 140 //
141 // Friends
142 ((AliESDEvent*)fEvent)->SetESDfriend(fFriend);
aaf2d706 143 called = kTRUE;
7b2a359f 144
d38034b8 145 // set transient pointer to event inside tracks
146 fEvent->ConnectTracks();
147
cec9e119 148 if (fMixingHandler) fMixingHandler->BeginEvent(entry);
149 if (fUseTags && fRunTag) {
150 fEventTag = 0;
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()));
153 return kTRUE;
154 }
155 fEventTag = fRunTag->GetEventTag(entry);
156 }
d7749dec 157 return kTRUE;
d3dd3d14 158}
159
aaf2d706 160//______________________________________________________________________________
161void AliESDInputHandler::CheckSelectionMask()
162{
163// This method can be called by a task only if IsUserCallSelectionMask is true.
164 if (!fEventCuts || !IsUserCallSelectionMask()) return;
b2747f93 165 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
aaf2d706 166}
6989bff3 167
aaf2d706 168//______________________________________________________________________________
9d4be30f 169void AliESDInputHandler::ConnectFriends()
1aa09fd1 170{
9d4be30f 171// Connect the friends tree as soon as available.
cec9e119 172 //
173 // Handle the friends first
174 //
d9880d92 175 TTree* cTree = fTree->GetTree();
176 if (!cTree) cTree = fTree;
177 if (!cTree->FindBranch("ESDfriend.")) {
cec9e119 178 // Try to add ESDfriend. branch as friend
d9880d92 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);
186 dirname += "/";
187 esdFriendTreeFName = dirname + basename;
188
cec9e119 189 cTree->AddFriend("esdFriendTree", esdFriendTreeFName.Data());
190 cTree->SetBranchStatus("ESDfriend.", 1);
21e601a3 191 fFriend = fEvent->FindFriend();
cec9e119 192 if (fFriend) cTree->SetBranchAddress("ESDfriend.", &fFriend);
9d4be30f 193 }
194}
195
196//______________________________________________________________________________
197Bool_t AliESDInputHandler::FinishEvent()
198{
199 // Finish the event
200 if(fEvent)fEvent->Reset();
d9880d92 201 if (fFriend) fFriend->Reset();
9d4be30f 202 if (fMixingHandler) fMixingHandler->FinishEvent();
203 return kTRUE;
204}
205
206//______________________________________________________________________________
207Bool_t AliESDInputHandler::Notify(const char* path)
208{
209 // Notify a directory change
210 static Bool_t firsttime = kFALSE;
211 AliInfo(Form("Directory change %s \n", path));
212 //
213 // Handle the friends first
214 //
215 if (fReadFriends) ConnectFriends();
cec9e119 216 //
217 //
218 SwitchOffBranches();
219 SwitchOnBranches();
220 fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
4cf1e5f2 221 fUserInfo=fTree->GetTree()->GetUserInfo();
222
cec9e119 223 //
224 if (fUseHLT) {
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);
50aaaf00 230
cec9e119 231 if (fHLTTree) {
232 if (!fHLTEvent) fHLTEvent = new AliESDEvent();
233 fHLTEvent->ReadFromTree(fHLTTree);
50aaaf00 234 }
cec9e119 235 }
50aaaf00 236
cec9e119 237 if (!fUseTags) {
238 if (fMixingHandler) fMixingHandler->Notify(path);
239 return kTRUE;
240 }
1aa09fd1 241
cec9e119 242 Bool_t zip = kFALSE;
1aa09fd1 243
cec9e119 244 // Setup the base path
245 TString pathName(path);
246 Int_t index = pathName.Index("#");
247 if (index>=0) {
248 zip = kTRUE;
249 pathName = pathName(0,index);
250 } else {
251 pathName = gSystem->DirName(pathName);
252 }
253 if (fTree->GetCurrentFile()->GetArchive()) zip = kTRUE;
254 if (pathName.IsNull()) pathName = "./";
255 printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
6ccd727b 256
cec9e119 257 if (fRunTag) {
258 fRunTag->Clear();
259 } else {
260 fRunTag = new AliRunTag();
261 }
1aa09fd1 262
cec9e119 263 const char* tagPattern = "ESD.tag.root";
264 TString sname;
265 TString tagFilename;
266 if (zip) {
267 TObjArray* arr = fTree->GetCurrentFile()->GetArchive()->GetMembers();
268 TIter next(arr);
269 TObject *objarchive;
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
279 }//pattern check
280 } // archive file loop
281 } else {
282 void * dirp = gSystem->OpenDirectory(pathName.Data());
283 while(1) {
284 sname = gSystem->GetDirEntry(dirp);
285 if (sname.IsNull()) break;
286 if (sname.Contains(tagPattern)) {
287 tagFilename = pathName;
288 tagFilename += "/";
289 tagFilename += sname;
290 AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
291 break;
292 }//pattern check
293 }//directory loop
62e57953 294 gSystem->FreeDirectory(dirp);
cec9e119 295 }
296 if (tagFilename.IsNull()) {
297 if (firsttime) AliWarning(Form("Tag file not found in directory: %s", pathName.Data()));
298 firsttime = kFALSE;
299 delete fRunTag; fRunTag = 0;
1aa09fd1 300 return kTRUE;
cec9e119 301 }
302 TFile *tagfile = TFile::Open(tagFilename);
303 if (!tagfile) {
304 AliError(Form("Cannot open tag file: %s", tagFilename.Data()));
305 delete fRunTag; fRunTag = 0;
306 return kTRUE;
307 }
308 fTreeT = (TTree*)tagfile->Get("T"); // file is the owner
309 if (!fTreeT) {
310 AliError(Form("Cannot get tree of tags from file: %s", tagFilename.Data()));
311 delete fRunTag; fRunTag = 0;
312 return kTRUE;
313 }
314
315 fTreeT->SetBranchAddress("AliTAG",&fRunTag);
316 fTreeT->GetEntry(0);
317 delete tagfile;
318 // Notify the mixing handler after the tags are loaded
319 if (fMixingHandler) fMixingHandler->Notify(path);
320 return kTRUE;
1aa09fd1 321}
322
aaf2d706 323//______________________________________________________________________________
c2b6979d 324Option_t *AliESDInputHandler::GetDataType() const
325{
326// Returns handled data type.
327 return gESDDataType;
328}
f0f29480 329
aaf2d706 330//______________________________________________________________________________
f0f29480 331Int_t AliESDInputHandler::GetNEventAcceptedInFile()
332{
333 // Get number of events in file accepted by the tag cuts
334 // return -1 if no info is available
335 if (!fTagCutSumm) {
336 TList *luo = fTree->GetUserInfo();
337 if (!luo) {
338 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
339 return -1;
340 }
341 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
342 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
343 if (fTagCutSumm) break;
344 }
345 if (!fTagCutSumm) {
346 AliInfo(Form("No tag summary map in input tree\n"));
347 return -1;
348 }
349 }
350
351 TObjString *ostr = 0;
352 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
353 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
354 else {
355 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
356 return -1;
357 }
358 char *iTagInfo;
359 iTagInfo = strdup(ostr->GetString().Data());
360
361 Int_t iAcc = atoi(strtok(iTagInfo, ","));
362
363 AliInfo(Form("Got %i accepted events for file %s", iAcc, fTree->GetCurrentFile()->GetName()));
364
365 free(iTagInfo);
366
367 return iAcc;
368}
aaf2d706 369
370//______________________________________________________________________________
f0f29480 371Int_t AliESDInputHandler::GetNEventRejectedInFile()
372{
373 // Get number of events in file rejected by the tag cuts
374 // return -1 if no info is available
375 if (!fTagCutSumm) {
376 TList *luo = fTree->GetUserInfo();
377 if (!luo) {
378 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
379 return -1;
380 }
381 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
382 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
383 if (fTagCutSumm) break;
384 }
385 if (!fTagCutSumm) {
386 AliInfo(Form("No tag summary map in input tree\n"));
387 return -1;
388 }
389 }
390
391 TObjString *ostr = 0;
392 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
393 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
394 else {
395 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
396 return -1;
397 }
398 char *iTagInfo;
399 iTagInfo = strdup(ostr->GetString().Data());
400
401 strtok(iTagInfo, ",");
402 Int_t iRej = atoi(strtok(NULL, ","));
403
404 AliInfo(Form("Got %i accepted events for file %s", iRej, fTree->GetCurrentFile()->GetName()));
405
406 free(iTagInfo);
407
408 return iRej;
409}
aaf2d706 410
411//______________________________________________________________________________
5e6a3170 412Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal, Int_t *aAccepted, Int_t *aRejected)
c334d0d3 413{
414 // Get number of events in the full chain
415 // Count accepted and rejected events
416 // return kFALSE if no info is available
417 if (!fTagCutSumm) {
418 TList *luo = fTree->GetUserInfo();
419 if (!luo) {
420 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
421 return kFALSE;
422 }
423 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
424 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
425 if (fTagCutSumm) break;
426 }
427 if (!fTagCutSumm) {
428 AliInfo(Form("No tag summary map in input tree\n"));
429 return kFALSE;
430 }
431 }
432
433 TMapIter *tIter = new TMapIter(fTagCutSumm);
434
435 Int_t iTotList=0, iAccList=0, iRejList=0;
436
437 TObject *cobj;
9a587f77 438 while ((cobj = tIter->Next())) {
c334d0d3 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());
442 char *iTagInfo;
443 iTagInfo = strdup(vstr->GetString().Data());
444
445 Int_t iAcc = atoi(strtok(iTagInfo, ","));
446 Int_t iRej = atoi(strtok(NULL, ","));
96f4e3cb 447 free(iTagInfo);
c334d0d3 448
449 iAccList += iAcc;
450 iRejList += iRej;
451 iTotList += (iAcc+iRej);
452 }
453
454 *aTotal = iTotList;
455 *aAccepted = iAccList;
456 *aRejected = iRejList;
457
458 return kTRUE;
459}
460
aaf2d706 461//______________________________________________________________________________
c334d0d3 462Int_t AliESDInputHandler::GetNFilesEmpty()
463{
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
467 if (!fTagCutSumm) {
468 TList *luo = fTree->GetUserInfo();
469 if (!luo) {
470 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
471 return -1;
472 }
473 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
474 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
475 if (fTagCutSumm) break;
476 }
477 if (!fTagCutSumm) {
478 AliInfo(Form("No tag summary map in input tree\n"));
479 return -1;
480 }
481 }
482
483 TMapIter *tIter = new TMapIter(fTagCutSumm);
484
485 Int_t iFilesEmpty = 0;
486
487 TObject *cobj;
9a587f77 488 while ((cobj = tIter->Next())) {
c334d0d3 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());
492 char *iTagInfo;
493 iTagInfo = strdup(vstr->GetString().Data());
494
495 Int_t iAcc = atoi(strtok(iTagInfo, ","));
496 Int_t iRej = atoi(strtok(NULL, ","));
2e8cd32b 497 free(iTagInfo);
c334d0d3 498 if ((iAcc == 0) && ((iRej+iAcc)>0))
499 iFilesEmpty++;
500 }
501
502 return iFilesEmpty;
503
504}
70908d05 505
506//______________________________________________________________________________
507TObject *AliESDInputHandler::GetStatistics(Option_t *option) const
508{
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;
513 TString opt(option);
514 opt.ToUpper();
515 if (opt=="BIN0") return fEventCuts->GetStatistics("BIN0");
516 else return fEventCuts->GetStatistics("ALL");
517}
9006fe9c 518
519//______________________________________________________________________________
520void AliESDInputHandler::CreatePIDResponse(Bool_t isMC/*=kFALSE*/)
521{
522 //
523 // create the pid response object if it does not exist yet
524 //
525 if (fESDpid) return;
526 fESDpid=new AliESDpid(isMC);
527
528}
529
4cf1e5f2 530
531
532