]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliESDInputHandler.cxx
For embedding: use AliESDEvent instead of AliESD (obsolete)
[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
33 #include "AliESDInputHandler.h"
34 #include "AliESDEvent.h"
35 #include "AliESD.h"
36 #include "AliRunTag.h"
37 #include "AliEventTag.h"
38 #include "AliLog.h"
39
40 ClassImp(AliESDInputHandler)
41
42 static Option_t *gESDDataType = "ESD";
43
44 //______________________________________________________________________________
45 AliESDInputHandler::AliESDInputHandler() :
46   AliInputEventHandler(),
47   fEvent(0x0),
48   fBranches(""),
49   fBranchesOn(""),
50   fAnalysisType(0),
51   fNEvents(0),
52   fUseTags(kFALSE),
53   fChainT(0),
54   fTreeT(0),
55   fRunTag(0)
56 {
57   // default constructor
58 }
59
60 //______________________________________________________________________________
61 AliESDInputHandler::~AliESDInputHandler() 
62 {
63   //  destructor
64   //  delete fEvent;
65 }
66
67 //______________________________________________________________________________
68 AliESDInputHandler::AliESDInputHandler(const char* name, const char* title):
69     AliInputEventHandler(name, title), fEvent(0x0), fBranches(""), fBranchesOn(""), fAnalysisType(0),
70      fNEvents(0), fUseTags(kFALSE), fChainT(0), fTreeT(0), fRunTag(0)
71 {
72     // Constructor
73 }
74
75 Bool_t AliESDInputHandler::Init(TTree* tree,  Option_t* opt)
76 {
77     // Initialisation necessary for each new tree 
78     fAnalysisType = opt;
79     fTree         = tree;
80     
81     if (!fTree) return kFALSE;
82     // Get pointer to ESD event
83     SwitchOffBranches();
84     SwitchOnBranches();
85     
86     if (fEvent) {
87       delete fEvent;
88       fEvent = 0;
89     }
90     fEvent = new AliESDEvent();
91
92     fEvent->ReadFromTree(fTree);
93     fNEvents = fTree->GetEntries();
94     return kTRUE;
95 }
96
97 Bool_t AliESDInputHandler::BeginEvent(Long64_t /*entry*/)
98 {
99     // Copy from old to new format if necessary
100   AliESD* old = ((AliESDEvent*) fEvent)->GetAliESDOld();
101   if (old) {
102         ((AliESDEvent*)fEvent)->CopyFromOldESD();
103         old->Reset();
104   }
105   return kTRUE;
106 }
107
108 Bool_t  AliESDInputHandler::FinishEvent()
109 {
110     // Finish the event 
111     if(fEvent)fEvent->Reset();
112     return kTRUE;
113
114
115 Bool_t AliESDInputHandler::Notify(const char* path)
116 {
117     // Notify a directory change
118     AliInfo(Form("Directory change %s \n", path));
119     //
120     if (!fUseTags) return (kTRUE);
121     
122     Bool_t zip = kFALSE;
123     
124     TString fileName(path);
125     if(fileName.Contains("#AliESDs.root")){
126         zip = kTRUE;
127     } 
128     else if (fileName.Contains("AliESDs.root")){
129         fileName.ReplaceAll("AliESDs.root", "");
130     }
131     else if(fileName.Contains("#AliAOD.root")){
132         zip = kTRUE;
133     }
134     else if(fileName.Contains("AliAOD.root")){
135         fileName.ReplaceAll("AliAOD.root", "");
136     }
137     else if(fileName.Contains("#galice.root")){
138         // For running with galice and kinematics alone...
139         zip = kTRUE;
140     }
141     else if(fileName.Contains("galice.root")){
142         // For running with galice and kinematics alone...
143         fileName.ReplaceAll("galice.root", "");
144     }
145
146     
147     TString pathName("./");
148     if (fileName.Length() != 0) {
149         pathName = fileName;
150     }
151     
152     printf("AliESDInputHandler::Notify() Path: %s\n", pathName.Data());
153     
154     if (fRunTag) {
155         fRunTag->Clear();
156     } else {
157         fRunTag = new AliRunTag();
158     }
159     
160     delete fTreeT; fTreeT = 0;
161
162     if (fChainT) {
163         delete fChainT;
164         fChainT = 0;
165     }
166     
167     if (!fChainT) {
168         fChainT = new TChain("T");
169     }
170     
171
172
173     const char* tagPattern = "ESD.tag.root";
174     const char* name = 0x0;
175     TString tagFilename;
176     if (zip) {
177         TFile* file = fTree->GetCurrentFile();
178         TArchiveFile* arch = file->GetArchive();
179         TObjArray* arr = arch->GetMembers();
180         TIter next(arr);
181         
182         while ((file = (TFile*) next())) {
183             name = file->GetName();
184             if (strstr(name,tagPattern)) { 
185                 tagFilename = pathName.Data();
186                 tagFilename += "#";
187                 tagFilename += name;
188                 fChainT->Add(tagFilename);  
189                 AliInfo(Form("Adding %s to tag chain \n", tagFilename.Data()));
190             }//pattern check
191         } // archive file loop
192     } else {
193         void * dirp = gSystem->OpenDirectory(pathName.Data());
194         while((name = gSystem->GetDirEntry(dirp))) {
195             if (strstr(name,tagPattern)) { 
196                 tagFilename = pathName.Data();
197                 tagFilename += "/";
198                 tagFilename += name;
199                 fChainT->Add(tagFilename);  
200                 AliInfo(Form("Adding %s to tag chain \n", tagFilename.Data()));
201             }//pattern check
202         }//directory loop
203     }
204     fChainT->SetBranchAddress("AliTAG",&fRunTag);
205     fChainT->GetEntry(0);
206     return kTRUE;
207 }
208
209
210 void AliESDInputHandler::SwitchOffBranches() const {
211   //
212   // Switch of branches on user request
213     TObjArray * tokens = fBranches.Tokenize(" ");
214     Int_t ntok = tokens->GetEntries();
215     for (Int_t i = 0; i < ntok; i++)  {
216         TString str = ((TObjString*) tokens->At(i))->GetString();
217         if (str.Length() == 0)
218             continue;
219         fTree->SetBranchStatus(Form("%s%s%s","*", str.Data(), "*"), 0);
220         AliInfo(Form("Branch %s switched off \n", str.Data()));
221     }
222 }
223
224 void AliESDInputHandler::SwitchOnBranches() const {
225   //
226   // Switch of branches on user request
227   TObjArray * tokens = fBranchesOn.Tokenize(" ");
228   Int_t ntok = tokens->GetEntries();
229
230   for (Int_t i = 0; i < ntok; i++)  {
231       TString str = ((TObjString*) tokens->At(i))->GetString();
232       if (str.Length() == 0)
233           continue;
234       fTree->SetBranchStatus(Form("%s%s%s","*", str.Data(), "*"), 1);
235       AliInfo(Form("Branch %s switched on \n", str.Data()));
236   }
237 }
238
239 Option_t *AliESDInputHandler::GetDataType() const
240 {
241 // Returns handled data type.
242    return gESDDataType;
243 }