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