]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliESDInputHandler.cxx
Change the X and Y values stored by the VertexerZ from (0.,0.) which are the
[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    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       
165       TTree* cTree = fTree->GetTree();
166       if (!cTree) cTree = fTree;
167       
168       cTree->AddFriend("esdFriendTree", esdFriendTreeFName.Data());
169       cTree->SetBranchStatus("ESDfriend.", 1);
170       fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
171       cTree->SetBranchAddress("ESDfriend.", &fFriend);
172     } 
173     //
174     //
175     SwitchOffBranches();
176     SwitchOnBranches();
177     fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
178     //
179     if (fUseHLT) {
180         // Get HLTesdTree from current file
181         TTree* cTree = fTree;
182         if (fTree->GetTree()) cTree = fTree->GetTree();
183         TFile* cFile = cTree->GetCurrentFile();
184         cFile->GetObject("HLTesdTree", fHLTTree);
185         
186         if (fHLTTree) {
187           if (!fHLTEvent) fHLTEvent = new AliESDEvent();
188           fHLTEvent->ReadFromTree(fHLTTree);
189         }
190     }
191
192     if (!fUseTags) return (kTRUE);
193     
194     Bool_t zip = kFALSE;
195     
196     TString fileName(path);
197     if(fileName.Contains("#AliESDs.root")){
198         zip = kTRUE;
199     } 
200     else if (fileName.Contains("AliESDs.root")){
201         fileName.ReplaceAll("AliESDs.root", "");
202     }
203     else if(fileName.Contains("#AliAOD.root")){
204         zip = kTRUE;
205     }
206     else if(fileName.Contains("AliAOD.root")){
207         fileName.ReplaceAll("AliAOD.root", "");
208     }
209     else if(fileName.Contains("#galice.root")){
210         // For running with galice and kinematics alone...
211         zip = kTRUE;
212     }
213     else if(fileName.Contains("galice.root")){
214         // For running with galice and kinematics alone...
215         fileName.ReplaceAll("galice.root", "");
216     }
217
218     
219     TString pathName("./");
220     if (fileName.Length() != 0) {
221         pathName = fileName;
222     }
223     
224     printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
225
226     if (fRunTag) {
227         fRunTag->Clear();
228     } else {
229         fRunTag = new AliRunTag();
230     }
231     
232     delete fTreeT; fTreeT = 0;
233     
234     if (fChainT) {
235         delete fChainT;
236         fChainT = 0;
237     }
238     
239     if (!fChainT) {
240         fChainT = new TChain("T");
241     }
242     
243
244
245     const char* tagPattern = "ESD.tag.root";
246     const char* name = 0x0;
247     TString tagFilename;
248     if (zip) {
249         TFile* file = fTree->GetCurrentFile();
250         TArchiveFile* arch = file->GetArchive();
251         TObjArray* arr = arch->GetMembers();
252         TIter next(arr);
253         
254         while ((file = (TFile*) next())) {
255             name = file->GetName();
256             if (strstr(name,tagPattern)) { 
257                 tagFilename = pathName.Data();
258                 tagFilename += "#";
259                 tagFilename += name;
260                 fChainT->Add(tagFilename);  
261                 AliInfo(Form("Adding %s to tag chain \n", tagFilename.Data()));
262             }//pattern check
263         } // archive file loop
264     } else {
265         void * dirp = gSystem->OpenDirectory(pathName.Data());
266         while((name = gSystem->GetDirEntry(dirp))) {
267             if (strstr(name,tagPattern)) { 
268                 tagFilename = pathName.Data();
269                 tagFilename += "/";
270                 tagFilename += name;
271                 fChainT->Add(tagFilename);  
272                 AliInfo(Form("Adding %s to tag chain \n", tagFilename.Data()));
273             }//pattern check
274         }//directory loop
275     }
276     fChainT->SetBranchAddress("AliTAG",&fRunTag);
277     fChainT->GetEntry(0);
278     return kTRUE;
279 }
280
281 //______________________________________________________________________________
282 Option_t *AliESDInputHandler::GetDataType() const
283 {
284 // Returns handled data type.
285    return gESDDataType;
286 }
287
288 //______________________________________________________________________________
289 Int_t AliESDInputHandler::GetNEventAcceptedInFile()
290 {
291   // Get number of events in file accepted by the tag cuts
292   // return -1 if no info is available
293   if (!fTagCutSumm) {
294     TList *luo = fTree->GetUserInfo();
295     if (!luo) {
296       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
297       return -1;
298     }
299     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
300       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
301       if (fTagCutSumm) break;
302     }
303     if (!fTagCutSumm) {
304       AliInfo(Form("No tag summary map in input tree\n"));
305       return -1;
306     }
307   }
308
309   TObjString *ostr = 0;
310   if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
311     ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
312   else {
313     AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
314     return -1;
315   }
316   char *iTagInfo;
317   iTagInfo = strdup(ostr->GetString().Data());
318
319   Int_t iAcc = atoi(strtok(iTagInfo, ","));
320   
321   AliInfo(Form("Got %i accepted events for file %s", iAcc,  fTree->GetCurrentFile()->GetName()));
322   
323   free(iTagInfo);
324
325   return iAcc;
326 }
327
328 //______________________________________________________________________________
329 Int_t AliESDInputHandler::GetNEventRejectedInFile()
330 {
331   // Get number of events in file rejected by the tag cuts
332   // return -1 if no info is available
333   if (!fTagCutSumm) {
334     TList *luo = fTree->GetUserInfo();
335     if (!luo) {
336       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
337       return -1;
338     }
339     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
340       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
341       if (fTagCutSumm) break;
342     }
343     if (!fTagCutSumm) {
344       AliInfo(Form("No tag summary map in input tree\n"));
345       return -1;
346     }
347   }
348
349   TObjString *ostr = 0;
350   if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
351     ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
352   else {
353     AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
354     return -1;
355   }
356   char *iTagInfo;
357   iTagInfo = strdup(ostr->GetString().Data());
358
359   strtok(iTagInfo, ",");
360   Int_t iRej = atoi(strtok(NULL, ","));
361   
362   AliInfo(Form("Got %i accepted events for file %s", iRej,  fTree->GetCurrentFile()->GetName()));
363   
364   free(iTagInfo);
365
366   return iRej;
367 }
368
369 //______________________________________________________________________________
370 Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal, Int_t *aAccepted, Int_t *aRejected)
371 {
372   // Get number of events in the full chain
373   // Count accepted and rejected events
374   // return kFALSE if no info is available
375   if (!fTagCutSumm) {
376     TList *luo = fTree->GetUserInfo();
377     if (!luo) {
378       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
379       return kFALSE;
380     }
381     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
382       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
383       if (fTagCutSumm) break;
384     }
385     if (!fTagCutSumm) {
386       AliInfo(Form("No tag summary map in input tree\n"));
387       return kFALSE;
388     }
389   }
390   
391   TMapIter *tIter = new TMapIter(fTagCutSumm);
392   
393   Int_t iTotList=0, iAccList=0, iRejList=0;
394
395   TObject *cobj;
396   while ((cobj = tIter->Next())) {
397     TObjString *kstr = (TObjString *) cobj;
398     TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
399     //    printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
400     char *iTagInfo;
401     iTagInfo = strdup(vstr->GetString().Data());
402     
403     Int_t iAcc = atoi(strtok(iTagInfo, ","));
404     Int_t iRej = atoi(strtok(NULL, ","));
405     
406     iAccList += iAcc;
407     iRejList += iRej;
408     iTotList += (iAcc+iRej);
409   }
410
411   *aTotal = iTotList;
412   *aAccepted = iAccList;
413   *aRejected = iRejList;
414
415   return kTRUE;
416 }
417
418 //______________________________________________________________________________
419 Int_t AliESDInputHandler::GetNFilesEmpty()
420 {
421   // Count number of files in which all events were de-selected
422   // For such files Notify() will NOT be called
423   // return -1 if no info is available
424   if (!fTagCutSumm) {
425     TList *luo = fTree->GetUserInfo();
426     if (!luo) {
427       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
428       return -1;
429     }
430     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
431       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
432       if (fTagCutSumm) break;
433     }
434     if (!fTagCutSumm) {
435       AliInfo(Form("No tag summary map in input tree\n"));
436       return -1;
437     }
438   }
439   
440   TMapIter *tIter = new TMapIter(fTagCutSumm);
441   
442   Int_t iFilesEmpty = 0;
443
444   TObject *cobj;
445   while ((cobj = tIter->Next())) {
446     TObjString *kstr = (TObjString *) cobj;
447     TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
448     //    printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
449     char *iTagInfo;
450     iTagInfo = strdup(vstr->GetString().Data());
451     
452     Int_t iAcc = atoi(strtok(iTagInfo, ","));
453     Int_t iRej = atoi(strtok(NULL, ","));
454     
455     if ((iAcc == 0) && ((iRej+iAcc)>0))
456       iFilesEmpty++;
457   }
458
459   return iFilesEmpty;
460   
461 }