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