Bug corrected.
[u/mrichter/AliRoot.git] / STEER / AliAODInputHandler.cxx
CommitLineData
397596ed 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
3ba74948 23#include <TSystem.h>
933fd60f 24#include <TTree.h>
26772015 25#include <TList.h>
26#include <TNamed.h>
fb0cfe69 27#include <TFile.h>
70908d05 28#include <TH2.h>
397596ed 29
30#include "AliAODInputHandler.h"
31#include "AliAODEvent.h"
70908d05 32#include "AliVCuts.h"
397596ed 33
34ClassImp(AliAODInputHandler)
35
c2b6979d 36static Option_t *gAODDataType = "AOD";
37
397596ed 38//______________________________________________________________________________
39AliAODInputHandler::AliAODInputHandler() :
90e04a88 40 AliInputEventHandler(),
4195c9c9 41 fEvent(0),
04a79fa0 42 fMCEvent(new AliMCEvent()),
1f842495 43 fFriends(new TList()),
44 fMergeEvents(kFALSE),
45 fFileToMerge(0),
46 fTreeToMerge(0),
04daa8bf 47 fAODEventToMerge(0),
48 fMergeOffset(0)
397596ed 49{
26772015 50 // Default constructor
70908d05 51 fHistStatistics[0] = fHistStatistics[1] = NULL;
397596ed 52}
53
54//______________________________________________________________________________
26772015 55AliAODInputHandler::AliAODInputHandler(const char* name, const char* title):
56 AliInputEventHandler(name, title),
57 fEvent(0),
04a79fa0 58 fMCEvent(new AliMCEvent()),
1f842495 59 fFriends(new TList()),
60 fMergeEvents(kFALSE),
61 fFileToMerge(0),
62 fTreeToMerge(0),
04daa8bf 63 fAODEventToMerge(0),
64 fMergeOffset(0)
397596ed 65{
26772015 66 // Constructor
70908d05 67 fHistStatistics[0] = fHistStatistics[1] = NULL;
397596ed 68}
69
70//______________________________________________________________________________
26772015 71AliAODInputHandler::~AliAODInputHandler()
397596ed 72{
26772015 73// Destructor
70908d05 74 fFriends->Delete();
75 if (fHistStatistics[0]) {
76 delete fHistStatistics[0];
77 fHistStatistics[0] = 0;
78 }
79 if (fHistStatistics[1]) {
80 delete fHistStatistics[1];
81 fHistStatistics[1] = 0;
82 }
397596ed 83}
84
70908d05 85//______________________________________________________________________________
b890a10d 86Bool_t AliAODInputHandler::Init(TTree* tree, Option_t* opt)
397596ed 87{
300d5701 88 // Initialisation necessary for each new tree
70908d05 89 if (!fMergeEvents) {
90 fTree = tree;
91 TIter next(fFriends);
92 TNamed* obj;
1f842495 93
70908d05 94 if (!fTree) return kFALSE;
32e5a29c 95 fTree->GetEntries();
70908d05 96 fTree->GetEntry(0);
97 TString aodTreeFName,aodFriendTreeFName;
98 TTree *ttree = fTree->GetTree();
99 if (!ttree) ttree = fTree;
100 aodTreeFName = ttree->GetCurrentFile()->GetName();
101
102 while((obj = (TNamed*)next())) {
103 aodFriendTreeFName = aodTreeFName;
104 aodFriendTreeFName.ReplaceAll("AliAOD.root",obj->GetName());
105 aodFriendTreeFName.ReplaceAll("AliAODs.root",obj->GetName());
106 ttree->AddFriend("aodTree", aodFriendTreeFName.Data());
4195c9c9 107 }
70908d05 108 } else {
109 // Friends have to be merged
110 TNamed* filename = (TNamed*) (fFriends->At(0));
111 fFileToMerge = new TFile(filename->GetName());
112 if (fFileToMerge) {
113 fFileToMerge->GetObject("aodTree", fTreeToMerge);
114 if (!fAODEventToMerge) fAODEventToMerge = new AliAODEvent();
115 fAODEventToMerge->ReadFromTree(fTreeToMerge);
116 }
117 }
d329cad3 118
70908d05 119 SwitchOffBranches();
120 SwitchOnBranches();
4195c9c9 121
70908d05 122 // Get pointer to AOD event
123 if (!fEvent) fEvent = new AliAODEvent();
300d5701 124
70908d05 125 fEvent->ReadFromTree(fTree);
126
127 if (fMixingHandler) fMixingHandler->Init(tree, opt);
04a79fa0 128
70908d05 129 return kTRUE;
397596ed 130}
131
70908d05 132//______________________________________________________________________________
1f842495 133Bool_t AliAODInputHandler::BeginEvent(Long64_t entry)
fbb264e0 134{
135 //
04a79fa0 136 TClonesArray* mcParticles = (TClonesArray*) (fEvent->FindListObject("mcparticles"));
137 if (mcParticles) fMCEvent->SetParticleArray(mcParticles);
04daa8bf 138 if (fTreeToMerge) fTreeToMerge->GetEntry(entry + fMergeOffset);
1f842495 139
0c6c629b 140 fIsSelectedResult = fEvent->GetHeader()->GetOfflineTrigger();
b890a10d 141
142 if (fMixingHandler) fMixingHandler->BeginEvent(entry);
0c6c629b 143
fbb264e0 144 return kTRUE;
145}
146
70908d05 147//______________________________________________________________________________
b890a10d 148Bool_t AliAODInputHandler::Notify(const char* path)
149{
150 // Notifaction of directory change
151 if (fMixingHandler) fMixingHandler->Notify(path);
70908d05 152 TTree *ttree = fTree->GetTree();
153 if (!ttree) ttree = fTree;
3ba74948 154 TString statFname(gSystem->DirName(ttree->GetCurrentFile()->GetName()));
155 statFname += "/EventStat_temp.root";
70908d05 156 TFile *statFile = TFile::Open(statFname, "READ");
157 if (statFile) {
158 TList *list = (TList*)statFile->Get("cstatsout");
159 if (list) {
160 AliVCuts *physSel = (AliVCuts*)list->At(0);
161 if (physSel) {
162 TH2F *hAll = dynamic_cast<TH2F*>(physSel->GetStatistics("ALL"));
163 TH2F *hBin0 = dynamic_cast<TH2F*>(physSel->GetStatistics("BIN0"));
164 if (fHistStatistics[0] && hAll) {
165 TList tmplist;
166 tmplist.Add(hAll);
167 fHistStatistics[0]->Merge(&tmplist);
168 tmplist.Clear();
169 tmplist.Add(hBin0);
170 if (fHistStatistics[1] && hBin0) fHistStatistics[1]->Merge(&tmplist);
171 } else {
3ba74948 172 fHistStatistics[0] = static_cast<TH2F*>(hAll->Clone());
173 fHistStatistics[1] = static_cast<TH2F*>(hBin0->Clone());
174 fHistStatistics[0]->SetDirectory(0);
175 fHistStatistics[1]->SetDirectory(0);
70908d05 176 }
177 }
3ba74948 178 delete list;
70908d05 179 }
b8047d30 180 delete statFile;
70908d05 181 }
b890a10d 182 return kTRUE;
183}
184
70908d05 185//______________________________________________________________________________
b890a10d 186Bool_t AliAODInputHandler::FinishEvent()
187{
188 // Finish event
189 if (fMixingHandler) fMixingHandler->FinishEvent();
190 return kTRUE;
191}
192
70908d05 193//______________________________________________________________________________
26772015 194void AliAODInputHandler::AddFriend(char* filename)
195{
196 // Add a friend tree
197 TNamed* obj = new TNamed(filename, filename);
198 fFriends->Add(obj);
199}
c2b6979d 200
70908d05 201//______________________________________________________________________________
c2b6979d 202Option_t *AliAODInputHandler::GetDataType() const
203{
204// Returns handled data type.
205 return gAODDataType;
206}
70908d05 207
208//______________________________________________________________________________
209TObject *AliAODInputHandler::GetStatistics(Option_t *option) const
210{
211// Get the statistics histogram(s) from the physics selection object. This
212// should be called during FinishTaskOutput(). Option can be empty (default
213// statistics histogram) or BIN0.
214 TString opt(option);
215 opt.ToUpper();
216 if (opt=="BIN0") return fHistStatistics[1];
217 return fHistStatistics[0];
218}