]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AOD/AliAODInputHandler.cxx
Changes for #89817: Commit calorimeter 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(0),
45     fFriends(0),
46     fAODpidUtil(0x0),
47     fMergeEvents(kFALSE),
48     fMergeTracks(kTRUE), 
49     fMergeEMCALClusters(kTRUE), 
50     fMergePHOSClusters(kTRUE), 
51     fMergeEMCALCells(kTRUE), 
52         fMergePHOSCells(kTRUE), 
53         fMergeEMCALTrigger(kTRUE), 
54         fMergePHOSTrigger(kTRUE),
55     fFriendsConnected(kFALSE),
56     fFileToMerge(0),
57     fTreeToMerge(0),
58     fAODEventToMerge(0),
59     fMergeOffset(0)
60 {
61   // Default constructor
62   fHistStatistics[0] = fHistStatistics[1] = NULL;
63 }
64
65 //______________________________________________________________________________
66 AliAODInputHandler::AliAODInputHandler(const char* name, const char* title):
67   AliInputEventHandler(name, title),
68   fEvent(0),
69   fMCEvent(new AliMCEvent()),
70   fFriends(new TList()),
71   fAODpidUtil(0x0),
72   fMergeEvents(kFALSE),
73   fMergeTracks(kTRUE), 
74   fMergeEMCALClusters(kTRUE), 
75   fMergePHOSClusters(kTRUE), 
76   fMergeEMCALCells(kTRUE), 
77   fMergePHOSCells(kTRUE),
78   fMergeEMCALTrigger(kTRUE), 
79   fMergePHOSTrigger(kTRUE),
80   fFriendsConnected(kFALSE),
81   fFileToMerge(0),
82   fTreeToMerge(0),
83   fAODEventToMerge(0),
84   fMergeOffset(0)
85 {
86     // Constructor
87   fHistStatistics[0] = fHistStatistics[1] = NULL;
88 }
89
90 //______________________________________________________________________________
91 AliAODInputHandler::~AliAODInputHandler() 
92 {
93 // Destructor
94   fFriends->Delete();
95   delete fHistStatistics[0];
96   delete fHistStatistics[1];
97   delete fAODpidUtil;
98 }
99
100 //______________________________________________________________________________
101 Bool_t AliAODInputHandler::Init(TTree* tree, Option_t* opt)
102 {
103     // Initialisation necessary for each new tree
104     fTree = tree;
105     if (!fTree) return kFALSE;
106     fTree->GetEntries();
107     ConnectFriends();
108
109     SwitchOffBranches();
110     SwitchOnBranches();
111     
112     // Get pointer to AOD event
113     if (!fEvent) fEvent = new AliAODEvent();
114     
115     fEvent->ReadFromTree(fTree);
116     
117     if (fMixingHandler) fMixingHandler->Init(tree, opt);
118     
119     return kTRUE;
120 }
121
122 //______________________________________________________________________________
123 Bool_t AliAODInputHandler::BeginEvent(Long64_t entry)
124 {
125     // Begin event
126     TClonesArray* mcParticles = (TClonesArray*) (fEvent->FindListObject("mcparticles"));
127     if (mcParticles) fMCEvent->SetParticleArray(mcParticles);
128
129     // When merging, get current event number from GetReadEntry(), 
130     // entry gives the events in the current file
131     if (fTreeToMerge) fTreeToMerge->GetEntry(GetReadEntry() + fMergeOffset);
132   
133     fIsSelectedResult = fEvent->GetHeader()->GetOfflineTrigger();
134
135     if (fMixingHandler) fMixingHandler->BeginEvent(entry);
136     
137     return kTRUE;
138 }
139
140 //______________________________________________________________________________
141 Bool_t AliAODInputHandler::Notify(const char* path)
142 {
143   // Notifaction of directory change
144   if (fMixingHandler) fMixingHandler->Notify(path);
145   if (!fFriendsConnected) {
146       ConnectFriends();
147       fEvent->ReadFromTree(fTree, "reconnect");
148   }
149   fFriendsConnected = kFALSE;
150     
151   TTree *ttree = fTree->GetTree();
152   if (!ttree) ttree = fTree;
153   TString statFname(ttree->GetCurrentFile()->GetName());
154   AliInfo(Form("Moving to file %s", statFname.Data()));
155   Int_t indarchive = statFname.Index("#");
156   if (indarchive<0) {
157      statFname = gSystem->DirName(statFname);
158      statFname += "/";
159   } else {
160      statFname.Remove(indarchive+1);
161   }   
162   statFname += "EventStat_temp.root";
163   TFile *statFile = 0;
164   if (IsCheckStatistics()) statFile = TFile::Open(statFname, "READ");
165   if (statFile) {
166      TList *list = (TList*)statFile->Get("cstatsout");
167      if (list) {
168         AliVCuts *physSel = (AliVCuts*)list->At(0);
169         if (physSel) {
170            TH2F *hAll = dynamic_cast<TH2F*>(physSel->GetStatistics("ALL"));
171            TH2F *hBin0 = dynamic_cast<TH2F*>(physSel->GetStatistics("BIN0"));
172            if (fHistStatistics[0] && hAll) {
173               TList tmplist;
174               tmplist.Add(hAll);
175               fHistStatistics[0]->Merge(&tmplist);
176               tmplist.Clear();
177               tmplist.Add(hBin0);
178               if (fHistStatistics[1] && hBin0) fHistStatistics[1]->Merge(&tmplist);
179            } else {
180              if (hAll && hBin0) {
181                 fHistStatistics[0] = static_cast<TH2F*>(hAll->Clone());
182                 fHistStatistics[1] = static_cast<TH2F*>(hBin0->Clone());
183                 fHistStatistics[0]->SetDirectory(0);
184                 fHistStatistics[1]->SetDirectory(0);
185              }   
186            }   
187         }
188         delete list;
189      }
190      delete statFile;
191   }
192   return kTRUE;
193 }
194
195 //______________________________________________________________________________
196 Bool_t AliAODInputHandler::FinishEvent()
197 {
198   // Finish event
199   if (fMixingHandler) fMixingHandler->FinishEvent();
200   if (fEvent) fEvent->Reset();
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