]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliESDInputHandler.cxx
fixed an omission in the previous commit of this class
[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 (!fEvent) fEvent = new AliESDEvent();
96     fEvent->ReadFromTree(fTree);
97     fNEvents = fTree->GetEntries();
98     fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
99     return kTRUE;
100 }
101
102 Bool_t AliESDInputHandler::BeginEvent(Long64_t entry)
103 {
104     
105     // Copy from old to new format if necessary
106   AliESD* old = ((AliESDEvent*) fEvent)->GetAliESDOld();
107   if (old) {
108         ((AliESDEvent*)fEvent)->CopyFromOldESD();
109         old->Reset();
110   }
111
112   if (fHLTTree) {
113       fHLTTree->GetEntry(entry);
114   }
115   
116   fNewEvent = kTRUE;
117   //
118   // Event selection
119   // 
120   if (fEventCuts)
121     fIsSelected = fEventCuts->IsSelected((AliESDEvent*)fEvent); 
122   //
123   // Friends
124   ((AliESDEvent*)fEvent)->SetESDfriend(fFriend);
125   
126   return kTRUE;
127 }
128
129 Bool_t  AliESDInputHandler::FinishEvent()
130 {
131     // Finish the event 
132     if(fEvent)fEvent->Reset();
133     return kTRUE;
134
135
136 Bool_t AliESDInputHandler::Notify(const char* path)
137 {
138     // Notify a directory change
139     AliInfo(Form("Directory change %s \n", path));
140     //
141     if (fUseHLT) {
142         // Get HLTesdTree from current file
143         TTree* cTree = fTree;
144         if (fTree->GetTree()) cTree = fTree->GetTree();
145         TFile* cFile = cTree->GetCurrentFile();
146         cFile->GetObject("HLTesdTree", fHLTTree);
147         
148         if (fHLTTree) {
149           if (!fHLTEvent) fHLTEvent = new AliESDEvent();
150           fHLTEvent->ReadFromTree(fHLTTree);
151         }
152     }
153
154     if (!fUseTags) return (kTRUE);
155     
156     Bool_t zip = kFALSE;
157     
158     TString fileName(path);
159     if(fileName.Contains("#AliESDs.root")){
160         zip = kTRUE;
161     } 
162     else if (fileName.Contains("AliESDs.root")){
163         fileName.ReplaceAll("AliESDs.root", "");
164     }
165     else if(fileName.Contains("#AliAOD.root")){
166         zip = kTRUE;
167     }
168     else if(fileName.Contains("AliAOD.root")){
169         fileName.ReplaceAll("AliAOD.root", "");
170     }
171     else if(fileName.Contains("#galice.root")){
172         // For running with galice and kinematics alone...
173         zip = kTRUE;
174     }
175     else if(fileName.Contains("galice.root")){
176         // For running with galice and kinematics alone...
177         fileName.ReplaceAll("galice.root", "");
178     }
179
180     
181     TString pathName("./");
182     if (fileName.Length() != 0) {
183         pathName = fileName;
184     }
185     
186     printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
187
188     if (fRunTag) {
189         fRunTag->Clear();
190     } else {
191         fRunTag = new AliRunTag();
192     }
193     
194     delete fTreeT; fTreeT = 0;
195     
196     if (fChainT) {
197         delete fChainT;
198         fChainT = 0;
199     }
200     
201     if (!fChainT) {
202         fChainT = new TChain("T");
203     }
204     
205
206
207     const char* tagPattern = "ESD.tag.root";
208     const char* name = 0x0;
209     TString tagFilename;
210     if (zip) {
211         TFile* file = fTree->GetCurrentFile();
212         TArchiveFile* arch = file->GetArchive();
213         TObjArray* arr = arch->GetMembers();
214         TIter next(arr);
215         
216         while ((file = (TFile*) next())) {
217             name = file->GetName();
218             if (strstr(name,tagPattern)) { 
219                 tagFilename = pathName.Data();
220                 tagFilename += "#";
221                 tagFilename += name;
222                 fChainT->Add(tagFilename);  
223                 AliInfo(Form("Adding %s to tag chain \n", tagFilename.Data()));
224             }//pattern check
225         } // archive file loop
226     } else {
227         void * dirp = gSystem->OpenDirectory(pathName.Data());
228         while((name = gSystem->GetDirEntry(dirp))) {
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         }//directory loop
237     }
238     fChainT->SetBranchAddress("AliTAG",&fRunTag);
239     fChainT->GetEntry(0);
240     return kTRUE;
241 }
242
243
244
245 Option_t *AliESDInputHandler::GetDataType() const
246 {
247 // Returns handled data type.
248    return gESDDataType;
249 }
250
251 Int_t AliESDInputHandler::GetNEventAcceptedInFile()
252 {
253   // Get number of events in file accepted by the tag cuts
254   // return -1 if no info is available
255   if (!fTagCutSumm) {
256     TList *luo = fTree->GetUserInfo();
257     if (!luo) {
258       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
259       return -1;
260     }
261     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
262       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
263       if (fTagCutSumm) break;
264     }
265     if (!fTagCutSumm) {
266       AliInfo(Form("No tag summary map in input tree\n"));
267       return -1;
268     }
269   }
270
271   TObjString *ostr = 0;
272   if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
273     ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
274   else {
275     AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
276     return -1;
277   }
278   char *iTagInfo;
279   iTagInfo = strdup(ostr->GetString().Data());
280
281   Int_t iAcc = atoi(strtok(iTagInfo, ","));
282   
283   AliInfo(Form("Got %i accepted events for file %s", iAcc,  fTree->GetCurrentFile()->GetName()));
284   
285   free(iTagInfo);
286
287   return iAcc;
288 }
289 Int_t AliESDInputHandler::GetNEventRejectedInFile()
290 {
291   // Get number of events in file rejected 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   strtok(iTagInfo, ",");
320   Int_t iRej = atoi(strtok(NULL, ","));
321   
322   AliInfo(Form("Got %i accepted events for file %s", iRej,  fTree->GetCurrentFile()->GetName()));
323   
324   free(iTagInfo);
325
326   return iRej;
327 }
328 Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal, Int_t *aAccepted, Int_t *aRejected)
329 {
330   // Get number of events in the full chain
331   // Count accepted and rejected events
332   // return kFALSE 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 kFALSE;
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 kFALSE;
346     }
347   }
348   
349   TMapIter *tIter = new TMapIter(fTagCutSumm);
350   
351   Int_t iTotList=0, iAccList=0, iRejList=0;
352
353   TObject *cobj;
354   while ((cobj = tIter->Next())) {
355     TObjString *kstr = (TObjString *) cobj;
356     TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
357     //    printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
358     char *iTagInfo;
359     iTagInfo = strdup(vstr->GetString().Data());
360     
361     Int_t iAcc = atoi(strtok(iTagInfo, ","));
362     Int_t iRej = atoi(strtok(NULL, ","));
363     
364     iAccList += iAcc;
365     iRejList += iRej;
366     iTotList += (iAcc+iRej);
367   }
368
369   *aTotal = iTotList;
370   *aAccepted = iAccList;
371   *aRejected = iRejList;
372
373   return kTRUE;
374 }
375
376 Int_t AliESDInputHandler::GetNFilesEmpty()
377 {
378   // Count number of files in which all events were de-selected
379   // For such files Notify() will NOT be called
380   // return -1 if no info is available
381   if (!fTagCutSumm) {
382     TList *luo = fTree->GetUserInfo();
383     if (!luo) {
384       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
385       return -1;
386     }
387     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
388       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
389       if (fTagCutSumm) break;
390     }
391     if (!fTagCutSumm) {
392       AliInfo(Form("No tag summary map in input tree\n"));
393       return -1;
394     }
395   }
396   
397   TMapIter *tIter = new TMapIter(fTagCutSumm);
398   
399   Int_t iFilesEmpty = 0;
400
401   TObject *cobj;
402   while ((cobj = tIter->Next())) {
403     TObjString *kstr = (TObjString *) cobj;
404     TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
405     //    printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
406     char *iTagInfo;
407     iTagInfo = strdup(vstr->GetString().Data());
408     
409     Int_t iAcc = atoi(strtok(iTagInfo, ","));
410     Int_t iRej = atoi(strtok(NULL, ","));
411     
412     if ((iAcc == 0) && ((iRej+iAcc)>0))
413       iFilesEmpty++;
414   }
415
416   return iFilesEmpty;
417   
418 }