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