]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliESDInputHandler.cxx
Event tag is available during UserExec via EventTag() function
[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   fEventTag(0),
64   fReadFriends(0),
65   fFriendFileName("AliESDfriends.root")
66 {
67   // default constructor
68 }
69
70 //______________________________________________________________________________
71 AliESDInputHandler::~AliESDInputHandler() 
72 {
73   //  destructor
74   if (fRunTag) delete fRunTag;
75 }
76
77 //______________________________________________________________________________
78 AliESDInputHandler::AliESDInputHandler(const char* name, const char* title):
79     AliInputEventHandler(name, title), fEvent(0x0), fFriend(0x0), fESDpid(0x0), fAnalysisType(0),
80     fNEvents(0),  fHLTEvent(0x0), fHLTTree(0x0), fUseHLT(kFALSE), fTagCutSumm(0x0), fUseTags(kFALSE), fChainT(0), fTreeT(0), fRunTag(0), fEventTag(0), fReadFriends(0), fFriendFileName("AliESDfriends.root")
81 {
82     // Constructor
83 }
84
85 //______________________________________________________________________________
86 Bool_t AliESDInputHandler::Init(TTree* tree,  Option_t* opt)
87 {
88     //
89     // Initialisation necessary for each new tree 
90     // 
91     fAnalysisType = opt;
92     fTree = tree;
93     
94     if (!fTree) return kFALSE;
95     fTree->GetEntry(0);
96     
97
98     if (!fEvent) fEvent = new AliESDEvent();
99     fEvent->ReadFromTree(fTree);
100     fNEvents = fTree->GetEntries();
101
102     if (fMixingHandler) fMixingHandler->Init(tree,  opt);
103
104     return kTRUE;
105 }
106
107 //______________________________________________________________________________
108 Bool_t AliESDInputHandler::BeginEvent(Long64_t entry)
109 {
110     
111     // Copy from old to new format if necessary
112   static Bool_t called = kFALSE;
113   if (!called && fEventCuts && IsUserCallSelectionMask())
114      AliInfo(Form("The ESD input handler expects that the first task calls AliESDInputHandler::CheckSelectionMask() %s", fEventCuts->ClassName()));
115   AliESD* old = ((AliESDEvent*) fEvent)->GetAliESDOld();
116   if (old) {
117    ((AliESDEvent*)fEvent)->CopyFromOldESD();
118    old->Reset();
119   }
120
121   if (fHLTTree) {
122       fHLTTree->GetEntry(entry);
123   }
124   
125   fNewEvent = kTRUE;
126   //
127   // Event selection
128   // 
129   fIsSelectedResult = 0;
130   if (fEventCuts && !IsUserCallSelectionMask())
131       fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent); 
132   //
133   // Friends
134   ((AliESDEvent*)fEvent)->SetESDfriend(fFriend);
135   called = kTRUE;
136
137   if (fMixingHandler) fMixingHandler->BeginEvent(entry);
138   if (fUseTags && fRunTag) {
139     fEventTag = 0;
140     if (entry >= fRunTag->GetNEvents()) {
141       AliError(Form("Current event %d does not match max range from run tag: 0-%d", (Int_t)entry, fRunTag->GetNEvents()));
142       return kTRUE;
143     }
144     fEventTag = fRunTag->GetEventTag(entry);   
145   }      
146   return kTRUE;
147 }
148
149 //______________________________________________________________________________
150 void AliESDInputHandler::CheckSelectionMask()
151 {
152 // This method can be called by a task only if IsUserCallSelectionMask is true.
153    if (!fEventCuts || !IsUserCallSelectionMask()) return;
154    fIsSelectedResult = fEventCuts->GetSelectionMask((AliESDEvent*)fEvent);
155 }
156    
157 //______________________________________________________________________________
158 Bool_t  AliESDInputHandler::FinishEvent()
159 {
160     // Finish the event 
161   if(fEvent)fEvent->Reset();
162   if (fMixingHandler) fMixingHandler->FinishEvent();
163   return kTRUE;
164
165
166 //______________________________________________________________________________
167 Bool_t AliESDInputHandler::Notify(const char* path)
168 {
169   // Notify a directory change
170   static Bool_t firsttime = kFALSE;
171   AliInfo(Form("Directory change %s \n", path));
172   //
173   // Handle the friends first
174   //
175   if (!fTree->FindBranch("ESDfriend.") && fReadFriends) {
176     // Try to add ESDfriend. branch as friend
177     TString esdTreeFName, esdFriendTreeFName;    
178     esdTreeFName = (fTree->GetCurrentFile())->GetName();
179     esdFriendTreeFName = esdTreeFName;
180     esdFriendTreeFName.ReplaceAll("AliESDs.root", fFriendFileName.Data());
181     TTree* cTree = fTree->GetTree();
182     if (!cTree) cTree = fTree;      
183     cTree->AddFriend("esdFriendTree", esdFriendTreeFName.Data());
184     cTree->SetBranchStatus("ESDfriend.", 1);
185     fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
186     if (fFriend) cTree->SetBranchAddress("ESDfriend.", &fFriend);
187   } 
188   //
189   //
190   SwitchOffBranches();
191   SwitchOnBranches();
192   fFriend = (AliESDfriend*)(fEvent->FindListObject("AliESDfriend"));
193   //
194   if (fUseHLT) {
195     // Get HLTesdTree from current file
196     TTree* cTree = fTree;
197     if (fTree->GetTree()) cTree = fTree->GetTree();
198     TFile* cFile = cTree->GetCurrentFile();
199     cFile->GetObject("HLTesdTree", fHLTTree);
200         
201     if (fHLTTree) {
202            if (!fHLTEvent) fHLTEvent = new AliESDEvent();
203            fHLTEvent->ReadFromTree(fHLTTree);
204     }
205   }
206
207   if (!fUseTags) {
208     if (fMixingHandler) fMixingHandler->Notify(path);
209     return kTRUE;
210   }
211     
212   Bool_t zip = kFALSE;
213     
214   // Setup the base path
215   TString pathName(path);
216   Int_t index = pathName.Index("#");
217   if (index>=0) {
218     zip = kTRUE;
219     pathName = pathName(0,index);
220   } else {
221     pathName = gSystem->DirName(pathName);
222   }
223   if (fTree->GetCurrentFile()->GetArchive()) zip = kTRUE;
224   if (pathName.IsNull()) pathName = "./";  
225   printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
226
227   if (fRunTag) {
228     fRunTag->Clear();
229   } else {
230     fRunTag = new AliRunTag();
231   }
232     
233   const char* tagPattern = "ESD.tag.root";
234   TString sname;
235   TString tagFilename;
236   if (zip) {
237     TObjArray* arr = fTree->GetCurrentFile()->GetArchive()->GetMembers();
238     TIter next(arr);
239     TObject *objarchive;
240     while ((objarchive = next())) {
241       sname = objarchive->GetName();
242            if (sname.Contains(tagPattern)) { 
243         tagFilename = pathName;
244         if (index>=0) tagFilename += "#";
245         else tagFilename += "/";
246         tagFilename += sname;
247         AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
248         break; // There should be only one such file in the archive
249       }//pattern check
250     } // archive file loop
251   } else {
252     void * dirp = gSystem->OpenDirectory(pathName.Data());
253     while(1) {
254       sname = gSystem->GetDirEntry(dirp);
255       if (sname.IsNull()) break;
256       if (sname.Contains(tagPattern)) { 
257         tagFilename = pathName;
258         tagFilename += "/";
259         tagFilename += sname;
260         AliInfo(Form("Tag file found: %s\n", tagFilename.Data()));
261         break;
262       }//pattern check
263     }//directory loop
264   }
265   if (tagFilename.IsNull()) {
266     if (firsttime) AliWarning(Form("Tag file not found in directory: %s", pathName.Data()));
267     firsttime = kFALSE;
268     delete fRunTag; fRunTag = 0;
269     return kTRUE;
270   }
271   TFile *tagfile = TFile::Open(tagFilename);
272   if (!tagfile) {
273     AliError(Form("Cannot open tag file: %s", tagFilename.Data()));
274     delete fRunTag; fRunTag = 0;
275     return kTRUE;
276   }   
277   fTreeT = (TTree*)tagfile->Get("T"); // file is the owner
278   if (!fTreeT) {
279     AliError(Form("Cannot get tree of tags from file: %s", tagFilename.Data()));
280     delete fRunTag; fRunTag = 0;
281     return kTRUE;
282   }
283     
284   fTreeT->SetBranchAddress("AliTAG",&fRunTag);
285   fTreeT->GetEntry(0);
286   delete tagfile;
287   // Notify the mixing handler after the tags are loaded
288   if (fMixingHandler) fMixingHandler->Notify(path);
289   return kTRUE;
290 }
291
292 //______________________________________________________________________________
293 Option_t *AliESDInputHandler::GetDataType() const
294 {
295 // Returns handled data type.
296    return gESDDataType;
297 }
298
299 //______________________________________________________________________________
300 Int_t AliESDInputHandler::GetNEventAcceptedInFile()
301 {
302   // Get number of events in file accepted by the tag cuts
303   // return -1 if no info is available
304   if (!fTagCutSumm) {
305     TList *luo = fTree->GetUserInfo();
306     if (!luo) {
307       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
308       return -1;
309     }
310     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
311       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
312       if (fTagCutSumm) break;
313     }
314     if (!fTagCutSumm) {
315       AliInfo(Form("No tag summary map in input tree\n"));
316       return -1;
317     }
318   }
319
320   TObjString *ostr = 0;
321   if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
322     ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
323   else {
324     AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
325     return -1;
326   }
327   char *iTagInfo;
328   iTagInfo = strdup(ostr->GetString().Data());
329
330   Int_t iAcc = atoi(strtok(iTagInfo, ","));
331   
332   AliInfo(Form("Got %i accepted events for file %s", iAcc,  fTree->GetCurrentFile()->GetName()));
333   
334   free(iTagInfo);
335
336   return iAcc;
337 }
338
339 //______________________________________________________________________________
340 Int_t AliESDInputHandler::GetNEventRejectedInFile()
341 {
342   // Get number of events in file rejected by the tag cuts
343   // return -1 if no info is available
344   if (!fTagCutSumm) {
345     TList *luo = fTree->GetUserInfo();
346     if (!luo) {
347       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
348       return -1;
349     }
350     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
351       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
352       if (fTagCutSumm) break;
353     }
354     if (!fTagCutSumm) {
355       AliInfo(Form("No tag summary map in input tree\n"));
356       return -1;
357     }
358   }
359
360   TObjString *ostr = 0;
361   if (fTagCutSumm->FindObject(fTree->GetCurrentFile()->GetName()))
362     ostr = (TObjString *) fTagCutSumm->GetValue(fTree->GetCurrentFile()->GetName());
363   else {
364     AliInfo(Form("No tag cut summary for file %s\n", fTree->GetCurrentFile()->GetName()));
365     return -1;
366   }
367   char *iTagInfo;
368   iTagInfo = strdup(ostr->GetString().Data());
369
370   strtok(iTagInfo, ",");
371   Int_t iRej = atoi(strtok(NULL, ","));
372   
373   AliInfo(Form("Got %i accepted events for file %s", iRej,  fTree->GetCurrentFile()->GetName()));
374   
375   free(iTagInfo);
376
377   return iRej;
378 }
379
380 //______________________________________________________________________________
381 Bool_t AliESDInputHandler::GetCutSummaryForChain(Int_t *aTotal, Int_t *aAccepted, Int_t *aRejected)
382 {
383   // Get number of events in the full chain
384   // Count accepted and rejected events
385   // return kFALSE if no info is available
386   if (!fTagCutSumm) {
387     TList *luo = fTree->GetUserInfo();
388     if (!luo) {
389       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
390       return kFALSE;
391     }
392     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
393       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
394       if (fTagCutSumm) break;
395     }
396     if (!fTagCutSumm) {
397       AliInfo(Form("No tag summary map in input tree\n"));
398       return kFALSE;
399     }
400   }
401   
402   TMapIter *tIter = new TMapIter(fTagCutSumm);
403   
404   Int_t iTotList=0, iAccList=0, iRejList=0;
405
406   TObject *cobj;
407   while ((cobj = tIter->Next())) {
408     TObjString *kstr = (TObjString *) cobj;
409     TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
410     //    printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
411     char *iTagInfo;
412     iTagInfo = strdup(vstr->GetString().Data());
413     
414     Int_t iAcc = atoi(strtok(iTagInfo, ","));
415     Int_t iRej = atoi(strtok(NULL, ","));
416     
417     iAccList += iAcc;
418     iRejList += iRej;
419     iTotList += (iAcc+iRej);
420   }
421
422   *aTotal = iTotList;
423   *aAccepted = iAccList;
424   *aRejected = iRejList;
425
426   return kTRUE;
427 }
428
429 //______________________________________________________________________________
430 Int_t AliESDInputHandler::GetNFilesEmpty()
431 {
432   // Count number of files in which all events were de-selected
433   // For such files Notify() will NOT be called
434   // return -1 if no info is available
435   if (!fTagCutSumm) {
436     TList *luo = fTree->GetUserInfo();
437     if (!luo) {
438       AliInfo(Form("No user info in input tree - no tag cut summary\n"));
439       return -1;
440     }
441     for (int iluo=0; iluo<luo->GetEntries(); iluo++) {
442       fTagCutSumm = dynamic_cast<TMap *>(luo->At(iluo));
443       if (fTagCutSumm) break;
444     }
445     if (!fTagCutSumm) {
446       AliInfo(Form("No tag summary map in input tree\n"));
447       return -1;
448     }
449   }
450   
451   TMapIter *tIter = new TMapIter(fTagCutSumm);
452   
453   Int_t iFilesEmpty = 0;
454
455   TObject *cobj;
456   while ((cobj = tIter->Next())) {
457     TObjString *kstr = (TObjString *) cobj;
458     TObjString *vstr = (TObjString *) fTagCutSumm->GetValue(kstr->GetString().Data());
459     //    printf("Got object value %s %s\n", kstr->GetString().Data(), vstr->GetString().Data());
460     char *iTagInfo;
461     iTagInfo = strdup(vstr->GetString().Data());
462     
463     Int_t iAcc = atoi(strtok(iTagInfo, ","));
464     Int_t iRej = atoi(strtok(NULL, ","));
465     
466     if ((iAcc == 0) && ((iRej+iAcc)>0))
467       iFilesEmpty++;
468   }
469
470   return iFilesEmpty;
471   
472 }
473
474 //______________________________________________________________________________
475 TObject *AliESDInputHandler::GetStatistics(Option_t *option) const
476 {
477 // Get the statistics histogram(s) from the physics selection object. This
478 // should be called during FinishTaskOutput(). Option can be empty (default
479 // statistics histogram) or BIN0.
480    if (!fEventCuts) return NULL;
481    TString opt(option);
482    opt.ToUpper();
483    if (opt=="BIN0") return fEventCuts->GetStatistics("BIN0");
484    else return fEventCuts->GetStatistics("ALL");
485 }