]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliESDInputHandler.cxx
Leak detected by Coverity fixed
[u/mrichter/AliRoot.git] / STEER / 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 esdTreeFName, esdFriendTreeFName;    
180     esdTreeFName = (fTree->GetCurrentFile())->GetName();
181     esdFriendTreeFName = esdTreeFName;
182     esdFriendTreeFName.ReplaceAll("AliESDs.root", fFriendFileName.Data());
183     TTree* cTree = fTree->GetTree();
184     if (!cTree) cTree = fTree;      
185     cTree->AddFriend("esdFriendTree", esdFriendTreeFName.Data());
186     cTree->SetBranchStatus("ESDfriend.", 1);
187     fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
188     if (fFriend) cTree->SetBranchAddress("ESDfriend.", &fFriend);
189   } 
190   //
191   //
192   SwitchOffBranches();
193   SwitchOnBranches();
194   fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
195   //
196   if (fUseHLT) {
197     // Get HLTesdTree from current file
198     TTree* cTree = fTree;
199     if (fTree->GetTree()) cTree = fTree->GetTree();
200     TFile* cFile = cTree->GetCurrentFile();
201     cFile->GetObject("HLTesdTree", fHLTTree);
202         
203     if (fHLTTree) {
204            if (!fHLTEvent) fHLTEvent = new AliESDEvent();
205            fHLTEvent->ReadFromTree(fHLTTree);
206     }
207   }
208
209   if (!fUseTags) {
210     if (fMixingHandler) fMixingHandler->Notify(path);
211     return kTRUE;
212   }
213     
214   Bool_t zip = kFALSE;
215     
216   // Setup the base path
217   TString pathName(path);
218   Int_t index = pathName.Index("#");
219   if (index>=0) {
220     zip = kTRUE;
221     pathName = pathName(0,index);
222   } else {
223     pathName = gSystem->DirName(pathName);
224   }
225   if (fTree->GetCurrentFile()->GetArchive()) zip = kTRUE;
226   if (pathName.IsNull()) pathName = "./";  
227   printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
228
229   if (fRunTag) {
230     fRunTag->Clear();
231   } else {
232     fRunTag = new AliRunTag();
233   }
234     
235   const char* tagPattern = "ESD.tag.root";
236   TString sname;
237   TString tagFilename;
238   if (zip) {
239     TObjArray* arr = fTree->GetCurrentFile()->GetArchive()->GetMembers();
240     TIter next(arr);
241     TObject *objarchive;
242     while ((objarchive = next())) {
243       sname = objarchive->GetName();
244            if (sname.Contains(tagPattern)) { 
245         tagFilename = pathName;
246         if (index>=0) tagFilename += "#";
247         else tagFilename += "/";
248         tagFilename += sname;
249         AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
250         break; // There should be only one such file in the archive
251       }//pattern check
252     } // archive file loop
253   } else {
254     void * dirp = gSystem->OpenDirectory(pathName.Data());
255     while(1) {
256       sname = gSystem->GetDirEntry(dirp);
257       if (sname.IsNull()) break;
258       if (sname.Contains(tagPattern)) { 
259         tagFilename = pathName;
260         tagFilename += "/";
261         tagFilename += sname;
262         AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
263         break;
264       }//pattern check
265     }//directory loop
266     gSystem->FreeDirectory(dirp);
267   }
268   if (tagFilename.IsNull()) {
269     if (firsttime) AliWarning(Form("Tag file not found in directory: %s", pathName.Data()));
270     firsttime = kFALSE;
271     delete fRunTag; fRunTag = 0;
272     return kTRUE;
273   }
274   TFile *tagfile = TFile::Open(tagFilename);
275   if (!tagfile) {
276     AliError(Form("Cannot open tag file: %s", tagFilename.Data()));
277     delete fRunTag; fRunTag = 0;
278     return kTRUE;
279   }   
280   fTreeT = (TTree*)tagfile->Get("T"); // file is the owner
281   if (!fTreeT) {
282     AliError(Form("Cannot get tree of tags from file: %s", tagFilename.Data()));
283     delete fRunTag; fRunTag = 0;
284     return kTRUE;
285   }
286     
287   fTreeT->SetBranchAddress("AliTAG",&fRunTag);
288   fTreeT->GetEntry(0);
289   delete tagfile;
290   // Notify the mixing handler after the tags are loaded
291   if (fMixingHandler) fMixingHandler->Notify(path);
292   return kTRUE;
293 }
294
295 //______________________________________________________________________________
296 Option_t *AliESDInputHandler::GetDataType() const
297 {
298 // Returns handled data type.
299    return gESDDataType;
300 }
301
302 //______________________________________________________________________________
303 Int_t AliESDInputHandler::GetNEventAcceptedInFile()
304 {
305   // Get number of events in file accepted by the tag cuts
306   // return -1 if no info is available
307   if (!fTagCutSumm) {
308     TList *luo = fTree->GetUserInfo();
309     if (!luo) {
310       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
311       return -1;
312     }
313     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
314       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
315       if (fTagCutSumm) break;
316     }
317     if (!fTagCutSumm) {
318       AliInfo(Form("No tag summary map in input tree\n"));
319       return -1;
320     }
321   }
322
323   TObjString *ostr = 0;
324   if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
325     ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
326   else {
327     AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
328     return -1;
329   }
330   char *iTagInfo;
331   iTagInfo = strdup(ostr->GetString().Data());
332
333   Int_t iAcc = atoi(strtok(iTagInfo, ","));
334   
335   AliInfo(Form("Got %i accepted events for file %s", iAcc,  fTree->GetCurrentFile()->GetName()));
336   
337   free(iTagInfo);
338
339   return iAcc;
340 }
341
342 //______________________________________________________________________________
343 Int_t AliESDInputHandler::GetNEventRejectedInFile()
344 {
345   // Get number of events in file rejected by the tag cuts
346   // return -1 if no info is available
347   if (!fTagCutSumm) {
348     TList *luo = fTree->GetUserInfo();
349     if (!luo) {
350       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
351       return -1;
352     }
353     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
354       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
355       if (fTagCutSumm) break;
356     }
357     if (!fTagCutSumm) {
358       AliInfo(Form("No tag summary map in input tree\n"));
359       return -1;
360     }
361   }
362
363   TObjString *ostr = 0;
364   if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
365     ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
366   else {
367     AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
368     return -1;
369   }
370   char *iTagInfo;
371   iTagInfo = strdup(ostr->GetString().Data());
372
373   strtok(iTagInfo, ",");
374   Int_t iRej = atoi(strtok(NULL, ","));
375   
376   AliInfo(Form("Got %i accepted events for file %s", iRej,  fTree->GetCurrentFile()->GetName()));
377   
378   free(iTagInfo);
379
380   return iRej;
381 }
382
383 //______________________________________________________________________________
384 Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal,  Int_t *aAccepted,  Int_t *aRejected)
385 {
386   // Get number of events in the full chain
387   // Count accepted and rejected events
388   // return kFALSE if no info is available
389   if (!fTagCutSumm) {
390     TList *luo = fTree->GetUserInfo();
391     if (!luo) {
392       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
393       return kFALSE;
394     }
395     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
396       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
397       if (fTagCutSumm) break;
398     }
399     if (!fTagCutSumm) {
400       AliInfo(Form("No tag summary map in input tree\n"));
401       return kFALSE;
402     }
403   }
404   
405   TMapIter *tIter = new TMapIter(fTagCutSumm);
406   
407   Int_t iTotList=0, iAccList=0, iRejList=0;
408
409   TObject *cobj;
410   while ((cobj = tIter->Next())) {
411     TObjString *kstr = (TObjString *) cobj;
412     TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
413     //    printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
414     char *iTagInfo;
415     iTagInfo = strdup(vstr->GetString().Data());
416     
417     Int_t iAcc = atoi(strtok(iTagInfo, ","));
418     Int_t iRej = atoi(strtok(NULL, ","));
419     free(iTagInfo);
420     
421     iAccList += iAcc;
422     iRejList += iRej;
423     iTotList += (iAcc+iRej);
424   }
425
426   *aTotal = iTotList;
427   *aAccepted = iAccList;
428   *aRejected = iRejList;
429
430   return kTRUE;
431 }
432
433 //______________________________________________________________________________
434 Int_t AliESDInputHandler::GetNFilesEmpty()
435 {
436   // Count number of files in which all events were de-selected
437   // For such files Notify() will NOT be called
438   // return -1 if no info is available
439   if (!fTagCutSumm) {
440     TList *luo = fTree->GetUserInfo();
441     if (!luo) {
442       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
443       return -1;
444     }
445     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
446       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
447       if (fTagCutSumm) break;
448     }
449     if (!fTagCutSumm) {
450       AliInfo(Form("No tag summary map in input tree\n"));
451       return -1;
452     }
453   }
454   
455   TMapIter *tIter = new TMapIter(fTagCutSumm);
456   
457   Int_t iFilesEmpty = 0;
458
459   TObject *cobj;
460   while ((cobj = tIter->Next())) {
461     TObjString *kstr = (TObjString *) cobj;
462     TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
463     //    printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
464     char *iTagInfo;
465     iTagInfo = strdup(vstr->GetString().Data());
466     
467     Int_t iAcc = atoi(strtok(iTagInfo, ","));
468     Int_t iRej = atoi(strtok(NULL, ","));
469     free(iTagInfo);
470     if ((iAcc == 0) && ((iRej+iAcc)>0))
471       iFilesEmpty++;
472   }
473
474   return iFilesEmpty;
475   
476 }
477
478 //______________________________________________________________________________
479 TObject *AliESDInputHandler::GetStatistics(Option_t *option) const
480 {
481 // Get the statistics histogram(s) from the physics selection object. This
482 // should be called during FinishTaskOutput(). Option can be empty (default
483 // statistics histogram) or BIN0.
484    if (!fEventCuts) return NULL;
485    TString opt(option);
486    opt.ToUpper();
487    if (opt=="BIN0") return fEventCuts->GetStatistics("BIN0");
488    else return fEventCuts->GetStatistics("ALL");
489 }   
490
491 //______________________________________________________________________________
492 void AliESDInputHandler::CreatePIDResponse(Bool_t isMC/*=kFALSE*/)
493 {
494   //
495   // create the pid response object if it does not exist yet
496   //
497   if (fESDpid) return;
498   fESDpid=new AliESDpid(isMC);
499
500 }
501