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