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