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