]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/ESD/AliESDInputHandler.cxx
Fix in the last caall to CleanOwnPrimaryVertex
[u/mrichter/AliRoot.git] / STEER / ESD / AliESDInputHandler.cxx
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 ClassImp(AliESDInputHandler)
45
46 static Option_t *gESDDataType = "ESD";
47
48 //______________________________________________________________________________
49 AliESDInputHandler::AliESDInputHandler() :
50   AliInputEventHandler(),
51   fEvent(0x0),
52   fFriend(0x0),
53   fESDpid(0x0),
54   fAnalysisType(0),
55   fNEvents(0),
56   fHLTEvent(0x0),
57   fHLTTree(0x0),
58   fUseHLT(kFALSE),
59   fTagCutSumm(0x0),
60   fUseTags(kFALSE),
61   fChainT(0),
62   fTreeT(0),
63   fRunTag(0),
64   fEventTag(0),
65   fReadFriends(0),
66   fFriendFileName("AliESDfriends.root")
67 {
68   // default constructor
69 }
70
71 //______________________________________________________________________________
72 AliESDInputHandler::~AliESDInputHandler() 
73 {
74   //  destructor
75   if (fRunTag) delete fRunTag;
76   delete fESDpid;
77 }
78
79 //______________________________________________________________________________
80 AliESDInputHandler::AliESDInputHandler(const char* name, const char* title):
81     AliInputEventHandler(name, title), fEvent(0x0), fFriend(0x0), fESDpid(0x0), fAnalysisType(0),
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")
83 {
84     // Constructor
85 }
86
87 //______________________________________________________________________________
88 Bool_t AliESDInputHandler::Init(TTree* tree,  Option_t* opt)
89 {
90     //
91     // Initialisation necessary for each new tree 
92     // 
93     fAnalysisType = opt;
94     fTree = tree;
95     
96     if (!fTree) return kFALSE;
97 //    fTree->GetEntry(0);
98     
99
100     if (!fEvent) fEvent = new AliESDEvent();
101     fEvent->ReadFromTree(fTree);
102     fNEvents = fTree->GetEntries();
103
104     if (fMixingHandler) fMixingHandler->Init(tree,  opt);
105
106     return kTRUE;
107 }
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   if (!called && fEventCuts && IsUserCallSelectionMask())
116      AliInfo(Form("The ESD input handler expects that the first task calls AliESDInputHandler::CheckSelectionMask() %s", fEventCuts->ClassName()));
117   AliESD* old = ((AliESDEvent*) fEvent)->GetAliESDOld();
118   if (old) {
119    ((AliESDEvent*)fEvent)->CopyFromOldESD();
120    old->Reset();
121   }
122
123   if (fHLTTree) {
124       fHLTTree->GetEntry(entry);
125   }
126   
127   fNewEvent = kTRUE;
128   //
129   // Event selection
130   // 
131   fIsSelectedResult = 0;
132   if (fEventCuts && !IsUserCallSelectionMask())
133       fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent); 
134   //
135   // Friends
136   ((AliESDEvent*)fEvent)->SetESDfriend(fFriend);
137   called = kTRUE;
138
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   }      
148   return kTRUE;
149 }
150
151 //______________________________________________________________________________
152 void AliESDInputHandler::CheckSelectionMask()
153 {
154 // This method can be called by a task only if IsUserCallSelectionMask is true.
155    if (!fEventCuts || !IsUserCallSelectionMask()) return;
156    fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
157 }
158    
159 //______________________________________________________________________________
160 Bool_t  AliESDInputHandler::FinishEvent()
161 {
162     // Finish the event 
163   if(fEvent)fEvent->Reset();
164   if (fMixingHandler) fMixingHandler->FinishEvent();
165   return kTRUE;
166
167
168 //______________________________________________________________________________
169 Bool_t AliESDInputHandler::Notify(const char* path)
170 {
171   // Notify a directory change
172   static Bool_t firsttime = kFALSE;
173   AliInfo(Form("Directory change %s \n", path));
174   //
175   // Handle the friends first
176   //
177   if (!fTree->FindBranch("ESDfriend.") && fReadFriends) {
178     // Try to add ESDfriend. branch as friend
179       TString esdFriendTreeFName;
180       esdFriendTreeFName = (fTree->GetCurrentFile())->GetName();
181     
182
183     if(esdFriendTreeFName.Contains("AliESDs.root")) {
184       esdFriendTreeFName.ReplaceAll("AliESDs.root", fFriendFileName.Data());
185     } else if(esdFriendTreeFName.Contains("AliESDs_wSDD.root")) {
186       esdFriendTreeFName.ReplaceAll("AliESDs_wSDD.root", fFriendFileName.Data());
187     }
188
189     TTree* cTree = fTree->GetTree();
190     if (!cTree) cTree = fTree;      
191     cTree->AddFriend("esdFriendTree", esdFriendTreeFName.Data());
192     cTree->SetBranchStatus("ESDfriend.", 1);
193     fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
194     if (fFriend) cTree->SetBranchAddress("ESDfriend.", &fFriend);
195   } 
196   //
197   //
198   SwitchOffBranches();
199   SwitchOnBranches();
200   fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
201   //
202   if (fUseHLT) {
203     // Get HLTesdTree from current file
204     TTree* cTree = fTree;
205     if (fTree->GetTree()) cTree = fTree->GetTree();
206     TFile* cFile = cTree->GetCurrentFile();
207     cFile->GetObject("HLTesdTree", fHLTTree);
208         
209     if (fHLTTree) {
210            if (!fHLTEvent) fHLTEvent = new AliESDEvent();
211            fHLTEvent->ReadFromTree(fHLTTree);
212     }
213   }
214
215   if (!fUseTags) {
216     if (fMixingHandler) fMixingHandler->Notify(path);
217     return kTRUE;
218   }
219     
220   Bool_t zip = kFALSE;
221     
222   // Setup the base path
223   TString pathName(path);
224   Int_t index = pathName.Index("#");
225   if (index>=0) {
226     zip = kTRUE;
227     pathName = pathName(0,index);
228   } else {
229     pathName = gSystem->DirName(pathName);
230   }
231   if (fTree->GetCurrentFile()->GetArchive()) zip = kTRUE;
232   if (pathName.IsNull()) pathName = "./";  
233   printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
234
235   if (fRunTag) {
236     fRunTag->Clear();
237   } else {
238     fRunTag = new AliRunTag();
239   }
240     
241   const char* tagPattern = "ESD.tag.root";
242   TString sname;
243   TString tagFilename;
244   if (zip) {
245     TObjArray* arr = fTree->GetCurrentFile()->GetArchive()->GetMembers();
246     TIter next(arr);
247     TObject *objarchive;
248     while ((objarchive = next())) {
249       sname = objarchive->GetName();
250            if (sname.Contains(tagPattern)) { 
251         tagFilename = pathName;
252         if (index>=0) tagFilename += "#";
253         else tagFilename += "/";
254         tagFilename += sname;
255         AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
256         break; // There should be only one such file in the archive
257       }//pattern check
258     } // archive file loop
259   } else {
260     void * dirp = gSystem->OpenDirectory(pathName.Data());
261     while(1) {
262       sname = gSystem->GetDirEntry(dirp);
263       if (sname.IsNull()) break;
264       if (sname.Contains(tagPattern)) { 
265         tagFilename = pathName;
266         tagFilename += "/";
267         tagFilename += sname;
268         AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
269         break;
270       }//pattern check
271     }//directory loop
272     gSystem->FreeDirectory(dirp);
273   }
274   if (tagFilename.IsNull()) {
275     if (firsttime) AliWarning(Form("Tag file not found in directory: %s", pathName.Data()));
276     firsttime = kFALSE;
277     delete fRunTag; fRunTag = 0;
278     return kTRUE;
279   }
280   TFile *tagfile = TFile::Open(tagFilename);
281   if (!tagfile) {
282     AliError(Form("Cannot open tag file: %s", tagFilename.Data()));
283     delete fRunTag; fRunTag = 0;
284     return kTRUE;
285   }   
286   fTreeT = (TTree*)tagfile->Get("T"); // file is the owner
287   if (!fTreeT) {
288     AliError(Form("Cannot get tree of tags from file: %s", tagFilename.Data()));
289     delete fRunTag; fRunTag = 0;
290     return kTRUE;
291   }
292     
293   fTreeT->SetBranchAddress("AliTAG",&fRunTag);
294   fTreeT->GetEntry(0);
295   delete tagfile;
296   // Notify the mixing handler after the tags are loaded
297   if (fMixingHandler) fMixingHandler->Notify(path);
298   return kTRUE;
299 }
300
301 //______________________________________________________________________________
302 Option_t *AliESDInputHandler::GetDataType() const
303 {
304 // Returns handled data type.
305    return gESDDataType;
306 }
307
308 //______________________________________________________________________________
309 Int_t AliESDInputHandler::GetNEventAcceptedInFile()
310 {
311   // Get number of events in file accepted by the tag cuts
312   // return -1 if no info is available
313   if (!fTagCutSumm) {
314     TList *luo = fTree->GetUserInfo();
315     if (!luo) {
316       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
317       return -1;
318     }
319     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
320       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
321       if (fTagCutSumm) break;
322     }
323     if (!fTagCutSumm) {
324       AliInfo(Form("No tag summary map in input tree\n"));
325       return -1;
326     }
327   }
328
329   TObjString *ostr = 0;
330   if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
331     ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
332   else {
333     AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
334     return -1;
335   }
336   char *iTagInfo;
337   iTagInfo = strdup(ostr->GetString().Data());
338
339   Int_t iAcc = atoi(strtok(iTagInfo, ","));
340   
341   AliInfo(Form("Got %i accepted events for file %s", iAcc,  fTree->GetCurrentFile()->GetName()));
342   
343   free(iTagInfo);
344
345   return iAcc;
346 }
347
348 //______________________________________________________________________________
349 Int_t AliESDInputHandler::GetNEventRejectedInFile()
350 {
351   // Get number of events in file rejected by the tag cuts
352   // return -1 if no info is available
353   if (!fTagCutSumm) {
354     TList *luo = fTree->GetUserInfo();
355     if (!luo) {
356       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
357       return -1;
358     }
359     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
360       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
361       if (fTagCutSumm) break;
362     }
363     if (!fTagCutSumm) {
364       AliInfo(Form("No tag summary map in input tree\n"));
365       return -1;
366     }
367   }
368
369   TObjString *ostr = 0;
370   if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
371     ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
372   else {
373     AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
374     return -1;
375   }
376   char *iTagInfo;
377   iTagInfo = strdup(ostr->GetString().Data());
378
379   strtok(iTagInfo, ",");
380   Int_t iRej = atoi(strtok(NULL, ","));
381   
382   AliInfo(Form("Got %i accepted events for file %s", iRej,  fTree->GetCurrentFile()->GetName()));
383   
384   free(iTagInfo);
385
386   return iRej;
387 }
388
389 //______________________________________________________________________________
390 Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal,  Int_t *aAccepted,  Int_t *aRejected)
391 {
392   // Get number of events in the full chain
393   // Count accepted and rejected events
394   // return kFALSE if no info is available
395   if (!fTagCutSumm) {
396     TList *luo = fTree->GetUserInfo();
397     if (!luo) {
398       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
399       return kFALSE;
400     }
401     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
402       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
403       if (fTagCutSumm) break;
404     }
405     if (!fTagCutSumm) {
406       AliInfo(Form("No tag summary map in input tree\n"));
407       return kFALSE;
408     }
409   }
410   
411   TMapIter *tIter = new TMapIter(fTagCutSumm);
412   
413   Int_t iTotList=0, iAccList=0, iRejList=0;
414
415   TObject *cobj;
416   while ((cobj = tIter->Next())) {
417     TObjString *kstr = (TObjString *) cobj;
418     TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
419     //    printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
420     char *iTagInfo;
421     iTagInfo = strdup(vstr->GetString().Data());
422     
423     Int_t iAcc = atoi(strtok(iTagInfo, ","));
424     Int_t iRej = atoi(strtok(NULL, ","));
425     free(iTagInfo);
426     
427     iAccList += iAcc;
428     iRejList += iRej;
429     iTotList += (iAcc+iRej);
430   }
431
432   *aTotal = iTotList;
433   *aAccepted = iAccList;
434   *aRejected = iRejList;
435
436   return kTRUE;
437 }
438
439 //______________________________________________________________________________
440 Int_t AliESDInputHandler::GetNFilesEmpty()
441 {
442   // Count number of files in which all events were de-selected
443   // For such files Notify() will NOT be called
444   // return -1 if no info is available
445   if (!fTagCutSumm) {
446     TList *luo = fTree->GetUserInfo();
447     if (!luo) {
448       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
449       return -1;
450     }
451     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
452       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
453       if (fTagCutSumm) break;
454     }
455     if (!fTagCutSumm) {
456       AliInfo(Form("No tag summary map in input tree\n"));
457       return -1;
458     }
459   }
460   
461   TMapIter *tIter = new TMapIter(fTagCutSumm);
462   
463   Int_t iFilesEmpty = 0;
464
465   TObject *cobj;
466   while ((cobj = tIter->Next())) {
467     TObjString *kstr = (TObjString *) cobj;
468     TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
469     //    printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
470     char *iTagInfo;
471     iTagInfo = strdup(vstr->GetString().Data());
472     
473     Int_t iAcc = atoi(strtok(iTagInfo, ","));
474     Int_t iRej = atoi(strtok(NULL, ","));
475     free(iTagInfo);
476     if ((iAcc == 0) && ((iRej+iAcc)>0))
477       iFilesEmpty++;
478   }
479
480   return iFilesEmpty;
481   
482 }
483
484 //______________________________________________________________________________
485 TObject *AliESDInputHandler::GetStatistics(Option_t *option) const
486 {
487 // Get the statistics histogram(s) from the physics selection object. This
488 // should be called during FinishTaskOutput(). Option can be empty (default
489 // statistics histogram) or BIN0.
490    if (!fEventCuts) return NULL;
491    TString opt(option);
492    opt.ToUpper();
493    if (opt=="BIN0") return fEventCuts->GetStatistics("BIN0");
494    else return fEventCuts->GetStatistics("ALL");
495 }   
496
497 //______________________________________________________________________________
498 void AliESDInputHandler::CreatePIDResponse(Bool_t isMC/*=kFALSE*/)
499 {
500   //
501   // create the pid response object if it does not exist yet
502   //
503   if (fESDpid) return;
504   fESDpid=new AliESDpid(isMC);
505
506 }
507