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