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