Macros dedicated to SDD QA, OCDB object plotting, noise, gain and drift speed computa...
[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),
038879cc 63 fReadFriends(0),
b626cd71 64 fFriendFileName("AliESDfriends.root")
d3dd3d14 65{
66 // default constructor
67}
68
69//______________________________________________________________________________
70AliESDInputHandler::~AliESDInputHandler()
71{
1aa09fd1 72 // destructor
d3dd3d14 73}
74
75//______________________________________________________________________________
76AliESDInputHandler::AliESDInputHandler(const char* name, const char* title):
67675e42 77 AliInputEventHandler(name, title), fEvent(0x0), fFriend(0x0), fESDpid(0x0), fAnalysisType(0),
038879cc 78 fNEvents(0), fHLTEvent(0x0), fHLTTree(0x0), fUseHLT(kFALSE), fTagCutSumm(0x0), fUseTags(kFALSE), fChainT(0), fTreeT(0), fRunTag(0), fReadFriends(0), fFriendFileName("AliESDfriends.root")
d3dd3d14 79{
e12e402c 80 // Constructor
d3dd3d14 81}
82
aaf2d706 83//______________________________________________________________________________
1aa09fd1 84Bool_t AliESDInputHandler::Init(TTree* tree, Option_t* opt)
d3dd3d14 85{
80ce1b4e 86 //
300d5701 87 // Initialisation necessary for each new tree
80ce1b4e 88 //
1aa09fd1 89 fAnalysisType = opt;
7b2a359f 90 fTree = tree;
300d5701 91
6073f8c9 92 if (!fTree) return kFALSE;
6ccd727b 93 fTree->GetEntry(0);
94
b481861d 95
efea3f54 96 if (!fEvent) fEvent = new AliESDEvent();
60996693 97 fEvent->ReadFromTree(fTree);
4d2a7370 98 fNEvents = fTree->GetEntries();
b890a10d 99
7b2a359f 100 if (fMixingHandler) fMixingHandler->Init(tree, opt);
b890a10d 101
d3dd3d14 102 return kTRUE;
103}
104
aaf2d706 105//______________________________________________________________________________
6ccd727b 106Bool_t AliESDInputHandler::BeginEvent(Long64_t entry)
d3dd3d14 107{
9b98c4df 108
d3dd3d14 109 // Copy from old to new format if necessary
aaf2d706 110 static Bool_t called = kFALSE;
111 if (!called && fEventCuts && IsUserCallSelectionMask())
112 AliInfo(Form("The ESD input handler expects that the first task calls AliESDInputHandler::CheckSelectionMask() %s", fEventCuts->ClassName()));
d7749dec 113 AliESD* old = ((AliESDEvent*) fEvent)->GetAliESDOld();
114 if (old) {
d3dd3d14 115 ((AliESDEvent*)fEvent)->CopyFromOldESD();
116 old->Reset();
d7749dec 117 }
6ccd727b 118
119 if (fHLTTree) {
120 fHLTTree->GetEntry(entry);
121 }
122
f0ee78b0 123 fNewEvent = kTRUE;
b2af62ab 124 //
125 // Event selection
126 //
aaf2d706 127 fIsSelectedResult = 0;
128 if (fEventCuts && !IsUserCallSelectionMask())
129 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
620ad672 130 //
131 // Friends
132 ((AliESDEvent*)fEvent)->SetESDfriend(fFriend);
aaf2d706 133 called = kTRUE;
7b2a359f 134
135 if (fMixingHandler) fMixingHandler->BeginEvent(entry);
b890a10d 136
d7749dec 137 return kTRUE;
d3dd3d14 138}
139
aaf2d706 140//______________________________________________________________________________
141void AliESDInputHandler::CheckSelectionMask()
142{
143// This method can be called by a task only if IsUserCallSelectionMask is true.
144 if (!fEventCuts || !IsUserCallSelectionMask()) return;
b2747f93 145 fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
aaf2d706 146}
147
148//______________________________________________________________________________
e12e402c 149Bool_t AliESDInputHandler::FinishEvent()
150{
151 // Finish the event
b890a10d 152 if(fEvent)fEvent->Reset();
7b2a359f 153 if (fMixingHandler) fMixingHandler->FinishEvent();
b890a10d 154 return kTRUE;
6989bff3 155}
156
aaf2d706 157//______________________________________________________________________________
1aa09fd1 158Bool_t AliESDInputHandler::Notify(const char* path)
159{
160 // Notify a directory change
a5112f7d 161 AliInfo(Form("Directory change %s \n", path));
162 //
80ce1b4e 163 // Handle the friends first
164 //
165 if (!fTree->FindBranch("ESDfriend.") && fReadFriends) {
166 // Try to add ESDfriend. branch as friend
167 TString esdTreeFName, esdFriendTreeFName;
168 esdTreeFName = (fTree->GetCurrentFile())->GetName();
169 esdFriendTreeFName = esdTreeFName;
3749af87 170 esdFriendTreeFName.ReplaceAll("AliESDs.root", fFriendFileName.Data());
6b0dccbe 171 TTree* cTree = fTree->GetTree();
172 if (!cTree) cTree = fTree;
173 cTree->AddFriend("esdFriendTree", esdFriendTreeFName.Data());
174 cTree->SetBranchStatus("ESDfriend.", 1);
175 fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
176 if (fFriend) cTree->SetBranchAddress("ESDfriend.", &fFriend);
80ce1b4e 177 }
178 //
179 //
180 SwitchOffBranches();
181 SwitchOnBranches();
182 fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
80ce1b4e 183 //
50aaaf00 184 if (fUseHLT) {
185 // Get HLTesdTree from current file
186 TTree* cTree = fTree;
187 if (fTree->GetTree()) cTree = fTree->GetTree();
188 TFile* cFile = cTree->GetCurrentFile();
189 cFile->GetObject("HLTesdTree", fHLTTree);
190
191 if (fHLTTree) {
192 if (!fHLTEvent) fHLTEvent = new AliESDEvent();
193 fHLTEvent->ReadFromTree(fHLTTree);
194 }
195 }
196
a37ff5af 197 if (!fUseTags) {
198 if (fMixingHandler) fMixingHandler->Notify(path);
199 return (kTRUE);
200 }
1aa09fd1 201
202 Bool_t zip = kFALSE;
203
204 TString fileName(path);
205 if(fileName.Contains("#AliESDs.root")){
1aa09fd1 206 zip = kTRUE;
207 }
208 else if (fileName.Contains("AliESDs.root")){
209 fileName.ReplaceAll("AliESDs.root", "");
210 }
211 else if(fileName.Contains("#AliAOD.root")){
1aa09fd1 212 zip = kTRUE;
213 }
214 else if(fileName.Contains("AliAOD.root")){
215 fileName.ReplaceAll("AliAOD.root", "");
216 }
217 else if(fileName.Contains("#galice.root")){
218 // For running with galice and kinematics alone...
1aa09fd1 219 zip = kTRUE;
220 }
221 else if(fileName.Contains("galice.root")){
222 // For running with galice and kinematics alone...
223 fileName.ReplaceAll("galice.root", "");
224 }
225
1aa09fd1 226
be4763e2 227 TString pathName("./");
228 if (fileName.Length() != 0) {
229 pathName = fileName;
230 }
231
232 printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
6ccd727b 233
1aa09fd1 234 if (fRunTag) {
235 fRunTag->Clear();
236 } else {
237 fRunTag = new AliRunTag();
238 }
239
240 delete fTreeT; fTreeT = 0;
6ccd727b 241
1aa09fd1 242 if (fChainT) {
243 delete fChainT;
244 fChainT = 0;
245 }
246
247 if (!fChainT) {
248 fChainT = new TChain("T");
249 }
250
251
252
253 const char* tagPattern = "ESD.tag.root";
254 const char* name = 0x0;
255 TString tagFilename;
256 if (zip) {
8ac5c1d7 257 TFile* file = fTree->GetCurrentFile();
1aa09fd1 258 TArchiveFile* arch = file->GetArchive();
259 TObjArray* arr = arch->GetMembers();
260 TIter next(arr);
261
7cec0871 262 while ((file = (TFile*) next())) {
1aa09fd1 263 name = file->GetName();
264 if (strstr(name,tagPattern)) {
be4763e2 265 tagFilename = pathName.Data();
1aa09fd1 266 tagFilename += "#";
267 tagFilename += name;
268 fChainT->Add(tagFilename);
269 AliInfo(Form("Adding %s to tag chain \n", tagFilename.Data()));
270 }//pattern check
271 } // archive file loop
272 } else {
be4763e2 273 void * dirp = gSystem->OpenDirectory(pathName.Data());
1aa09fd1 274 while((name = gSystem->GetDirEntry(dirp))) {
275 if (strstr(name,tagPattern)) {
be4763e2 276 tagFilename = pathName.Data();
1aa09fd1 277 tagFilename += "/";
278 tagFilename += name;
279 fChainT->Add(tagFilename);
280 AliInfo(Form("Adding %s to tag chain \n", tagFilename.Data()));
281 }//pattern check
282 }//directory loop
283 }
284 fChainT->SetBranchAddress("AliTAG",&fRunTag);
285 fChainT->GetEntry(0);
b890a10d 286
287 if (fMixingHandler) fMixingHandler->Notify(path);
288
1aa09fd1 289 return kTRUE;
290}
291
aaf2d706 292//______________________________________________________________________________
c2b6979d 293Option_t *AliESDInputHandler::GetDataType() const
294{
295// Returns handled data type.
296 return gESDDataType;
297}
f0f29480 298
aaf2d706 299//______________________________________________________________________________
f0f29480 300Int_t AliESDInputHandler::GetNEventAcceptedInFile()
301{
302 // Get number of events in file accepted by the tag cuts
303 // return -1 if no info is available
304 if (!fTagCutSumm) {
305 TList *luo = fTree->GetUserInfo();
306 if (!luo) {
307 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
308 return -1;
309 }
310 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
311 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
312 if (fTagCutSumm) break;
313 }
314 if (!fTagCutSumm) {
315 AliInfo(Form("No tag summary map in input tree\n"));
316 return -1;
317 }
318 }
319
320 TObjString *ostr = 0;
321 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
322 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
323 else {
324 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
325 return -1;
326 }
327 char *iTagInfo;
328 iTagInfo = strdup(ostr->GetString().Data());
329
330 Int_t iAcc = atoi(strtok(iTagInfo, ","));
331
332 AliInfo(Form("Got %i accepted events for file %s", iAcc, fTree->GetCurrentFile()->GetName()));
333
334 free(iTagInfo);
335
336 return iAcc;
337}
aaf2d706 338
339//______________________________________________________________________________
f0f29480 340Int_t AliESDInputHandler::GetNEventRejectedInFile()
341{
342 // Get number of events in file rejected by the tag cuts
343 // return -1 if no info is available
344 if (!fTagCutSumm) {
345 TList *luo = fTree->GetUserInfo();
346 if (!luo) {
347 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
348 return -1;
349 }
350 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
351 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
352 if (fTagCutSumm) break;
353 }
354 if (!fTagCutSumm) {
355 AliInfo(Form("No tag summary map in input tree\n"));
356 return -1;
357 }
358 }
359
360 TObjString *ostr = 0;
361 if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
362 ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
363 else {
364 AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
365 return -1;
366 }
367 char *iTagInfo;
368 iTagInfo = strdup(ostr->GetString().Data());
369
370 strtok(iTagInfo, ",");
371 Int_t iRej = atoi(strtok(NULL, ","));
372
373 AliInfo(Form("Got %i accepted events for file %s", iRej, fTree->GetCurrentFile()->GetName()));
374
375 free(iTagInfo);
376
377 return iRej;
378}
aaf2d706 379
380//______________________________________________________________________________
c334d0d3 381Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal, Int_t *aAccepted, Int_t *aRejected)
382{
383 // Get number of events in the full chain
384 // Count accepted and rejected events
385 // return kFALSE if no info is available
386 if (!fTagCutSumm) {
387 TList *luo = fTree->GetUserInfo();
388 if (!luo) {
389 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
390 return kFALSE;
391 }
392 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
393 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
394 if (fTagCutSumm) break;
395 }
396 if (!fTagCutSumm) {
397 AliInfo(Form("No tag summary map in input tree\n"));
398 return kFALSE;
399 }
400 }
401
402 TMapIter *tIter = new TMapIter(fTagCutSumm);
403
404 Int_t iTotList=0, iAccList=0, iRejList=0;
405
406 TObject *cobj;
9a587f77 407 while ((cobj = tIter->Next())) {
c334d0d3 408 TObjString *kstr = (TObjString *) cobj;
409 TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
410 // printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
411 char *iTagInfo;
412 iTagInfo = strdup(vstr->GetString().Data());
413
414 Int_t iAcc = atoi(strtok(iTagInfo, ","));
415 Int_t iRej = atoi(strtok(NULL, ","));
416
417 iAccList += iAcc;
418 iRejList += iRej;
419 iTotList += (iAcc+iRej);
420 }
421
422 *aTotal = iTotList;
423 *aAccepted = iAccList;
424 *aRejected = iRejList;
425
426 return kTRUE;
427}
428
aaf2d706 429//______________________________________________________________________________
c334d0d3 430Int_t AliESDInputHandler::GetNFilesEmpty()
431{
432 // Count number of files in which all events were de-selected
433 // For such files Notify() will NOT be called
434 // return -1 if no info is available
435 if (!fTagCutSumm) {
436 TList *luo = fTree->GetUserInfo();
437 if (!luo) {
438 AliInfo(Form("No user info in input tree - no tag cut summary\n"));
439 return -1;
440 }
441 for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
442 fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
443 if (fTagCutSumm) break;
444 }
445 if (!fTagCutSumm) {
446 AliInfo(Form("No tag summary map in input tree\n"));
447 return -1;
448 }
449 }
450
451 TMapIter *tIter = new TMapIter(fTagCutSumm);
452
453 Int_t iFilesEmpty = 0;
454
455 TObject *cobj;
9a587f77 456 while ((cobj = tIter->Next())) {
c334d0d3 457 TObjString *kstr = (TObjString *) cobj;
458 TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
459 // printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
460 char *iTagInfo;
461 iTagInfo = strdup(vstr->GetString().Data());
462
463 Int_t iAcc = atoi(strtok(iTagInfo, ","));
464 Int_t iRej = atoi(strtok(NULL, ","));
465
466 if ((iAcc == 0) && ((iRej+iAcc)>0))
467 iFilesEmpty++;
468 }
469
470 return iFilesEmpty;
471
472}