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