Line data Source code
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 :
23 : #include <TTree.h>
24 : #include <TChain.h>
25 : #include <TFile.h>
26 : #include <TArchiveFile.h>
27 : #include <TObjArray.h>
28 : #include <TSystem.h>
29 : #include <TString.h>
30 : #include <TObjString.h>
31 : #include <TProcessID.h>
32 : #include <TMap.h>
33 :
34 : #include "AliESDInputHandler.h"
35 : #include "AliESDEvent.h"
36 : #include "AliESDfriend.h"
37 : #include "AliVCuts.h"
38 : #include "AliESD.h"
39 : #include "AliRunTag.h"
40 : #include "AliEventTag.h"
41 : #include "AliLog.h"
42 : #include "AliESDpid.h"
43 :
44 172 : ClassImp(AliESDInputHandler)
45 :
46 : static Option_t *gESDDataType = "ESD";
47 :
48 : //______________________________________________________________________________
49 : AliESDInputHandler::AliESDInputHandler() :
50 2 : AliInputEventHandler(),
51 2 : fEvent(0x0),
52 2 : fFriend(0x0),
53 2 : fESDpid(0x0),
54 2 : fAnalysisType(0),
55 2 : fNEvents(0),
56 2 : fHLTEvent(0x0),
57 2 : fHLTTree(0x0),
58 2 : fUseHLT(kFALSE),
59 2 : fTagCutSumm(0x0),
60 2 : fUseTags(kFALSE),
61 2 : fChainT(0),
62 2 : fTreeT(0),
63 2 : fRunTag(0),
64 2 : fEventTag(0),
65 2 : fReadFriends(0),
66 2 : fFriendFileName("AliESDfriends.root")
67 10 : {
68 : // default constructor
69 4 : }
70 :
71 : //______________________________________________________________________________
72 : AliESDInputHandler::~AliESDInputHandler()
73 0 : {
74 : // destructor
75 0 : if (fRunTag) delete fRunTag;
76 0 : delete fESDpid;
77 0 : }
78 :
79 : //______________________________________________________________________________
80 : AliESDInputHandler::AliESDInputHandler(const char* name, const char* title):
81 0 : AliInputEventHandler(name, title), fEvent(0x0), fFriend(0x0), fESDpid(0x0), fAnalysisType(0),
82 0 : 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")
83 0 : {
84 : // Constructor
85 0 : }
86 :
87 : //______________________________________________________________________________
88 : Bool_t AliESDInputHandler::Init(TTree* tree, Option_t* opt)
89 : {
90 : //
91 : // Initialisation necessary for each new tree
92 : //
93 4 : fAnalysisType = opt;
94 2 : fTree = tree;
95 :
96 2 : if (!fTree) return kFALSE;
97 : // fTree->GetEntry(0);
98 :
99 :
100 6 : if (!fEvent) fEvent = new AliESDEvent();
101 2 : fEvent->ReadFromTree(fTree);
102 2 : fNEvents = fTree->GetEntries();
103 2 : if (fReadFriends) ConnectFriends();
104 :
105 2 : if (fMixingHandler) fMixingHandler->Init(tree, opt);
106 2 : return kTRUE;
107 2 : }
108 :
109 : //______________________________________________________________________________
110 : Bool_t AliESDInputHandler::BeginEvent(Long64_t entry)
111 : {
112 :
113 : // Copy from old to new format if necessary
114 : static Bool_t called = kFALSE;
115 18 : if (!called && fEventCuts && IsUserCallSelectionMask())
116 0 : AliInfo(Form("The ESD input handler expects that the first task calls AliESDInputHandler::CheckSelectionMask() %s", fEventCuts->ClassName()));
117 8 : AliESD* old = ((AliESDEvent*) fEvent)->GetAliESDOld();
118 8 : if (old) {
119 0 : ((AliESDEvent*)fEvent)->CopyFromOldESD();
120 0 : old->Reset();
121 0 : }
122 :
123 8 : if (fHLTTree) {
124 0 : fHLTTree->GetEntry(entry);
125 0 : }
126 :
127 8 : fNewEvent = kTRUE;
128 : //
129 : static Int_t prevRunNumber = -1;
130 16 : if (prevRunNumber != fEvent->GetRunNumber() && NeedField()) {
131 0 : fEvent->InitMagneticField();
132 0 : prevRunNumber = fEvent->GetRunNumber();
133 0 : }
134 : //
135 : // Event selection
136 : //
137 8 : fIsSelectedResult = 0;
138 8 : if (fEventCuts && !IsUserCallSelectionMask())
139 0 : fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
140 : //
141 : // Friends
142 8 : ((AliESDEvent*)fEvent)->SetESDfriend(fFriend);
143 8 : called = kTRUE;
144 :
145 : // set transient pointer to event inside tracks
146 8 : fEvent->ConnectTracks();
147 :
148 8 : if (fMixingHandler) fMixingHandler->BeginEvent(entry);
149 16 : if (fUseTags && fRunTag) {
150 8 : fEventTag = 0;
151 8 : if (entry >= fRunTag->GetNEvents()) {
152 0 : AliError(Form("Current event %d does not match max range from run tag: 0-%d", (Int_t)entry, fRunTag->GetNEvents()));
153 0 : return kTRUE;
154 : }
155 8 : fEventTag = fRunTag->GetEventTag(entry);
156 8 : }
157 8 : return kTRUE;
158 8 : }
159 :
160 : //______________________________________________________________________________
161 : void AliESDInputHandler::CheckSelectionMask()
162 : {
163 : // This method can be called by a task only if IsUserCallSelectionMask is true.
164 0 : if (!fEventCuts || !IsUserCallSelectionMask()) return;
165 0 : fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
166 0 : }
167 :
168 : //______________________________________________________________________________
169 : void AliESDInputHandler::ConnectFriends()
170 : {
171 : // Connect the friends tree as soon as available.
172 : //
173 : // Handle the friends first
174 : //
175 0 : TTree* cTree = fTree->GetTree();
176 0 : if (!cTree) cTree = fTree;
177 0 : if (!cTree->FindBranch("ESDfriend.")) {
178 : // Try to add ESDfriend. branch as friend
179 0 : TString esdFriendTreeFName;
180 0 : esdFriendTreeFName = (fTree->GetCurrentFile())->GetName();
181 0 : TString basename = gSystem->BaseName(esdFriendTreeFName);
182 0 : Int_t index = basename.Index("#")+1;
183 0 : basename.Remove(index);
184 0 : basename += fFriendFileName;
185 0 : TString dirname = gSystem->DirName(esdFriendTreeFName);
186 0 : dirname += "/";
187 0 : esdFriendTreeFName = dirname + basename;
188 :
189 0 : cTree->AddFriend("esdFriendTree", esdFriendTreeFName.Data());
190 0 : cTree->SetBranchStatus("ESDfriend.", 1);
191 0 : fFriend = fEvent->FindFriend();
192 0 : if (fFriend) cTree->SetBranchAddress("ESDfriend.", &fFriend);
193 0 : }
194 0 : }
195 :
196 : //______________________________________________________________________________
197 : Bool_t AliESDInputHandler::FinishEvent()
198 : {
199 : // Finish the event
200 24 : if(fEvent)fEvent->Reset();
201 16 : if (fFriend) fFriend->Reset();
202 8 : if (fMixingHandler) fMixingHandler->FinishEvent();
203 8 : return kTRUE;
204 : }
205 :
206 : //______________________________________________________________________________
207 : Bool_t AliESDInputHandler::Notify(const char* path)
208 : {
209 : // Notify a directory change
210 : static Bool_t firsttime = kFALSE;
211 4 : AliInfo(Form("Directory change %s \n", path));
212 : //
213 : // Handle the friends first
214 : //
215 2 : if (fReadFriends) ConnectFriends();
216 : //
217 : //
218 2 : SwitchOffBranches();
219 2 : SwitchOnBranches();
220 2 : fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
221 2 : fUserInfo=fTree->GetTree()->GetUserInfo();
222 :
223 : //
224 2 : if (fUseHLT) {
225 : // Get HLTesdTree from current file
226 0 : TTree* cTree = fTree;
227 0 : if (fTree->GetTree()) cTree = fTree->GetTree();
228 0 : TFile* cFile = cTree->GetCurrentFile();
229 0 : cFile->GetObject("HLTesdTree", fHLTTree);
230 :
231 0 : if (fHLTTree) {
232 0 : if (!fHLTEvent) fHLTEvent = new AliESDEvent();
233 0 : fHLTEvent->ReadFromTree(fHLTTree);
234 0 : }
235 0 : }
236 :
237 2 : if (!fUseTags) {
238 0 : if (fMixingHandler) fMixingHandler->Notify(path);
239 0 : return kTRUE;
240 : }
241 :
242 : Bool_t zip = kFALSE;
243 :
244 : // Setup the base path
245 2 : TString pathName(path);
246 2 : Int_t index = pathName.Index("#");
247 2 : if (index>=0) {
248 : zip = kTRUE;
249 0 : pathName = pathName(0,index);
250 : } else {
251 6 : pathName = gSystem->DirName(pathName);
252 : }
253 4 : if (fTree->GetCurrentFile()->GetArchive()) zip = kTRUE;
254 4 : if (pathName.IsNull()) pathName = "./";
255 4 : printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
256 :
257 2 : if (fRunTag) {
258 0 : fRunTag->Clear();
259 : } else {
260 6 : fRunTag = new AliRunTag();
261 : }
262 :
263 : const char* tagPattern = "ESD.tag.root";
264 2 : TString sname;
265 2 : TString tagFilename;
266 2 : if (zip) {
267 0 : TObjArray* arr = fTree->GetCurrentFile()->GetArchive()->GetMembers();
268 0 : TIter next(arr);
269 : TObject *objarchive;
270 0 : while ((objarchive = next())) {
271 0 : sname = objarchive->GetName();
272 0 : if (sname.Contains(tagPattern)) {
273 0 : tagFilename = pathName;
274 0 : if (index>=0) tagFilename += "#";
275 0 : else tagFilename += "/";
276 0 : tagFilename += sname;
277 0 : 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 0 : } else {
282 4 : void * dirp = gSystem->OpenDirectory(pathName.Data());
283 2 : while(1) {
284 250 : sname = gSystem->GetDirEntry(dirp);
285 250 : if (sname.IsNull()) break;
286 250 : if (sname.Contains(tagPattern)) {
287 2 : tagFilename = pathName;
288 2 : tagFilename += "/";
289 2 : tagFilename += sname;
290 8 : AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
291 : break;
292 : }//pattern check
293 : }//directory loop
294 2 : gSystem->FreeDirectory(dirp);
295 : }
296 4 : if (tagFilename.IsNull()) {
297 0 : if (firsttime) AliWarning(Form("Tag file not found in directory: %s", pathName.Data()));
298 0 : firsttime = kFALSE;
299 0 : delete fRunTag; fRunTag = 0;
300 0 : return kTRUE;
301 : }
302 4 : TFile *tagfile = TFile::Open(tagFilename);
303 2 : if (!tagfile) {
304 0 : AliError(Form("Cannot open tag file: %s", tagFilename.Data()));
305 0 : delete fRunTag; fRunTag = 0;
306 0 : return kTRUE;
307 : }
308 4 : fTreeT = (TTree*)tagfile->Get("T"); // file is the owner
309 2 : if (!fTreeT) {
310 0 : AliError(Form("Cannot get tree of tags from file: %s", tagFilename.Data()));
311 0 : delete fRunTag; fRunTag = 0;
312 0 : return kTRUE;
313 : }
314 :
315 2 : fTreeT->SetBranchAddress("AliTAG",&fRunTag);
316 2 : fTreeT->GetEntry(0);
317 4 : delete tagfile;
318 : // Notify the mixing handler after the tags are loaded
319 2 : if (fMixingHandler) fMixingHandler->Notify(path);
320 2 : return kTRUE;
321 4 : }
322 :
323 : //______________________________________________________________________________
324 : Option_t *AliESDInputHandler::GetDataType() const
325 : {
326 : // Returns handled data type.
327 0 : return gESDDataType;
328 : }
329 :
330 : //______________________________________________________________________________
331 : Int_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 0 : if (!fTagCutSumm) {
336 0 : TList *luo = fTree->GetUserInfo();
337 0 : if (!luo) {
338 0 : AliInfo(Form("No user info in input tree - no tag cut summary\n"));
339 0 : return -1;
340 : }
341 0 : for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
342 0 : fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
343 0 : if (fTagCutSumm) break;
344 : }
345 0 : if (!fTagCutSumm) {
346 0 : AliInfo(Form("No tag summary map in input tree\n"));
347 0 : return -1;
348 : }
349 0 : }
350 :
351 : TObjString *ostr = 0;
352 0 : if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
353 0 : ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
354 : else {
355 0 : AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
356 0 : return -1;
357 : }
358 : char *iTagInfo;
359 0 : iTagInfo = strdup(ostr->GetString().Data());
360 :
361 0 : Int_t iAcc = atoi(strtok(iTagInfo, ","));
362 :
363 0 : AliInfo(Form("Got %i accepted events for file %s", iAcc, fTree->GetCurrentFile()->GetName()));
364 :
365 0 : free(iTagInfo);
366 :
367 : return iAcc;
368 0 : }
369 :
370 : //______________________________________________________________________________
371 : Int_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 0 : if (!fTagCutSumm) {
376 0 : TList *luo = fTree->GetUserInfo();
377 0 : if (!luo) {
378 0 : AliInfo(Form("No user info in input tree - no tag cut summary\n"));
379 0 : return -1;
380 : }
381 0 : for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
382 0 : fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
383 0 : if (fTagCutSumm) break;
384 : }
385 0 : if (!fTagCutSumm) {
386 0 : AliInfo(Form("No tag summary map in input tree\n"));
387 0 : return -1;
388 : }
389 0 : }
390 :
391 : TObjString *ostr = 0;
392 0 : if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
393 0 : ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
394 : else {
395 0 : AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
396 0 : return -1;
397 : }
398 : char *iTagInfo;
399 0 : iTagInfo = strdup(ostr->GetString().Data());
400 :
401 0 : strtok(iTagInfo, ",");
402 0 : Int_t iRej = atoi(strtok(NULL, ","));
403 :
404 0 : AliInfo(Form("Got %i accepted events for file %s", iRej, fTree->GetCurrentFile()->GetName()));
405 :
406 0 : free(iTagInfo);
407 :
408 : return iRej;
409 0 : }
410 :
411 : //______________________________________________________________________________
412 : Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal, Int_t *aAccepted, Int_t *aRejected)
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 0 : if (!fTagCutSumm) {
418 0 : TList *luo = fTree->GetUserInfo();
419 0 : if (!luo) {
420 0 : AliInfo(Form("No user info in input tree - no tag cut summary\n"));
421 0 : return kFALSE;
422 : }
423 0 : for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
424 0 : fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
425 0 : if (fTagCutSumm) break;
426 : }
427 0 : if (!fTagCutSumm) {
428 0 : AliInfo(Form("No tag summary map in input tree\n"));
429 0 : return kFALSE;
430 : }
431 0 : }
432 :
433 0 : TMapIter *tIter = new TMapIter(fTagCutSumm);
434 :
435 : Int_t iTotList=0, iAccList=0, iRejList=0;
436 :
437 : TObject *cobj;
438 0 : while ((cobj = tIter->Next())) {
439 0 : TObjString *kstr = (TObjString *) cobj;
440 0 : 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 0 : iTagInfo = strdup(vstr->GetString().Data());
444 :
445 0 : Int_t iAcc = atoi(strtok(iTagInfo, ","));
446 0 : Int_t iRej = atoi(strtok(NULL, ","));
447 0 : free(iTagInfo);
448 :
449 0 : iAccList += iAcc;
450 0 : iRejList += iRej;
451 0 : iTotList += (iAcc+iRej);
452 : }
453 :
454 0 : *aTotal = iTotList;
455 0 : *aAccepted = iAccList;
456 0 : *aRejected = iRejList;
457 :
458 : return kTRUE;
459 0 : }
460 :
461 : //______________________________________________________________________________
462 : Int_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 0 : if (!fTagCutSumm) {
468 0 : TList *luo = fTree->GetUserInfo();
469 0 : if (!luo) {
470 0 : AliInfo(Form("No user info in input tree - no tag cut summary\n"));
471 0 : return -1;
472 : }
473 0 : for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
474 0 : fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
475 0 : if (fTagCutSumm) break;
476 : }
477 0 : if (!fTagCutSumm) {
478 0 : AliInfo(Form("No tag summary map in input tree\n"));
479 0 : return -1;
480 : }
481 0 : }
482 :
483 0 : TMapIter *tIter = new TMapIter(fTagCutSumm);
484 :
485 : Int_t iFilesEmpty = 0;
486 :
487 : TObject *cobj;
488 0 : while ((cobj = tIter->Next())) {
489 0 : TObjString *kstr = (TObjString *) cobj;
490 0 : 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 0 : iTagInfo = strdup(vstr->GetString().Data());
494 :
495 0 : Int_t iAcc = atoi(strtok(iTagInfo, ","));
496 0 : Int_t iRej = atoi(strtok(NULL, ","));
497 0 : free(iTagInfo);
498 0 : if ((iAcc == 0) && ((iRej+iAcc)>0))
499 0 : iFilesEmpty++;
500 : }
501 :
502 : return iFilesEmpty;
503 :
504 0 : }
505 :
506 : //______________________________________________________________________________
507 : TObject *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 0 : if (!fEventCuts) return NULL;
513 0 : TString opt(option);
514 0 : opt.ToUpper();
515 0 : if (opt=="BIN0") return fEventCuts->GetStatistics("BIN0");
516 0 : else return fEventCuts->GetStatistics("ALL");
517 0 : }
518 :
519 : //______________________________________________________________________________
520 : void AliESDInputHandler::CreatePIDResponse(Bool_t isMC/*=kFALSE*/)
521 : {
522 : //
523 : // create the pid response object if it does not exist yet
524 : //
525 0 : if (fESDpid) return;
526 0 : fESDpid=new AliESDpid(isMC);
527 :
528 0 : }
529 :
530 :
531 :
532 :
|