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