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