]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliAODInputHandler.cxx
Fix for #81567: fix in SetTOFResponse method (AliESDpid)
[u/mrichter/AliRoot.git] / STEER / AliAODInputHandler.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 AOD input 
20 //     Author: Andreas Morsch, CERN
21 //-------------------------------------------------------------------------
22
23 #include <TSystem.h>
24 #include <TTree.h>
25 #include <TList.h>
26 #include <TNamed.h>
27 #include <TFile.h>
28 #include <TH2.h>
29
30 #include "AliAODInputHandler.h"
31 #include "AliAODEvent.h"
32 #include "AliVCuts.h"
33 #include "AliMCEvent.h"
34 #include "AliAODpidUtil.h"
35
36 ClassImp(AliAODInputHandler)
37
38 static Option_t *gAODDataType = "AOD";
39
40 //______________________________________________________________________________
41 AliAODInputHandler::AliAODInputHandler() :
42     AliInputEventHandler(),
43     fEvent(0),
44     fMCEvent(new AliMCEvent()),
45     fFriends(new TList()),
46     fAODpidUtil(0x0),
47     fMergeEvents(kFALSE),
48     fMergeTracks(kTRUE), 
49     fMergeEMCALClusters(kTRUE), 
50     fMergePHOSClusters(kTRUE), 
51     fMergeEMCALCells(kTRUE), 
52     fMergePHOSCells(kTRUE),
53     fFriendsConnected(kFALSE),
54     fFileToMerge(0),
55     fTreeToMerge(0),
56     fAODEventToMerge(0),
57     fMergeOffset(0)
58 {
59   // Default constructor
60   fHistStatistics[0] = fHistStatistics[1] = NULL;
61 }
62
63 //______________________________________________________________________________
64 AliAODInputHandler::AliAODInputHandler(const char* name, const char* title):
65   AliInputEventHandler(name, title),
66   fEvent(0),
67   fMCEvent(new AliMCEvent()),
68   fFriends(new TList()),
69   fAODpidUtil(0x0),
70   fMergeEvents(kFALSE),
71   fMergeTracks(kTRUE), 
72   fMergeEMCALClusters(kTRUE), 
73   fMergePHOSClusters(kTRUE), 
74   fMergeEMCALCells(kTRUE), 
75   fMergePHOSCells(kTRUE),
76   fFriendsConnected(kFALSE),
77   fFileToMerge(0),
78   fTreeToMerge(0),
79   fAODEventToMerge(0),
80   fMergeOffset(0)
81 {
82     // Constructor
83   fHistStatistics[0] = fHistStatistics[1] = NULL;
84 }
85
86 //______________________________________________________________________________
87 AliAODInputHandler::~AliAODInputHandler() 
88 {
89 // Destructor
90   fFriends->Delete();
91   if (fHistStatistics[0]) {
92     delete fHistStatistics[0];
93     fHistStatistics[0] = 0;
94   }
95   if (fHistStatistics[1]) {
96     delete fHistStatistics[1];
97     fHistStatistics[1] = 0;
98   }
99   delete fAODpidUtil;
100 }
101
102 //______________________________________________________________________________
103 Bool_t AliAODInputHandler::Init(TTree* tree, Option_t* opt)
104 {
105     // Initialisation necessary for each new tree
106     fTree = tree;
107     if (!fTree) return kFALSE;
108     fTree->GetEntries();
109     ConnectFriends();
110
111     SwitchOffBranches();
112     SwitchOnBranches();
113     
114     // Get pointer to AOD event
115     if (!fEvent) fEvent = new AliAODEvent();
116     
117     fEvent->ReadFromTree(fTree);
118     
119     if (fMixingHandler) fMixingHandler->Init(tree, opt);
120     
121     return kTRUE;
122 }
123
124 //______________________________________________________________________________
125 Bool_t AliAODInputHandler::BeginEvent(Long64_t entry)
126 {
127     // Begin event
128     TClonesArray* mcParticles = (TClonesArray*) (fEvent->FindListObject("mcparticles"));
129     if (mcParticles) fMCEvent->SetParticleArray(mcParticles);
130     if (fTreeToMerge) fTreeToMerge->GetEntry(entry + fMergeOffset);
131     
132     fIsSelectedResult = fEvent->GetHeader()->GetOfflineTrigger();
133
134     if (fMixingHandler) fMixingHandler->BeginEvent(entry);
135     
136     return kTRUE;
137 }
138
139 //______________________________________________________________________________
140 Bool_t AliAODInputHandler::Notify(const char* path)
141 {
142   // Notifaction of directory change
143   if (fMixingHandler) fMixingHandler->Notify(path);
144   if (!fFriendsConnected) {
145       ConnectFriends();
146       fEvent->ReadFromTree(fTree, "reconnect");
147   }
148   fFriendsConnected = kFALSE;
149     
150   TTree *ttree = fTree->GetTree();
151   if (!ttree) ttree = fTree;
152   TString statFname(ttree->GetCurrentFile()->GetName());
153   Int_t indarchive = statFname.Index("#");
154   if (indarchive<0) {
155      statFname = gSystem->DirName(statFname);
156      statFname += "/";
157   } else {
158      statFname.Remove(indarchive+1);
159   }   
160   statFname += "EventStat_temp.root";
161   TFile *statFile = 0;
162   if (IsCheckStatistics()) statFile = TFile::Open(statFname, "READ");
163   if (statFile) {
164      TList *list = (TList*)statFile->Get("cstatsout");
165      if (list) {
166         AliVCuts *physSel = (AliVCuts*)list->At(0);
167         if (physSel) {
168            TH2F *hAll = dynamic_cast<TH2F*>(physSel->GetStatistics("ALL"));
169            TH2F *hBin0 = dynamic_cast<TH2F*>(physSel->GetStatistics("BIN0"));
170            if (fHistStatistics[0] && hAll) {
171               TList tmplist;
172               tmplist.Add(hAll);
173               fHistStatistics[0]->Merge(&tmplist);
174               tmplist.Clear();
175               tmplist.Add(hBin0);
176               if (fHistStatistics[1] && hBin0) fHistStatistics[1]->Merge(&tmplist);
177            } else {
178              if (hAll && hBin0) {
179                 fHistStatistics[0] = static_cast<TH2F*>(hAll->Clone());
180                 fHistStatistics[1] = static_cast<TH2F*>(hBin0->Clone());
181                 fHistStatistics[0]->SetDirectory(0);
182                 fHistStatistics[1]->SetDirectory(0);
183              }   
184            }   
185         }
186         delete list;
187      }
188      delete statFile;
189   }
190   return kTRUE;
191 }
192
193 //______________________________________________________________________________
194 Bool_t AliAODInputHandler::FinishEvent()
195 {
196   // Finish event
197   if (fMixingHandler) fMixingHandler->FinishEvent();
198   return kTRUE;
199 }
200
201 //______________________________________________________________________________
202 void AliAODInputHandler::AddFriend(char* filename)
203 {
204     // Add a friend tree 
205     TNamed* obj = new TNamed(filename, filename);
206     fFriends->Add(obj);
207 }
208
209 //______________________________________________________________________________
210 Option_t *AliAODInputHandler::GetDataType() const
211 {
212 // Returns handled data type.
213    return gAODDataType;
214 }
215
216 //______________________________________________________________________________
217 TObject *AliAODInputHandler::GetStatistics(Option_t *option) const
218 {
219 // Get the statistics histogram(s) from the physics selection object. This
220 // should be called during FinishTaskOutput(). Option can be empty (default
221 // statistics histogram) or BIN0.
222    TString opt(option);
223    opt.ToUpper();
224    if (opt=="BIN0") return fHistStatistics[1];
225    return fHistStatistics[0];
226 }   
227
228 void AliAODInputHandler::ConnectFriends()
229 {
230     // Connect the friend trees 
231     if (!fMergeEvents) {
232         TIter next(fFriends);
233         TNamed* obj;
234         TString aodTreeFName,aodFriendTreeFName;
235         TTree *ttree = fTree->GetTree();
236         if (!ttree) ttree = fTree;
237         aodTreeFName = ttree->GetCurrentFile()->GetName();
238         
239         while((obj = (TNamed*)next())) {
240             aodFriendTreeFName = aodTreeFName;
241             aodFriendTreeFName.ReplaceAll("AliAOD.root",obj->GetName());
242             aodFriendTreeFName.ReplaceAll("AliAODs.root",obj->GetName());
243             ttree->AddFriend("aodTree", aodFriendTreeFName.Data());
244         }
245     } else {
246         // Friends have to be merged
247         TNamed* filename = (TNamed*) (fFriends->At(0));
248         fFileToMerge = TFile::Open(filename->GetName());
249         if (fFileToMerge) {
250             fFileToMerge->GetObject("aodTree", fTreeToMerge);
251             if (!fAODEventToMerge) fAODEventToMerge = new AliAODEvent();
252             fAODEventToMerge->ReadFromTree(fTreeToMerge);
253         }
254     }
255     fFriendsConnected = kTRUE;
256 }
257
258 //______________________________________________________________________________
259 void AliAODInputHandler::CreatePIDResponse(Bool_t isMC/*=kFALSE*/)
260 {
261   //
262   // create the pid response object if it does not exist yet
263   //
264   if (fAODpidUtil) return;
265   fAODpidUtil=new AliAODpidUtil(isMC);
266   
267 }
268