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