]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AOD/AliAODInputHandler.cxx
Moving the classes that belong to the following libraries: STEERBase, ESD, CDB, AOD...
[u/mrichter/AliRoot.git] / STEER / AOD / 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
131     // When merging, get current event number from GetReadEntry(), 
132     // entry gives the events in the current file
133     if (fTreeToMerge) fTreeToMerge->GetEntry(GetReadEntry() + fMergeOffset);
134   
135     fIsSelectedResult = fEvent->GetHeader()->GetOfflineTrigger();
136
137     if (fMixingHandler) fMixingHandler->BeginEvent(entry);
138     
139     return kTRUE;
140 }
141
142 //______________________________________________________________________________
143 Bool_t AliAODInputHandler::Notify(const char* path)
144 {
145   // Notifaction of directory change
146   if (fMixingHandler) fMixingHandler->Notify(path);
147   if (!fFriendsConnected) {
148       ConnectFriends();
149       fEvent->ReadFromTree(fTree, "reconnect");
150   }
151   fFriendsConnected = kFALSE;
152     
153   TTree *ttree = fTree->GetTree();
154   if (!ttree) ttree = fTree;
155   TString statFname(ttree->GetCurrentFile()->GetName());
156   Int_t indarchive = statFname.Index("#");
157   if (indarchive<0) {
158      statFname = gSystem->DirName(statFname);
159      statFname += "/";
160   } else {
161      statFname.Remove(indarchive+1);
162   }   
163   statFname += "EventStat_temp.root";
164   TFile *statFile = 0;
165   if (IsCheckStatistics()) statFile = TFile::Open(statFname, "READ");
166   if (statFile) {
167      TList *list = (TList*)statFile->Get("cstatsout");
168      if (list) {
169         AliVCuts *physSel = (AliVCuts*)list->At(0);
170         if (physSel) {
171            TH2F *hAll = dynamic_cast<TH2F*>(physSel->GetStatistics("ALL"));
172            TH2F *hBin0 = dynamic_cast<TH2F*>(physSel->GetStatistics("BIN0"));
173            if (fHistStatistics[0] && hAll) {
174               TList tmplist;
175               tmplist.Add(hAll);
176               fHistStatistics[0]->Merge(&tmplist);
177               tmplist.Clear();
178               tmplist.Add(hBin0);
179               if (fHistStatistics[1] && hBin0) fHistStatistics[1]->Merge(&tmplist);
180            } else {
181              if (hAll && hBin0) {
182                 fHistStatistics[0] = static_cast<TH2F*>(hAll->Clone());
183                 fHistStatistics[1] = static_cast<TH2F*>(hBin0->Clone());
184                 fHistStatistics[0]->SetDirectory(0);
185                 fHistStatistics[1]->SetDirectory(0);
186              }   
187            }   
188         }
189         delete list;
190      }
191      delete statFile;
192   }
193   return kTRUE;
194 }
195
196 //______________________________________________________________________________
197 Bool_t AliAODInputHandler::FinishEvent()
198 {
199   // Finish event
200   if (fMixingHandler) fMixingHandler->FinishEvent();
201   return kTRUE;
202 }
203
204 //______________________________________________________________________________
205 void AliAODInputHandler::AddFriend(char* filename)
206 {
207     // Add a friend tree 
208     TNamed* obj = new TNamed(filename, filename);
209     fFriends->Add(obj);
210 }
211
212 //______________________________________________________________________________
213 Option_t *AliAODInputHandler::GetDataType() const
214 {
215 // Returns handled data type.
216    return gAODDataType;
217 }
218
219 //______________________________________________________________________________
220 TObject *AliAODInputHandler::GetStatistics(Option_t *option) const
221 {
222 // Get the statistics histogram(s) from the physics selection object. This
223 // should be called during FinishTaskOutput(). Option can be empty (default
224 // statistics histogram) or BIN0.
225    TString opt(option);
226    opt.ToUpper();
227    if (opt=="BIN0") return fHistStatistics[1];
228    return fHistStatistics[0];
229 }   
230
231 void AliAODInputHandler::ConnectFriends()
232 {
233     // Connect the friend trees 
234     if (!fMergeEvents) {
235         TIter next(fFriends);
236         TNamed* obj;
237         TString aodTreeFName,aodFriendTreeFName;
238         TTree *ttree = fTree->GetTree();
239         if (!ttree) ttree = fTree;
240         aodTreeFName = ttree->GetCurrentFile()->GetName();
241         
242         while((obj = (TNamed*)next())) {
243             aodFriendTreeFName = aodTreeFName;
244             aodFriendTreeFName.ReplaceAll("AliAOD.root",obj->GetName());
245             aodFriendTreeFName.ReplaceAll("AliAODs.root",obj->GetName());
246             ttree->AddFriend("aodTree", aodFriendTreeFName.Data());
247         }
248     } else {
249         // Friends have to be merged
250         TNamed* filename = (TNamed*) (fFriends->At(0));
251         fFileToMerge = TFile::Open(filename->GetName());
252         if (fFileToMerge) {
253             fFileToMerge->GetObject("aodTree", fTreeToMerge);
254             if (!fAODEventToMerge) fAODEventToMerge = new AliAODEvent();
255             fAODEventToMerge->ReadFromTree(fTreeToMerge);
256         }
257     }
258     fFriendsConnected = kTRUE;
259 }
260
261 //______________________________________________________________________________
262 void AliAODInputHandler::CreatePIDResponse(Bool_t isMC/*=kFALSE*/)
263 {
264   //
265   // create the pid response object if it does not exist yet
266   //
267   if (fAODpidUtil) return;
268   fAODpidUtil=new AliAODpidUtil(isMC);
269   
270 }
271