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